-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
140 lines (121 loc) · 2.57 KB
/
main.cpp
File metadata and controls
140 lines (121 loc) · 2.57 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
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
struct Website
{
string title, url;
int score;
};
int openAndReadFile(Website[]);
void sortByScore(Website[], int);
int findMode(Website[], int);
void displayMode(Website[],int ,int);
int main()
{
const int MAXSIZE = 100;
int numOfFiles;
Website *story = nullptr;
story = new Website [MAXSIZE];
int mode;
numOfFiles = openAndReadFile(story);
sortByScore(story, numOfFiles);
mode = findMode(story, numOfFiles);
displayMode(story, numOfFiles, mode);
delete[] story;
}
int openAndReadFile(Website *story)
{
int filesRead;
ifstream fin;
fin.open("program3stories.txt");
if (!fin)
cout << "ERROR NO FILE OPENING" << endl;
int i = 0;
fin >> filesRead;
while(!fin.eof())
{
fin.ignore();
getline(fin, (story+i)->title);
fin >> (story+i)->url;
fin >> (story+i)->score;
i++;
}
return filesRead;
}
void sortByScore(Website *story, int numOfFiles)
{
Website temp;
for (int i = 0; i < numOfFiles; i++)
{
for (int j = 1; j < numOfFiles - i; j++)
{
if ((story + (j-1))->score < (story + j)->score)
{
temp = *(story + (j-1));
*(story + (j-1)) = *(story + j);
*(story + j) = temp;
}
}
}
}
int findMode(Website *story, int numOfFiles)
{
int number = story->score;
int mode = number;
int counter = 1;
int countMode = 1;
for (int i = 1; i < numOfFiles; i++)
{
if ((story + i)->score == number)
{
++counter;
}
else
{
if (counter > countMode)
{
countMode = counter;
mode = number;
}
if(countMode == 1)
mode = -1;
counter = 1;
number = ((story + i)->score);
}
}
return mode;
}
void displayMode(Website *story, int numOfFiles, int mode)
{
if (mode != -1)
{
cout << "Mode: " << mode << endl;
cout << endl;
}
else
{
cout << "Mode: none" << endl;
cout << endl;
}
for (int i = 0; i < numOfFiles; i++)
{
if ((story + i)->score == mode)
{
cout << (story + i)->title << endl;
cout << (story + i)->url << endl;
cout << endl;
}
}
if (mode == -1)
{
openAndReadFile(story);
for (int i = 0; i < 5; i++)
{
cout << (story + i)->title << endl;
cout << (story + i)->url << endl;
cout << endl;
}
}
}