| |

VerySource

 Forgot password?
 Register
Search
View: 862|Reply: 3

Help me change the input method

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2021-3-10 12:00:02
| Show all posts |Read mode
I found a large number of programs, analyzed it, and wanted to change the input to keyboard input. I tried several methods without success. I have no idea. It may be that the program does not understand. Who can help me change it:
hugeint.h file
#include<iostream.h>
class hugeint
{
public:
    hugeint(int);
    hugeint(long);
    hugeint(hugeint&);
    ~hugeint();
    inline long getsize(){return size;};
    inline int elem(long index){return data[index];};
    hugeint&operator = (char*);
    hugeint&operator = (hugeint&);
protected:
    void inverse();
    void clear();
    int inc();
    int *data;
    long size;
//friends:
    friend ostream&operator << (ostream&, hugeint&);
    friend hugeint operator + (hugeint&,hugeint&);
    friend hugeint operator-(hugeint&,hugeint&);
    friend hugeint operator * (hugeint&,hugeint&);
    friend hugeint operator / (hugeint&,hugeint&);
    friend hugeint operator% (hugeint&,hugeint&);
    friend int add(hugeint&, hugeint&, int, int bitm=0);
    friend int mul(hugeint&,int);
    friend int div(hugeint&, hugeint&, hugeint&, hugeint&);
};
hugeint.cpp file
#include<string.h>
#include<iomanip.h>
#include "hugeint.h"
#define ERRORMSG "Over Flow!"
//public:
hugeint::hugeint(int n)
{
    size=(long)n/4;
    data=new int[size];
    for(long i=0;i<size;++i)data[i]=0;
}
hugeint::hugeint(long n)
{
    size=n/4;
    data=new int[size];
    for(long i=0;i<size;++i)data[i]=0;
}
hugeint::hugeint(hugeint&a)
{
    size=a.getsize();
    data=new int[size];
    for(long i=0;i<size;++i)data[i]=a.elem(i);
}
hugeint::~hugeint()
{
    delete[]data;
}
//protected:

void hugeint::inverse()
{
    for(long i=0;i<size;++i)data[i]=9999-data[i];
}
void hugeint::clear()
{
    for(long i=0;i<size;++i)data[i]=0;
}
int hugeint::inc()
{
    int s=1;
    for(long i=0;s&&i<size;++i)
    {
        int t=data[i]+s;
        data[i]=t%10000;
        s=t/10000;
    }
    if(s)return -1;
    return 0;
}
//friends:
ostream&operator <<(ostream&out, hugeint&a)
{
    int *p=a.data+a.size-1;
    while(!*p)--p;
    out<<*p--;
    while(p>(a.data-1))
        out<<setw(4)<<setfill('0')<<*p--;
    return out;
}
hugeint&hugeint::operator = (char *str)
{
    long len=strlen(str),count=0;
    if(len>(size<<2))return *this;
    char *p=str+len-1;
    int t,i;
    while(1)
    {
        if((p-str)<4)break;
        t=0;
        char *ph=p-3;
        for(int i=0;i<4;++i)
        {
            t*=10;
            t+=ph[i]-'0';
        }
        data[count++]=t;
        p-=4;
    }
    t=0;
    for(i=0;i<=p-str;++i)
    {
        t*=10;
        t+=str[i]-'0';
    }
    data[count]=t;
    return *this;
}
hugeint&hugeint::operator = (hugeint&a)
{
    if(size!=a.getsize())return *this;
    for(long i=0;i<size;++i)data[i]=a.elem(i);
    return *this;
}
hugeint operator +(hugeint&a, hugeint&b)
{
    hugeint temp=a;
    if(add(temp,b,0))
        cerr<<ERRORMSG<<endl;
    return temp;
}
hugeint operator -(hugeint&a, hugeint&b)
{
    hugeint temp1=a,temp2=b;
    temp2.inverse();
    add(temp1,temp2,1);
    return temp1;
}
hugeint operator *(hugeint&a, hugeint&b)
{
    hugeint temp1=a,temp2=temp1;
    temp1.clear();
    for(long i=0;i<b.size;++i)
    {
        temp2=a;
        mul(temp2,b.data[i]);
        add(temp1,temp2,0,i);
    }
    return temp1;
}
hugeint operator /(hugeint&a, hugeint&b)
{
    hugeint stemp=a,rtemp=a;
    if(div(a,b,stemp,rtemp))
        cerr<<ERRORMSG<<endl;
    return stemp;
}
hugeint operator %(hugeint&a, hugeint&b)
{
    hugeint stemp=a,rtemp=a;
    if(div(a,b,stemp,rtemp))
        cerr<<ERRORMSG<<endl;
    return rtemp;
}
int add(hugeint&a, hugeint&b, int s, int bitm)
{
    long size;
    if((size=a.size)!=b.size)return -2;
    for(long i=bitm;i<size;++i)
    {
        int t=a.data[i]+b.data[i-bitm]+s;
        a.data[i]=t%10000;
        s=t/10000;
    }
    if(s)return -1;
    return 0;
}
int mul(hugeint&a,int n)
{
    int s=0;
    for(long i=0;i<a.size;++i)
    {
        long t=a.data[i]*n+s;
        a.data[i]=t%10000;
        s=t/10000;
    }
    if(s)return -1;
    return 0;
}
int div(hugeint&a, hugeint&b, hugeint&s, hugeint&r)//s=a/b,r=a%b
{
    hugeint temp1=a,temp2=b,temp3=a;
    s.clear();
    while(1)
    {
        temp2=b;
        temp2.inverse();
        temp3=temp1;
        if(!add(temp1,temp2,1))break;
        if(s.inc())return -1;
    }
    r=temp3;
    return 0;
}
main.cpp file
#include "hugeint.h"
#define N 1000
void main()
{
    hugeint a(N);
    hugeint b=a,c=a;
    a="300000000";
    b="123432543";
    cout<<a+b<<endl
        <<a-b<<endl
        <<a*b<<endl
        <<a/b<<endl
        <<a%b<<endl;
}
Reply

