简便计算用什么程序做

时间:2025-01-27 22:53:13 手机游戏

简便计算可以使用多种编程语言来实现,以下是几种常见编程语言的简易计算器程序示例:

Python

```python

def calculate(expression):

try:

result = eval(expression)

return result

except:

return "无效的表达式"

while True:

expression = input("请输入数学表达式(输入 'q' 退出):")

if expression == 'q':

break

print(calculate(expression))

```

Java

```java

import java.util.Scanner;

public class SimpleCalculator {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("请输入第一个数字:");

double num1 = scanner.nextDouble();

System.out.println("请输入运算符:");

char operator = scanner.next().charAt(0);

System.out.println("请输入第二个数字:");

double num2 = scanner.nextDouble();

double result = 0;

switch (operator) {

case '+':

result = num1 + num2;

break;

case '-':

result = num1 - num2;

break;

case '*':

result = num1 * num2;

break;

case '/':

if (num2 == 0) {

System.out.println("除数不能为零");

continue;

}

result = num1 / num2;

break;

default:

System.out.println("无效的运算符");

continue;

}

System.out.println("结果: " + result);

}

}

```

C语言

```c

include

int main() {

float num1, num2, result = 0;

char ch;

printf("输入符号(+, -, *, /): ");

scanf(" %c", &ch);

printf("输入两个数:\n");

scanf("%f %f", &num1, &num2);

switch (ch) {

case '+':

result = num1 + num2;

break;

case '-':

result = num1 - num2;

break;

case '*':

result = num1 * num2;

break;

case '/':

if (num2 == 0) {

printf("Error!\n");

return 1;

}

result = num1 / num2;

break;

default:

printf("Error!\n");

return 1;

}

printf("结果: %.2f\n", result);

return 0;

}

```

JavaScript (适用于网页)

```html

简易计算器