This is my solution to ARITH- Simple Arithmetics problem! However my solution is getting runtime error (SIGXFSZ) ! Please someone suggest changes!
/*
Name: Kartik Gupta
IIT-Guwahati
*/
#include <bits/stdc++.h>
using namespace std;
void solve(string Num_A, char operator_, string Num_B)
{
int len_a = Num_A.length() ;
int len_b = Num_B.length() ;
int Num_Dashes = max(len_a,len_b) ;
if(operator_ == '+')
{
int operand_1 ;
int operand_2 ;
int Carry = 0 ;
string Answer = "" ;
int a ;
int b ;
for(a = len_a - 1,b = len_b - 1 ; ; a--,b--)
{
// printf("a = %d , b = %d\n",a,b );
if(a < 0 && b < 0)
{
break ;
}
else if(a < 0 && b >= 0)
{
operand_1 = 0 ;
operand_2 = Num_B[b]-'0' ;
}
else if(b < 0 && a >= 0)
{
operand_1 = Num_A[a]-'0' ;
operand_2 = 0 ;
}
else if(b >= 0 && a >= 0)
{
operand_1 = Num_A[a] - '0' ;
operand_2 = Num_B[b] - '0' ;
}
// printf("\noperand_1 =%d\noperand_2=%d\nCarry=%d\n\n",operand_1,operand_2,Carry);
int final = operand_2 + operand_1 + Carry ;
Carry = 0 ;
while(final > 9)
{
final -= 10 ;
Carry += 1 ;
}
Answer = (char)('0' + final) + Answer ;
// printf("Adding %c to Answer\n", '0'+final ) ;
// printf("Answer is %s\n", Answer.c_str()) ;
}
if(Carry)
{
Answer = (char)('0' + Carry) + Answer ;
}
Num_B = operator_ + Num_B ;
Num_Dashes = max(Num_A.length(),Num_B.length()) ;
Num_Dashes = max(Num_Dashes,(int)Answer.length()) ;
printf("%*s\n",Num_Dashes,Num_A.c_str());
printf("%*s\n",Num_Dashes,Num_B.c_str());
int i = Num_Dashes ;
while(i--) printf("-") ;
printf("\n");
printf("%*s\n",Num_Dashes,Answer.c_str());
return ;
}
if(operator_ == '-')
{
int operand_1 ;
int operand_2 ;
bool loan = false ;
string Answer = "" ;
int a ;
int b ;
for(a = len_a - 1,b = len_b - 1 ; ; a--,b--)
{
// printf("a = %d , b = %d\n",a,b );
if(a < 0 && b < 0)
{
break ;
}
else if(a < 0 && b >= 0)
{
operand_1 = 0 ;
operand_2 = Num_B[b]-'0' ;
}
else if(b < 0 && a >= 0)
{
operand_1 = Num_A[a]-'0' ;
operand_2 = 0 ;
}
else if(b >= 0 && a >= 0)
{
operand_1 = Num_A[a] - '0' ;
operand_2 = Num_B[b] - '0' ;
}
// printf("\noperand_1 =%d\noperand_2=%d\nCarry=%d\n\n",operand_1,operand_2,Carry);
if(loan)
{
operand_1 -= 1 ;
loan = false ;
}
if(operand_1 < operand_2)
{
operand_1 += 10 ;
loan = true ;
}
int final = operand_1 - operand_2 ;
Answer = (char)('0' + final) + Answer ;
// printf("Adding %c to Answer\n", '0'+final ) ;
// printf("Answer is %s\n", Answer.c_str()) ;
}
Num_B = operator_ + Num_B ;
// Num_Dashes = max(Num_A.length(),Num_B.length()) ;
Num_Dashes = max(Num_B.length(),Answer.length()) ;
printf("%*s\n",Num_Dashes,Num_A.c_str());
printf("%*s\n",Num_Dashes,Num_B.c_str());
int i = Num_Dashes ;
while(i--) printf("-") ;
printf("\n");
printf("%*s\n",Num_Dashes,Answer.c_str());
return ;
}
if(operator_ == '*')
{
int operand_1 ;
int operand_2 ;
// string Answer = '' ;
int Num_Rows = len_b ;
string * Answer = new string[Num_Rows] ;
Answer[0] = "" ;
for(int i = 1 ; i < Num_Rows ; i++)
{
/* Initialise with i spaces */
int j = i ;
while(j--)
{
Answer[i] += ' ' ;
}
}
int row_i = 0 ;
for(int i = len_b - 1 ; i >= 0 ; i--,row_i++)
{
/* For each i we have a line */
operand_1 = Num_B[i]-'0' ;
int Carry = 0 ;
if(!operand_1)
{
Answer[row_i] = '0' + Answer[row_i] ;
continue ;
}
for(int j = len_a - 1 ; j >= 0 ; j--)
{
operand_2 = Num_A[j]-'0' ;
int final = operand_1 * operand_2 + Carry ;
Carry = 0 ;
while(final > 9)
{
final -= 10 ;
Carry++ ;
}
Answer[row_i] = (char)('0'+final) + Answer[row_i] ;
}
if(Carry)
{
Answer[row_i] = (char)('0'+Carry) + Answer[row_i] ;
}
}
/* Calculating Final Answer */
row_i-- ;
int Length_Final_Row = Answer[row_i].length() ;
string * cpAnswer = new string[Num_Rows] ;
for(int i = 0 ; i < Num_Rows ; i++)
{
cpAnswer[i] = Answer[i] ;
reverse(cpAnswer[i].begin(), cpAnswer[i].end()) ;
}
string Final_Answer = "" ;
int Carry = 0 ;
for(int i = 0 ; i < Length_Final_Row ; i++ )
{
int sum = Carry ;
Carry = 0 ;
/* Take the number at ith position for all */
for(int j = 0 ; j < Num_Rows ; j++)
{
/* Take from cpAnswer */
if(i >= cpAnswer[j].length())
{
continue ;
}
else if(cpAnswer[j][i] == ' ')
{
continue ;
}
else
{
sum += cpAnswer[j][i] - '0' ;
}
}
while(sum > 9)
{
sum -= 10 ;
Carry++ ;
}
Final_Answer = (char)('0'+sum) + Final_Answer ;
}
if(Carry)
{
Final_Answer = (char)('0'+Carry) + Final_Answer ;
}
int Num_Dashes_2 = max(Final_Answer.length(),cpAnswer[Num_Rows-1].length()) ;
Num_B = operator_ + Num_B ;
// Num_Dashes = max(Num_A,Num_B) ;
Num_Dashes = max(Num_B.length(),(cpAnswer[0].length())) ;
/* Start Printing */
printf("%*s\n",Num_Dashes_2,Num_A.c_str() );
printf("%*s\n",Num_Dashes_2,Num_B.c_str() );
int i = Num_Dashes ;
int j = Num_Dashes_2 - Num_Dashes ;
while(j--)
{
printf(" ");
}
while(i--)
{
printf("-");
}
printf("\n");
if(Num_Rows == 1)
{
printf("%*s\n",Num_Dashes_2,Answer[0].c_str());
delete [] cpAnswer ;
delete [] Answer ;
return ;
}
for(int i = 0 ; i < Num_Rows ; i++)
{
printf("%*s\n",Num_Dashes_2,Answer[i].c_str());
}
i = Num_Dashes_2 ;
while(i--)
{
printf("-");
}
printf("\n");
printf("%*s\n",Num_Dashes_2,Final_Answer.c_str());
delete [] cpAnswer ;
delete [] Answer ;
return ;
}
return ;
}
int main( int argc , char ** argv )
{
// ios_base::sync_with_stdio(false) ;
// cin.tie(NULL) ;
int Num_Cases ;
cin >> Num_Cases ;
while(Num_Cases--)
{
string Num_A = "" ;
string Num_B = "" ;
char operator_ ;
string input ;
cin >> input ;
int len_input = (int) input.length() ;
string * ptr = &Num_A ;
for(int i = 0 ; i < len_input ; i++)
{
if(input[i] >= '0' && input[i] <= '9')
{
*ptr = *ptr + input[i] ;
}
else
{
operator_ = input[i] ;
ptr = &Num_B ;
}
}
/* cout << Num_A << '\n' ;
cout << Num_B << '\n' ;
cout << operator_ << '\n' ; */
solve(Num_A,operator_,Num_B) ;
cout << '\n' ;
}
return 0 ;
}