Use magic Report

0

Threads

63

Posts

43.00

Credits

Newbie

Rank: 1

Credits
43.00

 China

Post time: 2021-3-10 13:30:01
| Show all posts
Overload operator>>
Reply

Use magic Report

0

Threads

49

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2021-3-10 13:45:01
| Show all posts
friend istream&operator >> (istream&in, hugeint&hint)
{
char temp[100];
in>>temp;
hint = temp;
return in;
}
Reply

Use magic Report

0

Threads

9

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2021-3-10 14:45:01
| Show all posts
Use the code upstairs
Give a whole
Get it in one file for convenience^-^
//hugeint.h file
#include<iostream.h>
class hugeint
{
public:
hugeint(int);
hugeint(long);
hugeint(hugeint&);
~hugeint();
inline long getsize(){return size;};
inline int elem(long index){return data[index];};
hugeint&operator = (char*);
hugeint&operator = (hugeint&);
protected:
void inverse();
void clear();
int inc();
int *data;
long size;
//friends:
friend ostream&operator << (ostream&, hugeint&);
friend istream&operator >> (istream&in, hugeint&hint);
friend hugeint operator + (hugeint&,hugeint&);
friend hugeint operator-(hugeint&,hugeint&);
friend hugeint operator * (hugeint&,hugeint&);
friend hugeint operator / (hugeint&,hugeint&);
friend hugeint operator% (hugeint&,hugeint&);
friend int add(hugeint&, hugeint&, int, int bitm=0);
friend int mul(hugeint&,int);
friend int div(hugeint&, hugeint&, hugeint&, hugeint&);
};
//hugeint.cpp file
#include<string.h>
#include<iomanip.h>
//#include "hugeint.h"
#define ERRORMSG "Over Flow!"
//public:
hugeint::hugeint(int n)
{
size=(long)n/4;
data=new int[size];
for(long i=0;i<size;++i)data[i]=0;
}
hugeint::hugeint(long n)
{
size=n/4;
data=new int[size];
for(long i=0;i<size;++i)data[i]=0;
}
hugeint::hugeint(hugeint&a)
{
size=a.getsize();
data=new int[size];
for(long i=0;i<size;++i)data[i]=a.elem(i);
}
hugeint::~hugeint()
{
delete[]data;
}
//protected:

