如何在C中使用数组在堆栈中存储字符串? #包括 #包括 #包括 #定义最大值100 int a[max]; int top=-1; 字符x; int是空的(); int isfull(); 无效推送(); int-pop(); void display(); void main() { int-ch; 做 { printf(“\n 1.Push”); printf(“\n 2.Pop”); printf(“\n 3.显示”); printf(“\n 4.退出”); scanf(“%d”和“ch”); 开关(ch){ 案例1: 推(); 打破 案例2: pop(); 打破 案例3: 显示(); 打破 案例4: 打破 违约: printf(“无效选择”); 打破 } }while(ch!=4) } int isfull(){ 如果((顶部==最大值-1)) { 返回1; } 其他的 { 返回0; } } int isempty(){ 如果((顶部==-1)) { 返回1; } 其他的 { 返回0; } } 无效推送(){ 如果(isfull()) { printf(“堆栈已满”); } 其他的 { printf(“输入要添加的元素”); scanf(“%s”,x); top++; strcpy(a[顶部],x); } } int-pop(){ if(isempty()) { printf(“堆栈为空”); 出口(0); } 否则{ strcpy(x,a[顶部]); printf(“%s”,x); 顶部--; } } 无效显示() { if(isempty()) { printf(“无数据显示”); 出口(0); } 其他的 { int i; 对于(i=0;i
此答案提供了OP在使用上面发布的代码时遇到的所有问题。解决方案的第一部分提供了对这些错误的分析以及如何处理这些错误。第二部分提供了OP想要实现的解决方案。(将字符串存储在堆栈中) 您正在访问一个未初始化的变量,并将其垃圾值与如何在C中使用数组在堆栈中存储字符串? #包括 #包括 #包括 #定义最大值100 int a[max]; int top=-1; 字符x; int是空的(); int isfull(); 无效推送(); int-pop(); void display(); void main() { int-ch; 做 { printf(“\n 1.Push”); printf(“\n 2.Pop”); printf(“\n 3.显示”); printf(“\n 4.退出”); scanf(“%d”和“ch”); 开关(ch){ 案例1: 推(); 打破 案例2: pop(); 打破 案例3: 显示(); 打破 案例4: 打破 违约: printf(“无效选择”); 打破 } }while(ch!=4) } int isfull(){ 如果((顶部==最大值-1)) { 返回1; } 其他的 { 返回0; } } int isempty(){ 如果((顶部==-1)) { 返回1; } 其他的 { 返回0; } } 无效推送(){ 如果(isfull()) { printf(“堆栈已满”); } 其他的 { printf(“输入要添加的元素”); scanf(“%s”,x); top++; strcpy(a[顶部],x); } } int-pop(){ if(isempty()) { printf(“堆栈为空”); 出口(0); } 否则{ strcpy(x,a[顶部]); printf(“%s”,x); 顶部--; } } 无效显示() { if(isempty()) { printf(“无数据显示”); 出口(0); } 其他的 { int i; 对于(i=0;i,c,arrays,stack,C,Arrays,Stack,此答案提供了OP在使用上面发布的代码时遇到的所有问题。解决方案的第一部分提供了对这些错误的分析以及如何处理这些错误。第二部分提供了OP想要实现的解决方案。(将字符串存储在堆栈中) 您正在访问一个未初始化的变量,并将其垃圾值与4进行比较。我建议while #include <stdio.h> #include<string.h> #include<stdlib.h> #define max 100 int a[max]; int top = -1; char
4
进行比较。我建议while
#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]);
}
}
}
这个想法很简单。我们在ch
中得到一个输入,然后做所有的工作。比较是在所有工作结束时进行的,因此我们总是使用一个确定的ch
值
在前面的问题中,ch
中的垃圾值似乎不等于4
。这就是它甚至没有进入循环的原因
您已经将程序中的每一个变量都设置为全局变量。当您遇到一个我们必须跟踪数据变化的bug时,您就会明白调试过程中的真正痛苦。不要像这样使用全局变量
重大错误
不能将字符串存储在int数组中
do{
printf("\n 1. Push");
printf("\n 2. Pop");
printf("\n 3. Display");
printf("\n 4. Exit");
scanf("%d",&ch);
....
}while(ch!=4);
您正在使用%s
说明符输入字符。您应该使用%c
。然后,您也不能使用strcpy()
复制它
此外,您不需要strcpy(),只需使用aissgnment即可。
您可以将char
存储在int
变量和int
数组中
更正的代码将是(push()
function):
在pop()
函数中使用相同的方法
if( scanf(" %c",&x) == 1){
a[++top]=x;
}
另外,在display()
函数中,您需要进行更改
x = a[top--];
printf("%c",x);
同样,如果您确定只使用字符,为什么不使用char
数组呢
这里字符a[100]
非常适合
这些是程序生成的错误/警告列表(不使用任何标志)
使用alk所说的标志,错误和警告会更多
为了存储字符串,需要考虑使用2D char数组。这是实现字符串的一种方式。
代码 (用于说明。仅合并更改。代码具有最小/无错误检查。)#包括
#包括
#包括
#定义最大值100
字符a[max][max];
int top=-1;
字符x[max];
int是空的();
int isfull();
无效推送();
int-pop();
void display();
int main()
{
int-ch;
做
{
printf(“\n 1.Push”);
printf(“\n 2.Pop”);
printf(“\n 3.显示”);
printf(“\n 4.Exit\n”);
scanf(“%d”和“ch”);
开关(ch){
案例1:
推();
打破
案例2:
pop();
打破
案例3:
显示();
打破
案例4:
打破
违约:
printf(“无效选择”);
打破
}
}而(ch!=4);
返回0;
}
int isfull(){
如果((顶部==最大值-1))
{
返回1;
}
其他的
{
返回0;
}
}
int isempty(){
如果((顶部==-1))
{
返回1;
}
其他的
{
返回0;
}
}
无效推送(){
如果(isfull())
{
printf(“堆栈已满”);
}
其他的
{
printf(“输入要添加的元素”);
scanf(“%s”,x);
top++;
strcpy(a[顶部],x);
}
}
int-pop(){
if(isempty())
{
printf(“堆栈为空”);
出口(0);
}
否则{
strcpy(x,a[顶部]);
printf(“%s”,x);
顶部--;
}
}
无效显示()
{
if(isempty())
{
printf(“无数据显示”);
出口(0);
}
其他的
{
int i;
对于(i=0;iint-ch;而(ch!=4)
正在访问本地未初始化变量…建议进行任何更改吗?使用do-while
。仍然存在相同的问题。在给出注释/答案后不要更改代码,因为这会使它们无法理解。添加更新。我回滚了您上次的更改。在使用do-while循环时,也会出现相同的错误和错误不执行任何操作。只要在您的系统中尝试一次此代码,您就会明白。现在它甚至不要求输入,请检查更新。@Vivank:“您是否正确复制了它?因为它对我有效…您是否检查了scanf
@Vivank中的'
。:如果您不知道如何复制更改并理解那不是我的错。答案为程序正常运行提供了一切
x = a[top--];
printf("%c",x);
printf("%c \n",a[i]);
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 } ^
#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]);
}
}
}