1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
#include <iostream>
#include <cstdlib>
#include <sstream>
#include <string>
#include <cmath>
#include <cstring>
#include <stack>
#include <algorithm>
using namespace std;
class Calculator
{
public:
Calculator();
virtual ~Calculator();
void readEquation();
float nextNumber(int position, int*& );
string equation;
protected:
float add(float firstNumber, float secondNumber);
float subtract (float firstNumber, float secondNumber);
float multiply (float number, float multiple);
float divide (float numerator, float denominator);
float squareRoot(float number);
float square(float number);
private:
void temporaryPrintStack();
float answer();
stack<float>* numberStack;
};
Calculator::Calculator(){
numberStack = new stack<float>();
cout<<"Stack created.\n";
}
Calculator::~Calculator(){
delete numberStack;
numberStack=NULL;
cout<<"Stack destroyed.\n";
}
float Calculator::add(float numOne,float numTwo){
cout<<"Calculating "<<numOne<<" + "<<numTwo<<".\n";
return numOne+numTwo;
}
float Calculator::answer(){
return this->numberStack->top();
}
void Calculator::temporaryPrintStack()
{
cout<<"Stack: ";
for(int index = 0; index <= (int) this->numberStack->size();index++)
{
cout<<this->numberStack->top()<<", ";
this->numberStack->pop();
}
cout<<endl;
}
float Calculator::nextNumber(int position,int*& returnPosition)
{
stringstream convertCharacterTokensToString;
for(string::iterator next = equation.begin()+position+1;next!=equation.end();next++)
{
if(*next >= '0' && *next<='9')
{
convertCharacterTokensToString<<*next;
}
if(*next == '+' || *next == '-' || *next=='=')
{
string nextNumber;
convertCharacterTokensToString>>nextNumber;
cout<<"The next number is: "<<nextNumber<<".\n";
*returnPosition = std::distance(this->equation.begin(),next);//ACCESS VIOLATION: SEGMENTATION FAULT
cout<<"The next number is: "<<nextNumber<<".\n";
return atof(nextNumber.c_str());
}
}
}
void Calculator::readEquation()
{
stringstream convertCharacterTokensToString;
//add code to remove all spaces
for(string::iterator iterator = this->equation.begin();iterator!=this->equation.end();iterator++)
{
if(*iterator>='0' && *iterator<='9' || *iterator=='.')
convertCharacterTokensToString<<*iterator;
if(*iterator=='+')
{
string number;
convertCharacterTokensToString>>number;
cout<<number<<" pushed to stack."<<endl;
this->numberStack->push(atof(number.c_str()));
int* returnPosition;
this->numberStack->push(this->add(this->numberStack->top(),
this->nextNumber(std::distance(this->equation.begin(),iterator), returnPosition)));
this->temporaryPrintStack();
cout<<"The return character is: "<<*returnPosition<<".\n";
}
if(*iterator=='=')
{
this->answer();
}
}
}
int main()
{
Calculator *c=new Calculator();
std::cout<<"Enter sum: ";
getline(cin,c->equation);
c->readEquation();
system("pause");
delete c; c = NULL;
return 0;
}
|