void hugeint::inverse()
{
for(long i=0;i<size;++i)data[i]=9999-data[i];
}
void hugeint::clear()
{
for(long i=0;i<size;++i)data[i]=0;
}
int hugeint::inc()
{
int s=1;
for(long i=0;s&&i<size;++i)
{
int t=data[i]+s;
data[i]=t%10000;
s=t/10000;
}
if(s)return -1;
return 0;
}
//friends:
ostream&operator <<(ostream&out, hugeint&a)
{
int *p=a.data+a.size-1;
while(!*p)--p;
out<<*p--;
while(p>(a.data-1))
out<<setw(4)<<setfill('0')<<*p--;
return out;
}
//Overload>>
istream&operator >> (istream&in, hugeint&hint)
{
char temp[100];
in>>temp;
hint = temp;
return in;
}

hugeint&hugeint::operator = (char *str)
{
long len=strlen(str),count=0;
if(len>(size<<2))return *this;
char *p=str+len-1;
int t,i;
while(1)
{
if((p-str)<4)break;
t=0;
char *ph=p-3;
for(int i=0;i<4;++i)
{
t*=10;
t+=ph[i]-'0';
}
data[count++]=t;
p-=4;
}
t=0;
for(i=0;i<=p-str;++i)
{
t*=10;
t+=str[i]-'0';
}
data[count]=t;
return *this;
}
hugeint&hugeint::operator = (hugeint&a)
{
if(size!=a.getsize())return *this;
for(long i=0;i<size;++i)data[i]=a.elem(i);
return *this;
}
hugeint operator +(hugeint&a, hugeint&b)
{
hugeint temp=a;
if(add(temp,b,0))
cerr<<ERRORMSG<<endl;
return temp;
}
hugeint operator -(hugeint&a, hugeint&b)
{
hugeint temp1=a,temp2=b;
temp2.inverse();
add(temp1,temp2,1);
return temp1;
}
hugeint operator *(hugeint&a, hugeint&b)
{
hugeint temp1=a,temp2=temp1;
temp1.clear();
for(long i=0;i<b.size;++i)
{
temp2=a;
mul(temp2,b.data[i]);
add(temp1,temp2,0,i);
}
return temp1;
}
hugeint operator /(hugeint&a, hugeint&b)
{
hugeint stemp=a,rtemp=a;
if(div(a,b,stemp,rtemp))
cerr<<ERRORMSG<<endl;
return stemp;
}
hugeint operator %(hugeint&a, hugeint&b)
{
hugeint stemp=a,rtemp=a;
if(div(a,b,stemp,rtemp))
cerr<<ERRORMSG<<endl;
return rtemp;
}
int add(hugeint&a, hugeint&b, int s, int bitm)
{
long size;
if((size=a.size)!=b.size)return -2;
for(long i=bitm;i<size;++i)
{
int t=a.data[i]+b.data[i-bitm]+s;
a.data[i]=t%10000;
s=t/10000;
}
if(s)return -1;
return 0;
}
int mul(hugeint&a,int n)
{
int s=0;
for(long i=0;i<a.size;++i)
{
long t=a.data[i]*n+s;
a.data[i]=t%10000;
s=t/10000;
}
if(s)return -1;
return 0;
}
int div(hugeint&a, hugeint&b, hugeint&s, hugeint&r)//s=a/b,r=a%b
{
hugeint temp1=a,temp2=b,temp3=a;
s.clear();
while(1)
{
temp2=b;
temp2.inverse();
temp3=temp1;
if(!add(temp1,temp2,1))break;
if(s.inc())return -1;
}
r=temp3;
return 0;
}
//main.cpp file
//#include "hugeint.h"
#define N 1000
void main()
{

hugeint a(N);

hugeint b=a,c=a;
//a="300000000";
//b="123432543";
cout<<"enter a"<<endl;
cin>>a;
cout<<"enter b"<<endl;
cin>>b;

cout<<a+b<<endl
<<a-b<<endl
<<a*b<<endl
<<a/b<<endl
<<a%b<<endl;
}
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list