谁帮忙用switch编写一个简单的计算器,实现两个整型数的四则运算 。c语言 10
3个回答
展开全部
我都替你写好了,不只是两个数,几个数都行。
支持的数学函数一大堆,pi, e, torad, sin, cos, tan, sinh, cosh, tanh,arcsin, arccos, arctan, arcsinh, arccosh, arctanh,sqrt, abs, lg, ln, exp, gamma, rand, round, int, ceil, floor"方便你计算答案。
会编译吧,我就不帮你编译了。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
#define STACK_SIZE 4096
static const char* KEY_WORDS[]= {"e", "pi", "sqrt", "lg", "ln", "sin", "cos", "tan", "arcsin", "arccos", "arctan", "torad", "abs", "round", "floor", "ceil", "exp", "sinh", "cosh", "tanh", "arcsinh", "arccosh", "arctanh", "int", "gamma", "rand", NULL};
char STACK1[STACK_SIZE]= {0};
char STACK2[STACK_SIZE]= {0};
double STACK3[STACK_SIZE]= {0};
long long fact(long long n)
{
return (n<2) ?1 :n*(fact(n-1));
}
double RevPolishCore(const char* expression)
{
char *op=(char*)expression, *S1=STACK1, *S2=STACK2, **key, *cp, *kp;
double *S3=STACK3, di, ni;
int brackets=0;
STACK3[0]=0;
while(*op!='\0')
{
switch(*op)
{
case ' ' :
case '\t':
case '\r':
case '\n':
op++;
continue;
case '(':
brackets++;
*(++S1)=*op;
if(*(op+1)=='-' || *(op+1)=='+')
{
*(S2++)='0', *(S2++)=' ';
}
break;
case ')':
if(brackets ==0)
{
fputs("The brackets or ')' are not need", stderr);
exit(1);
}
brackets--;
while(*S1!='(')
{
*(S2++)=*(S1--);
}
S1--;
break;
case '+':
case '-':
while(S1!=STACK1 && *S1!='(')
{
*(S2++)=*(S1--);
}
*(++S1)=*op;
break;
case '&':
case '|':
case '^':
while('A'<=(*S1) && (*S1)<='Z')
{
*(S2++)=*(S1--);
}
*(++S1)=*op;
break;
case '!':
*(S2++)=*op;
break;
case '%':
case '*':
case '/':
while(('A'<=(*S1) && (*S1)<='Z') ||*S1=='%' ||*S1=='*' ||*S1=='/' ||*S1=='^')
{
*(S2++)=*(S1--);
}
*(++S1)=*op;
break;
default :
if('a'<=*op && *op<='z')
{
key=(char**)KEY_WORDS;
while(*key !=NULL)
{
cp=op, kp=*key;
while((*cp==*kp || *cp+32==*kp) && *kp!='\0')
{
cp++, kp++;
}
if(!('a'<=*cp && *cp<='z') && (*kp=='\0'))
{
op=cp;
break;
}
key++;
}
if(*key !=0)
{
*(S2++)='.';
*(S2++)=' ';
while('A'<=(*S1) && (*S1)<='Z')
{
*(S2++)=*(S1--);
}
*(++S1)=key-(char**)KEY_WORDS+65;
continue;
}
else
{
fputs("Unrecognized math function\n", stderr);
exit(1);
}
}
else if(('0'<=*op && *op<='9') || (*op=='.'))
{
while(('0'<=*op && *op<='9') || (*op=='.'))
{
*(S2++)=*(op++);
}
if('a'<=*op && *op<='z')
{
fputs("Missing required operator\n", stderr);
exit(1);
}
op--;
*(S2++)=' ';
}
else
{
fputs("Unrecognized operator\n", stderr);
exit(1);
}
break;
}
op++;
}
if(brackets !=0)
{
fputs("The brackets '(' are not closed", stderr);
exit(1);
}
while(S1 !=STACK1)
{
*(S2++)=*(S1--);
}
*S2=' ';
op=STACK2;
while(*op!=' ')
{
switch(*op)
{
case 'A':
*S3=2.7182818284590452;
break;
case 'B':
*S3=3.1415926535897932;
break;
case 'C':
if(*S3 <0)
{
fputs("Negative numbers have no square root\n", stderr);
exit(1);
}
*(S3-1)=sqrt(*S3);
S3--;
break;
case 'D':
if(*S3 <0)
{
fputs("Negative numbers are not logarithmic\n", stderr);
exit(1);
}
*(S3-1)=log10(*S3);
S3--;
break;
case 'E':
if(*S3 <0)
{
fputs("Negative numbers have no natural logarithms\n", stderr);
exit(1);
}
*(S3-1)=log(*S3);
S3--;
break;
case 'F':
*(S3-1)=sin(*S3);
S3--;
break;
case 'G':
*(S3-1)=cos(*S3);
S3--;
break;
case 'H':
if(*S3==3.1415926535897932/2)
{
fputs("The pi/2 has no tangent\n", stderr);
exit(1);
}
*(S3-1)=tan(*S3);
S3--;
break;
case 'I':
*(S3-1)=asin(*S3);
S3--;
break;
case 'J':
*(S3-1)=acos(*S3);
S3--;
break;
case 'K':
*(S3-1)=atan(*S3);
S3--;
break;
case 'L':
*(S3-1)=(*S3)*3.1415926535897932/180.0;
S3--;
break;
case 'M':
*(S3-1)=fabs(*S3);
S3--;
break;
case 'N':
*(S3-1)=round(*S3);
S3--;
break;
case 'O':
*(S3-1)=floor(*S3);
S3--;
break;
case 'P':
*(S3-1)=ceil(*S3);
S3--;
break;
case 'Q':
*(S3-1)=exp(*S3);
S3--;
break;
case 'R':
*(S3-1)=sinh(*S3);
S3--;
break;
case 'S':
*(S3-1)=cosh(*S3);
S3--;
break;
case 'T':
*(S3-1)=tanh(*S3);
S3--;
break;
case 'U':
*(S3-1)=asinh(*S3);
S3--;
break;
case 'V':
*(S3-1)=acosh(*S3);
S3--;
break;
case 'W':
*(S3-1)=atanh(*S3);
S3--;
break;
case 'X':
*(S3-1)=(int)(*S3);
S3--;
break;
case 'Y':
if(*S3 <0)
{
fputs("Negative numbers have no factorial", stderr);
exit(1);
}
*(S3-1)=tgamma(*S3);
S3--;
break;
case 'Z':
if(*S3 <0)
{
fputs("A negative number can not be used as a random upper bound", stderr);
exit(1);
}
else if(*S3 <2)
{
*(S3-1)=rand() % 8192 /8192.0;
}
else
{
*(S3-1)=rand() % (int)(*S3);
}
S3--;
break;
case '+':
*(S3-1)+=*S3;
S3--;
break;
case '-':
*(S3-1)-=*S3;
S3--;
break;
case '*':
*(S3-1)*=*S3;
S3--;
break;
case '%':
case '/':
if(*S3 !=0)
{
if(*op=='%')
{
*(S3-1)=(int)*(S3-1) % (int)*S3;
}
else
{
*(S3-1)/=*S3;
}
}
else
{
fputs("Divisor is zero error\n", stderr);
exit(1);
}
S3--;
break;
case '|':
*(S3-1) =(int)(*(S3-1)) | (int)(*S3);
S3--;
break;
case '&':
*(S3-1) =(int)(*(S3-1)) & (int)(*S3);
S3--;
break;
case '^':
if(*(S3-1)==0 && *S3<0)
{
fputs("Function pow's divisor is zero error\n", stderr);
exit(1);
}
*(S3-1)=pow(*(S3-1), *S3);
S3--;
break;
case '!':
if(*S3 <0)
{
fputs("Negative numbers have no factorial\n", stderr);
exit(1);
}
*S3=fact((long long)(*S3));
break;
default :
di=0, ni=1;
while('0'<=*op && *op<='9')
{
di=10*di+(*op)-'0';
op++;
}
if(*op=='.')
{
op++;
while('0'<=*op && *op<='9')
{
di=10*di+(*op)-'0';
op++, ni*=10;
}
}
*(++S3)=di/ni;
break;
}
op++;
}
if (isinf(*S3)||isnan(*S3))
{
fputs("Overflow or illegal operation is calculated", stderr);
exit(1);
}
return *S3;
}
int main()
{
char inPut[4096];
scanf("%s", inPut);
float result = RevPolishCore(inPut);
printf("%.12f", result);
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询