码迷,mamicode.com
首页 > 编程语言 > 详细

C语言实现四则运算的生成器

时间:2017-09-28 22:26:37      阅读:321      评论:0      收藏:0      [点我收藏+]

标签:cti   输出   nbsp   src   回车   简单的   log   dom   rate   

  该四则运算生成器前期只有简单的功能,全部代码均是小编自己编写,用的是C语言工具是VS2013,只支持windows平台运行。由用户界面但无图形化界面。

  功能目前只有两个,一是在cmd窗口中显示随机生成50道计算题,有四列整齐排布,用户可以控制上下左右来移动光标到每道题的等号后面来答题,答完一道题之后可以回车到下一道题,最后打完键入q退出并显示每道题的答案和用户的答案。二是随机生成四则运算,但并无答题功能。

  有用户界面,原来计划实现5个功能,分别是单一的加减乘除,最后一个是50道题中随机的生成加减运算。代码只实现了1号和5号功能,其余的在之后完善。

  中途有许多bug,有随机数的生成问题,其中用时最多的是解决光标的移动函数的调用问题,还有一个是接收用户输入的答案将其由字符转换成数字用到函数atoi并将用户答案存放在整形数组中。

  对该四则运算生成器的未修改Bug和未来拓展思路

    1.接收输入问题,该程序只能接收一次用户输入,如果用户有一次输入错误虽修改后,可能用户的答案数组中保存的是之前错误的答案。该Bug还未修正优化。

    2.还可以优化的是:可以加入判断答案对错的功能,并给出总分和那些题出错;加入鼠标点击功能,更方便用户跳转题目。

    3.对2、3、4号功能的实现,大致的代码与1号功能的差不多。

  缺点不足:本次的程序设计对时间上的安排不合理,不该在编程时就想到优化界面,从而放弃主要功能的实现,导致2、3、4号功能未实现,以及在功能上面的拓展也未实现。

  项目源码

技术分享
 1 #include "SimpleArithmetic.h"
 2 void main()
 3 {
 4     while (1)
 5     {
 6         int num;
 7         WelcomeMenu();
 8         num = Menu();
 9         FunctionRealize(num);
10         OverMenu();
11         break;
12     }
13 }
SimpleArithmetic.cpp

 

技术分享
 1 #include <stdio.h>
 2 #include <conio.h>
 3 #include <stdlib.h>
 4 #include <time.h>
 5 #include <windows.h>
 6 /*
 7 srand()函数定义 : void srand (unsigned int seed);
 8 通常可以利用geypid()或time(0)的返回值来当做seed
 9 如果你用time(0)的话,要加入头文件#include<time.h>
10 
11 例如:
12 #include<stdio.h>
13 #include<stdlib.h>
14 #include<time.h>
15 #define random(x) (rand()%x)
16 
17 void main()
18 {
19 
20 srand((int)time(0));
21 for(int x=0;x<10;x++)
22 printf("%d/n",random(100));
23 }
24 */
25 
26 #define range0 100;        //数字计算的范围
27 #define range1 3;        //运算符有+,-,*,/
28 #define random(x) rand()%x;//产生随机数
29 
30 //欢迎菜单
31 void WelcomeMenu();
32 //主菜单
33 int Menu();
34 //结束菜单
35 void OverMenu();
36 //功能实现
37 void FunctionRealize(int num);
38 //选择四则运算
39 void ChoseArithmetic();
40 void RandomArithmetic();                //随机四则运算switch
41 
42 //运算模板
43 void ModelOperation(int a, int b, char c);
44 
45 void Addition(int a, int b);        //  +
46 void Subtraction(int a, int b);        //  - 
47 void Multiplication(int a, int b);    //  *
48 void Division(int a, int b);        //  /
49 void ADD();
SimpleArithmetic.h

 

