/
salesRunner.cpp
144 lines (126 loc) · 3.83 KB
/
salesRunner.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Author: Sean Davis
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>
#include "mynew.h"
#include "CPUTimer.h"
#include "salesRunner.h"
#include "sales.h"
using namespace std;
extern int maxRAM;
extern int currentRAM;
class SongInfo
{
public:
char trackID[19];
char *artist;
char *song;
} ; // class SongInfo()
int readTrackFile(SongInfo* songInfos)
{
int count = 0;
ifstream inf("unique_tracks.txt");
char line[1000], *ptr;
while(inf.getline(line, 1000))
{
strcpy(songInfos[count].trackID, strtok(line, ","));
ptr = strtok(NULL, ",");
songInfos[count].artist = new char[strlen(ptr) + 1];
strcpy(songInfos[count].artist, ptr);
ptr = strtok(NULL, "\n");
songInfos[count].song = new char[strlen(ptr) + 1];
strcpy(songInfos[count++].song, ptr);
} // while more in file
inf.close();
return count;
} // readTrackFile()
SongInfo2* readSalesFile(const char *filename, int *numInserts,
int *numOperations, SongInfo infos[], Operation **operations)
{
int trackIndex, count;
char line[256], *ptr;
ifstream inf2(filename);
inf2 >> *numInserts;
*numOperations = 6 * *numInserts;
*operations = new Operation[*numOperations];
inf2.close();
maxRAM = currentRAM = 0;
ifstream inf(filename);
inf >> *numInserts;
SongInfo2 *songInfo2 = new SongInfo2[*numInserts];
for(int i = 0; i < *numInserts; i++)
{
inf >> trackIndex >> count;
strcpy(songInfo2[i].trackID, infos[trackIndex].trackID);
strcpy(songInfo2[i].artist, infos[trackIndex].artist);
strcpy(songInfo2[i].song, infos[trackIndex].song);
songInfo2[i].count = count;
} // for each insert
inf.ignore(100, '\n');
for(int i = 0; i < *numOperations; i++)
{
inf >> (*operations)[i].operation;
inf.getline(line, 256);
ptr = strtok(line, " ");
for(int j = 0;ptr; j++ )
{
(*operations)[i].indices[j] = atoi(ptr);
ptr = strtok(NULL, " ");
}
} // for each operation
inf.close();
return songInfo2;
}
void checkResult(const char* result, const char* expected, int operationNum)
{
if(!result
|| strcmp(result, expected) != 0)
{
cout << "Operation #" << operationNum << " ";
if(result)
cout << result;
else
cout << "NULL";
cout << " should be " << expected << endl;
} // if not a match
}// checkResult())
int main(int argc, char** argv)
{
int numOperations, numInserts;
const char *result;
Operation *operations;
CPUTimer ct;
initializeNew(); // determines if malloc uses 1 or 2 ints for size.
SongInfo *songInfos = new SongInfo[1000000];
readTrackFile(songInfos);
SongInfo2 *songInfos2 = readSalesFile(argv[1], &numInserts, &numOperations,
songInfos, &operations);
ct.reset();
Sales *sales = new Sales(songInfos2, numInserts);
delete [] songInfos2;
maxRAM = currentRAM; // eliminated the songInfos2 size
for(int i = 0; i < numOperations; i++)
{
// if(i == 465)
// cout << i << endl;
if(operations[i].operation == 'A')
{
result = sales->artistList(
songInfos[operations[i].indices[0]].artist,
operations[i].indices[1]);
checkResult(result, songInfos[operations[i].indices[2]].trackID, i);
} // if artistList
else // purchase
{
result = sales->purchase(
songInfos[operations[i].indices[0]].song,
songInfos[operations[i].indices[0]].artist,
songInfos[operations[i].indices[1]].song,
songInfos[operations[i].indices[1]].artist, operations[i].indices[2]);
checkResult(result, songInfos[operations[i].indices[3]].trackID, i);
} // else purchae
} // for each operation
cout << "CPU Time: " << ct.cur_CPUTime() << " maxRAM: " << maxRAM << endl;
return 0;
} // main())