一、项目介绍

C语言版简易计算器,包含几个小功能,包括基本运算、进制转换、一元二次方程,支持返回操作,整体源码比较精简,代码逻辑简单,扩展性强,非常适合新手了解和学习的小项目。


二、运行截图

导航菜单,支持六种计算(可扩展)

计算器-菜单

根据输入选择,可重复使用

计算器-运算

        阶乘计算

计算器-进制转换

三、代码思路

代码整体依靠一个swtich结构根据用户的输入做分支,分别调用对应的计算函数,由于代码比较短,将每个计算的代码分别写到了每个case里,代码逻辑简单,核心计算都是套数学公式即可,没有什么难度,大家可以直接读代码,并有参考注释。


建议:大家可以根据情况予以功能增加,然后将独立的代码封装到函数里,体会函数模块化思想,让代码更简洁。


四、完整源码

#include <stdio.h> 
#include <math.h> 
#include <stdlib.h> 
//预处理指令
int main(void)
{
        double bNumber, Number, Result;                //给加减乘除定义的变量
        int No;                //选项的定义变量
        double a, b, c, x1, x2, Rad;                //给一元一次方程定义的变量
        int Ary_10;                                        //定义进制的变量
        char string[32];                //二进制变量定义
        system ("title: www.dotcpp.com");
        while(1)
        {
                //界面
                printf ("┏ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┓\n");
                printf ("┇请选择你要计算的方法:                         ┇\n");
                printf ("┣ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┫\n");
                printf ("┇  加法请按_1    进制转换_5                    ┇\n");
                printf ("┇  减法请按_2    求一元二次方程_6              ┇\n");
                printf ("┇  乘法请按_3                                  ┇\n");
                printf ("┇  除法请按_4               退出_0             ┇\n");
                printf ("┗ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┛\n");
                printf ("Please write down the number:");
                scanf ("%d",&No);
                if (No == 1)
                {
                        //        加法
                        printf ("        请输入被加数:");
                        scanf ("%lf",&bNumber);
                        printf ("        请输入加数:");
                        scanf ("%lf",&Number);
                        Result = bNumber + Number;
                        printf (" 结果是: %lf\n\n",Result);
                }
                else if (No == 2)
                {
                        //        减法
                        printf ("        请输入被减数:");
                        scanf ("%lf",&bNumber);
                        printf ("        请输入减数:");
                        scanf ("%lf",&Number);
                        Result = bNumber - Number;
                        printf (" 结果是: %lf\n\n",Result);
                }
                else if (No == 3)
                {
                        //        乘法
                        printf ("        请输入被乘数:");
                        scanf ("%lf",&bNumber);
                        printf ("        请输入乘数:");
                        scanf ("%lf",&Number);
                        Result = bNumber * Number;
                        printf (" 结果是: %lf\n\n",Result);
                }
                else if (No == 4)
                {
                        //        除法
                        printf ("        请输入被除数:");
                        scanf ("%lf",&bNumber);
                        printf ("        请输入除数:");
                        scanf ("%lf",&Number);
                        Result = bNumber / Number;
                        printf (" 结果是: %lf\n\n",Result);
                }
                else if (No == 5)
                {
                        //进制转换的代码
                        printf("请输入需要转换的十进制数:");
                        scanf("%d", &Ary_10);
                        itoa (Ary_10, string ,2);
                        printf("二进制: %s\n", string);
                        printf("八进制: %o\n", Ary_10);
                        printf("十六进制: %x\n", Ary_10);
                }
                else if (No == 6)
                {
                        //求一元二次方程的解的代码
                        printf("请输入一元一次方程的a,b,c三个数:");
                        scanf("%lf%lf%lf",&a,&b,&c);
                        Rad = b*b - 4*a*c;
                        if (Rad > 0)
                        {
                                x1 = -b + sqrt(Rad) / (2*a);
                                x2 = -b - sqrt(Rad) / (2*a);
                                printf("有两个解 x1 = %lf, x2 = %lf\n", &x1, &x2);
                        }
                        else if (Rad == 0)
                        {
                                x1 = -b / (2*a);
                                printf("只有一个解 x1 = %lf\n", &x1);
                        }
                        else
                        {
                                printf("无解\n");
                        }
                }
                else if (No == 0)
                {
                        //        退出程序
                        break;
                }
                else
                {
                        //        输入的选项不对
                        printf("  请输入正确的数字。\n\n");
                }
                system ("pause");        //按任意键继续
                system ("cls");                //清屏
        }
        return 0;
        getchar();
}


点赞(0)

C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:

一点编程也不会写的:零基础C语言学练课程

解决困扰你多年的C语言疑难杂症特性的C语言进阶课程

从零到写出一个爬虫的Python编程课程

只会语法写不出代码?手把手带你写100个编程真题的编程百练课程

信息学奥赛或C++选手的 必学C++课程

蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程

手把手讲解近五年真题的蓝桥杯辅导课程

Dotcpp在线编译      (登录可减少运行等待时间)