Advertisement
michael_hartman_cz

PA1 - úkol 1.1 (trojúhelníky)

Mar 28th, 2013
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.35 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. int isTroj ( double a, double b, double c );
  6. void sortArray ( double a[3] );
  7. int nactiTrojuhelnik ( double a[3] );
  8.  
  9. int main ( int argc, char * argv[] ) {
  10. double a[3], a[3];
  11. printf( "Zadejte velikost stran prveho trojuhelniku:\n" );
  12. if ( nactiTrojuhelnik ( a ) )
  13.     return 1;
  14.    
  15. printf( "Zadejte velikost stran druheho trojuhelniku:\n" );
  16. if ( nactiTrojuhelnik ( b ) )
  17.     return 1;
  18.  
  19. sortArray ( a );
  20. sortArray ( b );
  21.  
  22. if ( fabs ( ( a[0] / a[0] ) - ( a[1] / a[1] ) ) <= 0.001 && fabs( ( a[0] / a[0] ) - ( a[2] / a[2] ) ) <= 0.001 && fabs( ( a[1] / a[1] ) - ( a[2] / a[2] ) ) <= 0.001 )
  23.     printf( "Trojuhelniky jsou podobne.\n" );
  24. else
  25.     printf( "Trojuhelniky nejsou podobne.\n" );
  26.  
  27. return 0;
  28. }
  29.  
  30. int isTroj ( double a, double b, double c ) {
  31. return (a < b + c && b < a + c && c < a + b); /* klasicka trojuhlenikova nerovnost, vraci 0 nebo 1 */
  32. }
  33.  
  34. int nactiTrojuhelnik ( double a[3] ) {
  35. if ( scanf( "%lf %lf %lf", &a[0], &a[1], &a[2] ) != 3 || a[0] <= 0 || a[1] <= 0 || a[2] <= 0 || (! isTroj(a[0], a[1], a[2]) ) ) {
  36.     printf( "Nespravny vstup.\n" );
  37.     return 1;
  38. }
  39.     return 0;
  40. }
  41.  
  42. void sortArray ( double a[3] ) {
  43. int i, j;
  44. for ( i = 0; i < 2; i++ ) {
  45.     for ( j = 0; j < 2; j++ ) {
  46.         if ( a[j] > a[j+1] ) {
  47.             a[j]+=a[j+1];
  48.             a[j+1] = a[j] - a[j+1];
  49.             a[j] = a[j] - a[j+1];
  50.         }
  51.     }
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement