-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
test_stack_students.cpp
53 lines (48 loc) · 1.34 KB
/
test_stack_students.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
/*
* This program reads a data file consisting of students' GPAs
* followed by their names. The program then prints the highest
* GPA and the names of the students with the highest GPA.
* It uses stack to store the names of the students
* Run:
* make all
* ./main student.txt
************************************************************
* */
#include <cassert>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#include "./stack.hpp"
int main(int argc, char* argv[]) {
double GPA = NAN;
double highestGPA = NAN;
std::string name;
assert(argc == 2);
std::ifstream infile;
stack<std::string> stk;
infile.open(argv[1]);
std::cout << std::fixed << std::showpoint;
std::cout << std::setprecision(2);
infile >> GPA >> name;
highestGPA = GPA;
while (infile) {
if (GPA > highestGPA) {
stk.clear();
stk.push(name);
highestGPA = GPA;
} else if (GPA == highestGPA) {
stk.push(name);
}
infile >> GPA >> name;
}
std::cout << "Highest GPA: " << highestGPA << std::endl;
std::cout << "Students the highest GPA are: " << std::endl;
while (!stk.isEmptyStack()) {
std::cout << stk.top() << std::endl;
stk.pop();
}
std::cout << std::endl;
return 0;
}