-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
52 lines (42 loc) · 1.44 KB
/
mainwindow.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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// Connect the button click to the calculateGrade slot
connect(ui->calculateButton, &QPushButton::clicked, this, &MainWindow::calculateGrade);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::calculateGrade()
{
// Retrieve grades and weights from the UI
QString gradesText = ui->gradesLineEdit->text();
QString weightsText = ui->weightInputLineEdit->text();
// Split the input strings into lists
QStringList gradesList = gradesText.split(",");
QStringList weightsList = weightsText.split(",");
if (gradesList.size() != weightsList.size()) {
QMessageBox::warning(this, "Input Error", "The number of grades and weights must match.");
return;
}
double totalWeightedGrade = 0.0;
double totalWeight = 0.0;
for (int i = 0; i < gradesList.size(); ++i) {
double grade = gradesList[i].toDouble();
double weight = weightsList[i].toDouble();
totalWeightedGrade += grade * (weight / 100.0);
totalWeight += weight;
}
if (totalWeight != 100.0) {
QMessageBox::warning(this, "Input Error", "The total weight must equal 100%.");
return;
}
// Display the result
ui->resultLabel->setText(QString("Final Grade: %1").arg(totalWeightedGrade));
}