-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem 22.cpp
119 lines (102 loc) · 2.52 KB
/
Problem 22.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
#include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<fstream>
#include<cstdlib>
#include<vector>
#define DEBUG
using namespace std;
#ifdef DEBUG
void vectorTest(bool isPause);
#endif
short letterValue (char letter);
bool isFirstStringLess (string string1, string string2);
long long expStringValue (string word);
void alphSort ();
void populate_vector();
long long alphaSum (string word);
vector<string> names;
int main(){
populate_vector();
alphSort();
cout << "TEST";
system("PAUSE");
vectorTest(false);
long long sumOfScores = 0;
for (int i = 0; i < names.size(); i++){
sumOfScores += (i + 1) * alphaSum(names[i]);
}
cout << sumOfScores;
system("PAUSE");
}
void alphSort (){
string swapVal;
bool isSorted = false, actionTaken;
do {
actionTaken = false;
for (int i = 0; i < names.size() -1 ; i++){
if (names[i] > names[i + 1]){
//isSorted = false;
actionTaken = true;
swapVal = names[i + 1];
names[i + 1] = names[i];
names[i] = swapVal;
}
}
if (!actionTaken){
isSorted = true;
}
} while (!isSorted);
}
long long alphaSum (string word){
long long sum = 0;
for (int i = 0; i < word.length(); i++){
sum += letterValue(word[i]);
}
return (sum);
}
short letterValue (char letter){
char letterDict [26] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
for (short i = 0; i < 26; i++){
if (letterDict [i] == letter){
return (i + 1);
}
}
return (-1);
}
void populate_vector(){
string Input;
ifstream openFile;
openFile.open("Problem22names.txt");
if(!openFile.is_open()){
cout << "FILE READ ERROR!" << endl;
system("PAUSE");
exit(EXIT_FAILURE);
}
openFile >> Input;
string tempWord = "";
cout << Input.length() << " ";
for (long i = 0; i <= Input.length(); i++){
if (Input[i] == '"'){
} else if (Input[i] == ',' || i == Input.length()){
names.push_back(tempWord);
tempWord = "";
} else {
tempWord.append(Input.substr(i,1));
}
}
vectorTest(false);
}
#ifdef DEBUG
void vectorTest(bool isPause){
cout << endl << names.size() << " ";
for (int i = 0; i < names.size(); i++){
cout << names[i] << endl;
if (isPause){
system("PAUSE");
}
}
}
#endif DEBUG