C語(yǔ)言版簡(jiǎn)易計(jì)算器,包含幾個(gè)小功能,而且比較精簡(jiǎn),適合新手了解整合框架結(jié)構(gòu)
源代碼如下:
#include <stdio.h> #include <math.h> #include <stdlib.h> //預(yù)處理指令 int main(void) { double bNumber, Number, Result; //給加減乘除定義的變量 int No; //選項(xiàng)的定義變量 double a, b, c, x1, x2, Rad; //給一元一次方程定義的變量 int Ary_10; //定義進(jìn)制的變量 char string[32]; //二進(jìn)制變量定義 system ("title: www.sztianhecheng.cn"); while(1) { //界面 printf ("┏ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┓\n"); printf ("┇請(qǐng)選擇你要計(jì)算的方法: ┇\n"); printf ("┣ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┫\n"); printf ("┇ 加法請(qǐng)按_1 進(jìn)制轉(zhuǎn)換_5 ┇\n"); printf ("┇ 減法請(qǐng)按_2 求一元二次方程_6 ┇\n"); printf ("┇ 乘法請(qǐng)按_3 ┇\n"); printf ("┇ 除法請(qǐng)按_4 退出_0 ┇\n"); printf ("┗ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┅ ┛\n"); printf ("Please write down the number:"); scanf ("%d",&No); if (No == 1) { // 加法 printf (" 請(qǐng)輸入被加數(shù):"); scanf ("%lf",&bNumber); printf (" 請(qǐng)輸入加數(shù):"); scanf ("%lf",&Number); Result = bNumber + Number; printf (" 結(jié)果是: %lf\n\n",Result); } else if (No == 2) { // 減法 printf (" 請(qǐng)輸入被減數(shù):"); scanf ("%lf",&bNumber); printf (" 請(qǐng)輸入減數(shù):"); scanf ("%lf",&Number); Result = bNumber - Number; printf (" 結(jié)果是: %lf\n\n",Result); } else if (No == 3) { // 乘法 printf (" 請(qǐng)輸入被乘數(shù):"); scanf ("%lf",&bNumber); printf (" 請(qǐng)輸入乘數(shù):"); scanf ("%lf",&Number); Result = bNumber * Number; printf (" 結(jié)果是: %lf\n\n",Result); } else if (No == 4) { // 除法 printf (" 請(qǐng)輸入被除數(shù):"); scanf ("%lf",&bNumber); printf (" 請(qǐng)輸入除數(shù):"); scanf ("%lf",&Number); Result = bNumber / Number; printf (" 結(jié)果是: %lf\n\n",Result); } else if (No == 5) { //進(jìn)制轉(zhuǎn)換的代碼 printf("請(qǐng)輸入需要轉(zhuǎn)換的十進(jìn)制數(shù):"); scanf("%d", &Ary_10); itoa (Ary_10, string ,2); printf("二進(jìn)制: %s\n", &string); printf("八進(jìn)制: %o\n", &Ary_10); printf("十六進(jìn)制: %x\n", &Ary_10); } else if (No == 6) { //求一元二次方程的解的代碼 printf("請(qǐng)輸入一元一次方程的a,b,c三個(gè)數(shù):"); 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("有兩個(gè)解 x1 = %lf, x2 = %lf\n", &x1, &x2); } else if (Rad == 0) { x1 = -b / (2*a); printf("只有一個(gè)解 x1 = %lf\n", &x1); } else { printf("無解\n"); } } else if (No == 0) { // 退出程序 break; } else { // 輸入的選項(xiàng)不對(duì) printf(" 請(qǐng)輸入正確的數(shù)字。\n\n"); } system ("pause"); //按任意鍵繼續(xù) system ("cls"); //清屏 } return 0; getchar(); }
C語(yǔ)言研究中心(www.sztianhecheng.cn)