home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 418.lha / TrigMaster / TrigMaster.c < prev    next >
C/C++ Source or Header  |  1990-09-12  |  4KB  |  157 lines

  1. /*
  2. This code is far from optimized.  It didn't really seem worth the time to
  3. regroup everything for an application that isn't written to sit resident
  4. in RAM.  It is provided for those who are weary of viruses, trojans, and
  5. other such filthy vermin.
  6. */
  7.  
  8. #include <ctype.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <math.h>
  12.  
  13. #define CONVERT 57.29577951 /* Degrees in a Radian */
  14.  
  15. main(argc,argv)
  16. int argc;
  17. char *argv[];
  18.  
  19. {
  20. double a,b,c,A,B,C,s,temp1,temp2,temp3;
  21. void explain();
  22. void results();
  23.  
  24. if(argc != 5 || argv[1][0] == '?')
  25.   explain();
  26.  
  27. argv[1][0] = toupper(argv[1][0]);
  28. argv[1][1] = toupper(argv[1][1]);
  29. argv[1][2] = toupper(argv[1][2]);
  30.  
  31. if(argv[1][3] != '\0')
  32.   explain();
  33. if(argv[1][0] != 'S' && argv[1][0] != 'A')
  34.   explain();
  35. if(argv[1][1] != 'S' && argv[1][1] != 'A')
  36.   explain();
  37. if(argv[1][2] != 'S' && argv[1][2] != 'A')
  38.   explain();
  39. if(argv[1][0] == 'A' && argv[1][1] == 'A' && argv[1][2] == 'A')
  40.   explain();
  41.  
  42. temp1 = atof(argv[2]);
  43. temp2 = atof(argv[3]);
  44. temp3 = atof(argv[4]);
  45.  
  46. /*  SSS  */
  47.  
  48. if(argv[1][0] == 'S' && argv[1][1] == 'S' && argv[1][2] == 'S')   {
  49.   a = temp1;
  50.   b = temp2;
  51.   c = temp3;
  52.   s = (a + b + c)/2;
  53.   A = atan( sqrt(( (s-b)*(s-c) )/(s*(s-a))) )*CONVERT*2;
  54.   B = atan( sqrt(( (s-a)*(s-c) )/(s*(s-b))) )*CONVERT*2;
  55.   C = atan( sqrt(( (s-b)*(s-a) )/(s*(s-c))) )*CONVERT*2;
  56.   results(A,B,C,a,b,c);
  57.                                                                   }
  58.  
  59. /*  SSA  */
  60.  
  61. if(argv[1][0] == 'S' && argv[1][1] == 'S' && argv[1][2] == 'A')   {
  62.   a = temp1;
  63.   b = temp2;
  64.   A = temp3;
  65.   B = asin ((b * sin(A/CONVERT))/a)*CONVERT;
  66.   C = (180.00 - A - B);
  67.   c = sqrt( (b*b)+(a*a)-(2*b*a)*(cos(C/CONVERT)) );
  68.   results(A,B,C,a,b,c);
  69.                                                                   }
  70.  
  71. /*  SAS  */
  72.  
  73. if(argv[1][0] == 'S' && argv[1][1] == 'A' && argv[1][2] == 'S')   {
  74.   a = temp1;
  75.   C = temp2;
  76.   b = temp3;
  77.   c = sqrt( (b*b)+(a*a)-(2*b*a)*(cos(C/CONVERT)) );
  78.   A = asin ((a * sin(C/CONVERT))/c)*CONVERT;
  79.   B = asin ((b * sin(C/CONVERT))/c)*CONVERT;
  80.   results(A,B,C,a,b,c);
  81.                                                                   }
  82.  
  83. /*  ASS  */
  84.  
  85. if(argv[1][0] == 'A' && argv[1][1] == 'S' && argv[1][2] == 'S')   {
  86.   A = temp1;
  87.   c = temp2;
  88.   a = temp3;
  89.   C = asin ((c * sin(A/CONVERT))/a)*CONVERT;
  90.   B = (180.00 - A - C);
  91.   b = sqrt( (a*a)+(c*c)-(2*a*c)*(cos(B/CONVERT)) );
  92.   results(A,B,C,a,b,c);
  93.                                                                   }
  94.  
  95. /*  AAS  */
  96.  
  97. if(argv[1][0] == 'A' && argv[1][1] == 'A' && argv[1][2] == 'S')   {
  98.   A = temp1;
  99.   B = temp2;
  100.   a = temp3;
  101.   C = (180.00 - A - B);
  102.   c = (a*sin(C/CONVERT))/sin(A/CONVERT); 
  103.   b = sqrt( (a*a)+(c*c)-(2*a*c)*(cos(B/CONVERT)) );
  104.   results(A,B,C,a,b,c);
  105.                                                                   }
  106.  
  107. /*  ASA  */
  108.  
  109. if(argv[1][0] == 'A' && argv[1][1] == 'S' && argv[1][2] == 'A')   {
  110.   A = temp1;
  111.   c = temp2;
  112.   B = temp3;
  113.   C = (180.00 - A - B);
  114.   a = (c*sin(A/CONVERT))/sin(C/CONVERT);
  115.   b = (c*sin(B/CONVERT))/sin(C/CONVERT);
  116.   results(A,B,C,a,b,c);
  117.                                                                   }
  118.  
  119. /*  SAA  */
  120.  
  121. if(argv[1][0] == 'S' && argv[1][1] == 'A' && argv[1][2] == 'A')   {
  122.   a = temp1;
  123.   C = temp2;
  124.   A = temp3;
  125.   B = (180.00 - A - C);
  126.   c = (a*sin(C/CONVERT))/sin(A/CONVERT);
  127.   b = (a*sin(B/CONVERT))/sin(A/CONVERT);
  128.   results(A,B,C,a,b,c);
  129.                                                                   }
  130. }
  131. void explain()
  132. {
  133. printf("H\n\nmTrigMaster1.0m  © 1990 by Don Lester\n");
  134. printf("Calculates three unknown triangle values from three user\n");
  135. printf("supplied values and a format qualifier.\n\n");
  136. printf("Usage:  mTrigMasterm [qualifiers] [value] [value] [value]\n\n");
  137. printf("Qualifiers:  A - Angle     S - Side\n");
  138. printf("You will need three qualifiers.\n");
  139. printf("\nThe values are the angles and sides that correspond to the\n");
  140. printf("qualifers already given (AAA is an impossible calculation).\n\n");
  141. printf("Example command string:  mTrigMasterm ASA 45 10 30 <RETURN>\n\n");
  142. exit(10);
  143. }
  144.  
  145. void results(A,B,C,a,b,c)
  146. double A,B,C,a,b,c;
  147. {
  148. if(A<=0 || B<=0 || C<=0 || a<=0 || b<=0 || c<=0) explain();
  149. printf("m\n\nSide   a = %1.2f units\n",a);
  150. printf("Angle  A = %1.2f degrees\n\n",A);
  151. printf("Side   b = %1.2f units\n",b);
  152. printf("Angle  B = %1.2f degrees\n\n",B);
  153. printf("Side   c = %1.2f units\n",c);
  154. printf("Angle  C = %1.2f degrees\n\n\nm",C);
  155. exit(10);
  156. }
  157.