#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define STACK_INIT_SIZE 20
#define STACKINCREMENT 20
typedef struct {
int* base;
int* top;
int stacksize;
}SqStack;
int InitStack(SqStack &S);
int Push(SqStack& S, char e);
int Pop(SqStack& S, char& e);
char GetTop(SqStack S);
int In(char ch); //运算符判断
char Precede(char ch1, char ch2); //优先级判断
int Operate(double a, char theta, double b); //运算方式
int Tz(char* str);// 弹栈
int main() {
char str[STACK_INIT_SIZE] = {};//输入的字符串算术表达式
char ch = 0;
int output = 0;
while (true)
{
scanf("",&str);//输入
ch = str[0];
if (ch == '=')
break;
else
{
output = Tz(str);
}
}
return 0;
}
int InitStack(SqStack &S) {
S.base = (int*)malloc(STACK_INIT_SIZE * sizeof(int));
if (!S.base)
return 0;
S.top = S.base;
S.stacksize = STACK_INIT_SIZE;
return 1;
}
int Push(SqStack& S, char e) {
if (S.top - S.base >= S.stacksize) {
S.base = (int*)realloc(S.base, (S.stacksize + STACKINCREMENT));
if (!S.base)
return 0;
S.top = S.base + S.stacksize;
S.stacksize += STACKINCREMENT;
}
*S.top = e;
S.top++;
return 1;
}
int Pop(SqStack& S, char& e){
if (S.top == S.base)
return -1;
--S.top;
e =* S.top;
return 1;
}
char GetTop(SqStack S) {
if (S.base != S.top)
return *S.top;
else
return 0;
}
int In(char ch) {
if (ch == '+' || ch == '-' || ch == '*'
|| ch == '/' || ch == '(' || ch == ')' || ch == '=')
return true;
else
return false;
}
char Precede(char ch1, char ch2) {
char cmpResult = 0;//返回的比较结果> < =
switch (ch1)
{
case '?':
cmpResult = '<';
break;
case '+':
switch (ch2)
{
case '+':
cmpResult = '>';
break;
case '-':
cmpResult = '>';
break;
case '*':
cmpResult = '<';
break;
case '/':
cmpResult = '<';
break;
case '(':
cmpResult = '<';
break;
case ')':
cmpResult = '>';
break;
case '=':
cmpResult = '>';
break;
default:
break;
}
break;
case '-':
switch (ch2)
{
case '+':
cmpResult = '>';
break;
case '-':
cmpResult = '>';
break;
case '*':
cmpResult = '<';
break;
case '/':
cmpResult = '<';
break;
case '(':
cmpResult = '<';
break;
case ')':
cmpResult = '>';
break;
case '=':
cmpResult = '>';
break;
default:
break;
}
break;
case '*':
switch (ch2)
{
case '+':
cmpResult = '>';
break;
case '-':
cmpResult = '>';
break;
case '*':
cmpResult = '>';
break;
case '/':
cmpResult = '>';
break;
case '(':
cmpResult = '<';
break;
case ')':
cmpResult = '>';
break;
case '=':
cmpResult = '>';
break;
default:
break;
}
break;
case '/':
switch (ch2)
{
case '+':
cmpResult = '>';
break;
case '-':
cmpResult = '>';
break;
case '*':
cmpResult = '>';
break;
case '/':
cmpResult = '>';
break;
case '(':
cmpResult = '<';
break;
case ')':
cmpResult = '>';
break;
case '=':
cmpResult = '>';
break;
default:
break;
}
break;
case '(':
switch (ch2)
{
case '+':
cmpResult = '<';
break;
case '-':
cmpResult = '<';
break;
case '*':
cmpResult = '<';
break;
case '/':
cmpResult = '<';
break;
case '(':
cmpResult = '<';
break;
case ')':
cmpResult = '=';
break;
case '=':
cmpResult = '>';
break;
default:
break;
}
break;
case ')':
switch (ch2)
{
case '+':
cmpResult = '>';
break;
case '-':
cmpResult = '>';
break;
case '*':
cmpResult = '>';
break;
case '/':
cmpResult = '>';
break;
case '(':
cmpResult = ' ';
break;
case ')':
cmpResult = '>';
break;
case '=':
cmpResult = '>';
break;
default:
break;
}
break;
case '=':
break;
default:
break;
}
return cmpResult;
}
int Operate(double a, char theta, double b)
{
int result = 0;
switch (theta)
{
case '+':
result = a + b;
break;
case '-':
result = a - b;
break;
case '*':
result = a * b;
break;
case '/':
result = a / b;
break;
default:
break;
}
return result;
return result;
}
int Tz(char* str) {
SqStack s1, s2; //s1=数字 s2=操作符
int InitStack(SqStack & s1);//初始化s1 s2
int InitStack(SqStack & s2);
char ch;
ch = str[0];
int i = 0;//扫描字符串的下标
char theta = 0;
char a = 0;
char b = 0;
char x = 0;
int i1 = 0;//数字和小数点构成的字符串下标
char z[10] = {};//存放数字的字符串
while (true) {
ch = str[i];//第i个字符
if (ch == '\0')
break;
if (In(ch))//是运算符
{
ch = str[i];
switch (Precede(GetTop(s2), ch))
{//比较s2的栈顶元素和ch的优先级
case '<':
Push(s2, ch); i++; ch = str[i];
break;
case '>':
Pop(s2, theta);//弹出栈顶的运算符
Pop(s1, b);
Pop(s1, a);//弹出栈顶的两个运算数
Push(s1, Operate(a, theta, b));//将运算结果压入OPND栈
break;
case '='://OPTR的栈顶元素是"("且ch")"
Pop(s1, x);
i++; ch = str[i];
break;
default:
break;
}
}
else if (ch >= '0' && ch <= '9')//数字
{
i1 = 0;
do
{
z[i1] = ch;
i1++;
i++;
ch = str[i];
} while ((ch >= '0' && ch <= '9') || ch == '.');
z[i1] = 0;
double d = atof(z);
Push(s1, d);
}
else
{
printf("输入错误!\n");
break;
}
}
return GetTop(s1);
}
错误如下,明明在上面调用了InitStack函数,为啥还提示这个错误呢?s1却不提示
其他地方还有错误的话,恳求改正!