主题:计算器的几种实现
这次的编写计算器的作业,我实现了3个版本.
文件夹1中的是用C++中的栈实现的, 但是它有一些不足之处, 比如没有图形界面, 如果输入错误会直接
退出程序等, 也可以说是不具有界面友好型.
文件夹2中的也是用同样的方法实现的, 也就是文件夹1中的更新版, 解决了其中的一些不足之处,但由于
不会用图形界面,所以还是在命令行下执行.
最后一个JAVA 文件是用JAVA GUI 编写的一个计算器. 个人觉得执行的还是不错的.
原本还准备用数据结构中的表达式树来实现一遍, 但由于时间关系就没来得及编写.
////////////////////////////////////////////////////////////////////////////
//文件夹1
/////////////////////////////////////////////////calculator.h
#ifndef _calculator_H
#define _calculator_H
#include <iostream>
#include <ctype.h>
#include <stack>
using namespace std;
class calculator{
stack<char> sOperator;
stack<double> sOperand;
int icp(char)const;
int isp(char)const;
void putOperator(char ch){
sOperator.push(ch);}
void putOperand(double oper1){
sOperand.push(oper1);
}
bool getOperand(double&, double&);
bool getOperator(char&);
void doOperand(char);
public:
void run();
double getResult()const{
if(sOperand.size() != 1){
cerr<<"the expression is wrong"<<endl;exit(1);
}else return sOperand.top();
}
};
#endif
////////////////////////////calculator.cpp
#include "calculator.h"
int calculator::icp(char ch)const{
switch(ch){
case ')':return 1;
case '+':return 2;
case '-':return 2;
case '*':return 4;
case '/':return 4;
case '(':return 7;
case '#':return 0;
}
}
int calculator::isp(char ch)const{
switch(ch){
case ')':return 7;
case '+':return 3;
case '-':return 3;
case '*':return 5;
case '/':return 5;
case '(':return 1;
case '#':return 0;
}
}
bool calculator::getOperand(double& oper1, double& oper2){
if(sOperand.empty()){
cerr<<"Missing operand "<<endl;
return false;
}
oper1 = sOperand.top(); sOperand.pop();
if(sOperand.empty()){
cerr<<"Missing operand "<<endl;
return false;
}
oper2 = sOperand.top(); sOperand.pop();
return true;
}
bool calculator::getOperator(char& operat){
operat = sOperator.top();sOperator.pop();
}
void calculator::doOperand(char ch){
double oper1, oper2;
if(!getOperand(oper1, oper2)) {
cerr<<"The expression is wrong"<<endl;system("PAUSE");exit(1);}
else switch(ch){
case '+':sOperand.push(oper2 + oper1);break;
case '-':sOperand.push(oper2-oper1);break;
case '*':sOperand.push(oper2*oper1);break;
case '/':if(oper1 == 0){
cerr<<"The dividend is zero"<<endl;system("PAUSE");exit(1);
}
sOperand.push(oper2/oper1);break;
}
}
void calculator::run(){
char ch;
putOperator('#');
cout<<"Please input the expression(end with '#'): ";
while(cin>>ch, ch != '#'){
if(isdigit(ch)){
double number;
cin.putback(ch);cin>>number;putOperand(number);
}else if(ch == ')'){
char y; getOperator(y);
while(y != '('){
doOperand(y);getOperator(y);
}
}else if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='('){
char y;
getOperator(y);
while(icp(ch) < isp(y)){
doOperand(y);
getOperator(y);
}
putOperator(y);
putOperator(ch);
}else{
cerr<<"some char is wrong";system("PAUSE");exit(1);}
}
while(sOperator.top() != '#'){
char y;getOperator(y);
if(y == '(') {
cout<<"À¨ºÅ²»Æ¥Åä"<<endl;exit(1);
}
doOperand(y);
}
}
////////////main.cpp
/*
This program is a calculator. And it can complete basic arithmetic, for example 5*(4+6/2*8-9/3*2+5*8)/2+5/2#
author: Tang chaoyong 200532580087
version: 1.1
date: march 11, 2007
*/
#include <cstdlib>
#include <iostream>
#include "calculator.h"
using namespace std;
int main(int argc, char *argv[])
{
calculator obj;
obj.run();
cout<<"The result is: "<<(obj.getResult())<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
///////////////////////////////文件夹2
//////////////////Stack.h
#ifndef Stack_H
#define Stack_H
template<class type>
class Stack{
type* s;
int sTop;
int capation;
public:
Stack(){
capation = 100;
s = new type[capation];
sTop = -1;}
~Stack(){
delete [] s;
}
type pop(){
type temp = s[sTop--];
return temp;}
type top(){
return s[sTop];}
void push(const type&);
bool empty()const{
return (sTop == -1);}
int size()const{
return (sTop + 1);}
};
/*template<class type>type Stack::pop(){
type temp = s[sTop--];
return temp;}*/
template<class type>void Stack<type>::push(const type& t){
if(sTop == capation-1){
capation *= 2;
auto type *p = new type[capation];
delete [] s;
s = p;}
s[++sTop] = t;}
#endif
///////////////calculator.h
#ifndef calculator_H
#define calculator_H
#include <iostream>
#include <ctype.h>
#include "Stack.h"
using namespace std;
class calculator
{
Stack<char> sOperator;
Stack<double> sOperand;
int icp(char)const;
int isp(char)const;
void putOperator(char ch){
sOperator.push(ch);}
void putOperand(double oper1){
sOperand.push(oper1);
}
bool getOperand(double&, double&);
char getOperator(){
return sOperator.pop();}
bool doOperand(char);
bool operation();
public:
void run();
};
#endif
/////////////////////////////////calculator.cpp
#include "calculator.h"
int calculator::icp(char ch)const{
switch(ch){
case ')':return 1;
case '+':
case '-':return 2;
case '*':
case '/':return 4;
case '(':return 7;
case '#':return 0;
}
}
int calculator::isp(char ch)const{
switch(ch){
case ')':return 7;
case '+':
case '-':return 3;
case '*':
case '/':return 5;
case '(':return 1;
case '#':return 0;
}
}
bool calculator::getOperand(double& oper1, double& oper2){
if(sOperand.empty()){
cerr<<"Missing operand "<<endl;
return false;}
oper1 = sOperand.pop();
if(sOperand.empty()){
cerr<<"Missing operand "<<endl;
return false;}
oper2 = sOperand.pop();
return true;
}
bool calculator::doOperand(char ch){
double oper1, oper2;
if(!getOperand(oper1, oper2)){ cerr<<"The expression is wrong"<<endl;return false;}
switch(ch){
case '+':sOperand.push(oper2 + oper1);break;
case '-':sOperand.push(oper2-oper1);break;
case '*':sOperand.push(oper2*oper1);break;
case '/':if(oper1 == 0){
cerr<<"The dividend is zero"<<endl; return false;
}
sOperand.push(oper2/oper1);break;
}
return true;
}
bool calculator::operation(){
char ch;
sOperator.push('#');
cout<<"Please input the expression(end with '#'): ";
while(cin>>ch, ch != '#'){
if(isdigit(ch)){
double amount;
cin.putback(ch); cin>>amount; putOperand(amount);}
else if(ch == ')'){
char y = getOperator();
while(y != '('){
if(!doOperand(y)) return false;
y = getOperator();}
}else if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='('){
char y = getOperator();
while(icp(ch) < isp(y)){
if(!doOperand(y)) return false;
y = getOperator();}
putOperator(y);
putOperator(ch);
}else{
cerr<<"some char is wrong"<<endl;return false;
}
}
while(sOperator.size() != 1){
char y = getOperator();
if(y == '('||y == ')'){
cout<<"À¨ºÅ²»Æ¥Åä"<<endl;return false;}
if(!doOperand(y)) return false;
}
return true;
}
void calculator::run(){
int input;
do{
cout<<"[0] Quit\n[1] Input the expression\nChoice> ";
cin>>input;
if(input<0 || input>2) {
cerr<<"Invalid choice: "<<input<<endl;continue;}
if(input == 0)break;
else if(input == 1){
if(operation()){
cout<<"The result is "<<sOperand.top()<<endl;break;}
else continue;}
}while(1);
}
////////////////////main.cpp
/*
This program is a calculator. And it can complete basic arithmetic, for example 5*(4+6/2*8-9/3*2+5*8)/2+5/2#
author: Tang chaoyong 200532580087
version: 1.2
date: march 13, 2007
*/
#include "calculator.h"
int main(int argc, char* argv[]){
calculator obj;
obj.run();
system("PAUSE");
return EXIT_SUCCESS;
}
第三种方法见 http://www.programfan.com/club/showbbs.asp?id=221510
文件夹1中的是用C++中的栈实现的, 但是它有一些不足之处, 比如没有图形界面, 如果输入错误会直接
退出程序等, 也可以说是不具有界面友好型.
文件夹2中的也是用同样的方法实现的, 也就是文件夹1中的更新版, 解决了其中的一些不足之处,但由于
不会用图形界面,所以还是在命令行下执行.
最后一个JAVA 文件是用JAVA GUI 编写的一个计算器. 个人觉得执行的还是不错的.
原本还准备用数据结构中的表达式树来实现一遍, 但由于时间关系就没来得及编写.
////////////////////////////////////////////////////////////////////////////
//文件夹1
/////////////////////////////////////////////////calculator.h
#ifndef _calculator_H
#define _calculator_H
#include <iostream>
#include <ctype.h>
#include <stack>
using namespace std;
class calculator{
stack<char> sOperator;
stack<double> sOperand;
int icp(char)const;
int isp(char)const;
void putOperator(char ch){
sOperator.push(ch);}
void putOperand(double oper1){
sOperand.push(oper1);
}
bool getOperand(double&, double&);
bool getOperator(char&);
void doOperand(char);
public:
void run();
double getResult()const{
if(sOperand.size() != 1){
cerr<<"the expression is wrong"<<endl;exit(1);
}else return sOperand.top();
}
};
#endif
////////////////////////////calculator.cpp
#include "calculator.h"
int calculator::icp(char ch)const{
switch(ch){
case ')':return 1;
case '+':return 2;
case '-':return 2;
case '*':return 4;
case '/':return 4;
case '(':return 7;
case '#':return 0;
}
}
int calculator::isp(char ch)const{
switch(ch){
case ')':return 7;
case '+':return 3;
case '-':return 3;
case '*':return 5;
case '/':return 5;
case '(':return 1;
case '#':return 0;
}
}
bool calculator::getOperand(double& oper1, double& oper2){
if(sOperand.empty()){
cerr<<"Missing operand "<<endl;
return false;
}
oper1 = sOperand.top(); sOperand.pop();
if(sOperand.empty()){
cerr<<"Missing operand "<<endl;
return false;
}
oper2 = sOperand.top(); sOperand.pop();
return true;
}
bool calculator::getOperator(char& operat){
operat = sOperator.top();sOperator.pop();
}
void calculator::doOperand(char ch){
double oper1, oper2;
if(!getOperand(oper1, oper2)) {
cerr<<"The expression is wrong"<<endl;system("PAUSE");exit(1);}
else switch(ch){
case '+':sOperand.push(oper2 + oper1);break;
case '-':sOperand.push(oper2-oper1);break;
case '*':sOperand.push(oper2*oper1);break;
case '/':if(oper1 == 0){
cerr<<"The dividend is zero"<<endl;system("PAUSE");exit(1);
}
sOperand.push(oper2/oper1);break;
}
}
void calculator::run(){
char ch;
putOperator('#');
cout<<"Please input the expression(end with '#'): ";
while(cin>>ch, ch != '#'){
if(isdigit(ch)){
double number;
cin.putback(ch);cin>>number;putOperand(number);
}else if(ch == ')'){
char y; getOperator(y);
while(y != '('){
doOperand(y);getOperator(y);
}
}else if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='('){
char y;
getOperator(y);
while(icp(ch) < isp(y)){
doOperand(y);
getOperator(y);
}
putOperator(y);
putOperator(ch);
}else{
cerr<<"some char is wrong";system("PAUSE");exit(1);}
}
while(sOperator.top() != '#'){
char y;getOperator(y);
if(y == '(') {
cout<<"À¨ºÅ²»Æ¥Åä"<<endl;exit(1);
}
doOperand(y);
}
}
////////////main.cpp
/*
This program is a calculator. And it can complete basic arithmetic, for example 5*(4+6/2*8-9/3*2+5*8)/2+5/2#
author: Tang chaoyong 200532580087
version: 1.1
date: march 11, 2007
*/
#include <cstdlib>
#include <iostream>
#include "calculator.h"
using namespace std;
int main(int argc, char *argv[])
{
calculator obj;
obj.run();
cout<<"The result is: "<<(obj.getResult())<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
///////////////////////////////文件夹2
//////////////////Stack.h
#ifndef Stack_H
#define Stack_H
template<class type>
class Stack{
type* s;
int sTop;
int capation;
public:
Stack(){
capation = 100;
s = new type[capation];
sTop = -1;}
~Stack(){
delete [] s;
}
type pop(){
type temp = s[sTop--];
return temp;}
type top(){
return s[sTop];}
void push(const type&);
bool empty()const{
return (sTop == -1);}
int size()const{
return (sTop + 1);}
};
/*template<class type>type Stack::pop(){
type temp = s[sTop--];
return temp;}*/
template<class type>void Stack<type>::push(const type& t){
if(sTop == capation-1){
capation *= 2;
auto type *p = new type[capation];
delete [] s;
s = p;}
s[++sTop] = t;}
#endif
///////////////calculator.h
#ifndef calculator_H
#define calculator_H
#include <iostream>
#include <ctype.h>
#include "Stack.h"
using namespace std;
class calculator
{
Stack<char> sOperator;
Stack<double> sOperand;
int icp(char)const;
int isp(char)const;
void putOperator(char ch){
sOperator.push(ch);}
void putOperand(double oper1){
sOperand.push(oper1);
}
bool getOperand(double&, double&);
char getOperator(){
return sOperator.pop();}
bool doOperand(char);
bool operation();
public:
void run();
};
#endif
/////////////////////////////////calculator.cpp
#include "calculator.h"
int calculator::icp(char ch)const{
switch(ch){
case ')':return 1;
case '+':
case '-':return 2;
case '*':
case '/':return 4;
case '(':return 7;
case '#':return 0;
}
}
int calculator::isp(char ch)const{
switch(ch){
case ')':return 7;
case '+':
case '-':return 3;
case '*':
case '/':return 5;
case '(':return 1;
case '#':return 0;
}
}
bool calculator::getOperand(double& oper1, double& oper2){
if(sOperand.empty()){
cerr<<"Missing operand "<<endl;
return false;}
oper1 = sOperand.pop();
if(sOperand.empty()){
cerr<<"Missing operand "<<endl;
return false;}
oper2 = sOperand.pop();
return true;
}
bool calculator::doOperand(char ch){
double oper1, oper2;
if(!getOperand(oper1, oper2)){ cerr<<"The expression is wrong"<<endl;return false;}
switch(ch){
case '+':sOperand.push(oper2 + oper1);break;
case '-':sOperand.push(oper2-oper1);break;
case '*':sOperand.push(oper2*oper1);break;
case '/':if(oper1 == 0){
cerr<<"The dividend is zero"<<endl; return false;
}
sOperand.push(oper2/oper1);break;
}
return true;
}
bool calculator::operation(){
char ch;
sOperator.push('#');
cout<<"Please input the expression(end with '#'): ";
while(cin>>ch, ch != '#'){
if(isdigit(ch)){
double amount;
cin.putback(ch); cin>>amount; putOperand(amount);}
else if(ch == ')'){
char y = getOperator();
while(y != '('){
if(!doOperand(y)) return false;
y = getOperator();}
}else if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='('){
char y = getOperator();
while(icp(ch) < isp(y)){
if(!doOperand(y)) return false;
y = getOperator();}
putOperator(y);
putOperator(ch);
}else{
cerr<<"some char is wrong"<<endl;return false;
}
}
while(sOperator.size() != 1){
char y = getOperator();
if(y == '('||y == ')'){
cout<<"À¨ºÅ²»Æ¥Åä"<<endl;return false;}
if(!doOperand(y)) return false;
}
return true;
}
void calculator::run(){
int input;
do{
cout<<"[0] Quit\n[1] Input the expression\nChoice> ";
cin>>input;
if(input<0 || input>2) {
cerr<<"Invalid choice: "<<input<<endl;continue;}
if(input == 0)break;
else if(input == 1){
if(operation()){
cout<<"The result is "<<sOperand.top()<<endl;break;}
else continue;}
}while(1);
}
////////////////////main.cpp
/*
This program is a calculator. And it can complete basic arithmetic, for example 5*(4+6/2*8-9/3*2+5*8)/2+5/2#
author: Tang chaoyong 200532580087
version: 1.2
date: march 13, 2007
*/
#include "calculator.h"
int main(int argc, char* argv[]){
calculator obj;
obj.run();
system("PAUSE");
return EXIT_SUCCESS;
}
第三种方法见 http://www.programfan.com/club/showbbs.asp?id=221510