计算器.c

#include <stdio.h>
#include <stdlib.h>
#include<ctype.h>
#include<mem.h>
//#include<alloc.h>
#include<string.h>
char token;
char buff[100];
int f=0;

#include <stdlib.h>
#include<ctype.h>
#include<mem.h>
//#include<alloc.h>
#include<string.h>
char token;
char buff[100];
int f=0;

int c=0;
int n=0;
double exp(char *p,int flag);
double term(char *p);
double factor(char *p);
void match(char expectedToken);
int get();

void error();

tradeMark();

 main(){ 

 tradeMark(); 

  while(1){ 
 double result; 
 int flag=1;
 char ch;
 int count=0;
 f=0;
 c=0; 
 printf("printf 'q' to ESC\n");
 ch=getchar(); 
 if(ch!='q'){ 
 while(ch!='-'&&!isdigit(ch)&&ch!='('){
 f=1;
 count++;
 if(count==4)
 break;
 error();
 ch=getchar();
 }
 if(ch=='-'){
 flag=-1;
 buff[c++]=ch;
 count=0;
 while(get()){
 f=1;
 count++;
 if(count==4)
 break;
 error();
 }
 }
 else 
 token=ch;
 buff[c++]=token;
 if(f)
 ch='q';
 }
if(ch=='q'){
printf("EXPRESS ERROR!!!");
break;
}
ch='0';
if(flag==-1){
if(buff[c-1]=='(')
result=exp(&ch,1)*flag;
else
result=exp(&ch,-1);
}
else
result = exp(&ch,1);
if(f){
printf("EXPRESS ERROR!!!");
break;
}
if (token == '\n')
printf("Result is: %g\n", result); 
  }
 }
 int get(){
  char temp;
  temp=getchar();
  if(temp=='('||isdigit(temp)){
   token=temp;
   return 0;
  }
  return 1;
 }
 double exp(char *p,int flag){ 
  double temp ;
  int count=0;
  temp=term(p)*flag;  
  if(f)
   exit(-1);
  while (token == '+' || token == '-')
   switch(token){
case '+':
while(get()){ 
count++;
f=1;
if(count==4)
exit(-1);
error();

buff[c++]=token;
temp += term(p);
if(f)
exit(-1);
break;
case '-': 
while(get()){ 
count++;
f=1;
if(count==4)
exit(-1);
error();

buff[c++]=token;
temp -= term(p);
if(f)
exit(-1);
break;
  }
  return temp;
 }
double term(char *p){
 int count=0;
 double temp = factor(p);  
 if(f)
  exit(-1); 
 while (token == '*' || token == '/')
  switch(token){
              case '*':
 while(get()){ 
 count++;
 f=1;
 if(count==4)
 exit(-1);
 error();
 }
 buff[c++]=token;
 temp *= factor(p);
 if(f)
 exit(-1);
 break;
 case '/': 
 while(get()){ 
 count++;
 f=1;
 if(count==4)
 exit(-1);
 error();
 }
 buff[c++]=token;
 temp/=factor(p);
 if(f)
 exit(-1);
 break;
 }
 return temp;
}
double factor(char *p){
 char ch;
 char *sym,symbol; 
 double temp;
 int count=0,flag=1;
 symbol='0'; // symbol=(char *)malloc(1);*symbol='0'; 
 sym=&symbol; 
 if(token == '('){
n++;
  symbol='(';
  ch=getchar();
  while(ch!='-'&&!isdigit(ch)&&ch!='('){
   f=1;
   if(count==4)
    exit(-1);
   error();
   ch=getchar();
  }  
  if(ch=='-'){
 flag=-1;
 buff[c++]=ch;
 count=0;
 while(get()){
 f=1;
 count++;
 if(count==4)
 exit(-1);
 error();
 } 
  }
  else
 token=ch;
  buff[c++]=token; 
  if(flag==-1){
 if(buff[c-1]=='(')
 temp=exp(sym,1)*flag;
 else
 temp=exp(sym,-1);
  }
  else
 temp = exp(sym,1);
  match(')',p); 
  if(f)
 exit(-1);
 }
 else if (isdigit(token)){ 
ungetc(token, stdin);
scanf("%lf", &temp);  
gcvt(temp,10,&buff[c-1]);
c=strlen(buff);
token=getchar();
if(token==')'&&*p=='('){
buff[c++]=token;
return temp;
}
while(token!='+'&&token!='-'&&token!='*'&&token!='/'&&token!='\n'){  
f=1;
count++;
if(count==4)
exit(-1);
error(-1);
token=getchar();
if(token==')'&&*p=='(')
break; 

buff[c++]=token;
 } 
 return temp;
}
void match(char expectedToken){ 
 int count=0;
 if (expectedToken == token){
n--;  
  token=getchar();  
  while(token!='+'&&token!='-'&&token!='*'&&token!='/'&&token!='\n'&&token==')'){
 if(token==')'&&n){
 n--;
 break;
 }
 f=1;   
 count++;
 if(count==4)
 exit(-1);
 error();
 token=getchar();
 
  }
  buff[c++]=token;
 } 
 else {
c=c-1;
while(token!=')'){
f=1;
count++;
if(count==4)
exit(-1);
error();
token=getchar();
}
buff[c++]=token;
match(token);
 }
}


void error(){
 int i;
 f=0;
 fflush(stdin);
 printf("\nEXPRESS ERROR!!!\n");
 for(i=0;i<c;i++)
  printf("%c",buff[i]);
}
tradeMark(){
printf("***************************************\n");
printf("*                                              *\n");
printf("*             兵主出品                        *\n");
printf("*                                               *\n");
printf("****************************************\n");
}

 
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值