Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
For my programming class I need to make a super calculator that allows me to multiply, divide, add, subtract, compare, and calculate factorials of big numbers. I got the code to compile without errors and everything works except for the compare function. When I input two numbers and make "compare" my operation, the calculator outputs the first number I input. Any suggestions with this?

Thank you!!

Updated: Made the compare function an int instead of a void function and changed some lines around in the input function that weren't in the right spots, this is why I wasn't getting any output from the calculator. Now everything works except the compare function's output isn't working correctly.

struct BigNumber
{   int * digitos;
    int size;
    bool pos;
    BigNumber()
   { digitos = new int[1];
     digitos [0] = 0;
     size = 1;
     pos = true;
   }

int Compare(BigNumber b)  
{
    if(pos && !b.pos)
      return 1;
    if(!pos && b.pos)
      return -1;
    if(size > b.size)
      return 1;
    else if(b.size > size)
      return -1;
    for(int i = size-1; i >= 0; i--)
       {
         if(digitos[i]>b.digitos[i])
           {
              if(pos)
                return 1;
              else
                return -1;
           }   
         else if(b.digitos[i] > digitos[i])
           { 
              if( pos)
                return -1;
              else
                return 1;
           }
       }
    return 0;
}       

void input()
{  string n1, n2, op;
    while (true)
  {
     cout << "1st #? ";
     cin >> n1;
     cout<< "2nd #? ";
     cin >> n2;
     cout << "Operation? ";
     cin >> op;
     BigNumber a,b;
     a.set(n1);
     b.set(n2);
     if (op == "+")
        {  if (a.pos && b.pos)
              a.Add(b);
           else if (a.pos)
              a.Subtract(b);
           else if (b.pos)
              b.Subtract(a);
           else
              a.Add(b);
        } //both are negative
     if (op == "-")
        {  if (a.pos && b.pos)
              a.Subtract(b);
           else if (a.pos)
              a.Add(b);
           else if (b.pos)
           {  a.Add(b);
           a.pos = false;}
           else
           {  a.Subtract(b);
              a.pos = !a.pos;}
     }
     if (op == "*")
     {  int B = toInt(n2);
        a.Multiply(B);
     }
     if (op == "/")
     {  int B = toInt(n2);
        a.Divide(B);
     }
     if (op == "!")
     {
        a.Factorial(toInt(n1));
     }
      if (op == "Compare")
      {
         a.Compare(b);
      }
    a.print();
    cout << "\n";}
}
    
void main()
{
input();
}
Posted
Updated 21-Apr-12 13:22pm
v5
Comments
Nelek 21-Apr-12 17:54pm    
That looks like the whole programm, please use "improve question" and edit the code. Delete the functions that are already working and leave only the parts that are giving you problems. The easier you make it for us to read, the more probability you will have to get answers.
Member 8863422 21-Apr-12 18:04pm    
Thank you for that advice! I just deleted all the unnecessary code.
Nelek 21-Apr-12 18:11pm    
Much better :) Let's take a look
Nelek 21-Apr-12 18:15pm    
I don't know if it is a typo here, or in your programm but... you are giving the << and >> false in your Input function, when asking for the 2nd number
Member 8863422 21-Apr-12 18:20pm    
That is definitely a late-night programming typo!

1 solution

I must discourage you a bit: such big integer type with all required arithmetic already exists; this is the wonderful structure System.Numerics.BigInteger first introduced in .NET Framework v.4.0; please see:
http://msdn.microsoft.com/en-us/library/system.numerics.biginteger.aspx[^].

Even if you cannot use v.4.0 by some reason, you get the source code of this type; it is fully implemented in Mono:
http://en.wikipedia.org/wiki/Mono_%28software%29[^],
http://www.mono-project.com/[^].

—SA
 
Share this answer
 
Comments
Nelek 21-Apr-12 18:33pm    
His first sentence is: "For my programming class I need to make a super calculator..."
So I think it is just for learning purpose :)
Sergey Alexandrovich Kryukov 23-Apr-12 23:21pm    
Ye-e, super... :-)
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900