栈求表达式的值

来源:互联网 发布:android 广告机源码 编辑:程序博客网 时间:2024/05/01 09:59
#include<stdio.h>#include<malloc.h>#include<math.h>#include<windows.h>typedef struct Node1{   char ch;   struct Node1 *Next;}Optr,*LinkOptr;//运算符栈 typedef struct Node{    int num; struct Node *Next;}Opnd,*LinkOpnd;//运算数栈 void InitStack(LinkOptr *S){    (*S)=(LinkOptr)malloc(sizeof(Optr)); (*S)->Next=NULL;} void InitStack(LinkOpnd *S){    (*S)=(LinkOpnd)malloc(sizeof(Opnd)); (*S)->Next=NULL;} int PushOptr(LinkOptr pr,char ch1){   LinkOptr temp;   temp=(LinkOptr)malloc(sizeof(Optr));   temp->ch=ch1;   temp->Next=pr->Next;   pr->Next=temp;   return true;} int PushOpnd(LinkOpnd pn,int num1){   LinkOpnd temp;   temp=(LinkOpnd)malloc(sizeof(Opnd));   temp->num=num1;   temp->Next=pn->Next;   pn->Next=temp;   return true;} char PopOptr(LinkOptr S){   if(S->Next==NULL)    return true;   LinkOptr temp;   temp=S->Next;   S->Next=temp->Next;   return temp->ch;   free(temp);} int PopOpnd(LinkOpnd S){   if(S->Next==NULL)    return true;   LinkOpnd temp;   temp=S->Next;   S->Next=temp->Next;   return temp->num;   free(temp);} char GetTop1(LinkOptr S){   return S->Next->ch;} int GetTop2(LinkOpnd S){   return S->Next->num;} int IsOptr(char ch){ char ptr[10]={'+','-','*','/','(',')','#'}; for(int i=0;i<7;i++) {     if(ch==ptr[i])   return true; } return false;} char Precede(char ch1,char ch2){    if((ch1=='+'||ch1=='-'||ch1=='*'||ch1=='/'||ch1==')')&&(ch2=='+'||ch2=='-'||ch2==')'||ch2=='#'))  return '>'; else if((ch1=='('||ch1=='#')&&(ch2=='+'||ch2=='-'||ch2=='*'||ch2=='/'||ch2=='('))  return '<'; else if((ch1=='+'||ch1=='-'||ch1=='*'||ch1=='/')&&ch2=='(')  return '<'; else if((ch1=='+'||ch1=='-')&&(ch2=='*'||ch2=='/'))  return '<'; else if((ch1=='*'||ch1=='/'||ch1==')')&&(ch2=='*'||ch2=='/'))     return '>'; else if(ch1=='('&&ch2==')')  return '='; else if(ch1=='#'&&ch2=='#')  return '='; else   return false;} int Operate(int x,char ch,int y){    if(ch=='+')  return x+y; else if(ch=='-')  return x-y; else if(ch=='*')  return x*y; else if(ch=='/')  return x/y; else   return false;} int main(){   LinkOptr T;   LinkOpnd N;   InitStack(&T);   PushOptr(T,'#');   InitStack(&N);   char ch;   int i=0,j=0,num2=0,a,b,c,mark;   printf("请输入算术表达式(输入#号结束输入):/n");   ch=getchar();   while(ch!='#'||GetTop1(T)!='#')   {      if(!IsOptr(ch))   {      i++;   if(i<=j)             num2=(int(ch)-48);   if(i>j)   {       num2=num2*10+(int(ch)-48);    i=j=0;   }   if(!IsOptr(ch=getchar()))    i++;   if(i==j)    PushOpnd(N,num2);//用getchar()函数读进来的字符肯定只能是一个字符,比如12是先读'1'   }      //然后读'2',这里的i和j是用来判断个位数还是十位数上的数,num2是把运算数如运算数栈前把          //把零散的字符(比如'1'和'2'合并成自己想要运算的数12;   else    switch(Precede(GetTop1(T),ch))   {   case '<':    PushOptr(T,ch);    if(ch!='('&&ch!=')')       j++;    ch=getchar();    break;   case '=':    PopOptr(T);    ch=getchar();    break;   case '>':    a=PopOpnd(N);    b=PopOpnd(N);    PushOpnd(N,Operate(b,PopOptr(T),a));             break;   }   }   printf("请输入结果:/n");   scanf("%d/n",&c);   printf("表达式的最终结果为:/n");   printf("%5d/n",GetTop2(N));   mark=0;   if(c==GetTop2(N))    mark+=5;   else    mark=0;   printf("the point you will got/n ");   printf("%d/n",mark);      return 0;}
原创粉丝点击