C语言:实现具有加法和乘法功能的计算器
时间: 2024-02-28 13:53:33 浏览: 28
好的,以下是一个简单的 C 语言程序,实现了具有加法和乘法功能的计算器:
```
#include <stdio.h>
int main()
{
char operator;
float num1, num2, result;
printf("请输入要进行的运算符(+ 或 *):");
scanf("%c", &operator);
printf("请输入两个数字:");
scanf("%f %f", &num1, &num2);
if (operator == '+')
{
result = num1 + num2;
printf("%.2f + %.2f = %.2f", num1, num2, result);
}
else if (operator == '*')
{
result = num1 * num2;
printf("%.2f * %.2f = %.2f", num1, num2, result);
}
else
{
printf("输入的运算符不正确,请重新输入!");
}
return 0;
}
```
运行程序后,会提示用户输入要进行的运算符(+ 或 *),以及两个数字。根据用户输入的运算符,程序会进行加法或乘法运算,并输出结果。
相关问题
C语言:实现具有加法和乘法功能的计算器,多个数
好的,以下是一个 C 语言程序,实现了具有加法和乘法功能的计算器,可以进行多个数的运算:
```
#include <stdio.h>
int main()
{
char operator;
float num, result;
printf("请输入要进行的运算符(+ 或 *):");
scanf("%c", &operator);
printf("请输入数字,以空格分隔:");
// 读取第一个数字
scanf("%f", &num);
result = num;
// 读取其他数字并进行运算
while (scanf("%f", &num) == 1)
{
if (operator == '+')
{
result += num;
}
else if (operator == '*')
{
result *= num;
}
}
// 输出结果
if (operator == '+')
{
printf("结果为:%.2f", result);
}
else if (operator == '*')
{
printf("结果为:%.2f", result);
}
else
{
printf("输入的运算符不正确,请重新输入!");
}
return 0;
}
```
运行程序后,会提示用户输入要进行的运算符(+ 或 *),以及要进行运算的数,以空格分隔。根据用户输入的运算符,程序会进行加法或乘法运算,并输出结果。程序使用 while 循环来读取用户输入的所有数,并进行运算。
C语言:实现具有加法和乘法功能的计算器,输入5*7+2
好的,以下是一个 C 语言程序,实现了具有加法和乘法功能的计算器,可以处理如“5*7+2”的表达式:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
float parse_expression(char* expression);
int main()
{
char expression[100];
printf("请输入表达式:");
scanf("%s", expression);
float result = parse_expression(expression);
printf("%s = %.2f", expression, result);
return 0;
}
float parse_expression(char* expression)
{
char operator = '+';
float num1 = 0, num2 = 0, result = 0;
int i = 0;
while (i < strlen(expression))
{
char c = expression[i];
if (c >= '0' && c <= '9')
{
// 读取数字
float num = atof(&c);
// 处理多位数字
while (i + 1 < strlen(expression))
{
char next_c = expression[i + 1];
if (next_c >= '0' && next_c <= '9')
{
num = num * 10 + atof(&next_c);
i++;
}
else
{
break;
}
}
// 根据运算符进行运算
if (operator == '+')
{
result += num;
}
else if (operator == '*')
{
if (num1 == 0)
{
num1 = num;
}
else
{
num2 = num;
result = num1 * num2;
num1 = result;
}
}
}
else if (c == '+' || c == '*')
{
// 读取运算符
operator = c;
}
i++;
}
return result;
}
```
运行程序后,会提示用户输入表达式,并输出计算结果。程序使用一个名为 parse_expression 的函数来解析表达式,并进行加法和乘法运算。函数使用一个 while 循环来遍历表达式中的每个字符,并根据字符类型进行处理。函数使用 atof 函数将字符转换为数字。函数处理多位数字的方法是,如果下一个字符也是数字,则将其加到当前数字中。函数还使用变量 num1 和 num2 来存储乘法运算中的两个数字。
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.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)