Click here to Skip to main content
15,917,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
bool compare(string m, string n)
 { if(m.length()>n.length()) return true;
   else if(m.length()<n.length()) return false;
   for(int i=0;i<m.length();i++)  
    {  if((m[i]-'0')>(n[i]-'0')) return true;
       if((m[i]-'0')<(n[i]-'0')) return false;   
    } 
  }     

    string substract(string str1, string str2)
     { string str = "";
      int n1 = str1.length(), n2 = str2.length();
      int m=n1-n2;
      for(int i=0;i<m;i++)
      str2="0"+str2;
      reverse(str1.begin(), str1.end());
      reverse(str2.begin(), str2.end());
      n2 = str2.length(); 
      int carry = 0;
      for (int i=0; i<n2; i++)
       { int sub = ((str1[i]-'0')-(str2[i]-'0')-carry);
          if (sub < 0)
           { sub = sub + 10;
             carry = 1;
           }
          else  carry = 0;
        str.push_back(sub + '0');
       }
      for (int i=n2; i<n1; i++)
       { int sub = ((str1[i]-'0') - carry);
         carry = 0;
        str.push_back(sub + '0');
       }
      reverse(str.begin(), str.end());
      if(str.length()>1) 
       { int i = 0; 
      while (str[i] == '0') 
       i++; 
      str.erase(0, i); } 
      return str;
    }

     string sum(string a, string b)
      { if(a.size() < b.size())
       swap(a, b);
       int j = a.size()-1;
       for(int i=b.size()-1; i>=0; i--, j--)
       a[j]+=(b[i]-'0');
       for(int i=a.size()-1; i>0; i--)
        {  if(a[i] > '9')
            {  int d = a[i]-'0';
               a[i-1] = ((a[i-1]-'0') + d/10) + '0';
               a[i] = (d%10)+'0';
            }
         }
        if(a[0] > '9')
           { string k;
             k+=a[0];
             a[0] = ((a[0]-'0')%10)+'0';
             k[0] = ((k[0]-'0')/10)+'0';
             a = k+a;
           }
        return a;
     }

    string division(string a, string b)
     { bool p;
       bool t=compare(a,b); 
       string ans;
      if(t==true)  p=true;
       while(p==true)
       { a=substract(a,b); 
         ans=sum(ans,"1"); 
         t=compare(a,b); 
        if(t==false) p=false; 
       } 
     return ans;    
      }


What I have tried:

I wrote code that uses the compare,substract and sum functions for division.
Posted
Updated 21-Jun-19 3:40am
v2
Comments
Patrice T 21-Jun-19 5:53am    
Give working code and data which exhibit the error.

This code should not compile:
C++
bool compare(string m, string n)
 { if(m.length()>n.length()) return true;
   else if(m.length()<n.length()) return false;
   for(int i=0;i<m.length();i++)  
    {  if((m[i]-'0')>(n[i]-'0')) return true;
       if((m[i]-'0')<(n[i]-'0')) return false;   
    } 
   // because there is no return value when both string are equal
  }

Optimization: both lines do exactly the same thing:
C++
if((m[i]-'0')>(n[i]-'0')) return true;
if( m[i]     > n[i]     ) return true;

The only difference is that the second is simpler and may be faster depending on compiler optimizations.
 
Share this answer
 
v2
Comments
Stefan_Lang 21-Jun-19 6:24am    
I think the function compare() is meant to implement 'greater than'
Patrice T 21-Jun-19 6:43am    
Probably, but when both values are the same, the function should still return something.
Member 14507684 21-Jun-19 6:36am    
Oh, thank you. Now i got it. Yes, I am using function compare() to find greater number. Can you tell me, what i can do to avoid that problem?
Patrice T 21-Jun-19 6:46am    
you need to replace my comment with the answer when both string are identical.
Member 14507684 21-Jun-19 6:41am    
"3948298909", "143" this numbers are causing error.In some cases this program works, If I am changing the numbers. It works better, if numbers are small
I've already added this to my solution 2, but maybe you missed my edit: You must change the line
C++
bool p;
in your function division() to bool
C++
p=false;
This gives the correct result.

I should add that the result is not correct when ther division has no remainder: for 150 / 15 I get 9, and for 1500/15 I get 99. But this should be easy to fix.

P.S.: I did not change the function compare() !

P.P.S.: When using the online compiler at https://www.onlinegdb.com/online_c++_compiler[^] your original program runs even without the modification I suggested! It does take a couple of minutes though! Maybe your system is set to terminate processes that run longer than a specific time limit?
 
Share this answer
 
v3
Comments
Member 14507684 22-Jun-19 6:41am    
Thank you very much for your comment. I already changed to p = false, but it still shows me the same error. I use rextester to compile my code, and I tried other compilers. The program works for a long time. Maybe this causes a problem? Because code works good for small numbers
Stefan_Lang 24-Jun-19 2:50am    
Well it works with the online compiler I linked above. So I can only imagine that it's some kind of restriction imposed on your OS, compiler, or system.

SigKill sounds very much that your procss is killed from outside! If that's your personal computer, I have no idea what is causing this. If you're working within the network of a university or company, it might be possible there's a restriction enforced by network tools. In that case you should ask an admin.
1. Your function compare should be renamed to greater_than, because that is what it does.
2. division() has an error: p is not initialized, so it may enter the while loop even if a < b. Try replacing
C++
bool p;
with
C++
bool p=false;
 
Share this answer
 
v3
bool compare(string m, string n)
{if(m.length()>n.length()) return true;
else if(m.length()<n.length()) return false;
bool k=true;
for(int i=0;i<m.length();i++)
  { 
   if((m[i])!=(n[i])) k=false; 
  }
  if(k==true) return true; 
   else 
    {
     for(int i=0;i<m.length();i++)  
      {  
        if((m[i])>(n[i])) return true;
        if((m[i])<(n[i]))  return false;
      } 
    }
}
 
Share this answer
 
v2
Comments
Member 14507684 21-Jun-19 7:23am    
I changed compare function like this. It still shows me that error: kill signal(SIGKILL). I don't know what to do. It is possible that something else is causing this problem?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900