C 語言實例 – 實現(xiàn)簡單的計算器

C 語言實例 - 實現(xiàn)簡單的計算器

C 語言實例 C 語言實例

實現(xiàn)加減乘除計算。

實例

# include <stdio.h> int main() { char operator; double firstNumber,secondNumber; printf("輸入操作符 (+, -, *, /): "); scanf("%c", &operator); printf("輸入兩個數(shù)字: "); scanf("%lf %lf",&firstNumber, &secondNumber); switch(operator) { case '+': printf("%.1lf + %.1lf = %.1lf",firstNumber, secondNumber, firstNumber + secondNumber); break; case '-': printf("%.1lf - %.1lf = %.1lf",firstNumber, secondNumber, firstNumber - secondNumber); break; case '*': printf("%.1lf * %.1lf = %.1lf",firstNumber, secondNumber, firstNumber * secondNumber); break; case '/': printf("%.1lf / %.1lf = %.1lf",firstNumber, secondNumber, firstNumber / secondNumber); break; // operator doesn't match any case constant (+, -, *, /) default: printf("Error! operator is not correct"); } return 0; }

輸出結果為:

輸入操作符 (+, -, *, /): *
輸入兩個數(shù)字: 4 5
4.0 * 5.0 = 20.0

C 語言實例 C 語言實例

相關文章
亚洲国产精品第一区二区,久久免费视频77,99V久久综合狠狠综合久久,国产免费久久九九免费视频