计算器——C++实现
来源:互联网 发布:网站seo分析工具 编辑:程序博客网 时间:2024/05/30 04:12
//实现了+ - * /功能
#include <iostream>#include <cctype>#include <cstdlib>using namespace std;void eatspaces(char* str);double expr(char* str);double term(char* str,int & index);double number(char* str,int & index);const int MAX=80;int main(void){char buffer[MAX]={0};cout<<endl<<"Welcome to your friendly calculator"<<endl<<"enter an expression,or an empty line to quit"<<endl;for(;;){cin.getline(buffer,sizeof buffer);eatspaces(buffer);if(!buffer[0])return 0;cout<<"\t="<<expr(buffer)<<endl;}}void eatspaces(char* str){int i=0;int j=0;while((*(str+i)=*(str+j++))!='\0')if(*(str+i)!=' ')i++;return ;}double expr(char* str){double value=0.0;int index=0;value=term(str,index);for(;;){switch(*(str+index++)){case '\0':return value;case '+':value=value+term(str,index);break;case '-':value=value-term(str,index);break;default:cout<<endl<<"Baby,There is an error"<<endl;exit(1);} }}double term(char* str,int & index){double value=0.0;value=number(str,index);while(true){if(*(str + index)=='*')value*=number(str,++index);else if(*(str + index)=='/')value/=number(str,++index);elsebreak;}return value;}double number(char* str,int & index){double value=0.0;if(!isdigit(*(str+index))){cout<<endl<<"Baby!!! There is an error"<<endl;exit(1);}while(isdigit(*(str + index)))value=10*value + (*(str + index++)-'0');if(*(str + index)!='.')return value;double factor=1.0; while(isdigit(*(str + (++index)))){factor*=0.1;value=value + (*(str + index)-'0')*factor;}return value;}
0 0
- C/C++ — 实现日期计算器
- c语言实现计算器
- C++-------实现计算器
- 简单计算器实现(C++)
- 计算器实现(c++)
- C语言实现计算器
- 计算器——C++实现
- Android学习—计算器实现
- ANTLR实现简单计算器[C#]
- 【C#】简易网页计算器实现
- 《简单计算器实现(c#)》
- c/c++实现简单计算器
- C语言实现大数计算器
- C语言模块化开发简单实例——实现一个计算器的加减乘除
- C语言——使用函数指针数组实现简单的计算器
- shell命令实现——bc计算器
- 计算器——C++实现2
- 急求一用C编程实现多功能计算器
- ubuntu14.04 IBUS 中文输入法的设置框 跟随设置
- Android APP架构心得
- Android线程间的通信
- Android手机截屏并上传到电脑
- Android 生成so
- 计算器——C++实现
- 计算回文子序列(Java语言,穷举法,递归)
- mac brew安装遇到的找不到: command not found
- SAS里INDEX, INDEXC和SCAN的区别
- maven实现storm实例项目
- oracle中普通用户如何查看自己登录的数据库名称?这个用户与scott权限相同,无法切换到sys下面!没有权限查看v$database和v$instance
- 比较分析 Spring AOP 和 AspectJ 之间的差别
- select from where 格式详解
- Jetbrains tomcat css文件路径错误