小項(xiàng)目1 ---C 語言做一個(gè)簡單的計(jì)算器
1,項(xiàng)目說明:
實(shí)現(xiàn)一個(gè)簡易的僅支持兩個(gè)操作數(shù)運(yùn)算的計(jì)算器,不涉及詞法分析與語法樹蠢莺,內(nèi)容很簡單,適合 C 語言入門(剛掌握語法程度)的進(jìn)行練手。
2,項(xiàng)目介紹
能執(zhí)行加芯侥、減、乘乳讥、除操作柱查。本程序涉及的所有數(shù)學(xué)知識(shí)都很簡單,但輸入過程會(huì)增加復(fù)雜性云石。因?yàn)槲覀冃枰獧z查輸入唉工,確保用戶沒有要求計(jì)算機(jī)完成不可能的任務(wù)。還必須允許用戶一次輸入一個(gè)計(jì)算式汹忠,例如: 32.4 + 32 或者 9 * 3.2
2.1,項(xiàng)目流程:
1.獲取用戶輸入的計(jì)算表達(dá)式淋硝。
2.檢查輸入的表達(dá)式格式,確保表達(dá)式對(duì)應(yīng)的實(shí)際操作可以執(zhí)行宽菜。
3.執(zhí)行計(jì)算谣膳。
4.返回計(jì)算結(jié)果并在終端打印。
2.2,項(xiàng)目效果圖:
![S6Q]OY{HLV``7YHC41(CZMK.jpg](http://upload-images.jianshu.io/upload_images/2619158-238b2ea80960b82f.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
3,項(xiàng)目實(shí)現(xiàn)
3.1獲取輸入
double number1=0.0; // 定義第一個(gè)操作數(shù)
double number2=0.0; // 定義第二個(gè)操作數(shù)
char operation=0; // operation 必須是 '+' '-' '*' '/' 或 '%'
printf("\nEnter the calculation\n");
scanf("%lf%c%lf",&number1,&operation,&number2);```
##### ????3.2輸入檢查
當(dāng)輸入的操作為 / 或者 % 時(shí)铅乡,第二個(gè)操作數(shù)不能為 0 继谚。如果為 0 則操作無效。
##### ????3.3循環(huán)輸入,用戶選擇y/n是夠繼續(xù)
for(;;){
switch(){
……
}
char answer = getchar();//從鍵盤中輸入一個(gè)字符
if(answer == 'y' || answer == 'Y'){
printf("\nEnter the calculation\n");
scanf("%lf %c %lf", &number1, &operation, &number2);
}
if(answer == 'n' || answer == 'N'){
break; /* Go back to the beginning */
}
}
#### 4,項(xiàng)目源碼
/*Exercise 3.4 A calculator that allows multiple calculations */
include <stdio.h>
int main()
{
double number1 = 0.0; /* First operand value a decimal number /
double number2 = 0.0; / Second operand value a decimal number /
char operation = 0;
char answer ;/ Operation - must be +, -, *, /, or % */
printf("\nEnter the calculation\n");
scanf("%lf %c %lf", &number1, &operation, &number2);
for(;;){
switch(operation)
{
case '+': // No checks necessary for add
printf("= %lf\n", number1 + number2);
break;
case '-': // No checks necessary for subtract
printf("= %lf\n", number1 - number2);
break;
case '*': // No checks necessary for multiply
printf("= %lf\n", number1 * number2);
break;
case '/':
if(number2 == 0) // Check second operand for zero
printf("\n\n\aDivision by zero error!\n");
else
printf("= %lf\n", number1 / number2);
break;
case '%': // Check second operand for zero
if((long)number2 == 0)
printf("\n\n\aDivision by zero error!\n");
else
printf("= %ld\n", (long)number1 % (long)number2);
break;
default: // Operation is invalid if we get to here
printf("\n\n\aIllegal operation!\n");
break;
}
/* The following statements added to prompt for continuing */
printf("\n Do you want to do another calculation? (y or n): ");
scanf(" %c", &answer);
if(answer == 'y' || answer == 'Y'){
printf("\nEnter the calculation\n");
scanf("%lf %c %lf", &number1, &operation, &number2); /* Go back to the beginning /
}
if(answer == 'n' || answer == 'N'){
break; / Go back to the beginning */
}
}
return 0;
}
#### 5,項(xiàng)目提升
能支持任意多個(gè)操作數(shù)的運(yùn)算阵幸,引入運(yùn)算符優(yōu)先關(guān)系機(jī)制犬庇,屆時(shí)更新
######[友情鏈接](https://github.com/apress/beg-c-5th-edition)