cleaned all trailing white space from source files.
[sdk] / installer / coursework / Chapter 5 - Structures and Enumerations / Lab5 / vectors / lab5vectors.ec
1 #include <stdio.h>
2
3 struct Vector
4 {
5    float x, y;
6 };
7
8 bool FloatToString(char * string, float * value)
9 {
10    char * end;
11    *value = (float)strtod(string, &end);
12    return end > string;
13 }
14
15 char GetOperation()
16 {
17    char operation = 0;
18    PrintLn("Chose an operation to perform: +, -, *, /, m (module/length). q to quit.");
19    do
20    {
21       char input[1024];
22       gets(input);
23       switch(input[0])
24       {
25          case '+': case '-': case '*': case '/': case 'm': case 'q':
26             operation = input[0];
27             break;
28          default:
29             PrintLn("Invalid Operation");
30       }
31    } while(!operation);
32    return operation;
33 }
34
35 float GetScalar()
36 {
37    float scalar;
38    char input[1024];
39    gets(input);
40    while(!FloatToString(input, &scalar))
41    {
42       PrintLn("Print enter a valid numeric value");
43       gets(input);
44    }
45    return scalar;
46 }
47
48 Vector GetVector()
49 {
50    Vector vector;
51    char input[1024];
52    gets(input);
53    while(!vector.OnGetDataFromString(input))
54    {
55       PrintLn("Print enter a valid 2D vector value");
56       gets(input);
57    }
58    return vector;
59 }
60
61 class Lab5VectorApp : Application
62 {
63    void Main()
64    {
65       while(true)
66       {
67          Vector vector1, vector2;
68          float scalar;
69          char operation = GetOperation();
70          if(operation == 'q') break;
71
72          PrintLn("Enter the first operand:");
73          vector1 = GetVector();
74
75          if(operation != 'm')
76          {
77             PrintLn("Enter the second operand:");
78             if(operation == '+' || operation == '-')
79                vector2 = GetVector();
80             else
81                scalar = GetScalar();
82          }
83          if(operation == '/' && scalar == 0)
84             PrintLn("Cannot divide by 0");
85          else
86          {
87             switch(operation)
88             {
89                case '+': PrintLn(vector1, " + ", vector2, " = ", Vector { vector1.x + vector2.x, vector1.y + vector2.y }); break;
90                case '-': PrintLn(vector1, " - ", vector2, " = ", Vector { vector1.x - vector2.x, vector1.y - vector2.y }); break;
91                case '*': PrintLn(vector1, " * ", scalar, " = ", Vector { vector1.x * scalar, vector1.y * scalar }); break;
92                case '/': PrintLn(vector1, " / ", scalar, " = ", Vector { vector1.x / scalar, vector1.y / scalar }); break;
93                case 'm': PrintLn("|",vector1,"| = ", sqrt(vector1.x * vector1.x + vector2.y * vector2.y)); break;
94             }
95          }
96       }
97       system("pause");
98    }
99 }