-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
74 lines (67 loc) · 2.75 KB
/
main.cpp
File metadata and controls
74 lines (67 loc) · 2.75 KB
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
/****************************************************/
/** **/
/** Jonathon Wong **/
/** 998823698 **/
/** CSC444 **/
/** Assignment 1: Billiard Ball Simulation **/
/** Last updated October 6, 2013 **/
/** **/
/****************************************************/
#include <iostream>
#include <string.h>
#include "utils.h"
#include "BilliardTable.h"
using namespace std;
int main()
{
bool exit = false;
char fileName[150];
BilliardTable table = *new BilliardTable(0, 0);
while (!exit) {
cout << "Enter the name of the input file (type exit to end session): ";
std::cin.getline(fileName, 150 - 2);
if (fileName == NULL || strcmp(fileName, "") == 0) {
cout << "Invalid input. Please try again." << endl;
}
else if (strcmp(fileName, "exit") == 0) {
exit = true;
}
else {
cout << "Loading..." << endl;
// Attempt to load the new table.
if (!table.initialize(fileName)) {
cout << "Error reading file " << fileName << ". Please try again." << endl;
}
else {
cout << "Finished loading file" << endl;
cout << "Calculating..." << endl;
// Carry out simulation.
while (table.getNumActive() > 0) {
table.update();
}
// Load results into output file.
char temp[150];
strncpy(temp, fileName, strlen(fileName) - 4);
temp[strlen(fileName) - 4] = '\0';
strcat(temp, "_o.txt"); // Append "_o" to the end of the file name.
cout << "Outputting results to " << temp << endl;
FILE* out = fopen(temp, "w"); // Overwrites file if it already exists.
if (out == NULL) {
cout << "Error opening/writing to output file." << endl;
}
else {
// Start writing lines to the file.
fprintf(out, "%f %f\n", table.getWidth(), table.getHeight());
for (int i = 0; i < table.getNumBalls(); i++) {
Ball b = table.getBall(i);
fprintf(out, "%f %f %f %f\n", b.getPos().getX(), b.getPos().getY(), b.getSpeed().getX(), b.getSpeed().getY());
}
cout << "Finished output." << endl;
}
strcpy (fileName, "");
}
}
}
cout << "Finished." << endl;
return 0;
}