我的代码中出现了分段错误,无法解释原因。我正在编写一个程序,该程序采用中缀表达式并使用 stuct 堆栈转换为后缀。
这是我的主要方法和声明以及我的堆栈声明:
#define LINELN 72
#define STACKSZ 25
#define NEWLN 'n'
#include <stdlib.h>
#include <string.h>
typedef struct stack {
char data[STACKSZ];
int top;
} stack;
void initstk(stack *s1);
int emptystk(stack s1);
char peek(stack s);
int getline(char infix[]);
void push(char item, stack *s);
char pop(stack *s);
int preced(char x);
void processinfix(char infix[], int l, char postifx[]);
#include <stdio.h>
int main() {
char infix[LINELN];
char postfix[LINELN];
processinfix(infix,getline(infix),postfix);
exit(0);
}
我已经测试过并且正在工作的方法都是基本的堆栈操作方法:
//makes top of stack -1
void initstk(stack *s1) {
s1->top=-1;
}
//returns 1 if stack is empty, 0 otherwise
int emptystk(stack s) {
if(s.top == -1){
return one;
}
else{
return 0;
}
}
//peeks at top of the stack
char peek(stack s) {
return s.data[s.top];
}
//pushes value to top of stack
void push(char item, stack *s) {
s->top = s->top +1;
s->data[s->top] = item;
}
//pops off the top of stack and returns it
char pop(stack *s) {
char x;
x = s -> data[s->top];
s-> top =s->top-1;
return x;
}
//gives character a precedence number according to what it is
int preced(char x) {
switch (x) {
case '(':
case ')':
return 3;
case '*':
case '/':
case '%':
return 2;
case '+': case '-':
return 1;
}
return 0;
}
我有一个函数 getLine(( 它接受用户输入,例如:A+B,并将其存储在中缀 [] 中
。int getline(char infix[]){
char c;
int i=0;
printf("Type in your expression");
while((c = getchar()) !=NEWLN && i <= LINELN && c != ' ' ){
infix[i]=c;
i++;
}
return i;
}
我的下一个函数是对中缀表达式进行排序并将其放入后缀[]
void processinfix(char infix[], int inlen, char postfix[])
{
stack s1;
initstk(&s1);
printf("%d characters are in infixn", inlen);
int i =0;
int k=0;
// while "i" isnt bigger then the number of elements in infix
while( i<sizeof(infix)){
/* if infix[i] is a letter send it to post fix right away,
increment inlen down one because infix is now smaller by 1 */
if (('a'<= infix[i]&& infix[i] <= 'z') ||( 'A' <= infix[i]&& infix[i]
<='Z')){
printf("%c is a letter, sending to postfixn", infix[i]);
postfix[k] = infix[i];
inlen--;
printf("infix has shrinked by 1n");
k++;
}
/* else if infix[i] has a higher precedence then whats on top of stack
push infix[i] on to stack and increment inlen down 1 */
else if(preced(infix[i])>preced( s1.data[s1.top])){
push(infix[i],&s1);
inlen--;
printf("infix has shrinked by 1n");
printf("%c is top of stackn", s1.data[s1.top]);
}
/* else if infix[i] is lower or equal to whats on top of stack. put
top of stack into postfix[] until infix[i] is higher precedene then
top of stack. then put it on top of stack and decrease */
else if(preced(infix[i]) <= preced(s1.data[s1.top])){
printf("%c is higher or equal then %cn", s1.data[s1.top],
infix[i]);
while (preced(infix[i])<=preced(s1.data[s1.top])){
postfix[k] = s1.data[s1.top];
printf("%c is put into postfix expressionn", s1.data[s1.top]);
pop(&s1);
k++;
}
push(infix[i], &s1);
inlen--;
printf("%c is now the topn", s1.data[s1.top]);
}
// if nothing is left in infix pop stack until s1.top = -1
if(inlen==0){
printf("nothing left in infixn");
while (s1.top!= -1){
printf("sendin %c to postfixn", s1.data[s1.top]);
postfix[k]=s1.data[s1.top]
pop(&s1);
k++;
}
break;
}
i++;
}
// print postfix
int x=0;
while(x<= sizeof(postfix)){
printf("%s",postfix[x]);
x++;
}
printf("n");
}
我得到的输出是
Type in your expressionA+B
3 characters are in infix
A is a letter, sending to postfix
infix has shrinked by 1
infix has shrinked by 1
+ is top of stack
B is a letter, sending to postfix
infix has shrinked by 1
nothing left in infix
sending + to postfix
Segmentation fault
所以一切都运行良好,直到它遇到
if(inlen==0){
printf("nothing left in infixn");
while (s1.top!= -1){
printf("sendin %c to postfixn", s1.data[s1.top]);
THIS LINE -> postfix[k]=s1.data[s1.top]<-THIS LINE
pop(&s1);
k++;
}
后缀[k] = s1.data[s1.top];在while循环中很常见,但在这里不工作。有什么想法吗?
在修复了一些错误后,代码编译,你会从编译器得到几个警告。其中之一是:
foo.c:183:18: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
printf("%s",postfix[x]);
~^ ~~~~~~~~~~
%d
您使用了错误的格式说明符。 postfix[x]
是一个字符(在这种情况下转换为整数,因此是关于"int"的警告(,而不是字符串。您应该使用"%c"
而不是"%s"
。
此更改后,当我运行它时,程序不再崩溃。