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;
}

Recommended Answers

All 7 Replies

Can you explain a little more on what the problem is, and how you determine its a problem?

Sorry guys for unclear post > <
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...
The input is:
126456789 + 23

and the output is its sum
The output I see is:
987654621
32
126456812
********* (some ascii characters between 0 and 31)

I dont agree with the logic of your + operator. I would do something of this sort

while(A.top!=NULL && B.top!=NULL)
{
     sum= (A.top)->data + (B.top)->data;
     Z.push(sum);
}

while(A.top!=NULL)
Z.push((A.top)->data);

while(B.top!=NULL)
Z.push((B.top)->data);

So if the input is 123+ 50, you will have some thing of this sort
Stack 1: 1,2,3
Stack 2: 5,0
Stack 3: (3+0),(2+5),(1)

Now you can figure out how to display the result

Z can't push a number, it can only put a digit, so if the stack data is 8 and 9, your "sum" will be 17 and it will goes wrong.
Thanks for your concern but what I mean is I can't return Z for C in function main(). The algorithm isn't wrong!

My algorithm is not wrong. I just showed you a simple scenario. If you want to do 8+9 then have a if condition after the summation and handle it there
Also when you return from the function I think you should write

return z;

instead of

return stack(z);

I used this first time and it didn't work too :((

OH finally I found it!!!! I forgot to write overloaded operator = to perform assignment so C = A + B could work. Question answered!!! =))...By me =))

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.