高精度加法运算~简便版

//1.把数存进字符数组 因为字符数组代表以'\0'结尾的字符串,所以长度要减1
//2.把字符数组的数    倒序存进整形数组
//3.把整形数组的数按竖式运算 右对齐 若长度小于 则用0来填补
//4.整形数组的数相加减 满10进1
#include "stdafx.h"
#include"iostream"
#include"string"
using namespace std;
void First(char[], char[], int[], int[],int[]);
int main()
{
 char num_one[1001]; char number_two[1001];
 int one_num[1001]; int two_num[1001]; int three_num[1001];
 cin >> num_one >> number_two;
 First(num_one, number_two, one_num, two_num, three_num);
 cin.get();
 cin.get();
 return 0;
}
void First(char num_one[], char number_two[], int one_num[], int two_num[],int three_num[])

 int one_length = strlen(num_one);
 int two_length = strlen(number_two);
 
 for (int i = 0; i <=one_length-1; i++)     
 {
  one_num[one_length - i] = num_one[i] - 48;
  
 }
 for (int  j = 0; j <=two_length-1; j++)
 {
  two_num[two_length - j] = number_two[j] - 48;
  
 }
 
 int time = 1, x = 0, y = 0;
 while (time<=one_length||time<=two_length)
 { 
  if (one_length +y<time&&one_length<two_length)
  { 
   one_num[time] = 0;
   y++;
  
  }
  else if(two_length + y<time&&two_length<one_length)
  {
   two_num[time] = 0;
   y++;
  }
  three_num[time] = (one_num[time] + two_num[time] + x);  
  x = three_num[time] / 10;
  three_num[time] = three_num[time] % 10;
  time++;
 }
 for (int i = time-1; i>=1; i--)
 { 
  if (three_num[(time--)-1]!=0)
  {
   cout << three_num[i];
   time = ++time;
  }
  
 }
}

猜你喜欢

转载自blog.csdn.net/qq_32952043/article/details/77900588