我究竟做错了什么?堆栈与字符图形(C ++)

时间:2013-11-16 07:03:56

标签: c++ char stack

这显示了一些漫画(XD:D)而不是int值。我只想要堆栈为空的空格,所以我传递空字符串。我做错了什么???顺便说一下,我用简单的字符显示图形,而不是图形库。

#include <iostream>
using namespace std;
void pushh(int);
void pop(int);

int val;
char stack[4];
char a[1]="";

int main()
{

    cout<<"                    Stack Graphic representation\n\n\n\n\n";
    for(int i=0;i<4;i++)
            stack[i]=a[1];    
    cout<<endl;

    cout<<"\t\t\t4   |"<<stack[3]<<" |"<<endl;
    cout<<"\t\t\t3   |"<<stack[2]<<" |"<<endl;
    cout<<"\t\t\t2   |"<<stack[2]<<" |"<<endl;
    cout<<"\t\t\t1   |"<<stack[0]<<" |"<<endl;
    cout<<"\t\t\t    |___|"<<endl<<endl<<endl<<endl;
    pushh(1);
    cout<<"\t\t\t4   |"<<stack[3]<<" |"<<endl;
    cout<<"\t\t\t3   |"<<stack[2]<<" |"<<endl;
    cout<<"\t\t\t2   |"<<stack[1]<<" |"<<endl;
    cout<<"\t\t\t1   | "<<stack[0]<<" |"<<endl;
    cout<<"\t\t\t    |___|"<<endl<<endl<<endl<<endl;

    pushh(2);
    cout<<"\t\t\t4   |"<<stack[3]<<" |"<<endl;
    cout<<"\t\t\t3   |"<<stack[2]<<" |"<<endl;
    cout<<"\t\t\t2   | "<<stack[1]<<" |"<<endl;
    cout<<"\t\t\t1   | "<<stack[0]<<" |"<<endl;
    cout<<"\t\t\t    |___|"<<endl<<endl<<endl<<endl;
    pushh(3);
    cout<<"\t\t\t4   |"<<stack[3]<<" |"<<endl;
    cout<<"\t\t\t3   | "<<stack[2]<<" |"<<endl;
    cout<<"\t\t\t2   | "<<stack[1]<<" |"<<endl;
    cout<<"\t\t\t1   | "<<stack[0]<<" |"<<endl;
    cout<<"\t\t\t    |___|"<<endl<<endl<<endl<<endl;
    pushh(4);
    cout<<"\t\t\t4   | "<<stack[3]<<" |"<<endl;
    cout<<"\t\t\t3   | "<<stack[2]<<" |"<<endl;
    cout<<"\t\t\t2   | "<<stack[1]<<" |"<<endl;
    cout<<"\t\t\t1   | "<<stack[0]<<" |"<<endl;
    cout<<"\t\t\t    |___|"<<endl<<endl<<endl<<endl;
    system ("pause");
    return 0;
};
void pushh(int val)
{
    for(int i=0;i<4;i++)
    {
        if(stack[i]==a[1])
        {
            stack[i]=val;
            break;
        }
        //break;
    };
};

2 个答案:

答案 0 :(得分:1)

请检查函数pushh()的参数,ascii代码1是SOH,这是一个特殊字符。 您的程序旨在在堆栈数组的所有索引中进行值插入。 通过:http://ascii.cl/并检查您的问题陈述是否与解决方案匹配。

答案 1 :(得分:0)

如果您想绘制“黑色空格”,请使用空格" "而不是空字符串""

- EDIT-- 也在你的代码的顶部,

cout<<"\t\t\t3   |"<<stack[2]<<" |"<<endl;
cout<<"\t\t\t2   |"<<stack[2]<<" |"<<endl;

但我想你想在stack[1]下面显示stack[2],就像这样,

cout<<"\t\t\t3   |"<<stack[2]<<" |"<<endl;
cout<<"\t\t\t2   |"<<stack[1]<<" |"<<endl;

- 编辑 -

在Windows中,对于0到31之间的字符,显示字符表与ascii表不同。例如,如果打印整数​​0,您将从主板扬声器发出蜂鸣声。 1你得到一张笑脸。请查看此Table以获取更多信息。

您必须在char values之类的程序中输入'2'才能正确打印。