当前位置:首页 » 编程语言 » c语言小学生四则运算程序流程图
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言小学生四则运算程序流程图

发布时间: 2023-08-22 03:53:06

Ⅰ 请问怎么用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;

}