模拟简单运算器的工作,输入一个算式(没有空格),遇等号"="说明输入 结束,输出结果。假设计算器只能进行加、减、乘、除运算,运算数和结果都是整数,4 种运算符的优先级相同,按从左到右的顺序计算。例如,输入 1+2*10-10/2=后,输出10。试编写相应程序。其中的运算部分要求定义函数实现。
我想设四个函数让他分头进行,但是要输入啊——不会了
模拟简单运算器的工作,输入一个算式(没有空格),遇等号"="说明输入 结束,输出结果。假设计算器只能进行加、减、乘、除运算,运算数和结果都是整数,4 种运算符的优先级相同,按从左到右的顺序计算。例如,输入 1+2*10-10/2=后,输出10。试编写相应程序。其中的运算部分要求定义函数实现。
我想设四个函数让他分头进行,但是要输入啊——不会了
我小学的时候写过一个类似的,但是因为那个时候还小。写的好丑,凑合着看吧。不懂问我;
望采纳!
#include<stdio.h>
#include<conio.h>
#include<windows.h>
#define TEN 10
int ONE_H;
int area;
int OPERATION_1[TEN];
int OPERATION_2[TEN];
int OPERATION_A_YUN;
int OPERATION_B_YUN;
void Clearn();
void AREA();
void OPERATION_FOR_1();
void OPERATION_FOR_2();
void VALUATION();
void ADD();
void MINUS();
void MULTIPLY();
void DIVISION();
bool GUI();
int main() {
printf("0");
for (;;) {
// 结果初始化为0
area = 0;
// 标志现在是 第奇次 还是第偶次运算 运算
ONE_H = 0;
//Clearn();
while (1) {
// 输出答案
// 奇次
if (ONE_H % 2 == 0)
OPERATION_FOR_1();
// 偶次
if (ONE_H % 2 == 1)
OPERATION_FOR_2();
if (ONE_H == 0)
VALUATION();
// 加法
ADD();
// 减法
MINUS();
// 乘法
MULTIPLY();
// 除法
DIVISION();
if ( GUI() ) {
break;
}
AREA();
ONE_H++;
}
}
return 0;
}
void AREA() { //?????
system("cls");
printf("%d", area);
}
void OPERATION_FOR_1() {
// 单个字符的读入 在10个字符 范围内
for (OPERATION_A_YUN = 0; OPERATION_A_YUN < TEN; OPERATION_A_YUN++) {
OPERATION_1[OPERATION_A_YUN] = getch();
if (OPERATION_A_YUN == 0)
system("cls");
putchar(OPERATION_1[OPERATION_A_YUN]);
// 如果输入的字符不是 数字的话就停止读入
if (OPERATION_1[OPERATION_A_YUN] > '9' || OPERATION_1[OPERATION_A_YUN] < '0')
break;
}
}
void OPERATION_FOR_2() {
for (OPERATION_B_YUN = 0; OPERATION_B_YUN < TEN; OPERATION_B_YUN++) {
OPERATION_2[OPERATION_B_YUN] = getch();
if (OPERATION_B_YUN == 0) system("cls");
putchar(OPERATION_2[OPERATION_B_YUN]);
if (OPERATION_2[OPERATION_B_YUN] > '9' || OPERATION_2[OPERATION_B_YUN] < '0') break;
}
}
void VALUATION() { //??OPERATION_1???????area
int i, E_1 = 1;
for (i = OPERATION_A_YUN - 1; i >= 0; i--) {
area = area + (OPERATION_1[i] - '0') * E_1;
E_1 *= 10;
}
}
void ADD() {
if (OPERATION_1[OPERATION_A_YUN] == '+' && ONE_H != 0 && ONE_H % 2 == 1) {
int i, E_1 = 1;
for (i = OPERATION_B_YUN - 1; i >= 0; i--) {
area = area + (OPERATION_2[i] - '0') * E_1;
E_1 *= 10;
}
}
if (OPERATION_2[OPERATION_B_YUN] == '+' && ONE_H != 0 && ONE_H % 2 == 0) {
int i, E_1 = 1;
for (i = OPERATION_A_YUN - 1; i >= 0; i--) {
area = area + (OPERATION_1[i] - '0') * E_1;
E_1 *= 10;
}
}
}
void MINUS() {
if (OPERATION_1[OPERATION_A_YUN] == '-' && ONE_H != 0 && ONE_H % 2 == 1) {
int i, E_1 = 1;
for (i = OPERATION_B_YUN - 1; i >= 0; i--) {
area = area - (OPERATION_2[i] - '0') * E_1;
E_1 *= 10;
}
}
if (OPERATION_2[OPERATION_B_YUN] == '-' && ONE_H != 0 && ONE_H % 2 == 0) {
int i, E_1 = 1;
for (i = OPERATION_A_YUN - 1; i >= 0; i--) {
area = area - (OPERATION_1[i] - '0') * E_1;
E_1 *= 10;
}
}
}
void MULTIPLY() {
if (OPERATION_1[OPERATION_A_YUN] == '*' && ONE_H != 0 && ONE_H % 2 == 1) {
int i, E_1 = 1, mod_A = 0;
for (i = OPERATION_B_YUN - 1; i >= 0; i--) {
mod_A = mod_A + (OPERATION_2[i] - '0') * E_1;
E_1 *= 10;
}
area *= mod_A;
}
if (OPERATION_2[OPERATION_B_YUN] == '*' && ONE_H != 0 && ONE_H % 2 == 0) {
int i, E_1 = 1, mod_A = 0;
for (i = OPERATION_A_YUN - 1; i >= 0; i--) {
mod_A = mod_A + (OPERATION_1[i] - '0') * E_1;
E_1 *= 10;
}
area *= mod_A;
}
}
void DIVISION() {
if (OPERATION_1[OPERATION_A_YUN] == '/' && ONE_H != 0 && ONE_H % 2 == 1) {
int i, E_1 = 1, DIVISION_A = 0;
for (i = OPERATION_B_YUN - 1; i >= 0; i--) {
DIVISION_A = DIVISION_A + (OPERATION_2[i] - '0') * E_1;
E_1 *= 10;
}
area /= DIVISION_A;
}
if (OPERATION_2[OPERATION_B_YUN] == '/' && ONE_H != 0 && ONE_H % 2 == 0) {
int i, E_1 = 1, DIVISION_A = 0;
for (i = OPERATION_A_YUN - 1; i >= 0; i--) {
DIVISION_A = DIVISION_A + (OPERATION_1[i] - '0') * E_1;
E_1 *= 10;
}
area /= DIVISION_A;
}
}
bool GUI() {
// 如果输入的是 A 的话就归零
if (OPERATION_1[OPERATION_A_YUN] == 'a' || OPERATION_1[OPERATION_A_YUN] == 'A') {
area = 0;
system("cls");
printf("%d", area);
return true;
}
if (OPERATION_2[OPERATION_B_YUN] == 'a' || OPERATION_1[OPERATION_B_YUN] == 'A') {
area = 0;
system("cls");
printf("%d", area);
return true;
}
if (OPERATION_1[OPERATION_A_YUN] == '=' && ONE_H != 0 && ONE_H % 2 == 0) {
system("cls");
printf("%d", area);
return true;
}
if (OPERATION_2[OPERATION_B_YUN] == '=' && ONE_H != 0 && ONE_H % 2 == 1) {
system("cls");
printf("%d", area);
return true;
}
return false;
}
void Clearn(){
ONE_H = 0;
area = 0;
for(int i = 0 ; i < TEN ; i++ ){
OPERATION_1[i] = 0;
}
for(int i = 0 ; i < TEN ; i++ ){
OPERATION_2[i] = 0;
}
OPERATION_A_YUN = 0;
OPERATION_B_YUN = 0;
}