Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Adding BigInt's

Adding BigInt's

Scheduled Pinned Locked Moved C / C++ / MFC
graphicsdata-structuresjsonquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TannerB
    wrote on last edited by
    #1

    //WTF?
    BigInt BigInt::add_bigint(BigInt add)
    {
    int carry = 0;
    int temp;
    bool carry2;
    stack<int> answer;
    vector<int> tempvector;
    vector<int> add_int = add.get_bigint();

    while(bigint\_int.size() > add\_int.size())
         add\_int.insert(add\_int.begin(), 1, 0);
    
    while(bigint\_int.size() < add\_int.size())
         bigint\_int.insert(bigint\_int.begin(), 1, 0);
    cout<<add\_int.size()<<" "<<bigint\_int.size()<<"\\n";
    for(int i = size(); i > 0 ;i--) //<------if I do size - 1 here
    {                                  //the first addition works but not the rest
        temp = add\_int\[i\] + bigint\_int\[i\];
        if(temp >= 10)
        {
            temp -= 10;
            carry2 = true;
        }
        else
            carry2 = false;
    
        answer.push(temp + carry);
        
        if(carry2)
            carry = 1;
        else
            carry = 0;
    }
    
    if(carry)
        answer.push(carry);
        
    while(!answer.empty())
    {
        tempvector.push\_back(answer.top());
        answer.pop();
    }
    BigInt ret(tempvector);
    return ret;
    

    }

    J 1 Reply Last reply
    0
    • T TannerB

      //WTF?
      BigInt BigInt::add_bigint(BigInt add)
      {
      int carry = 0;
      int temp;
      bool carry2;
      stack<int> answer;
      vector<int> tempvector;
      vector<int> add_int = add.get_bigint();

      while(bigint\_int.size() > add\_int.size())
           add\_int.insert(add\_int.begin(), 1, 0);
      
      while(bigint\_int.size() < add\_int.size())
           bigint\_int.insert(bigint\_int.begin(), 1, 0);
      cout<<add\_int.size()<<" "<<bigint\_int.size()<<"\\n";
      for(int i = size(); i > 0 ;i--) //<------if I do size - 1 here
      {                                  //the first addition works but not the rest
          temp = add\_int\[i\] + bigint\_int\[i\];
          if(temp >= 10)
          {
              temp -= 10;
              carry2 = true;
          }
          else
              carry2 = false;
      
          answer.push(temp + carry);
          
          if(carry2)
              carry = 1;
          else
              carry = 0;
      }
      
      if(carry)
          answer.push(carry);
          
      while(!answer.empty())
      {
          tempvector.push\_back(answer.top());
          answer.pop();
      }
      BigInt ret(tempvector);
      return ret;
      

      }

      J Offline
      J Offline
      josda1000
      wrote on last edited by
      #2

      This might be the problem: for(int i = size(); i > 0 ;i--) should be for(int i = size() -1; i > -1 ;i--) I believe this is the problem, hopefully...

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups