Ⅰ 請問怎麼用C語言編寫四則運算的程序呢
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define size 20
typedef float NUM;
typedef struct
{
NUM data[size];
int top;
}*Space,Lnode;
void begin();
void initialize(Space x,Space y);
void input(Space x,Space y);
int is_operator(char a_operator);
int pushf(Space s,float x);
int pushc(Space s,char x);
int empty(Space s);
int priority(char o);
int popf(Space s,float *x);
int popc(Space s,int *x);
float result(int a_operator,float operand1,float operand2);
main()
{
begin();
system("pause");
}
void begin()
{
Lnode operand,a_operator;//定義兩個指向結構體的指針
Space s_operand=&operand,s_operator=&a_operator;
initialize(s_operand,s_operator);//初始化
input(s_operand,s_operator);//開始
}
void initialize(Space s,Space t)//初始化數據棧、運算符棧
{
s->top=0;
t->top=0;
}
void input(Space x,Space y)
{
int i,j,position=0,op=0;
float operand1=0,operand2=0,evaluate=0;//用來儲存兩個計算數 和 一個結果
char string[50];//所輸入的表達式
char temp[50];//用來臨時存放小數
printf("請輸入表達式: ");
gets(string);
while(string[position]!='\0'&&string[position]!='\n')
{
i=0;
strcpy(temp,"0");
if(is_operator(string[position]))//判斷是否為運算符
{
if(!empty(y))
{
while(!empty(y)&&priority(string[position])<=priority(y->data[y->top-1]))//判斷優先順序
{
popf(x,&operand1);
popf(x,&operand2);
popc(y,&op);
pushf(x,result(op,operand1,operand2));//計算結果
}
}
pushc(y,string[position]);//運算符入棧
position++;
}
while((string[position]!='\0'&&string[position]!='\n')&&(!is_operator(string[position])))//數據存入temp數組
{
temp[i]=string[position];
i++;
position++;
}
pushf(x,atof(temp));//將數組強制轉換為浮點型 然後進行入棧操作 x為指向數據棧的指針 atof函數即使強制轉換類型
}
while(!empty(y))
{
popc(y,&op);
popf(x,&operand1);
popf(x,&operand2);
pushf(x,result(op,operand1,operand2));
}
popf(x,&evaluate);
printf("結果是 : %f",evaluate);
}
int pushf(Space s,float x)//數據入棧
{
if(s->top==size)
return 0;
s->data[s->top]=x;
s->top++;
return 1;
}
int pushc(Space s,char x)//運算符入棧
{
if(s->top==size)
return 0;
s->data[s->top]=x;
s->top++;
return 1;
}
int popf(Space s,float *x)//數據出棧
{
if(s->top==0)
return 0;
else
{
s->top--;
*x=s->data[s->top];
return 1;
}
}
int popc(Space s,int *x)//運算符出棧
{
if(s->top==0)
return 0;
else
{
s->top--;
*x=s->data[s->top];
return 1;
}
}
int empty(Space s)//判斷棧空
{
if(s->top==0)
return 1;
else
return 0;
}
int is_operator(char Operator) //判斷是否為運算符
{
switch(Operator)
{
case '+':
case '-':
case '*':
case '/':
return 1;
default:
return 0;
}
}
int priority(char o) //判斷運算符的優先順序別
{
switch(o)
{
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
default:
return 0;
}
}
float result(int a_operator,float operand1,float operand2)//計算結果
{
switch(a_operator)
{
case '+':
return operand2+operand1;
case '-':
return operand2-operand1;
case '*':
return operand2*operand1;
case '/':
return operand2/operand1;
}
}
這是用棧寫的 沒有寫輸入錯誤的判斷 你自己添加一下吧
我是因為剛好有一個現成的程序
Ⅱ 用C語言設計一個兒童四則運算程序,根據用戶的設定自動出題並對全部回答進行判斷,全部答完後統計正確率
/*程序功能:一開始程序自動進入第一輪運算測試,通過按「p」「m」「t」「d」而選擇加、減、乘、除運算,再每做完一道題
時按回車繼續該運算,按「p」「m」「t」「d」進行相應運算切換,其間可按「s」退出該輪測試;
若按了「s」則選擇是否進行下一輪,按分別按「y」「n」表示是或不是。*/
# include <stdio.h>
# include <time.h>
# include <stdlib.h>
void main()
{int p[2]={0},m[2]={0},t[2]={0},d[2]={0},i=0,j=0,k=0;//數組分別存放每輪做相應運算的總數和正答個數;k、j分別記所做輪數和每輪所做的題數
char c,exit='y';
void pluss(int *p);
void minus(int *m);
void times(int *t);
void division(int*t);
void test(int*p,int*m,int*t,int*d);
while(exit!='n'){ //輸出提示語
printf("\n 按 \"p\"--->\"+\"運算; 按 \"m\"--->\"-\"運算;\n 按 \"t\"--->\"*\"運算; 按 \"d\"--->\"/\"運算;");
printf(" 按 \"s\"--->\"退出該輪測試\"\n 做完每道題後您可按回車鍵繼續該運算或切換到別的運算");
printf("\n\n歡迎進入第%d輪測試 請選擇運算 ",++k);
c=getchar();
if(k!=1)c=getchar();//選擇運算
while(c!='s'){
while(c=='p'||(c!='s'&&c!='m'&&c!='t'&&c!='d')){printf("第%d道 ",++j);pluss(p);c=getchar();c=getchar();}
while(c=='m'||(c!='s'&&c!='p'&&c!='t'&&c!='d')){printf("第%d道 ",++j);minus(m);c=getchar();c=getchar();}
while(c=='t'||(c!='s'&&c!='m'&&c!='p'&&c!='d')){printf("第%d道 ",++j);times(t);c=getchar();c=getchar();}
while(c=='d'||(c!='s'&&c!='m'&&c!='t'&&c!='p')){printf("第%d道 ",++j);division(d);c=getchar();c=getchar();}
}
test(p,m,t,d); //按s後退出一輪測試,調用函數給出測試結果
printf("\n 按 \"y\"---> \"進入第%d輪\" 按 \"n\"---> \"退出程序\" ",k+1);
exit=getchar();exit=getchar();
if(exit!='n'){ //按s後選則是否進入下一輪
j=0;
for(i=0;i<2;i++)p[i]=m[i]=t[i]=d[i]=0;
}
}
printf("\n\n Bye-Bye\n\n");
}
void pluss(int *p){
int y,x,sum;
srand (time(NULL));
x=(int)rand()%10+1;
y=(int)rand()%10+1;
printf("%d+%d=",x,y);
scanf("%d",&sum);
p[0]++;
if(sum==x+y){printf("Congratuations! ");p[1]++;}
else printf("Sorry! Right answer is %d ",x+y);}
void minus(int *m){
int y,x,minus;
srand (time(NULL));
x=(int)rand()%10+1;
y=(int)rand()%10+1;
m[0]++;
printf("%d-%d=",x+y,y);
scanf("%d",&minus);
if(minus==x){printf("Congratulations! ");m[1]++;}
else printf("Sorry! Right answer is %d ",x);
}
void times(int *t){
int y,x,tim;
srand (time(NULL));
x=(int)rand()%10+1;
y=(int)rand()%10+1;
printf("%d*%d=",x,y);
scanf("%d",&tim);
t[0]++;
if(tim==x*y){printf("Congratulations! ");t[1]++;}
else printf("Sorry! Right answer is %d ",x*y);
}
void division(int *d){
int y,x,div;
srand (time(NULL));
x=(int)rand()%10+1;
y=(int)rand()%10+1;
printf("%d/%d=",x*y,y);
scanf("%d",&div);
d[0]++;
if(div==x){printf("Congratulations! ");d[1]++;}
else printf("Sorry! Right answer is %d ",x);
}
void test(int *p,int*m,int*t,int*d){
int s[2];
float rate;
s[0]=p[0]+m[0]+t[0]+d[0];
s[1]=p[1]+m[1]+t[1]+d[1];
rate=s[1]/(s[0]*1.0);
if(rate>0.8)printf("\nYou have done a good job !\n\n");
else if(rate<0.6)printf("\nYou have done a bad job !\n\n");
else printf("\nCome on! You can do it better !\n\n");
if(s[0]!=0)
printf("total done:%d right:%d rate:%5.2f%c\n",s[0],s[1],rate*100,'%');
else printf("total done:0 right:0\n");
if(p[0]!=0)
printf("\"+\" done:%d right:%d rate:%5.2f%c ",p[0],p[1],(p[1]/(p[0]*1.0))*100,'%');
else printf("\"+\" done:%d right:%d ",p[0],p[1]);
if(m[0]!=0)
printf("\"-\" done:%d right:%d rate:%5.2f%c\n",m[0],m[1],(m[1]/(m[0]*1.0))*100,'%');
else printf("\"-\" done:%d right:%d\n",m[0],m[1]);
if(t[0]!=0)
printf("\"*\" done:%d right:%d rate:%5.2f%c ",t[0],t[1],(t[1]/(t[0]*1.0))*100,'%');
else printf("\"*\" done:%d right:%d ",t[0],t[1]);
if(d[0]!=0)
printf("\"/\" done:%d right:%d rate:%5.2f%c\n",d[0],d[1],(d[1]/(d[0]*1.0))*100,'%');
else printf("\"/\" done:%d right:%d\n",d[0],d[1]);
}
找過來的,運行下看看是否可行。
Ⅲ C語言。編程實現簡單的四則運算。比如輸入3+5=8,輸入3*5=15。要有演算法分析圖和完整的源代碼
#include <stdio.h>
int main()
{int a,b,c;
char op;
scanf("%d%c%d",&a,&op,&b);
while(op!='+'&&op!='-'&&op!='*'&&op!='/')
{printf("只能計算加減乘除,請重新輸入: ");
scanf("%d%c%d",&a,&op,&b);
}
if(op=='/'&&b==0)
{printf("divided by zero. ");
return (1);
}
switch(op)
{case '+':c=a+b;break;
case '-':c=a-b;break;
case '*':c=a*b;break;
case '/':c=a/b;break;
}
printf("%d%c%d=%d ",a,op,b,c);
return 0;
}