Skip to content

Instantly share code, notes, and snippets.

@Gelio
Created January 25, 2016 18:46
Show Gist options
  • Save Gelio/4373f22ed2148ddfd95d to your computer and use it in GitHub Desktop.
Save Gelio/4373f22ed2148ddfd95d to your computer and use it in GitHub Desktop.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define LICZBA_PUSTA {NULL,0}
#define ZERO_ASCII 48
#define B 8 //Podstawa systemu liczbowego
//Struktura przechowująca bardzo dużą liczbę w postaci kolejnych cyfr, oraz ilość tych cyfr.
typedef struct dlugaLiczba {
char *liczba;
unsigned long long int n;
} dlugaLiczba;
int wczytajLiczbe(dlugaLiczba *L);
void wyswietlLiczbe(dlugaLiczba *L);
void odwrocLiczbe(dlugaLiczba *L);
int uzupelnijZerami(dlugaLiczba *L1, dlugaLiczba *L2);
int odejmij(dlugaLiczba *L1, dlugaLiczba *L2, dlugaLiczba *wynik);
int main(void)
{
dlugaLiczba L1 = LICZBA_PUSTA, L2 = LICZBA_PUSTA, wynik = LICZBA_PUSTA;
//ETAP 1
printf("Pierwsza liczba L1: ");
if (!wczytajLiczbe(&L1))
{
printf("\nWprowadzono L1 = ");
wyswietlLiczbe(&L1);
}
printf("\n\n\nDruga liczba L2: ");
if (!wczytajLiczbe(&L2))
{
printf("\nWprowadzono L2 = ");
wyswietlLiczbe(&L2);
}
//ETAP 2
printf("\n\nOdwrocone:");
odwrocLiczbe(&L1);
odwrocLiczbe(&L2);
printf("\nL1 = ");
wyswietlLiczbe(&L1);
printf("\nL2 = ");
wyswietlLiczbe(&L2);
odwrocLiczbe(&L1);
odwrocLiczbe(&L2);
//ETAP 3
printf("\n\nPo ponownym odwroceniu i uzupelnieniu zerami :");
if (!uzupelnijZerami(&L1, &L2))
{
printf("\n\nL1 = ");
wyswietlLiczbe(&L1);
printf("\n\nL2 = ");
wyswietlLiczbe(&L2);
}
//ETAP 4
/*
printf("\n\nOdejmowanie liczb w systemie %d:",B);
if(!odejmij(&L1,&L2,&wynik))
{
printf("\n\nWynik L1 - L2 = "); // 12345 - 7654 = 2471 (8)
wyswietlLiczbe(&wynik);
}
*/
free(L1.liczba); free(L2.liczba); free(wynik.liczba);
_getch();
return 0;
}
int wczytajLiczbe(dlugaLiczba *L)
{
int n = 0;
char d = 0;
char *wsk = NULL;
while (d != 10) //Znaki pobierane dopóki nie wciśniemy ENTER
{
d = getchar();
if (d >= ZERO_ASCII && d < ZERO_ASCII + B)
{
wsk = (char *)realloc(wsk, (1 + n)*sizeof(char));
if (!wsk) return -1;
*(wsk + n) = d - ZERO_ASCII;
++n;
}
}
L->liczba = wsk;
L->n = n;
return 0;
}
/////////////////////////////////////////////////////////////
//Do uzupełnienia
////ETAP 1
void wyswietlLiczbe(dlugaLiczba *L)
{
int i;
for (i = 0; i < L->n; i++)
{
printf("%d ", L->liczba[i]);
}
printf("\n");
}
//ETAP 2
void odwrocLiczbe(dlugaLiczba *L)
{
unsigned long long int i, j;
unsigned long long int size = L->n;
char *temp = NULL;
temp = (char*)malloc(size*sizeof(char));
j = size;
for (i = 0; i < size; i++, j--)
{
temp[i] = L->liczba[j - 1];
}
L->liczba = temp;
}
//ETAP 3
int uzupelnijZerami(dlugaLiczba *L1, dlugaLiczba *L2)
{
unsigned long long int i, diff;
if (L1->n > L2->n)
{
diff = L1->n - L2->n;
odwrocLiczbe(L2);
L2->liczba = (char*)realloc(L2->liczba, (L2->n + diff)*sizeof(char));
if (!L2->liczba) return -1;
for (L2->n; L2->n < L1->n; L2->n++)
{
//strcpy(L2->liczba[i], "0");
L2->liczba[L2->n] = 0;
}
odwrocLiczbe(L2);
}
else
{
diff = L2->n - L1->n;
odwrocLiczbe(L1);
L1->liczba = (char*)realloc(L1->liczba, (L1->n + diff)*sizeof(char));
for (L1->n; L1->n < L2->n; L1->n++)
{
//strcpy(L1->liczba[i], "0");
L1->liczba[L1->n] = 0;
}
odwrocLiczbe(L1);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment