main bigint

Solutions on MaxInterview for main bigint by the best coders in the world

showing results for - "main bigint"
Emil
19 Jun 2018
1#include <stdexcept>
2#include"Bigint.hpp"
3#include<algorithm>
4// CONSTRUCTOR OVERLOAD 
5Bigint::Bigint(std::list<unsigned char>B) 
6   :m_digits(B){}
7Bigint::~Bigint(){}
8//##################################### is_zero #########>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Amir Ammar
9bool Bigint::is_zero()const
10{
11     if(m_digits.front()=='0'){
12       return true;
13     }
14     return false;
15}
16//##################################### is_negative #########################################################
17bool Bigint::is_negative() const{
18   if(m_is_negative == true){
19     return  true ;
20   }else 
21     return false;
22}
23//<<<<<<<<<<<<<<<<<<<<<<<<<<<insertion operator overloading<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Amir Ammar
24std::ostream& operator<<(std::ostream& out, const Bigint& i){
25  for(auto b = i.m_digits.begin(); b != i.m_digits.end(); ++b){
26    out<<(*b);
27  }
28  return (out);
29}
30//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>extraction operator overloading>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Efi Fogel
31std::istream& operator>>(std::istream& in, Bigint& i) {
32  char c;
33  in.get(c);
34  if (c == '-') i.m_is_negative = true;
35  else {
36    if (! std::isdigit(c)) throw std::runtime_error("Invalid input");
37    i.m_digits.emplace_front(c);
38  }
39  while (in.get(c) && (c != 0xa)) {
40    if (! std::isdigit(c)) throw std::runtime_error("Invalid input");
41    i.m_digits.emplace_front(c);
42  }
43  i.m_digits.reverse(); // additional method to return the reversed value (the real input)
44  while(i.m_digits.front()=='0'&&i.m_digits.size()!= 1){ // while loop to earse additional zeroes 
45    i.m_digits.pop_front();
46    if(i.m_digits.size()== 1)
47      break;
48  }
49  return in;
50}
Lena
22 Oct 2020
1#include"Bigint.hpp"
2#include"Command.hpp"
3#include<string>
4#include<sstream>
5#include<iostream>
6int main()
7{
8    try
9    {
10      Bigint list1 ;
11      Bigint list2 ;
12      std::string userinput;
13      std::cout<<"Please enter the first integer: ";  // ===>>> Bigint 1 
14      std::cin >> list1;
15      std::cout<<"Please enter the second integer: ";  // ===>>> Bigint 2 
16      std::cin >> list2;
17      std::cout<<"Please enter the command (0=+, 1=-, 2=*):\t";
18      std::cin>>userinput;
19      if(userinput[0]<48||userinput[0]>50||userinput.length()>1)throw std::runtime_error("invalid input");
20      switch(userinput[0]%48)
21          {
22            case(ADD):
23             std::cout<<list1+list2<<std::endl;
24             break;
25            case(SUB):
26             std::cout<<list1-list2<<std::endl;
27             break;
28            case(MUL):
29             std::cout<<list1*list2<<std::endl;
30             break;
31          }
32    }catch(std::runtime_error& e){ std::cout<<e.what()<<std::endl;}
33  return 0;
34}