求C++题目,很难得

2025-01-21 12:13:51
推荐回答(3个)
回答1:

.h文件
#ifndef __CBigInteger__
#define __CBigInteger__ 4096
#include
class CBigInteger
{
private:
char data[__CBigInteger__];
char chmode[__CBigInteger__ + 1];
int toDec(char k)
{
if (k >= '0' && k <= '9') return k - '0';
if (k >= 'a' && k <= 'f') return k - 'a' + 10;
if (k >= 'A' && k <= 'F') return k - 'A' + 10;
return -1;
}
public:
CBigInteger(const char* toParse = "1", bool ignoreElse = false)
{
Parse(toParse, ignoreElse);
}
CBigInteger(unsigned long int toParse)
{
char a[100];
sprintf(a, "%lu", toParse);
Parse(a, false);
}
const char* toString()
{
int i;
for (i = __CBigInteger__ - 1; i >= 0; i--)
{
chmode[__CBigInteger__ - 1 - i] = data[i] + '0';
}
chmode[__CBigInteger__] = 0;
i = 0;
while (chmode[i] == '0') i++;
if (!chmode[i]) i--;
return chmode + i;
}
bool Parse(const char* ch, bool ignoreElse = false)
{
if (ch[0] == '0' && (ch[1] == 'x' || ch[1] =='X'))
{
*this = "0";
CBigInteger nowhex = "1";
CBigInteger temp;
ch++; ch++;
int len = 0;
for (; ch[len]; len++);
for (int i = len - 1; i >= 0; i--)
{
if (toDec(ch[i]) == -1)
{
if (!ignoreElse) return false;
}
else
{
temp = nowhex;
for (int j = 1; j < toDec(ch[i]); j++)
{
temp = temp + nowhex;
}
nowhex = nowhex + nowhex;
nowhex = nowhex + nowhex;
nowhex = nowhex + nowhex;
nowhex = nowhex + nowhex;
*this = *this + temp;
}
}
return true;
}
else
{
int offset = 0;
for (int i = 0; i < __CBigInteger__; i++) data[i] = 0;
int len = 0;
for (; ch[len]; len++);
for (int i = len - 1; i >= 0; i--)
{
if (ch[i] >= '0' && ch[i] <= '9')
{
data[len - 1 - i - offset] = ch[i] - '0';
}
else
{
if (ignoreElse) offset++;
else return false;
}
}
return true;
}
}
const CBigInteger operator + (const CBigInteger another)
{
CBigInteger result = another;
for (int i = 0; i < __CBigInteger__; i++)
{
result.data[i] += this->data[i];
if (result.data[i] > 9)
{
result.data[i + 1]++;
result.data[i] -= 10;
}
}
return result;
}
};
#endif
.cpp
#include "CBigInteger.h"
#include
#include
using namespace std;
int main()
{
char fname[100];
cout << "输入文件1:";
cin >> fname;
ifstream fin(fname);
cout << "输入文件2:";
cin >> fname;
ifstream fin2(fname);
CBigInteger a, b, c;
fin >> fname;
a.Parse(fname, true);
fin2 >> fname;
b.Parse(fname, true);
fin.close(); fin2.close();
c = a + b;
cout << "结果:";
cout << c.toString() << endl;
system("pause");
return 0;
}
声明不是我做出来的 嘿嘿 你看看吧

回答2:

不定长的,为了省事把结果送第一个数了
#include "iostream.h"
#include "string.h"
using namespace std
class CBigInteger
{
private:
char *BigInt;
public:
void display()
{
int lr=strlen(BigInt);
for(int i=0;i cout< cout< }
CBigInteger operator+(CBigInteger &num_b)
{
CBigInteger res;
int lb=strlen(num_b.BigInt);
int l0=strlen(BigInt);
int lmax=lb>l0?lb:l0;
int lmin=lb>l0?l0:lb;
int flag=0;
res.BigInt=new char[lmax+2];
for(int i=0;i {
char b='0';
if(i b=num_b.BigInt[i];
if(lb>l0)
res.BigInt[i]=num_b.BigInt[i]+b-48+flag;
else
res.BigInt[i]=BigInt[i]+b-48+flag;
flag=0;
if(res.BigInt[i]-48>9)
{
flag=1;
res.BigInt[i]-=10;
}
}
res.BigInt[lmax]=flag==1?flag+48:'\0';
res.BigInt[lmax+1]='\0';
BigInt=new char[lmax+2];
strcpy(BigInt,res.BigInt);
return res;
}
CBigInteger(char *filename)
{
FILE* fp;
char c;
fp=fopen(filename,"r+");
fseek(fp,0,SEEK_END);
int len=ftell(fp),scl=0;
char *str=new char[len];
fseek(fp,0,SEEK_SET);
do
{
c=fgetc(fp);
if(c>=48&&c<58)
{
*(str+scl)=c;
scl++;
}
}while(c!=EOF);
fclose(fp);
for(int i=0;i {
char tmp=*(str+i);
*(str+i)=*(str+scl-1-i);
*(str+scl-1-i)=tmp;
}
BigInt=new char[scl+1];
memcpy(BigInt,str,scl);
*(BigInt+scl)='\0';
delete str;
}
CBigInteger()
{
}
~CBigInteger()
{
delete BigInt;
}
};

int main(int argc, char* argv[])
{
CBigInteger c1("a.txt"),c2("b.txt");
c1+c2;
c1.display();
return 0;
}

回答3:

作业还是自己做的好