/* 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 "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
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
}
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
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
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
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
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';
}
如果你要能够 4+5*6=34,那是很复杂的代码啦,相对新手而言很复杂的,你如果真的要,我也给你,超详细的解析,就不用我再写了,有不懂再问我:
http://zhidao.baidu.com/question/144607634.html
简单的就如这个,能计算简单的2+2,2*2,2/2,2-2,为了简单出错处理什么都省略去了,代码如下:
#include
#include
main()
{
float a,b;
char c;
printf("计算小程序\n计算方法:如计算1+2,则输入1+2按回车\n(保留2位)\n");
scanf("%f %c %f",&a,&c,&b);
switch(c)
{
case '+':printf("%.2f+%.2f=%.2f\n",a,b,a+b);break;
case '-':printf("%.2f-%.2f=%.2f\n",a,b,a-b);break;
case '*':printf("%.2f*%.2f=%.2f\n",a,b,a*b);break;
case '/':printf("%.2f/%.2f=%.2f\n",a,b,a/b);break;
}
system("pause");
return 0;
}
#include
float Plus(float a)////加法
{
float sum ,n;
//printf("ewfwe");
scanf("%f",&n);
sum=a+n;
return sum;
}
float Subtract(float a)/////减法
{
float sum ,n;
scanf("%f",&n);
sum=a-n;
return sum;
}
float Multiplication(float a)////乘法
{
float sum ,n;
scanf("%f",&n);
sum=a*n;
return sum;
}
float Division(float a)//////除法
{
float sum ,n;
scanf("%f",&n);
sum=a/n;
return sum;
}
main()
{
float a,sum;
char c;
printf("请输入数字!\n");
scanf("%f",&a);
do
{
scanf("%c",&c);
switch(c)
{
case 43:
sum=Plus(a);
break;
case 45:
sum=Subtract(a);
break;
case 42:
sum=Multiplication(a);
break;
case 47:
sum=Division(a);
break;
default:
break;
}
a=sum;
}while(c!='\n');
printf("%f\n",sum);
}
原原 15:35:31
#include
float Plus(float a)////加法
{
float sum ,n;
//printf("ewfwe");
scanf("%f",&n);
sum=a+n;
return sum;
}
float Subtract(float a)/////减法
{
float sum ,n;
scanf("%f",&n);
sum=a-n;
return sum;
}
float Multiplication(float a)////乘法
{
float sum ,n;
scanf("%f",&n);
sum=a*n;
return sum;
}
float Division(float a)//////除法
{
float sum ,n;
scanf("%f",&n);
sum=a/n;
return sum;
}
main()
{
float a,sum;
char c;
printf("请输入数字!\n");
scanf("%f",&a);
do
{
scanf("%c",&c);
switch(c)
{
case 43:
sum=Plus(a);
break;
case 45:
sum=Subtract(a);
break;
case 42:
sum=Multiplication(a);
break;
case 47:
sum=Division(a);
break;
default:
break;
}
a=sum;
}while(c!='\n');
printf("%f\n",sum);
}
这个一会写不出来,你可以参考一下这个链接,
http://apps.hi.baidu.com/share/detail/30272236
希望对你有帮助。GOOD LUCK!