C implements a simple calculator

Mondo Technology Updated on 2024-02-03

According to the operands and operators entered by the user, the corresponding result is calculated, and the switch statement can be used to complete the work. For example, define crt secure no warnings 1

#include

int jisuanqi(int num_a, char type, int num_b);

int addtest(int num_a, int num_b);

int reducetest(int num_a, int num_b);

int multiplytest(int num_a, int num_b);

int dividetest(int num_a, int num_b);

int remaindertest(int num_a, int num_b);

int main()

printf("Please enter an expression (example: 3*2):");

int num_a, num_b;

char type;

scanf("%d%c%d", &num_a, &type, &num_b);

int rs = jisuanqi(num_a, type, num_b);

printf("%d", rs);

return 0;

int jisuanqi(int num_a, char type, int num_b) {

int result;

switch (type) {

case '+':

result = addtest(num_a, num_b);

break;

case '-':

result = reducetest(num_a, num_b);

break;

case '*':

result = multiplytest(num_a, num_b);

break;

case '/':

result = dividetest(num_a, num_b);

break;

case '%':

result = remaindertest(num_a, num_b);

break;

default:

printf("Operator error! ");

result = -1;

break;

return result;

Addition. int addtest(int num_a, int num_b) {

int result = num_a + num_b;

return result;

Subtraction. int reducetest(int num_a, int num_b) {

int result = num_a - num_b;

return result;

Multiplication. int multiplytest(int num_a, int num_b) {

int result = num_a * num_b;

return result;

Division. int dividetest(int num_a, int num_b) {

int result = num_a / num_b;

return result;

Division. int dividetest(int num_a, int num_b) {

int result = num_a / num_b;

return result;

Remainder. int remaindertest(int num_a, int num_b) {

int result = num_a % num_b;

return result;

The test addition is as follows

The test multiplication is as follows

The test subtraction is as follows

Related Pages