TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
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
20int 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;
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}
for std::invalid_argument
Definition stack.hpp:19
bool isEmptyStack() const
Definition stack.hpp:44
void pop()
Definition stack.hpp:62
void clear()
Definition stack.hpp:69
void push(const value_type &item)
Definition stack.hpp:47
value_type top() const
Definition stack.hpp:56
int main()
Main function.
This class specifies the basic operation on a stack as a linked list.