Lab 1 (four arithmetic exercises in primary school) and four arithmetic operations in primary school
# Include <stdio. h> # include <stdlib. h> int getChoice (); void showMenu (); void doExercise (int); int test (int); int getChoice () {int choice; scanf ("% d ", & choice); return choice;} void showMenu () {printf ("============================================= \ n "); printf ("\ n welcome to use the fourth arithmetic exercise SOFTWARE \ n"); printf ("\ n "); printf ("\ t1, addition exercise \ t2, subtraction exercise \ n"); printf ("\ t3, multiplication exercise \ t4, division exercise \ n "); printf ("\ t5, general exercise \ t0, exit system \ n"); printf ("\ n"); printf ("Enter your choice (0- -5): \ n "); printf ("\ n ======================================== \ n ");} void doExercise (int n) {int count = 0; int score = 0; // exercise score, initialized to 0 int I = 0; // counter of the number of exercise questions for (I = 1; I <= 10; I ++) {count = count + test (n); // call test (n) indirectly and recursively) score = count * 10;} printf ("You have done % d for ten questions in this exercise. The score is % d \ n", count, score );} /* function: test A Question */int test (int n) {int ranswer = 0; // The correct answer is int uanswer = 0; // user-entered answer int t = 0; // Temporary Variable char operation; // operation class int num1 = 0; // operand 1 int num2 = 0; // operand 2 srand (time (NULL); // initialize the random number seed num1 = rand () % 10; // obtain the random number num2 = rand () % 10 between 0-9; // enter the program function, if option 5 is selected, 1-4 represents a random generation of each operation method. if (n = 5) {n = rand () % 4 + 1;} switch (n) {case 1: operation = '+'; break; case 2: operation = '-'; break; case 3: operation = '*'; break; case 4: operation = '/'; break;}/* adds a statement to prevent "not enough subtraction". When Subtraction is selected and num1 is less than num2, swap two operands with temporary variables */if (operation = '-') & (num1 <num2) {t = num1; Num1 = num2; num2 = t;}/* prevents program vulnerabilities with a division of 0. When num2 is randomly set to 0, when num2 is forcibly set to 1 for division, the value of num1 is forcibly assigned to num1 * num2 to prevent division of */if (operation = '/') {if (num2 = 0) {num2 = 1;} num1 = num1 * num2; // prevent num1 from being divisible by num2} // output the test question itself and prompt the user to enter printf ("% d % c % d =", num1, operation, num2 ); scanf ("% d", & uanswer); // switch (operation) {case '+': ranswer = num1 + num2; break; case '-': ranswer = num1-num2; break; case '*': ranswer = num1 * num2; break; cas E '/': ranswer = num1/num2; break;} // judge. if yes, 1 is returned. if no result is returned, 0 if (uanswer = ranswer) {printf ("done! \ N "); return 1;} else {printf (" wrong! \ N "); return 0 ;}/// main function int main (void) {int choice = 0; // receives the user's selection // The cycle start function, do ...... While loop do {showMenu (); // display menu choice = getChoice (); // control the choice range if (choice <0 | choice> 5) {choice = 1; // force addition} if (choice = 0) {break; // The user selects 0 and exits the loop (exits the system)} doExercise (choice); // what to do, and start rating} while (choice! = 0); printf ("welcome to use this software. Goodbye! \ N "); return 0 ;}
Although the Code is relatively simple to write and the functions have not yet met the requirements of the teacher, this well-defined writing method is quite preferred. Functions to be improved...