吠错了树 2018-12-06 09:37 采纳率: 0%
浏览 338

请教为什么第二个Print是乱码?如何改正?

#include
#include
#include
#define OK 1
#define ERROR 0
#define Stack_Size 20//定义栈最多能够存储的元素个数
typedef struct
{
int stacksize//当前已分配储存已使用空间
int *base//栈底指针
int *top;//整型变量top存储栈顶元素的下标,作为栈顶指针,top为-1时表示空栈
}SeqStack;

int InitStack(SeqStack );//初始化栈,即将一个栈清除为空
int StackEmpty(SeqStack *);//检查一个栈是否为空
int pop(SeqStack
,int*);//向一个栈中删除元素
int push(SeqStack , int);//向一个栈中插入元素
void Print(SeqStack
);//输出栈

int main()
{
SeqStack p = (SeqStack)malloc(sizeof(SeqStack)), q = (SeqStack)malloc(sizeof(SeqStack));
InitStack(p);
InitStack(q);
srand(time(NULL));
int i = 0, x, A[100];
cout << "随机生成100个要入栈的数字...." << endl;
while (i < 100)
{
cin.unget();
A[i] = rand() % 200;
Push(p, A[i++]);
}
cout << "打印第一个栈的内容:" << endl;
for (i = 0; i < p->sacksize; i++)
{
printf("%-4d", A[i]);
if ((i + 1) % 10 == 0)cout << endl;
}
system("pause");
cout << "打印第二个栈的内容:" << endl;
for (i = 0; p->sacksize != 0; i++)
{
Pop(p, &x);
Push(q, x);
printf("%-4d", x);
if ((i + 1) % 10 == 0)cout << endl;
}
system("pause");
}
int InitStack(SeqStack S)
{
s->base = (int
)malloc(Stack_Size * sizeof(int));
s->top = s->base;
s->sacksize = 0;
}
int StackEmpty(SeqStack *S)
{
return(s->top == s->base ? OK: ERROR);
}
int pop(SeqStack *S, int *x)
{
if (s->sacksize == 0)
return FALSE;
else
{
*x = *(--s->top);
*(s->top) = NULL;
s->sacksize--;
}
return TRUE;

}
int push(SeqStack *S, int x)
{
if (Stack_Size == s->sacksize)
return FALSE;
else
{
*(s->top++) = e;
s->sacksize++;
}
return TRUE;
}
void Print(SeqStack *S)
{
printf("输出元素:\n");
for (int i = S->top; i >=S->base; i--)
{
printf("%d", S->elem[i]);
}
printf("\n");
}

  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥15 乘性高斯噪声在深度学习网络中的应用
    • ¥15 运筹学排序问题中的在线排序
    • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
    • ¥30 求一段fortran代码用IVF编译运行的结果
    • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
    • ¥15 C++ 头文件/宏冲突问题解决
    • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
    • ¥50 安卓adb backup备份子用户应用数据失败
    • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
    • ¥30 python代码,帮调试,帮帮忙吧