编写计算器程序 c语言
#include <stdio.h>
#include <stdlib.h>
char s[]="0123456789abcdef",ss[100];
int f1(int x,int y)
{
int t=1;
while(y--) t*=x;
return t;
}
char *f2(int x,int y)
{
int i=0,j;
while(x){ss[i++]=s[x%y];x/=y;}
ss[i]='\0';
for(j=0,i--;j<i;j++,i--)
ss[i]^=ss[j]^=ss[i]^=ss[j];
return ss;
}
int f3(int x,int y)
{
int i=1,j=0;
while(x){j+=x%10*i;x/=10;i*=y;}
return j;
}
void ff(int x,char t,int y)
{
switch(t)
{
case '+':printf("%d+%d=%d\n",x,y,x+y);break;
case '-':printf("%d-%d=%d\n",x,y,x-y);break;
case '*':printf("%d*%d=%d\n",x,y,x*y);break;
case '/':printf("%d/%d=%f\n",x,y,1.0*x/y);break;//y!=0
case '^':printf("%d^%d=%d\n",x,y,f1(x,y));break;
case '&':printf("%d&%d=%s\n",x,y,f2(x,y));break;
case '@':printf("%d@%d=%d\n",x,y,f3(x,y));break;
case '?':exit(0);
default:printf("Error\n");
}
}
int main(int argc, char *argv[])
{
int x,y;
char t;
while(1)
{
scanf("%d%c%d",&x,&t,&y);
ff(x,t,y);
}
return 0;
}