当前位置:网站首页>Huge number (C language)
Huge number (C language)
2022-07-04 10:27:00 【Lol only plays Timo】
What is a huge number ? seeing the name of a thing one thinks of its function , Huge numbers are especially large numbers , As big as C Language is no longer recognizable , For example, thousands of trillions of addition, subtraction, multiplication and division ,C Linguistic int, And long int Obviously, it can't be expressed , Because they are all four byte complements , The range of integer numbers that can be represented is -2,147,483,648 ~ 2,147,483,647, namely -2^32 ~ 2^32-1.
So how to solve this problem ?
First , according to int We can use Ten thousand system , Similar to decimal , It's just that the decimal is 4 One by one , The decimal system is a group of digits . Speaking of this, some people may be confused , Why is it ten thousand decimal , Not 100000 , Or thousands , Choose a decimal . Because if we are doing multiplication , Everyone is at most 9999 The multiplication of the two will not exceed int Representation range of , It's not true int The representation range of causes too much waste .
Now let's solve the problem of digital storage :
We can save the numbers into the file in advance , Transfer directly from the file , This is convenient for our testing , And it is convenient for us to modify , After the data is transferred from the file, the next step is to convert these a lot of strings into hexadecimal and store them in a int In an array of types .
Here's a tip , It's the opposite , In other words, the low order of a huge number is stored in the front of the array , The higher the bit, the lower the stored subscript , The advantage of doing this is that it is very consistent with our computing habits when performing operations, starting from the low order , And it is also subscripted from the array 0 Start with the number of . Then let's take a look at the code :
void storeInfor(HUGE_NUM *NUM, char *temp) {
int i = 0;
int j = 0;
int k = 0;
int flag = 1;
if (temp[0] == '-') {
NUM->sign = 1;
for (i = 0; i < NUM->count-1; i++) {
temp[i] = temp[i+1];
}
NUM->count--;
} else {
NUM->sign = 0;
}
for (j = NUM->count-1, i = 0; j >= NUM->count%4 + 3; i++) {
NUM->value[i] = ((int)temp[j]-48) + ((int)temp[j-1] - 48) * 10 + ((int)temp[j-2] - 48)* 100 + ((int)temp[j-3] - 48)* 1000;
j = j-4;
}
for (k = NUM->count%4-1,flag = 1; k >= 0; k--) {
if (k == NUM->count%4-1) {
NUM->value[i] = ((int)temp[k]-48) * flag;
} else {
NUM->value[i] += ((int)temp[k]-48) * flag;
}
flag = flag * 10;
}
printInfor(NUM);
}
It doesn't matter if you can't understand some of the code here , Finally, I will attach all the complete code , For your reference .
At this point, we have saved all the figures , Let's first look at the basic operations - Addition and subtraction .
Of course, the addition operation is very simple , Here you can first try how to realize the addition operation of decimal .
Compared with addition , Subtraction is much harder to achieve , Because of carry, we can easily add one to him , But excuse me , If there is continuous borrowing, it will be a big test for us , What's going on ?
Here I would like to introduce a way studied by my teacher Zhu Hong - Wechat complement , According to the underlying principle knowledge , Inferential , We all know that computer subtraction is realized by complement , My teacher's method is the same . Next, let's take a look at this hand of God :
Now these main knowledge and technologies have been told to you , Let's take a look at my code :
#include <stdio.h>
#include <malloc.h>
typedef struct HUGE_NUM {
int sign;
int *value;
int count;
}HUGE_NUM;
void storeInfor(HUGE_NUM *NUM, char *temp);
void addNum(HUGE_NUM *NUM1, HUGE_NUM *NUM2, HUGE_NUM *NUM3, char *temp1, char *temp2);
void printInfor(HUGE_NUM *NUM);
void printInfor(HUGE_NUM *NUM) {
int i;
int count = (NUM->count+3) / 4 - 1;
if (NUM->sign == 1) {
printf("-:");
} else {
printf("+:");
}
if(NUM->value[(NUM->count+3) / 4 - 1] == 0) {
count = (NUM->count+3) / 4 - 2;
}
for (i = count; i >= 0; i--) {
if (i == count) {
printf("%d ",NUM->value[i]);
} else {
printf("%04d ",NUM->value[i]);
}
}
printf("\n");
}
void addNum(HUGE_NUM *NUM1, HUGE_NUM *NUM2, HUGE_NUM *NUM3, char *temp1, char *temp2) {
int sign;
int *caculate1;
int *caculate2;
int count;
int i;
if (NUM1->count >= NUM2->count) {
count = (NUM1->count + 3) / 4 + 1;
caculate1 = (int *) calloc(sizeof(int),count);
caculate2 = (int *) calloc(sizeof(int),count);
} else {
count = (NUM2->count + 3) / 4 + 1;
caculate1 = (int *) calloc(sizeof(int),count);
caculate2 = (int *) calloc(sizeof(int),count);
}
for (i = 0; i < (NUM1->count+3)/4; i++) {
caculate1[i] = NUM1->value[i];
}
for (i = 0; i < (NUM2->count+3)/4; i++) {
caculate2[i] = NUM2->value[i];
}
if (NUM1->sign == NUM2->sign) {
if(NUM1->sign == 0) {
NUM3->sign = 0;
} else {
NUM3->sign = 1;
}
for (i = 0; i < count; i++) {
if (i == 0) {
NUM3->value[i] = (caculate1[i]+caculate2[i])%10000;
} else {
NUM3->value[i] = (caculate1[i]+caculate2[i])%10000 + (caculate1[i-1]+caculate2[i-1])/10000;
if(NUM3->value[i-1] == 10000) {
NUM3->value[i]++;
NUM3->value[i-1] = 0;
}
}
}
if(NUM3->value[count-1] >= 10000) {
NUM3->value[count-1] -= 10000;
NUM3->value[count]++;
}
} else {
if (NUM1->sign == 1) {
for (i = 0; i < count; i++) {
caculate1[i] = 9999 - caculate1[i];
}
}
if (NUM2->sign == 1) {
for (i = 0; i < count; i++) {
caculate2[i] = 9999 - caculate2[i];
}
}
for (i = 0; i < count; i++) {
if (i == 0) {
NUM3->value[i] = (caculate1[i]+caculate2[i])%10000;
} else {
NUM3->value[i] = (caculate1[i]+caculate2[i])%10000 + (caculate1[i-1]+caculate2[i-1])/10000;
if(NUM3->value[i-1] == 10000) {
NUM3->value[i]++;
NUM3->value[i-1] = 0;
}
}
}
if(NUM3->value[count-1] >= 10000) {
NUM3->value[count-1] -= 10000;
NUM3->value[count]++;
}
if (NUM3->value[count] == 1) {
NUM3->value[0] += 1;
NUM3->sign = 0;
}
if (NUM3->value[count] == 0) {
NUM3->sign = 1;
for (i = 0; i < count; i++) {
NUM3->value[i] = 9999 - NUM3->value[i];
}
}
}
}
void storeInfor(HUGE_NUM *NUM, char *temp) {
int i = 0;
int j = 0;
int k = 0;
int flag = 1;
if (temp[0] == '-') {
NUM->sign = 1;
for (i = 0; i < NUM->count-1; i++) {
temp[i] = temp[i+1];
}
NUM->count--;
} else {
NUM->sign = 0;
}
for (j = NUM->count-1, i = 0; j >= NUM->count%4 + 3; i++) {
NUM->value[i] = ((int)temp[j]-48) + ((int)temp[j-1] - 48) * 10 + ((int)temp[j-2] - 48)* 100 + ((int)temp[j-3] - 48)* 1000;
j = j-4;
}
for (k = NUM->count%4-1,flag = 1; k >= 0; k--) {
if (k == NUM->count%4-1) {
NUM->value[i] = ((int)temp[k]-48) * flag;
} else {
NUM->value[i] += ((int)temp[k]-48) * flag;
}
flag = flag * 10;
}
printInfor(NUM);
}
int main () {
FILE *fp1;
FILE *fp2;
char temp1[10000];
char temp2[10000];
int i = 0;
int j = 0;
int k = 0;
int flag = 1;
HUGE_NUM NUM1;
HUGE_NUM NUM2;
HUGE_NUM NUM3;
fp1 = fopen("testdata1.txt","r");
fp2 = fopen("testdata2.txt","r");
fseek(fp1,0,SEEK_END);
NUM1.count = ftell(fp1);
fseek(fp2,0,SEEK_END);
NUM2.count = ftell(fp2);
if (NUM1.count >= NUM2.count) {
NUM3.count = NUM1.count + 1;
} else {
NUM3.count = NUM2.count + 1;
}
NUM1.value = (int *) calloc(sizeof(int), ((NUM1.count+3) / 4));
NUM2.value = (int *) calloc(sizeof(int), ((NUM2.count+3) / 4));
NUM3.value = (int *) calloc(sizeof(int), ((NUM3.count+3) / 4));
fseek(fp1,0,SEEK_SET);
fseek(fp2,0,SEEK_SET);
fgets(temp1,1000,fp1);
fgets(temp2,1000,fp2);
storeInfor(&NUM1,temp1);
storeInfor(&NUM2,temp2);
addNum(&NUM1, &NUM2, &NUM3, temp1, temp2);
printInfor(&NUM3);
fclose(fp1);
fclose(fp2);
return 0;
}
It's not very good , You can point out any problems , Welcome to leave a message below , The multiplication program will be updated later .
边栏推荐
- System.currentTimeMillis() 和 System.nanoTime() 哪个更快?别用错了!
- Differences among opencv versions
- Vs201 solution to failure to open source file HPP (or link library file)
- Press the button wizard to learn how to fight monsters - identify the map, run the map, enter the gang and identify NPC
- MPLS: multi protocol label switching
- Three schemes of ZK double machine room
- Static comprehensive experiment ---hcip1
- Whether a person is reliable or not, closed loop is very important
- How can Huawei online match improve the success rate of player matching
- Ruby time format conversion strftime MS matching format
猜你喜欢
今日睡眠质量记录78分
RHCE - day one
Jianzhi offer 04 (implemented in C language)
5g/4g wireless networking scheme for brand chain stores
Online troubleshooting
Tables in the thesis of latex learning
入职中国平安三周年的一些总结
Time complexity and space complexity
基于线性函数近似的安全强化学习 Safe RL with Linear Function Approximation 翻译 1
【Day1】 deep-learning-basics
随机推荐
Ruby时间格式转换strftime毫秒匹配格式
Static comprehensive experiment ---hcip1
C language - stack
Some summaries of the third anniversary of joining Ping An in China
The time difference between the past time and the present time of uniapp processing, such as just, a few minutes ago, a few hours ago, a few months ago
Read a piece of text into the vector object, and each word is stored as an element in the vector. Convert each word in the vector object to uppercase letters. Output the converted elements in the vect
Lavel document reading notes -how to use @auth and @guest directives in lavel
Latex arranges single column table pictures in double column format articles
基于线性函数近似的安全强化学习 Safe RL with Linear Function Approximation 翻译 2
Use the data to tell you where is the most difficult province for the college entrance examination!
Rhcsa12
Service developers publish services based on EDAs
Exercise 7-2 finding the maximum value and its subscript (20 points)
Leetcode48. Rotate image
今日睡眠质量记录78分
Rhcsa - day 13
uniapp 小于1000 按原数字显示 超过1000 数字换算成10w+ 1.3k+ 显示
Batch distribution of SSH keys and batch execution of ansible
leetcode729. My schedule 1
Latex insert picture, insert formula