用C语言编写一个小程序
求大神,用C语言编写一个程序,完成一个问题,就是家长每天给孩子布置30道加减法,用C语言实现 新手菜鸟,没有思路,求大神指引
#include <stdio.h> #include <time.h> #include <stdlib.h> #define MAX 100 int main(int argc, char **argv) { int x , y , n , i , j , z , k , l ; srand(time(NULL)); printf("输入想要随机的题目数:"); scanf("%d",&n); if ( n >= 0 && n <= MAX){ printf("输入随机数的范围,例如 -100 100 为-100~100里的随机数,中间有空格隔开: "); if ((i = scanf("%d %d",&k,&l)) == 2){ l = l - k; for ( i = 1 ; i <= n ; i++ ){ x = rand()%l; x=x+k; y = rand()%l; y=y+k; j = rand()%2; printf("第%d题: (%d) %c (%d) = ",i,x,((j == 0) ? ('+') : ('-')),y); scanf("%d",&z); if (j == 0){ ((x+y) == z) ? (printf("第%d题: 正确\n",i)) : (printf("第%d题: 错误\n",i)) ; } if (j == 1){ ((x-y) == z) ? (printf("第%d题: 正确\n",i)) : (printf("第%d题: 错误\n",i)) ; } } } } return 0; }
[此贴子已经被作者于2017-2-25 15:08编辑过]
#include <stdio.h> #include <time.h> int main(void) { FILE *fp; if ((fp = fopen("今日题目.txt", "a")) == NULL) { fprintf(stderr, "创建题目失败\n"); exit(0); } srand((unsigned)time(NULL)); for (int i = 0; i < 10; i++) { if (i % 4 == 0) fprintf(fp, "%d + %d = \t\t%d + %d = \t\t%d + %d = \n", rand(), rand(), rand(), rand(), rand(), rand()); if (i % 4 == 1) fprintf(fp, "%d - %d = \t\t%d - %d = \t\t%d - %d = \n", rand(), rand(), rand(), rand(), rand(), rand()); if (i % 4 == 2) fprintf(fp, "%.2f + %.2f = \t\t%.2f + %.2f = \t\t%.2f + %.2f = \n", rand()*0.7, rand()*0.7, rand()*0.7, rand()*0.7, rand()*0.7, rand()*0.7); if (i % 4 == 3) fprintf(fp, "%.2f - %.2f = \t\t%.2f - %.2f = \t\t%.2f - %.2f = \n", rand()*0.7, rand()*0.7, rand()*0.7, rand()*0.7, rand()*0.7, rand()*0.7); } fclose(fp); return 0; }