-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.cpp
128 lines (98 loc) · 3.05 KB
/
calculator.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
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
#include<iostream>
#include<cctype>
#include<unordered_map>
#include<iomanip>
using namespace std;
float gradePoints(string grade);
void calculateGPA(float& credits, float& weightedSum);
void calculateCGPA(float credits, float weightedSum);
struct Course {
string name;
string grade;
float credit;
};
int main() {
int input = 1;
while (input == 1) {
float credits = 0;
float weightedSum = 0;
system("cls"); // clear the console
int numOfSemesters;
cout<< "Enter number of semester: ";
cin>> numOfSemesters;
for (int i=0; i<numOfSemesters; i++) {
cout<< endl << "SEMESTER-" << i+1;
calculateGPA(credits, weightedSum);
calculateCGPA(credits, weightedSum);
}
cout<< endl << "\t\e[38;5;105m 0.Exit Program" << endl << "\t 1.Calculate Again" << endl;
cout<< endl << "Enter Your Choice: \e[0m";
cin>> input;
cout<< endl;
}
}
//*****************Calculate GPA******************
void calculateGPA(float& credits, float& weightedSum) {
float semesterCredit;
float semesterGPA;
float weightGPA;
int numOfCourses;
cout<< " Enter number of courses: ";
cin>> numOfCourses;
for (int i=0; i<numOfCourses; i++) {
Course c;
cout<< "COURSE: " << i+1 << endl;
cout<< "\t" << "Name: ";
cin>> c.name;
cout<< "\t" << "Grade: ";
cin>> c.grade;
for (char& ch: c.grade) {
ch = toupper(ch);
}
cout<< "\t" << "Credit: ";
cin>> c.credit;
cout<< endl;
weightGPA += gradePoints(c.grade) * c.credit;
semesterCredit += c.credit;
}
// update GPA for each semester
semesterGPA = weightGPA/semesterCredit;
cout << ">Credit Complete: ";
// check if semester credit is int
if (semesterCredit == static_cast<int>(semesterCredit)) {
cout<< static_cast<int>(semesterCredit);
} else {
cout<< fixed << setprecision(1) << semesterCredit;
}
cout<< " | Semester GPA " << fixed << setprecision(2) << semesterGPA;
// sum credits for each semester, to calculate CGPA
credits += semesterCredit;
// SemesterGPA * SemesterCredits, to calculate CGPA
weightedSum += semesterGPA * semesterCredit;
}
//*****************Calculate CGPA*****************
void calculateCGPA(float credits, float weightedSum) {
float CGPA = weightedSum/credits;
cout<< " | Total Credit: ";
// check if credit is an int
if (credits == static_cast<int>(credits)) {
cout<< static_cast<int>(credits);
} else {
cout<< fixed << setprecision(1) << credits;
}
cout<< " | CGPA " << fixed << setprecision(2) << CGPA << endl;
}
float gradePoints(const string grade) {
unordered_map<string, float> point;
point["A+"] = 4.00;
point["A"] = 3.75;
point["A-"] = 3.50;
point["B+"] = 3.25;
point["B"] = 3.00;
point["B-"] = 2.75;
point["C+"] = 2.50;
point["C"] = 2.25;
point["D"] = 2.00;
point["F"] = 0.00;
return point[grade];
}