main bigint code example
Example 1: bigint
#include <stdexcept>
#include"Bigint.hpp"
#include<algorithm>
Bigint::Bigint(std::list<unsigned char>B)
:m_digits(B){}
Bigint::~Bigint(){}
bool Bigint::is_zero()const
{
if(m_digits.front()=='0'){
return true;
}
return false;
}
bool Bigint::is_negative() const{
if(m_is_negative == true){
return true ;
}else
return false;
}
std::ostream& operator<<(std::ostream& out, const Bigint& i){
for(auto b = i.m_digits.begin(); b != i.m_digits.end(); ++b){
out<<(*b);
}
return (out);
}
std::istream& operator>>(std::istream& in, Bigint& i) {
char c;
in.get(c);
if (c == '-') i.m_is_negative = true;
else {
if (! std::isdigit(c)) throw std::runtime_error("Invalid input");
i.m_digits.emplace_front(c);
}
while (in.get(c) && (c != 0xa)) {
if (! std::isdigit(c)) throw std::runtime_error("Invalid input");
i.m_digits.emplace_front(c);
}
i.m_digits.reverse();
while(i.m_digits.front()=='0'&&i.m_digits.size()!= 1){
i.m_digits.pop_front();
if(i.m_digits.size()== 1)
break;
}
return in;
}
Example 2: main bigint
#include"Bigint.hpp"
#include"Command.hpp"
#include<string>
#include<sstream>
#include<iostream>
int main()
{
try
{
Bigint list1 ;
Bigint list2 ;
std::string userinput;
std::cout<<"Please enter the first integer: ";
std::cin >> list1;
std::cout<<"Please enter the second integer: ";
std::cin >> list2;
std::cout<<"Please enter the command (0=+, 1=-, 2=*):\t";
std::cin>>userinput;
if(userinput[0]<48||userinput[0]>50||userinput.length()>1)throw std::runtime_error("invalid input");
switch(userinput[0]%48)
{
case(ADD):
std::cout<<list1+list2<<std::endl;
break;
case(SUB):
std::cout<<list1-list2<<std::endl;
break;
case(MUL):
std::cout<<list1*list2<<std::endl;
break;
}
}catch(std::runtime_error& e){ std::cout<<e.what()<<std::endl;}
return 0;
}
Example 3: main bigint
#include"Bigint.hpp"
#include"Command.hpp"
#include<string>
#include<sstream>
#include<iostream>
int main()
{
try
{
Bigint list1 ;
Bigint list2 ;
std::string userinput;
std::cout<<"Please enter the first integer: ";
std::cin >> list1;
std::cout<<"Please enter the second integer: ";
std::cin >> list2;
std::cout<<"Please enter the command (0=+, 1=-, 2=*):\t";
std::cin>>userinput;
if(userinput[0]<48||userinput[0]>50||userinput.length()>1)throw std::runtime_error("invalid input");
switch(userinput[0]%48)
{
case(ADD):
std::cout<<list1+list2<<std::endl;
break;
case(SUB):
std::cout<<list1-list2<<std::endl;
break;
case(MUL):
std::cout<<list1*list2<<std::endl;
break;
}
}catch(std::runtime_error& e){ std::cout<<e.what()<<std::endl;}
return 0;
}