#include <stdio.h>
#include<string.h>
#include<stdlib.h>
#define max 100
int a[max];
int top = -1;
char x;
int isempty() ;
int isfull() ;
void push() ;
int pop() ;
void display() ;
void main()
{
int ch;
do
{
printf("\n 1. Push");
printf("\n 2. Pop");
printf("\n 3. Display");
printf("\n 4. Exit");
scanf("%d",&ch);
switch (ch) {
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
break;
default:
printf("Invalid Choice");
break;
}
}while(ch!=4)
}
int isfull(){
if ( (top == max-1))
{
return 1;
}
else
{
return 0;
}
}
int isempty(){
if((top==-1))
{
return 1;
}
else
{
return 0;
}
}
void push(){
if (isfull())
{
printf("Stack is full");
}
else
{
printf("Enter element to add");
scanf("%s",x);
top++;
strcpy(a[top],x);
}
}
int pop(){
if(isempty())
{
printf("Stack is empty");
exit(0);
}
else{
strcpy(x,a[top]);
printf("%s",x);
top --;
}
}
void display()
{
if(isempty())
{
printf("NO data to display");
exit(0);
}
else
{
int i;
for(i=0;i<top+1;i++)
{
printf("%s \n",a[i]);
}
}
}
在运行推送操作时,它会以非零错误退出,而不执行任何操作。
我们必须使用数组以及入栈和出栈将字母数字值添加到堆栈中。 它应该接受一个字符串值并将其存储在堆栈数组中,当选择显示时它应该显示所有值。
有人可以帮助我纠正此代码中的错误,即为什么它没有向字符串分配或存储任何值。
最佳答案
这个答案提供了OP在使用上面发布的代码时遇到的所有问题。解决方案的第一部分提供了对这些错误的分析以及如何处理它们。第二部分提供了OP想要实现的解决方案。 (将字符串存储在堆栈中)。
您正在访问一个未初始化的变量并将其垃圾值与4
进行比较。我建议do-while
。
do{
printf("\n 1. Push");
printf("\n 2. Pop");
printf("\n 3. Display");
printf("\n 4. Exit");
scanf("%d",&ch);
....
}while(ch!=4);
这个想法很简单。我们正在 ch 中获取输入并完成所有工作。比较是在所有工作结束时完成的,因此我们始终使用 ch
的确定值。
之前从您的问题来看,ch
中的垃圾值不等于4
。这就是为什么它甚至没有进入循环。
您已将程序中的每个变量设置为全局变量。当你遇到一个需要跟踪数据突变的错误时,你就会明白调试过程中的真正痛苦。 不要使用这样的全局变量。
严重错误
永远不能将字符串存储在 int 数组中。
scanf("%s",x); This is wrong.
您正在输入带有 %s
说明符的字符。您应该使用%c
。另外,您也不能使用 strcpy()
来复制它。
此外,您不需要 strcpy()
您可以简单地使用 aissgnment 来完成它。
您可以将 char
存储在 int
变量和 int
数组中。
更正后的代码为(push()
函数):
if( scanf(" %c",&x) == 1){
a[++top]=x;
}
与您在 pop()
函数中使用的方式相同
x = a[top--];
printf("%c",x);
此外,您还需要更改 display()
函数
printf("%c \n",a[i]);
再说一次,如果您确定只使用字符,为什么不使用 char
数组。
此处 char a[100]
非常适合。
这些也是您的程序生成的错误/警告列表(不使用任何标志)
main.c: In function ‘main’: main.c:48:1: error: expected ‘;’ before ‘}’ token } ^ main.c: In function ‘pop’: main.c:93:12: warning: passing argument 1 of ‘strcpy’ makes pointer from integer without a cast [-Wint-conversion]
strcpy(x,a[top]);
^ In file included from main.c:2:0: /usr/include/string.h:125:14: note: expected ‘char * restrict’ but argument is of type ‘char’ extern char *strcpy (char *__restrict
__dest, const char *__restrict __src)
^~~~~~ main.c:93:14: warning: passing argument 2 of ‘strcpy’ makes pointer from integer without a cast [-Wint-conversion]
strcpy(x,a[top]);
^ In file included from main.c:2:0: /usr/include/string.h:125:14: note: expected ‘const char * restrict’ but argument is of type ‘int’ extern char *strcpy (char *__restrict
__dest, const char *__restrict __src)
^~~~~~ main.c: In function ‘main’: main.c:114:1: error: expected declaration or statement at end of input } ^
使用 alk 所说的标志,错误和警告将会更多。
<小时/>为了存储字符串,您需要考虑使用二维字符数组。这是实现字符串的一种方法。
代码
(出于说明目的。仅合并了更改。代码具有最少/没有错误检查。)
#include <stdio.h>
#include<string.h>
#include<stdlib.h>
#define max 100
char a[max][max];
int top = -1;
char x[max];
int isempty() ;
int isfull() ;
void push() ;
int pop() ;
void display() ;
int main()
{
int ch;
do
{
printf("\n 1. Push");
printf("\n 2. Pop");
printf("\n 3. Display");
printf("\n 4. Exit\n");
scanf("%d",&ch);
switch (ch) {
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
break;
default:
printf("Invalid Choice");
break;
}
}while(ch!=4);
return 0;
}
int isfull(){
if ( (top == max-1))
{
return 1;
}
else
{
return 0;
}
}
int isempty(){
if((top==-1))
{
return 1;
}
else
{
return 0;
}
}
void push(){
if (isfull())
{
printf("Stack is full");
}
else
{
printf("Enter element to add");
scanf("%s",x);
top++;
strcpy(a[top],x);
}
}
int pop(){
if(isempty())
{
printf("Stack is empty");
exit(0);
}
else{
strcpy(x,a[top]);
printf("%s",x);
top--;
}
}
void display()
{
if(isempty())
{
printf("NO data to display");
exit(0);
}
else
{
int i;
for(i=0;i<top+1;i++)
{
printf("%s \n",a[i]);
}
}
}
在此解决方案中,我们使用了两个主要更改
说明
将 char x
更改为 char x[max]
并将 a
从 int [max]
更改为char a[max][max]
。现在这样做可以存储以 null 结尾的 char 数组。这里使用二维字符数组来实现堆栈。
关于c - 如何在C中使用数组将字符串存储在堆栈中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47738873/