TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Toggle main menu visibility
Loading...
Searching...
No Matches
test_stack_students.cpp
1
/*
2
* This program reads a data file consisting of students' GPAs
3
* followed by their names. The program then prints the highest
4
* GPA and the names of the students with the highest GPA.
5
* It uses stack to store the names of the students
6
* Run:
7
* make all
8
* ./main student.txt
9
************************************************************
10
* */
11
#include <cassert>
12
#include <cmath>
13
#include <fstream>
14
#include <iomanip>
15
#include <iostream>
16
#include <string>
17
18
#include "
./stack.hpp
"
19
20
int
main
(
int
argc,
char
* argv[]) {
21
double
GPA = NAN;
22
double
highestGPA = NAN;
23
std::string name;
24
25
assert(argc == 2);
26
std::ifstream infile;
27
stack<std::string>
stk;
28
29
infile.open(argv[1]);
30
std::cout << std::fixed << std::showpoint;
31
std::cout << std::setprecision(2);
32
infile >> GPA >> name;
33
highestGPA = GPA;
34
35
while
(infile) {
36
if
(GPA > highestGPA) {
37
stk.
clear
();
38
stk.
push
(name);
39
highestGPA = GPA;
40
}
else
if
(GPA == highestGPA) {
41
stk.
push
(name);
42
}
43
infile >> GPA >> name;
44
}
45
std::cout <<
"Highest GPA: "
<< highestGPA << std::endl;
46
std::cout <<
"Students the highest GPA are: "
<< std::endl;
47
while
(!stk.
isEmptyStack
()) {
48
std::cout << stk.
top
() << std::endl;
49
stk.
pop
();
50
}
51
std::cout << std::endl;
52
return
0;
53
}
stack
for std::invalid_argument
Definition
stack.hpp:19
stack::isEmptyStack
bool isEmptyStack() const
Definition
stack.hpp:44
stack::pop
void pop()
Definition
stack.hpp:62
stack::clear
void clear()
Definition
stack.hpp:69
stack::push
void push(const value_type &item)
Definition
stack.hpp:47
stack::top
value_type top() const
Definition
stack.hpp:56
main
int main()
Main function.
Definition
generate_parentheses.cpp:110
stack.hpp
This class specifies the basic operation on a stack as a linked list.
data_structures
test_stack_students.cpp
Generated by
1.18.0