EE_wanna_be
asked on
I need assistance with my C++ calculator code...there has to be an easier way
I am writing code for a big integer calculator...this seems to be a hot topic. The code below is what I have started with. I am getting stumped when it comes to how to get this program to output a large number without eliminating the extra zeros. ex...100000000 + 100000000. This program is only a rough draft (not too user friendly). Essetially it takes input in type char - changes it to sets of 3 int. then adds the sets of 3 int then outputs into someform...this is where I have the problem.
Thanks for your help..
ps...the extra cout's are for debugging only. I have not removed them because I have not figured out
how to make this thing work yet.
/*
*
*
************************** ********** ********** ********** ********** ********** ***/
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
int charToInt(char a);
int main()
{
cout << "Enter a really big number: ";
char num;
vector<char> bigNum;
vector<char> bigNum1;
vector<int> bigIntX;
vector<int> bigIntY;
vector<int> bigAnswer;
ostringstream ostr;
int sum = 0, y = 0, x = 0,
bigSum = 0, rem = 0,
repeat = 1;
for(;;)
{
cin >> num;
if (num == '+' ) break;
bigNum.push_back(num);
}
int find = bigNum.size() - 1;
for (int n = 0; n <= find ; n++)
{
char numb = bigNum[find - n];
x = x * 10;
if (x < 1)
x++;
sum += x * (charToInt(numb));
if (repeat == 3)
{
if ( cin.eof())break;
bigIntX.push_back(sum);
sum = 0;
x = 0;
repeat = 0;
}
if (n == find && repeat != 3 && sum > 0)
bigIntX.push_back(sum);
cout << repeat;
repeat++;
}
cout << "\nEnter another big number: ";
for(;;)
{
cin >> num;
if (num == '+' ) break;
bigNum1.push_back(num);
}
int find2 = bigNum1.size() - 1;
sum = 0;
x = 0;
repeat = 1;
for (int m = 0; m <= find2 ; m++)
{
char numb = bigNum1[find2 - m];
x = x * 10;
if (x < 1)
x++;
sum += x * (charToInt(numb));
if (repeat == 3)
{
if ( cin.eof())break;
bigIntY.push_back(sum);
sum = 0;
x = 0;
repeat = 0;
}
if (m == find2 && repeat != 3 && sum > 0)
bigIntY.push_back(sum);
cout << repeat << " " << bigIntY.size() << "bsize ";
repeat++;
}
int sizeBig = 0;
if (bigIntX.size() >= bigIntY.size())
sizeBig = bigIntX.size() - 1;
if (bigIntY.size() > bigIntX.size())
sizeBig = bigIntY.size() - 1;
cout << " " << sizeBig << "szbig";
for (int i = 0; i <= sizeBig ; i++)
{
bigSum = bigIntX[i] + bigIntY[i] + rem;
rem = 0;
if (bigSum > 1000){
bigSum = bigSum - 1000;
rem++;}
if (bigSum < 1){
bigAnswer.push_back(0);}
else if (bigSum < 10){
cout << "not supposed to come out here";
bigAnswer.push_back(bigSum );}
else if (bigSum < 100){
bigAnswer.push_back(bigSum );}
else
bigAnswer.push_back(bigSum );
cout << bigSum << "\n";
}
for (int k = sizeBig; k >= 0 ; k--)
cout << bigAnswer[k];
}
/************************* ********** ********** ********** ********** ********** ********** ***
************************** ********** ********** ********** ********** ********** ********** **/
int charToInt(char a)
{
switch (a)
{
case '1':
return 1;
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
case '0':
return 0;
default:
cerr << "\nThere was an error in the number entered.";
}
}
Thanks for your help..
ps...the extra cout's are for debugging only. I have not removed them because I have not figured out
how to make this thing work yet.
/*
*
*
**************************
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
int charToInt(char a);
int main()
{
cout << "Enter a really big number: ";
char num;
vector<char> bigNum;
vector<char> bigNum1;
vector<int> bigIntX;
vector<int> bigIntY;
vector<int> bigAnswer;
ostringstream ostr;
int sum = 0, y = 0, x = 0,
bigSum = 0, rem = 0,
repeat = 1;
for(;;)
{
cin >> num;
if (num == '+' ) break;
bigNum.push_back(num);
}
int find = bigNum.size() - 1;
for (int n = 0; n <= find ; n++)
{
char numb = bigNum[find - n];
x = x * 10;
if (x < 1)
x++;
sum += x * (charToInt(numb));
if (repeat == 3)
{
if ( cin.eof())break;
bigIntX.push_back(sum);
sum = 0;
x = 0;
repeat = 0;
}
if (n == find && repeat != 3 && sum > 0)
bigIntX.push_back(sum);
cout << repeat;
repeat++;
}
cout << "\nEnter another big number: ";
for(;;)
{
cin >> num;
if (num == '+' ) break;
bigNum1.push_back(num);
}
int find2 = bigNum1.size() - 1;
sum = 0;
x = 0;
repeat = 1;
for (int m = 0; m <= find2 ; m++)
{
char numb = bigNum1[find2 - m];
x = x * 10;
if (x < 1)
x++;
sum += x * (charToInt(numb));
if (repeat == 3)
{
if ( cin.eof())break;
bigIntY.push_back(sum);
sum = 0;
x = 0;
repeat = 0;
}
if (m == find2 && repeat != 3 && sum > 0)
bigIntY.push_back(sum);
cout << repeat << " " << bigIntY.size() << "bsize ";
repeat++;
}
int sizeBig = 0;
if (bigIntX.size() >= bigIntY.size())
sizeBig = bigIntX.size() - 1;
if (bigIntY.size() > bigIntX.size())
sizeBig = bigIntY.size() - 1;
cout << " " << sizeBig << "szbig";
for (int i = 0; i <= sizeBig ; i++)
{
bigSum = bigIntX[i] + bigIntY[i] + rem;
rem = 0;
if (bigSum > 1000){
bigSum = bigSum - 1000;
rem++;}
if (bigSum < 1){
bigAnswer.push_back(0);}
else if (bigSum < 10){
cout << "not supposed to come out here";
bigAnswer.push_back(bigSum
else if (bigSum < 100){
bigAnswer.push_back(bigSum
else
bigAnswer.push_back(bigSum
cout << bigSum << "\n";
}
for (int k = sizeBig; k >= 0 ; k--)
cout << bigAnswer[k];
}
/*************************
**************************
int charToInt(char a)
{
switch (a)
{
case '1':
return 1;
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
case '0':
return 0;
default:
cerr << "\nThere was an error in the number entered.";
}
}
ASKER
I cannot get the output right....when I input a number with alot of zeros the program either times out or it puts out a much smaller number ex. 100000000000+ 1000000000000 ends up being 2000 or <vector out of range>..
ASKER
when the vectors add in the code:
bigSum = bigIntX[i] + bigIntY[i] + rem;
rem = 0;
bigSum == 0 -- ends up bieng zero when I need it to be 000
know what i mean?
2 - 000 - 000 - 000 is what I need
2 - 0 - 0 - 0 is what I get
I am not sure how to do this...
I will eventually make a class with all of this stuff, but I need to work out the bugs first..
bigSum = bigIntX[i] + bigIntY[i] + rem;
rem = 0;
bigSum == 0 -- ends up bieng zero when I need it to be 000
know what i mean?
2 - 000 - 000 - 000 is what I need
2 - 0 - 0 - 0 is what I get
I am not sure how to do this...
I will eventually make a class with all of this stuff, but I need to work out the bugs first..
>> bigSum == 0 -- ends up bieng zero when I need it to be 000
bigSum is an int. You can't store 000 in an int.
You can solve that when you output though :
#include <iomanip> // <--- this include is needed for setfill and setw
cout << setfill('0') << setw(3) << bigSum;
will show 000 if bigSum is 0.
Reference pages :
http://www.cplusplus.com/reference/iostream/manipulators/setfill.html
http://www.cplusplus.com/reference/iostream/manipulators/setw.html
bigSum is an int. You can't store 000 in an int.
You can solve that when you output though :
#include <iomanip> // <--- this include is needed for setfill and setw
cout << setfill('0') << setw(3) << bigSum;
will show 000 if bigSum is 0.
Reference pages :
http://www.cplusplus.com/reference/iostream/manipulators/setfill.html
http://www.cplusplus.com/reference/iostream/manipulators/setw.html
ASKER
that did something but not what I was looking for...now I am getting 002200000, it looks like somekind of progress... here is what I changed in the code (is this correct?):
for (int i = 0; i <= sizeBig ; i++)
{
bigSum = bigIntX[i] + bigIntY[i] + rem;
rem = 0;
if (bigSum > 1000){
bigSum = bigSum - 1000;
rem++;}
bigAnswer.push_back(bigSum );
cout << bigSum << "\n";
}
cout << setfill('0') << setw(3) << bigSum;
for (int k = sizeBig; k >= 0 ; k--)
cout << bigAnswer[k];
for (int i = 0; i <= sizeBig ; i++)
{
bigSum = bigIntX[i] + bigIntY[i] + rem;
rem = 0;
if (bigSum > 1000){
bigSum = bigSum - 1000;
rem++;}
bigAnswer.push_back(bigSum
cout << bigSum << "\n";
}
cout << setfill('0') << setw(3) << bigSum;
for (int k = sizeBig; k >= 0 ; k--)
cout << bigAnswer[k];
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ok...I put it in the wrong place. I think it will work here:
for (int k = sizeBig; k >= 0 ; k--)
cout << setfill('0') << setw(3)<< bigAnswer[k];
but for example when I add 1000 + 1000 I get 002000... how do I fix that problem?
for (int k = sizeBig; k >= 0 ; k--)
cout << setfill('0') << setw(3)<< bigAnswer[k];
but for example when I add 1000 + 1000 I get 002000... how do I fix that problem?
>> but for example when I add 1000 + 1000 I get 002000... how do I fix that problem?
See my last post ;)
See my last post ;)
ASKER
I am using this format because this is the way I was requested to do it....I think it is a little ridiculous myself...I am just doing as requested.
thank you for the charToInt simplification....I will try it out
thank you for the charToInt simplification....I will try it out
ASKER
Thank You!!!..I am sure there will be more where that came from...
What's the problem exactly ?