Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #include <ctype.h>
- #define degrees 0.0174532925199432958
- struct Vector2D //Initialisatie van een Array van 10 structures met invoer x, y, hoek en lengte
- {
- float x;
- float y;
- float lengte;
- float hoek;
- }test[10];
- int i = 0;
- int doorgaan = 1; //Moet het loopje doorgaan?
- int j;
- float GetInproduct();
- void PrintToFile(float inproduct);
- float inproduct;
- int main()
- {
- char ch;
- while (doorgaan) //invul loop voor de array
- {
- int valid = 0;
- printf("X: ");
- scanf("%f", &test[i].x);
- printf("Y: ");
- scanf("%f", &test[i].y);
- test[i].hoek = atan(test[i].y/test[i].x) / degrees;
- test[i].lengte = sqrt((test[i].x * test[i].x) + (test[i].y * test[i].y));
- printf("%f, %f\t\t%f,\t%f", test[i].x, test[i].y, test[i].hoek, test[i].lengte);
- while( valid == 0 && i != 9)
- { //als je iets anders dan y / n invult gaat vraagt de compiler het opnieuw
- printf("\nAnother vector (Y/N)?\n");
- scanf(" %c", &ch );
- ch = toupper( ch ); // maakt een uppercase van een lowercase y of n
- if((ch == 'Y') || (ch == 'N') )
- valid = 1;
- }
- if( ch == 'N' || i == 9)
- {
- doorgaan = 0; //als de keuze n is of 10 vectoren zijn gevonden gaat hij uit de loop
- }
- i++;
- }
- GetInproduct(i);
- PrintToFile(GetInproduct());
- }
- void PrintToFile(float inproduct) //Invoer en berekeningen schrijven naar een bestand
- {
- printf("\n1Inproduct: %f", inproduct);
- FILE *fp;
- if (fp = fopen("test.txt", "w"))
- {
- fprintf(fp, "Cartesisch\t\t\thoek,\t\tlengte\n");
- for (j = 0; j < 10; j++)
- fprintf(fp, "%f, %f\t\t%f,\t%f\n", test[j].x, test[j].y, test[j].hoek, test[j].lengte);
- fprintf(fp, "Inproduct: %f", inproduct);
- fclose(fp);
- }
- else
- {
- printf("Error writing file\n");
- }
- printf("\n2Inproduct: %f", inproduct);
- }
- float GetInproduct(int i) //Hier word het inproduct berekend
- {
- switch(i)
- {
- case 2:
- inproduct = ((test[i].x * test[i-1].x) + (test[i].y * test[i-1].y));
- printf(" %f", inproduct);
- return (inproduct);
- break;
- case 3:
- inproduct = ((test[i].x * test[i-1].x * test[i-2].x) + (test[i].y * test[i-1].y * test[i-2].y));
- return (inproduct);
- break;
- default:
- printf("Geen inproduct mogelijk");
- break;
- }
- printf("\nInproduct: %f", inproduct);
- return (inproduct);
- }
Add Comment
Please, Sign In to add comment