#include <iostream>
usingnamespace std;
int main(){
int num1, num2, ans = 0;
char oper = ' ';
cin >> num1>> oper >> num2;
//The first time
if (oper == '*')
ans =num1*num2;
elseif (oper == '+')
ans = num1+num2;
//other times
do {cin>> oper >> num2;
if (oper == '*')
ans =num2*ans;
elseif (oper == '+')
ans = num2+ans;
else
ans = (ans % num2);} while (oper!='%');
cout<<ans;
}
You are correct, my mistake, I did not realize that guy's Big Integer implementation was in C and not C++. I'll look for another solution for you. I know you can use boost libraries but I've never used them before.
You've encountered overflow. Since you're only adding positive integers and multiplying by positive integers, you can check to see if 'ans' wrapped around since the previous operation. This won't work if you incorporate division or subtraction. I had it print out the answer from the last computation and the answer from the current computation, and this it what it gave me:
5058
*
45
*
7635
Ans = 1737802350 | Old Ans = 0
+ 23
Ans = 1737802373 | Old Ans = 1737802350
* 4066
Ans = 7065904448618 | Old Ans = 1737802373
* 522
Ans = 3688402122178596 | Old Ans = 7065904448618
+ 9730
Ans = 3688402122188326 | Old Ans = 3688402122178596
* 100
Ans = 368840212218832600 | Old Ans = 3688402122188326
* 647
Ans = 17278688421070072808 | Old Ans = 368840212218832600
* 79
Ans = 18404067883738483864 | Old Ans = 17278688421070072808
* 4168
Ans = 6593080937685125824 | Old Ans = 18404067883738483864
Overflow
Press any key to continue . . .
Using a BigInt class, I got this:
5058
* 45
* 7635
Ans = 1737802350 | Old Ans =
+ 23
Ans = 1737802373 | Old Ans = 1737802350
* 4066
Ans = 7065904448618 | Old Ans = 1737802373
* 552
Ans = 3900379255637136 | Old Ans = 7065904448618
+ 9730
Ans = 3900379255646866 | Old Ans = 3900379255637136
* 100
Ans = 390037925564686600 | Old Ans = 3900379255646866
* 647
Ans = 252354537840352230200 | Old Ans = 390037925564686600
* 79
Ans = 19936008489387826185800 | Old Ans = 252354537840352230200
* 4168
Ans = 83093283383768459542414400 | Old Ans = 19936008489387826185800
* 74
Ans = 6148902970398866006138665600 | Old Ans = 83093283383768459542414400
* 66
Ans = 405827596046325156405151929600 | Old Ans = 6148902970398866006138665600
* 1626
Ans = 659875671171324704314777037529600 | Old Ans = 405827596046325156405151929600
+ 23
Ans = 659875671171324704314777037529623 | Old Ans = 659875671171324704314777037529600
* 5311
Ans = 3504599689590905504615780846319827753 | Old Ans = 659875671171324704314777037529623
* 5
Ans = 17522998447954527523078904231599138765 | Old Ans = 3504599689590905504615780846319827753
* 7926
Ans = 138887285698487585147923394939654773851390 | Old Ans = 17522998447954527523078904231599138765
+ 4
Ans = 138887285698487585147923394939654773851394 | Old Ans = 138887285698487585147923394939654773851390
+ 72
Ans = 138887285698487585147923394939654773851466 | Old Ans = 138887285698487585147923394939654773851394
* 67
Ans = 9305448141798668204910867460956869848048222 | Old Ans = 138887285698487585147923394939654773851466
+ 131
Ans = 9305448141798668204910867460956869848048353 | Old Ans = 9305448141798668204910867460956869848048222
* 6
Ans = 55832688850792009229465204765741219088290118 | Old Ans = 9305448141798668204910867460956869848048353
+ 38
Ans = 55832688850792009229465204765741219088290156 | Old Ans = 55832688850792009229465204765741219088290118
+ 878
Ans = 55832688850792009229465204765741219088291034 | Old Ans = 55832688850792009229465204765741219088290156
* 9
Ans = 502494199657128083065186842891670971794619306 | Old Ans = 55832688850792009229465204765741219088291034
+ 94
Ans = 502494199657128083065186842891670971794619400 | Old Ans = 502494199657128083065186842891670971794619306
* 876
Ans = 440184918899644200765103674373103771292086594400 | Old Ans = 502494199657128083065186842891670971794619400
+ 1053
Ans = 440184918899644200765103674373103771292086595453 | Old Ans = 440184918899644200765103674373103771292086594400
* 460
Ans = 202485062693836332351947690211627734794359833908380 | Old Ans = 440184918899644200765103674373103771292086595453
* 96
Ans = 19438566018608287905786978260316262540258544055204480 | Old Ans = 202485062693836332351947690211627734794359833908380
* 50
Ans = 971928300930414395289348913015813127012927202760224000 | Old Ans = 19438566018608287905786978260316262540258544055204480
+ 6
Ans = 971928300930414395289348913015813127012927202760224006 | Old Ans = 971928300930414395289348913015813127012927202760224000
* 805
Ans = 782402282248983588207925874977729567245406398221980324830 | Old Ans = 971928300930414395289348913015813127012927202760224006
* 4
Ans = 3129609128995934352831703499910918268981625592887921299320 | Old Ans = 782402282248983588207925874977729567245406398221980324830
+ 5861
Ans = 3129609128995934352831703499910918268981625592887921305181 | Old Ans = 3129609128995934352831703499910918268981625592887921299320
+ 470
Ans = 3129609128995934352831703499910918268981625592887921305651 | Old Ans = 3129609128995934352831703499910918268981625592887921305181
+ 712
Ans = 3129609128995934352831703499910918268981625592887921306363 | Old Ans = 3129609128995934352831703499910918268981625592887921305651
+ 6
Ans = 3129609128995934352831703499910918268981625592887921306369 | Old Ans = 3129609128995934352831703499910918268981625592887921306363
* 2720
Ans = 8512536830868941439702233519757697691630021612655145953323680 | Old Ans = 3129609128995934352831703499910918268981625592887921306369
* 61
Ans = 519264746683005427821836244705219559189431318371963903152744480 | Old Ans = 8512536830868941439702233519757697691630021612655145953323680
+ 98
Ans = 519264746683005427821836244705219559189431318371963903152744578 | Old Ans = 519264746683005427821836244705219559189431318371963903152744480
* 135
Ans = 70100740802205732755947893035204640490573227980215126925620518030 | Old Ans = 519264746683005427821836244705219559189431318371963903152744578
* 721
Ans = 50542634118390333317038430878382545793703297373735106513372393499630 | Old Ans = 70100740802205732755947893035204640490573227980215126925620518030
* 9015
Ans = 455641846577288854853101454368618650330235225824221985218052127399164450 | Old Ans = 50542634118390333317038430878382545793703297373735106513372393499630
* 6979
Ans = 3179924447262898918019795050038589560654711641027245234836785797118768696550 | Old Ans = 455641846577288854853101454368618650330235225824221985218052127399164450
* 6414
Ans = 20396035404744233660178965450947513442039320465548750936243144102719782419671700 | Old Ans = 3179924447262898918019795050038589560654711641027245234836785797118768696550
+ 2187
Ans = 20396035404744233660178965450947513442039320465548750936243144102719782419673887 | Old Ans = 20396035404744233660178965450947513442039320465548750936243144102719782419671700
* 223
Ans = 4548315895257964106219909295561295497574768463817371458782221134906511479587276801 | Old Ans = 20396035404744233660178965450947513442039320465548750936243144102719782419673887
+ 1
Ans = 4548315895257964106219909295561295497574768463817371458782221134906511479587276802 | Old Ans = 4548315895257964106219909295561295497574768463817371458782221134906511479587276801
* 273
Ans = 1241690239405424200998035237688233670837911790622142408247546369829477633927326566946 | Old Ans = 4548315895257964106219909295561295497574768463817371458782221134906511479587276802
* 7023
Ans = 8720390551344294163609201474284465070294654505539306133122518155312421423071614479661758 | Old Ans = 1241690239405424200998035237688233670837911790622142408247546369829477633927326566946
% 6661
Ans = 2560 | Old Ans = 8720390551344294163609201474284465070294654505539306133122518155312421423071614479661758
Got Expected Result
Press any key to continue . . .
If you reach the maximum size you can store in a variable and then make that bigger, it will wrap around. If 1000 was the max for an unsigned integer, then adding 1 to it would make it wrap around and be equal to 0.
In this case, you are only adding and multiplying positive numbers. Because of that, we are guaranteed that the number we get after the operation will not be smaller than the one that we had before the operation. IF we a get a smaller number than what we had before, then that signals that overflow occurred. This method will not work if you start subtracting or dividing.
so.... what can I do for the overflow. Let's suppose I can detect when I overflow. What do I do? How do I prevent it from reaching so high it will exceed int?