| ofey (10) | |
|
The following program attempts to work as a calculator for huge inputs. PROBLEM STATEMENT: http://www.spoj.com/problems/ARITH/ It's working perfectly on IDE for all imaginable cases. IDE URL: http://ideone.com/lP3JZe On SPOJ it has started giving SIGSEGV. I know it may be difficult to read and understand the whole code, but any idea about where this fault may be arising from will be appreciated. Thanks /////////////////////////////////////CODE////////////////////////////////// #include<iostream> #include<string> #include<algorithm> #include<iomanip> #include<vector> #define sz size() using namespace std; /////////////////////////////////////////////////////////////////////////// string str1Copy(""),strAns; vector<string> store,storeAdd; ////////////////////////////////////////////////////////////////////////// /////////////////ADD 2 NUMBERS AND STORE IN strAns//////////////////////// void getSum(string str1,string str2) { strAns.erase(); if(str2=="0") { strAns=str1; return; } int carry=0,i=0,j=0,sum=0; for(i=str1.sz-1,j=str2.sz-1;i>=0&&j>=0;i--,j--) { sum=(str1[i]-'0')+(str2[j]-'0')+carry; carry=sum==0?0:sum/10; sum%=10; strAns=char(sum+'0')+strAns; } while(i>=0) { sum=carry+(str1[i]-'0'); carry=sum==0?0:sum/10; sum%=10; strAns=char(sum+'0')+strAns; i--; } while(j>=0) { sum=carry+(str2[j]-'0'); carry=sum==0?0:sum/10; sum%=10; strAns=char(sum+'0')+strAns; j--; } strAns=carry>0?char(carry+'0')+strAns:strAns; } ////////////////////////////////////////////////////////////////////////////// //////////////////////////SUBTRACT 2 NUMBERS///////////////////////////////// void getDiff(string str1, string str2) { strAns.erase(); int carry=0,i=0,j,diff=0,temp=0; for(i=str1.sz-1,j=str2.sz-1;i>=0&&j>=0;i--,j--) { diff=(str1[i]-'0')-(str2[j]-'0'); if(diff>=0) { strAns=char(diff+'0')+strAns; continue; } diff+=10; strAns=char(diff+'0')+strAns; temp=i-1; while(str1[temp]=='0') { str1[temp]='9'; temp--; } str1[temp]--; } while(i>=0) { strAns=str1[i]+strAns; i--; } for(i=0;strAns[i]=='0';) //REMOVE STARTING ZEROES strAns.erase(i,1); } ///////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////// int main() { int setwLen=0,cases=0,i=0,pos=0,j=0,caseVar=0; string str(""),str1(""),str2(""),strDash(""),tempStr1(""); cin>>cases; for(caseVar=0;caseVar<cases;caseVar++) { strDash.erase(); str.erase(); str1.erase(); str2.erase(); cin>>str; pos=str.find_first_of("+-*"); ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// ////////////////////ADDITION CASE//////////////////////////////////////// if(str[pos]=='+') { str1=str.substr(0,pos); //FIRST NUMBER str2=str.substr(pos+1); //SECOND NUMBER getSum(str1,str2); ///////////////////////STEPS FOR FORMATTED O/P////////////////// setwLen=max(strAns.sz,max(str2.sz+1,str1.sz)); cout<<setw(setwLen)<<str1<<"\n"; cout<<setw(setwLen-str2.sz)<<'+'<<str2<<"\n"; //setwLen=max(strAns.sz,str2.sz+1); //cout<<setfill('-')<<setw(setwLen)<<"-"<<"\n"; for(i=0;i<max(strAns.sz,str2.sz+1);i++) strDash+='-'; cout<<setw(setwLen)<<strDash<<"\n"; setwLen=max(strAns.sz,max(str2.sz+1,str1.sz)); cout<<setfill(' ')<<setw(setwLen)<<strAns<<"\n\n"; } ////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// //SUBTRACTION////////////////////////////////////////////////////////////// else if(str[pos]=='-') { strDash.erase(); str1Copy.erase(); str1Copy=str1=str.substr(0,pos); //FIRST NUMBER str2=str.substr(pos+1); //SECOND NUMBER getDiff(str1Copy,str2); //////////////STEPS FOR FORMATTED O/P////////////////// setwLen=max(strAns.sz,max(str2.sz+1,str1.sz)); cout<<setw(setwLen)<<str1<<"\n"; cout<<setw(setwLen-str2.sz)<<'-'<<str2<<"\n"; for(i=0;i<max(strAns.sz,str2.sz+1);i++) strDash+='-'; cout<<setw(setwLen)<<strDash<<"\n"; setwLen=max(strAns.sz,max(str2.sz+1,str1.sz)); cout<<setfill(' ')<<setw(setwLen)<<strAns<<"\n\n"; } //////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// //MULTIPLICATION////////////////////////////////////////////////// else if(str[pos]=='*') { tempStr1.erase(); strDash.erase(); store.erase(store.begin(),store.end()); storeAdd.erase(store.begin(),store.end()); str1=str.substr(0,pos); //1st NUMBER str2=str.substr(pos+1); //2nd NUMBER tempStr1=str1; for(i=str2.sz-1;i>=0;i--) { if(str2[i]=='0') { tempStr1="0"; goto skip; } else if(str2[i]=='1') { tempStr1=str1; goto skip; } for(j=0,tempStr1=str1;j<(str2[i]-'0')-1;j++) { getSum(tempStr1,str1); tempStr1=strAns; } skip: store.push_back(tempStr1); storeAdd.push_back(tempStr1); } for(i=1;i<store.sz;i++) for(j=0;j<i;j++) storeAdd[i]+='0'; tempStr1=storeAdd[0]; for(i=1;i<store.size();i++) { getSum(tempStr1,storeAdd[i]); tempStr1=strAns; } ////////////////////////STEPS FOR FORMATTED O/P///////////////////// setwLen=max(tempStr1.sz,max(str1.sz,str2.sz+1)); cout<<setw(setwLen)<<str1<<"\n"; cout<<setw(setwLen-str2.sz)<<'*'<<str2<<"\n"; for(i=0;i<max(str2.sz+1,store[0].sz);i++) strDash+='-'; cout<<setw(setwLen)<<strDash<<"\n"; for(i=0;i<store.size();i++) cout<<setw(setwLen-i)<<store[i]<<"\n"; if(store.sz==1) continue; strDash.erase(); for(i=0;i<max(tempStr1.sz,store[store.sz-1].sz);i++) strDash+='-'; cout<<setw(setwLen)<<strDash<<"\n"; cout<<setw(setwLen)<<tempStr1<<"\n\n"; //////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////// } } return 0; } | |
|
Last edited on
|
|
| NwN (770) | |
|
Hi there, Welcome to the forums :) My advice: - wrap the code in code tags: [code] code here [/code] - place debug statements in the program that trace all steps, then you have an idea at least after which step the segfault occurs. - Have a look at this article: http://cplusplus.com/articles/iwTbqMoL/ Hope that helps. All the best, NwN | |
|
|
|
| cire (2354) | |
|
global variables, goto, obnoxious looking comments, a near complete lack of whitespace, poorly named variables, most of the work done in a monolithic function and a two letter macro that hides function calls. No wonder you can't debug it? | |
|
|
|
| ofey (10) | |
|
@NwN: Got the error. Was a slight typo. Thanks anyways. @cire: i am a noob presently, hence the apparently abhorrent code. Any documentation/reference on writing better, easier to debug code would be most welcome. Thanks. | |
|
|
|