四则运算

#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());
 }  
}

posted @ 2018-03-09 17:45  wang蛋蛋  阅读(158)  评论(1编辑  收藏  举报