C Language Note: Calculation of precision

Original link: http://c.biancheng.net/c/

C language type of large data Brief

As we know, internal computer to store data using data type int or double, etc. is to limit the scope of, when the operation data is large, the computer will overflow occurs, so that the calculation result is not accurate. For example, a 20-bit decimal integer, if stored with the int type variables, data overflow will occur. When the number exceeds the integer arithmetic, real range can be represented, certainly can not be directly used in the form of a number is represented. In the course of operation, the large number of data can be represented in two types: a string and an integer array

  • Integer array: Each element is stored one, how many bits the number of array elements is required; forms are a number of each, subtract directly, very convenient operation, but can not enter all integer array elements directly, only a one input, and the input, there must be a delimiter between each two digits, the input value does not meet the people's habits, inconvenient input and output.
  • String (is essentially an array of characters): What is the maximum length of the string, it can indicate how many digits. String represents all bit values can be directly input and output, but each bit string is a character, it must be further converted into a numerical calculation, inconvenient operation

Advantages and disadvantages of the comprehensive array of integers and an array of characters, we in the next question, read in the data string, integer array dump operation carried out during operation, followed by conversion to a string output.

In fact, high-precision calculation method is through programming, the simple mathematical calculation steps on the computer again demonstrates perfectly just.

Precision adder

Description of the problem:
find the two is not more than 200 non-negative integer and. Two input lines of not more than 200 is a non-negative integer, no extra leading 0; an output of the calculation formula, and the addition result, the results of which can not have extra leading zero.

Analysis:
with a string to hold a value of not more than 200 large integer, and when the operation for convenience, will be transferred to the character string stored in the large integer array of integers stored. When two numbers together and because a first bit aligned, then calculated from the low-order, and in fact when the user inputs, integer array indices corresponding to the most significant bit is 0, and the integer array index corresponding to the maximum bit when, the string stored addend transferred to an array of integers, opposed first inverse conversion, i.e., an integer array labeled bits corresponding to 0, and the integer array labeled 1 correspond to ten. . .
Here Insert Picture Description

#include <stdio.h>
#include <string.h>
#define MAXLEN 210
void Invert(char *a,int *b);//将a字符逆置转换到整数数组b中,确保下标0对应个位而不是最高位 
void Output(int *p,int len);//输出整型数组元素 
int main(void)
{
 char str1[MAXLEN],str2[MAXLEN],str[MAXLEN];//存放两个加数(输入)以及和(输出)的字符串
 int a[MAXLEN],b[MAXLEN],c[MAXLEN];//存放加数以及和的整型数组(中间处理) 
 printf("输入两个加数:\n");
 scanf("%s %s",str1,str2);
 //整型数组a,b,c的元素全部清零,memset函数一般用于在对定义的字符串进行初始化为"\0",对较大的结构体或数组进行清0操作的一种最快方法 
 memset(a,0,sizeof(a));
 memset(b,0,sizeof(b)); 
 memset(c,0,sizeof(c));
 //将两个加数字字符串按位逆置存放到整型数组中,下标0对应个位
 Invert(str1,a);
 Invert(str2,b);
 printf("******************运算过程(列竖式做加法)******************\n");
 Output(a,strlen(str1));
 printf("\n");
 Output(b,strlen(str2));
 int len=strlen(str1)>=strlen(str2) ? strlen(str1):strlen(str2);//求加数较长的位数
 for(int i=0;i<len;i++)//从第一位到最高位逐位相加运算 
 {
  c[i]+=a[i]+b[i];
  c[i+1]=c[i]/10;//c[i]能除多少个10就表示进多少个位,i位的进位数存放到c[i+1]上
  c[i]%=10; //c[i]进完位后的数则是(a[i]+b[i])求模10,为余下的数 
 } 
 printf("\n");
 Output(c,len);
 printf("\n******************运算过程******************");
 while(len>=0&&c[len]==0)//和的处理,去掉前导0,并把结果复制到串中
 {
  len--;
 } 
 memset(str,0,sizeof(str));//0<=>'\0'字符串结束符 
 int i=0;
 for(int j=len;j>=0;j--)
 {
  str[i++]=c[j]+'0';//整型数字转换为字符型数字 
 } 
  if(strlen(str)==0)
 {
  str[0]='0';//结果为0的情况 
 }
 printf("\n");
 printf("运算结果:%s + %s = %s\n",str1,str2,str);
 return 0;
} 
void Invert(char *a,int *b)
{
 int len=strlen(a),j=0;
 for(int i=len-1;i>=0;i--)
 {
  b[j++]=a[i]-'0';
 }
}
void Output(int *p,int len)
{
 for(int i=0;i<len;i++)
 {
  printf("%d",p[i]);
 }
}

Operation output:
Here Insert Picture Description
Code doubts:
how concrete operational integer array it? I.e., simulated primary vertical columns do addition method, bits are aligned to the start position corresponding to the upper bit by bit from the sum, and greater than or equal to 10 carry. In the following source code, with int a [210] The first addend stored, int b [210] The second addend to save, and then adding bit by bit, the result of adding two numbers stored in the int c [210 ], in which the following code segment is to be processed into the position where

 for(int i=0;i<len;i++)//从第一位到最高位逐位相加运算 
 {
  c[i]+=a[i]+b[i];
  c[i+1]=c[i]/10;//c[i]能除多少个10就表示进多少个位,i位的进位数存放到c[i+1]上
  c[i]%=10; //c[i]进完位后的数则是(a[i]+b[i])求模10,为余下的数 
 } 

Precision multiplication

Description of the problem:
the product of the two requirements of not more than 1000 large integers. Two input lines, each line is an integer of not more than 1000, no extra leading 0; formula and outputs the multiplied result. The results can not have extra leading 0.

Analysis:
Here the multiplication sign to be considered, data structures change, consider just changing the simulation process is multiplication algorithm:

Guess you like

Origin blog.csdn.net/weixin_42124234/article/details/102725470