?? 中綴轉(zhuǎn)后綴求值.cpp
字號:
#include<stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
#define MAXNUM 100
typedef int DataType;
struct SeqStack {
DataType s[MAXNUM];
int t;
};
typedef struct SeqStack *PSeqStack;
PSeqStack createEmptyStack_seq() { //建立空棧
PSeqStack pastack;
pastack = (PSeqStack)malloc(sizeof(struct SeqStack));
if (pastack == NULL)
printf("空間溢出!!\n");
else
pastack->t = -1;
return pastack;
}
int isEmptyStack_seq(PSeqStack pastack)
{
return pastack->t == -1;
}
void push_seq(PSeqStack pastack, DataType x)
{
if (pastack->t >= MAXNUM - 1)
printf("溢出!\n");
else
{
pastack->t = pastack->t + 1;
pastack->s[pastack->t] = x;
}
}
void pop_seq(PSeqStack pastack)
{
if (pastack->t == -1)
printf("錯誤!\n");
else
pastack->t = pastack->t - 1;
}
DataType top_seq(PSeqStack pastack)
{
return pastack->s[pastack->t];
}
int infixtoSuffix(const char * infix, char * suffix)
{ //將中綴表達式轉(zhuǎn)換為后綴表達式,順利轉(zhuǎn)換返回true,若轉(zhuǎn)換過程中發(fā)現(xiàn)中綴表達式非法則返回false
int state_int = FALSE;
//state_int記錄狀態(tài),等于true表示剛讀入的是數(shù)字字符,等于false表示剛讀入的不是數(shù)字字符,
//設(shè)置這個變量是為了在每輸出一個整數(shù)后輸出一個空格,以免連續(xù)輸出的兩個整數(shù)混在一起。
char c, c2;
PSeqStack ps = createEmptyStack_seq(); //運算符棧
int i, j = 0;
if (infix[0] == '\0')
return FALSE; //不允許出現(xiàn)空表達式
for (i = 0; infix[i] != '\0'; i++)
{
c = infix[i];
switch (c)
{
case ' ':
case '\t':
case '\n':
if (state_int == TRUE)
suffix[j++] = ' ';//狀態(tài)從true轉(zhuǎn)換為false時輸出一個空格
state_int = FALSE;
break; //遇到空格或制表符忽略
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
state_int = TRUE;
suffix[j++] = c; //遇到數(shù)字輸出
break;
case '(':
if (state_int == TRUE)
suffix[j++] = ' ';//狀態(tài)從true轉(zhuǎn)換為false時輸出一個空格
state_int = FALSE;
push_seq(ps, c); //遇到左括號,入棧
break;
case ')':
if (state_int == TRUE)
suffix[j++] = ' ';//狀態(tài)從true轉(zhuǎn)換為false時輸出一個空格
state_int = FALSE;
c2 = ')';
while (!isEmptyStack_seq(ps))
{
c2 = top_seq(ps);//取棧頂
pop_seq(ps); //出棧
if (c2 == '(')
{
break;
}
suffix[j++] = c2;
}
if (c2 != '(')
{
free(ps);
suffix[j++] = '\0';
return FALSE;
}
break;
case '+':
case '-':
if (state_int == TRUE)
suffix[j++] = ' ';
state_int = FALSE;
while(!isEmptyStack_seq(ps))
{
c2 = top_seq(ps);
if (c2 == '+' || c2 == '-' || c2 == '*' || c2 == '/')
{
pop_seq(ps);
suffix[j++] = c2;
}
else if(c2=='(') break;
}
push_seq(ps, c);
break;
case '*':
case '/':
if (state_int == TRUE)
suffix[j++] = ' ';
state_int = FALSE;
while (!isEmptyStack_seq(ps))
{
c2 = top_seq(ps);
if (c2 == '*' || c2 == '/')
{
pop_seq(ps);
suffix[j++] = c2;
}
else if(c2=='+'||c2=='-'||c2=='(') break;
}
push_seq(ps, c);
break;
default:
free(ps);
suffix[j++] = '\0';
return FALSE;
}
}
if (state_int == TRUE) suffix[j++] = ' ';
while (!isEmptyStack_seq(ps))
{
c2 = top_seq(ps);
pop_seq(ps);
if (c2 == '(')
{
free(ps);
suffix[j++] = '\0';
return FALSE;
}
suffix[j++] = c2;
}
free(ps);
suffix[j++] = '\0';
return TRUE;
}
int calculateSuffix(const char * suffix, int * presult)
{
int state_int = FALSE;
PSeqStack ps = createEmptyStack_seq();
int num = 0, num1, num2;
int i;
char c;
for (i = 0; suffix[i] != '\0'; i++)
{
c = suffix[i];
switch (c)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (state_int == TRUE)
num = num * 10 + c - '0';
else num = c - '0';
state_int = TRUE;
break;
case ' ':
case'\t':
case '\n':
if (state_int == TRUE)
{
push_seq(ps, num);
state_int = FALSE;
}
break;
case '+':
case '-':
case '*':
case '/':
if (state_int == TRUE)
{
push_seq(ps, num);
state_int = FALSE;
}
if (isEmptyStack_seq(ps))
{
free(ps);
return FALSE;
}
num2 = top_seq(ps);
pop_seq(ps);
if (isEmptyStack_seq(ps))
{
free(ps);
return FALSE;
}
num1 = top_seq(ps);
pop_seq(ps);
if (c == '+')
push_seq(ps, num1 + num2);
if (c == '-')
push_seq(ps, num1 - num2);
if (c == '*')
push_seq(ps, num1 * num2);
if (c == '/')
push_seq(ps, num1 / num2);
break;
default:
free(ps);
return FALSE;
}
}
*presult = top_seq(ps);
pop_seq(ps);
if (!isEmptyStack_seq(ps))
{
free(ps);
return FALSE;
}
free(ps);
return TRUE;
} void getline(char * line, int limit)
{
char c;
int i = 0;
while (i < limit - 1 && (c = getchar()) != EOF && c != '\n')
line[i++] = c;
line[i] = '\0';
} void main()
{ char c, infix[MAXNUM], suffix[MAXNUM];
int result;
int flag = TRUE;
while (flag == TRUE)
{
printf("請輸入一個中綴表達式,以回車結(jié)束!\n輸入:");
getline(infix, MAXNUM);
if(infixtoSuffix(infix, suffix) == TRUE)
printf("后綴表達式:%s\n", suffix);
else
{
printf("無效的中綴表達式!\n");
printf("\n繼續(xù)? (y/n)");
scanf("%c", &c);
if (c == 'n' || c == 'N') flag = FALSE;
while (getchar() != '\n');
printf("\n");
continue;
}
if(calculateSuffix(suffix, &result) == TRUE)
printf("運算結(jié)果:%d\n", result);
else
printf("您輸入的是后綴表達式,請輸入中綴表達式!\n");
printf("\n繼續(xù)? (y/n)");
scanf("%c", &c);
if (c == 'n' || c == 'N') flag = FALSE;
while (getchar() != '\n');
printf("\n");
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -