C语言一个作业,运用栈设计一个计算器,VC++6.0的
运用C语言程序设计课程的知识设计一个计算器程序,利用算符优先关系,实现对算术四则混合运算表达式的求值。⑴输入的形式:算术表达式,以井号“#”结束。例如2*(3+4)#;包...
运用C语言程序设计课程的知识设计一个计算器程序,利用算符优先关系,实现对算术四则混合运算表达式的求值。
⑴输入的形式:算术表达式,以井号“#”结束。例如2*(3+4)#;
包含的运算符只能有'+' 、'-' 、'*' 、'/' 、'('、 ')';
⑵输出的形式:运算结果,例如Answer is:14;
⑶程序所能达到的功能:对算术表达式求值并输出结果。 展开
⑴输入的形式:算术表达式,以井号“#”结束。例如2*(3+4)#;
包含的运算符只能有'+' 、'-' 、'*' 、'/' 、'('、 ')';
⑵输出的形式:运算结果,例如Answer is:14;
⑶程序所能达到的功能:对算术表达式求值并输出结果。 展开
2个回答
展开全部
#include "stdio.h"
#include "string.h"
//网上找的,在VC下测试通过,还改了一个显示的小错。
#include "ctype.h"
#include "math.h"
//expression evaluate
#define iMUL 0
#define iDIV 1
#define iADD 2
#define iSUB 3
#define iCap 4
//#define LtKH 5
//#define RtKH 6
#define MaxSize 100
void iPush(float);
float iPop();
float StaOperand[MaxSize];
int iTop=-1;
//
char Srcexp[MaxSize];
char Capaexp[MaxSize];
char RevPolishexp[MaxSize];
float NumCapaTab[26];
char validexp[]="*/+-()";
char NumSets[]="0123456789";
char StackSymb[MaxSize];
int operands;
//
void NumsToCapas(char [], int , char [], float []);
int CheckExpress(char);
int PriorChar(char,char);
int GetOperator(char [], char);
void counterPolishexp(char INexp[], int slen, char Outexp[]);
float CalcRevPolishexp(char [], float [], char [], int);
void main()
{
int ilen;
float iResult=0.0;
printf("enter a valid number string:\n");
memset(StackSymb,0,MaxSize);
memset(NumCapaTab,0,26); //A--NO.1, B--NO.2, etc.
gets(Srcexp);
ilen=strlen(Srcexp);
//printf("source expression:%s\n",Srcexp);
NumsToCapas(Srcexp,ilen,Capaexp,NumCapaTab);
printf("Numbers listed as follows:\n");
int i;
for (i=0; i<operands; ++i)
printf("%.2f ",NumCapaTab[i]);
printf("\nCapaexp listed in the following:\n");
printf("%s\n",Capaexp);
ilen=strlen(Capaexp);
counterPolishexp(Capaexp,ilen,RevPolishexp);
printf("RevPolishexp:\n%s\n",RevPolishexp);
ilen=strlen(RevPolishexp);
iResult=CalcRevPolishexp(validexp, NumCapaTab, RevPolishexp,ilen);
printf("\ncounterPolish expression:\n%s=%.6f\n",Srcexp,iResult);
}
void iPush(float value)
{
if(iTop<MaxSize) StaOperand[++iTop]=value;
}
float iPop()
{
if(iTop>-1)
return StaOperand[iTop--];
return -1.0;
}
void NumsToCapas(char Srcexp[], int slen, char Capaexp[], float NumCapaTab[])
{
char ch;
int i, j, k, flg=0;
int sign;
float val=0.0,power=10.0;
i=0; j=0; k=0;
while (i<slen)
{
ch=Srcexp[i];
if (i==0)
{
sign=(ch=='-')?-1:1;
if(ch=='+'||ch=='-')
{
ch=Srcexp[++i];
flg=1;
}
}
if (isdigit(ch))
{
val=ch-'0';
while (isdigit(ch=Srcexp[++i]))
{
val=val*10.0+ch-'0';
}
if (ch=='.')
{
while(isdigit(ch=Srcexp[++i]))
{
val=val+(ch-'0')/power;
power*=10;
}
} //end if
if(flg)
{
val*=sign;
flg=0;
}
} //end if
//write Capaexp array
// write NO.j to array
if(val)
{
Capaexp[k++]='A'+j;
Capaexp[k++]=ch;
NumCapaTab[j++]=val; //A--0, B--1,and C, etc.
}
else
{
Capaexp[k++]=ch;
}
val=0.0;
power=10.0;
//
i++;
}
Capaexp[k]='\0';
operands=j;
}
float CalcRevPolishexp(char validexp[], float NumCapaTab[], char RevPolishexp[], int slen)
{
float sval=0.0, op1,op2;
int i, rt;
char ch;
//recursive stack
i=0;
while((ch=RevPolishexp[i]) && i<slen)
{
switch(rt=GetOperator(validexp, ch))
{
case iMUL: op2=iPop(); op1=iPop();
sval=op1*op2;
iPush(sval);
break;
case iDIV: op2=iPop(); op1=iPop();
if(!fabs(op2))
{
printf("overflow\n");
iPush(0);
break;
}
sval=op1/op2;
iPush(sval);
break;
case iADD: op2=iPop(); op1=iPop();
sval=op1+op2;
iPush(sval);
break;
case iSUB: op2=iPop(); op1=iPop();
sval=op1-op2;
iPush(sval);
break;
case iCap: iPush(NumCapaTab[ch-'A']);
break;
default: ;
}
++i;
}
while(iTop>-1)
{
sval=iPop();
}
return sval;
}
int GetOperator(char validexp[],char oper)
{
int oplen,i=0;
oplen=strlen(validexp);
if (!oplen) return -1;
if(isalpha(oper)) return 4;
while(i<oplen && validexp[i]!=oper) ++i;
if(i==oplen || i>=4) return -1;
return i;
}
int CheckExpress(char ch)
{
int i=0;
char cc;
while((cc=validexp[i]) && ch!=cc) ++i;
if (!cc)
return 0;
return 1;
}
int PriorChar(char curch, char stach)
{
//栈外优先级高于(>)栈顶优先级时,才入栈
//否则(<=),一律出栈
if (curch==stach) return 0; //等于时应该出栈
else if (curch=='*' || curch=='/')
{
if(stach!='*' && stach!='/')
return 1;
}
else if (curch=='+' || curch=='-')
{
if (stach=='(' || stach==')')
return 1;
}
else if (curch=='(')
{
if (stach==')')
return 1;
}
return 0;
}
void counterPolishexp(char INexp[], int slen, char Outexp[])
{
int i, j, k,pr;
char t;
i=0;
j=k=0;
while (INexp[i]!='=' && i<slen)
{
if (INexp[i]=='(')
StackSymb[k++]=INexp[i];
//iPush(*(INexp+i));
else if(INexp[i]==')')
{
//if((t=iPop())!=-1)
while((t=StackSymb[k-1])!='(')
{
Outexp[j++]=t;
k--;
}
k--;
}
else if (CheckExpress(INexp[i])) // is oparator
{
// printf("operator %c k=%d\n",INexp[i],k);
while (k)
{
// iPush(*(INexp+i));
if(pr=PriorChar(INexp[i],StackSymb[k-1]))
break;
else
{
//if ((t=iPop())!=-1)
t=StackSymb[k-1]; k--;
Outexp[j++]=t;
}
} //end while
StackSymb[k++]=INexp[i]; //common process
}
else //if() 变量名
{
// printf("operand %c k=%d\n",INexp[i],k);
Outexp[j++]=INexp[i];
}
i++; //
}
while (k)
{
t=StackSymb[k-1]; k--;
Outexp[j++]=t;
}
Outexp[j]='\0';
}
不用栈的话
/* Note:Your choice is C IDE */
#include "stdio.h"
#include "stdlib.h"
#include "ctype.h"
int n=0;
char record[20];
float product();
float change();
float muli()
{
float summ;
summ=product();
while(record[n]=='-'||record[n]=='+')
{
switch(record[n])
{
case '+':n++;summ+=product();break;
case '-':n++;summ-=product();break;
}
}
return summ;
}
float product()
{
float sump;
sump=change();
while(record[n]=='*'||record[n]=='/')
{
switch(record[n])
{
case '*':n++;sump*=change();break;
case '/':n++;sump/=change();break;
}
}
return sump;
}
float change()
{
float sumc;
char rec[20],i=0;
if(record[n]=='(')
{
n++;
sumc=muli();
}
if(record[n]==')')
n++;
while(isdigit(record[n])||record[n]=='.')
{
while(isdigit(record[n])||record[n]=='.')
rec[i++]=record[n++];
rec[i]='\0';
sumc=atof(rec);
}
return sumc;
}
void main()
{
while(1)
{
n=0;
scanf("%s",record);
printf("%s=%g\n",record,muli());
}
}
#include "string.h"
//网上找的,在VC下测试通过,还改了一个显示的小错。
#include "ctype.h"
#include "math.h"
//expression evaluate
#define iMUL 0
#define iDIV 1
#define iADD 2
#define iSUB 3
#define iCap 4
//#define LtKH 5
//#define RtKH 6
#define MaxSize 100
void iPush(float);
float iPop();
float StaOperand[MaxSize];
int iTop=-1;
//
char Srcexp[MaxSize];
char Capaexp[MaxSize];
char RevPolishexp[MaxSize];
float NumCapaTab[26];
char validexp[]="*/+-()";
char NumSets[]="0123456789";
char StackSymb[MaxSize];
int operands;
//
void NumsToCapas(char [], int , char [], float []);
int CheckExpress(char);
int PriorChar(char,char);
int GetOperator(char [], char);
void counterPolishexp(char INexp[], int slen, char Outexp[]);
float CalcRevPolishexp(char [], float [], char [], int);
void main()
{
int ilen;
float iResult=0.0;
printf("enter a valid number string:\n");
memset(StackSymb,0,MaxSize);
memset(NumCapaTab,0,26); //A--NO.1, B--NO.2, etc.
gets(Srcexp);
ilen=strlen(Srcexp);
//printf("source expression:%s\n",Srcexp);
NumsToCapas(Srcexp,ilen,Capaexp,NumCapaTab);
printf("Numbers listed as follows:\n");
int i;
for (i=0; i<operands; ++i)
printf("%.2f ",NumCapaTab[i]);
printf("\nCapaexp listed in the following:\n");
printf("%s\n",Capaexp);
ilen=strlen(Capaexp);
counterPolishexp(Capaexp,ilen,RevPolishexp);
printf("RevPolishexp:\n%s\n",RevPolishexp);
ilen=strlen(RevPolishexp);
iResult=CalcRevPolishexp(validexp, NumCapaTab, RevPolishexp,ilen);
printf("\ncounterPolish expression:\n%s=%.6f\n",Srcexp,iResult);
}
void iPush(float value)
{
if(iTop<MaxSize) StaOperand[++iTop]=value;
}
float iPop()
{
if(iTop>-1)
return StaOperand[iTop--];
return -1.0;
}
void NumsToCapas(char Srcexp[], int slen, char Capaexp[], float NumCapaTab[])
{
char ch;
int i, j, k, flg=0;
int sign;
float val=0.0,power=10.0;
i=0; j=0; k=0;
while (i<slen)
{
ch=Srcexp[i];
if (i==0)
{
sign=(ch=='-')?-1:1;
if(ch=='+'||ch=='-')
{
ch=Srcexp[++i];
flg=1;
}
}
if (isdigit(ch))
{
val=ch-'0';
while (isdigit(ch=Srcexp[++i]))
{
val=val*10.0+ch-'0';
}
if (ch=='.')
{
while(isdigit(ch=Srcexp[++i]))
{
val=val+(ch-'0')/power;
power*=10;
}
} //end if
if(flg)
{
val*=sign;
flg=0;
}
} //end if
//write Capaexp array
// write NO.j to array
if(val)
{
Capaexp[k++]='A'+j;
Capaexp[k++]=ch;
NumCapaTab[j++]=val; //A--0, B--1,and C, etc.
}
else
{
Capaexp[k++]=ch;
}
val=0.0;
power=10.0;
//
i++;
}
Capaexp[k]='\0';
operands=j;
}
float CalcRevPolishexp(char validexp[], float NumCapaTab[], char RevPolishexp[], int slen)
{
float sval=0.0, op1,op2;
int i, rt;
char ch;
//recursive stack
i=0;
while((ch=RevPolishexp[i]) && i<slen)
{
switch(rt=GetOperator(validexp, ch))
{
case iMUL: op2=iPop(); op1=iPop();
sval=op1*op2;
iPush(sval);
break;
case iDIV: op2=iPop(); op1=iPop();
if(!fabs(op2))
{
printf("overflow\n");
iPush(0);
break;
}
sval=op1/op2;
iPush(sval);
break;
case iADD: op2=iPop(); op1=iPop();
sval=op1+op2;
iPush(sval);
break;
case iSUB: op2=iPop(); op1=iPop();
sval=op1-op2;
iPush(sval);
break;
case iCap: iPush(NumCapaTab[ch-'A']);
break;
default: ;
}
++i;
}
while(iTop>-1)
{
sval=iPop();
}
return sval;
}
int GetOperator(char validexp[],char oper)
{
int oplen,i=0;
oplen=strlen(validexp);
if (!oplen) return -1;
if(isalpha(oper)) return 4;
while(i<oplen && validexp[i]!=oper) ++i;
if(i==oplen || i>=4) return -1;
return i;
}
int CheckExpress(char ch)
{
int i=0;
char cc;
while((cc=validexp[i]) && ch!=cc) ++i;
if (!cc)
return 0;
return 1;
}
int PriorChar(char curch, char stach)
{
//栈外优先级高于(>)栈顶优先级时,才入栈
//否则(<=),一律出栈
if (curch==stach) return 0; //等于时应该出栈
else if (curch=='*' || curch=='/')
{
if(stach!='*' && stach!='/')
return 1;
}
else if (curch=='+' || curch=='-')
{
if (stach=='(' || stach==')')
return 1;
}
else if (curch=='(')
{
if (stach==')')
return 1;
}
return 0;
}
void counterPolishexp(char INexp[], int slen, char Outexp[])
{
int i, j, k,pr;
char t;
i=0;
j=k=0;
while (INexp[i]!='=' && i<slen)
{
if (INexp[i]=='(')
StackSymb[k++]=INexp[i];
//iPush(*(INexp+i));
else if(INexp[i]==')')
{
//if((t=iPop())!=-1)
while((t=StackSymb[k-1])!='(')
{
Outexp[j++]=t;
k--;
}
k--;
}
else if (CheckExpress(INexp[i])) // is oparator
{
// printf("operator %c k=%d\n",INexp[i],k);
while (k)
{
// iPush(*(INexp+i));
if(pr=PriorChar(INexp[i],StackSymb[k-1]))
break;
else
{
//if ((t=iPop())!=-1)
t=StackSymb[k-1]; k--;
Outexp[j++]=t;
}
} //end while
StackSymb[k++]=INexp[i]; //common process
}
else //if() 变量名
{
// printf("operand %c k=%d\n",INexp[i],k);
Outexp[j++]=INexp[i];
}
i++; //
}
while (k)
{
t=StackSymb[k-1]; k--;
Outexp[j++]=t;
}
Outexp[j]='\0';
}
不用栈的话
/* Note:Your choice is C IDE */
#include "stdio.h"
#include "stdlib.h"
#include "ctype.h"
int n=0;
char record[20];
float product();
float change();
float muli()
{
float summ;
summ=product();
while(record[n]=='-'||record[n]=='+')
{
switch(record[n])
{
case '+':n++;summ+=product();break;
case '-':n++;summ-=product();break;
}
}
return summ;
}
float product()
{
float sump;
sump=change();
while(record[n]=='*'||record[n]=='/')
{
switch(record[n])
{
case '*':n++;sump*=change();break;
case '/':n++;sump/=change();break;
}
}
return sump;
}
float change()
{
float sumc;
char rec[20],i=0;
if(record[n]=='(')
{
n++;
sumc=muli();
}
if(record[n]==')')
n++;
while(isdigit(record[n])||record[n]=='.')
{
while(isdigit(record[n])||record[n]=='.')
rec[i++]=record[n++];
rec[i]='\0';
sumc=atof(rec);
}
return sumc;
}
void main()
{
while(1)
{
n=0;
scanf("%s",record);
printf("%s=%g\n",record,muli());
}
}
追问
这个用栈的好麻烦啊,有没有简单点的
2014-06-29
展开全部
#include<iostream>
#define Stack_Size 50
using namespace std;
//运算符栈
typedef char StackElementTypeoptr;
typedef struct
{
StackElementTypeoptr elem[Stack_Size];
int top;
}SeqStack1;
//运算数栈
typedef int StackElementTypeovs;
typedef struct
{
StackElementTypeovs elem[Stack_Size];
int top;
}SeqStack2;
void InitStack(SeqStack1 &s)
{
s.top= -1;
}
void Push(SeqStack1 &s,StackElementTypeoptr x)
{
s.top++;
s.elem[s.top]=x;
}
void Gettop(SeqStack1 s,StackElementTypeoptr &e)
{
e=s.elem[s.top];
}
void Pop(SeqStack1 &s,StackElementTypeoptr &e)
{
e=s.elem[s.top];
s.top--;
}
void InitStack(SeqStack2 &s)
{
s.top= -1;
}
void Push(SeqStack2 &s,StackElementTypeovs x)
{
s.top++;
s.elem[s.top]=x;
}
void Gettop(SeqStack2 s,StackElementTypeovs &e)
{
e=s.elem[s.top];
}
void Pop(SeqStack2 &s,StackElementTypeovs &e)
{
e=s.elem[s.top];
s.top--;
}
//栈内优先级比较
int compare1(char c)
{
switch(c)
{
case '+' :
case '-' :return 3;break;
case '*' :
case '/' :return 4;break;
case '(' :return 2;break;
case '#' :return 0;break;
}
}
//栈外优先级比较
int compare2(char c)
{
switch(c)
{
case '+' :
case '-' :return 3;break;
case '*' :
case '/' :return 4;break;
case '(' :return 5;break;
case ')' :return 1;break;
case '#' :return 0;break;
}
}
int yunsuan(char a,int b1,int b2)
{
switch(a)
{
case '+': return b1+b2;break;
case '-': return b1-b2;break;
case '*': return b1*b2;break;
case '/': return b1/b2;break;
}
}
int main()
{
char c,x,t;//c存储输入的字符,x存储oper栈顶运算符
StackElementTypeovs y1,y2,result;//y1y2存储获取的两个ovs数字
SeqStack1 OPTR;SeqStack2 OVS;
InitStack(OPTR);Push(OPTR,'#');
InitStack(OVS);
cout<<"请输入一段表达式,以#结束:";
while(cin>>c)
{
if(isdigit(c)) {cin.putback(c);int n;cin>>n;Push(OVS,n);}//送回缓冲区,取出完整的数字
else {
Gettop(OPTR,x);
if(compare2(c)>compare1(x))Push(OPTR,c);
else
{
if(c==')')
{
while(x!='(')
{
Pop(OVS,y2);Pop(OVS,y1);
Pop(OPTR,t);
Push(OVS,yunsuan(t,y1,y2));
Gettop(OPTR,x);
if(x=='(') Pop(OPTR,t);
}
}
else
{
Pop(OVS,y2);Pop(OVS,y1);
Pop(OPTR,t);
Push(OVS,yunsuan(t,y1,y2));Push(OPTR,c);
}
}
}
if(c=='#')break;
}
char z;//把optr中栈顶的#弹出,然后继续运算
Pop(OPTR,z);
while(OVS.top!=0)
{
Pop(OVS,y2);Pop(OVS,y1);
Pop(OPTR,t);
Push(OVS,yunsuan(t,y1,y2));
}
Gettop(OVS,result);
cout<<"Answer is:"<<result<<endl;
return 0;
}
希望能帮到你。
#define Stack_Size 50
using namespace std;
//运算符栈
typedef char StackElementTypeoptr;
typedef struct
{
StackElementTypeoptr elem[Stack_Size];
int top;
}SeqStack1;
//运算数栈
typedef int StackElementTypeovs;
typedef struct
{
StackElementTypeovs elem[Stack_Size];
int top;
}SeqStack2;
void InitStack(SeqStack1 &s)
{
s.top= -1;
}
void Push(SeqStack1 &s,StackElementTypeoptr x)
{
s.top++;
s.elem[s.top]=x;
}
void Gettop(SeqStack1 s,StackElementTypeoptr &e)
{
e=s.elem[s.top];
}
void Pop(SeqStack1 &s,StackElementTypeoptr &e)
{
e=s.elem[s.top];
s.top--;
}
void InitStack(SeqStack2 &s)
{
s.top= -1;
}
void Push(SeqStack2 &s,StackElementTypeovs x)
{
s.top++;
s.elem[s.top]=x;
}
void Gettop(SeqStack2 s,StackElementTypeovs &e)
{
e=s.elem[s.top];
}
void Pop(SeqStack2 &s,StackElementTypeovs &e)
{
e=s.elem[s.top];
s.top--;
}
//栈内优先级比较
int compare1(char c)
{
switch(c)
{
case '+' :
case '-' :return 3;break;
case '*' :
case '/' :return 4;break;
case '(' :return 2;break;
case '#' :return 0;break;
}
}
//栈外优先级比较
int compare2(char c)
{
switch(c)
{
case '+' :
case '-' :return 3;break;
case '*' :
case '/' :return 4;break;
case '(' :return 5;break;
case ')' :return 1;break;
case '#' :return 0;break;
}
}
int yunsuan(char a,int b1,int b2)
{
switch(a)
{
case '+': return b1+b2;break;
case '-': return b1-b2;break;
case '*': return b1*b2;break;
case '/': return b1/b2;break;
}
}
int main()
{
char c,x,t;//c存储输入的字符,x存储oper栈顶运算符
StackElementTypeovs y1,y2,result;//y1y2存储获取的两个ovs数字
SeqStack1 OPTR;SeqStack2 OVS;
InitStack(OPTR);Push(OPTR,'#');
InitStack(OVS);
cout<<"请输入一段表达式,以#结束:";
while(cin>>c)
{
if(isdigit(c)) {cin.putback(c);int n;cin>>n;Push(OVS,n);}//送回缓冲区,取出完整的数字
else {
Gettop(OPTR,x);
if(compare2(c)>compare1(x))Push(OPTR,c);
else
{
if(c==')')
{
while(x!='(')
{
Pop(OVS,y2);Pop(OVS,y1);
Pop(OPTR,t);
Push(OVS,yunsuan(t,y1,y2));
Gettop(OPTR,x);
if(x=='(') Pop(OPTR,t);
}
}
else
{
Pop(OVS,y2);Pop(OVS,y1);
Pop(OPTR,t);
Push(OVS,yunsuan(t,y1,y2));Push(OPTR,c);
}
}
}
if(c=='#')break;
}
char z;//把optr中栈顶的#弹出,然后继续运算
Pop(OPTR,z);
while(OVS.top!=0)
{
Pop(OVS,y2);Pop(OVS,y1);
Pop(OPTR,t);
Push(OVS,yunsuan(t,y1,y2));
}
Gettop(OVS,result);
cout<<"Answer is:"<<result<<endl;
return 0;
}
希望能帮到你。
追问
fatal error C1189: #error : eh.h is only for C++!
有错误啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询