-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbaseline.cpp
More file actions
127 lines (102 loc) · 2.7 KB
/
baseline.cpp
File metadata and controls
127 lines (102 loc) · 2.7 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
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
// THIS IS THE PROVIDED CODE FOR PROGRAM #2, DSA 1, SPRING 2014
#include <iostream>
#include <fstream>
#include <sstream>
#include <list>
#include <vector>
#include <string>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <cstring>
#include <cctype>
#include <cstdlib>
using namespace std;
// A simple class; each object holds one string
class Data {
public:
string data;
Data(const string &s) { data = s; }
};
// Load the data from a specified input file
void loadDataList(list<Data *> &l) {
string filename;
cout << "Enter name of input file: ";
cin >> filename;
ifstream input(filename.c_str());
if (!input) {
cerr << "Error: could not open " << filename << endl;
exit(1);
}
// The first line indicates the size
string line;
getline(input, line);
stringstream ss(line);
int size;
ss >> size;
// Load the data
for (int i = 0; i < size; i++) {
string line;
getline(input, line);
l.push_back(new Data(line));
}
input.close();
}
// Output the data to a specified input file
void writeDataList(const list<Data *> &l) {
string filename;
cout << "Enter name of output file: ";
cin >> filename;
ofstream output(filename.c_str());
if (!output) {
cerr << "Error: could not open " << filename << endl;
exit(1);
}
// Write the size first
int size = l.size();
output << size << endl;
// Write the data
for (list<Data *>::const_iterator ipD = l.begin(); ipD != l.end(); ipD++) {
output << (*ipD)->data << endl;
}
output.close();
}
void sortDataList(list<Data *> &);
// The main function calls routines to get the data, sort the data,
// and output the data. The sort is timed according to CPU time.
int main() {
list<Data *> theList;
loadDataList(theList);
cout << "Data loaded. Executing sort..." << endl;
clock_t t1 = clock();
sortDataList(theList);
clock_t t2 = clock();
double timeDiff = ((double) (t2 - t1)) / CLOCKS_PER_SEC;
cout << "Sort finished. CPU time was " << timeDiff << " seconds." << endl;
writeDataList(theList);
}
// -------------------------------------------------
// YOU MAY NOT CHANGE OR ADD ANY CODE ABOVE HERE !!!
// -------------------------------------------------
// You may add global variables, functions, and/or
// class defintions here if you wish.
bool compareStrings(Data *d1, Data *d2);
int locateDecimal(Data *d);
bool compareStrings(Data *a, Data *b){
if (locateDecimal(a)!=locateDecimal(b)){
return locateDecimal(a)<locateDecimal(b);
} else {
return (a->data) < (b->data);
}
}
int locateDecimal(Data *d){
int i=20;
while(d->data.at(i) != '.') {
i--;
}
return i;
}
void sortDataList(list<Data *> &l) {
// Fill in this function
l.sort(compareStrings);
}