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

问题描述 投票:-1回答:2

这显示了一些漫画(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;
    };
};
c++ char stack
2个回答
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中,显示字符表与ascii表的不同之处在于0到31之间的字符。例如,如果打印整数​​0,您将从主板扬声器发出蜂鸣声。 1你得到一张笑脸。检查这个Table获取更多信息。

你必须输入char values到你的程序,如'2'正确打印它们。


1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.