c language implements a scientific calculator (dividing and rounding) with arithmetic practice functions

Catalog

1. calculate.h header file definition

2. Write main.c source file, choose calculator mode or arithmetic practice mode

3. In calculator. Writing related functions for scientific calculators in C source files

3.1 Create data and symbol stacks to store data and symbols in formulas

3.2 Initialize data stack and symbol stack header pointer labeled -1, data.top = -1 and symbol.top = -1

3.3 Start by simply writing the void calculator() function, opening up string space to store the input formulas in the string str

3.4 Write a loop in the calculator() function to store data and symbols in str strings on the data and symbol stacks

3.5 Write functions to store data and symbols on the stack

3.6 Write functions to read stack top symbols

3.7 Writing functions to output data and symbols

3.8 Writing functions for four operations

4. On exercise. Writing functions related to arithmetic exercises in C source file

4.1 Write the void exercise() function

4.2 Write the first order addition and subtraction exercises for the primary() function

4.2 Write the median factorial division exercise, the medium() function, involving floating-point numbers, and keep the results two decimal places compared to the input numbers

4.3 Write the high() function for the high-order synthesis exercise, noting that one place does not involve division and does not round

5. Complete Functions

5.1 calculate.h header file

5.2 calculator.c Source File

5.3 exercise.c Source File

5.4 main.c Source File

First effect:

 

(PS: Deliberately answered two wrong questions in order to show the effect.. It was not a mistake.)

Ideas:

1. Create calculate.h header file, function declaration;

2. Create a calculator.c source file, write scientific calculator function calculator();

3. Create exercise.c source file, write arithmetic exercise function exercise();

4. Create main.c source file, run the main function.

Steps:

1. calculate.h header file definition

#include <stdio.h>
#Include <stdlib. H> //Clear Screen
#Include <time. H> //Random Numbers, Timing

void calculator();		//Calculator calculation

void exercise();	//number work

2. Write main.c source file, choose calculator mode or arithmetic practice mode

#include "calculate.h"

void menu()
{
	printf("************************************\n");
	printf("***********1.Calculator   **************\n");
	printf("***********2.Arithmetic exercises **************\n");
	printf("***********0.Sign out     **************\n");
	printf("************************************\n");
	printf("Please select:>");
}

int main()
{
	int input;
	do
	{
		menu();
		scanf("%d", &input);
		system("cls");	//Clear screen, improve the look and feel
		switch (input)
		{
		case 1:
			calculator();	//Calculator function
			break;
		case 2:
			exercise();		//Calculating exercise functions
			break;
		case 0:
			printf("Exit the program\n");
			break;
		default:
			printf("Input error, please re-enter\n");
			break;
		}
	} while (input);
	return 0;
}

3. In calculator. Writing related functions for scientific calculators in C source files

3.1 Create data and symbol stacks to store data and symbols in formulas

struct shujv
{
	int data[100];
	int top;
};

struct fuhao
{
	char symbol[100];
	int top;
};

3.2 Initialize data stack and symbol stack header pointer labeled -1, data.top = -1 and symbol.top = -1

void InitNum(struct shujv* Num)
{
	Num->top = -1;
}

void InitSymbol(struct fuhao* Symbol)
{
	Symbol->top = -1;
}

3.3 Start by simply writing the void calculator() function, opening up string space to store the input formulas in the string str

void calculator()
{
	struct shujv data;
	struct fuhao symbol;	//Create Data Stack, Symbol Stack
	InitNum(&data);
	InitSymbol(&symbol);	//Initialize Data Stack, Symbol Stack
	int i, j;
	i = j = 0;
	int v1, v2, ret;	//Variables for calculation
	char c;
	char v[100];
	printf("                Calculator\n");
	printf("************************************\n");
	printf("Please enter an equation:  ");
	char* str = (char*)malloc(sizeof(char) * 200);	//Open up space to store input formulas
	getchar();	//Absorb'\n'from last selection
	while ((c = getchar()) != '\n')
	{
		str[i] = c;
		i++;
	}
	str[i] = '\0';
	//The following is a calculation loop in which the data and symbols in the formula are stored on different stacks
}

3.4 Write a loop in the calculator() function to store data and symbols in str strings on the data and symbol stacks

3.4.1 Creates the string number v[100], and if str[i] is a number, loops through the consecutive numbers into the string v, then converts the string V to a number in the data stack using the function atio(v). Data

