Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lab2 #213

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

lab2 #213

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 119 additions & 0 deletions lab2/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#include <stdio.h>
#include <string.h>

const long long SIZE = 35;
const long long BASE = 1e9;


typedef struct uint1024_t
{
long long digits[35];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А зачем тогда создавать константу SIZE?


} uint1024_t;

uint1024_t from_uint(unsigned long x){
uint1024_t result;
for(int i = 0; i < SIZE; i++){
result.digits[i] = 0;
}
int next = 0;
while (x)
{
result.digits[next++] = x % BASE;
x /= BASE;
}
return result;
}

uint1024_t add_op(uint1024_t x, uint1024_t y){
for(int i = 0; i < SIZE; i++){
x.digits[i] += y.digits[i];
}
for(int i = 0; i < SIZE - 1; i++){
if(x.digits[i] >= BASE){
x.digits[i] -= BASE;
x.digits[i+1]++;
}
}
return x;
}

uint1024_t subtr_op(uint1024_t x, uint1024_t y){
for(int i = 0; i < SIZE; i++){
x.digits[i] -= y.digits[i];
}
for(int i = 0; i < SIZE - 1; i++){
if(x.digits[i] < 0){
x.digits[i] += BASE;
x.digits[i+1]--;
}
}
return x;
}

uint1024_t mult_op(uint1024_t x, uint1024_t y){
uint1024_t result = from_uint(0);

for(int i = 0; i < SIZE; i++){
for(int j = 0; j < SIZE - i; j++){
result.digits[i+j] += x.digits[i] * y.digits[j];
}
}

for(int i = 0; i < SIZE - 1; i++){
result.digits[i+1] += result.digits[i] / BASE;
result.digits[i] %= BASE;
}
return result;
}

void printf_value(uint1024_t x){
int i = SIZE - 1;
while (x.digits[i] == 0 && i > 0)
{
i--;
}

for(int j = i; j >= 0; j--){
printf("%d", x.digits[j]);
}

Comment on lines +77 to +80
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Не выводит ведущие нули в цифрах
-1

}

void scanf_value(uint1024_t *x){
char buffer[309];
*x = from_uint(0);
uint1024_t ten = from_uint(10);

scanf("%s", buffer);
for(int i = 0 ; i < 309; i++){
if(buffer[i] == '\0'){
break;
}
*x = mult_op(*x, ten);
uint1024_t tmp = from_uint(buffer[i] - '0');
*x = add_op(*x, tmp);
}
}

int main()
{
uint1024_t a;
uint1024_t b;
uint1024_t temp;
printf("Введите первое число: ");
scanf_value(&a);
printf("\n");
printf("Введите второе число: ");
scanf_value(&b);
printf("\n\nСумма: ");
temp = add_op(a, b);
printf_value(temp);
printf("\n\nРазность: ");
temp = subtr_op(a, b);
printf_value(temp);
printf("\n\nПроизведение: ");
temp = mult_op(a, b);
printf_value(temp);
return 0;
}