⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 largeint_multi.cpp

📁 从文件读入两个很大的整数
💻 CPP
字号:
/*大整数乘法*/

#include<iostream.h>
#include<fstream.h>
#include<sstream>
#define MAX 10000


void main()
{
	char EXP[MAX];//暂存从文件读入的字符 
	int counter=0;


	ifstream reada("e:\\a.txt",ios::in||ios::nocreate);	//从a.txt读入被乘数
	while(!reada.eof()) 
	{
		EXP[counter]=reada.get();
		counter++;
	}
	reada.close();
	
	int al=counter-1;
	char *a;
	a=new char[al];//定义动态数组a[al]存被乘数
	for(int m=0;m<al;m++)
		a[m]=EXP[m];

	counter=0;
	
	
	ifstream readb("e:\\b.txt",ios::in||ios::nocreate);	//从b.txt读入乘数
	while(!readb.eof()) 
	{
		EXP[counter]=readb.get();
		counter++;
	}
	readb.close();

	int bl=counter-1;
	char *b;
	b=new char[bl];//定义动态数组b[bl]存被乘数
	for(int n=0;n<bl;n++)
		b[n]=EXP[n];


	for(int p=0;p<al;p++)
		cout<<a[p];
	cout<<endl;
	for(int q=0;q<bl;q++)
		cout<<b[q];
	cout<<endl;

	int cl=al+bl;//结果数组长度
	int *x;
	x=new int[cl];//定义动态数组c[cl]存结果
	memset(x,0,sizeof(int)*cl);//数组清零
	
	int cp;
	for(int bp=bl-1;bp>=0;bp--)//乘数从低位到高位循环
	{
		for(int ap=al-1;ap>=0;ap--)//被乘数从低位到高位循环
		{
			cp=cl-((al-ap)+(bl-bp))+1;//定位结果在c[]中的位置
			x[cp]+=(a[ap]-48)*(b[bp]-48);//做乘法并累加(0的ASCII码为48)
		}
	}
	for(int i=cl-1;i>0;i--)//结果从低位到高位循环
	{
		while(x[i]>10)//处理进位
		{
			x[i-1]+=x[i]/10;
			x[i]=x[i]%10;
		}
	}


	ofstream write("e:\\c.txt",ios::out);
	bool start=false;
	for(int j=0;j<cl;j++)
	{	
		if(x[j]==0&&start==false)
			continue;
		else
			start=true;
		write<<x[j];		
	}
	write.close();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -