当前位置:网站首页>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 .
边栏推荐
- Lavel document reading notes -how to use @auth and @guest directives in lavel
- DCL statement of MySQL Foundation
- Devop basic command
- Es entry series - 6 document relevance and sorting
- Velodyne configuration command
- Custom type: structure, enumeration, union
- Legion is a network penetration tool
- 按键精灵跑商学习-商品数量、价格提醒、判断背包
- Delayed message center design
- Rhcsa day 10 operation
猜你喜欢
随机推荐
AUTOSAR从入门到精通100讲(106)-域控制器中的SOA
Collection of practical string functions
Deep learning 500 questions
System. Currenttimemillis() and system Nanotime (), which is faster? Don't use it wrong!
leetcode729. My schedule 1
2021-08-11 function pointer
Architecture introduction
Write a program to judge whether the two arrays are equal, and then write a similar program to compare the two vectors.
Si vous ne connaissez pas ces quatre modes de mise en cache, vous osez dire que vous connaissez la mise en cache?
Basic principle of servlet and application of common API methods
For programmers, if it hurts the most...
Does any teacher know how to inherit richsourcefunction custom reading Mysql to do increment?
原生div具有编辑能力
Vs201 solution to failure to open source file HPP (or link library file)
Histogram equalization
uniapp 处理过去时间对比现在时间的时间差 如刚刚、几分钟前,几小时前,几个月前
Exercise 7-2 finding the maximum value and its subscript (20 points)
使用 C# 提取 PDF 文件中的所有文字(支持 .NET Core)
如果不知道這4種緩存模式,敢說懂緩存嗎?
Kotlin: collection use