大整数四则运算_C语言课程设计

* 大整数的四则运算。

  * 1 大整数指超过十位的十进制整数,这里为简便,假定不超过五十位。

  * 2 用数组来表示大整数,在此基础上编写出实现大整数加、减、乘、除

  * 的程序,并努力加以优化。

  */

  #include<stdio.h>

  #include<math.h>

  #include<string.h>

  #include<stdlib.h>

  #define MAX 50

  /*

  * 处理大数的加法论文网http://www.lwfree.com/  

  */

  void getsum(char bigIntFir[MAX],charbigIntSec[MAX]){

  int i,j,k,m,mid;

  char ch[MAX];

  for(i=0;bigIntFir!=NULL;i++);

  for(j=0;bigIntSec[j]!=NULL;j++);

  for(k=0;k<MAX;k++)

  ch[k]='0';

  i--;

  j--;

  m=0;

  while(i>=0&&j>=0){

  mid=(bigIntFir-'0')+(bigIntSec[j]-'0');

  if((mid+(ch-'0'))>=10)

  {

  ch=((ch-'0')+mid-10)+'0';

  ch[m+1]=((ch[m+1]-'0')+1)+'0';

  m++;

  }

  else{

  ch=(ch-'0')+mid+'0';

  m++;

  }

  i--;

  j--;

  }

  while(i>=0){

  ch=(ch-'0')+(bigIntFir-'0')+'0';

  i--;

  m++;

  }

  while(j>=0){

  ch=(ch-'0')+(bigIntSec[j]-'0')+'0';

  j--;

  m++;

  }

  if(ch=='0')

  m--;

  printf("result is: ");

  while(m>=0){

  printf("%c",ch);

  m--;

  }

  }

  /*

  * 处理数组的减法

  */

  void getminus(charbigIntFir[MAX],char bigIntSec[MAX]){

  int i,j,k,m,mid;

  char ch[MAX];

  for(i=0;bigIntFir!=NULL;i++);

  for(j=0;bigIntSec[j]!=NULL;j++);

  for(k=0;k<MAX;k++)

  ch[k]='0';

  i--;

  j--;

  m=0;

  if(i>=j||(bigIntFir-bigIntSec[j])>=0){

  while(i>=0&&j>=0){

  mid=(bigIntFir-'0')-(bigIntSec[j]-'0');

  if(ch!='-'&&(mid+(ch-'0'))<0){

  ch[m+1]='-';

  ch=((ch-'0')+mid+10)+'0';

  m++;

  } else if (ch!='-'&&(mid+(ch-'0'))>=0)

  {

  ch=(ch-'0')+mid+'0';

  m++;

  }else if(ch=='-'){

  ch=(mid-1)+'0';

  }

  i--;

  j--;

  }

  while(i>=0){

  if(ch=='-'){

  ch=((bigIntFir-'0')-1)+'0';

  }else {

  ch=bigIntFir;

  }

  i--;

  m++;

  }

  while(j>=0){

  if(ch=='-'){

  ch=((bigIntSec[j]-'0')-1)+'0';

  }else

  ch=bigIntSec[j];

  j--;

  m++;

  }

  if(ch=='0')

  m--;

  printf("resultis: ");

  while(m>=0&&ch!='-'){

  printf("%c",ch);

  m--;

  }

  }else{

  printf("ERROR!");

  }

  }

  /*

  *处理数组的乘法

  *注:只能执行多为乘以个位数

  */

  void getmultiply(charbigIntFir[MAX],char bigIntSec[MAX]){

  int i,j,k,m,mid;

  char ch[MAX];

  for(i=0;bigIntFir!=NULL;i++);

  for(j=0;bigIntSec[j]!=NULL;j++);

  for(k=0;k<MAX;k++)

  ch[k]='0';

  i--;

  j--;

  m=0;

  if(j==0){

  while(i>=0){

  mid=(bigIntFir-'0')*(bigIntSec[j]-'0');

  if((mid+(ch-'0'))>=10){

  ch=((ch-'0')+mid%10)+'0';

  ch[m+1]=((ch[m+1]-'0')+mid/10)+'0';

  m++;

  }else{

  ch=(ch-'0')+mid+'0';

  m++;

  }

  i--;

  }

  printf("result is: ");

  if(ch=='0')

  m--;

  while(m>=0){

  printf("%c",ch);

  m--;

  }

  }else{

  printf("\nSorry! input ERROR!\n");

  }

  }

  /*

  * 处理数组的除法

  * 注:只能处理多位数除以一位数

  */

  void getdiv(char bigIntFir[MAX],charbigIntSec[MAX]){

  int i,j,k,m,mid;

  char ch[MAX];

  int temp1=0,temp2=0;

  for(i=0;bigIntFir!=NULL;i++);

  i--;

  for(j=0;bigIntSec[j]!=NULL;j++);

  j--;

  printf("the result is:");

  if(i<9){

  for(k=0;k<=i;k++){

  temp1=(bigIntFir[k]-'0')+temp1*10;

  }

  for(k=0;k<=j;k++){

  temp2=(bigIntSec[k]-'0')+temp2*10;

  }

  printf("%.2f\n",temp1/(float)temp2);/**/

  }else{

  for(k=0;k<8;k++){

  temp1=(bigIntFir[k]-'0')+temp1*10;

  }

  printf("%d",temp1/(bigIntSec[0]-'0'));

  temp1=temp1%(bigIntSec[0]-'0');

  for(k=8;k<=i;k++){

  temp1=(bigIntFir[k]-'0')+temp1*10;

  }

  printf("%d",temp1/(bigIntSec[0]-'0'));

  }

  }

  void Control(char bigIntFir[MAX],char bigIntSec[MAX]){

  char ch;

  printf("\na :plus (+)\nb:sub (-)\nc :mul (*)\nd :divi (/)\n");

  printf("Now,scanf yourchance according to the view: ");

  scanf("%s",&ch);

  switch(ch){

  case 'a': printf("\nYou choose plus...\n");

  getsum(bigIntFir,bigIntSec);

  break;

  case 'b': printf("\nYou choose sub ...\n");

  getminus(bigIntFir,bigIntSec);

  break;

  case 'c': printf("\nYou choose mul...\n");

  getmultiply(bigIntFir,bigIntSec);

  break;

  case 'd': printf("\nYou choose div...\n");

  getdiv(bigIntFir,bigIntSec);

  break;

  defalut: printf("\nThe input you set is wrong,Ican do nothing!\n");

  break;

  }

  }

  void main(){

  charbigIntFir[MAX],bigIntSec[MAX],ch;

  int i,j,m,n;

  printf("Input the firstInteger : ");

  scanf("%s",bigIntFir);

  printf("Input the secondInteger: ");

  scanf("%s",bigIntSec);

  printf("\n");

  Control (bigIntFir,bigIntSec);

  exit(0);

  }

  • 上一篇文章:
  • 下一篇文章: 没有了
  • Copyright © 2007-2012 www.chuibin.com 六维论文网 版权所有