C language to realize the calculator (visual interface and multi-function)

I am Chenpi, an ITer in Internet Coding. Search for " JavaLib of Chenpi " on WeChat to read the latest article as soon as possible, and reply to [ Information ] to get my carefully organized technical information, e-books, first-line interview information and excellent. Resume template.

Article Directory


introduction


In college, for courses such as C language or data structure, the teacher will ask students to write a small project to practice hands, or write a small software and other curriculum design at the end of the semester. Today, when I saw the freshman learning C language on the computer, the 计算器functions written in the experimental class were quite interesting. So share it with the younger students who are learning C language, remember to save it!


Calculator


Simple version of the calculator, the main achievement of the two numbers is simple 加法, 减法, 乘法, 除法, 求余functions. The user can select the function to be calculated in the main menu, and then calculate and output the result according to the number entered by the user.

First, we define five methods to achieve two numbers 加法, 减法, 乘法, 除法, 求余functions. code show as below:

// 加法
float add(float a, float b) {
    return a + b;
}

// 减法
float sub(float a, float b) {
    return a - b;
}

// 乘法
float mul(float a, float b) {
    return a * b;
}

// 除法
float mod(float a, float b) {
    return a / b;
}

// 求余
int com(int a, int b) {
    return a % b;
}

Then define a function to print the main menu interface, and use the brief and beautiful menu interface for the user to select the function to be calculated. The code is as follows:

// 打印主功能菜单
void printMenu() {
    // 清屏
    // system("cls");

    printf("|----------------------- 计算器 -----------------------|\n");
    printf("|                                                     |\n");
    printf("|                                 By -- 陈皮的JavaLib  |\n");
    printf("|                                                     |\n");
    printf("|---------------------- 1:加法 -----------------------|\n");
    printf("|---------------------- 2:减法 -----------------------|\n");
    printf("|---------------------- 3:乘法 -----------------------|\n");
    printf("|---------------------- 4:除法 -----------------------|\n");
    printf("|---------------------- 5:求余 -----------------------|\n");
    printf("|---------------------- 6:退出 -----------------------|\n");
    printf("|                                                     |\n");
    printf("|----------------------- 计算器 -----------------------|\n");
}

Finally, in the main function main, we define a while loop code block to continuously read the content input by the user and perform numerical calculations. The code is as follows:

int main() {

    // 选择的功能
    int select = 0;

    // 定义算术的两个数
    float a, b;

    while (select != 6){

        select = 0;

        // 打印主菜单
        printMenu();

        // 只允许选择菜单栏内的数字,不是则重新选择
        while (select < 1 || select > 6) {
            printf("请输入菜单功能(1-6):");
            scanf("%d", &select);
        }

        if (6 == select) {
            printf("\n|----------------- 欢迎使用计算器!再见!-----------------|\n");
            return 0;
        }

        printf("\n请输入两个数(用空格隔开两个数):");
        scanf("%f %f", &a, &b);

        switch (select) {
            case 1:
                printf("%f+%f=%f\n\n", a, b, add(a, b));
                break;
            case 2:
                printf("%f-%f=%f\n\n", a, b, sub(a, b));
                break;
            case 3:
                printf("%f*%f=%f\n\n", a, b, mul(a, b));
                break;
            case 4:
                printf("%f/%f=%f\n\n", a, b, mod(a, b));
                break;
            case 5:
                printf("%f/%f=%d\n\n", a, b, com((int)a, (int)b));
                break;
            case 6:
            default:
                break;
        }
    }
}

Below we demonstrate a few examples to experience the functions of the calculator more intuitively:

  • Menu function selection error, you will be prompted to reselect
Insert picture description here
  • addition
Insert picture description here
  • division
Insert picture description here
  • drop out
Insert picture description here

The simple version of the calculator, the complete code is as follows:

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

// 加法
float add(float a, float b) {
    return a + b;
}

// 减法
float sub(float a, float b) {
    return a - b;
}

// 乘法
float mul(float a, float b) {
    return a * b;
}

// 除法
float mod(float a, float b) {
    return a / b;
}

// 求余
int com(int a, int b) {
    return a % b;
}

// 打印主功能菜单
void printMenu() {
    // 清屏
    // system("cls");

    printf("|----------------------- 计算器 -----------------------|\n");
    printf("|                                                     |\n");
    printf("|                                 By -- 陈皮的JavaLib  |\n");
    printf("|                                                     |\n");
    printf("|---------------------- 1:加法 -----------------------|\n");
    printf("|---------------------- 2:减法 -----------------------|\n");
    printf("|---------------------- 3:乘法 -----------------------|\n");
    printf("|---------------------- 4:除法 -----------------------|\n");
    printf("|---------------------- 5:求余 -----------------------|\n");
    printf("|---------------------- 6:退出 -----------------------|\n");
    printf("|                                                     |\n");
    printf("|----------------------- 计算器 -----------------------|\n");
}

/**
 * 简单版本计算器功能 By -- 陈皮的JavaLib
 * @return 
 */
int main() {

    // 选择的功能
    int select = 0;

    // 定义算术的两个数
    float a, b;

    while (select != 6){

        select = 0;

        // 打印主菜单
        printMenu();

        // 只允许选择菜单栏内的数字,不是则重新选择
        while (select < 1 || select > 6) {
            printf("请输入菜单功能(1-6):");
            scanf("%d", &select);
        }

        if (6 == select) {
            printf("\n|----------------- 欢迎使用计算器!再见!-----------------|\n");
            return 0;
        }

        printf("\n请输入两个数(用空格隔开两个数):");
        scanf("%f %f", &a, &b);

        switch (select) {
            case 1:
                printf("%f+%f=%f\n\n", a, b, add(a, b));
                break;
            case 2:
                printf("%f-%f=%f\n\n", a, b, sub(a, b));
                break;
            case 3:
                printf("%f*%f=%f\n\n", a, b, mul(a, b));
                break;
            case 4:
                printf("%f/%f=%f\n\n", a, b, mod(a, b));
                break;
            case 5:
                printf("%f/%f=%d\n\n", a, b, com((int)a, (int)b));
                break;
            case 6:
            default:
                break;
        }
    }
}

to sum up


During the university, if you are a computer major, you will generally learn the C language. As an introductory programming language, you must learn this language well, and if you do well, in fact, learning other programming languages ​​is very easy to use. In fact, different languages With similarities, as long as you have improved your programming thinking and mastered the basic grammar, you can learn more in-depth knowledge. One of the paths is to type more codes, from basic codes to classic question types, and constantly exercise programming thinking and code typing ability.

The above is a simple version of the demo calculator, to achieve the 加法, 减法, 乘法, 除法, 求余to the functions, the user can select the function to be calculated in the main menu, and then the digital input by a user, calculates and outputs the result. Interested students can achieve more advanced gameplay, such as supporting brackets, polynomial addition, subtraction, multiplication and division, etc.

We recommend CSDN独家出品the Java工程师学习成长知识图谱covers comprehensive knowledge point, you can start with interest, recommend, learn to master especially if college students can turn knowledge, Zhanxia BAT manufacturers Offer is not a dream! 😉

Insert picture description here