技术分享
  1 #include "SimpleArithmetic.h"
  2 //菜单实现
  3 int Menu()
  4 {
  5     int a;
  6     printf("\n\n\n--------------------------------------------------------------------------------\n");
  7     printf("\t\t\t--------------------------------\n");
  8     printf("\t\t\t\t欢迎来到运算练习\n");
  9     printf("\t\t\t    1.加法运算\n");
 10     printf("\t\t\t    2.减法运算\n");
 11     printf("\t\t\t    3.乘法运算\n");
 12     printf("\t\t\t    4.除法运算\n");
 13     printf("\t\t\t    5.四则运算\n");
 14     printf("\t\t\t    0.退出\n");
 15     printf("\t\t\t--------------------------------\n");
 16     printf("\t\t\t请输入你的选项:");
 17     scanf_s("%d", &a);
 18     system("cls");
 19     return a;
 20 }
 21 //功能实现
 22 void FunctionRealize(int num)
 23 {
 24     switch (num)
 25     {
 26     case 1:
 27         ADD();
 28         break;
 29     case 2:
 30         break;
 31     case 3:
 32         break;
 33     case 4:
 34         break;
 35     case 5:
 36         RandomArithmetic();
 37         break;
 38     case 0:
 39         exit(0);
 40         break;
 41     }
 42     system("cls");
 43 }
 44 //欢饮菜单
 45 void WelcomeMenu()
 46 {
 47     printf("\n\n\n————————————————————————————————————————\n");
 48     printf("\tWelcome to use four arithmetic questions to generate program\n");
 49     printf("\t\t\t欢迎使用四则运算题目生成程序\n");
 50     printf("\n\n\n                                                    This program made by ZLTiger\n");
 51     printf("————————————————————————————————————————\n");
 52     printf("please input any key to continue....\n");
 53     _getch();
 54     system("cls");
 55 }
 56 //结束菜单
 57 void OverMenu()
 58 {
 59     printf("\n\n\n★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★");
 60     printf("\n\t\t感谢使用本程序,希望你的数学有个好的提升!\n");
 61     printf("\n\t\t\t如果你对本程序有什么建议,感谢你的提出!\n");
 62     printf("\n\n                                                            Author:ZLTiger\n");
 63     printf("★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★");
 64     printf("please input any key to continue...");
 65     _getch();
 66 }
 67 
 68 
 69 //选择四则运算
 70 void ChoseArithmetic()
 71 {}
 72 //随机四则运算switch
 73 void RandomArithmetic()
 74 {
 75     srand((int)time(0));
 76     int a = 0, b = 0, Operator;
 77     char ch;
 78     a = random(range0);
 79     b = random(range0);
 80     Operator = random(range1);
 81     switch (Operator)
 82     {
 83     case 0:
 84         ch = +;
 85         ModelOperation(a, b, ch);
 86         break;
 87     case 1:
 88         ch = -;
 89         ModelOperation(a, b, ch);
 90         break;
 91     case 2:
 92         ch = *;
 93         ModelOperation(a, b, ch);
 94         break;
 95     case 3:
 96         ch = /;
 97         ModelOperation(a, b, ch);
 98         break;
 99     }
100 }
101 void Addition(int a, int b)        //  +
102 {
103     printf("%d+%d=\n", a, b);
104 }
105 void Subtraction(int a, int b)        //  -
106 {
107     printf("%d-%d=\n", a, b);
108 }
109 void Multiplication(int a, int b)    //  *
110 {
111     printf("%d*%d=\n", a, b);
112 }
113 void Division(int a, int b)        //  /
114 {
115     printf("%d/%d=\n", a, b);
116 }
117 //模板运算
118 void ModelOperation(int a, int b, char ch)
119 {
120     float c, answer = 0;
121     char yn;
122     switch (ch)
123     {
124     case +:
125         c = a + b;
126         break;
127     case -:
128         c = a - b;
129         break;
130     case *:
131         c = a * b;
132         break;
133     case /:
134         c = a / b;
135         break;
136     }
137     printf("\n\n\n————————————————————————————————————————\n");
138     printf("%d%c%d=", a, ch, b);
139     scanf_s("%f", &answer);
140     if (answer == c)
141     {
142         printf("OK!That is right\n");
143     }
144     else
145     {
146         printf("Wrong!You shoule good learn\n");
147     }
148 }
149 void ADD()
150 {
151     HANDLE hout;
152     COORD coord;//屏幕上的坐标
153     int realize[100], user[100];
154     int a, b, count = 1, u_ss = 0, u_count = 1;
155     char ch;
156     char ss[5];
157     srand((int)time(0));
158     hout = GetStdHandle(STD_OUTPUT_HANDLE);//从键盘获取输入,如果是方向键则执行方向功能,如果是回车键则换行,如果是字符则输出
159     printf("答完题后按q键退出!\n");
160     printf("--------------------------------------------------------------------------------");
161     for (int i = 0; i < 50; i++)
162     {
163         a = random(range0);
164         b = random(range0);
165         realize[count] = a + b;
166         count++;
167         printf("%2d%c%2d=              ", a, +, b);
168     }
169     coord.X = 6;
170     coord.Y = 2;
171     SetConsoleCursorPosition(hout, coord);
172     while (1)
173     {
174         ch = _getch();
175         if (ch == q)
176         {
177             break;
178         }
179         printf("%c", ch);
180         if (ch == 0x0d)
181         {
182             a = atoi(ss);
183             ss[2] = {   };
184             u_ss = 0;
185             user[u_count] = a;
186             u_count++;
187             coord.X += 20;
188             SetConsoleCursorPosition(hout, coord);
189             if (coord.X > 80)
190             {
191                 coord.Y += 1;
192                 coord.X = 6;
193                 SetConsoleCursorPosition(hout, coord);
194             }
195         }
196         else if (ch >= 0 || ch <= 9)
197         {
198             ss[u_ss] = ch;
199             u_ss++;
200         }
201     }
202     printf("\n正确答案:\n");
203     for (int i = 0; i < 50; i++)
204     {
205         printf("%4d", realize[i + 1]);
206     }
207     printf("\n你的答案:\n");
208     for (int i = 0; i < 50; i++)
209     {
210         printf("%4d", user[i + 1]);
211     }
212     _getche();
213     printf("\nPlease input any key to continue...");
214     system("cls");
215 }
RealizeFunction.cpp

  本项目源代码上传至个人的GitHub:https://github.com/BelieveMyself-ZLH/Four_Arithmetic_Operations

C语言实现四则运算的生成器

标签:cti   输出   nbsp   src   回车   简单的   log   dom   rate   

原文地址:http://www.cnblogs.com/Blog-Of-ZhouLinHu/p/7561011.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!