Skip to content

Commit 35525e9

Browse files
feat: add Semester Grade Calculator feature in main.cpp
1 parent 804a4b4 commit 35525e9

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
#include <iostream>
2+
#include <iomanip>
3+
#include <vector>
4+
#include <string>
5+
#include <limits>
6+
7+
using namespace std;
8+
9+
class Subject {
10+
public:
11+
string name;
12+
double marks;
13+
double credits;
14+
15+
Subject(string n = "", double m = 0, double c = 1)
16+
: name(n), marks(m), credits(c) {}
17+
};
18+
19+
class Calculator {
20+
public:
21+
static string getLetterGrade(double marks) {
22+
if (marks >= 90) return "A+";
23+
if (marks >= 85) return "A";
24+
if (marks >= 80) return "A-";
25+
if (marks >= 75) return "B+";
26+
if (marks >= 70) return "B";
27+
if (marks >= 65) return "C+";
28+
if (marks >= 60) return "C";
29+
if (marks >= 50) return "D";
30+
return "F";
31+
}
32+
33+
static double gradePoint4(double marks) {
34+
if (marks >= 90) return 4.0;
35+
if (marks >= 85) return 3.7;
36+
if (marks >= 80) return 3.5;
37+
if (marks >= 75) return 3.3;
38+
if (marks >= 70) return 3.0;
39+
if (marks >= 65) return 2.5;
40+
if (marks >= 60) return 2.0;
41+
if (marks >= 50) return 1.0;
42+
return 0.0;
43+
}
44+
45+
static double gradePoint10(double marks) {
46+
if (marks >= 90) return 10;
47+
if (marks >= 80) return 9;
48+
if (marks >= 70) return 8;
49+
if (marks >= 60) return 7;
50+
if (marks >= 50) return 6;
51+
if (marks >= 40) return 5;
52+
return 0;
53+
}
54+
};
55+
56+
class Semester {
57+
private:
58+
vector<Subject> subjects;
59+
60+
public:
61+
void inputSubjects() {
62+
int n;
63+
cout << "Enter number of subjects: ";
64+
cin >> n;
65+
cin.ignore(numeric_limits<streamsize>::max(), '\n');
66+
67+
for (int i = 0; i < n; i++) {
68+
string name;
69+
double marks, credits;
70+
71+
cout << "\nSubject " << i + 1 << " name: ";
72+
getline(cin, name);
73+
74+
cout << "Marks (0–100): ";
75+
cin >> marks;
76+
while (marks < 0 || marks > 100) {
77+
cout << "Invalid marks. Enter again (0–100): ";
78+
cin >> marks;
79+
}
80+
81+
cout << "Credits (default 1): ";
82+
if (!(cin >> credits)) {
83+
cin.clear();
84+
cin.ignore(numeric_limits<streamsize>::max(), '\n');
85+
credits = 1;
86+
}
87+
88+
cin.ignore(numeric_limits<streamsize>::max(), '\n');
89+
subjects.emplace_back(name, marks, credits);
90+
}
91+
}
92+
93+
void displaySummary() {
94+
double totalWeighted4 = 0, totalWeighted10 = 0, totalCredits = 0;
95+
96+
cout << "\n-----------------------------------------------\n";
97+
cout << left << setw(12) << "Subject"
98+
<< setw(8) << "Marks"
99+
<< setw(8) << "Grade"
100+
<< setw(10) << "4.0 GPA"
101+
<< setw(10) << "10-Point" << "\n";
102+
cout << "-----------------------------------------------\n";
103+
104+
for (auto &sub : subjects) {
105+
string grade = Calculator::getLetterGrade(sub.marks);
106+
double gp4 = Calculator::gradePoint4(sub.marks);
107+
double gp10 = Calculator::gradePoint10(sub.marks);
108+
109+
cout << left << setw(12) << sub.name
110+
<< setw(8) << sub.marks
111+
<< setw(8) << grade
112+
<< setw(10) << gp4
113+
<< setw(10) << gp10 << "\n";
114+
115+
totalWeighted4 += gp4 * sub.credits;
116+
totalWeighted10 += gp10 * sub.credits;
117+
totalCredits += sub.credits;
118+
}
119+
120+
cout << "-----------------------------------------------\n";
121+
cout << " Semester GPA (4.0 scale): "
122+
<< fixed << setprecision(2) << (totalWeighted4 / totalCredits) << "\n";
123+
cout << " Semester GPA (10-point): "
124+
<< fixed << setprecision(2) << (totalWeighted10 / totalCredits) << "\n";
125+
cout << "-----------------------------------------------\n";
126+
}
127+
};
128+
129+
int main() {
130+
cout << "=== C++ Semester Grade Calculator ===\n";
131+
Semester sem;
132+
sem.inputSubjects();
133+
sem.displaySummary();
134+
cout << "\nThank you for using the calculator!\n";
135+
return 0;
136+
}
180 KB
Binary file not shown.

0 commit comments

Comments
 (0)