3.4.2 If str[i] is a character, make a priority judgement (str[i] is str[symbol.tol]) and save the decision in the symbol stack symbol. Symbols or let the previous data do the operation;

  1. If symbol.top = -1 and -, is a negative sign symbol, stored directly in the symbol stack, and then stored in the data;
  2. If symbol.top = -1, indicating that the symbol stack is empty, saving str[i] directly;
  3. If str[i] is (, (highest priority, store directly in the symbol stack;
  4. If str[i] is + or -, read the stack top symbol (str[symbol.top]). If the top symbol of the stack is (, save directly; if the top symbol of the stack is + or -), a stack loop is performed (let the first two data be computed and the top symbol is consumed), and then str[i] is saved; If the top symbol of the stack is * or /, it is also a stack loop, and then str[i] is saved;
  5. If str[i] is * or /, read the top symbol as well. If the top symbol of the stack is (, save directly; if the top symbol of the stack is + or -, str[i] is saved directly because of the higher priority of division; if the top symbol of the stack is * or /, loop out and then save str[i];
  6. If str[i] is, the stack loops until it encounters (, then (disposes of, str[i] does not reside on the data stack)

3.4.3 When the scan string str loop ends, a stack loop is repeated until symbol[symbol.top] = -1, at which point data.data[0] is what we need.

for (i = 0; str[i] != '\0'; i++)
	{
		if (i == 0 && str[i] == '-')
		{
			v[j++] = str[i];
		}
		else if (str[i] == '(' && str[i + 1] == '-')
		{
			i++;
			v[j++] = str[i++];
			while (str[i] >= '0' && str[i] <= '9')
			{
				v[j] = str[i];
				j++;
				i++;
			}
			Inshujv(&data, atoi(v));	//Stores data on the data stack, and atoi() converts strings to integers of type int
			while (j > 0)               //data.top++
			{
				v[j] = 0;
				j--;
			}
			if (str[i] != ')')
			{
				i--;
				Infuhao(&symbol, '(');    //Save the symbol on the symbol stack, symbol.top++.
			}
            else
            {
                Putfuhao(&symbol);    //The Putfuhan function is symbol.top--, get rid of'('
            }
		}
		else if (str[i] >= '0' && str[i] <= '9')
		{
			while (str[i] >= '0' && str[i] <= '9')
			{
				v[j] = str[i];
				j++;
				i++;
			}
			Inshujv(&data, atoi(v));
			while (j > 0)
			{
				v[j] = 0;
				j--;
			}
			i--;
		}
		else
		{
			if (symbol.top == -1)	//If the symbol stack has no elements, save the symbol directly
			{
				Infuhao(&symbol, str[i]);
			}
			else if (str[i] == '(')		//If it's the highest priority'('it's also stored directly in the symbol stack
			{
				Infuhao(&symbol, str[i]);
			}
			else if (str[i] == '+' || str[i] == '-')	//At this point, the priority is determined by the top symbol of the stack
			{
				if (Readfuhao(&symbol) == '(')	//If the top of the stack is'(', save directly
				{
					Infuhao(&symbol, str[i]);
				}
				else if (Readfuhao(&symbol) == '+' || Readfuhao(&symbol) == '-')	//If the top of the stack makes a stack operation for addition and subtraction
				{
					while (symbol.top >= 0 && data.top >= 1)
					{
						v2 = Putshujv(&data);
						v1 = Putshujv(&data);
						ret = Math(v1, v2, Putfuhao(&symbol));
						Inshujv(&data, ret);
					}
					Infuhao(&symbol, str[i]);
				}
				else if (Readfuhao(&symbol) == '*' || Readfuhao(&symbol) == '/')
				{
					while (symbol.top >= 0 && data.top >= 1)
					{
						v2 = Putshujv(&data);
						v1 = Putshujv(&data);
						ret = Math(v1, v2, Putfuhao(&symbol));
						Inshujv(&data, ret);
					}
					Infuhao(&symbol, str[i]);
				}
			}
			else if (str[i] == '*' || str[i] == '/')
			{
				if (Readfuhao(&symbol) == '(')
				{
					Infuhao(&symbol, str[i]);
				}
				else if (Readfuhao(&symbol) == '+' || Readfuhao(&symbol) == '-')
				{
					Infuhao(&symbol, str[i]);
				}
				else if (Readfuhao(&symbol) == '*' || Readfuhao(&symbol) == '/')
				{
					while (symbol.top >= 0 && data.top >= 1 && symbol.symbol[symbol.top] != '(' &&
						symbol.symbol[symbol.top] != '+' && symbol.symbol[symbol.top] != '-')
					{
						v2 = Putshujv(&data);
						v1 = Putshujv(&data);
						ret = Math(v1, v2, Putfuhao(&symbol));
						Inshujv(&data, ret);
					}
					Infuhao(&symbol, str[i]);
				}
			}
			else if (str[i] == ')')		//If')', make a station operation until you encounter'('
			{
				do
				{
						v2 = Putshujv(&data);
						v1 = Putshujv(&data);
						ret = Math(v1, v2, Putfuhao(&symbol));
						Inshujv(&data, ret);
				} while (symbol.symbol[symbol.top] != '(');
				Putfuhao(&symbol);	//Dispose of'('
			}
		}
	}
	while (symbol.top >= 0 && data.top >= 1)
	{
		v2 = Putshujv(&data);
		v1 = Putshujv(&data);
		ret = Math(v1, v2, Putfuhao(&symbol));
		Inshujv(&data, ret);
	}
	system("cls");
	printf("                Calculator\n");
	printf("************************************\n");
	printf("%s = %d\n\n",str, data.data[0]);
	free(str);

3.5 Write functions to store data and symbols on the stack

//Save on Data Stack
void Inshujv(struct shujv* Num, int n)
{
	Num->top++;
	Num->data[Num->top] = n;
}

//Save on Symbol Stack
void Infuhao(struct fuhao* Symbol, char ch)
{
	Symbol->top++;
	Symbol->symbol[Symbol->top] = ch;
}

3.6 Write functions to read stack top symbols

//Read stack top symbols
char Readfuhao(struct fuhao* Symbol)
{
	return Symbol->symbol[Symbol->top];
}

3.7 Writing functions to output data and symbols

//output data
int Putshujv(struct shujv* Num)
{
	int x;
	x = Num->data[Num->top];
	Num->top--;
	return x;
}

//Output Symbol
char Putfuhao(struct fuhao* Symbol)
{
	char ch;
	ch = Symbol->symbol[Symbol->top];
	Symbol->top--;
	return ch;
}

3.8 Writing functions for four operations

//Four Operations
int Math(int a, int b, char ch)
{
	if (ch == '+')
		return a + b;
	if (ch == '-')
		return a - b;
	if (ch == '*')
		return a * b;
	if (ch == '/')
		return a / b;
}

4. On exercise. Writing functions related to arithmetic exercises in C source file

4.1 Write the void exercise() function

#include "calculate.h"

void e_menu()
{
	printf("*****************************\n");
	printf("******** 1.Initial Practice   *******\n");
	printf("******** 2.Intermediate Practice   *******\n");
	printf("******** 3.Advanced Practice   *******\n");
	printf("******** 0.Exit Practice   *******\n");
	printf("*****************************\n");
}

void exercise()
{
	srand((unsigned int)time(NULL));
	int n;
	do
	{
		e_menu();
		printf("Please select:>");
		scanf("%d", &n);
		system("cls");
		switch (n)
		{
		case 1:
			primary();	//Initial Practice, Addition and Subtraction
			break;
		case 2:
			medium();	//Intermediate Practice, Multiplication and Division
			break;
		case 3:
			high();		//Advanced Exercises, Comprehensive Training
			break;
		case 0:
			printf("Exit Practice\n");
			break;
		default:
			printf("Input error, please re-enter\n");
			break;
		}
	} while (n);
}

4.2 Write the first order addition and subtraction exercises for the primary() function

void primary()
{
	clock_t start, finish, total;	//Calculate answer time
	int min, sec, ms;
	int i, n;
	int count = 0;		//Record number of errors
	printf("          Initial Practice\n");
	printf("---------------------------------\n");
	printf("Enter the number of topics you want to challenge:");
	scanf("%d", &n);
	start = clock();
	for (i = 0; i < n; i++)
	{
		int a = rand() % 100;
		int b = rand() % 100;
		int c = rand() % 100;
		if (i % 2 == 0)
		{
			printf("\n No. %d Title:%d + %d - %d = ?\n", i + 1, a, b, c);
			printf("Please enter an answer:");
			int s;
			scanf("%d", &s);
			while (s != (a + b - c))
			{
				printf("\n Wrong answer\n Please re-enter:");
				count++;
				scanf("%d", &s);
			}
			printf("\n Congratulations, the answer is correct!\n");
		}
		else
		{
			printf("\n No. %d Title:%d - %d + %d = ?\n",i + 1, a, b, c);
			printf("Please enter an answer:");
			int s;
			scanf("%d", &s);
			while (s != (a - b + c))
			{
				printf("\n Wrong answer\n Please re-enter:");
				count++;
				scanf("%d", &s);
			}
			printf("\n Congratulations, the answer is correct!\n");
		}
	}
	finish = clock();
	total = (double)(finish - start) / CLOCKS_PER_SEC;	//Convert units to seconds
	min = (int)total / 60;
	sec = (int)total % 60;
	ms = (int)(finish - start) - 60000 * min - 1000 * sec;
	printf("\n Number of wrong answers:%d", count);
	printf("\n This challenge %d Topics, when shared %d branch %d second %d Millisecond\n\n", n, min, sec, ms);
}

4.2 Write the median factorial division exercise, the medium() function, involving floating-point numbers, and keep the results two decimal places compared to the input numbers

void medium()
{
	clock_t start, finish, total;
	int min, sec, ms;
	int i, n;
	int count = 0;
	printf("          Intermediate Practice\n");
	printf("---------------------------------\n");
	printf("Enter the number of topics you want to challenge:");
	scanf("%d", &n);
	start = clock();
	for (i = 0; i < n; i++)
	{
		float a = rand() % 100;
		float b = rand() % 100;
		float c = rand() % 10 + 1;
		float ret = 0;
		float temp = 0;
		if (i % 2 == 0)
		{
			printf("\n No. %d Title:%.1f * %.1f / %.1f = ?\n", i + 1, a, b, c);
			ret = a * b / c;
			temp = (int)(ret * 100 + 0.5);
			temp = temp / 100;
			printf("Enter the answer (keep two decimal places):");
			float s;
			scanf("%f", &s);
			while (s != temp)
			{
				printf("\n Wrong answer\n Please re-enter:");
				count++;
				scanf("%f", &s);
			}
			printf("\n Congratulations, the answer is correct!\n");
		}
		else
		{
			printf("\n No. %d Title:%.1f / %.1f * %.1f = ?\n", i + 1, a, c, b);
			ret = a / c * b;
			temp = (int)(ret * 100 + 0.5);
			temp = temp / 100;                //Round the result around leaving two decimal places to assign to temp
			printf("Enter the answer (keep two decimal places):");
			float s;
			scanf("%f", &s);
			while (s != temp)
			{
				printf("\n Wrong answer\n Please re-enter:");
				count++;
				scanf("%f", &s);
			}
			printf("\n Congratulations, the answer is correct!\n");
		}
	}
	finish = clock();
	total = (int)(finish - start) / CLOCKS_PER_SEC;	    //Convert units to seconds
	min = (int)total / 60;
	sec = (int)total % 60;
	ms = (int)(finish - start) - 60000 * min - 1000 * sec;
	printf("\n Number of wrong answers:%d", count);
	printf("\n This challenge %d Topics, when shared %d branch %d second %d Millisecond\n\n", n, min, sec, ms);
}

4.3 Write the high() function for the high-order synthesis exercise, noting that one place does not involve division and does not round

void high()
{
	clock_t start, finish, total;
	int min, sec, ms;
	int i, n;
	int count = 0;
	printf("          Advanced Practice\n");
	printf("---------------------------------\n");
	printf("Enter the number of topics you want to challenge:");
	scanf("%d", &n);
	start = clock();
	for (i = 0; i < n; i++)
	{
		float a = rand() % 100;
		float b = rand() % 100;
		float c = rand() % 100;
		float d = rand() % 10 + 1;        //Add one to prevent decimals from appearing in dividends
		float ret = 0;
		float temp = 0;
		if (i % 5 == 0)
		{
			printf("\n No. %d Title:%.1f + %.1f * %.1f / %.1f = ?\n", i + 1, a, b, c, d);
			ret = a + b * c / d;
			temp = (int)(ret * 100 + 0.5);
			temp = temp / 100;
			printf("%.2f\n", temp);
			printf("Enter the answer (keep two decimal places):");
			float s;
			scanf("%f", &s);
			while (s != temp)
			{
				printf("\n Wrong answer\n Please re-enter:");
				count++;
				scanf("%f", &s);
			}
			printf("\n Congratulations, the answer is correct!\n");
		}
		else if (i % 3 == 0)
		{
			printf("\n No. %d Title:%.1f * %.1f / %.1f - %.1f = ?\n", i + 1, a, b, d, c);
			ret = a * b / d - c;
			temp = (int)(ret * 100 + 0.5);
			temp = temp / 100;
			printf("%.2f\n", temp);
			printf("Enter the answer (keep two decimal places):");
			float s;
			scanf("%f", &s);
			while (s != temp)
			{
				printf("\n Wrong answer\n Please re-enter:");
				count++;
				scanf("%f", &s);
			}
			printf("\n Congratulations, the answer is correct!\n");
		}
		else if (i % 2 == 0)
		{
			printf("\n No. %d Title:%.1f * %.1f + %.1f / %.1f = ?\n", i + 1, a, b, c, d);
			ret = a * b + c / d;
			temp = (int)(ret * 100 + 0.5);
			temp = temp / 100;
			printf("%.2f\n", temp);
			printf("Enter the answer (keep two decimal places):");
			float s;
			scanf("%f", &s);
			while (s != temp)
			{
				printf("\n Wrong answer\n Please re-enter:");
				count++;
				scanf("%f", &s);
			}
			printf("\n Congratulations, the answer is correct!\n");
		}
		else
		{
			printf("\n No. %d Title:%.1f + %.1f - %.1f * %.1f = ?\n", i + 1, a, b, c, d);
			ret = a + b - c * d;
			printf("%.2f\n", ret);
			printf("Enter the answer (keep two decimal places):");
			float s;
			scanf("%f", &s);
			while (s != ret)
			{
				printf("\n Wrong answer\n Please re-enter:");
				count++;
				scanf("%f", &s);
			}
			printf("\n Congratulations, the answer is correct!\n");
		}
	}
	finish = clock();
	total = (int)(finish - start) / CLOCKS_PER_SEC;	//Convert units to seconds
	min = (int)total / 60;
	sec = (int)total % 60;
	ms = (int)(finish - start) - 60000 * min - 1000 * sec;
	printf("\n Number of wrong answers:%d", count);
	printf("\n This challenge %d Topics, when shared %d branch %d second %d Millisecond\n\n", n, min, sec, ms);
}

5. Complete Functions

5.1 calculate.h header file

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void calculator();		//Calculator calculation

void exercise();	//Calculating exercises

5.2 calculator.c Source File

#include "calculate.h"

//Data Stack
struct shujv
{
	int data[100];
	int top;
};

//Symbol stack
struct fuhao
{
	char symbol[100];
	int top;
};

void InitNum(struct shujv* Num)
{
	Num->top = -1;
}

void InitSymbol(struct fuhao* Symbol)
{
	Symbol->top = -1;
}

//Save on Data Stack
void Inshujv(struct shujv* Num, int n)
{
	Num->top++;
	Num->data[Num->top] = n;
}

//Save on Symbol Stack
void Infuhao(struct fuhao* Symbol, char ch)
{
	Symbol->top++;
	Symbol->symbol[Symbol->top] = ch;
}

//Read stack top symbols
char Readfuhao(struct fuhao* Symbol)
{
	return Symbol->symbol[Symbol->top];
}

//output data
int Putshujv(struct shujv* Num)
{
	int x;
	x = Num->data[Num->top];
	Num->top--;
	return x;
}

//Output Symbol
char Putfuhao(struct fuhao* Symbol)
{
	char ch;
	ch = Symbol->symbol[Symbol->top];
	Symbol->top--;
	return ch;
}

//Four Operations
int Math(int a, int b, char ch)
{
	if (ch == '+')
		return a + b;
	if (ch == '-')
		return a - b;
	if (ch == '*')
		return a * b;
	if (ch == '/')
		return a / b;
}

void calculator()
{
	struct shujv data;
	struct fuhao symbol;	//Create Data Stack, Symbol Stack
	InitNum(&data);
	InitSymbol(&symbol);	//Initialize Data Stack, Symbol Stack
	int i, j;
	i = j = 0;
	int v1, v2, ret;	//Variables for calculation
	char c;
	char v[100];
	printf("                Calculator\n");
	printf("************************************\n");
	printf("Please enter an equation:  ");
	char* str = (char*)malloc(sizeof(char) * 200);	//Open up space to store input formulas
	getchar();	//Absorb'\n'from last selection
	while ((c = getchar()) != '\n')
	{
		str[i] = c;
		i++;
	}
	str[i] = '\0';
	for (i = 0; str[i] != '\0'; i++)
	{
		if (i == 0 && str[i] == '-')
		{
			v[j++] = str[i];
		}
		else if (str[i] == '(' && str[i + 1] == '-')
		{
			i++;
			v[j++] = str[i++];
			while (str[i] >= '0' && str[i] <= '9')
			{
				v[j] = str[i];
				j++;
				i++;
			}
			Inshujv(&data, atoi(v));	//Stores data on the data stack, and atoi() converts strings to integers of type int
			while (j > 0)
			{
				v[j] = 0;
				j--;
			}
			if (str[i] != ')')
			{
				i--;
				Infuhao(&symbol, '(');
			}
            else
            {
                Putfuhao(&symbol);    //If str[i] is')', then this bracket only serves as a negative quote
            }               //Store negative numbers and use Putfuhao() to make symbols. Top--, remove the brackets
		}
		else if (str[i] >= '0' && str[i] <= '9')
		{
			while (str[i] >= '0' && str[i] <= '9')
			{
				v[j] = str[i];
				j++;
				i++;
			}
			Inshujv(&data, atoi(v));
			while (j > 0)
			{
				v[j] = 0;
				j--;
			}
			i--;
		}
		else
		{
			if (symbol.top == -1)	//If the symbol stack has no elements, save the symbol directly
			{
				Infuhao(&symbol, str[i]);
			}
			else if (str[i] == '(')		//If it's the highest priority'('it's also stored directly in the symbol stack
			{
				Infuhao(&symbol, str[i]);
			}
			else if (str[i] == '+' || str[i] == '-')	//At this point, the priority is determined by the top symbol of the stack
			{
				if (Readfuhao(&symbol) == '(')	//If the top of the stack is'(', save directly
				{
					Infuhao(&symbol, str[i]);
				}
				else if (Readfuhao(&symbol) == '+' || Readfuhao(&symbol) == '-')	//If the top of the stack makes a stack operation for addition and subtraction
				{
					while (symbol.top >= 0 && data.top >= 1)
					{
						v2 = Putshujv(&data);
						v1 = Putshujv(&data);
						ret = Math(v1, v2, Putfuhao(&symbol));
						Inshujv(&data, ret);
					}
					Infuhao(&symbol, str[i]);
				}
				else if (Readfuhao(&symbol) == '*' || Readfuhao(&symbol) == '/')
				{
					while (symbol.top >= 0 && data.top >= 1)
					{
						v2 = Putshujv(&data);
						v1 = Putshujv(&data);
						ret = Math(v1, v2, Putfuhao(&symbol));
						Inshujv(&data, ret);
					}
					Infuhao(&symbol, str[i]);
				}
			}
			else if (str[i] == '*' || str[i] == '/')
			{
				if (Readfuhao(&symbol) == '(')
				{
					Infuhao(&symbol, str[i]);
				}
				else if (Readfuhao(&symbol) == '+' || Readfuhao(&symbol) == '-')
				{
					Infuhao(&symbol, str[i]);
				}
				else if (Readfuhao(&symbol) == '*' || Readfuhao(&symbol) == '/')
				{
					while (symbol.top >= 0 && data.top >= 1 && symbol.symbol[symbol.top] != '(' &&
						symbol.symbol[symbol.top] != '+' && symbol.symbol[symbol.top] != '-')
					{
						v2 = Putshujv(&data);
						v1 = Putshujv(&data);
						ret = Math(v1, v2, Putfuhao(&symbol));
						Inshujv(&data, ret);
					}
					Infuhao(&symbol, str[i]);
				}
			}
			else if (str[i] == ')')		//If')', make a station operation until you encounter'('
			{
				do
				{
						v2 = Putshujv(&data);
						v1 = Putshujv(&data);
						ret = Math(v1, v2, Putfuhao(&symbol));
						Inshujv(&data, ret);
				} while (symbol.symbol[symbol.top] != '(');
				Putfuhao(&symbol);	//Dispose of'('
			}
		}
	}
	while (symbol.top >= 0 && data.top >= 1)
	{
		v2 = Putshujv(&data);
		v1 = Putshujv(&data);
		ret = Math(v1, v2, Putfuhao(&symbol));
		Inshujv(&data, ret);
	}
	system("cls");
	printf("                Calculator\n");
	printf("************************************\n");
	printf("%s = %d\n\n",str, data.data[0]);
	free(str);
}

5.3 exercise.c Source File

#include "calculate.h"

void e_menu()
{
	printf("*****************************\n");
	printf("******** 1.Initial Practice   *******\n");
	printf("******** 2.Intermediate Practice   *******\n");
	printf("******** 3.Advanced Practice   *******\n");
	printf("******** 0.Exit Practice   *******\n");
	printf("*****************************\n");
}

void primary()
{
	clock_t start, finish, total;	//Record answer times
	int min, sec, ms;
	int i, n;
	int count = 0;		//Record number of errors
	printf("          Initial Practice\n");
	printf("---------------------------------\n");
	printf("Enter the number of topics you want to challenge:");
	scanf("%d", &n);
	start = clock();
	for (i = 0; i < n; i++)
	{
		int a = rand() % 100;
		int b = rand() % 100;
		int c = rand() % 100;
		if (i % 2 == 0)
		{
			printf("\n No. %d Title:%d + %d - %d = ?\n", i + 1, a, b, c);
			printf("Please enter an answer:");
			int s;
			scanf("%d", &s);
			while (s != (a + b - c))
			{
				printf("\n Wrong answer\n Please re-enter:");
				count++;
				scanf("%d", &s);
			}
			printf("\n Congratulations, the answer is correct!\n");
		}
		else
		{
			printf("\n No. %d Title:%d - %d + %d = ?\n",i + 1, a, b, c);
			printf("Please enter an answer:");
			int s;
			scanf("%d", &s);
			while (s != (a - b + c))
			{
				printf("\n Wrong answer\n Please re-enter:");
				count++;
				scanf("%d", &s);
			}
			printf("\n Congratulations, the answer is correct!\n");
		}
	}
	finish = clock();
	total = (double)(finish - start) / CLOCKS_PER_SEC;	//Convert units to seconds
	min = (int)total / 60;
	sec = (int)total % 60;
	ms = (int)(finish - start) - 60000 * min - 1000 * sec;
	printf("\n Number of wrong answers:%d", count);
	printf("\n This challenge %d Topics, when shared %d branch %d second %d Millisecond\n\n", n, min, sec, ms);
}

void medium()
{
	clock_t start, finish, total;
	int min, sec, ms;
	int i, n;
	int count = 0;
	printf("          Intermediate Practice\n");
	printf("---------------------------------\n");
	printf("Enter the number of topics you want to challenge:");
	scanf("%d", &n);
	start = clock();
	for (i = 0; i < n; i++)
	{
		float a = rand() % 100;
		float b = rand() % 100;
		float c = rand() % 10 + 1;
		float ret = 0;
		float temp = 0;
		if (i % 2 == 0)
		{
			printf("\n No. %d Title:%.1f * %.1f / %.1f = ?\n", i + 1, a, b, c);
			ret = a * b / c;
			temp = (int)(ret * 100 + 0.5);
			temp = temp / 100;
			printf("Enter the answer (keep two decimal places):");
			float s;
			scanf("%f", &s);
			while (s != temp)
			{
				printf("\n Wrong answer\n Please re-enter:");
				count++;
				scanf("%f", &s);
			}
			printf("\n Congratulations, the answer is correct!\n");
		}
		else
		{
			printf("\n No. %d Title:%.1f / %.1f * %.1f = ?\n", i + 1, a, c, b);
			ret = a / c * b;
			temp = (int)(ret * 100 + 0.5);
			temp = temp / 100;
			printf("Enter the answer (keep two decimal places):");
			float s;
			scanf("%f", &s);
			while (s != temp)
			{
				printf("\n Wrong answer\n Please re-enter:");
				count++;
				scanf("%f", &s);
			}
			printf("\n Congratulations, the answer is correct!\n");
		}
	}
	finish = clock();
	total = (int)(finish - start) / CLOCKS_PER_SEC;	//Convert units to seconds
	min = (int)total / 60;
	sec = (int)total % 60;
	ms = (int)(finish - start) - 60000 * min - 1000 * sec;
	printf("\n Number of wrong answers:%d", count);
	printf("\n This challenge %d Topics, when shared %d branch %d second %d Millisecond\n\n", n, min, sec, ms);
	count = 0;
}

void high()
{
	clock_t start, finish, total;
	int min, sec, ms;
	int i, n;
	int count = 0;
	printf("          Advanced Practice\n");
	printf("---------------------------------\n");
	printf("Enter the number of topics you want to challenge:");
	scanf("%d", &n);
	start = clock();
	for (i = 0; i < n; i++)
	{
		float a = rand() % 100;
		float b = rand() % 100;
		float c = rand() % 100;
		float d = rand() % 10 + 1;
		float ret = 0;
		float temp = 0;
		if (i % 5 == 0)
		{
			printf("\n No. %d Title:%.1f + %.1f * %.1f / %.1f = ?\n", i + 1, a, b, c, d);
			ret = a + b * c / d;
			temp = (int)(ret * 100 + 0.5);
			temp = temp / 100;
			printf("%.2f\n", temp);
			printf("Enter the answer (keep two decimal places):");
			float s;
			scanf("%f", &s);
			while (s != temp)
			{
				printf("\n Wrong answer\n Please re-enter:");
				count++;
				scanf("%f", &s);
			}
			printf("\n Congratulations, the answer is correct!\n");
		}
		else if (i % 3 == 0)
		{
			printf("\n No. %d Title:%.1f * %.1f / %.1f - %.1f = ?\n", i + 1, a, b, d, c);
			ret = a * b / d - c;
			temp = (int)(ret * 100 + 0.5);
			temp = temp / 100;
			printf("%.2f\n", temp);
			printf("Enter the answer (keep two decimal places):");
			float s;
			scanf("%f", &s);
			while (s != temp)
			{
				printf("\n Wrong answer\n Please re-enter:");
				count++;
				scanf("%f", &s);
			}
			printf("\n Congratulations, the answer is correct!\n");
		}
		else if (i % 2 == 0)
		{
			printf("\n No. %d Title:%.1f * %.1f + %.1f / %.1f = ?\n", i + 1, a, b, c, d);
			ret = a * b + c / d;
			temp = (int)(ret * 100 + 0.5);
			temp = temp / 100;
			printf("%.2f\n", temp);
			printf("Enter the answer (keep two decimal places):");
			float s;
			scanf("%f", &s);
			while (s != temp)
			{
				printf("\n Wrong answer\n Please re-enter:");
				count++;
				scanf("%f", &s);
			}
			printf("\n Congratulations, the answer is correct!\n");
		}
		else
		{
			printf("\n No. %d Title:%.1f + %.1f - %.1f * %.1f = ?\n", i + 1, a, b, c, d);
			ret = a + b - c * d;
			printf("%.2f\n", ret);
			printf("Enter the answer (keep two decimal places):");
			float s;
			scanf("%f", &s);
			while (s != ret)
			{
				printf("\n Wrong answer\n Please re-enter:");
				count++;
				scanf("%f", &s);
			}
			printf("\n Congratulations, the answer is correct!\n");
		}
	}
	finish = clock();
	total = (int)(finish - start) / CLOCKS_PER_SEC;	//Convert units to seconds
	min = (int)total / 60;
	sec = (int)total % 60;
	ms = (int)(finish - start) - 60000 * min - 1000 * sec;
	printf("\n Number of wrong answers:%d", count);
	printf("\n This challenge %d Topics, when shared %d branch %d second %d Millisecond\n\n", n, min, sec, ms);
	count = 0;
}

void exercise()
{
	srand((unsigned int)time(NULL));
	int n;
	do
	{
		e_menu();
		printf("Please select:>");
		scanf("%d", &n);
		system("cls");
		switch (n)
		{
		case 1:
			primary();	//Initial Practice, Addition and Subtraction
			break;
		case 2:
			medium();	//Intermediate Practice, Multiplication and Division
			break;
		case 3:
			high();		//Advanced Exercises, Comprehensive Training
			break;
		case 0:
			printf("Exit Practice\n");
			break;
		default:
			printf("Input error, please re-enter\n");
			break;
		}
	} while (n);
}

5.4 main.c Source File

#include "calculate.h"

void menu()
{
	printf("************************************\n");
	printf("***********1.Calculator   **************\n");
	printf("***********2.Arithmetic exercises **************\n");
	printf("***********0.Sign out     **************\n");
	printf("************************************\n");
	printf("Please select:>");
}

int main()
{
	int input;
	do
	{
		menu();
		scanf("%d", &input);
		system("cls");	//Clear screen, improve the look and feel
		switch (input)
		{
		case 1:
			calculator();	//Calculator function
			break;
		case 2:
			exercise();		//Calculating exercise functions
			break;
		case 0:
			printf("Exit the program\n");
			break;
		default:
			printf("Input error, please re-enter\n");
			break;
		}
	} while (input);
	return 0;
}

Keywords: C

Added by jonabomer on Thu, 03 Feb 2022 19:56:21 +0200