简易计算器的算法
求编写简易计算器的算法,要求可以计算小数,而且任输入一个计算式,都能得出结果
#include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> #define N 100 double getvalue(int *i, char *str, double val1); double yunsuan(double result, double val2, char ch); int main(void) { char str[N]; //储存计算表达式 char ch = 0; char isch = 0; int i = 0; int j = 0; double val1 = 0.0; double val2 = 0.0; size_t length = 0; printf("请输入计算表达式:\n"); fgets(str, N, stdin); length = strlen(str); //如果超过内存则结束程序,否则将换行符替换 if(str[length-1] != '\n') { printf("表达式过长,程序异常退出!\n"); return 1; } str[--length] = '\0'; //获得第一个数字的值 for(i=0;i<(int)length;i++) { if(isspace(str[i])) //跳过空格 continue; //如果为数字 if(isdigit(str[i]) || str[i] == '.') val1 = getvalue(&i,str,val1); while(isspace(str[i]) && str[i] != '\0') //跳过空格 i++; //如果不是数字和空格,则做为运算符处理 if(!isspace(str[i]) && str[i] != '\0') { isch = 1; ch = str[i++]; } while(isspace(str[i]) && str[i] != '\0') //跳过空格 i++; if(isdigit(str[i]) || str[i] == '.') val2 = getvalue(&i,str,val2); //如果已储存了运算符 if(isch) { isch = 0; val1 = yunsuan(val1, val2, ch); val2 = 0; //归零 } } printf("\n结果为:%2g\n\n",val1); return 0; } double getvalue(int *i, char *str, double val1) { int j = 0; char buffer[N]; while(isdigit(str[*i]) || str[*i] == '.') { //如果是浮数点 if(str[*i] == '.') { //得到小数点后的数字 do { buffer[j++] = str[(*i)++]; }while(isdigit(str[*i])); buffer[j] = '\0'; val1 += atof(buffer); break; } val1 += str[(*i)++] - '0'; } return val1; } double yunsuan(double result, double val2, char ch) { for(;;) switch(ch) { case '+': return result + val2; case '-': return result - val2; case '*': return result * val2; case '/': if(val2 == 0.0) { printf("除数不能为0,请重新输入除数:"); scanf("%lf",&val2); break; } else return result / val2; default: printf("无法定义的运算符%2c,请重新输入运算符号:",ch); scanf(" %c",&ch); break; } return -1; //如果冲出循环,说明程序出错,返回-1 }
#include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> #include <math.h> #define N 100 double getvalue(int *i, char *str); double yunsuan(double result, double val2, char ch); int main(void) { char str[N]; //储存计算表达式 char ch = 0; char isch = 1; int i = 0; int isfirst = 1; int val2can = 0; double val2 = 0.0; double result = 0.0; size_t length = 0; printf("请输入计算表达式:\n"); for(;;) { val2 = 0; fgets(str, N, stdin); if(strcmp(str,"退出\n") == 0) break; length = strlen(str); //如果超过内存则结束程序,否则将换行符替换 if(str[length-1] != '\n') { printf("表达式过长,程序异常退出!\n"); return 1; } str[--length] = '\0'; //如果输入'=' if(str[0] == '=') { i = 1; while(isspace(str[i]) && str[i] != '\0') //跳过空格 i++; //如果不是数字和空格,则做为运算符处理 if(!isspace(str[i]) && str[i] != '\0') { isch = 1; ch = str[i++]; } while(isspace(str[i]) && str[i] != '\0') //跳过空格 i++; if(isdigit(str[i]) || str[i] == '.') val2 = getvalue(&i,str); result = yunsuan(result , val2 , ch); } else { isch = 1; i = 0; isfirst = 1; result = 0.0; //获得第一个数字的值 while(str[i] != '\0') { while(isspace(str[i]) && str[i] != '\0') //跳过空格 i++; //如果为数字 if(isdigit(str[i]) || str[i] == '.') if(isfirst) result = getvalue(&i,str); else { val2can = 1; val2 = getvalue(&i,str); } while(isspace(str[i]) && str[i] != '\0') //跳过空格 i++; //如果不是数字和空格,则做为运算符处理 if(!isspace(str[i]) && str[i] != '\0' && isch) { isch = 1; ch = str[i++]; } //如果储存了运算符 if(val2can) { isch = 1; result = yunsuan(result, val2, ch); val2 = 0; //归零 } else isch = 0; isfirst = 0; val2can = 0; } } printf("=%2g\n",result); } return 0; } double getvalue(int *i, char *str) { int j = 0; int j2 = 0; int z = 0; double val1 = 0.0; char buffer[N]; char buffer2[N]; while(isdigit(str[*i]) || str[*i] == '.') { //如果是浮数点 if(str[*i] == '.') { //得到小数点后的数字 do { buffer2[j2++] = str[(*i)++]; }while(isdigit(str[*i])); buffer[j2] = '\0'; val1 += atof(buffer2); break; } buffer[j++] = str[(*i)++]; } for(z=0;z<j;z++) val1 += (buffer[z] - '0')*pow(10,j-z-1); printf("val1 = %lf\n",val1); return val1; } double yunsuan(double result, double val2, char ch) { for(;;) switch(ch) { case '+': return result + val2; case '-': return result - val2; case '*': return result * val2; case '/': if(val2 == 0.0) { printf("除数不能为0,请重新输入除数:"); scanf("%lf",&val2); break; } else return result / val2; default: printf("无法定义的运算符%2c,请重新输入运算符号:",ch); scanf(" %c",&ch); break; } return -1; //如果冲出循环,说明程序出错,返回-1 }不好意思整数忘记进位了...对了,优先级我没有去注意,楼主需要的话可以自己动手改一改,不然我就成苦工了...
#include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> #include <math.h> #define N 100 double getvalue(int *i, char *str); double yunsuan(double result, double val2, char ch); int main(void) { char str[N]; //储存计算表达式 char ch = 0; char isch = 1; int i = 0; int isfirst = 1; int val2can = 0; double val2 = 0.0; double result = 0.0; size_t length = 0; printf("请输入计算表达式:\n"); for(;;) { val2 = 0; fgets(str, N, stdin); if(strcmp(str,"退出\n") == 0) break; length = strlen(str); //如果超过内存则结束程序,否则将换行符替换 if(str[length-1] != '\n') { printf("表达式过长,程序异常退出!\n"); return 1; } str[--length] = '\0'; //如果输入'=' if(str[0] == '=') { i = 1; while(isspace(str[i]) && str[i] != '\0') //跳过空格 i++; //如果不是数字和空格,则做为运算符处理 if(!isspace(str[i]) && str[i] != '\0') { isch = 1; ch = str[i++]; } while(isspace(str[i]) && str[i] != '\0') //跳过空格 i++; if(isdigit(str[i]) || str[i] == '.') val2 = getvalue(&i,str); result = yunsuan(result , val2 , ch); } else { isch = 1; i = 0; isfirst = 1; result = 0.0; //获得第一个数字的值 while(str[i] != '\0') { while(isspace(str[i]) && str[i] != '\0') //跳过空格 i++; //如果为数字 if(isdigit(str[i]) || str[i] == '.') if(isfirst) result = getvalue(&i,str); else { val2can = 1; val2 = getvalue(&i,str); } while(isspace(str[i]) && str[i] != '\0') //跳过空格 i++; //如果不是数字和空格,则做为运算符处理 if(!isspace(str[i]) && str[i] != '\0' && isch) { isch = 1; ch = str[i++]; } //如果储存了运算符 if(val2can) { isch = 1; result = yunsuan(result, val2, ch); val2 = 0; //归零 } else isch = 0; isfirst = 0; val2can = 0; } } printf("= %g\n",result); } return 0; } double getvalue(int *i, char *str) { int j = 0; int j2 = 0; int z = 0; double val1 = 0.0; char buffer[N]; char buffer2[N]; while(isdigit(str[*i]) || str[*i] == '.') { //如果是浮数点 if(str[*i] == '.') { //得到小数点后的数字 do { buffer2[j2++] = str[(*i)++]; }while(isdigit(str[*i])); buffer[j2] = '\0'; val1 += atof(buffer2); break; } buffer[j++] = str[(*i)++]; } for(z=0;z<j;z++) val1 += (buffer[z] - '0')*pow(10,j-z-1); return val1; } double yunsuan(double result, double val2, char ch) { for(;;) switch(ch) { case '+': return result + val2; case '-': return result - val2; case '*': return result * val2; case '/': if(val2 == 0.0) { printf("除数不能为0,请重新输入除数:"); scanf("%lf",&val2); break; } else return result / val2; default: printf("无法定义的运算符%2c,请重新输入运算符号:",ch); scanf(" %c",&ch); break; } return -1; //如果冲出循环,说明程序出错,返回-1 }