这也不是根据lifo原则删除
静态堆栈实现: 它没有第二次取名 这是新代码现在告诉我为什么它不起作用 请帮忙
typedef struct student {
char name[20];
int roll;
int age;
} mystruct;
#define size 40
int top;
static mystruct s[size];
void push()
{
if (top == size - 1) {
printf("\noverflow"); //
} else {
printf("\nenter the name of the student");
gets(s[top].name);//not taking name for d 2 time
printf("\nenter the roll number");
scanf("%d", &s[top].roll);
printf("\nenter the age of the student");
scanf("%d", &s[top].age);
++top;
}
}
void pop()
{
if (top == -1)
{
printf("\nunderflow");
} else {
printf("%s", s[top].name);
printf("%d", s[top].roll);
printf("%d", s[top].age);
printf("\npopped");
--top;
}
}
void display()
{
int i;
if (top == -1) {
printf("\nstack is empty");
} else {
for (i = top; i > 0; i--) {
printf("\nthe name of the student is%s", s[top].name);
}
printf("\nthe roll no of the student is%d", s[top].roll);
printf("\nthe age of the student is%d", s[top].age);
}
}
main()
{
top = -1;
char ch;
while (1) {
printf("\nwelcome to static stack menu");
printf("\n1.PUSH\n2.POP\n3.DISPLAY\n0.EXIT");
printf("\nplease enter your choice\n");
ch = getche();
if (ch == '0') {
break;
}
switch (ch) {
case '1':
push();
break;
case '2':
pop();
break;
case '3':
display();
break;
default:
printf("choice not valid");
break;
}
}
}
答案 0 :(得分:1)
我注意到的第一个问题是top
被初始化为-1
。当s[top]
初始化为top
时,尝试访问-1
的成员数据会导致无法预测的行为。
我建议更改一行
top = -1;
到
top = 0;
这改变了您在push
,pop
和display
中关于堆栈何时为空以及何时已满的基本假设。您现在必须检查if ( top == -1 )
,而不是检查if (top == 0 )
。您现在必须检查if ( top == size - 1 )
。
if ( top == size )
在pop
中,您必须使用top-1
代替top
。
for
中的display
循环未正确确定范围。你需要使用:
for (i = top-1; i >= 0; i--) {
printf("\nthe name of the student is %s", s[i].name);
printf("\nthe roll no of the student is %d", s[i].roll);
printf("\nthe age of the student is %d", s[i].age);
}
此外,阅读菜单选项并阅读后续输入有点棘手。
阅读菜单选项后,您必须确保在下一个换行符之前占用所有输入。否则,gets()
将读取菜单选项后的所有内容,直到该行结束。如果为菜单键入1
,然后键入Return / Enter,则名称将自动接受为“\ n”。因此,我建议行:
printf("\nwelcome to static stack menu");
printf("\n1.PUSH\n2.POP\n3.DISPLAY\n0.EXIT");
printf("\nplease enter your choice\n");
ch = fgetc(stdin);
/* Skip till the end of line is read. */
while ( fgetc(stdin) != '\n' );
此外,在您阅读了对象的年龄后,您必须将所有内容都吃到换行符。否则,读入换行符作为下一个菜单选项的选择。
scanf("%d", &s[top].age);
/* Skip till the end of line is read. */
while ( fgetc(stdin) != '\n' );
这是完整的文件。我已将gets
替换为fgets
而将getche
替换为fgetc
。
#include <stdio.h>
#include <string.h>
typedef struct student {
char name[20];
int roll;
int age;
} mystruct;
#define size 40
int top;
static mystruct s[size];
void push()
{
if (top == size) {
printf("\noverflow"); //
} else {
printf("\nenter the name of the student: ");
fgets(s[top].name, 20, stdin);//not taking name for d 2 time
// The newline character is part of s[top].name when fgets is
// finished. Remove that.
s[top].name[strlen(s[top].name)-1] = '\0';
printf("\nenter the roll number: ");
scanf("%d", &s[top].roll);
printf("\nenter the age of the student: ");
scanf("%d", &s[top].age);
/* Skip till the end of line is read. */
while ( fgetc(stdin) != '\n' );
++top;
}
}
void pop()
{
if (top == 0)
{
printf("\nunderflow");
} else {
printf("%s, ", s[top-1].name);
printf("%d, ", s[top-1].roll);
printf("%d", s[top-1].age);
printf("\npopped");
--top;
}
}
void display()
{
int i;
if (top == 0) {
printf("\nstack is empty");
} else {
for (i = top-1; i >= 0; i--) {
printf("\nthe name of the student is %s", s[i].name);
printf("\nthe roll no of the student is %d", s[i].roll);
printf("\nthe age of the student is %d", s[i].age);
}
}
}
main()
{
top = 0;
char ch;
while (1) {
printf("\nwelcome to static stack menu");
printf("\n1.PUSH\n2.POP\n3.DISPLAY\n0.EXIT");
printf("\nplease enter your choice\n");
ch = fgetc(stdin);
/* Skip till the end of line is read. */
while ( fgetc(stdin) != '\n' );
if (ch == '0') {
break;
}
switch (ch) {
case '1':
push();
break;
case '2':
pop();
break;
case '3':
display();
break;
default:
printf("choice, %c, not valid", ch);
break;
}
}
}
答案 1 :(得分:0)
您需要将getche()
更改为getchar()
注意:getche()
是非标准功能。
答案 2 :(得分:0)
也许这会有用http://www.delorie.com/djgpp/doc/libc/libc_385.html 注意实施说明: “如果你可以在程序启动后第一次调用其中一个conio函数时检测到这种情况,你可以通过手动调用gppconio_init函数解决这个问题” 或者只是用getchar()替换它。并且包括了conio conio。