#include<stdio.h> #include<stdlib.h> #include<math.h> #define STACK_INIT_SIZE 100 /*定义栈初始容量*/ #define STACK_INCREMENT 10 /*定义栈增加容量*/ #define M 100 /*定义字符串的长度*/ typedef enum{ OK=1, ERROR=0 }Status; typedef struct{ void* pBase; void* pTop; void* shuzu[M]; int elementSize; int stackSize; }Stack; Stack* Stack_Construct(int sizeOfElement); /*功能:构造一个栈 *参数:栈元素类型的长度 *返回:构造成功返回新构造的栈;否则返回NULL */ Stack* Stack_Construct(int sizeOfElement){ Stack *pStack; pStack=(Stack*)malloc(sizeof(Stack)); if(pStack==NULL){ return NULL; } pStack->pBase=malloc(STACK_INIT_SIZE*sizeOfElement); if(pStack->pBase==NULL){ free(pStack); return NULL; } pStack->pTop=pStack->pBase; pStack->elementSize=sizeOfElement; pStack->stackSize=STACK_INIT_SIZE; return pStack; } Status Stack_Init(Stack *pStack); /*参数:pStack是指向栈的指针 *返回:成功返回OK;否则返回ERROR */ 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是指向栈的指针 *返回:栈的长度 */ char Stack_GetTop(Stack *pStack); /*功能:取出栈顶元素 *参数:pStack是指向栈的指针 *返回:返回栈顶元素 */ Status Stack_Push(Stack *pStack,void *pVoid); /*功能:将pVoid指向的数据压入pStack指向的栈 *参数:pStack是指向栈的指针,pVoid是指向数据的指针 *返回:成功返回OK;否则返回ERROR */ Status Stack_Pop(Stack *pStack,void *pVoid); /*功能:将pStack指向的栈的元素弹出存入pVoid指向的内存 *参数:pStack是指向栈的指针,pVoid是指向数据的指针 *返回:成功返回OK;否则返回ERROR */ Status Stack_Traverse(Stack *pStack,Status(*visit)()); /*功能:对pStack指向的栈的每个元素执行visit操作 *参数:pStack是指向栈的指针,visit是函数指针 *返回:成功返回OK;失败返回ERROR */ 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是运算符 *返回:返回运算结果 */ Stack *pIntStack; pIntStack=Stack_Construct(sizeof(int)); /*构造一个数字栈*/ Stack *pSymbolStack; pSymbolStack=Stack_Construct(sizeof(char)); /*构造一个符号栈*/ Status Stack_Init(Stack *pStack){ pStack->pBase=(void*)malloc(STACK_INIT_SIZE*sizeof(void)); if(pStack->pBase==NULL){ return ERROR; } pStack->pTop=pStack->pBase; pStack->stackSize=STACK_INIT_SIZE; return OK; } void Stack_Free(Stack *pStack){ free(pStack->pBase); pStack->pBase=NULL; pStack->pTop=NULL; pStack->stackSize=0; return; } void Stack_Clear(Stack *pStack){ pStack->pTop=pStack->pBase; } bool Stack_IsEmpty(Stack *pStack){ if(pStack->pTop==pStack->pBase){ return true; } else{ return false; } } bool Stack_IsFull(Stack *pStack){ if(pStack->pTop-pStack->pBase==pStack->stackSize){ return true; } else{ return false; } } int Stack_Length(Stack *pStack){ return pStack->pTop-pStack->pBase; } char Stack_GetTop(Stack *pStack){ return pIntStack->shuzu[pIntStack->pTop]; } Status Stack_Push(Stack *pStack,void *pVoid){ int newSize; void *pVoid_2; if(Stack_Length(pStack)>=Stack->stackSize){ newSize=pStack->stackSize+STACK_INCREMENT; pVoid_2=(void*)realloc(pStack->pBase,newSize*sizeof(void)); if(pVoid_2==NULL){ return ERROR; } else{ pStack->pBase=pVoid_2; pStack->pTop=pStack->pBase+pStack->stackSize; pStack->stackSize=newSize; } } *(pStack->pTop)=*pVoid; pStack->pTop++; return OK; } Status Stack_Pop(Stack *pStack,void *pVoid){ if(pStack->pTop==pStack->pBase){ return ERROR; } else{ pStack->pTop--; *pVoid=*(pStack->pTop); return OK; } } Status Stack_Traverse(Stack *pStack,Status(*visit)()){ int i,j; j=Stack_Length(pStack); for(i=0;i<n;i++){ if(visit(&(pStack->pBase[i]))==ERROR){ return ERROR; } } return OK; } 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; int 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 1; } else if(x=='+'||x=='-'){ return 2; } else if(x=='*'||x=='/'){ return 3; } else if(x=='^'){ return 4; } else if(x==')'){ return 5; } } 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; } case '^':{ result=pow(x1,x2); break; } } } int main(){ printf("Enter The Expression Including '+' '-' '*' '/' '^' '(' ')' And Intengers\n"); Stack* stack=(Stack*)malloc(sizeof(Stack)); Stack_Init(stack); int a=0,b=0,c=0,result=0,x1,x2; char y; char s[M]={0}; char n[M]={0}; char d[M]={0}; char *str=(char*)malloc(sizeof(char)*M); char fuhao; 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]; n[b]=atoi(s); b++; a++; } Stack_Push(pIntStack,&n[b]); while(b>0){ s[b]=0; n[b]=0; b--; } } else if(str[a]>='0'&&str[a]<='9'){ while(str[a]>='0'&&str[a]<='9'){ s[b]=str[a]; n[b]=atoi(s); b++; a++; } Stack_Push(pIntStack,&n[b]); while(b>0){ s[b]=0; n[b]=0; b--; } a--; } else{ if(pSymbolStack->pTop==-1){ Stack_Push(pSymbolStack,&str[a]); } else if(Judge(str[a])==1){ Stack_Push(pSymbolStack,&str[a]); } else if(Judge(str[a])==2){ if(Judge(Stack_GetTop(pSymbolStack))==1){ Stack_Push(pSymbolStack,&str[a]); } else if(Judge(Stack_GetTop(pSymbolStack))==2){ while(pSymbolStack->pTop>=0&&pIntStack>=1){ Stack_Pop(pIntStack,&x2); Stack_Pop(pIntStack,&x1); Stack_Pop(pSymbolStack,&fuhao); result=Culculate(x1,x2,fuhao); Stack_Push(pIntStack,&result); } Stack_Push(pSymbolStack,&str[a]); } else if(Judge(Stack_GetTop(pSymbolStack))==3){ while(pSymbolStack->pTop>=0&&pIntStack->pTop>=1){ Stack_Pop(pIntStack,&x2); Stack_Pop(pIntStack,&x1); Stack_Pop(pSymbolStack,&fuhao); result=Culculate(x1,x2,fuhao); Stack_Push(pIntStack,&result); } Stack_Push(pSymbolStack,&str[a]); } } else if(Judge(str[a])==3){ if(Judge(Stack_GetTop(pSymbolStack))==1){ Stack_Push(pSymbolStack,&str[a]); } else if(Judge(Stack_GetTop(pSymbolStack))==2){ Stack_Push(pSymbolStack,&str[a]); } else if(Judge(Stack_GetTop(pSymbolStack))==3){ while(pSymbolStack->pTop>=0&&pIntStack->pTop>=1){ Stack_Pop(pIntStack,&x2); Stack_Pop(pIntStack,&x1); Stack_Pop(pSymbolStack,&fuhao); result=Culculate(x1,x2,fuhao); Stack_Push(pIntStack,&result); } Stack_Push(pSymbolStack,&str[a]); } } else if(Judge(str[a])==4){ if(Judge(Stack_GetTop(pSymbolStack))==1){ Stack_Push(pSymbolStack,&str[a]); } else if(Judge(Stack_GetTop(pSymbolStack))==2){ Stack_Push(pSymbolStack,&str[a]); } else if(Judge(Stack_GetTop(pSymbolStack))==3){ Stack_Push(pSymbolStack,&str[a]); } else if(Judge(Stack_GetTop(pSymbolStack))==4){ while(pSymbolStack->pTop>=0&&pIntStack->pTop>=1){ Stack_Pop(pIntStack,&x2); Stack_Pop(pIntStack,&x1); Stack_Pop(pSymbolStack,&fuhao); result=Culculate(x1,x2,fuhao); Stack_Push(pIntStack,&result); } Stack_Push(pSymbolStack,&str[a]); } } else if(Judge(str[a])==5){ do{ Stack_Pop(pIntStack,&x2); Stack_Pop(pIntStack,&x1); Stack_Pop(pSymbolStack,&fuhao); result=Culculate(x1,x2,fuhao); Stack_Push(pIntStack,&result); }while(Judge(Stack_GetTop(pSymbolStack))!=1); StackSymbol_Pop(pSymbolStack); } } } free(str); while(pSymbolStack->pTop!=-1){ Stack_Pop(pIntStack,&x2); Stack_Pop(pIntStack,&x1); Stack_Pop(pSymbolStack,&fuhao); result=Culculate(x1,x2,fuhao); Stack_Push(pIntStack,&result); } printf("The Result Is %d",result); return 0; }
05-31
807
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)