计算器程序求改错
```c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define M 100
/*定义字符串的长度*/
#define DEBUG 0
/*定义调试模式*/
typedef enum{
OK=1,
ERROR=0
}Status;
typedef enum{
TRUE=1,
FALSE=0
}Bool;
/*数据结构*/
typedef struct DataType{
float dt;
char dc;
}DataType;
/*链栈结构*/
typedef struct LinkStackNode{
DataType data;
struct LinkStackNode* next;
int size;
}Stack;
Stack* Stack_Init(Stack* pStack);
/*参数:pStack是指向栈的指针
*返回:返回该指针
*/
void Stack_Free(Stack* pStack);
/*参数:pStack是指向栈的指针
*返回:无
*/
void Stack_Clear(Stack* pStack);
/*参数:pStack是指向栈的指针
*返回:无
*/
Bool Stack_IsEmpty(Stack* pStack);
/*参数:pStack是指向栈的指针
*返回:若栈为空则返回TRUE;否则返回FAlSE
*/
Bool Stack_IsFull(Stack* pStack);
/*参数:pStack是指向栈的指针
*返回:若栈为满则返回TRUE;否则返回FALSE
*/
int Stack_Length(Stack* pStack);
/*参数:pStack是指向栈的指针
*返回:该栈的长度
*/
Status Stack_GetTop(Stack* pStack,int f,int* d,char* c);
/*参数:pStack是指向栈的指针,f是用于选择取出字符的类型,d是指向数字的指针,c是指向符号的指针
*返回:如果栈为空则返回ERROR;否则返回OK
*/
Status Stack_Push(Stack* pStack,int d=0,char c=0);
/*参数:pStack是指向栈的指针,d是用于选择执行何种操作的数,c是用于判断的字符
*返回:成功执行入栈操作返回OK;否额返回ERROR
*/
Status Stack_Pop(Stack* pStack);
/*参数:pStack是指向栈的指针
*返回:成功执行出栈操作返回OK;否则返回ERROR
*/
Status Stack_Traverse(Stack* pStack,Status(*visit)());
/*参数:pStack是指向栈的指针
*返回:成功遍历返回OK;否则返回ERROR
*/
void Stack_Destruct(Stack* pStack);
/*参数:pStack是指向栈的指针
*返回:无
*/
Stack* Stack_Init(Stack* pStack){
pStack=(Stack*)malloc(sizeof(Stack));
pStack->next=0;
return pStack;
}
void Stack_Free(Stack* pStack){
Stack* p;
while(pStack){
p=pStack->next;
free(pStack);
pStack=p;
}
}
/*void Stack_Clear(Stack* pStack){
Stack* p;
p=pStack->next=0;
Stack->data=0;
} */
Bool Stack_IsEmpty(Stack* pStack){
if (pStack==0||pStack->next==0){
return TRUE;
}
else{
return FALSE;
}
}
int Stack_Length(Stack* pStack){
return pStack->size;
}
Status Stack_GetTop(Stack* pStack,int f,float* d,char* c){ /*f=0表示int类型,其他值表示char*/
Stack* p=0;
if(pStack==0||pStack->next==0){
return ERROR;
}
p=pStack;
while(p->next){
p=p->next;
}
if(f==0){
*d=p->data.dt;
}
else{
*c=p->data.dc;
}
return OK;
}
Status Stack_Push(Stack* pStack,int d,char c){
Stack* p,* t;
t=(Stack*)malloc(sizeof(Stack));
t->next=0;
if(c==0){
t->data.dt=d;
}
else{
t->data.dc=c;
}
if(pStack==0){
pStack=t;
}
else{
p=pStack;
while(p->next){
p=p->next;
}
p->next=t;
}
pStack->size++;
return OK;
}
Status Stack_Pop(Stack* pStack){
Stack* p,* t;
if(pStack==0||pStack->next==0){
return ERROR;
}
p=pStack;
t=p->next;
while(t->next){
p=p->next;
t=t->next;
}
p->next=0;
/*if (f==0){
*d=p->data.dt;
}
else{
*c=p->data.dc;
}*/
pStack->size--;
free(t);
return OK;
}
Status Stack_Traverse(Stack* pStack,Status (*visit)()){
int i,j;
j=Stack_Length(pStack);
for(i=0;i<j;i++){
if(visit()==ERROR){
return ERROR;
}
}
return OK;
}
float ConvertToFloat(char m[M],float r);
/*功能:将数字串转换为浮点数
*参数:m[M]是原数字串,r是浮点数即结果
*返回:返回结果浮点数r
*/
int Judge(char x);
/*功能:判断运算符的类型并分级
*参数:x是运算符的符号
*返回:字符所代表的级数
*/
int Culculate(int x1, int x2, char s);
/*功能:执行计算
*参数:x1是第一个数字
x2是第二个数字
s是运算符
*返回:返回运算结果
*/
Status Check(char left, char right);
/*功能:判断左右括号是否匹配
*参数:left是左括号,right是右括号
*返回:若左右括号匹配返回OK,否则返回ERROR
*/
Status CharIsSymbol(char c);
/*功能:判断字符是运算符
*参数:c是字符
*返回:若c为运算符返回OK;否则返回ERROR
*/
Status CharIsNum(char d);
/*功能:判断字符是数字
*参数:d是字符
*返回:若d是数字返回OK;否则返回ERROR
*/
int DoSingleOperation(char s[]);
/*功能:执行一行字符的计算
*参数:s[]是这一行字符串
*返回:返回运算结果
*/
float ConvertToFloat(char m[M]){
int i=0;
float value=0;
while(m[i]!='\0'&&m[i]!='.'){
value=value*10+(m[i]-'0');
i=i+1;
}
if(m[i]=='\0'){
return value;
}
i=i+1;
float weight=0.1;
while(m[i]!='\0'){
value=value+(m[i]-'0')*weight;
weight=weight/10;
i=i+1;
}
return value;
}
int Judge(char x){
if (x == '('){
return 4;
}
else if(x=='+'||x=='-'){
return 1;
}
else if(x=='*'||x=='/'){
return 2;
}
else if(x=='^'){
return 3;
}
else if(x==')'){
return 0;
}
}
int Culculate(int x1, int x2, char s){
int result = 0;
switch (s){
case '+':{
result=x1+x2;
break;
}
case '-':{
result=x1-x2;
break;
}
case '*':{
result=x1*x2;
break;
}
case '/':{
result=x1/x2;
break;
}
case '^':{
result=pow((double)x1,x2);
break;
}
}
return result;
}
Status Check(char left,char right){
if (left=='('&&right==')'){
return OK;
}
else{
return ERROR;
}
}
Status CharIsSymbol(char c){
if(c=='+'||c=='-'||c=='*'||c=='/'||c=='('||c==')'||c=='^'){
return OK;
}
else{
return ERROR;
}
}
Status CharIsNum(char d){
if (d>='0'&&d<='9'){
return OK;
}
else{
return ERROR;
}
}
int DoSingleOperation(char s[]){
Stack* pIntStack=0; /*这个栈存放数字*/
Stack* pSymbolStack=0; /*这个栈存放除括号外的符号*/
Stack* pSymbolStack_2=0; /*这个栈存放括号*/
Status sta;
pIntStack=Stack_Init(pIntStack);
pSymbolStack=Stack_Init(pSymbolStack);
pSymbolStack_2=Stack_Init(pSymbolStack_2);
int len,n;
len=strlen(s);
s[len]='#';
len++;
int i;
float a,b,c=0,d=0;
float topele_int;
char topele_c;
char x[M];
char xnum[M];
float m;
for (i=0;i<len;i++){
if (s[i]=='('){
Stack_Push(pSymbolStack_2,0,s[i]);
}
else if(s[i]==')'){
Stack_GetTop(pSymbolStack_2,1,&topele_int,&topele_c);
if (Check(topele_c,s[i]) == 1) {
Stack_Pop(pSymbolStack_2);
}
else {
printf("括号不匹配"); /*判断括号是否匹配*/
return 0;
}
}
}
i = 0;
if (s[0]=='+'||s[0]=='-'||s[0]=='*'||s[0]=='/'||s[0]=='^'){
printf("运算符不能在开头"); /*除括号外的运算符不能在字符串开始处*/
return 0;
}
while (s[i]!='#'){
if(CharIsNum(s[i])==OK){
int index=0;
while(1){
if(CharIsNum(s[i])||s[i]=='.'){
xnum[index++]=s[i];
i++;
}
else{
break;
}
}
xnum[index]='\0';
m=ConvertToFloat(xnum);
Stack_Push(pIntStack,m);
continue;
}
else{
char theta=s[i];
while(Stack_IsEmpty(pSymbolStack)==0) {
Stack_GetTop(pSymbolStack,1,0,&topele_c);
if (topele_c=='('||Judge(topele_c)<Judge(s[i]))
break;
Stack_GetTop(pIntStack,0,&a,0);
Stack_Pop(pIntStack);
Stack_GetTop(pIntStack,0,&b,0);
Stack_Pop(pIntStack);
if (a==0&&topele_c=='/'){
printf("除数不能为0\n"); /*判断除数是否为0,若为0则结束程序,否则继续运行*/
return 0;
}
c=Culculate(b,a,topele_c);
Stack_Push(pIntStack,c);
Stack_Pop(pSymbolStack);
}
sta=Stack_GetTop(pSymbolStack,1,0,&topele_c);
if(sta==OK&&Judge(theta)== 0&&Judge(topele_c)==4) {
Stack_Pop(pSymbolStack);
}
if (Judge(theta)!= 0) {
Stack_Push(pSymbolStack,0,theta);
}
i++;
}
}
while(Stack_IsEmpty(pSymbolStack)==0) {
Stack_GetTop(pSymbolStack,1,0,&topele_c);
Stack_GetTop(pIntStack,0,&a,0);
Stack_Pop(pIntStack);
Stack_GetTop(pIntStack,0,&b,0);
Stack_Pop(pIntStack);
if(a==0&&topele_c=='/') {
printf("除数不能为0\n"); /*判断除数是否为0,若为0则结束程序,否则继续运行*/
return 0;
}
c=Culculate(b,a,topele_c);
Stack_Push(pIntStack,c);
Stack_Pop(pSymbolStack);
}
Stack_GetTop(pIntStack,0,&a,0);
printf("%d\n",a);
return a;
}
int main(){
int i;
printf("Please choose the mode of operation\nFrom file to file:0\tFrom the keyboard:1\n");
scanf("%d",&i);
switch(i){
case 0:{
char all[M]={0};
int result=0;
FILE *fp=fopen("C:/Users/zh/Desktop/1.txt","r"); /*以只读模式打开文件1*/
if(fp==0){
printf("The first txt can not be opened!"); /*若打开文件1失败,结束程序,否则继续运行*/
return 0;
}
FILE *fp2=fopen("C:/Users/zh/Desktop/2.txt","w"); /*以写入模式打开文件2*/
while(!feof(fp)){
fgets(all,100,fp); /*对文件1逐行读取,直到文件尾*/
int len=strlen(all);
if(len==0){ /*若为空行,则结束此次循环,开始执行下一行的程序*/
continue;
}
if(all[len-1]=='\n'){
all[len-1]='\0';
}
printf("%s\n",all);
result=DoSingleOperation(all); /*对读入的一行进行运算*/
fprintf(fp2,"%d\n",result); /*对读入的一行的运算结果打印到文件2中*/
}
fclose(fp); /*关闭文件1*/
fclose(fp2); /*关闭文件2*/
break;
}
case 1:{
printf("Please input the exxpression\n");
int j=0,result;
char all[M]={0};
char y;
gets(all);
/*while((y=getchar())!='\n'){
all[j]=y;
j++;
}*/
result=DoSingleOperation(all);
printf("%d\n",result);
break;
}
default:{
printf("error number\n");
break;
}
}
return 0;
}
```