写一个能判断除数的字符是不是0,能判断输入的两个数是否是字符0到9的数,能计算大数运算,能计算科学记数法,拥有加减乘除四种运算的简易计算器的代码
时间: 2023-09-17 12:06:03 浏览: 88
### 回答1:
以下是能判断除数的字符是不是,能判断输入的两个数是否是字符到9的数,能计算大数运算,能计算科学记数法,拥有加减乘除四种运算的简易计算器的代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define MAX_LEN 100
int is_digit(char c) {
return c >= '' && c <= '9';
}
int is_valid_number(char *s) {
int len = strlen(s);
if (len == ) return ;
if (s[] == '-' || s[] == '+') {
if (len == 1) return ;
s++;
len--;
}
int dot_count = ;
for (int i = ; i < len; i++) {
if (s[i] == '.') {
dot_count++;
if (dot_count > 1) return ;
} else if (!is_digit(s[i])) {
return ;
}
}
return 1;
}
int is_zero(char *s) {
int len = strlen(s);
if (len == 1 && s[] == '') return 1;
return ;
}
int is_valid_operator(char c) {
return c == '+' || c == '-' || c == '*' || c == '/';
}
int get_priority(char c) {
if (c == '+' || c == '-') return 1;
if (c == '*' || c == '/') return 2;
return ;
}
double calculate(double a, double b, char op) {
if (op == '+') return a + b;
if (op == '-') return a - b;
if (op == '*') return a * b;
if (op == '/') return a / b;
return ;
}
double eval(char *s) {
int len = strlen(s);
double num_stack[MAX_LEN];
char op_stack[MAX_LEN];
int num_top = -1, op_top = -1;
for (int i = ; i < len; i++) {
if (s[i] == ' ') continue;
if (is_digit(s[i])) {
double num = s[i] - '';
while (i + 1 < len && is_digit(s[i + 1])) {
num = num * 10 + (s[i + 1] - '');
i++;
}
if (i + 1 < len && s[i + 1] == '.') {
i++;
double frac = .1;
while (i + 1 < len && is_digit(s[i + 1])) {
num += frac * (s[i + 1] - '');
frac *= .1;
i++;
}
}
if (i + 1 < len && (s[i + 1] == 'e' || s[i + 1] == 'E')) {
i += 2;
int exp = , sign = 1;
if (s[i] == '-') {
sign = -1;
i++;
} else if (s[i] == '+') {
i++;
}
while (i < len && is_digit(s[i])) {
exp = exp * 10 + (s[i] - '');
i++;
}
num *= pow(10, sign * exp);
}
num_stack[++num_top] = num;
} else if (s[i] == '(') {
op_stack[++op_top] = s[i];
} else if (s[i] == ')') {
while (op_top >= && op_stack[op_top] != '(') {
double b = num_stack[num_top--];
double a = num_stack[num_top--];
char op = op_stack[op_top--];
num_stack[++num_top] = calculate(a, b, op);
}
op_top--;
} else if (is_valid_operator(s[i])) {
while (op_top >= && get_priority(op_stack[op_top]) >= get_priority(s[i])) {
double b = num_stack[num_top--];
double a = num_stack[num_top--];
char op = op_stack[op_top--];
num_stack[++num_top] = calculate(a, b, op);
}
op_stack[++op_top] = s[i];
}
}
while (op_top >= ) {
double b = num_stack[num_top--];
double a = num_stack[num_top--];
char op = op_stack[op_top--];
num_stack[++num_top] = calculate(a, b, op);
}
return num_stack[];
}
int main() {
char s[MAX_LEN];
printf("请输入表达式:");
fgets(s, MAX_LEN, stdin);
s[strlen(s) - 1] = '\';
if (!is_valid_number(s)) {
printf("表达式不合法!\n");
return ;
}
char *p = s;
while (*p) {
if (*p == '/') {
p++;
if (is_zero(p)) {
printf("除数不能为!\n");
return ;
}
}
p++;
}
double result = eval(s);
printf("计算结果为:%g\n", result);
return ;
}
```
### 回答2:
以下是一个满足以上要求的简易计算器的代码:
```python
def is_zero(divisor):
if divisor == '0':
return True
else:
return False
def is_numeric(num1, num2):
numeric_chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
if num1 in numeric_chars and num2 in numeric_chars:
return True
else:
return False
def calculate(num1, num2, operator):
if operator == '+':
result = num1 + num2
elif operator == '-':
result = num1 - num2
elif operator == '*':
result = num1 * num2
elif operator == '/':
if is_zero(num2):
print("除数不能为0")
return None
else:
result = num1 / num2
else:
print("无效的运算符")
return None
return result
def scientific_notation(number):
if 'e' in number:
parts = number.split('e')
num = float(parts[0])
exp = int(parts[1])
result = num * (10 ** exp)
return result
else:
return float(number)
num1 = input("请输入第一个数字:")
num2 = input("请输入第二个数字:")
operator = input("请输入运算符(+、-、*、/):")
if not is_numeric(num1, num2):
print("输入的数字必须为字符0到9的数")
elif '.' in num1 or '.' in num2 or 'e' in num1 or 'e' in num2:
num1 = scientific_notation(num1)
num2 = scientific_notation(num2)
result = calculate(num1, num2, operator)
if result is not None:
print("计算结果为:", result)
else:
num1 = int(num1)
num2 = int(num2)
result = calculate(num1, num2, operator)
if result is not None:
print("计算结果为:", result)
```
这段代码包含了函数来判断除数是否为0,判断输入的数字是否为字符0到9,以及计算大数和科学记数法的功能。它还包含一个简单的计算函数,接受两个数字和一个运算符,并根据不同的运算符来执行相应的运算。最后,根据输入的数字类型和运算符,选择相应的处理方式,并输出计算结果。
### 回答3:
代码如下:
```
def is_zero(divisor):
if divisor == '0':
return True
else:
return False
def is_number(num1, num2):
if num1.isdigit() and num2.isdigit():
return True
else:
return False
def calc_num(num1, num2, operator):
result = 0
if operator == '+':
result = int(num1) + int(num2)
elif operator == '-':
result = int(num1) - int(num2)
elif operator == '*':
result = int(num1) * int(num2)
elif operator == '/':
result = int(num1) / int(num2)
return result
def scientific_notation(num):
if 'e' in num:
num_parts = num.split('e')
base = float(num_parts[0])
exponent = int(num_parts[1])
result = base * (10 ** exponent)
return result
else:
return float(num)
divisor = input("请输入除数: ")
if is_zero(divisor):
print("除数不能为0。")
else:
num1 = input("请输入第一个数(0-9的字符): ")
num2 = input("请输入第二个数(0-9的字符): ")
if is_number(num1, num2):
operator = input("请输入运算符(+, -, *, /): ")
result = calc_num(num1, num2, operator)
print("计算结果:", result)
else:
print("输入错误,请输入0-9的字符。")
num = input("请输入一个数字: ")
result = scientific_notation(num)
print("科学记数法计算结果:", result)
```
这段代码中,定义了is_zero函数用于判断除数是否为0,is_number函数用于判断输入的两个数是否为0-9的字符,calc_num函数用于进行加减乘除四种运算,scientific_notation函数用于计算科学记数法。根据用户的输入,通过调用相应的函数进行计算并输出结果。
阅读全文