#include <stdio.h> #include <stdlib.h> #include <time.h> int add(int num1,int num2) { return num1+num2; } int subtraction(int num1,int num2) { return num1-num2; } int mul(int num1,int num2) { return num1*num2; } float divis(int num1,int num2) { return ((int)(((float)num1/num2)*100+0.5))/100.0; } int main() { int type = -1; int data = -1; int choice,num1,num2,results; float div_result; char ch; int i,j,num3,num4; printf("请输入四则运算题目的数量: "); scanf("%d",&j); srand((unsigned int)time(NULL)); for(i=0;i<j;i++){ num3=rand()%100+1; num4=rand()%100+1; choice=rand()%8; switch (choice) { case 1: num1 = rand()%100+1; num2 = rand()%100+1; printf("%d+%d= ",num1,num2); break; case 2: num1 = rand()%100+1; num2 = rand()%100+1; printf("%d-%d= ",num1,num2); break; case 3: num1 = rand()%10; num2 = rand()%10; printf("%d*%d= ",num1,num2); break; case 4: num1 = rand()%10; num2 = rand()%10+1; printf("%d/%d= ",num1,num2); break; case 5: num1 = rand()%100+1; num2 = rand()%100+1; printf("%d/%d+%d/%d= ",num1,num3,num2,num4); break; case 6: num1 = rand()%100+1; num2 = rand()%100+1; printf("%d/%d-%d/%d= ",num1,num3,num2,num4); break; case 7: num1 = rand()%10; num2 = rand()%10; printf("%d/%d*%d/%d= ",num1,num3,num2,num4); break; case 8: num1 = rand()%10; num2 = rand()%10+1; printf("%d/%d/%d/%d= ",num1,num3,num2,num4); break; } } }
1. 编写一个能自动生成小学四则运算题目的程序。
- 题目:自动生成四则运算题目
- 主要功能:
- 除了整数以外,还能支持真分数的四则运算。
- 对实现的功能进行描述,并且对实现结果要求截图
- 设计思想:使用c-free的随机函数rand,之后利用switch语句实现一个简单的四则运算的符号选择。
总结:老师和同学们都说这次作业特别简单,但是在做的过程中,我还是遇到了很多问题。说明c语言的基础还有待提高,在接下来的时间要加强学习。