C语言-四则运算
C语言课程设计小学生四则运算
#include"stdio.h"#include"stdlib.h"void main(){int a,b,c,d,e,f,g,t;printf("****************************欢迎使用!************************\n");printf("\n");printf("1.加法运算\n");printf("2.减法运算\n");printf("3.乘法运算\n");printf("4.除法运算\n");printf("*****************************请选择运算类型:");scanf("%d",&a);while(a<1||a>4){printf("输入错误,请重新输入");scanf("%d",&a);}if(a==1){printf("5,一位数加一位数\n");printf("6,一位数加两位数\n");printf("7,两位数加两位数\n");printf("*********************************请输入题型:");scanf("%d",&a);while(a<5||a>7){printf("输入错误,请重新输入");scanf("%d",&a);}if(a==5){loop:b=rand()%10;c=rand()%10;d=b+c;printf("%d+%d=?\n",b,c);printf("请输入答案:");scanf("%d",&e);while(e!=d){printf("回答错误,请重做");printf("%d+%d=?\n",b,c);printf("请输入答案:");scanf("%d",&e);}printf("回答正确!是否做下一题?\n");printf("输入0表示是,输入1表示否\n");scanf("%d",&f);if(f==0)goto loop;if(f==1)printf("欢迎再次使用!");}if(a==6){loo: b=rand()%10;c=rand()%100;if (c<10) c=c+10;d=b+c;printf("%d+%d=?\n",b,c);printf("请输入答案:");scanf("%d",&e);while(e!=d){printf("回答错误,请重做");printf("%d+%d=?\n",b,c);printf("请输入答案:");scanf("%d",&e);}printf("回答正确!是否做下一题?\n");printf("输入0表示是,输入1表示否\n");scanf("%d",&f);if(f==0)goto loo;if(f==1)printf("欢迎再次使用!");}if(a==7){loog:b=rand()%100;c=rand()%100;if(c<10) c=c+10;if(b<10) b=b+10;d=b+c;printf("%d+%d=?\n",b,c);printf("请输入答案:");scanf("%d",&e);while(e!=d){printf("回答错误,请重做");printf("%d+%d=?\n",b,c);printf("请输入答案:");scanf("%d",&e);}printf("回答正确!是否做下一题?\n");printf("输入0表示是,输入1表示否\n");scanf("%d",&f);if(f==0)goto loog;if(f==1)printf("欢迎再次使用!");}}if(a==2){printf("5,一位数减一位数\n");printf("6,两位数减一位数\n");printf("7,两位数减两位数\n");printf("请输入题型:");scanf("%d",&a);while(a<5||a>7){printf("输入错误,请重新输入");scanf("%d",&a);}if(a==5){car: b=rand()%10;c=rand()%10;d=b-c;printf("%d-%d=?\n",b,c);printf("请输入答案:");scanf("%d",&e);while(e!=d){printf("回答错误,请重做");printf("%d-%d=?\n",b,c);printf("请输入答案:");scanf("%d",&e);}printf("回答正确!是否做下一题?\n");printf("输入0表示是,输入1表示否\n");scanf("%d",&f);if(f==0)goto car;if(f==1)printf("欢迎再次使用!");}if(a==6){truck:b=rand()%100;if(b<10) b=b+10;c=rand()%10;d=b-c;printf("%d-%d=?\n",b,c);printf("请输入答案:");scanf("%d",&e);while(e!=d){printf("回答错误,请重做");printf("%d-%d=?\n",b,c);printf("请输入答案:");scanf("%d",&e);}printf("回答正确!是否做下一题?\n");printf("输入0表示是,输入1表示否\n");scanf("%d",&f);if(f==0)goto truck;if(f==1)printf("欢迎再次使用!");}if(a==7){boat:b=rand()%100;if(b<10) b=b+10;c=rand()%100;if(c<10) c=c+10;d=b-c;printf("%d-%d=?\n",b,c);printf("请输入答案:");scanf("%d",&e);while(e!=d){printf("回答错误,请重做");printf("%d-%d=?\n",b,c);printf("请输入答案:");scanf("%d",&e);}printf("回答正确!是否做下一题?\n");printf("输入0表示是,输入1表示否\n");scanf("%d",&f);if(f==0)goto boat;if(f==1)printf("欢迎再次使用!");}}if(a==3){printf("5,一位数乘一位数\n");printf("6,一位数乘两位数\n");printf("7,两位数乘两位数\n");printf("请输入题型:");scanf("%d",&a);while(a<5||a>7){printf("输入错误,请重新输入");scanf("%d",&a);}if(a==5){life:b=rand()%10;c=rand()%10;d=b*c;printf("%d*%d=?\n",b,c);printf("请输入答案:");scanf("%d",&e);while(e!=d){printf("回答错误,请重做");printf("%d*%d=?\n",b,c);printf("请输入答案:");scanf("%d",&e);}printf("回答正确!是否做下一题?\n");printf("输入0表示是,输入1表示否\n");scanf("%d",&f);if(f==0)goto life;if(f==1)printf("欢迎再次使用!");}if(a==6){shame:b=rand()%100;if(b<10) b=b+10;c=rand()%10;d=b*c;printf("%d*%d=?\n",b,c);printf("请输入答案:");scanf("%d",&e);while(e!=d){printf("回答错误,请重做");printf("%d*%d=?\n",b,c);printf("请输入答案:");scanf("%d",&e);}printf("回答正确!是否做下一题?\n");printf("输入0表示是,输入1表示否\n");scanf("%d",&f);if(f==0)goto shame;if(f==1)printf("欢迎再次使用!");}if(a==7){yeah:b=rand()%100;c=rand()%100;if(b<10) b=b+10;if(c<10) c=c+10;d=b*c;printf("%d*%d=?\n",b,c);printf("请输入答案:");scanf("%d",&e);while(e!=d){printf("回答错误,请重做");printf("%d*%d=?\n",b,c);printf("请输入答案:");scanf("%d",&e);}printf("回答正确!是否做下一题?\n");printf("输入0表示是,输入1表示否\n");scanf("%d",&f);if(f==0)goto yeah;if(f==1)printf("欢迎再次使用!");}}if(a==4){printf("5,一位数除一位数\n");printf("6,两位数除一位数\n");printf("7,两位数除两位数\n");printf("请输入题型:");scanf("%d",&a);while(a<5||a>7){printf("输入错误,请重新输入");scanf("%d",&a);}if(a==5){hit: b=rand()%10;c=rand()%10;{if (b>c){g=b%c;if (g!=0){b=b-g;d=b/c;printf("%d/%d=?\n",b,c);}else d=b/c;printf("%d/%d=?\n",b,c);}if (c>b){g=c%b;if (g!=0){c=c-g;t=c;c=b;b=t;d=b/c;printf("%d/%d=?\n",b,c);}elset=c;c=b;b=t;d=b/c;printf("%d/%d=?\n",b,c);}printf("请输入答案:");scanf("%d",&e);}while(e!=d){printf("回答错误,请重做");printf("%d/%d=?\n",b,c);printf("请输入答案:");scanf("%d",&e);}printf("回答正确!是否做下一题?\n");printf("输入0表示是,输入1表示否\n");scanf("%d",&f);if(f==0)goto hit;if(f==1)printf("欢迎再次使用!");}if(a==6){haha:b=rand()%100;c=rand()%10;if(b<10)b=b+10;g=b%c;if (g!=0)b=b-g;d=b/c;printf("%d/%d=?\n",b,c);printf("请输入答案:");scanf("%d",&e);while(e!=d){printf("回答错误,请重做");printf("%d/%d=?\n",b,c);printf("请输入答案:");scanf("%d",&e);}printf("回答正确!是否做下一题?\n");printf("输入0表示是,输入1表示否\n");scanf("%d",&f);if(f==0)goto haha;if(f==1)printf("欢迎再次使用!");}if(a==7){over:b=rand()%100;c=rand()%100;if(b<10) b=b+10;if(c<10) c=c+10;if (b>c){g=b%c;if (g!=0){b=b-g;d=b/c;printf("%d/%d=?\n",b,c);}else{d=b/c;printf("%d/%d=?\n",b,c);}}if (c>b){g=c%b;if (g!=0){c=c-g;t=c;c=b;b=t;d=b/c;printf("%d/%d=?\n",b,c);}else{t=c;c=b;b=t;d=b/c;printf("%d/%d=?\n",b,c);}}scanf("%d",&e);while(e!=d){printf("回答错误,请重做");printf("%d/%d=?\n",b,c);printf("请输入答案:");scanf("%d",&e);}printf("回答正确!是否做下一题?\n");printf("输入0表示是,输入1表示否\n");scanf("%d",&f);if(f==0)goto over;if(f==1)printf("欢迎再次使用!");}}}。
数据结构课程设计四则运算表达式求值(C语言版)
数据结构课程设计四则运算表达式求值(C语⾔版) 明⼈不说暗话,直接上,输⼊提取码z3fy即可下载。
⽂件中包含程序,程序运⾏⽂件,设计报告和测试样例,应有尽有,欢迎⼩伙伴们在中下载使⽤。
本课程设计为四则运算表达式求值,⽤于带⼩括号的⼀定范围内正负数的四则运算标准(中缀)表达式的求值。
注意事项:1、请保证输⼊的四则表达式的合法性。
输⼊的中缀表达式中只能含有英⽂符号“+”、“-”、“*”、“/”、“(”、“)”、“=”、数字“0”到“9”以及⼩数点“.”,输⼊“=”表⽰输⼊结束。
例如9+(3-1)*3.567+10/2=,特别是请勿输⼊多余空格和中⽂左右括号。
2、输⼊的中缀表达式默认限定长度是1001,可根据具体情况调整字符串数组的长度。
3、请保证输⼊的操作数在double数据类型范围内,单个数字有效数字长度不可超过15位。
本课程设计中操作数是C语⾔中的双精度浮点数类型。
4、本课程设计中的运算数可以是负数,另外如果是正数可直接省略“+”号(也可带“+”号)。
下⾯的程序正常运⾏需要在上⾯的百度⽹盘中下载相应⽂件,否则⽆法正常使⽤哦。
1/*本程序为四则运算表达式求值系统,⽤于计算带⼩括号的四则运算表达式求值。
2具体算法:3先将字符串处理成操作单元(操作数或操作符),再利⽤栈根据四则运算4的运算法则进⾏计算,最后得出结果。
*/56 #include<stdio.h>7 #include<ctype.h>8 #include<stdlib.h>9 #include<string.h>10 #include<stdlib.h>11 #include<ctype.h>1213const int Expmax_length = 1001;//表达式最⼤长度,可根据适当情况调整14struct Ope_unit15 {//定义操作单元16int flag;//=1表⽰是操作数 =0表⽰是操作符 -1表⽰符号单元17char oper;//操作符18double real;//操作数,为双精度浮点数19 };2021void Display();//菜单22void Instru(); //使⽤说明23int Check(char Exp_arry[]);24void Evalua(); //先调⽤Conver操作单元化,再调⽤Calculate函数计算结果并输出25int Conver(struct Ope_unit Opeunit_arry[],char Exp_arry[]);//将字符串处理成操作单元26int Isoper(char ch);//判断合法字符(+ - * / ( ) =)27int Ope_Compar(char ope1,char ope2);//操作符运算优先级⽐较28double Calculate(struct Ope_unit Opeunit_arry[],int Opeunit_count,int &flag);//⽤栈计算表达式结果29double Four_arithm(double x,double y,char oper);//四则运算3031int main()32 {33int select;34while(1)35 {36 Display();37 printf("请输⼊欲执⾏功能对应的数字:");38 scanf("%d",&select);39 printf("\n");40switch(select)41 {42case1: Evalua(); break;43case2: Instru(); break;44case0: return0;45default : printf("⽆该数字对应的功能,请重新输⼊\n");46 system("pause");47 }48 }49return0;50 }5152int Check(char Exp_arry[])53 {//检查是否有⾮法字符,返回1表⽰不合法,0表⽰合法54int Explength=strlen(Exp_arry),i;55for(i=0;i<Explength;i++)56 {57if(!Isoper(Exp_arry[i]) && Exp_arry[i] != '.' && !isdigit(Exp_arry[i]))58return1;59if(isdigit(Exp_arry[i]))60 {61int Dig_number=0,Cur_positoin=i+1;62while(isdigit(Exp_arry[Cur_positoin]) || Exp_arry[Cur_positoin]=='.')63 {64 Dig_number++;65 Cur_positoin++;66 }67if(Dig_number >= 16)//最多能够计算15位有效数字68return1;69 }70 }71return0;72 }7374void Evalua()75 {//先调⽤Conver函数将字符串操作单元化,再调⽤Calculate函数计算结果并输出76char Exp_arry[Expmax_length];77int flag=0;//假设刚开始不合法,1表达式合法,0不合法78struct Ope_unit Opeunit_arry[Expmax_length];7980 getchar();//吃掉⼀个换⾏符81 printf("请输⼊四则运算表达式,以=结尾:\n");82 gets(Exp_arry);83 flag=Check(Exp_arry);84if(flag)85 printf("该表达式不合法!\n");86else87 {88int Opeunit_count = Conver(Opeunit_arry,Exp_arry);89double ans = Calculate(Opeunit_arry,Opeunit_count,flag);90if(flag)91 {92 printf("计算结果为:\n");93 printf("%s%lf\n",Exp_arry,ans);94 }95else96 printf("该表达式不合法!\n");97 }98 system("pause");99 }100101int Conver(struct Ope_unit Opeunit_arry[],char Exp_arry[])102 {//将字符串操作单元化103int Explength=strlen(Exp_arry);104int i,Opeunit_count=0;105for(i=0;i<Explength;i++)106 {107if(Isoper(Exp_arry[i]))//是操作符108 {109 Opeunit_arry[Opeunit_count].flag=0;110 Opeunit_arry[Opeunit_count++].oper=Exp_arry[i];111 }112else//是操作数113 {114 Opeunit_arry[Opeunit_count].flag=1;115char temp[Expmax_length];116int k=0;117for(; isdigit(Exp_arry[i]) || Exp_arry[i]=='.' ;i++)118 {119 temp[k++]=Exp_arry[i];120 }121 i--;122 temp[k]='\0';123 Opeunit_arry[Opeunit_count].real=atof(temp);//将字符转化为浮点数124125//负数126if(Opeunit_count == 1 && Opeunit_arry[Opeunit_count-1].flag==0127 && Opeunit_arry[Opeunit_count-1].oper=='-')128 {129 Opeunit_arry[Opeunit_count-1].flag = -1;130 Opeunit_arry[Opeunit_count].real *= -1;131 }// -9132if(Opeunit_count >= 2 && Opeunit_arry[Opeunit_count-1].flag==0133 && Opeunit_arry[Opeunit_count-1].oper=='-' && Opeunit_arry[Opeunit_count-2].flag==0 134 && Opeunit_arry[Opeunit_count-2].oper !=')')135 {136 Opeunit_arry[Opeunit_count-1].flag = -1;137 Opeunit_arry[Opeunit_count].real *= -1;138 }// )-9139140//正数141if(Opeunit_count == 1 && Opeunit_arry[Opeunit_count-1].flag==0142 && Opeunit_arry[Opeunit_count-1].oper=='+')143 {144 Opeunit_arry[Opeunit_count-1].flag = -1;145 }// +9146if(Opeunit_count >= 2 && Opeunit_arry[Opeunit_count-1].flag==0147 && Opeunit_arry[Opeunit_count-1].oper=='+' && Opeunit_arry[Opeunit_count-2].flag==0148 && Opeunit_arry[Opeunit_count-2].oper !=')')149 {150 Opeunit_arry[Opeunit_count-1].flag = -1;151 }// )+9152 Opeunit_count++;153 }154 }155/*for(i=0;i<Opeunit_count;i++)156 {//查看各操作单元是否正确,1是操作数,0是操作符157 if(Opeunit_arry[i].flag == 1)158 printf("该单元是操作数为:%lf\n",Opeunit_arry[i].real);159 else if(Opeunit_arry[i].flag == 0)160 printf("该单元是操作符为:%c\n",Opeunit_arry[i].oper);161 else162 printf("该单元是负号符为:%c\n",Opeunit_arry[i].oper);163 }*/164return Opeunit_count;165 }166167double Calculate(struct Ope_unit Opeunit_arry[],int Opeunit_count,int &flag)168 {//根据运算规则,利⽤栈进⾏计算169int i,dS_pointer=0,oS_pointer=0;//dS_pointer为操作数栈顶指⽰器,oS_pointer为操作符栈顶指⽰器170double Dig_stack[Expmax_length];//操作数栈(顺序存储结构)171char Ope_stack[Expmax_length];//操作符栈172173for(i=0;i<Opeunit_count-1;i++)174 {175if( Opeunit_arry[i].flag != -1 )176 {177if(Opeunit_arry[i].flag)//是操作数178 {179 Dig_stack[dS_pointer++]=Opeunit_arry[i].real;//⼊操作数栈180//printf("%lf\n",Digit[dS_pointer-1]);181 }182else//是操作符 + - * / ( )183 {184//操作符栈为空或者左括号⼊栈185if(oS_pointer==0 || Opeunit_arry[i].oper=='(')186 {187 Ope_stack[oS_pointer++]=Opeunit_arry[i].oper;188//printf("%oS_pointer\Ope_u_count",Operator[oS_pointer-1]);189 }190else191 {192if(Opeunit_arry[i].oper==')')//是右括号将运算符⼀直出栈,直到遇见左括号193 {194 oS_pointer--;//指向栈顶195 dS_pointer--;//指向栈顶196while(Ope_stack[oS_pointer] != '(' && oS_pointer != 0)197 {198 Dig_stack[dS_pointer-1] = Four_arithm(Dig_stack[dS_pointer-1],Dig_stack[dS_pointer], 199 Ope_stack[oS_pointer--]);//oS_pointer--为操作符出栈200201 dS_pointer--;//前⼀个操作数出栈202//printf("操作数栈顶元素等于%lf\n",Digit[dS_pointer]);203 }204 oS_pointer--;//左括号出栈205206 oS_pointer++;//恢复指向栈顶之上207 dS_pointer++;208 }209else if(Ope_Compar(Opeunit_arry[i].oper,Ope_stack[oS_pointer-1]))//和栈顶元素⽐较210 {211 Ope_stack[oS_pointer++]=Opeunit_arry[i].oper;212//printf("%oS_pointer\Ope_u_count",Operator[oS_pointer-1]);213 }214else//运算符出栈,再将该操作符⼊栈215 {216 oS_pointer--;//指向栈顶217 dS_pointer--;//指向栈顶218while(Ope_Compar(Opeunit_arry[i].oper,Ope_stack[oS_pointer])==0 && oS_pointer != -1) 219 {//当前操作符⽐栈顶操作符优先级⾼220 Dig_stack[dS_pointer-1]=Four_arithm(Dig_stack[dS_pointer-1],Dig_stack[dS_pointer], 221 Ope_stack[oS_pointer--]);222 dS_pointer--;223//printf("操作数栈顶元素等于%lf\n",Digit[dS_pointer]);224 }225 oS_pointer++;//恢复指向栈顶之上226 dS_pointer++;227 Ope_stack[oS_pointer++]=Opeunit_arry[i].oper;228 }229 }230 }231 }232 }233/*for(i=0;i<oS_pointer;i++)234 printf("操作符栈%oS_pointer\Ope_u_count",Operator[i]);235 for(i=0;i<dS_pointer;i++)236 printf("操作数栈%lf\n",Digit[i]);*/237 oS_pointer--;//指向栈顶元素238 dS_pointer--;//指向栈顶元素239while(oS_pointer != -1)240 {241 Dig_stack[dS_pointer-1]=Four_arithm(Dig_stack[dS_pointer-1],Dig_stack[dS_pointer], 242 Ope_stack[oS_pointer--]);//oS_pointer--为操作符出栈243 dS_pointer--;//前⼀个操作数出栈244//printf("操作数栈顶元素为%lf\Ope_u_count",Digit[dS_pointer]);245 }246//printf("%dS_pointer,%dS_pointer\n",oS_pointer,dS_pointer);247if(oS_pointer==-1 && dS_pointer==0)248 flag=1;//为1表⽰表达式合法249return Dig_stack[0];250 }251252int Ope_Compar(char ope1,char ope2)253 {//操作符运算优先级⽐较254char list[]={"(+-*/"};255int map[5][5]={//先⾏后列,⾏⽐列的运算级优先级低为0,⾼为1256// ( + - * /257/* ( */1,0,0,0,0,258/* + */1,0,0,0,0,259/* - */1,0,0,0,0,260/* * */1,1,1,0,0,261/* / */1,1,1,0,0 };262int i,j;263for(i=0;i<5;i++)264if(ope1==list[i]) break;265for(j=0;j<5;j++)266if(ope2==list[j]) break;267return map[i][j];268 }269270double Four_arithm(double x,double y,char oper)271 {//四则运算272switch(oper)//保证不含其它运算符273 {274case'+': return x+y;275case'-': return x-y;276case'*': return x*y;277case'/': return x/y;//y不能为0278default : return0;279 }280 }281282int Isoper(char ch)283 {//判断合法字符 + - * / ( ) =284if(ch=='+' || ch=='-' || ch=='*' || ch=='/' || ch=='(' || ch==')' || ch=='=')285return1;286return0;287 }288289void Display()290 {//打印菜单291 system("cls");292 printf("/******************************************************************************/\n");293 printf("\t\t 欢迎使⽤本四则运算表达式求值系统\n");294 printf("\n\t说明:建议请您先阅读使⽤说明,再输⼊相应的数字进⾏操作,谢谢配合!\n"); 295 printf("\n\t\t1 四则运算表达式求值\n");296 printf("\n\t\t2 使⽤说明\n");297 printf("\n\t\t0 退出\n");298 printf("/******************************************************************************/\n");299 }300301void Instru()302 {//打印使⽤说明303 FILE *fp;304char ch;305if( ( fp=fopen("使⽤说明.txt","r") ) == NULL)306 {307 printf("⽂件打开失败!\n");308 exit(0);309 }310for(; (ch = fgetc(fp)) != EOF; )311 putchar(ch);312 fclose(fp);313 printf("\n");314 system("pause");315 }。
c语言编写四则运算
C语言编写四则运算#include<stdio.h>//计算器#include<malloc.h>#define STACK_SIZE100 //max size of the stack#define STACK_INCREMENT10 //realloc sizetypedef struct FStack//stack of int{float* base;float* top;int stacksize;}FStack;void Init(FStack* s){s->base = (float*)malloc(STACK_SIZE * sizeof(FStack));if (!s->base){printf("overflow!\n");return;}s->top = s->base;s->stacksize = STACK_SIZE;}bool isEmpty(FStack* s){if (s->top == s->base){return true;}else{return false;}}void Push(FStack* s, float e){if (s->top - s->base >= s->stacksize){printf("stack is full!\nrealloc %d\n", STACK_INCREMENT);s->base = (float*)realloc(s->base, (s->stacksize +sizeof(FStack)));if (!s->base){printf("overflow!\n");return;}s->top = s->base + s->stacksize;s->stacksize += STACK_INCREMENT;}*(s->top) = e;(s->top)++;}float GetTop(FStack* s){if (s->top == s->base){printf("stack is empty!\n");return 0;}float e = *(s->top - 1);return e;}void Pop(FStack* s){if (s->top == s->base){printf("stack is empty!\n");return;}s->top--;}typedef struct CStack//stack of char{char* base;char* top;int stacksize;}CStack;void Init(CStack* s){s->base = (char*)malloc(STACK_SIZE * sizeof(CStack));if (!s->base){printf("overflow!\n");return;}s->top = s->base;s->stacksize = STACK_SIZE;}bool isEmpty(CStack* s){if (s->top == s->base){return true;}else{return false;}}void Push(CStack* s, int e){if (s->top - s->base >= s->stacksize){printf("stack is full!\nrealloc %d\n", STACK_INCREMENT);s->base = (char*)realloc(s->base, (s->stacksize +sizeof(CStack)));if (!s->base){printf("overflow!\n");return;}s->top = s->base + s->stacksize;s->stacksize += STACK_INCREMENT;}*(s->top) = e;(s->top)++;}char GetTop(CStack* s){if (s->top == s->base){printf("stack is empty!\n");return 0;}char e = *(s->top - 1);return e;}void Pop(CStack* s){if (s->top == s->base){printf("stack is empty!\n");return;}s->top--;}bool isOper(char ch){if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '%') {return true;}else{return false;}}int Priority(char ch){int p;switch(ch){case'(':p = 0;break;case'+':case'-':p = 1;break;case'*':case'/':case'%':p = 2;break;}return p;}float Calculate(float f1, float f2, char oper) {float f3;switch(oper){case'+':f3 = f1 + f2;break;case'-':f3 = f1 - f2;break;case'*':f3 = f1 * f2;break;case'%':f3 = (float)((int)f1 % (int)f2);break;case'/':if (f2 == 0){printf("\nDevided by zero!");exit(1);}else{f3 = f1 / f2;}break;}return f3;}float StrtoFloat(char* str, int* pos){float fRes;int i = *pos;int k;char n[50];for(k= 0; str[i] >= '0'&& str[i] <= '9'|| str[i] == '.'; i++, k++) {n[k] = str[i];}n[k] = '\0';*pos = i;fRes = atof(n);return fRes;}bool Check(char* str){int i = 0;while (str[i] != '\0'){if (str[i] != '+' && str[i] != '-' && str[i] != '*' && str[i] != '/' && str[i] != '%' && str[i] != '.' && str[i] != '(' && str[i] != ')' && (str[i] < '0' || str[i] > '9')){return false;}i++;}return true;}void main(){char exp[100];int i;float f, f1, f2;char oper;FStack fstack;CStack cstack;Init(&fstack);Init(&cstack);printf("The expression is:");gets(exp);if (!Check(exp)){printf("input error! exit now!\n");exit(1);}for (i = 0; exp[i] != '\0' && exp[i] != -52; i++){if (!isOper(exp[i])){f = StrtoFloat(exp, &i);Push(&fstack, f);}if (isOper(exp[i])){if (!isEmpty(&cstack)){while (!isEmpty(&cstack) && Priority(exp[i]) <= Priority(GetTop(&cstack))){oper = GetTop(&cstack);Pop(&cstack);f2 = GetTop(&fstack);Pop(&fstack);f1 = GetTop(&fstack);Pop(&fstack);f = Calculate(f1, f2, oper);Push(&fstack, f);}Push(&cstack, exp[i]);}else{Push(&cstack, exp[i]);}}else if (exp[i] == '('){Push(&cstack, exp[i]);}else if (exp[i] == ')'){while (GetTop(&cstack) != '(' && !isEmpty(&cstack)){oper = GetTop(&cstack);Pop(&cstack);f2 = GetTop(&fstack);Pop(&fstack);f1 = GetTop(&fstack);Pop(&fstack);f = Calculate(f1, f2, oper);Push(&fstack, f);}Pop(&cstack);}}while (!isEmpty(&cstack)){oper = GetTop(&cstack);Pop(&cstack);f2 = GetTop(&fstack);Pop(&fstack);f1 = GetTop(&fstack);Pop(&fstack);f = Calculate(f1, f2, oper);Push(&fstack, f);}printf("\nThe result is:%f\n", GetTop(&fstack));Pop(&fstack);}。
c语言随机生成四则运算
c语言随机生成四则运算在 C 语言中,可以使用随机数生成器来随机生成四则运算式(加、减、乘、除)。
以下是一个简单的示例代码,演示了如何随机生成四则运算式:```c#include <stdio.h>#include <stdlib.h>#include <time.h>void generateArithmeticExpression() {// 定义操作符数组char operators[] = { '+', '-', '*', '/' };// 定义操作数数组int operands[2];int operatorIndex;double result;// 生成随机操作符operatorIndex = rand() % 4;// 生成随机操作数operands[0] = rand() % 10 + 1;operands[1] = rand() % 10 + 1;// 根据操作符进行运算switch (operatorIndex) {case 0:result = operands[0] + operands[1];break;case 1:result = operands[0] - operands[1];break;case 2:result = operands[0] * operands[1];break;case 3:if (operands[1] != 0) {result = operands[0] / operands[1];}else {printf("Error: Dividing by zero is not allowed.\n");return;}break;}// 输出运算结果printf("%d %c %d = %.2f\n", operands[0], operators[operatorIndex], operands[1], result);}int main() {srand(time(NULL));generateArithmeticExpression();return 0;}```在上述示例代码中,使用了`rand()`函数来生成随机数。
c语言四则运算符
C语言中的四则运算符包括加法运算符(+)、减法运算符(-)、乘法运算符(*)和除法运算符(/)。
以下是它们的基本用法:
1. 加法运算符(+):用于将两个数值相加。
示例:`int sum = 3 + 5; // 结果为8`
2. 减法运算符(-):用于将一个数值减去另一个数值。
示例:`int difference = 10 - 4; // 结果为6`
3. 乘法运算符(*):用于将两个数值相乘。
示例:`int product = 6 * 7; // 结果为42`
4. 除法运算符(/):用于将一个数值除以另一个数值,得到商(整数相除时结果为整数,浮点数相除时结果为浮点数)。
示例:`float quotient = 10 / 3; // 结果为3.3333...`
此外,还有取模运算符(%),用于求两个整数相除的余数。
示例:`int remainder = 10 % 3; // 结果为1`
需要注意的是,当进行多个运算符混合使用时,C语言会根据运算符优先级和结合性来确定运算顺序。
可以使用括号来改变运算次序。
希望以上解答能对你有所帮助!如果还有其他问题,请随时提问。
1。
四则运算(c语言)
四则运算(c语⾔)博客班级作业要求作业⽬标能够实现四则运算⼩程序的基本需求,同时练习写博客(排版等)学号3190704217⼆. 题⽬要求写⼀个能⾃动⽣成⼩学四则运算题⽬的程序,然后在此基础上扩展:1)除了整数以外,还要⽀持真分数的四则运算,例如:1/6+1/8=7/242)程序要求能处理⽤户的输⼊,判断对错,累积分数3)程序⽀持可以由⽤户⾃⾏选择加、减、乘、除运算4)使⽤-n参数控制⽣成题⽬的个数,例如Myapp.exe -n 10,将⽣成10个题⽬三. 代码#include <stdio.h>#include <stdlib.h>#include <time.h>#include <math.h>int s;//答对的题⽬数量float score;//得分(百分制)//判断答案是否正确void judge0(float a,float ta) //专为除法提供判断函数{float error=a-ta;//计算结果与真正答案的误差⼩于⼀定值if((error<=0.005)&&(error>=-0.004)){printf("回答正确!\n");s++;}elseprintf("回答错误,正确答案是 %.2f\n",ta);}void judge(int a,int ta) //加法减法,乘法的判断函数{if(a==ta){printf("回答正确!\n");s++;}elseprintf("回答错误,正确答案是 %d\n",ta);}void judge1(int a1,int a2,float ta) //真分数运算的判断函数{float a=(float)a1/(float)a2;if(a==ta){printf("回答正确!\n");s++;}elseprintf("回答错误,正确答案是 %.2f\n",ta);}//真分数的随机运算int random0(){int num,i=0;printf("请输⼊题⽬数量\n");scanf("%d",&num);srand(unsigned(time(NULL)));while(i<num){int type;//运算类型,加减乘除int left1,left2,right1,right2;int answer1,answer2;float trueanswer;left1= rand() % 100;left2= rand() % 100;type = rand() % 4;if(left1>left2) //转化为真分数{int t;t=left1;left2=left1;left1=t;}right1 = rand() % 100;right2 = rand() % 100;if(right1>right2)//转化为真分数{int t;t=right1;right2=right1;right1=t;}switch(type){case 0:trueanswer=((float)left1/(float)left2)+((float)right1/(float)right2); printf("%d/%d + %d/%d = ", left1, left2,right1,right2);scanf("%d/%d",&answer1,&answer2);judge1(answer1,answer2,trueanswer);break;case 1:trueanswer=((float)left1/(float)left2)-((float)right1/(float)right2); printf("%d/%d - %d/%d = ", left1, left2,right1,right2);scanf("%d/%d",&answer1,&answer2);judge1(answer1,answer2,trueanswer);break;case 2:trueanswer=((float)left1/(float)left2)*((float)right1/(float)right2); printf("%d/%d * %d/%d = ", left1, left2,right1,right2);scanf("%d/%d",&answer1,&answer2);judge1(answer1,answer2,trueanswer);break;case 3:trueanswer=((float)left1/(float)left2)/((float)right1/(float)right2); printf("%d/%d / %d/%d = ", left1, left2,right1,right2);scanf("%d/%d",&answer1,&answer2);judge1(answer1,answer2,trueanswer);break;}i++;}return num;}// 整数的随机运算int random(){int num,i=0;printf("请输⼊题⽬数量\n");scanf("%d",&num);srand(unsigned(time(NULL)));while(i<num){int type;//运算类型int left, right;int answer,trueanswer;left = rand() % 100;type = rand() % 4;right = rand() % 100;switch(type){case 0:trueanswer=left+right;printf("%d + %d = ", left, right); scanf("%d",&answer);judge(answer,trueanswer); break;case 1:trueanswer=left-right;printf("%d - %d = ", left, right); scanf("%d",&answer);judge(answer,trueanswer); break;case 2:trueanswer=left*right;printf("%d * %d = ", left, right); scanf("%d",&answer);judge(answer,trueanswer); break;case 3:float answer0,trueanswer0; trueanswer0=((float)left)/((float)right); printf("%d /%d = ", left, right);scanf("%f",&answer0);judge0(answer0,trueanswer0); break;}i++;}return num;}//加法int add(){int num,i=0;printf("请输⼊题⽬数量\n");scanf("%d",&num);srand(unsigned(time(NULL))); while(i<num){int left, right,answer,trueanswer;left = rand() % 100;right = rand() % 100;trueanswer=left+right;printf("%d +%d = ", left, right); scanf("%d",&answer);judge(answer,trueanswer);i++;}return num;}//减法int subtract(){int num,i=0;printf("请输⼊题⽬数量\n");scanf("%d",&num);srand(unsigned(time(NULL))); while(i<num){int left, right,answer,trueanswer; left = rand() % 100;right = rand() % 100; trueanswer=left-right;printf("%d -%d = ", left, right); scanf("%d",&answer);judge(answer,trueanswer);i++;}return num;}//乘法int multiply(){int num,i=0;printf("请输⼊题⽬数量\n");scanf("%d",&num);srand(unsigned(time(NULL))); while(i<num){int left, right,answer,trueanswer; left = rand() % 100;right = rand() % 100; trueanswer=left*right;printf("%d *%d = ", left, right); scanf("%d",&answer);judge(answer,trueanswer);i++;}return num;}//除法int divide(){int num,i=0;printf("请输⼊题⽬数量\n");scanf("%d",&num);srand(unsigned(time(NULL))); while(i<num){int left, right;float answer,trueanswer;left = rand() % 100;right = rand() % 100; trueanswer=((float)left)/((float)right); printf("%d /%d = ", left, right); scanf("%f",&answer);judge0(answer,trueanswer);i++;}return num;}//打印界⾯提⽰信息void main(){printf("welcome!\n");printf("\n");int choose,t;do{s=0;score=0;int n;printf("********************四则运算********************\n");printf("\n");printf(" 1.随即题⽬\n");printf(" 2.加法题⽬\n");printf(" 3.减法题⽬\n");printf(" 4.乘法题⽬\n");printf(" 5.除法题⽬\n");printf(" 6.分数运算\n");printf(" 7.退出\n");printf("\n");printf("******************Designed by 吴⼭茶******************\n");printf("\n");printf("请选择计算类型:");scanf("%d",&t);choose=t;printf("%d",choose);printf("\n");system("cls");switch(choose){case 1:n=random();printf("\n");break;case 2:n=add();printf("\n");break;case 3:n=subtract();printf("\n");break;case 4:n=multiply();printf("\n");break;case 5:n=divide();printf("\n");break;case 6:n=random0();printf("\n");break;default:break;}score=(float)s/(float)n;printf("得分:%.2f\n",score);}while(choose!=7);exit(0);}三.运⾏结果1.显⽰界⾯2.加法运算3.随机运算4.真分数运算任务内容计划完成需要的时间(min)实际完成需要的时间(min)计划1020任务内容计划完成需要的时间(min)实际完成需要的时间(min)估计这个任务需要多少时间,并规划⼤致⼯作步骤55开发00需求分析(包括学习新技术)1010⽣成设计⽂档00设计复审1530代码规范55具体设计2020具体编码150180代码复审1010报告00测试报告00计算⼯作量300330+事后总结,并提出过程改进计划2010。
用C语言实现超长整数的加减乘除四则运算
q= q > nex t;
}
© 1995-2005 Tsinghua Tongfang Optical Disc Co., Ltd. All rights reserved.
·3 8· (总 284) 用 C 语言实现超长整数的加减乘除四则运算 2003 年
} if (i 1) 加数、被加数均已结束, 最高位有进位 { h= m a lloc (sizeof (struct node) ) ; h > data= 1; h > nex t= hc > nex t; hc > nex t= h; q= q > nex t; } link to str (hc, s) ; 将加和链表转换为字符串 delink (ha) ; delink (hb) ; delink (hc) ; retu rn s;
四则运算(C语言)
四则运算(C语⾔)源代码:include<stdio.h>include<stdlib.h>include<math.h>include<time.h>int right=0;int wrong=0;int sum,s=0;//真分数void zhen(){int e,f,k,c,d,n,p;//输⼊要⽣成的题⽬数量printf("\t请输⼊要⽣成的题⽬数量:");scanf("%d",&n);while(s<n){e=rand()%100+1;f=rand()%100+1;k=rand()%4+1;if(e>=f){p=e;e=f;f=p;}c=rand()%100+1;d=rand()%100+1;if(c>=d){p=c;c=d;d=p;}switch(k){case 1:printf("%d/%d+%d/%d",e,f,c,d);break;case 2:printf("%d/%d-%d/%d",e,f,c,d);break;case 3:printf("%d/%d*%d/%d",e,f,c,d);break;case 4:printf("%d/%d/%d/%d",e,f,c,d);break;}s++;}printf("\t答对了%d题,打错了%d题\n",right,wrong);}//加法运算void add(){int a,b,c,n;//输⼊要⽣成的题⽬数量printf("\t请输⼊要⽣成的题⽬数量:");scanf("%d",&n);while(s<n){a=rand()%101;b=rand()%101;c = a + b ;printf("\t%d+%d=",a,b);scanf("%d",&sum);if(sum==c){printf("\t答对啦!\n");right++;}else{printf("\t答错了!\n");wrong++;}s++;}printf("\t答对了%d题,打错了%d题\n",right,wrong); }//减法运算void sub(){int a,b,c,n,q;//输⼊要⽣成的题⽬数量printf("\t请输⼊要⽣成的题⽬数量:");scanf("%d",&n);while(s<n){a=rand()%101;b=rand()%101;if(a<b){q=a;a=b;b=q;}c = a - b ;printf("\t%d-%d=",a,b);scanf("%d",&sum);if(sum==c){printf("\t答对啦!\n");right++;}else{printf("\t答错了!\n");wrong++;}s++;}printf("\t答对了%d题,打错了%d题\n",right,wrong); }//乘法运算void mul(){int a,b,c,n;//输⼊要⽣成的题⽬数量printf("\t请输⼊要⽣成的题⽬数量:");scanf("%d",&n);while(s<n){a=rand()%11;b=rand()%11;c = a * b;printf("\t%d*%d=",a,b);scanf("%d",&sum);if(sum==c){printf("\t答对啦!\n");right++;}else{printf("\t打错了!\n");wrong++;}s++;}printf("\t答对了%d题,打错了%d题\n",right,wrong);}//除法运算void chu(){int a,b,n,q;float c;//输⼊要⽣成的题⽬数量printf("\t请输⼊要⽣成的题⽬数量:");scanf("%d",&n);while(s<n){a=rand()%101;b=rand()%101;if(a<b){q=a;a=b;b=q;}c = a / b;printf("\t%d/%d=",a,b);scanf("%d",&sum);if(sum==c){printf("\t答对啦!\n");right++;}else{printf("\t打错了!\n");wrong++;}s++;}printf("\t答对了%d题,打错了%d题\n",right,wrong);}void main(){int n,t;//n 题数 t 运算类型编号//输⼊产⽣的运算类型printf("\t**四则运算⽣成器**\n");printf("\t请输⼊你要做的运算类型编号:\n");printf("\t0 加法\n\t1 减法\n\t2 乘法\n\t3 除法\n\t4 真分数运算\n\t5 退出四则运算程序\n"); scanf("%d",&t);while(t!=4){switch(t){case 0:add();break;case 1:sub();break;case 2:mul();break;case 3:chu();break;case 4:zhen();break;}}}在写程序的时候复习了遗忘的知识,也收获了许多,代码还不是很完善,真分数部分还⽆法运⾏,接下来要更加全⾯的去学习代码的编写。
c语言运算符号
c语言运算符号C语言是一门受欢迎的编程语言,它在计算机世界有着重要地位。
使用C语言,可以完成从数据库程序到科学和技术计算的大量任务,而许多这些任务都需要使用运算符号来完成。
运算符号是一种对变量的数学计算、比较或逻辑运算,使得计算机能够快速处理程序指令。
本文将介绍C语言中的一些常见运算符号,以及它们在程序中的用途。
算术运算符号是C语言中最常用的运算符号,用于表示四则运算。
它们分别是加号(+)、减号(-)、乘号(*)和除号(/)。
这些运算符号可以用来完成简单的四则运算,包括加法、减法、乘法和除法等。
与加号(+)和减号(-)相似的运算符号还有“累加”和“累减”,它们和加号(+)和减号(-)的不同之处在于,它们会将计算结果保存在变量中,例如:a=a+b中,其中a可以保存上次计算结果,再将b加上去。
比较运算符号可以用来比较两个变量或操作数的大小。
它们分别是“等于”(==)、“不等于”(!=)、“大于”(>)、“小于”(、“大于等于”(>=)和“小于等于”(<=)。
比较运算符号可以使用在条件判断、循环等语句中,用于根据给定的条件来决定是否执行下一步操作。
逻辑运算符号包括“与”(&&)“或”(||)“非”(!)和“异或”(^)四种,它们用于表示两个变量或操作数之间的关系。
常用的两个逻辑运算符号是“与”(&&)和“或”(||),它们可以用来判断多个变量或操作数之间的关系,例如:“a&&b”的意思是:如果a和b都为真,则返回真;而“a||b”的意思是:如果a或b中任意一个为真,则返回真。
位运算符号可以将变量或操作数当作二进制形式进行操作,它们包括“与”(&)、“或”(|)、“左移”(、“右移”(>>)、“异或”(^)等。
位运算符号可以用来做一些复杂的操作,比如将一个整数拆分成两个整数,或者计算一个整数的二进制位是否为1等。
运算符号还有一些特殊的运算,比如“三目运算符”(?:)、“逗号”(,)和“大括号”({})。
c语言编写四则运算
C语言编写四则运算#include<stdio.h>//计算器#include<malloc.h>#define STACK_SIZE100 //ma* size of the stack#define STACK_INCREMENT10 //realloc sizetypedefstruct FStack//stack of int{float* base;float* top;int stacksize;}FStack;void Init(FStack* s){s->base = (float*)malloc(STACK_SIZE * sizeof(FStack));if (!s->base){printf("overflow!\n");return;}s->top = s->base;s->stacksize = STACK_SIZE;}bool isEmpty(FStack* s){if (s->top == s->base){returntrue;}else{returnfalse;}}void Push(FStack* s, float e){if (s->top - s->base >= s->stacksize){printf("stack is full!\nrealloc %d\n", STACK_INCREMENT);s->base = (float*)realloc(s->base, (s->stacksize + sizeof(FStack))); if (!s->base){printf("overflow!\n");return;}s->top = s->base + s->stacksize;s->stacksize += STACK_INCREMENT;}*(s->top) = e;(s->top)++;}float GetTop(FStack* s){if (s->top == s->base){printf("stack is empty!\n");return 0;}float e = *(s->top - 1);return e;}void Pop(FStack* s){if (s->top == s->base){printf("stack is empty!\n");return;}s->top--;}typedefstruct CStack//stack of char{char* base;char* top;int stacksize;}CStack;void Init(CStack* s){s->base = (char*)malloc(STACK_SIZE * sizeof(CStack)); if (!s->base){printf("overflow!\n");return;}s->top = s->base;s->stacksize = STACK_SIZE;}bool isEmpty(CStack* s){if (s->top == s->base){returntrue;}else{returnfalse;}}void Push(CStack* s, int e){if (s->top - s->base >= s->stacksize){printf("stack is full!\nrealloc %d\n", STACK_INCREMENT);s->base = (char*)realloc(s->base, (s->stacksize + sizeof(CStack))); if (!s->base){printf("overflow!\n");return;}s->top = s->base + s->stacksize;s->stacksize += STACK_INCREMENT;}*(s->top) = e;(s->top)++;}char GetTop(CStack* s){if (s->top == s->base){printf("stack is empty!\n");return 0;}char e = *(s->top - 1);return e;}void Pop(CStack* s){if (s->top == s->base){printf("stack is empty!\n");return;}s->top--;}bool isOper(char ch){if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '%') {returntrue;}else{returnfalse;}}int Priority(char ch){int p;switch(ch){case'(':p = 0;break;case'+':case'-':p = 1;break;case'*':case'/':case'%':p = 2;break;}return p;}float Calculate(float f1, float f2, char oper){float f3;switch(oper){case'+':f3 = f1 + f2;break;case'-':f3 = f1 - f2;break;case'*':f3 = f1 * f2;break;case'%':f3 = (float)((int)f1 % (int)f2);break;case'/':if (f2 == 0){printf("\nDevided by zero!");e*it(1);}else{f3 = f1 / f2;}break;}return f3;}float StrtoFloat(char* str, int* pos){float fRes;int i = *pos;int k;char n[50];for (k = 0; str[i] >= '0' && str[i] <= '9' || str[i] == '.'; i++, k++) {n[k] = str[i];}n[k] = '\0';*pos = i;fRes = atof(n);return fRes;}bool Check(char* str){int i = 0;while (str[i] != '\0'){if (str[i] != '+' && str[i] != '-' && str[i] != '*' && str[i] != '/' && str[i] != '%'&& str[i] != '.'&& str[i] != '('&& str[i] != ')'&& (str[i] < '0' || str[i] > '9')){returnfalse;}i++;}returntrue;}void main(){char e*p[100];int i;float f, f1, f2;char oper;FStackfstack;CStackcstack;Init(&fstack);Init(&cstack);printf("The e*pression is:");gets(e*p);if (!Check(e*p)){printf("input error! e*it now!\n");e*it(1);}for (i = 0; e*p[i] != '\0' && e*p[i] != -52; i++) {if (!isOper(e*p[i])){f = StrtoFloat(e*p, &i);Push(&fstack, f);}if (isOper(e*p[i])){if (!isEmpty(&cstack)){while (!isEmpty(&cstack) && Priority(e*p[i]) <= Priority(GetTop(&cstack))){oper = GetTop(&cstack);Pop(&cstack);f2 = GetTop(&fstack);Pop(&fstack);f1 = GetTop(&fstack);Pop(&fstack);f = Calculate(f1, f2, oper);Push(&fstack, f);}Push(&cstack, e*p[i]);}else{Push(&cstack, e*p[i]);}}elseif (e*p[i] == '('){Push(&cstack, e*p[i]);}elseif (e*p[i] == ')'){while (GetTop(&cstack) != '(' && !isEmpty(&cstack)) {oper = GetTop(&cstack);Pop(&cstack);f2 = GetTop(&fstack);Pop(&fstack);f1 = GetTop(&fstack);Pop(&fstack);f = Calculate(f1, f2, oper);Push(&fstack, f);}Pop(&cstack);}}while (!isEmpty(&cstack)){oper = GetTop(&cstack);Pop(&cstack);f2 = GetTop(&fstack);Pop(&fstack);f1 = GetTop(&fstack);Pop(&fstack);f = Calculate(f1, f2, oper);Push(&fstack, f);}printf("\nThe result is:%f\n", GetTop(&fstack)); Pop(&fstack);}。
c语言编写四则运算
C语言编写四则运算#include<stdio.h>//计算器#include<malloc.h>#define STACK_SIZE100 //max size of the stack#define STACK_INCREMENT10 //realloc sizetypedef struct FStack//stack of int{float* base;float* top;int stacksize;}FStack;void Init(FStack* s){s->base = (float*)malloc(STACK_SIZE * sizeof(FStack));if (!s->base){printf("overflow!\n");return;}s->top = s->base;s->stacksize = STACK_SIZE;}bool isEmpty(FStack* s){if (s->top == s->base){return true;}else{return false;}}void Push(FStack* s, float e){if (s->top - s->base >= s->stacksize){printf("stack is full!\nrealloc %d\n", STACK_INCREMENT);s->base = (float*)realloc(s->base, (s->stacksize +sizeof(FStack)));if (!s->base){printf("overflow!\n");return;}s->top = s->base + s->stacksize;s->stacksize += STACK_INCREMENT;}*(s->top) = e;(s->top)++;}float GetTop(FStack* s){if (s->top == s->base){printf("stack is empty!\n");return 0;}float e = *(s->top - 1);return e;}void Pop(FStack* s){if (s->top == s->base){printf("stack is empty!\n");return;}s->top--;}typedef struct CStack//stack of char{char* base;char* top;int stacksize;}CStack;void Init(CStack* s){s->base = (char*)malloc(STACK_SIZE * sizeof(CStack));if (!s->base){printf("overflow!\n");return;}s->top = s->base;s->stacksize = STACK_SIZE;}bool isEmpty(CStack* s){if (s->top == s->base){return true;}else{return false;}}void Push(CStack* s, int e){if (s->top - s->base >= s->stacksize){printf("stack is full!\nrealloc %d\n", STACK_INCREMENT);s->base = (char*)realloc(s->base, (s->stacksize +sizeof(CStack)));if (!s->base){printf("overflow!\n");return;}s->top = s->base + s->stacksize;s->stacksize += STACK_INCREMENT;}*(s->top) = e;(s->top)++;}char GetTop(CStack* s){if (s->top == s->base){printf("stack is empty!\n");return 0;}char e = *(s->top - 1);return e;}void Pop(CStack* s){if (s->top == s->base){printf("stack is empty!\n");return;}s->top--;}bool isOper(char ch){if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '%') {return true;}else{return false;}}int Priority(char ch){int p;switch(ch){case'(':p = 0;break;case'+':case'-':p = 1;break;case'*':case'/':case'%':p = 2;break;}return p;}float Calculate(float f1, float f2, char oper) {float f3;switch(oper){case'+':f3 = f1 + f2;break;case'-':f3 = f1 - f2;break;case'*':f3 = f1 * f2;break;case'%':f3 = (float)((int)f1 % (int)f2);break;case'/':if (f2 == 0){printf("\nDevided by zero!");exit(1);}else{f3 = f1 / f2;}break;}return f3;}float StrtoFloat(char* str, int* pos){float fRes;int i = *pos;int k;char n[50];for(k= 0; str[i] >= '0'&& str[i] <= '9'|| str[i] == '.'; i++, k++) {n[k] = str[i];}n[k] = '\0';*pos = i;fRes = atof(n);return fRes;}bool Check(char* str){int i = 0;while (str[i] != '\0'){if (str[i] != '+' && str[i] != '-' && str[i] != '*' && str[i] != '/' && str[i] != '%' && str[i] != '.' && str[i] != '(' && str[i] != ')' && (str[i] < '0' || str[i] > '9')){return false;}i++;}return true;}void main(){char exp[100];int i;float f, f1, f2;char oper;FStack fstack;CStack cstack;Init(&fstack);Init(&cstack);printf("The expression is:");gets(exp);if (!Check(exp)){printf("input error! exit now!\n");exit(1);}for (i = 0; exp[i] != '\0' && exp[i] != -52; i++){if (!isOper(exp[i])){f = StrtoFloat(exp, &i);Push(&fstack, f);}if (isOper(exp[i])){if (!isEmpty(&cstack)){while (!isEmpty(&cstack) && Priority(exp[i]) <= Priority(GetTop(&cstack))){oper = GetTop(&cstack);Pop(&cstack);f2 = GetTop(&fstack);Pop(&fstack);f1 = GetTop(&fstack);Pop(&fstack);f = Calculate(f1, f2, oper);Push(&fstack, f);}Push(&cstack, exp[i]);}else{Push(&cstack, exp[i]);}}else if (exp[i] == '('){Push(&cstack, exp[i]);}else if (exp[i] == ')'){while (GetTop(&cstack) != '(' && !isEmpty(&cstack)){oper = GetTop(&cstack);Pop(&cstack);f2 = GetTop(&fstack);Pop(&fstack);f1 = GetTop(&fstack);Pop(&fstack);f = Calculate(f1, f2, oper);Push(&fstack, f);}Pop(&cstack);}}while (!isEmpty(&cstack)){oper = GetTop(&cstack);Pop(&cstack);f2 = GetTop(&fstack);Pop(&fstack);f1 = GetTop(&fstack);Pop(&fstack);f = Calculate(f1, f2, oper);Push(&fstack, f);}printf("\nThe result is:%f\n", GetTop(&fstack));Pop(&fstack);}。
C语言四则运算代码
int leftData[100],rightData[100],answerData[100];
char signData[100];
srand (time(NULL));
while(true){
int left=getRand(min,max);
批注本地保存成功开通会员云端永久保存去开通
// sinian.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include"iostream.h"
#include"stdlib.h"
#include"time.h"
wrongResult++;
}
n++;
}//while
printSta(rightResult,wrongResult,n);
printAllProblem(leftData,rightData,signData,answerData,n);
}
int getRand(int min,int max){
int right=getRand(min,max);
char sign=getRandsign();
if(sign=='-')getRightErase(&left,&right);
int answer=getAnswer(left,right,sign);
leftData[n]=left;rightData[n]=right;signData[n]=sign;answerData[n]=answer;
C语言编写四则运算
C语言编写四则运算#include<> ; i++, k++){n[k] = str[i];}n[k] = '\0';*pos = i;fRes = atof(n);return fRes;}bool Check(char* str){int i = 0;while (str[i] != '\0'){if (str[i] != '+' && str[i] != '-' && str[i] != '*' && str[i] != '/' && str[i] != '%' && str[i] != '.' && str[i] != '(' && str[i] != ')' && (str[i] < '0' || str[i] > '9')){return false;}i++;}return true;}void main(){char exp[100];int i;float f, f1, f2;char oper;FStack fstack;CStack cstack;Init(&fstack);Init(&cstack);printf("The expression is:");gets(exp);if (!Check(exp)){printf("input error! exit now!\n");exit(1);}for (i = 0; exp[i] != '\0' && exp[i] != -52; i++) {if (!isOper(exp[i])){f = StrtoFloat(exp, &i);Push(&fstack, f);}if (isOper(exp[i])){if (!isEmpty(&cstack)){while (!isEmpty(&cstack) && Priority(exp[i]) <= Priority(GetTop(&cstack))){oper = GetTop(&cstack);Pop(&cstack);f2 = GetTop(&fstack);Pop(&fstack);f1 = GetTop(&fstack);Pop(&fstack);f = Calculate(f1, f2, oper);Push(&fstack, f);}Push(&cstack, exp[i]);}else{Push(&cstack, exp[i]);}}else if (exp[i] == '('){Push(&cstack, exp[i]);}else if (exp[i] == ')'){while (GetTop(&cstack) != '(' && !isEmpty(&cstack)){oper = GetTop(&cstack);Pop(&cstack);f2 = GetTop(&fstack);Pop(&fstack);f1 = GetTop(&fstack);Pop(&fstack);f = Calculate(f1, f2, oper);Push(&fstack, f);}Pop(&cstack);}}while (!isEmpty(&cstack)){oper = GetTop(&cstack);Pop(&cstack);f2 = GetTop(&fstack);Pop(&fstack);f1 = GetTop(&fstack);Pop(&fstack);f = Calculate(f1, f2, oper);Push(&fstack, f);}printf("\nThe result is:%f\n", GetTop(&fstack));Pop(&fstack);}。
C语言随机生成四则运算题目
C语⾔随机⽣成四则运算题⽬⼀、基本功能1.随机产⽣含有“+”“-”“*”“/”中两个运算符的四则运算题⽬,运算数在1-100之间。
2.可以指定产⽣的题⽬的数量。
3.可指定题⽬的显⽰⽅式。
4.可指定多长时间显⽰正确答案。
⼆、实现思路调⽤rand函数产⽣随机数,不过要记得先调⽤srand(time(NULL))来启动启动随机数⽣成器,将产⽣的随机数对100取余再加1,就控制产⽣的随机数是在1-100之间的。
同样随机产⽣运算符。
根据输⼊的题⽬数量来指定for循环的次数,通过对输⼊的每⾏显⽰数量取余来控制每⾏的显⽰题⽬数量,通过⽔平制表符\t来控制显⽰排版,通过Sleep函数来控制多长时间显⽰题⽬的答案。
三、实现代码#include <stdio.h>#include <stdlib.h>#include <time.h>#include <windows.h>int main(){int num1, num2, num3, sum=0, result,flag;char op1, op2;int count,N,second;srand(time(NULL));printf("请输⼊想要产⽣题⽬的数⽬:");scanf("%d",&N);printf("请输⼊每⾏显⽰的题⽬数量:");scanf("%d",&count);printf("请输⼊想要多少秒后显⽰题⽬的答案:");scanf("%d",&second);printf("出题中请稍等...");for(int i=0;i<N;i++) {num1 = rand() % 100+1;num2 = rand() % 100+1;num3 = rand() % 100+1;switch (num1 % 4){case0:op1 = '+';break;case1:op1 = '-';break;case2:op1 = '*';break;case3:op1 = '/';break;default:break;}switch (num2 % 4){case0:op2 = '+';flag = 0;break;case1:op2 = '-';flag = 0;break;case2:op2 = '*';if((op1=='*')||(op1=='/'))flag = 0;elseflag = 1;break;case3:op2 = '/';if((op1=='*')||(op1=='/'))flag = 0;elseflag = 1;break;default:break;}if(sum%count==0)printf("\n");sum++;printf("%d %c %d %c %d =",num1,op1,num2,op2,num3);if(flag==0){result = 0;switch (op1){case'+':result = num1+num2;break;case'-':result = num1-num2;break;case'*':result = num1*num2;break;case'/':result = num1/num2;break;default:break;}switch (op2){case'+':result += num3;break;case'-':result -= num3;break;case'*':result *= num3;break;case'/':result /= num3;break;default:break;}}else{result = 0;switch (op2){case'+':result = num2+num3;break;case'-':result = num2-num3;break;case'*':result = num2*num3;break;case'/':result = num2/num3;break;default:break;}switch (op1){case'+':result = num1 + result;break;case'-':result = num1 - result;break;case'*':result = num1 * result;break;case'/':result = num1 / result;break;default:break;}}Sleep(1000*second);printf("%d \t",result);}printf("\n出题完成,感谢使⽤"); return0;}四、运⾏截图。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
四则运算姓名:学号:班级:1.功能结构图2.程序功能进行整数的加减乘除和求模运算。
程序采用随机产生1~100的两个数进行运算每种运算有10个题目用户输入对应的答案程序提示答案的对错最后统计正确率。
每次给出两次答题机会。
3.程序流程图4.函数列表及功能5.源程序代码#include<stdio.h>#include<stdlib.h>#include<time.h>#define N 10int f(int a,int b){ //自定义函数int result;result=a+b;return result;}int f1(int a,int b){int result;result=a-b;return result;}int f2(int a,int b){int result;result=a*b;return result;}int f3(int a,int b){int result;result=a*b/b;return result;}int mod(int a,int b){int result;result=a%b;return result;}int main(){int a,b,res,ans;int i,count;int op,c;srand((unsigned)time(NULL)); while(1){printf("\n---加减乘除运算练习系统---\n");printf("1.加法运算\n");printf("2.减法运算\n");printf("3.乘法运算\n");printf("4.除法运算\n");printf("5.求模运算\n");printf("6.混合运算\n");printf("0.退出练习\n");printf(" 请输入数字0~6:");scanf("%d",&op); //输入相应数字进行练习switch(op){case 1:printf("--请进行加法运算--\n");count=0;for(i=1;i<=N;i++){a=rand()%100+1;b=rand()%100+1;res=f(a,b); //调用加法函数printf("%d+%d=",a,b);scanf("%d",&ans); //输入数值if(ans==res){printf("Very Good!\n");count++;}else{printf("wrong! 请重新输入\n");scanf("%d",&ans);if(ans==res)printf("Very Good!\n");elseprintf("Wrong Answer!\n");}}printf("***正确率为%.0f%%***\n",100.0*count/N); break;case 2:printf("--请进行减法运算--\n");count=0;for(i=1;i<=N;i++){a=rand()%100+1;b=rand()%100+1;res=f1(a,b); //调用减法函数printf("%d-%d=",a,b);scanf("%d",&ans); //输入数值if(ans==res){printf("Very Good!\n");count++;}else{printf("wrong! 请重新输入\n");scanf("%d",&ans);if(ans==res)printf("Very Good!\n");elseprintf("Wrong Answer!\n");}}printf("***正确率为%.0f%%***\n",100.0*count/N); break;case 3:printf("--请进行乘法运算--\n");count=0;for(i=1;i<=N;i++){a=rand()%100+1;b=rand()%100+1;res=f2(a,b); //调用乘法函数printf("%d*%d=",a,b);scanf("%d",&ans); //输入数值if(ans==res){printf("Very Good!\n");count++;}else{printf("wrong! 请重新输入\n");scanf("%d",&ans);if(ans==res)printf("Very Good!\n");elseprintf("Wrong Answer!\n");}}printf("***正确率为%.0f%%***\n",100.0*count/N); break;case 4:printf("--请进行除法运算--\n");count=0;for(i=1;i<=N;i++){a=rand()%10+1;b=rand()%10+1;res=f3(a,b); //调用除法函数printf("%d/%d=",a*b,b);scanf("%d",&ans); //输入数值if(ans==res){printf("Very Good!\n");count++;}else{printf("wrong! 请重新输入\n");scanf("%d",&ans);if(ans==res)printf("Very Good!\n");elseprintf("Wrong Answer!\n");}}printf("***正确率为%.0f%%***\n",100.0*count/N); break;case 6:printf("--请进行混合运算--\n");count=0;for(i=1;i<=N;i++){a=rand()%100+1;b=rand()%100+1;c=rand()%5; //产生0~4的随机数{switch(c) //随机出现进行混合运算{case 0:printf("%d+%d=",a,b);res=f(a,b);break;case 1:printf("%d-%d=",a,b);res=f1(a,b);break;case 2:printf("%d*%d=",a,b);res=f2(a,b);break;case 3:a=a%10+1;b=b%10+1;printf("%d/%d=",a*b,b);res=f3(a,b);break;case 4:printf("%d%%%d=",a,b);res=mod(a,b);break;}}scanf("%d",&ans); //输入数值if(ans==res){printf("Very Good!\n");count++;}else{printf("wrong! 请重新输入\n");scanf("%d",&ans);if(ans==res)printf("Very Good!\n");elseprintf("Wrong Answer!\n");}}printf("***正确率为%.0f%%***\n",100.0*count/N); break;case 5:printf("--请进行求模运算--\n");count=0;for(i=1;i<=N;i++){a=rand()%100+1;b=rand()%100+1;res=mod(a,b); //调用求模函数printf("%d%%%d=",a,b);scanf("%d",&ans); //输入数值if(ans==res){printf("Very Good!\n");count++;}else{printf("wrong! 请重新输入\n");scanf("%d",&ans);if(ans==res)printf("Very Good!\n");elseprintf("Wrong Answer!\n");}}printf("***正确率为%.0f%%***\n",100.0*count/N); break;case 0:goto END; //转到结束}}END:printf("---练习结束---\n");return 0;}6程序运行图选取起始和加法混合运算程序开始显示6种练习方式输入0为退出。
加法运算练习结束后输出正确率。