netx_iit

challenging program

Jan 14th, 2021
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.53 KB | None | 0 0
  1. /*
  2. *
  3. Challenging C++ program VNSGU(MCA)
  4. Create a class HugeInteger that uses a 40-element array of digits to store integers as large as 40-digits each.Provide member methods inputHugeInteger, outputHugeInteger, addHugeIntegers and substractHugeIntegers. For comparing HugeInteger objects, provide methods isEqualTo, isNotEqualTo,isGreaterThan, isLessThan, IsGreaterThanOrEqualTo and isLessThanOrEqualTo--each of these is a
  5. “predicate” method that simply returns true if the relationship holds between the two huge integers and returns
  6. false if the relationship does not hold. Also provide a predicate method isZero.
  7.  
  8. */
  9. #include<iostream>
  10. using namespace std;
  11. class HugeInteger
  12. {
  13.     string num;
  14.     int no[40];
  15.     int l;
  16.     public:
  17.         void inputHugeInteger()
  18.         {
  19.             cout<<"\n Enter no=";
  20.             cin>>num;
  21.             if(num.length()>40)
  22.             {
  23.                 cout<<"\n Number is not more than 40 digits";
  24.             }
  25.             else
  26.             {
  27.                 convert_str_to_num(num);//this method convert str to num
  28.             }
  29.         }
  30.        
  31.         void convert_str_to_num(string s)
  32.         {
  33.             l=s.length();
  34.             for(int k=0;k<l;k++)
  35.             {
  36.                 no[k]=s[k]-'0';//convert char to int
  37.             }  
  38.         }
  39.        
  40.         void outputHugeInteger()
  41.         {  
  42.             cout<<"\n";
  43.             for(int k=0;k<l;k++)
  44.             {
  45.                 cout<<no[k];   
  46.             }
  47.         }
  48.        
  49.         bool is_zero()
  50.         {
  51.             int flag=1;
  52.             for(int k=0;k<l;k++)
  53.             {
  54.                 if(no[k]!=0)
  55.                 {
  56.                     flag=0;
  57.                     break;
  58.                 }
  59.             }
  60.             if(flag==1)
  61.             {
  62.                 return true;
  63.             }
  64.             else
  65.             {
  66.                 return false;
  67.             }
  68.         }
  69.        
  70.         bool isEqualTo(HugeInteger temp)
  71.         {
  72.             int flag=1;
  73.             if(l==temp.l)
  74.             {
  75.                 for(int k=0;k<l;k++)
  76.                 {
  77.                     if(no[k]!=temp.no[k])
  78.                     {
  79.                         flag=0;
  80.                         break;
  81.                     }
  82.                 }
  83.             }
  84.             else
  85.             {
  86.                 flag=0;
  87.             }
  88.             if(flag==1)
  89.             {
  90.                 return true;
  91.             }
  92.             else
  93.             {
  94.                 return false;
  95.             }
  96.         }
  97.        
  98.         bool isNotEqualTo(HugeInteger temp)
  99.         {
  100.             isEqualTo(temp)?cout<<"False":cout<<"True";
  101.         }
  102.         bool isgreaterthan(HugeInteger temp)
  103.         {
  104.             int flag=0;
  105.             int d;
  106.             if(l!=temp.l)
  107.             {
  108.                 d=l-temp.l;
  109.                 if(d < 0)
  110.                 {
  111.                     flag=1;
  112.                 }
  113.             }
  114.             else
  115.             {
  116.                 for(int k=0;k<l;k++)
  117.                 {
  118.                     if(no[k] < temp.no[k])
  119.                     {
  120.                         flag=1;
  121.                         break;
  122.                     }
  123.                 }
  124.             }
  125.             if(flag==1)
  126.             {
  127.                 return false;
  128.             }
  129.             else
  130.             {
  131.                 if(isEqualTo(temp)==0)
  132.                 {
  133.                     return true;
  134.                 }
  135.                 else
  136.                 {
  137.                     return false;
  138.                 }
  139.             }
  140.         }
  141.         bool islessthan(HugeInteger temp)
  142.         {
  143.             int flag=0;
  144.             int d;
  145.             if(l!=temp.l)
  146.             {
  147.                 d=l-temp.l;
  148.                 if(d < 0)
  149.                 {
  150.                     flag=1;
  151.                 }
  152.             }
  153.             else
  154.             {
  155.                 for(int k=0;k<l;k++)
  156.                 {
  157.                     if(no[k] < temp.no[k])
  158.                     {
  159.                         flag=1;
  160.                         break;
  161.                     }
  162.                 }
  163.             }
  164.             if(flag==1)
  165.             {
  166.                 return true;
  167.             }
  168.             else
  169.             {
  170.                 if(isEqualTo(temp)==0)
  171.                 {
  172.                     return true;
  173.                 }
  174.                 else
  175.                 {
  176.                     return false;
  177.                 }
  178.             }
  179.         }
  180.         bool isgreaterthanequalto(HugeInteger temp)
  181.         {
  182.             if(isEqualTo(temp)==1)
  183.             {
  184.                 return true;
  185.             }
  186.             else
  187.             {
  188.                 return true;
  189.             }
  190.         }
  191.         bool islessthanequalto(HugeInteger temp)
  192.         {
  193.             if(isEqualTo(temp)==0)
  194.             {
  195.                 return true;
  196.             }
  197.             else
  198.             {
  199.                 return true;
  200.             }
  201.         }
  202.        
  203.         void addHugeIntegers(HugeInteger temp)
  204.         {
  205.             int sum[40];
  206.             int s,flag=0;
  207.             int t=0;
  208.             int size1=num.length();
  209.             int size2=temp.num.length();
  210.             if(size1-size2==0)
  211.             {
  212.                 for(int k=size2-1;k>=0;k--)
  213.                 {
  214.                     s=no[k]+temp.no[k];
  215.                     if(k==0 && s>=10)
  216.                     {
  217.                         flag=1;
  218.                     }
  219.                     if(s>=10)
  220.                     {
  221.                         sum[k]=s%10;
  222.                         no[k-1]=no[k-1]+s/10;
  223.                        
  224.                     }
  225.                     else
  226.                     {
  227.                         sum[k]=no[k]+temp.no[k];
  228.                     }
  229.                    
  230.                 }
  231.                 if(flag==1)
  232.                 {
  233.                     cout<<1;
  234.                 }
  235.                 for(int i=0;i<size1;i++)
  236.                 {
  237.                     cout<<sum[i];
  238.                 }
  239.             }
  240.            
  241.         }
  242. };
  243. int main()
  244. {
  245.     HugeInteger h1,h2;
  246.     h1.inputHugeInteger();
  247.     h2.inputHugeInteger();
  248.     h1.outputHugeInteger();
  249.     h2.outputHugeInteger();
  250.     cout<<"\n No1 is zero=";
  251.     h1.is_zero()?cout<<"true":cout<<"false";
  252.     cout<<"\n No2 is zero=";
  253.     h2.is_zero()?cout<<"true":cout<<"false";
  254.     cout<<"\n No1 is equal to No2=";
  255.     h1.isEqualTo(h2)?cout<<"true":cout<<"false";
  256.     cout<<"\n No1 is not equal to No2=";
  257.     h1.isNotEqualTo(h2);
  258.     cout<<"\n No1 is greater than No2=";
  259.     h1.isgreaterthan(h2)?cout<<"true":cout<<"false";
  260.     cout<<"\n No1 is greater than equal to No2=";
  261.     h1.isgreaterthanequalto(h2)?cout<<"true":cout<<"false";
  262.     cout<<"\n No1 is less than No2=";
  263.     h1.islessthan(h2)?cout<<"true":cout<<"false";
  264.     cout<<"\n No1 is less than equal to No2=";
  265.     h1.islessthanequalto(h2)?cout<<"true":cout<<"false";
  266.     cout<<"\n Addition of 2 number is ";
  267.     h1.addHugeIntegers(h2);
  268. }
Advertisement
Add Comment
Please, Sign In to add comment