1.计算器设计过于繁琐,主要用于编写代码。当然,在设计函数时,主函数main还是冗杂而不简洁;
2.读取计算器代码
首先在主函数中包装封面;输入用户,用户输入值进入第三个while循环;如果输入的值是非1、2、3、4、5、6的数字就将进入循环就会打印“输入的数字有误请重新打印”然后循环被goto破坏转到end1.继续执行代码;如果输入1、2、3、4、5、6,将进入第四个while循环进行if判断句再次,我们重新定义了几个函数,即将独立于功能区if在判断中调用实现功能的实现;
3.这本书还有一些使用技巧
(1)system("title 在这里输入执行页的名称");执行页面的名称可以修改;
(2)system("color 两个数字和字母");第一个数字是背景色,第二个数字前景色是背景色;
(3)system("mode con cols=数字 lines=数字");左上为坐标原点lines为行横为列cols;
(4)system("cls");意思是清除屏幕;
#include<stdio.h> #include<string.h> #include<stdlib.h> int Sub(); int Mul(); int Div(); int Add(); int Mod(); int sum; int c; int main() { /// ///左上角坐标原点数列lines为行横为列cols; system("title 简易计算器 wpq"); system("mode con cols=30 lines=20"); system("color 09");//第一个数字是背景色,即背景色的第二个数字 /// / int select =1; start: while (1) { printf("******************************\n"); printf("**** 简易计算器 ****\n"); printf("**** [1]Add [2]Sub ****\n"); printf("**** [3]Mul [4]Div ****\n"); printf("**** [5]Mod [6]Quit ****\n"); printf("******************************\n"); while (1) { printf("请输入您的选择\n"); end1: scanf_s("%d", &select); while (select != 1 && select != 1 && select != 2 && select != 3 && select != 4 && select != 5 && select != 6) { printf("选择有误,请输入正确的数字\n"); goto end1; } while (1) { if (select == 6) { goto end; } printf("请输入两个数字 op1 和 op2 :>\n"); if (select == 1) { c=Add(); break; } else if (select == 2) { c=Sub(); break; } else if (select == 3) { c=Mul(); break; } else if (select == 4) { Div(); c = sum; break; } else if (select == 5) { c=Mod(); break; } } printf("sum=%d\n", c); printf("是否继续计算\n"); printf("输入1继续任意键退出\n"); int n; scanf_s("%d", &n); if (1) { system("cls");//清除屏幕 goto start; } else { goto end; } } } end: printf("感谢使用\n"); system("pause"); return 0; } int a; int b; int Add() { scanf_s("%d %d", &a, &b); return sum = a b; } int Sub() { scanf_s("%d %d", &a, &b); return sum = a - b; } int Mul() { scanf_s("%d %d", &a, &b); return sum = a * b; } int Div() { float sum; scanf_s("%d %d", &a, &b); return sum = a/b; } int Mod() { scanf_s("%d %d", &a, &b); return sum = a % b; }