C語言實現四則運算的生成器


  該四則運算生成器前期只有簡單的功能,全部代碼均是小編自己編寫,用的是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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM