㈠ 用c語言做個簡單的計算器 要有加減乘除 先乘後加 先算括弧里的
//給你個全新的代碼
/*輸入表達式只能包含整數,+-*/和()*/
#include<stdio.h>
#include<string.h>
int add(int x,int y);
int sub(int x,int y);
int mul(int x,int y);
int div(int x,int y);
int getach(); //用於獲取一個合法字元
int getid(); //咐薯虛對合法字元進行判斷處理
int cal(); //計算表達式的值
int num,curch;
int (*func[])(int x,int y)={add,sub,mul,div}; //函數指針數組,數組元素分別指向+-*/運算函數
char chtbl[]="+-*/衡燃()=";
char corch[]="+-*/()=0123456789";
void main()
{
int value;
printf("請輸入要計算的表達式,以=結束:");
getach();
while(curch!='=')
{
value=cal();
printf("結果為:%d\n",value);
printf("請輸入要計算的表達式,以=結束:");
getach();
}
}
int add(int x,int y)
{
return x+y;
}
int sub(int x,int y)
{
return x-y;
}
int mul(int x,int y)
{
return x*y;
}
int div(int x,int y)
{
return x/y;
}
int getach()
{
unsigned int i;
while(true)
{
curch=getchar();
if(curch==EOF)
return -1;
for(i=0;corch[i]&&curch!=corch[i];i++)
;
if(i<strlen(corch))
break;
}
return curch;
}
int getid()
{
int i;
if(curch>='0'&&curch<='9')
{
for(num=0;curch>='0'&&curch<='9';getach())
num=num*10+curch-'手兆0';
return -1;
}
else
{
for(i=0;chtbl[i];i++)
{
if(chtbl[i]==curch)
break;
}
if(i<=5)
getach();
return i;
}
}
int cal()
{
int x1,x2,x3,op1,op2,i;
i=getid();
if(i==4)
x1=cal();
else
x1=num;
op1=getid();
if(op1>=5)
return x1;
i=getid();
if(i==4)
x2=cal();
else
x2=num;
op2=getid();
while((op2>=0)&&(op2<5))
{
i=getid();
if(i==4)
x3=cal();
else
x3=num;
if((op1/2==0)&&(op2/2==1))
x2=func[op2](x2,x3);
else
{
x1=func[op1](x1,x2);
x2=x3;
op1=op2;
}
op2=getid();
}
return func[op1](x1,x2);
}
㈡ C語言作業,編寫一個簡單的計算器,實現加減乘除運算,謝謝大家
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
intcls()
{
system("cls");
圓旅return0;
}
intpause()
{
system("pause");
return0;
}
intswitchs(doublea,doubleb,chars)
{
switch(s)
{
case'+':
{
cls();
printf("a+b ");
Sleep(500);
printf("=%0.3lf+%0.3lf ",a,b);
Sleep(500);
printf("=%0.3lf 07",a+b);
Sleep(500);
}break;
case'-':
{
cls();
printf("a-b ");
Sleep(500);
printf("=%0.3lf-%0.3lf ",a,b);
Sleep(500);
printf("=%0.3lf 07",a-b);
Sleep(500);
}break;
case'*':
{
cls();
printf("a*b ");
Sleep(500);
printf("=%0.3lf*%0.3lf "升沒,a,b);
Sleep(500);
printf("=%0.3lf 07",a*b);
Sleep(500);
}break;
case'/':
{
if(b==0)
{
cls();
printf("³ýÊý吵腔納²»ÄÜΪ0£¡ 07");
Sleep(500);
gotoR;
}
else
{
cls();
printf("a/b ");
Sleep(500);
printf("=%0.3lf/%0.3lf ",a,b);
Sleep(500);
printf("=%0.5lf 07",a/b);
Sleep(500);
}
}break;
default:
{
cls();
printf("error! 07");
Sleep(500);
pause();
cls();
gotoR;
}
}
R:;
}
intmain(void)
{
charc;
doublea,b;
chars;
R: printf("