问题遇到的现象和发生背景
C语言计算器
代码
#include<stdio.h>
#include<stdlib.h>
typedef struct{
int data[100];
int top;
}StackData;
typedef struct{
char symbol[100];
int top;
}StackSymbol;
void InitData(StackData* pStackData);
void InitSymbol(StackSymbol* pStackSymbol);
void PushData(StackData* pStackData,int num);
void PushSymbol(StackSymbol* pStackSymbol,char fuhao);
int GetData(StackData* pStackData);
char GetSymbol(StackSymbol* pStackSymbol);
int PopData(StackData* pStackData);
char PopSymbol(StackSymbol* pStackSymbol);
int Judge(char x);
int Culculate(int x1,int x2,char s);
void InitData(StackData* pStackData){
pStackData->top=-1;
}
void InitSymbol(StackSymbol* pStackSymbol){
pStackSymbol->top=-1;
}
void PushData(StackData* pStackData,int num){
pStackData->top++;
pStackData->data[pStackData->top]=num;
}
void PushSymbol(StackSymbol* pStackSymbol,char fuhao){
pStackSymbol->top++;
pStackSymbol->symbol[pStackSymbol->top]=fuhao;
}
int GetData(StackData* pStackData){
return pStackData->data[pStackData->top];
}
char GetSymbol(StackSymbol* pStackSymbol){
return pStackSymbol->symbol[pStackSymbol->top];
}
int PopData(StackData* pStackData){
int i;
i=pStackData->data[pStackData->top];
pStackData->top--;
return i;
}
char PopSymbol(StackSymbol* pStackSymbol){
char a;
a=pStackSymbol->symbol[pStackSymbol->top];
pStackSymbol->top--;
return a;
}
int Judge(char x){
if(x=='('){
return 1;
}
else if(x=='+'||x=='-'){
return 2;
}
else if(x=='*'||x=='/'){
return 3;
}
else if(x==')'){
return 4;
}
}
int Culculate(int x1,int x2,char s){
int result;
switch(s){
case '+':{
result=x1+x2;
break;
}
case '-':{
result=x1-x2;
break;
}
case '*':{
result=x1*x2;
break;
}
case '/':{
result=x1/x2;
break;
}
}
return result;
}
int main(){
StackData *data;
StackSymbol *symbol;
InitData(data);
InitSymbol(symbol);
int a=0,b=0,result=0,x1,x2;
char y;
char s[100]={0};
char *str=(char *)malloc(sizeof(char)*200);
while((y=getchar())!='\n'){
str[a]=y;
a++;
}
str[a]='\0';
for(a=0;str[a]!='\0';a++){
if(a==0&&str[a]=='-'){
s[b++]=str[a];
}
else if(str[a]=='('&&str[a+1]=='-'){
a++;
s[b++]=str[a++];
while(str[a]>='0'&&str[a]<='9'){
s[b]=str[a];
b++;
a++;
}
PushData(data,atoi(s));
while(b>0){
s[b]=0;
b--;
}
if(str[a]!=')'){
a--;
PushSymbol(symbol,'(');
}
}
else if(str[a]>='0'&&str[a]<='9'){
while(str[a]>='0'&&str[a]<='9'){
s[b]=str[a];
b++;
a++;
}
PushData(data,atoi(s));
while(b>0){
s[b]=0;
b--;
}
a--;
}
else{
if(symbol->top==-1){
PushSymbol(symbol,str[a]);
}
else if(Judge(str[a])==1){
PushSymbol(symbol,str[a]);
}
else if(Judge(str[a])==2){
if(Judge(GetSymbol(symbol))==1){
PushSymbol(symbol,str[a]);
}
else if(Judge(GetSymbol(symbol))==2){
while(symbol->top>=0&&data->top>=1){
x2=PopData(data);
x1=PopData(data);
result=Culculate(x1,x2,PopSymbol(symbol));
PushData(data,result);
}
PushSymbol(symbol,str[a]);
}
else if(Judge(GetSymbol(symbol))==3){
while(symbol->top>=0&&data->top>=1){
x2=PopData(data);
x1=PopData(data);
result=Culculate(x1,x2,PopSymbol(symbol));
PushData(data,result);
}
PushSymbol(symbol,str[a]);
}
}
else if(Judge(str[a])==3){
if(Judge(GetSymbol(symbol))==1){
PushSymbol(symbol,str[a]);
}
else if(Judge(GetSymbol(symbol))==2){
PushSymbol(symbol,str[a]);
}
else if(Judge(GetSymbol(symbol))==3){
while(symbol->top>=0&&data->top>=1){
x2=PopData(data);
x1=PopData(data);
result=Culculate(x1,x2,PopSymbol(symbol));
PushData(data,result);
}
PushSymbol(symbol,str[a]);
}
}
else if(Judge(str[a])==4){
do{
x2=PopData(data);
x1=PopData(data);
result=Culculate(x1,x2,PopSymbol(symbol));
PushData(data,result);
}while(Judge(GetSymbol(symbol))!=1);
PopSymbol(symbol);
}
}
}
free(str);
while(symbol->top!=-1){
x2=PopData(data);
x1=PopData(data);
result=Culculate(x1,x2,PopSymbol(symbol));
PushData(data,result);
}
printf("%d",data->data[0]);
return 0;
}
运行结果及报错内容
运行后直接结束,如何正常运行,并实现乘方和调试功能