Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct punkt
- {
- int x;
- int y;
- };
- struct punkt nowyPunkt()
- {
- struct punkt ret;
- printf("X:>");
- fflush(stdin);
- scanf("%d", &ret.x);
- printf("Y:>");
- fflush(stdin);
- scanf("%d", &ret.y);
- return ret;
- }
- struct trojkat
- {
- struct punkt punkty[3];
- double pole;
- };
- struct trojkat nowyTrojkat(struct punkt a, struct punkt b, struct punkt c)
- {
- struct trojkat ret;
- ret.punkty[0] = a;
- ret.punkty[1] = b;
- ret.punkty[2] = c;
- ret.pole = 0.5 * abs((a.x - b.x) * (a.y - c.y) - (a.y - b.y) * (a.x - c.x));
- return ret;
- }
- int sprawdzCzyTworzaTrojkat(struct punkt a, struct punkt b, struct punkt c)
- {
- if((double) (b.y - a.y) / (double) (b.x - a.x) == (double) (c.y - a.y) / (double) (c.x - a.x))
- return 0;
- return 1;
- }
- void drukujTrojkat(struct trojkat t)
- {
- printf("\ta\tb\tc\nX:\t%d\t%d\t%d\nY:\t%d\t%d\t%d\n\nPole wynosi: %g\n", t.punkty[0].x, t.punkty[1].x, t.punkty[2].x, t.punkty[0].y, t.punkty[1].y, t.punkty[2].y, t.pole);
- }
- int main()
- {
- int lp = 0; //liczba punktów
- int i = 0;
- int j = 0;
- int k = 0;
- int lt = 0; //liczba trójkątów
- struct punkt* A; //punkty
- struct trojkat* T; //trojkaty
- void* tmp;
- printf("Witaj w programie.\nPodaj ile punktow chcesz wprowadzic[4-6]>");
- while(1)
- {
- fflush(stdin);
- scanf("%d", &lp);
- if(lp == 0) break;
- if(lp < 4 || lp > 6)
- {
- printf("Podano niepoprawna ilosc punktow!\nPodaj ilosc jeszcze raz, lub wpisz 0 aby zakonczyc program.\nIlosc>");
- continue;
- }
- break;
- }
- A = (struct punkt*) malloc(sizeof(struct punkt) * lp);
- T = (struct trojkat*) malloc(sizeof(struct trojkat));
- for(i = 0; i < lp; i++)
- {
- printf("Podawanie punktu nr %d.\n", i+1);
- A[i] = nowyPunkt();
- }
- for(i = 0; i < lp; i++)
- {
- for(j = i+1; j < lp; j++)
- {
- for(k = j+1; k < lp; k++)
- {
- if(sprawdzCzyTworzaTrojkat(A[i], A[j], A[k]) == 1)
- {
- //printf("Nastepujace punkty tworza trojkat:\n\tA[%d]\tA[%d]\tA[%d]\nX:\t%d\t%d\t%d\nY:\t%d\t%d\t%d\n", i, j, k, A[i].x, A[j].x, A[k].x, A[i].y, A[j].y, A[k].y);
- lt++;
- T = (struct trojkat*) realloc(T, sizeof(struct trojkat) * lt);
- T[lt-1] = nowyTrojkat(A[i], A[j], A[k]);
- }
- }
- }
- if(i+2 >= lp)
- break;
- }
- if(lt == 0)
- {
- printf("\nZ podanych puntkow nie mozna utworzyc zadnego trojkata!.\n");
- }
- else
- {
- printf("\nLiczba trojkatow mozliwych do stworzenia: %d\nOto te trojkaty:\n", lt);
- for(i = 0; i < lt; i++)
- {
- printf("\nTrojkat nr %d:\n", i+1);
- drukujTrojkat(T[i]);
- }
- }
- printf("\n\nKoniec programu. Nacisnij dowolny klawisz..\n");
- getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement