Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Autor: Wojciech KumoƱ
- * Wybrana wersja: C
- * Numer: 4
- */
- #include <stdio.h>
- #include <stdlib.h>
- struct Rectangle
- {
- double x;
- double y;
- int amount;
- struct Rectangle *next;
- };
- // wczytywanie danych
- void loadData(struct Rectangle **pointer)
- {
- (*pointer)->next = NULL;
- do
- {
- printf("Wprowadz poprawna dlugosc: ");
- scanf("%lf", &((*pointer)->x));
- }
- while ((*pointer)->x <= 0);
- do
- {
- printf("Wprowadz poprawna szerokosc: ");
- scanf("%lf", &((*pointer)->y));
- }
- while ((*pointer)->y <= 0);
- do
- {
- printf("Wprowadz poprawna ilosc: ");
- scanf("%d", &((*pointer)->amount));
- }
- while ((*pointer)->amount < 0);
- printf("\n");
- }
- // dodwanie nowego elementu listy
- void addNew(struct Rectangle **headPointer)
- {
- struct Rectangle *pointer;
- pointer = *headPointer;
- if (pointer == NULL)
- {
- pointer = (struct Rectangle*)malloc(sizeof(struct Rectangle));
- loadData(&pointer);
- *headPointer = pointer;
- }
- else
- {
- while(pointer->next != NULL)
- {
- pointer = pointer->next;
- }
- pointer->next = (struct Rectangle*)malloc(sizeof(struct Rectangle));
- pointer = pointer->next;
- loadData(&pointer);
- }
- }
- // glowna funkcja wczytujaca
- void loadState(struct Rectangle **headPointer)
- {
- int choose;
- printf("1. Dodaj scinki.\n");
- printf("2. Zakoncz dodawanie.\n");
- scanf("%d", &choose);
- while(choose == 1)
- {
- addNew(headPointer);
- printf("1. Dodaj scinki.\n");
- printf("2. Zakoncz dodawanie.\n");
- scanf("%d", &choose);
- }
- }
- // funckja wyswietlajaca cala liste (m.in. do testow)
- void showAll(struct Rectangle *head)
- {
- if (head == NULL)
- {
- printf("Lista pusta!\n");
- }
- else
- {
- printf("Dlugosc: %lf\n", head->x);
- printf("Szerokosc: %lf\n", head->y);
- printf("Ilosc: %d\n\n", head->amount);
- while (head->next != NULL)
- {
- head = head->next;
- printf("Dlugosc: %lf\n", head->x);
- printf("Szerokosc: %lf\n", head->y);
- printf("Ilosc: %d\n\n", head->amount);
- }
- }
- }
- // zamowienie
- void order(double *x, double *y)
- {
- printf("Wprowadz wymiary poszukiwanej szyby.\n");
- do
- {
- printf("Wprowadz poprawna dlugosc: ");
- scanf("%lf", x);
- }
- while (*x <= 0);
- do
- {
- printf("Wprowadz poprawna szerokosc: ");
- scanf("%lf", y);
- }
- while (*y <= 0);
- }
- // funkcja znajdujaca odpowiednia szybe
- void findGlass(struct Rectangle **head, double x, double y, double *foundX, double *foundY)
- {
- struct Rectangle *pointer;
- double surfaceMin;
- int counter, savedCounter, i;
- *foundX = -1;
- *foundY = -1;
- pointer = *head;
- counter = 0;
- savedCounter = 0;
- surfaceMin = -1;
- if (pointer != NULL)
- {
- counter++;
- if ( (pointer->x >= x) && (pointer->y >= y) && (pointer->amount > 0) )
- {
- surfaceMin = (pointer->x) * (pointer->y);
- *foundX = pointer->x;
- *foundY = pointer->y;
- savedCounter = counter;
- }
- while (pointer->next != NULL)
- {
- pointer = pointer->next;
- counter++;
- if ( (pointer->x >= x) && (pointer->y >= y) && (pointer->amount > 0))
- {
- if ( (surfaceMin == -1) || (surfaceMin > pointer->x * pointer->y) )
- {
- surfaceMin = (pointer->x) * (pointer->y);
- *foundX = pointer->x;
- *foundY = pointer->y;
- savedCounter = counter;
- }
- }
- }
- }
- if (savedCounter == 1)
- {
- ((*head)->amount)--;
- if ((*head)->amount == 0)
- {
- if ((*head)->next != NULL)
- {
- pointer = (*head)->next;
- free(*head);
- *head = pointer;
- }
- else
- {
- free(*head);
- *head = NULL;
- }
- }
- }
- else if (savedCounter > 1)
- {
- struct Rectangle *pointerBefore;
- pointer = *head;
- for (i = 0; i < (savedCounter - 1); i++)
- {
- pointerBefore = pointer;
- pointer = pointer->next;
- }
- (pointer->amount)--;
- if (pointer->amount == 0)
- {
- if (pointer->next != NULL)
- {
- pointerBefore->next = pointer->next;
- free(pointer);
- }
- else
- {
- free(pointer);
- pointerBefore->next = NULL;
- }
- }
- }
- }
- int main()
- {
- struct Rectangle *head;
- double orderedX, orderedY, foundX, foundY;
- head = NULL;
- loadState(&head);
- order(&orderedX, &orderedY);
- findGlass(&head, orderedX, orderedY, &foundX, &foundY);
- if (foundX != -1)
- {
- printf("Znaleziony prostokat ma wymiary: %lf x %lf\n", foundX, foundY);
- printf("Powstaly odpad: %lf\n\n\n", (foundX*foundY - orderedX*orderedY));
- }
- else
- {
- printf("Nie znaleziono odpowiedniego prostokata!\n\n\n");
- }
- printf("Pozostale scinki:\n");
- showAll(head);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement