Guest User

Untitled

a guest
Feb 21st, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <ctype.h>
  4. #define degrees 0.0174532925199432958
  5.  
  6. struct Vector2D //Initialisatie van een Array van 10 structures met invoer x, y, hoek en lengte
  7. {
  8. float x;
  9. float y;
  10. float lengte;
  11. float hoek;
  12. }test[10];
  13. int i = 0;
  14. int doorgaan = 1; //Moet het loopje doorgaan?
  15. int j;
  16. float GetInproduct();
  17. void PrintToFile(float inproduct);
  18. float inproduct;
  19.  
  20. int main()
  21. {
  22.  
  23.  
  24. char ch;
  25.  
  26. while (doorgaan) //invul loop voor de array
  27. {
  28. int valid = 0;
  29. printf("X: ");
  30. scanf("%f", &test[i].x);
  31. printf("Y: ");
  32. scanf("%f", &test[i].y);
  33. test[i].hoek = atan(test[i].y/test[i].x) / degrees;
  34. test[i].lengte = sqrt((test[i].x * test[i].x) + (test[i].y * test[i].y));
  35. printf("%f, %f\t\t%f,\t%f", test[i].x, test[i].y, test[i].hoek, test[i].lengte);
  36. while( valid == 0 && i != 9)
  37. { //als je iets anders dan y / n invult gaat vraagt de compiler het opnieuw
  38. printf("\nAnother vector (Y/N)?\n");
  39. scanf(" %c", &ch );
  40. ch = toupper( ch ); // maakt een uppercase van een lowercase y of n
  41. if((ch == 'Y') || (ch == 'N') )
  42. valid = 1;
  43. }
  44. if( ch == 'N' || i == 9)
  45. {
  46. doorgaan = 0; //als de keuze n is of 10 vectoren zijn gevonden gaat hij uit de loop
  47. }
  48. i++;
  49. }
  50. GetInproduct(i);
  51. PrintToFile(GetInproduct());
  52. }
  53.  
  54.  
  55. void PrintToFile(float inproduct) //Invoer en berekeningen schrijven naar een bestand
  56. {
  57. printf("\n1Inproduct: %f", inproduct);
  58. FILE *fp;
  59. if (fp = fopen("test.txt", "w"))
  60. {
  61. fprintf(fp, "Cartesisch\t\t\thoek,\t\tlengte\n");
  62. for (j = 0; j < 10; j++)
  63. fprintf(fp, "%f, %f\t\t%f,\t%f\n", test[j].x, test[j].y, test[j].hoek, test[j].lengte);
  64. fprintf(fp, "Inproduct: %f", inproduct);
  65. fclose(fp);
  66. }
  67. else
  68. {
  69. printf("Error writing file\n");
  70. }
  71. printf("\n2Inproduct: %f", inproduct);
  72. }
  73.  
  74. float GetInproduct(int i) //Hier word het inproduct berekend
  75. {
  76. switch(i)
  77. {
  78. case 2:
  79. inproduct = ((test[i].x * test[i-1].x) + (test[i].y * test[i-1].y));
  80. printf(" %f", inproduct);
  81. return (inproduct);
  82. break;
  83. case 3:
  84. inproduct = ((test[i].x * test[i-1].x * test[i-2].x) + (test[i].y * test[i-1].y * test[i-2].y));
  85. return (inproduct);
  86. break;
  87. default:
  88. printf("Geen inproduct mogelijk");
  89. break;
  90. }
  91. printf("\nInproduct: %f", inproduct);
  92. return (inproduct);
  93. }
Add Comment
Please, Sign In to add comment