-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
89 lines (73 loc) · 4.47 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "Vector3D.h"
#include <iostream>
using namespace std;
//For operations + or -
void sum_or_subtr_vec(unsigned int &n, char operation, Vector3D* &vectors)
{
Vector3D v = vectors[0]; //Vector to be added to or subtracted from
for (unsigned int i = 1; i < n; i++)
{
switch (operation)
{
case '+':
v = v + vectors[i];
break;
case '-':
v = v - vectors[i];
break;
default:
break;
}
}
delete[] vectors; //Clean up the other vectors that we don't need anymore
vectors = new Vector3D[1]; //New array with only one vector as a result of the operation
vectors[0] = v;
n = 1; //Change the size of the array passed to the print function
}
//For operations *NUM/NUM*/NEG
void neg_or_mult_vec(unsigned int n, char* operation, Vector3D* &vectors)
{
for (unsigned int i = 0; i < n; i++)
{
if (!strcmp(operation, "NEG"))
vectors[i] = -vectors[i]; //Negate all n vectors
else if (operation[0] == '*')
vectors[i] = vectors[i]*atof(&operation[1]); //Multiply all n vectors by a num
else if (operation[1] == '*' //Example: 2*
|| operation[2] == '*' //Example: 20*
|| operation[3] == '*' //Example: 200*
|| operation[4] == '*') //Example: 1000*
vectors[i] = vectors[i]*atof(operation); //Myltiply a num [0,1000] by all n vectors
}
}
void input_vectors(unsigned int n, Vector3D* &vectors)
{
for (unsigned int i = 0; i < n; i++)
cin >> vectors[i];
}
void print_vectors(unsigned int n, Vector3D* &vectors)
{
for (unsigned int i = 0; i < n; i++)
cout << vectors[i] << " ";
cout << endl;
}
int main()
{
char operation[6]; //Assume 1000* as max length for operation
unsigned int n; //Number of vectors
cin >> operation;
cin >> n;
if (n != 0) //If n=0, then the output would be empty
{
Vector3D* vectors = new Vector3D[n]; //Container for vectors
input_vectors(n, vectors);
if (operation[0] != '+' && operation[0] != '-')
neg_or_mult_vec(n, operation, vectors);
else
sum_or_subtr_vec(n, operation[0], vectors); //In this case only one char '+'/'-' contains the operation info
print_vectors(n, vectors);
delete[] vectors; //Program execution complete, clean up memory
}
system("pause");
return 0;
}