I am attempting to write a program that adds 50 digit numbers read from a .txt file, outputting the first approximately 10 integers of the sum in the end.
I am using gcc in Code::Blocks in 64-bit Windows.
Debugging got as far as line 76. After going through to_letters() it seems to jump back into main function at 'running+=addend".
Continuing debugger step by step, the position arrow disappears and I get:
Cannot open file: ../../../../src/gcc-4.7.1/libgcc/unwind-sjlj.c
At ../../../../src/gcc-4.7.1/libgcc/unwind-sjlj.c:127
However it has not crashed yet, so I keep on truckin' and get many other messages with each step further:
"Multiple information windows with the same message have been surpressed"
In TlsGetValue@4 () ()
611 ./gthr-default.h: No such file or directory.
#1 0x0041764a in __gthread_getspecific (__key=1) at ./gthr-default.h:611
In QueueUserWorkItem () (C:\Windows\syswow64\kernel32.dll)
#1 0x0041764a in __gthread_getspecific (__key=1) at ./gthr-default.h:611
611 in ./gthr-default.h
Cannot open file: ./gthr-default.h
At ./gthr-default.h:613
In SetLastError@4 () ()
#1 0x00417655 in __gthread_getspecific (__key=<optimized out>) at ./gthr-default.h:613
613 in ./gthr-default.h
In QueueUserWorkItem () (C:\Windows\syswow64\kernel32.dll)
#1 0x00417655 in __gthread_getspecific (__key=<optimized out>) at ./gthr-default.h:613
613 in ./gthr-default.h
In ntdll!RtlpNtEnumerateSubKey () (C:\Windows\system32\ntdll.dll)
#1 0x00417655 in __gthread_getspecific (__key=<optimized out>) at ./gthr-default.h:613
613 in ./gthr-default.h
Cannot open file: ../../../../src/gcc-4.7.1/libgcc/unwind-sjlj.c
At ../../../../src/gcc-4.7.1/libgcc/unwind-sjlj.c:134
In TlsSetValue@8 () ()
621 ./gthr-default.h: No such file or directory.
#1 0x0041766f in __gthread_setspecific (__ptr=0x27fbac, __key=<optimized out>) at ./gthr-default.h:621
#1 0x0041766f in __gthread_setspecific (__ptr=0x27fbac, __key=<optimized out>) at ./gthr-default.h:621
621 in ./gthr-default.h
Cannot open file: ../../../../src/gcc-4.7.1/libgcc/unwind-sjlj.c
At ../../../../src/gcc-4.7.1/libgcc/unwind-sjlj.c:143
At ../../../../src/gcc-4.7.1/libgcc/unwind-sjlj.c:143
In operator new(unsigned int) () ()
Program received signal SIGTRAP, Trace/breakpoint trap.
In ntdll!RtlDosPathNameToRelativeNtPathName_U_WithStatus () (C:\Windows\system32\ntdll.dll)
In __cxa_throw () ()
#2 0x00401a2f in main () at C:\Users\Hume Dog Jr\Desktop\PE13\main.cpp:185
C:\Users\Hume Dog Jr\Desktop\PE13\main.cpp:185:4871:beg:0x401a2f
At C:\Users\Hume Dog Jr\Desktop\PE13\main.cpp:185
#2 0x00401a2f in main () at C:\Users\Hume Dog Jr\Desktop\PE13\main.cpp:185
C:\Users\Hume Dog Jr\Desktop\PE13\main.cpp:185:4871:beg:0x401a2f
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <cmath>
using namespace std;
string to_letters (unsigned long long int x){
int siz=log(x)+1,temp=0;
string out;
for(int i=0;i<siz;++i){
temp=x%(10*(i+1));
if(i>0){
temp-=x%(10*(i));
}
out[i]=temp;
}
return out;
};
class BigNum { //ONLY BIGNUMS MAY BE ADDED TO BIGNUMS
public:
string GetSection(int c);
void SetSection(int c, string in);
BigNum();
private: //ints to store, long long int to calculate and move carry over
string FirstSixth;
string SecondSixth;
string ThirdSixth;
string FourthSixth;
string FifthSixth;
string LastSixth;
public:
BigNum & operator=(string number){
this->FirstSixth=number.substr(0,9);
this->SecondSixth=number.substr(9,9);
this->ThirdSixth=number.substr(18,9);
this->FourthSixth=number.substr(27,9);
this->FifthSixth=number.substr(36,9);
this->LastSixth=number.substr(45,number.length()-45);
}
BigNum & operator+=(BigNum other){
unsigned long long int overflow=0,X=0,Y=0;
int length=0;
string temp,preX,preY;
bool carry_the_one=0;
for(int i=0;i<5;++i){
preY=other.GetSection(i);
switch(i){
case 0:
preX=this->FirstSixth;
break;
case 1:
preX=this->SecondSixth;
break;
case 2:
preX=this->ThirdSixth;
break;
case 3:
preX=this->FourthSixth;
break;
case 4:
preX=this->FifthSixth;
break;
case 5:
preX=this->LastSixth;
break;
}
X=strtoull(preX.c_str(),NULL,0); //adding in sections!!!!
Y=strtoull(preY.c_str(),NULL,0);
if(carry_the_one==1){
++X;
}
overflow=X+Y; //overflow is an ull with the current partial sum
length=log(overflow)+1; //0 will make this fail
temp=to_letters(overflow); //SEGFAULT OCCURS HERE
//will be 10 characters long at most, 5 at least
if(temp.length()==10){
carry_the_one=1;
temp=temp.substr(0,9);
}
switch(i){
case 0:
this->FirstSixth=temp;
break;
case 1:
this->SecondSixth=temp;
break;
case 2:
this->ThirdSixth=temp;
break;
case 3:
this->FourthSixth=temp;
break;
case 4:
this->FifthSixth=temp;
break;
case 5:
this->LastSixth=temp;
break;
}
}
}
};
BigNum::BigNum(){
FirstSixth="";
SecondSixth="";
ThirdSixth="";
FourthSixth="";
FifthSixth="";
LastSixth="";
};
string BigNum::GetSection(int c){
switch(c){
case 0:
return FirstSixth;
break;
case 1:
return SecondSixth;
break;
case 2:
return ThirdSixth;
break;
case 3:
return FourthSixth;
break;
case 4:
return FifthSixth;
break;
case 5:
return LastSixth;
break;
default:
cout<<"Fatal Error";
throw;
}
};
void BigNum::SetSection(int c,string in){
switch(c){
case 1:
FirstSixth=in;
break;
case 2:
SecondSixth=in;
break;
case 3:
ThirdSixth=in;;
break;
case 4:
FourthSixth=in;
break;
case 5:
FifthSixth=in;
break;
case 6:
LastSixth=in;
break;
}
};
int main() //goal is FIRST 10 digits of the sum
{
ifstream eyeballs;
eyeballs.open("Pe13.txt");
string knees;
BigNum running, addend;
getline(eyeballs,knees,'\n');
running=knees;
for(int i=0;i<99;++i){
getline(eyeballs,knees,'\n');
addend=knees;
running+=addend;
}
cout<<"First digits of sum: "<<running.GetSection(6)<<running.GetSection(5);
cin.get();
}
|