当前位置:网站首页>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 .
边栏推荐
- Ruby时间格式转换strftime毫秒匹配格式
- Rhcsa12
- Basic data types of MySQL
- Use the data to tell you where is the most difficult province for the college entrance examination!
- Ruby time format conversion strftime MS matching format
- Machine learning -- neural network (IV): BP neural network
- OSPF comprehensive experiment
- Tables in the thesis of latex learning
- Linked list operation can never change without its roots
- Work order management system OTRs
猜你喜欢
Recursion and divide and conquer strategy
IPv6 comprehensive experiment
Delayed message center design
What is an excellent architect in my heart?
Idea SSH channel configuration
Development guidance document of CMDB
Dynamic memory management
Work order management system OTRs
183 sets of free resume templates to help everyone find a good job
【Day2】 convolutional-neural-networks
随机推荐
Time complexity and space complexity
使用 C# 提取 PDF 文件中的所有文字(支持 .NET Core)
Ruby时间格式转换strftime毫秒匹配格式
leetcode842. Split the array into Fibonacci sequences
system design
System.currentTimeMillis() 和 System.nanoTime() 哪个更快?别用错了!
Recursion and divide and conquer strategy
C language - stack
How can Huawei online match improve the success rate of player matching
PHP代码审计3—系统重装漏洞
Delayed message center design
Knapsack problem and 0-1 knapsack problem
对于程序员来说,伤害力度最大的话。。。
Differences among opencv versions
Intelligent gateway helps improve industrial data acquisition and utilization
Introduction to tree and binary tree
C language structure to realize simple address book
转载:等比数列的求和公式,及其推导过程
If you don't know these four caching modes, dare you say you understand caching?
A little feeling