小程序之计算器 【C++ STL栈实现】 + 【C 数组模拟栈实现】 【适用VC, DEV, codeblack】
来源:互联网 发布:怎样修改网卡mac地址 编辑:程序博客网 时间:2024/06/05 00:52
自己写的小程序,记录一下提醒这两个只能在DEV或者codeblack 运行。 VC能运行的在最后面(哎,毕竟课程设计是在VC里面测试)。
C++版本的,用STL栈实现的:
#include <cstdio>#include <cstring>#include <stack>#include <cstdlib>#include <windows.h>using namespace std;int in = 0, out = 0;void input(){system("color 1f");printf("**********************************简单计算器**********************************\n\n");printf("********************功能:实现小数和整数加减乘除四则混合运算*******************\n\n");printf(">>>>>>>>>>>输入: (加号 +) (减号 -) (乘号 *) (除号 /) (数字正常输入)>>>>>>>>>>>\n\n"); printf("!!!!!!!!!!!!!!!提醒:可以有多个括号, 但模仿计算器不能输入等号(=)!!!!!!!!!!!!!!!\n\n"); printf("<<<<<<<<<<<<<<<<<<<<<<<<输出:请按回车(默认保留6位小数)<<<<<<<<<<<<<<<<<<<<<<<<\n\n");printf("\n");printf("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&模拟键盘&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n");printf("^^^^^^^^^^^^^^^^^^^^^^^^^^^^[ 7 8 9 / ( ]^^^^^^^^^^^^^^^^^^^^^^^^^^^\n");printf("<<<<<<<<<<<<<<<<<<<<<<<<<<<<[ 4 5 6 * ) ]>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");printf("____________________________[ 1 2 3 - = ]___________________________\n");printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~[ 0 . + ]~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");printf("\n");printf("!!!!!!!!!!!!!输入像1+2/5*6-7类型的表达式,否则可能无法得到正确结果!!!!!!!!!!!!!\n"); printf("!!!!!!!!!!!!!如果程序出错误,应该是输入出了问题,记着错误关闭再打开!!!!!!!!!!!!!\n");printf("@@@@@@@@@@@使用者输入模拟键盘上的数字及运算符,该计算器可以处理空格哦@@@@@@@@@@\n");printf("\n");} void inputcolor(){if(in > 16)in -= 16;switch(in){case 0: system("color 0f"); break;case 1: system("color 1e"); break;case 2: system("color 2d"); break;case 3: system("color 3c"); break;case 4: system("color 4b"); break;case 5: system("color 5a"); break;case 6: system("color 69"); break;case 7: system("color 78"); break;case 8: system("color f0"); break;case 9: system("color e1"); break;case 10:system("color d2"); break;case 11:system("color c3"); break;case 12:system("color b4"); break;case 13:system("color a5"); break;case 14:system("color 96"); break;case 15:system("color 87"); break;}in++;}void outputcolor(){if(out > 16)out -= 16;switch(out){case 15:system("color 0f"); break;case 14:system("color 96"); break;case 13:system("color d8"); break;case 12:system("color 3c"); break;case 11:system("color 4b"); break;case 10:system("color 5a"); break;case 9: system("color 69"); break;case 8: system("color 72"); break;case 7: system("color f0"); break;case 6: system("color e1"); break;case 5: system("color 2e"); break;case 4: system("color ca"); break;case 3: system("color b4"); break;case 2: system("color a5"); break;case 1: system("color 97"); break;case 0: system("color 8f"); break;}out++;}char compare(char a, char b)//比较运算符a,b优先级 {if(a == '+' || a == '-'){if(b == '*' || b == '/' || b == '(')return '<';elsereturn '>';//连续的加减 优先前面的算 }if(a == '*' || a == '/'){if(b == '(')return '<';elsereturn '>';//连续的乘除 优先前面的算 }if(a == '('){if(b == ')')return '=';elsereturn '<';//优先括号里面的运算符 }if(a == '='){if(b == '=')return '=';elsereturn '<';//因为存储运算符的栈顶为 = 所以后来碰到的运算符除了=外 优先级均比它高 }}double work(double a, char op, double b)//对浮点数 a b 进行四则运算 {switch(op){case '+': return a + b;case '-': return a - b;case '*': return a * b;case '/': return a / b;}}bool judge(char a)//判断字符 是否是数字或者小数点 {return a >= '0' && a <= '9' || a == '.';}int main(){input();//打印计算器 窗口 int i, j;int len;char str[1010];//输入的运算式 char s[1010];//把浮点数当成字符存储起来 int t;//记录存储浮点数的 字符数组的位数 int exist;//标记是否出现数字 double x, y, z;//求每一步的值 用到的变量 while(gets(str))//输入计算内容 {inputcolor();len = strlen(str);str[++len] = '=';//在末尾添加一个 = stack<double> num;//存储浮点数 stack<char> op;//存储运算符 op.push('=');//逆波兰式 = 先进栈 t = 0;exist = 0;for(i = 0; i < len;){if(str[i] == ' ')//碰到空格 {i++;continue;}if(judge(str[i])){s[t++] = str[i++];//字符串后移一位 exist = 1;//出现数字 continue;}if(exist)//出现过数字 且当前字符是运算符 {s[t] = '\0';num.push(atof(s));//上一数字 进栈 t = exist = 0;//初始化 } /*下面比较上一运算符 和 当前运算符优先级 *//* 若 < 进栈,若 > 计算,若 = 上一运算符出栈*/switch(compare(op.top(), str[i])){case '<': op.push(str[i++]); break;//字符串后移一位 case '=': op.pop(), i++; break;//上一运算符出栈 字符串后移一位 case '>': x = num.top(), num.pop(); y = num.top(), num.pop(); z = work(y, op.top(), x), op.pop();//用过的运算符出栈 num.push(z); break;//新浮点数进栈 } }outputcolor();printf("%.6lf\n", num.top()); }return 0;}
C写的, 用数组模拟栈:
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <windows.h>int in = 0, out = 0;void input(){system("color 1f");printf("**********************************简单计算器**********************************\n\n");printf("********************功能:实现小数和整数加减乘除四则混合运算*******************\n\n");printf(">>>>>>>>>>>输入: (加号 +) (减号 -) (乘号 *) (除号 /) (数字正常输入)>>>>>>>>>>>\n\n"); printf("!!!!!!!!!!!!!!!提醒:可以有多个括号, 但模仿计算器不能输入等号(=)!!!!!!!!!!!!!!!\n\n"); printf("<<<<<<<<<<<<<<<<<<<<<<<<输出:请按回车(默认保留6位小数)<<<<<<<<<<<<<<<<<<<<<<<<\n\n");printf("\n");printf("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&模拟键盘&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n");printf("^^^^^^^^^^^^^^^^^^^^^^^^^^^^[ 7 8 9 / ( ]^^^^^^^^^^^^^^^^^^^^^^^^^^^\n");printf("<<<<<<<<<<<<<<<<<<<<<<<<<<<<[ 4 5 6 * ) ]>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");printf("____________________________[ 1 2 3 - = ]___________________________\n");printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~[ 0 . + ]~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");printf("\n");printf("!!!!!!!!!!!!!输入像1+2/5*6-7类型的表达式,否则可能无法得到正确结果!!!!!!!!!!!!!\n"); printf("!!!!!!!!!!!!!如果程序出错误,应该是输入出了问题,记着错误关闭再打开!!!!!!!!!!!!!\n");printf("@@@@@@@@@@@使用者输入模拟键盘上的数字及运算符,该计算器可以处理空格哦@@@@@@@@@@\n");printf("\n");} void inputcolor(){if(in > 16)in -= 16;switch(in){case 0: system("color 0f"); break;case 1: system("color 1e"); break;case 2: system("color 2d"); break;case 3: system("color 3c"); break;case 4: system("color 4b"); break;case 5: system("color 5a"); break;case 6: system("color 69"); break;case 7: system("color 78"); break;case 8: system("color f0"); break;case 9: system("color e1"); break;case 10:system("color d2"); break;case 11:system("color c3"); break;case 12:system("color b4"); break;case 13:system("color a5"); break;case 14:system("color 96"); break;case 15:system("color 87"); break;}in++;}void outputcolor(){if(out > 16)out -= 16;switch(out){case 15:system("color 0f"); break;case 14:system("color 96"); break;case 13:system("color d8"); break;case 12:system("color 3c"); break;case 11:system("color 4b"); break;case 10:system("color 5a"); break;case 9: system("color 69"); break;case 8: system("color 72"); break;case 7: system("color f0"); break;case 6: system("color e1"); break;case 5: system("color 2e"); break;case 4: system("color ca"); break;case 3: system("color b4"); break;case 2: system("color a5"); break;case 1: system("color 97"); break;case 0: system("color 8f"); break;}out++;}char compare(char a, char b)//比较运算符a,b优先级 {if(a == '+' || a == '-'){if(b == '*' || b == '/' || b == '(')return '<';elsereturn '>';//连续的加减 优先前面的算 }if(a == '*' || a == '/'){if(b == '(')return '<';elsereturn '>';//连续的乘除 优先前面的算 }if(a == '('){if(b == ')')return '=';elsereturn '<';//优先括号里面的运算符 }if(a == '='){if(b == '=')return '=';elsereturn '<';//因为存储运算符的栈顶为 = 所以后来碰到的运算符除了=外 优先级均比它高 }}double work(double a, char op, double b)//对浮点数 a b 进行四则运算 {switch(op){case '+': return a + b;case '-': return a - b;case '*': return a * b;case '/': return a / b;}}int judge(char a)//判断字符 是否是数字或者小数点 {return (a >= '0' && a <= '9' || a == '.') ? 1 : 0;}int main(){input();//打印计算器 窗口 int i, j;int len;char str[1010];//输入的运算式 char s[1010];//把浮点数当成字符存储起来 int t;//记录存储浮点数的 字符数组的位数 int exist;//标记是否出现数字 double x, y, z;//求每一步的值 用到的变量 int numtop;//浮点数数组顶部 用数组模拟栈 int optop;//运算符数组顶部 模拟栈 while(gets(str))//输入计算内容 {inputcolor();numtop = optop = 0;//初始化 len = strlen(str);str[++len] = '=';//在末尾添加一个 = double num[1010];//存储浮点数 char op[1010];//存储运算符 op[optop++] = '=';//逆波兰式 = 先进栈 t = 0;exist = 0;for(i = 0; i < len;){if(str[i] == ' ')//碰到空格 {i++;continue;}if(judge(str[i])){s[t++] = str[i++];//字符串后移一位 exist = 1;//出现数字 continue;}if(exist)//出现过数字 且当前字符是运算符 {s[t] = '\0';num[numtop++] = atof(s); //模拟进栈 t = exist = 0;//初始化 } /*下面比较上一运算符 和 当前运算符优先级 *//* 若 < 进栈,若 > 计算,若 = 上一运算符出栈*/switch(compare(op[optop-1], str[i])){case '<': op[optop++] = str[i++]; break;//字符串后移一位 case '=': optop--, i++; break;//上一运算符出栈 字符串后移一位 case '>': x = num[numtop-1], numtop--; y = num[numtop-1], numtop--; z = work(y, op[optop-1], x), optop--;//用过的运算符出栈 num[numtop++] = z; break;//新浮点数进栈 } }outputcolor();printf("%.6lf\n", num[numtop-1]); }return 0;}
在VC实现的,功能效果和上面的一样:
#include <stdio.h>#include <string.h>#include <stdlib.h>int in = 0, out = 0;void output(){system("color 1f");printf("**********************************简单计算器**********************************\n\n");printf("********************功能:实现小数和整数加减乘除四则混合运算*******************\n\n");printf(">>>>>>>>>>>输入: (加号 +) (减号 -) (乘号 *) (除号 /) (数字正常输入)>>>>>>>>>>>\n\n"); printf("!!!!!!!!!!!!!!!提醒:可以有多个括号, 但模仿计算器不能输入等号(=)!!!!!!!!!!!!!!!\n\n"); printf("<<<<<<<<<<<<<<<<<<<<<<<<输出:请按回车(默认保留6位小数)<<<<<<<<<<<<<<<<<<<<<<<<\n\n");printf("\n");printf("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&模拟键盘&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n");printf("^^^^^^^^^^^^^^^^^^^^^^^^^^^^[ 7 8 9 / ( ]^^^^^^^^^^^^^^^^^^^^^^^^^^^\n");printf("<<<<<<<<<<<<<<<<<<<<<<<<<<<<[ 4 5 6 * ) ]>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");printf("____________________________[ 1 2 3 - = ]___________________________\n");printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~[ 0 . + ]~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");printf("\n");printf("!!!!!!!!!!!!!输入像1+2/5*6-7类型的表达式,否则可能无法得到正确结果!!!!!!!!!!!!!\n"); printf("!!!!!!!!!!!!!如果程序出错误,应该是输入出了问题,记着错误关闭再打开!!!!!!!!!!!!!\n");printf("@@@@@@@@@@@使用者输入模拟键盘上的数字及运算符,该计算器可以处理空格哦@@@@@@@@@@\n");printf("\n");}void inputcolor(){if(in > 16)in -= 16;switch(in){case 0: system("color 0f"); break;case 1: system("color 1e"); break;case 2: system("color 2d"); break;case 3: system("color 3c"); break;case 4: system("color 4b"); break;case 5: system("color 5a"); break;case 6: system("color 69"); break;case 7: system("color 78"); break;case 8: system("color f0"); break;case 9: system("color e1"); break;case 10:system("color d2"); break;case 11:system("color c3"); break;case 12:system("color b4"); break;case 13:system("color a5"); break;case 14:system("color 96"); break;case 15:system("color 87"); break;}in++;}void outputcolor(){if(out > 16)out -= 16;switch(out){case 15:system("color 0f"); break;case 14:system("color 96"); break;case 13:system("color d8"); break;case 12:system("color 3c"); break;case 11:system("color 4b"); break;case 10:system("color 5a"); break;case 9: system("color 69"); break;case 8: system("color 72"); break;case 7: system("color f0"); break;case 6: system("color e1"); break;case 5: system("color 2e"); break;case 4: system("color ca"); break;case 3: system("color b4"); break;case 2: system("color a5"); break;case 1: system("color 97"); break;case 0: system("color 8f"); break;}out++;}char compare(char a, char b)//比较运算符a,b优先级 {if(a == '+' || a == '-'){if(b == '*' || b == '/' || b == '(')return '<';elsereturn '>';//连续的加减 优先前面的算 }if(a == '*' || a == '/'){if(b == '(')return '<';elsereturn '>';//连续的乘除 优先前面的算 }if(a == '('){if(b == ')')return '=';elsereturn '<';//优先括号里面的运算符 }if(a == '='){if(b == '=')return '=';elsereturn '<';//因为存储运算符的栈顶为 = 所以后来碰到的运算符除了=外 优先级均比它高 }}double work(double a, char op, double b)//对浮点数 a b 进行四则运算 {switch(op){case '+': return a + b;case '-': return a - b;case '*': return a * b;case '/': return a / b;}}int judge(char a)//判断字符 是否是数字或者小数点 {return (a >= '0' && a <= '9' || a == '.') ? 1 : 0;}int main(){ int i, j;int len;char str[1010];//输入的运算式 char s[1010];//把浮点数当成字符存储起来 int t;//记录存储浮点数的 字符数组的位数 int exist;//标记是否出现数字 double x, y, z;//求每一步的值 用到的变量 int numtop;//浮点数数组顶部 用数组模拟栈 int optop;//运算符数组顶部 模拟栈 double num[1010];//存储浮点数 char op[1010];//存储运算符 /*打印计算器界面*/output();while(gets(str))//输入计算内容 {inputcolor();numtop = optop = 0;//初始化 len = strlen(str);str[++len] = '=';//在末尾添加一个 = op[optop++] = '=';//逆波兰式 = 先进栈 t = 0;exist = 0;for(i = 0; i < len;){if(str[i] == ' ')//碰到空格 {i++;continue;}if(judge(str[i])){s[t++] = str[i++];//字符串后移一位 exist = 1;//出现数字 continue;}if(exist)//出现过数字 且当前字符是运算符 {s[t] = '\0';num[numtop++] = atof(s); //模拟进栈 t = exist = 0;//初始化 } /*下面比较上一运算符 和 当前运算符优先级 *//* 若 < 进栈,若 > 计算,若 = 上一运算符出栈*/switch(compare(op[optop-1], str[i])){case '<': op[optop++] = str[i++]; break;//字符串后移一位 case '=': optop--, i++; break;//上一运算符出栈 字符串后移一位 case '>': x = num[numtop-1], numtop--; y = num[numtop-1], numtop--; z = work(y, op[optop-1], x), optop--;//用过的运算符出栈 num[numtop++] = z; break;//新浮点数进栈 } }outputcolor();printf("%.6lf\n", num[numtop-1]); }return 0;}
0 0
- 小程序之计算器 【C++ STL栈实现】 + 【C 数组模拟栈实现】 【适用VC, DEV, codeblack】
- c语言实现计算器小程序
- C语言小程序------------数组实现栈功能(一)
- 用栈实现的模拟计算器程序
- 【C++】模拟实现栈
- C语言-计算器实现(Dev c++编译通过)
- C语言利用栈实现Polish计算器
- c语言计算器(栈实现)
- C语言栈实现逆波兰计算器
- 【C++】C++顺序栈实现的计算器
- C语言栈实现表达式计算器
- c用栈实现逆波兰计算器
- 模拟实现C++STL中的string类
- 【C++】模拟实现STL中的list
- c语言实现数据结构中的栈(数组模拟)
- c语言实现计算器
- C++-------实现计算器
- 简单计算器实现(C++)
- Call to localhost/127.0.0.1:9000 failed on connection exception:java.net.ConnectException
- 使用JqGrid列表数据分组显示问题的解决方法
- 【leetcode】Unique Binary Search Trees II
- 分治法——算法总结二
- reids翻译_开始
- 小程序之计算器 【C++ STL栈实现】 + 【C 数组模拟栈实现】 【适用VC, DEV, codeblack】
- vs2013 boost 安装问题若干
- Android之窗口样式设置
- 一个Linux中用于监控的简易shell脚本
- C积累
- JAVA 面向对象和集合知识点总结
- 第三章 44题
- DataGridView中的常用技巧
- 哈希(4) - 求两个链表的交集(intersection)以及并集(union)