mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-02-04 19:15:52 +08:00
55 lines
1.6 KiB
C++
55 lines
1.6 KiB
C++
/*
|
|
* 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> // for assert
|
|
#include <cmath> // for NAN
|
|
#include <fstream> // for basic_ostream, basic_ifstream, basic_istream
|
|
#include <iomanip> // for operator<<, setprecision, _Setprecision
|
|
#include <iostream> // for cout
|
|
#include <string> // for char_traits, operator>>, basic_string, operat...
|
|
#include <memory> // for operator==
|
|
|
|
#include "./stack.hpp" // for stack
|
|
|
|
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;
|
|
}
|