#include <stdio.h>
#include<stdlib.h>
char push();
char pop();
char display();
char peek();
char reverse();
# define MAX 50
struct stack
{
char string[MAX];
int top= -1;
};
void main()
{
int a;
printf("Choose an option\n");
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Display\n");
printf("4.Peek\n");
printf("5.Reverse\n");
scanf("%d", &a);
switch(a)
{
{
case 1: push();
break;
}
{
case 2: pop();
break;
}
{
case 3: display();
break;
}
{
case 4: peek();
break;
}
{
case 5: reverse();
break;
}
default : printf("Invalid Input");
}
}
char push(char a)
(
char a;
printf("Enter the string");
scanf("%c",&a);
if(top=MAX-1)
{
printf("Stack Overflow");
}
else
(
top=top+1;
string[top]=a;
)
)
char pop()
(
char c;
(
if(top=-1)
{
printf("Stack Underflow");
}
else(
c=string[top];
top=top-1;
printf("The character removed is %c", c);
)
)
)
char display()
{
int i;
for(i=0,i<=MAX-1,i++)
{
printf("%c", string[i]);
}
}
char peek()
(
printf("The top element is %c", string[top]);
)
char reverse()
(
int i;
printf("The reverse of string is");
for(i=MAX-1,i>=0,i--)
(
printf("%c",string[i])
)
)
这是我的程序,大约一个月前刚刚开始 C 编程。 以下是我的错误。请帮忙。我无法从其他答案中找出答案,如果有人可以纠正我的错误,我会学得更好。谢谢
prog.c:12:16: error: expected ':', ',', ';', '}' or '__attribute__' before '=' token
int top= -1;
^
prog.c:14:6: warning: return type of 'main' is not 'int' [-Wmain]
void main()
^
prog.c:54:9: error: expected declaration specifiers or '...' before 'printf'
printf("Enter the string");
^
prog.c:55:9: error: expected declaration specifiers or '...' before 'scanf'
scanf("%c",&a);
^
prog.c:56:9: error: expected declaration specifiers or '...' before 'if'
if(top=MAX-1)
^
prog.c:73:13: error: expected identifier or '(' before 'if'
if(top=-1)
^
prog.c:98:6: error: expected declaration specifiers or '...' before 'printf'
printf("The top element is %c", string[top]);
^
prog.c:96:6: error: 'peek' declared as function returning a function
char peek()
^
prog.c:96:6: error: conflicting types for 'peek'
prog.c:6:6: note: previous declaration of 'peek' was here
char peek();
^
prog.c: In function 'peek':
prog.c:105:6: error: expected declaration specifiers or '...' before 'printf'
printf("The reverse of string is");
^
prog.c:106:6: error: expected declaration specifiers or '...' before 'for'
for(i=MAX-1,i>=0,i--)
^
prog.c:112:6: error: expected '{' at end of input
)
^
prog.c:112:6: warning: control reaches end of non-void function [-Wreturn-type]
)
^
最佳答案
struct stack {...};
定义了一个类型,即struct stack
。
您不能“初始化”类型,也不能为struct
定义默认值。类型的成员,或者您在做时的意图是什么 ... top = -1
.
您可以做的是定义该类型的变量并初始化它:
#include <stdlib.h>
#include <stdio.h>
#define STRING_MAX (42)
struct Stack {
char string[STRING_MAX];
int top;
}
int main(void) {
struct Stack stack = {
"top entry",
1
};
printf("string = '%s', top = %d\n", stack.string, stack.top);
return EXIT_SUCCESS;
}
上面的例子打印:
string = 'top entry', top = 1
关于我的程序中出现编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39436434/