Small Program for random generation of four Arithmetic Operations Based on c, four arithmetic applets

Source: Internet
Author: User

Small Program for random generation of four Arithmetic Operations Based on c, four arithmetic applets

A small program based on http://www.cnblogs.com/haozhe/p/5276763.html is developed based on the random generation of four small programs.

Github source code and engineering documentation address: https://github.com/HuChengLing-

Basic requirements:

In addition to integers, it also supports the four arithmetic operations of real scores.

Describes the implemented functions and requirements for the implementation results.

Main functions:You can select addition, subtraction, multiplication, division, and hybrid operation for arithmetic operations.

Design Philosophy:First, consider the calculation of the number range, consider the calculation ability of primary school students, and then the classification calculation method, divided into addition, subtraction, multiplication, division, and hybrid operation,

Then, you can choose whether to continue or exit the program. You can sum up the number of correct or wrong questions to facilitate the calculation of correct and wrong questions.

Second program code:Septem

# Include <stdio. h> # include <stdlib. h> # include <math. h> # include <time. h> # include <conio. h> # include <string. h> int right = 0; int wrong = 0; char getSignal (); int calculateIntegerResult (int num1, int num2, char signal); // int getNumber (); void doIntegerSubject (); void falsePrint (int); void correctPrint (int);/* char getSignal () {char signal [4] = {'+ ','-', '*', '/'}; srand (unsigned) time (NULL); return signal [rand () % 4 ];} */Char getSignal () {srand (unsigned) time (NULL); int a; a = rand () % 4 + 1; switch () {case 1: return '+'; break; case 2: return '-'; break; case 3: return '*'; break; case 4: return '/'; break; default: break;}/* int getNumber () {srand (unsigned) time (NULL); int a, B; a = rand () % 100 + 1; B = rand () % 100 + 1; return 0;} */int calculateIntegerResult (int num1, int num2, char signal) {int t; switch (Signal) {case '+': t = num1 + num2; break; case '-': t = num1-num2; break; case '*': t = num1 * num2; break; case '/': t = num1/num2; break; default: t = 0; printf ("the operator does not comply with the rule \ n "); break;} return t;} void calculateMarkResult (int num1, int num2, int num3, int num4, char signal, int mark [5]) {switch (signal) {case '+': if (num2 = num4) {mark [0] = num1 + num3; mark [1] = num1;} else {mark [0] = nu M1 * num4 + num2 * num3; mark [1] = num2 * num4;} break; case '-': if (num2 = num4) {mark [0] = num1-num3; mark [1] = num1;} else {mark [0] = num1 * num4-num2 * num3; mark [1] = num2 * num4;} break; case '*': mark [0] = num1 * num3; mark [1] = num2 * num4; break; case '/': mark [0] = num1 * num4; mark [1] = num2 * num3; break; default: printf ("the operator does not comply with the rule \ n "); break;} void doIntegerSubject () {int a, B, giverAnswer, print Flag; char signal; srand (unsigned) time (NULL); a = rand () % 100 + 1; B = rand () % 100 + 1; signal = getSignal (); if (signal = '/') {if (B = 0) {int temp; temp = B; B =; a = temp ;}} printf ("please do blew problem: \ n"); printf ("% d % c % d =", a, signal, B ); // giverAnswer = _ getch (); scanf_s ("% d", & giverAnswer); fflush (stdin); if (calculateIntegerResult (a, B, signal) = giverAnswer) {right ++; Srand (unsigned) time (NULL); printFlag = rand () % 4 + 1; correctPrint (printFlag);} else {wrong ++; srand (unsigned) time (NULL); printFlag = rand () % 4 + 1; falsePrint (printFlag); printf ("\ n correct answer: % d \ n", calculateIntegerResult (, b, signal) ;}} void doMarkSubject () {int a, B, c, d, I = 0, printFlag; int result [5]; char giverNumber [10]; char signal; int x, y, j; signal = getSignal (); srand (unsigned) Time (NULL); while (1) {a = rand () % 100; while (1) {B = rand () % 100; if (B) break ;} if (a <B) break;} while (1) {c = rand () % 100; while (1) {d = rand () % 10; if (d) break;} if (c <d) break;}/* a = rand () % 100 + 1; B = rand () % 100 + 1; c = rand () % 100 + 1; d = rand () % 100 + 1; signal = getSignal (); if (B = 0 | a> B) {int temp; temp = B; B = a; a = temp;} if (d = 0 | c> d) {in T f; f = B; B = a; a = f;} */printf ("please do blew problem: \ n "); printf ("% d/% d % c % d/% d =", a, B, signal, c, d); // giverAnswer = _ getch (); I = 0; while (1) {giverNumber [I] = _ getche (); if (giverNumber [I] = '/') break; I ++ ;} x = 0; for (j = 0; j <I; j ++) {x = x + pow (10, (I-1-j )) * (giverNumber [j]-'0');} scanf_s ("% d", & y); calculateMarkResult (a, B, c, d, signal, result ); // scanf_s ("% d ", & GiverNumber [I]); fflush (stdin); if (x = result [0] & y = result [1]) {right ++; srand (unsigned) time (NULL); printFlag = rand () % 4 + 1; correctPrint (printFlag);} else {wrong ++; srand (unsigned) time (NULL); printFlag = rand () % 4 + 1; falsePrint (printFlag); int * dd; printf ("\ n correct answer: % d/% d \ n ", result [0], result [1]); // printf (" \ n correct answer: % d \ n ", calculateMarkResult (, b, signal, c, d) ;}// the correct information is printed. vo Id correctPrint (int num) {switch (num) {case 1: printf ("You're right! \ N "); break; case 2: printf (" It's OK! \ N "); break; case 3: printf (" Correct! \ N "); break; case 4: printf (" Perfect! \ N "); break; case 5: printf (" Oh my gag! \ N "); break; default: printf (" Powerful! \ N "); break ;}// response error message void falsePrint (int num) {switch (num) {case 1: printf (" You're lost! "); Break; case 2: printf (" Failure! "); Break; case 3: printf (" Come on! "); Break; case 4: printf (" It's wrong! "); Break; default: printf (" Incorrect! "); Break ;}} void main () {int total; int con = 0, choice, choice1; printf (" welcome to math world!: "); System (" cls "); if (con = 0) {printf (" \ t 1. integer Operation \ n "); printf (" \ t 2. score calculation \ n "); printf (" select \ n "); scanf_s (" % d ", & choice); system (" cls "); switch (choice) {case 1: while (right >=wrong) {doIntegerSubject () ;}total = right + wrong; printf ("You have answered a total of questions: % d correct % d wrong % d \ n ", total, right, wrong); break; case 2: while (right> = wrong) {doMarkSubject () ;}total = right + wrong; printf ("You answered in total: % d % D: \ n ", total, right, wrong); break;} system (" pause ");} printf ("\ n \ t \ t1. reselect? \ N "); printf (" \ n \ t \ t2. exit the operation? \ N "); scanf_s (" % d ", & choice1); if (choice1 = 1) {system (" cls "); con = 0 ;} else if (choice1 = 2) {exit;} else printf ("Sorry !, The command you entered is incorrect! Enter again! \ N "); system (" pause ");}

Summary:A simple automatic random generation of Baidu source code before the four arithmetic operations, its function is mainly to generate random symbols and random numbers to achieve random addition, subtraction, multiplication, division of the number of arithmetic expressions, he does not allow the user to choose whether to continue or exit the system, nor does he support positive score calculation. Based on the network source code, I modified the original random retrieval function, and then added the score calculation and score judgment, and the variables for calculating the incorrect data, and the output can be used to determine whether to continue the operation.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.