C语言:储蓄账户余额计算器
时间: 2023-08-13 16:02:29 浏览: 123
好的,我可以帮您编写一个简单的储蓄账户余额计算器。以下是示例代码:
```c
#include <stdio.h>
int main() {
double balance = 0, deposit = 0, withdraw = 0;
printf("请输入您的账户余额:");
scanf("%lf", &balance);
printf("请输入您要存入的金额:");
scanf("%lf", &deposit);
balance += deposit; // 存款操作
printf("请输入您要取出的金额:");
scanf("%lf", &withdraw);
balance -= withdraw; // 取款操作
printf("您的最终余额为:%.2lf\n", balance);
return 0;
}
```
这个程序首先要求用户输入账户余额,然后分别输入存款和取款金额。程序会计算出最终的余额并输出。注意,这个程序非常简单,没有做任何错误处理,例如用户输入了非法字符,或者取出的金额超过了余额等情况都没有进行处理,您可以根据自己的需要进行改进。
相关问题
C语言:实现加法和乘法计算器
以下是C语言实现加法和乘法计算器的代码示例:
```c
#include <stdio.h>
int main() {
int num1, num2, result;
char operator;
printf("请输入两个数字和运算符(+或*):\n");
scanf("%d %d %c", &num1, &num2, &operator);
switch (operator) {
case '+':
result = num1 + num2;
printf("%d + %d = %d\n", num1, num2, result);
break;
case '*':
result = num1 * num2;
printf("%d * %d = %d\n", num1, num2, result);
break;
default:
printf("错误的运算符\n");
break;
}
return 0;
}
```
运行程序后,会提示用户输入两个数字和运算符(+或*),然后根据运算符计算结果并输出。
C语言:实现计算器,输入9+3*6,输出27
以下是C语言代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main() {
char op; // 存储运算符
int num, result;
scanf("%d", &result);
while (scanf("%c%d", &op, &num) != EOF) {
if (op == '+') {
result += num;
} else if (op == '-') {
result -= num;
} else if (op == '*') {
result *= num;
} else if (op == '/') {
result /= num;
} else {
printf("Invalid input!\n");
exit(1);
}
}
printf("%d\n", result);
return 0;
}
```
这段代码中,我们首先读入一个数字作为结果。然后使用一个循环,每次读入一个运算符和一个数字,根据运算符更新结果。如果读入的运算符不是加、减、乘、除中的任何一个,就输出错误信息并终止程序。最终输出结果即可。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)