Please help me solve this problem, I've spent 2 days to search for what error is but I can't!!! The operator + return error object though this object was still right before it is returned. Maybe it's because of the copy constructor but I find no doubt in its syntax...
Here is just a beta ver so it could be quite coarse ;))
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
struct StackFrame
{
char data;
StackFrame * link;
};
typedef StackFrame * StackFramePtr;
class Stack
{
public:
Stack();//default constructor
Stack(const Stack & a_stack);
~Stack();//delete the object
//void input(ifstream & in);
void push(const char & symbol); //insert new char
char top_data();// return the data of the first node
char pop();//delete the first node and return its data
bool empty(); //check if the Stack object is empty
void print(ostream & out);//print out
friend Stack operator +(const Stack & A, const Stack & B);
//friend Stack operator *(const Stack & A, const Stack & B);
private:
StackFramePtr top;
};
Stack::Stack(): top(NULL)
{
}
Stack::~Stack()
{
char next;
while (!empty())
next = pop();
}
Stack::Stack(const Stack & a_stack): top(NULL)
{
StackFramePtr temp1, temp2, temp3;
temp1 = new StackFrame;
this->top = temp1;
temp2 = a_stack.top;
while(temp2 != NULL)
{
temp1->data = temp2->data;
temp1->link = new StackFrame;
temp3 = temp1;
temp1 = temp1->link;
temp2 = temp2->link;
}
temp3->link = NULL;
}
void Stack::push(const char & symbol)
{
StackFramePtr current;
current = new StackFrame;
if (current == NULL)
{
cout << "out of memory" <<endl;
exit(1);
}
current->data = symbol;
current->link = top;
top = current;
return;
}
/*void Stack::input(ifstream & in)
{
string lineA;
char * p = & lineA[0];
getline(in, lineA, ' ');
lineA = lineA + '.';
while(*p != '.')
{
cout<<*p<<endl;
push(*p);
p++;
}
return;
}
*/
bool Stack::empty()
{
return(top == NULL);
}
char Stack::pop()
{
if (empty())
{
cout << "popping an empty stack" << endl;
exit(1);
}
char result;
StackFramePtr temp;
result = top->data;
temp = top;
top = top->link;
delete temp;
return result;
}
void Stack::print(ostream & out)
{
StackFramePtr temp = top;
while(temp != NULL)
{
out << temp->data;
temp = temp->link;
}
out << endl;
return;
}
char Stack::top_data()
{
if(empty()) return 'a';
return(top->data);
}
Stack operator +(const Stack & A, const Stack & B)
{
Stack Z;
int sum , module = 0;
StackFramePtr tempA = A.top;
StackFramePtr tempB = B.top;
while((tempA != NULL) || (tempB != NULL))
{
if(tempA == NULL)
{
sum = tempB->data - int('0');
tempB = tempB->link;
}
else if(tempB == NULL)
{
sum = tempA->data - int('0');
tempA = tempA->link;
}
else
{
sum = tempA->data + tempB->data - int('0') *2;
tempA = tempA->link;
tempB = tempB->link;
}
Z.push((sum + module) % 10 + '0');
module = (sum + module) / 10;
}Z.print(cout);
return Stack(Z);
}
int main()
{
ifstream in("bignumber.inp");
ofstream out("bignumber.out");
Stack A,B,C;
int n;
char operato, *p;
string a;
getline(in, a, ' ');
a += '.';
p = & a[0];
while(*p != '.')
{
A.push(*p);
p++;
}
in >> operato;
getline(in, a);
a += '.';
p = &a[1];
while(*p != '.')
{
B.push(*p);
p++;
}
A.print(cout);
B.print(cout);
if(operato == '+')
C = A + B;
//else if(operator == '*')
// C = A * B;
C.print(cout);
in.close();
out.close();
system("pause");
return 0;
}