求人解答源程序
#include <iostream.h>#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#define Max 10000000
void BigNumsAdd(char *numa,char *numb);
void BigNumsSub(char *numa,char *numb);
void BigNumsMul(char *numa,char *numb);
int BigNumsChu(char *numa,char *numb);
int check(char *s);
clock_t start,end;
void inscanf(char c,char *&n1,char *&n2){
char t;
int i=0;
while(1){
t=getchar();
if(t==c && !(c=='-'&&i==0)) break;
n1[i++]=t;
}
n1[i]=0;
i=0;
while(1){
t=getchar();
if(t==10) break;
n2[i++]=t;
}
n2[i]=0;
putchar('=');
}
void switcher(char *n,int *num,int &weishu) //将字符数组转换为整形数组
{ //整形数组的低位至高位分别
int i; //储存数据的个位到高位数字
for(i=0;i<Max+1;i++) num[i]=0;
for(i=1;i<weishu;i++)
num[i]=n[weishu-i]-'0';
if(n[0]=='+' || n[0]=='-')
{
weishu--;
num[0]=n[0];
}
else num[weishu]=n[0]-'0';
}
void BigNumsAdd(char *numa,char *numb) //大整数的相加,其中两个参数分别为两个待输入的字符串
{
int *Num1=NULL,*Num2=NULL,*answer=NULL;
int weishu1,weishu2,wei;
int i,s=0;
if(numa[0]=='-' && numb[0]!='-') //两个数一正一负的时候,改变符号后转跳到相减
{
numa[0]='+';
BigNumsSub(numb,numa);
return;
}
if(numb[0]=='-' && numa[0]!='-')
{
numb[0]='+';
BigNumsSub(numa,numb);
return;
}
weishu1=strlen(numa);
Num1=(int*)malloc((Max+1)*sizeof(int));
weishu2=strlen(numb);
Num2=(int*)malloc((Max+1)*sizeof(int));
switcher(numa,Num1,weishu1);
switcher(numb,Num2,weishu2);
wei=(weishu1>weishu2)?weishu1:weishu2;
answer=(int*)malloc((wei+2)*sizeof(int)); //(wei+2)??防止相加后位数增1
for(i=0;i<wei+2;i++) answer[i]=0;
for(i=1;i<=wei;i++)
{
answer[i]+=(Num1[i]+Num2[i]);
if(answer[i]>=10) //逢十进一
{
answer[i]-=10;
answer[i+1]++;
}
}
if(numa[0]=='-' || numb[0]=='-') //注意负数的时候
printf("-");
if(answer[wei+1]!=0)
printf("%d",answer[wei+1]);
for(i=wei;i>=1;i--)
{
if(s==1)
printf("%d",answer[i]);
else if(answer[i]!=0)
{
s=1;
printf("%d",answer[i]);
}
}
if(s==0) printf("\r0");
printf("\n");
free(Num1);
free(Num2);
free(answer);
}
void BigNumsSub(char *numa,char *numb) //大整数的相减(numa-numb)
{
int *Num1=NULL,*Num2=NULL,*answer=NULL;
int weishu1,weishu2,wei,len,j=0;
int i,sign=0,s=0; //负数情况比正数稍麻烦,于是用sign作为答案的正负符号标志
if(numa[0]=='-' && numb[0]!='-') //两个数一正一负的时候,稍作调整后转跳到相加
{
len=strlen(numb);
numb[len+1]='\0';
for(j=0;j<len;j++){
numb[j+1]=numb[j];
}
numb[0]='-';
BigNumsAdd(numa,numb);
return;
}
if(numb[0]=='-' && numa[0]!='-')
{
numb[0]='+';
BigNumsAdd(numa,numb);
return;
}
weishu1=strlen(numa);
Num1=(int*)malloc((Max+1)*sizeof(int));
weishu2=strlen(numb);
Num2=(int*)malloc((Max+1)*sizeof(int));
switcher(numa,Num1,weishu1);
switcher(numb,Num2,weishu2);
wei=(weishu1>weishu2)?weishu1:weishu2;
answer=(int*)malloc((wei+2)*sizeof(int));
for(i=0;i<wei+2;i++) answer[i]=0;
for(i=1;i<=wei;i++)
{
answer[i]+=(Num1[i]-Num2[i]);
if(answer[i]<0) //借位
{
answer[i]+=10;
answer[i+1]--;
}
}
if(answer[wei+1]<0)
{
for(i=1;i<=weishu2;i++) Num1[i]=0;
for(i=1;i<=weishu2;i++)
{
answer[i]=Num1[i]-answer[i];
if(answer[i]<0)
{
answer[i]+=10;
answer[i+1]++;
}
}
answer[weishu2+1]=0;
sign++;
}
if(numa[0]=='-') sign++; //注意负数的时候
if(sign%2!=0)
printf("-");
for(i=wei;i>=1;i--)
{
if(s==1)
printf("%d",answer[i]);
else if(answer[i]!=0)
{
s=1;
printf("%d",answer[i]);
}
}
if(s==0) printf("\r0");
printf("\n");
free(Num1);
free(Num2);
free(answer);
}
void BigNumsMul(char *numa,char *numb) //大整数乘法
{
int *Num1=NULL,*Num2=NULL,*answer=NULL;
int weishu1,weishu2,wei;
int i,j;
int yu,s=0;
weishu1=strlen(numa);
Num1=(int*)malloc((Max+1)*sizeof(int));
weishu2=strlen(numb);
Num2=(int*)malloc((Max+1)*sizeof(int));
switcher(numa,Num1,weishu1);
switcher(numb,Num2,weishu2);
wei=weishu1+weishu2; //乘法结果的最大位数是两者位数和
answer=(int*)malloc((wei+1)*sizeof(int));
for(i=0;i<wei+1;i++) answer[i]=0;
for(j=1;j<=weishu1;j++)
{
for(i=1;i<=weishu2;i++)
{
answer[j+i-1]+=(Num1[j]*Num2[i]);
if((yu=answer[i+j-1]/10)!=0)
{
answer[i+j]+=yu;
answer[i+j-1]-=yu*10;
}
}
}
if((numa[0]=='-' && numb[0]!='-') || (numa[0]!='-' && numb[0]=='-'))
printf("-");
for(i=wei;i>=1;i--)
{
if(s==1)
printf("%d",answer[i]);
else if(answer[i]!=0)
{
s=1;
printf("%d",answer[i]);
}
}
if(s==0) printf("\r0");
printf("\n");
free(Num1);
free(Num2);
free(answer);
}
int Substract( int * p1, int * p2, int nLen1, int nLen2)
{
int i;
if( nLen1 < nLen2 )
return -1;
//下面判断 p1是否比 p2大,如果不是,返回-1
bool bLarger = false;
if( nLen1 == nLen2 ) {
for( i = nLen1-1; i >= 0; i -- ) {
if( p1[i] > p2[i] )
bLarger = true;
else if( p1[i] < p2[i] ) {
if ( ! bLarger )
return -1;
}
}
}
for( i = 0; i < nLen1; i ++ ) { //做减法
p1[i] -= p2[i]; //要求调用本函数时给的参数能确保当 i>=nLen2时,p2[i] = 0
if( p1[i] < 0 ) {
p1[i]+=10;
p1[i+1] --;
}
}
for( i = nLen1 -1 ; i >= 0 ; i-- )
if( p1[i] )
return i + 1;
return 0;
}
int BigNumsChu(char *numa,char *numb)
{
#define MAX_LEN 10000000
int an1[MAX_LEN + 10];
int an2[MAX_LEN + 10];
int aResult[MAX_LEN + 10];
int t, n;
int *Num1=NULL,*Num2=NULL,*answer=NULL;
int weishu1,weishu2;
Num1=(int*)malloc((MAX_LEN+1)*sizeof(int));
Num2=(int*)malloc((MAX_LEN+1)*sizeof(int));
n=1;
for( t = 0; t < n; t ++ ) {
switcher(numa,Num1,weishu1);
switcher(numb,Num2,weishu2);
int i, j;
int nLen1 = strlen( numa);
memset( an1, 0, sizeof(an1));
memset( an2, 0, sizeof(an2));
memset( aResult, 0, sizeof(aResult));
int nLen2 = strlen(numb);
if( nLen1 < nLen2 ) {
printf("0\n");
continue;
}
nLen1 = Substract( an1, an2, nLen1, nLen2) ;
if( nLen1 < 0) {
printf("0\n");
continue;
}
else if( nLen1 == 0) {
printf("1\n");
continue;
}
aResult[0] ++; //减掉一次了,商加 1
//减去一次后的结果长度是 nLen1
int nTimes = nLen1 - nLen2;
if( nTimes < 0) //减一次后就不能再减了
goto OutputResult;
else if( nTimes > 0 ) {
//将 an2 乘以 10的某次幂,使得结果长度和 an1相同
for( i = nLen1 -1; i >= 0; i -- ) {
if( i >= nTimes )
an2[i] = an2[i-nTimes];
else
an2[i] = 0;
}
}
nLen2 = nLen1;
for( j = 0 ; j <= nTimes; j ++ ) {
int nTmp;
//一直减到不够减为止
//先减去若干个 an2×(10 的 nTimes 次方),
//不够减了,再减去若干个 an2×(10 的 nTimes-1 次方),......
while( (nTmp = Substract(an1, an2+j, nLen1, nLen2-j)) >= 0) {
nLen1 = nTmp;
aResult[nTimes-j]++; //每成功减一次,则将商的相应位加 1
}
}
OutputResult:
//下面的循环统一处理进位问题
for( i = 0; i < MAX_LEN; i ++ ) {
if( aResult[i] >= 10 ) {
aResult[i+1] += aResult[i] / 10;
aResult[i] %= 10;
}
}
//下面输出结果
bool bStartOutput = false;
for( i = MAX_LEN ; i >= 0; i -- )
if( bStartOutput)
printf("%d", aResult[i]);
else if( aResult[i] ) {
printf("%d", aResult[i]);
bStartOutput = true;
}
if(! bStartOutput )
printf("0\n");
printf("\n");
}
system("pause");
return 0;
}
int check(char *s) //字符串检查,当格式错误时候返回1,整个数字为0的时候返回2(用于除法的余数),正常返回0
{
int i;
int l,sign=1;
l=strlen(s);
if(s[0]!='-' && s[0]!='+' && (s[0]>'9' || s[0]<'0'))
{
printf("ERROR:输入格式错误!\n");
return 1;
}
if(s[0]>='0' && s[0]<='9') sign=0;
for(i=1;i<l;i++)
{
if(s[i]!='0') sign=0;
if(s[i]>'9' || s[i]<'0')
{
printf("ERROR:输入格式错误!\n");
return 1;
}
}
if(sign==1) return 2;
else return 0;
}
int main()
{srand(time(NULL));
char *num1,*num2;
int i;
int tmp;
for(i=0;i<33;i++)
printf(" ");
num1=(char*)malloc(Max*sizeof(char));
num2=(char*)malloc(Max*sizeof(char));
while(1)
{
system("cls");
printf("\n");
printf("\t\t\t\t 大整数运算程序\n");
printf("\t\t\t\t\tMenu\n");
printf("\t\t\t**************************************\n");
printf("\t\t\t* *\n");
printf("\t\t\t* 1.大整数的加法 *\n");
printf("\t\t\t* 2.大整数的减法 *\n");
printf("\t\t\t* 3.大整数的乘法 *\n");
printf("\t\t\t* 4.大整数的除法 *\n");
printf("\t\t\t* 5.退出 *\n");
printf("\t\t\t* *\n");
printf("\t\t\t**************************************\n");
printf("\n\n\t\t\t 请选择(1-5):");
scanf("%d",&i);
while((tmp=fgetc(stdin))!='\n');
switch(i)
{
case 1:
printf("输入 大整数A+大整数B:");
inscanf('+',num1,num2);
if(check(num1)==1 || check(num2)==1)
break;
start=clock();
BigNumsAdd(num1,num2);
end=clock();
printf("时间是%f\n",(double)(end-start)/CLOCKS_PER_SEC/1000);
system("pause");
break;
case 2:
printf("输入 大整数A-大整数B:");
inscanf('-',num1,num2);
if(check(num1)==1 || check(num2)==1)
break;
start=clock();
BigNumsSub(num1,num2);
end=clock();
printf("时间是%f\n",(double)(end-start)/CLOCKS_PER_SEC/1000);
system("pause");
break;
case 3:
printf("输入 大整数A*大整数B:");
inscanf('*',num1,num2);
if(check(num1)==1 || check(num2)==1)
break;
start=clock();
BigNumsMul(num1,num2);
end=clock();
printf("运算时间是%f\n",(double)(end-start)/CLOCKS_PER_SEC/1000);
system("pause");
break;
case 4:
printf("输入 大整数A/大整数B:");
inscanf('/',num1,num2);
if(check(num1)==1 || check(num2)==1)
break;
start=clock();
BigNumsChu(num1,num2);
end=clock();
printf("运算时间是%f\n",(double)(end-start)/CLOCKS_PER_SEC/1000);
system("pause");
break;
case 5:
free(num1);
free(num2);
return 0;
default:
printf("输入错误!\n");
}
printf("\n");
}
}
我想问一下~~又谁能能帮我做一下c++吗~~我今天费了好多的力气才把一个大整数计算器的程序整理好~~我是从网上找的~~可是不知道为什么~~一运行大整数的除法的时候~~窗口就自动关闭了~~其他的加减乘法则都是正常的~~能有人帮我看看嘛~