This commit is contained in:
louiscklaw
2025-01-31 21:27:38 +08:00
parent 8cce226ca9
commit ca0eb416dd
90 changed files with 4082 additions and 0 deletions

0
marissa.sam/.gitignore vendored Normal file
View File

View File

@@ -0,0 +1,45 @@
#include <iostream>
using namespace std;
int main() {
// Prompt the user to enter their name
cout << "Please enter your name: ";
string name;
cin >> name;
// Prompt the user to enter their math, Chinese, and English marks
float mathMark, chineseMark, englishMark;
cout << "Please enter your math mark: ";
cin >> mathMark;
cout << "Please enter your Chinese mark: ";
cin >> chineseMark;
cout << "Please enter your English mark: ";
cin >> englishMark;
// Calculate the average mark
float averageMark = (mathMark + chineseMark + englishMark) / 3;
// Determine the grade based on the average mark
char grade;
if (averageMark >= 90) {
grade = 'A';
} else if (averageMark >= 80) {
grade = 'B';
} else if (averageMark >= 70) {
grade = 'C';
} else if (averageMark >= 60) {
grade = 'D';
} else {
grade = 'F';
}
// Print the results
cout << endl << "Name: " << name;
cout << endl << "Math Mark: " << mathMark;
cout << endl << "Chinese Mark: " << chineseMark;
cout << endl << "English Mark: " << englishMark;
cout << endl << "Average Mark: " << averageMark;
cout << endl << "Grade: " << grade << endl;
return 0;
}

View File

@@ -0,0 +1,7 @@
write some cpp functions:
This system collects student details like name, marks in each subject.
The system can calculates the overall grade of each student.
User can add, edit and delete the record to the system manually or upload a file.
The system should have a counter to check how many records are update on each time.
User can generate report and the report should be able to download.

View File

@@ -0,0 +1,25 @@
write some cpp functions:
1. collects student details like name, marks in each subject.
using cpp, write a console program that accepts user input name, maths marks, chinese marks, english marks and grade,
print them out when the user input done
The system can calculates the overall grade of each student.
User can
add,
edit and
delete the record to the
system manually or
upload a file.
add,
edit and
delete
The system should have a counter to check how many records are update on each time.
User can generate report and the report should be able to download.

View File

@@ -0,0 +1,41 @@
#include <iostream>
#include <string>
struct Student {
std::string name;
int maths_marks, chinese_marks, english_marks;
char grade;
};
int main() {
while (true) {
Student student;
std::cout << "Enter name (or 'END' to quit): ";
std::cin >> student.name;
if (student.name == "END") {
break;
}
std::cout << "Enter maths marks: ";
std::cin >> student.maths_marks;
std::cout << "Enter chinese marks: ";
std::cin >> student.chinese_marks;
std::cout << "Enter english marks: ";
std::cin >> student.english_marks;
std::cout << "Enter grade: ";
std::cin >> student.grade;
std::cout << "\nStudent details:\n";
std::cout << "Name: " << student.name << "\n";
std::cout << "Maths marks: " << student.maths_marks << "\n";
std::cout << "Chinese marks: " << student.chinese_marks << "\n";
std::cout << "English marks: " << student.english_marks << "\n";
std::cout << "Grade: " << student.grade << "\n";
}
return 0;
}

View File

@@ -0,0 +1,17 @@
write a cpp function:
This system collects student details like name, marks in each subject.
using cpp, write a console program:
that accepts user input
- name,
- maths marks,
- chinese marks,
- english marks and
- grade,
loop until user enter "END" as name input
print them out when the user input done

View File

@@ -0,0 +1,40 @@
write some cpp functions:
```
Add marks record
Delete marks record
Edit marks record
Print current marks record in system
press E to exit
```
if the user entered 1, the run `functionAdd` so on and forth
```cpp
#include <iostream>
void print_menu() {
std::cout << "Mark Record System\n";
std::cout << "-----------------\n";
std::cout << "1. Add marks record\n";
std::cout << "2. Delete marks record\n";
std::cout << "3. Edit marks record\n";
std::cout << "4. Print current marks record in system\n";
std::cout << "E. Exit\n";
std::cout << "Enter your choice: ";
}
```
write a cpp function that print the above menu
The system can calculates the overall grade of each student.
User can add, edit and delete the record to the system manually or upload a file.
The system should have a counter to check how many records are update on each time.
User can generate report and the report should be able to download.
using cpp, write a console program that accepts user input name, maths marks, chinese marks, english marks and grade,
print them out when the user input done

View File

@@ -0,0 +1,106 @@
#include <iostream>
#include <vector>
#include <string>
struct MarksRecord {
std::string name;
int maths_marks, chinese_marks, english_marks;
};
std::vector<MarksRecord> records;
void print_menu() {
std::cout << "Mark Record System\n";
std::cout << "-----------------\n";
std::cout << "1. Add marks record\n";
std::cout << "2. Delete marks record\n";
std::cout << "3. Edit marks record\n";
std::cout << "4. Print current marks record in system\n";
std::cout << "E. Exit\n";
std::cout << "Enter your choice: ";
}
void functionAdd() {
MarksRecord record;
std::cout << "Enter name: ";
std::cin >> record.name;
std::cout << "Enter maths marks: ";
std::cin >> record.maths_marks;
std::cout << "Enter chinese marks: ";
std::cin >> record.chinese_marks;
std::cout << "Enter english marks: ";
std::cin >> record.english_marks;
records.push_back(record);
}
void functionDelete() {
std::string name;
std::cout << "Enter name to delete: ";
std::cin >> name;
for (size_t i = 0; i < records.size(); i++) {
if (records[i].name == name) {
records.erase(records.begin() + i);
break;
}
}
}
void functionEdit() {
std::string name;
std::cout << "Enter name to edit: ";
std::cin >> name;
for (size_t i = 0; i < records.size(); i++) {
if (records[i].name == name) {
std::cout << "Enter new maths marks: ";
std::cin >> records[i].maths_marks;
std::cout << "Enter new chinese marks: ";
std::cin >> records[i].chinese_marks;
std::cout << "Enter new english marks: ";
std::cin >> records[i].english_marks;
break;
}
}
}
void functionPrint() {
for (const auto& record : records) {
std::cout << "Name Maths Chinese English Grade " << "\n";
std::cout << record.name << " "
<< record.maths_marks << " "
<< record.chinese_marks << " "
<< record.english_marks << " "
<< "\n";
std::cout << "\n";
}
}
int main() {
char choice;
while (true) {
print_menu();
std::cin >> choice;
switch (choice) {
case '1':
functionAdd();
break;
case '2':
functionDelete();
break;
case '3':
functionEdit();
break;
case '4':
functionPrint();
break;
case 'E':
case 'e':
return 0;
default:
std::cout << "Invalid choice. Please try again.\n";
}
}
return 0;
}

View File

@@ -0,0 +1,16 @@
write some cpp functions:
- User can add, edit and delete the record to the system manually or upload a file.
using cpp, write a console program that accepts:
user input a csv file path
the program then parse the inputted csv file and insert the record
```
name,maths,chinese,english
apple,99,98,99
banana,99,98,99
```

View File

@@ -0,0 +1,3 @@
burger 15
fries 11
ice-cream 9

View File

@@ -0,0 +1,45 @@
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int main(){
//Declare variables
ifstream inFile;
ofstream outFile;
string burger_name, fries_name, icecream_name;
int burger_price, fries_price, icecream_price;
//Open the input file and output file
inFile.open("price.txt");
if (!inFile) {
cout << "Cannot open the input file."
<< "The program terminates." << endl;
return 1;
}
outFile.open("price_output.out");
cout << "Processing data" << endl;
//Read file word by word
inFile >> burger_name >> burger_price;
inFile >> fries_name >> fries_price;
inFile >> icecream_name >> icecream_price;
//Output file
outFile << "The price of " << burger_name
<< " is $" << burger_price <<"." << endl;
outFile << "The price of " << fries_name
<< " is $" << fries_price << "." << endl;
outFile << "The price of " << icecream_name
<< " is $" << icecream_price << "." << endl;
inFile.close(); // .close(): close a file
outFile.close();
cout << "Processing completed" << endl;
return 0;
}

View File

@@ -0,0 +1,13 @@
write some cpp functions:
This system collects student details like name, marks in each subject.
The system can calculates the overall grade of each student.
User can add, edit and delete the record to the system manually or upload a file.
The system should have a counter to check how many records are update on each time.
User can generate report and the report should be able to download.
using cpp, write a console program that accepts user input name, maths marks, chinese marks, english marks and grade,
print them out when the user input done

View File

@@ -0,0 +1,13 @@
write some cpp functions:
This system collects student details like name, marks in each subject.
The system can calculates the overall grade of each student.
User can add, edit and delete the record to the system manually or upload a file.
The system should have a counter to check how many records are update on each time.
User can generate report and the report should be able to download.
using cpp, write a console program that accepts user input name, maths marks, chinese marks, english marks and grade,
print them out when the user input done

View File

@@ -0,0 +1,7 @@
git status .
@pause
git add .
git commit -m"update marissa.sam,"
start git push

27
marissa.sam/meta.md Normal file
View File

@@ -0,0 +1,27 @@
---
tags: [cpp, console]
---
# marissa.sam
Topic 1 Student Report Management System
This system collects student details like
- [ ] name,
- [ ] marks in each subject.
- [ ] maths
- [ ] chinese
- [ ] english
- [ ] grade
The system can calculates the overall grade of each student.
User can add, edit and delete the record to the system manually or upload a file.
The system should have a counter to check how many records are update on each time.
User can generate report and the report should be able to download.
2-4 additional features are required.
Topic 1 Suggested Records

13
marissa.sam/package.json Normal file
View File

@@ -0,0 +1,13 @@
{
"name": "sunny9898",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"gitUpdate": "git add . && git commit -m\"update marissa.sam,\""
},
"keywords": [],
"author": "",
"license": "ISC"
}

View File

@@ -0,0 +1,27 @@
Topic 1 Student Report Management System
i. This system collects student details like name, marks in each subject.
- HKD 0
ii. The system can calculates the overall grade of each student.
- HKD 100
iii. User can add, edit and delete the record to the system manually or upload a file.
- HKD 200
iv. The system should have a counter to check how many records are update on each time.
- commit button ?
- HKD 150
v. User can generate report and the report should be able to download.
- HKD 150
vi. 2-4 additional features are required.
- Find highest and lowest marks for each subject (who & marks)
- Find average marks for each subject (highest and lowest, who & marks)
- HKD 300
https://www.onlinegdb.com/online_c++_compiler

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,19 @@
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
std::cout << "Price list of a restaurant"<<endl;
std::cout << setw(20)
<< setfill(' ')
<< left
<< "Cheese burger: "
<< " $"
<< setw(4)
<< setfill(' ')
<< right
<< "14"
<< endl;
}

Binary file not shown.

View File

@@ -0,0 +1,45 @@
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int main(){
//Declare variables
ifstream inFile;
ofstream outFile;
string burger_name, fries_name, icecream_name;
int burger_price, fries_price, icecream_price;
//Open the input file and output file
inFile.open("price.txt");
if (!inFile) {
cout << "Cannot open the input file."
<< "The program terminates." << endl;
return 1;
}
outFile.open("price_output.out");
cout << "Processing data" << endl;
//Read file word by word
inFile >> burger_name >> burger_price;
inFile >> fries_name >> fries_price;
inFile >> icecream_name >> icecream_price;
//Output file
outFile << "The price of " << burger_name
<< " is $" << burger_price <<"." << endl;
outFile << "The price of " << fries_name
<< " is $" << fries_price << "." << endl;
outFile << "The price of " << icecream_name
<< " is $" << icecream_price << "." << endl;
inFile.close(); // .close(): close a file
outFile.close();
cout << "Processing completed" << endl;
return 0;
}

View File

@@ -0,0 +1,3 @@
burger 15
fries 11
ice-cream 9

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,297 @@
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
struct MarksRecord {
string name;
int maths_marks, chinese_marks, english_marks;
string grade;
};
vector<MarksRecord> records;
string functionGetGrade(int maths_marks, int chinese_marks, int english_marks){
string grade;
int marks_sum;
float marks_avg;
marks_sum = 1;
marks_sum = (maths_marks + chinese_marks + english_marks);
marks_avg = marks_sum / 3;
if (marks_avg > 90) return "A";
if (marks_avg > 80) return "B";
if (marks_avg > 70) return "C";
return "D";
}
void print_menu() {
cout << "\n";
cout << "Mark Record System\n";
cout << "\n";
cout << "------------------------------------------\n";
cout << "1. Add marks record \n";
cout << "2. Delete marks record \n";
cout << "3. Edit marks record \n";
cout << "4. Print current marks record in system \n";
cout << "5. import records from file \n";
cout << "6. export records to file \n";
cout << "------------------------------------------\n";
cout << "E. Exit\n";
cout << "Enter your choice: ";
}
void functionAdd() {
string temp;
bool name_pass;
name_pass = false;
MarksRecord record;
while (!name_pass){
bool error;
error = false;
cout << "Enter name: ";
cin >> temp;
for (size_t i = 0; i < records.size(); i++) {
if (records[i].name == temp) {
error = true;
cout << "ERROR: duplicated student name found"<<"\n";
}
}
// check if name entered is empty
if (temp == string()){
error = true;
}
if (!error) {
record.name = temp;
name_pass = true;
}
}
cout << "Enter maths marks: ";
cin >> record.maths_marks;
cout << "Enter chinese marks: ";
cin >> record.chinese_marks;
cout << "Enter english marks: ";
cin >> record.english_marks;
records.push_back(record);
}
void functionDelete() {
string name;
bool user_found;
user_found = false;
cout << "Enter name to delete: ";
cin >> name;
for (size_t i = 0; i < records.size(); i++) {
if (records[i].name == name) {
records.erase(records.begin() + i);
user_found = true;
break;
}
}
if (!user_found){
cout << "ERROR: student record not found";
}else{
cout << "1 record deleted.";
}
}
void functionEdit() {
string name;
string temp;
bool user_found;
cout << "Enter name to edit: ";
cin >> name;
for (size_t i = 0; i < records.size(); i++) {
if (records[i].name == name) {
user_found = true;
cout << "Enter new maths marks [- to skip]: ";
cin >> temp;
if (temp != "-") {
records[i].maths_marks = stoi(temp);
}
cout << "Enter new chinese marks [- to skip]: ";
cin >> temp;
if (temp != "-") {
records[i].chinese_marks = stoi(temp);
}
cout << "Enter new english marks [- to skip]: ";
cin >> temp;
if (temp != "-") {
records[i].english_marks = stoi(temp);
}
break;
}
}
if (!user_found){
cout << "sorry but no user found to update record";
}else{
cout << "1 record updated";
}
}
void functionPrint() {
cout << endl;
cout
<< setw(10) << setfill(' ') << left << "Name" << setw(10)
<< setw(10) << setfill(' ') << left << "Maths" << setw(10)
<< setw(10) << setfill(' ') << left << "Chinese" << setw(10)
<< setw(10) << setfill(' ') << left << "English" << setw(10)
<< setw(10) << setfill(' ') << left << "Grade" << setw(10);
cout << "" << setw(50) << setfill('-') << "\n";
cout << "\n";
for (const auto& record : records) {
string calc_grade;
calc_grade = functionGetGrade(record.maths_marks, record.chinese_marks, record.english_marks);
cout
<< setw(10) << setfill(' ') << left << record.name
<< setw(10) << setfill(' ') << left << record.maths_marks
<< setw(10) << setfill(' ') << left << record.chinese_marks
<< setw(10) << setfill(' ') << left << record.english_marks
// append 2 chars on the left
<< setw(2) << setfill(' ') << left << " "
<< setw(10) << setfill(' ') << left << calc_grade << "\n";
}
cout << "\n";
}
void functionImport(){
ifstream inFile;
ofstream outFile;
string student_name, maths_mark, chinese_mark, english_mark, grade;
int record_added;
record_added = 0;
string filename;
cout << "Please enter a file with list of student: ";
cin >> filename;
inFile.open(filename);
if (!inFile) {
cout << "Cannot open the input file." << endl;
// << "The program terminates." << endl;
// return 1;
}else{
while (true){
inFile >> student_name;
if (student_name == "END") {
break;
}else{
inFile >> maths_mark >> chinese_mark >> english_mark;
MarksRecord record;
record.name = student_name;
record.maths_marks = stoi(maths_mark);
record.chinese_marks = stoi(chinese_mark);
record.english_marks = stoi(english_mark);
records.push_back(record);
record_added = record_added + 1;
}
}
cout << "record imported : " << record_added << "\n";
cout << "\n";
inFile.close();
}
}
void functionExport(){
ofstream outFile;
string filename;
cout << "Please enter a file to export: ";
cin >> filename;
outFile.open(filename);
outFile << setw(10) << setfill(' ') << ""
<< "~ STUDENT RECORDS ~";
outFile << "\n";
outFile << "\n";
outFile
<< setw(10) << setfill(' ') << left << "Name" << setw(10)
<< setw(10) << setfill(' ') << left << "Maths" << setw(10)
<< setw(10) << setfill(' ') << left << "Chinese" << setw(10)
<< setw(10) << setfill(' ') << left << "English" << setw(10)
<< setw(10) << setfill(' ') << left << "Grade" << setw(10);
outFile << "" << setw(50) << setfill('-') << "\n";
outFile << "\n";
for (const auto& record : records) {
string calc_grade;
calc_grade = functionGetGrade(record.maths_marks, record.chinese_marks, record.english_marks);
outFile
<< setw(10) << setfill(' ') << left << record.name
<< setw(10) << setfill(' ') << left << record.maths_marks
<< setw(10) << setfill(' ') << left << record.chinese_marks
<< setw(10) << setfill(' ') << left << record.english_marks
// append 2 chars on the left
<< setw(2) << setfill(' ') << left << " "
<< setw(10) << setfill(' ') << left << calc_grade << "\n";
}
outFile << "\n";
cout << "export done !" << "\n";
}
int main() {
char choice;
while (true) {
print_menu();
cin >> choice;
switch (choice) {
case '1': functionAdd(); break;
case '2': functionDelete(); break;
case '3': functionEdit(); break;
case '4': functionPrint(); break;
case '5': functionImport(); break;
case '6': functionExport(); break;
// exit handler
case 'E':
case 'e':
return 0;
// exception handler
default:
cout << "Invalid choice. Please try again.\n";
}
}
return 0;
}

View File

@@ -0,0 +1,299 @@
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
struct MarksRecord {
string name;
int maths_marks, chinese_marks, english_marks;
string grade;
};
vector<MarksRecord> records;
string functionGetGrade(int maths_marks, int chinese_marks, int english_marks){
string grade;
int marks_sum;
float marks_avg;
marks_sum = 1;
marks_sum = (maths_marks + chinese_marks + english_marks);
marks_avg = marks_sum / 3;
if (marks_avg > 90) return "A";
if (marks_avg > 80) return "B";
if (marks_avg > 70) return "C";
return "D";
}
void print_menu() {
cout << "\n";
cout << "Mark Record System\n";
cout << "\n";
cout << "------------------------------------------\n";
cout << "1. Add marks record \n";
cout << "2. Delete marks record \n";
cout << "3. Edit marks record \n";
cout << "4. Print current marks record in system \n";
cout << "5. import records from file \n";
cout << "6. export records to file \n";
cout << "------------------------------------------\n";
cout << "E. Exit\n";
cout << "Enter your choice: ";
}
void functionAdd() {
string temp;
bool name_pass;
name_pass = false;
MarksRecord record;
while (!name_pass){
bool error;
error = false;
cout << "Enter name: ";
cin >> temp;
for (size_t i = 0; i < records.size(); i++) {
if (records[i].name == temp) {
error = true;
cout << "ERROR: duplicated student name found"<<"\n";
}
}
// check if name entered is empty
if (temp == string()){
error = true;
}
if (!error) {
record.name = temp;
name_pass = true;
}
}
cout << "Enter maths marks: ";
cin >> record.maths_marks;
cout << "Enter chinese marks: ";
cin >> record.chinese_marks;
cout << "Enter english marks: ";
cin >> record.english_marks;
records.push_back(record);
cout << "1 record added.";
}
void functionDelete() {
string name;
bool user_found;
user_found = false;
cout << "Enter name to delete: ";
cin >> name;
for (size_t i = 0; i < records.size(); i++) {
if (records[i].name == name) {
records.erase(records.begin() + i);
user_found = true;
break;
}
}
if (!user_found){
cout << "ERROR: student record not found";
}else{
cout << "1 record deleted.";
}
}
void functionEdit() {
string name;
string temp;
bool user_found;
cout << "Enter name to edit: ";
cin >> name;
for (size_t i = 0; i < records.size(); i++) {
if (records[i].name == name) {
user_found = true;
cout << "Enter new maths marks [- to skip]: ";
cin >> temp;
if (temp != "-") {
records[i].maths_marks = stoi(temp);
}
cout << "Enter new chinese marks [- to skip]: ";
cin >> temp;
if (temp != "-") {
records[i].chinese_marks = stoi(temp);
}
cout << "Enter new english marks [- to skip]: ";
cin >> temp;
if (temp != "-") {
records[i].english_marks = stoi(temp);
}
break;
}
}
if (!user_found){
cout << "sorry but no user found to update record";
}else{
cout << "1 record updated.";
}
}
void functionPrint() {
cout << endl;
cout
<< setw(10) << setfill(' ') << left << "Name" << setw(10)
<< setw(10) << setfill(' ') << left << "Maths" << setw(10)
<< setw(10) << setfill(' ') << left << "Chinese" << setw(10)
<< setw(10) << setfill(' ') << left << "English" << setw(10)
<< setw(10) << setfill(' ') << left << "Grade" << setw(10);
cout << "" << setw(50) << setfill('-') << "\n";
cout << "\n";
for (const auto& record : records) {
string calc_grade;
calc_grade = functionGetGrade(record.maths_marks, record.chinese_marks, record.english_marks);
cout
<< setw(10) << setfill(' ') << left << record.name
<< setw(10) << setfill(' ') << left << record.maths_marks
<< setw(10) << setfill(' ') << left << record.chinese_marks
<< setw(10) << setfill(' ') << left << record.english_marks
// append 2 chars on the left
<< setw(2) << setfill(' ') << left << " "
<< setw(10) << setfill(' ') << left << calc_grade << "\n";
}
cout << "\n";
}
void functionImport(){
ifstream inFile;
ofstream outFile;
string student_name, maths_mark, chinese_mark, english_mark, grade;
int record_added;
record_added = 0;
string filename;
cout << "Please enter a file with list of student: ";
cin >> filename;
inFile.open(filename);
if (!inFile) {
cout << "Cannot open the input file." << endl;
// << "The program terminates." << endl;
// return 1;
}else{
while (true){
inFile >> student_name;
if (student_name == "END") {
break;
}else{
inFile >> maths_mark >> chinese_mark >> english_mark;
MarksRecord record;
record.name = student_name;
record.maths_marks = stoi(maths_mark);
record.chinese_marks = stoi(chinese_mark);
record.english_marks = stoi(english_mark);
records.push_back(record);
record_added = record_added + 1;
}
}
cout << "record imported : " << record_added << "\n";
cout << "\n";
inFile.close();
}
}
void functionExport(){
ofstream outFile;
string filename;
cout << "Please enter a file to export: ";
cin >> filename;
outFile.open(filename);
outFile << setw(10) << setfill(' ') << ""
<< "~ STUDENT RECORDS ~";
outFile << "\n";
outFile << "\n";
outFile
<< setw(10) << setfill(' ') << left << "Name" << setw(10)
<< setw(10) << setfill(' ') << left << "Maths" << setw(10)
<< setw(10) << setfill(' ') << left << "Chinese" << setw(10)
<< setw(10) << setfill(' ') << left << "English" << setw(10)
<< setw(10) << setfill(' ') << left << "Grade" << setw(10);
outFile << "" << setw(50) << setfill('-') << "\n";
outFile << "\n";
for (const auto& record : records) {
string calc_grade;
calc_grade = functionGetGrade(record.maths_marks, record.chinese_marks, record.english_marks);
outFile
<< setw(10) << setfill(' ') << left << record.name
<< setw(10) << setfill(' ') << left << record.maths_marks
<< setw(10) << setfill(' ') << left << record.chinese_marks
<< setw(10) << setfill(' ') << left << record.english_marks
// append 2 chars on the left
<< setw(2) << setfill(' ') << left << " "
<< setw(10) << setfill(' ') << left << calc_grade << "\n";
}
outFile << "\n";
cout << "export done !" << "\n";
}
int main() {
char choice;
while (true) {
print_menu();
cin >> choice;
switch (choice) {
case '1': functionAdd(); break;
case '2': functionDelete(); break;
case '3': functionEdit(); break;
case '4': functionPrint(); break;
case '5': functionImport(); break;
case '6': functionExport(); break;
// exit handler
case 'E':
case 'e':
return 0;
// exception handler
default:
cout << "Invalid choice. Please try again.\n";
}
}
return 0;
}

View File

@@ -0,0 +1,5 @@
Ivy 86 96 83
Anson 55 70 67
Mark 56 89 74
Candy 78 63 97
END

View File

@@ -0,0 +1,69 @@
/**
* @file main.cpp
* @author Your Name
* @brief A brief description of the program.
*
* A more detailed description of the program.
*
* @version 0.1
* @date 2022-02-28
*
* @copyright Copyright (c) 2022 Your Name
*
*/
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
/**
* @brief A brief description of the main function.
*
* A more detailed description of the main function.
*
* @param argc command line argument count
* @param argv command line arguments
* @return int exit status
*/
int main(int argc, char** argv) {
// Declare variables
ifstream inFile;
ofstream outFile;
string burger_name, fries_name, icecream_name;
int burger_price, fries_price, icecream_price;
// Open the input file and output file
inFile.open("price.txt");
if (!inFile) {
cout << "Cannot open the input file."
<< "The program terminates." << endl;
return 1;
}
outFile.open("price_output.out");
cout << "Processing data" << endl;
// Read file word by word
inFile >> burger_name >> burger_price;
inFile >> fries_name >> fries_price;
inFile >> icecream_name >> icecream_price;
// Output file
outFile << "The price of " << burger_name
<< " is $" << burger_price <<"." << endl;
outFile << "The price of " << fries_name
<< " is $" << fries_price << "." << endl;
outFile << "The price of " << icecream_name
<< " is $" << icecream_price << "." << endl;
inFile.close(); //.close(): close a file
outFile.close();
cout << "Processing completed" << endl;
return 0;
}

View File

@@ -0,0 +1,403 @@
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
/**
* Represents a structure for storing marks records.
*
* Attributes:
* - name : a string representing the name of the student
* - maths_marks : an integer representing the marks obtained in maths
* - chinese_marks: an integer representing the marks obtained in chinese
* - english_marks: an integer representing the marks obtained in english
* - grade : a string representing the grade obtained based on the marks
*/
struct MarksRecord {
string name;
int maths_marks, chinese_marks, english_marks;
string grade;
};
vector<MarksRecord> records;
/**
* Calculates the grade based on the average marks obtained in three subjects.
*
* @param maths_marks The marks obtained in Maths.
* @param chinese_marks The marks obtained in Chinese.
* @param english_marks The marks obtained in English.
* @return The grade based on the average marks.
* - "A" if the average marks are greater than 90.
* - "B" if the average marks are greater than 80.
* - "C" if the average marks are greater than 70.
* - "D" otherwise.
*/
string functionGetGrade(int maths_marks, int chinese_marks, int english_marks){
string grade;
int marks_sum;
float marks_avg;
marks_sum = 1;
marks_sum = (maths_marks + chinese_marks + english_marks);
marks_avg = marks_sum / 3;
if (marks_avg > 90) return "A";
if (marks_avg > 80) return "B";
if (marks_avg > 70) return "C";
return "D";
}
/**
* Displays the menu options for the Mark Record System.
*
* The menu options include:
* 1. Add marks record
* 2. Delete marks record
* 3. Edit marks record
* 4. Print current marks record in system
* 5. Import records from file
* 6. Export records to file
* E. Exit
*
* The user is prompted to enter their choice after the menu is displayed.
*/
void print_menu() {
cout << "\n";
cout << "Mark Record System\n";
cout << "\n";
cout << "------------------------------------------\n";
cout << "1. Add marks record \n";
cout << "2. Delete marks record \n";
cout << "3. Edit marks record \n";
cout << "4. Print current marks record in system \n";
cout << "5. import records from file \n";
cout << "6. export records to file \n";
cout << "------------------------------------------\n";
cout << "E. Exit\n";
cout << "Enter your choice: ";
}
/**
* Adds a new marks record to the system.
*
* Prompts the user to enter the name, maths marks, chinese marks, and english marks for the new record.
* Checks if the entered name is already present in the records. If so, displays an error message.
* Checks if the entered name is empty. If so, displays an error message.
* If all checks pass, creates a new MarksRecord object with the entered details and adds it to the records vector.
*
*/
void functionAdd() {
string temp;
bool name_pass;
name_pass = false;
MarksRecord record;
while (!name_pass){
bool error;
error = false;
cout << "Enter name: ";
cin >> temp;
cin.clear();
// check if name entered is empty
if (temp == string()){
error = true;
}else if (temp.find(' ') != string::npos){ // check if name contain space
cout << "ERROR: name cannot contain space" << endl;
error = true;
}
if (!error) {
record.name = temp;
name_pass = true;
}
}
cout << "Enter maths marks: ";
cin >> record.maths_marks;
cout << "Enter chinese marks: ";
cin >> record.chinese_marks;
cout << "Enter english marks: ";
cin >> record.english_marks;
records.push_back(record);
cout << "1 record added.";
}
/**
* Deletes a marks record from the system.
*
* This function prompts the user to enter the name of the record to be deleted.
* It then searches for the record in the 'records' vector and removes it if found.
* If the record is not found, an error message is displayed.
*
*/
void functionDelete() {
string name;
bool user_found;
user_found = false;
cout << "Enter name to delete: ";
cin >> name;
for (size_t i = 0; i < records.size(); i++) {
if (records[i].name == name) {
records.erase(records.begin() + i);
user_found = true;
break;
}
}
if (!user_found){
cout << "ERROR: student record not found";
}else{
cout << "1 record deleted.";
}
}
/**
* Edit the marks record for a student.
*
* This function allows the user to edit the marks record for a student by entering the student's name and the new marks for each subject.
* The function searches for the student's name in the records vector and updates the marks accordingly.
* If the user enters "-" for a subject, the marks for that subject will be skipped and not updated.
*
*/
void functionEdit() {
string name;
string temp;
bool user_found;
user_found = false;
cout << "Enter name to edit: ";
cin >> name;
for (size_t i = 0; i < records.size(); i++) {
if (records[i].name == name) {
user_found = true;
cout << "Enter new maths marks [- to skip]: ";
cin >> temp;
if (temp != "-") {
records[i].maths_marks = stoi(temp);
}
cout << "Enter new chinese marks [- to skip]: ";
cin >> temp;
if (temp != "-") {
records[i].chinese_marks = stoi(temp);
}
cout << "Enter new english marks [- to skip]: ";
cin >> temp;
if (temp != "-") {
records[i].english_marks = stoi(temp);
}
break;
}
}
if (!user_found){
cout << "sorry but no user found to update record";
}else{
cout << "1 record updated.";
}
}
/**
* Prints the current marks record in the system.
*
* This function displays the student records stored in the 'records' vector.
* It prints the student name, maths marks, chinese marks, english marks, and grade for each record.
* The grade is calculated using the 'functionGetGrade' function.
*
* Note:
* - The records are printed in a tabular format with appropriate spacing.
* - The grade is calculated based on the maths marks, chinese marks, and english marks using the 'functionGetGrade' function.
*/
void functionPrint() {
cout << endl;
cout
<< setw(10) << setfill(' ') << left << "Name" << setw(10)
<< setw(10) << setfill(' ') << left << "Maths" << setw(10)
<< setw(10) << setfill(' ') << left << "Chinese" << setw(10)
<< setw(10) << setfill(' ') << left << "English" << setw(10)
<< setw(10) << setfill(' ') << left << "Grade" << setw(10);
cout << "" << setw(50) << setfill('-') << "\n";
cout << "\n";
for (const auto& record : records) {
string calc_grade;
calc_grade = functionGetGrade(record.maths_marks, record.chinese_marks, record.english_marks);
cout
<< setw(10) << setfill(' ') << left << record.name
<< setw(10) << setfill(' ') << left << record.maths_marks
<< setw(10) << setfill(' ') << left << record.chinese_marks
<< setw(10) << setfill(' ') << left << record.english_marks
// append 2 chars on the left
<< setw(2) << setfill(' ') << left << " "
<< setw(10) << setfill(' ') << left << calc_grade << "\n";
}
cout << "\n";
}
/**
* Imports records from a file and adds them to the current marks record system.
*
* This function prompts the user to enter the name of a file containing a list of student records.
* It then opens the file and reads each line, extracting the student name, maths marks, chinese marks, and english marks.
* A new MarksRecord object is created for each line and added to the records vector.
* The function keeps track of the number of records added and displays the total at the end.
*
* Note:
* - The file should be formatted with each line containing the student name, maths marks, chinese marks, and english marks separated by spaces.
* - The last line of the file should contain the word "END" to indicate the end of the records.
*/
void functionImport(){
ifstream inFile;
ofstream outFile;
string student_name, maths_mark, chinese_mark, english_mark, grade;
int record_added;
record_added = 0;
string filename;
cout << "Please enter a file with list of student: ";
cin >> filename;
inFile.open(filename);
if (!inFile) {
cout << "Cannot open the input file." << endl;
// << "The program terminates." << endl;
// return 1;
}else{
while (true){
inFile >> student_name;
if (student_name == "END") {
break;
}else{
inFile >> maths_mark >> chinese_mark >> english_mark;
MarksRecord record;
record.name = student_name;
record.maths_marks = stoi(maths_mark);
record.chinese_marks = stoi(chinese_mark);
record.english_marks = stoi(english_mark);
records.push_back(record);
record_added = record_added + 1;
}
}
cout << "record imported : " << record_added << "\n";
cout << "\n";
inFile.close();
}
}
/**
* Exports the student records to a file.
*
* This function prompts the user to enter the name of the file to export the records to.
* It then opens the file and writes the student records in a formatted manner.
* The file will contain the student name, maths marks, chinese marks, english marks, and grade for each record.
* The function uses the functionGetGrade() function to calculate the grade based on the marks.
*
* Note:
* - The file will be formatted with each record on a separate line.
* - The fields will be separated by spaces.
* - The file will start with a header line indicating the fields.
* - The file will end with an empty line.
*/
void functionExport(){
ofstream outFile;
string filename;
cout << "Please enter a file to export: ";
cin >> filename;
outFile.open(filename);
outFile << setw(10) << setfill(' ') << ""
<< "~ STUDENT RECORDS ~";
outFile << "\n";
outFile << "\n";
outFile
<< setw(10) << setfill(' ') << left << "Name" << setw(10)
<< setw(10) << setfill(' ') << left << "Maths" << setw(10)
<< setw(10) << setfill(' ') << left << "Chinese" << setw(10)
<< setw(10) << setfill(' ') << left << "English" << setw(10)
<< setw(10) << setfill(' ') << left << "Grade" << setw(10);
outFile << "" << setw(50) << setfill('-') << "\n";
outFile << "\n";
for (const auto& record : records) {
string calc_grade;
calc_grade = functionGetGrade(record.maths_marks, record.chinese_marks, record.english_marks);
outFile
<< setw(10) << setfill(' ') << left << record.name
<< setw(10) << setfill(' ') << left << record.maths_marks
<< setw(10) << setfill(' ') << left << record.chinese_marks
<< setw(10) << setfill(' ') << left << record.english_marks
// append 2 chars on the left
<< setw(2) << setfill(' ') << left << " "
<< setw(10) << setfill(' ') << left << calc_grade << "\n";
}
outFile << "\n";
cout << "export done !" << "\n";
}
/**
* Main function of the program.
*
* This function runs a menu-driven program for managing marks records.
* It continuously displays a menu and prompts the user for their choice.
* Based on the user's choice, it calls the corresponding functions to perform the desired operation.
* The program keeps running until the user chooses to exit (e/E).
*
*/
int main() {
char choice;
while (true) {
print_menu();
cin >> choice;
switch (choice) {
case '1': functionAdd(); break;
case '2': functionDelete(); break;
case '3': functionEdit(); break;
case '4': functionPrint(); break;
case '5': functionImport(); break;
case '6': functionExport(); break;
// exit handler
case 'E':
case 'e':
return 0;
// exception handler
default:
cout << "Invalid choice. Please try again.\n";
}
}
return 0;
}

View File

@@ -0,0 +1,5 @@
Ivy 86 96 83
Anson 55 70 67
Mark 56 89 74
Candy 78 63 97
END

View File

@@ -0,0 +1,297 @@
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
struct MarksRecord {
string name;
int maths_marks, chinese_marks, english_marks;
string grade;
};
vector<MarksRecord> records;
string functionGetGrade(int maths_marks, int chinese_marks, int english_marks){
string grade;
int marks_sum;
float marks_avg;
marks_sum = 1;
marks_sum = (maths_marks + chinese_marks + english_marks);
marks_avg = marks_sum / 3;
if (marks_avg > 90) return "A";
if (marks_avg > 80) return "B";
if (marks_avg > 70) return "C";
return "D";
}
void print_menu() {
cout << "\n";
cout << "Mark Record System\n";
cout << "\n";
cout << "------------------------------------------\n";
cout << "1. Add marks record \n";
cout << "2. Delete marks record \n";
cout << "3. Edit marks record \n";
cout << "4. Print current marks record in system \n";
cout << "5. import records from file \n";
cout << "6. export records to file \n";
cout << "------------------------------------------\n";
cout << "E. Exit\n";
cout << "Enter your choice: ";
}
void functionAdd() {
string temp;
bool name_pass;
name_pass = false;
MarksRecord record;
while (!name_pass){
bool error;
error = false;
cout << "Enter name: ";
cin >> temp;
for (size_t i = 0; i < records.size(); i++) {
if (records[i].name == temp) {
error = true;
cout << "ERROR: duplicated student name found"<<"\n";
}
}
// check if name entered is empty
if (temp == string()){
error = true;
}
if (!error) {
record.name = temp;
name_pass = true;
}
}
cout << "Enter maths marks: ";
cin >> record.maths_marks;
cout << "Enter chinese marks: ";
cin >> record.chinese_marks;
cout << "Enter english marks: ";
cin >> record.english_marks;
records.push_back(record);
}
void functionDelete() {
string name;
bool user_found;
user_found = false;
cout << "Enter name to delete: ";
cin >> name;
for (size_t i = 0; i < records.size(); i++) {
if (records[i].name == name) {
records.erase(records.begin() + i);
user_found = true;
break;
}
}
if (!user_found){
cout << "ERROR: student record not found";
}else{
cout << "1 record deleted.";
}
}
void functionEdit() {
string name;
string temp;
bool user_found;
cout << "Enter name to edit: ";
cin >> name;
for (size_t i = 0; i < records.size(); i++) {
if (records[i].name == name) {
user_found = true;
cout << "Enter new maths marks [- to skip]: ";
cin >> temp;
if (temp != "-") {
records[i].maths_marks = stoi(temp);
}
cout << "Enter new chinese marks [- to skip]: ";
cin >> temp;
if (temp != "-") {
records[i].chinese_marks = stoi(temp);
}
cout << "Enter new english marks [- to skip]: ";
cin >> temp;
if (temp != "-") {
records[i].english_marks = stoi(temp);
}
break;
}
}
if (!user_found){
cout << "sorry but no user found to update record";
}else{
cout << "1 record updated";
}
}
void functionPrint() {
cout << endl;
cout
<< setw(10) << setfill(' ') << left << "Name" << setw(10)
<< setw(10) << setfill(' ') << left << "Maths" << setw(10)
<< setw(10) << setfill(' ') << left << "Chinese" << setw(10)
<< setw(10) << setfill(' ') << left << "English" << setw(10)
<< setw(10) << setfill(' ') << left << "Grade" << setw(10);
cout << "" << setw(50) << setfill('-') << "\n";
cout << "\n";
for (const auto& record : records) {
string calc_grade;
calc_grade = functionGetGrade(record.maths_marks, record.chinese_marks, record.english_marks);
cout
<< setw(10) << setfill(' ') << left << record.name
<< setw(10) << setfill(' ') << left << record.maths_marks
<< setw(10) << setfill(' ') << left << record.chinese_marks
<< setw(10) << setfill(' ') << left << record.english_marks
// append 2 chars on the left
<< setw(2) << setfill(' ') << left << " "
<< setw(10) << setfill(' ') << left << calc_grade << "\n";
}
cout << "\n";
}
void functionImport(){
ifstream inFile;
ofstream outFile;
string student_name, maths_mark, chinese_mark, english_mark, grade;
int record_added;
record_added = 0;
string filename;
cout << "Please enter a file with list of student: ";
cin >> filename;
inFile.open(filename);
if (!inFile) {
cout << "Cannot open the input file." << endl;
// << "The program terminates." << endl;
// return 1;
}else{
while (true){
inFile >> student_name;
if (student_name == "END") {
break;
}else{
inFile >> maths_mark >> chinese_mark >> english_mark;
MarksRecord record;
record.name = student_name;
record.maths_marks = stoi(maths_mark);
record.chinese_marks = stoi(chinese_mark);
record.english_marks = stoi(english_mark);
records.push_back(record);
record_added = record_added + 1;
}
}
cout << "record imported : " << record_added << "\n";
cout << "\n";
inFile.close();
}
}
void functionExport(){
ofstream outFile;
string filename;
cout << "Please enter a file to export: ";
cin >> filename;
outFile.open(filename);
outFile << setw(10) << setfill(' ') << ""
<< "~ STUDENT RECORDS ~";
outFile << "\n";
outFile << "\n";
outFile
<< setw(10) << setfill(' ') << left << "Name" << setw(10)
<< setw(10) << setfill(' ') << left << "Maths" << setw(10)
<< setw(10) << setfill(' ') << left << "Chinese" << setw(10)
<< setw(10) << setfill(' ') << left << "English" << setw(10)
<< setw(10) << setfill(' ') << left << "Grade" << setw(10);
outFile << "" << setw(50) << setfill('-') << "\n";
outFile << "\n";
for (const auto& record : records) {
string calc_grade;
calc_grade = functionGetGrade(record.maths_marks, record.chinese_marks, record.english_marks);
outFile
<< setw(10) << setfill(' ') << left << record.name
<< setw(10) << setfill(' ') << left << record.maths_marks
<< setw(10) << setfill(' ') << left << record.chinese_marks
<< setw(10) << setfill(' ') << left << record.english_marks
// append 2 chars on the left
<< setw(2) << setfill(' ') << left << " "
<< setw(10) << setfill(' ') << left << calc_grade << "\n";
}
outFile << "\n";
cout << "export done !" << "\n";
}
int main() {
char choice;
while (true) {
print_menu();
cin >> choice;
switch (choice) {
case '1': functionAdd(); break;
case '2': functionDelete(); break;
case '3': functionEdit(); break;
case '4': functionPrint(); break;
case '5': functionImport(); break;
case '6': functionExport(); break;
// exit handler
case 'E':
case 'e':
return 0;
// exception handler
default:
cout << "Invalid choice. Please try again.\n";
}
}
return 0;
}

View File

@@ -0,0 +1,299 @@
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
struct MarksRecord {
string name;
int maths_marks, chinese_marks, english_marks;
string grade;
};
vector<MarksRecord> records;
string functionGetGrade(int maths_marks, int chinese_marks, int english_marks){
string grade;
int marks_sum;
float marks_avg;
marks_sum = 1;
marks_sum = (maths_marks + chinese_marks + english_marks);
marks_avg = marks_sum / 3;
if (marks_avg > 90) return "A";
if (marks_avg > 80) return "B";
if (marks_avg > 70) return "C";
return "D";
}
void print_menu() {
cout << "\n";
cout << "Mark Record System\n";
cout << "\n";
cout << "------------------------------------------\n";
cout << "1. Add marks record \n";
cout << "2. Delete marks record \n";
cout << "3. Edit marks record \n";
cout << "4. Print current marks record in system \n";
cout << "5. import records from file \n";
cout << "6. export records to file \n";
cout << "------------------------------------------\n";
cout << "E. Exit\n";
cout << "Enter your choice: ";
}
void functionAdd() {
string temp;
bool name_pass;
name_pass = false;
MarksRecord record;
while (!name_pass){
bool error;
error = false;
cout << "Enter name: ";
cin >> temp;
for (size_t i = 0; i < records.size(); i++) {
if (records[i].name == temp) {
error = true;
cout << "ERROR: duplicated student name found"<<"\n";
}
}
// check if name entered is empty
if (temp == string()){
error = true;
}
if (!error) {
record.name = temp;
name_pass = true;
}
}
cout << "Enter maths marks: ";
cin >> record.maths_marks;
cout << "Enter chinese marks: ";
cin >> record.chinese_marks;
cout << "Enter english marks: ";
cin >> record.english_marks;
records.push_back(record);
cout << "1 record added.";
}
void functionDelete() {
string name;
bool user_found;
user_found = false;
cout << "Enter name to delete: ";
cin >> name;
for (size_t i = 0; i < records.size(); i++) {
if (records[i].name == name) {
records.erase(records.begin() + i);
user_found = true;
break;
}
}
if (!user_found){
cout << "ERROR: student record not found";
}else{
cout << "1 record deleted.";
}
}
void functionEdit() {
string name;
string temp;
bool user_found;
cout << "Enter name to edit: ";
cin >> name;
for (size_t i = 0; i < records.size(); i++) {
if (records[i].name == name) {
user_found = true;
cout << "Enter new maths marks [- to skip]: ";
cin >> temp;
if (temp != "-") {
records[i].maths_marks = stoi(temp);
}
cout << "Enter new chinese marks [- to skip]: ";
cin >> temp;
if (temp != "-") {
records[i].chinese_marks = stoi(temp);
}
cout << "Enter new english marks [- to skip]: ";
cin >> temp;
if (temp != "-") {
records[i].english_marks = stoi(temp);
}
break;
}
}
if (!user_found){
cout << "sorry but no user found to update record";
}else{
cout << "1 record updated.";
}
}
void functionPrint() {
cout << endl;
cout
<< setw(10) << setfill(' ') << left << "Name" << setw(10)
<< setw(10) << setfill(' ') << left << "Maths" << setw(10)
<< setw(10) << setfill(' ') << left << "Chinese" << setw(10)
<< setw(10) << setfill(' ') << left << "English" << setw(10)
<< setw(10) << setfill(' ') << left << "Grade" << setw(10);
cout << "" << setw(50) << setfill('-') << "\n";
cout << "\n";
for (const auto& record : records) {
string calc_grade;
calc_grade = functionGetGrade(record.maths_marks, record.chinese_marks, record.english_marks);
cout
<< setw(10) << setfill(' ') << left << record.name
<< setw(10) << setfill(' ') << left << record.maths_marks
<< setw(10) << setfill(' ') << left << record.chinese_marks
<< setw(10) << setfill(' ') << left << record.english_marks
// append 2 chars on the left
<< setw(2) << setfill(' ') << left << " "
<< setw(10) << setfill(' ') << left << calc_grade << "\n";
}
cout << "\n";
}
void functionImport(){
ifstream inFile;
ofstream outFile;
string student_name, maths_mark, chinese_mark, english_mark, grade;
int record_added;
record_added = 0;
string filename;
cout << "Please enter a file with list of student: ";
cin >> filename;
inFile.open(filename);
if (!inFile) {
cout << "Cannot open the input file." << endl;
// << "The program terminates." << endl;
// return 1;
}else{
while (true){
inFile >> student_name;
if (student_name == "END") {
break;
}else{
inFile >> maths_mark >> chinese_mark >> english_mark;
MarksRecord record;
record.name = student_name;
record.maths_marks = stoi(maths_mark);
record.chinese_marks = stoi(chinese_mark);
record.english_marks = stoi(english_mark);
records.push_back(record);
record_added = record_added + 1;
}
}
cout << "record imported : " << record_added << "\n";
cout << "\n";
inFile.close();
}
}
void functionExport(){
ofstream outFile;
string filename;
cout << "Please enter a file to export: ";
cin >> filename;
outFile.open(filename);
outFile << setw(10) << setfill(' ') << ""
<< "~ STUDENT RECORDS ~";
outFile << "\n";
outFile << "\n";
outFile
<< setw(10) << setfill(' ') << left << "Name" << setw(10)
<< setw(10) << setfill(' ') << left << "Maths" << setw(10)
<< setw(10) << setfill(' ') << left << "Chinese" << setw(10)
<< setw(10) << setfill(' ') << left << "English" << setw(10)
<< setw(10) << setfill(' ') << left << "Grade" << setw(10);
outFile << "" << setw(50) << setfill('-') << "\n";
outFile << "\n";
for (const auto& record : records) {
string calc_grade;
calc_grade = functionGetGrade(record.maths_marks, record.chinese_marks, record.english_marks);
outFile
<< setw(10) << setfill(' ') << left << record.name
<< setw(10) << setfill(' ') << left << record.maths_marks
<< setw(10) << setfill(' ') << left << record.chinese_marks
<< setw(10) << setfill(' ') << left << record.english_marks
// append 2 chars on the left
<< setw(2) << setfill(' ') << left << " "
<< setw(10) << setfill(' ') << left << calc_grade << "\n";
}
outFile << "\n";
cout << "export done !" << "\n";
}
int main() {
char choice;
while (true) {
print_menu();
cin >> choice;
switch (choice) {
case '1': functionAdd(); break;
case '2': functionDelete(); break;
case '3': functionEdit(); break;
case '4': functionPrint(); break;
case '5': functionImport(); break;
case '6': functionExport(); break;
// exit handler
case 'E':
case 'e':
return 0;
// exception handler
default:
cout << "Invalid choice. Please try again.\n";
}
}
return 0;
}

View File

@@ -0,0 +1,5 @@
Ivy 86 96 83
Anson 55 70 67
Mark 56 89 74
Candy 78 63 97
END

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,10 @@
#!/usr/bin/env bash
set -ex
# g++ -o output helloworld.cpp
g++ -o output main.cpp
echo "build done"
./output

BIN
marissa.sam/task2/chrome_RlDTbVJXwU.png (Stored with Git LFS) Normal file

Binary file not shown.

BIN
marissa.sam/task2/deliver/1721723283615.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
marissa.sam/task2/deliver/chrome_OQtOQkTD0a.png (Stored with Git LFS) Normal file

Binary file not shown.

View File

@@ -0,0 +1,185 @@
#include <iostream>
#include <stack>
#include <string>
#include <cctype>
#include <map>
// 2. pseudo code of the algorithm
// convert to postfix expression use stack,
// 1. if it is an operand, add to postfix,
// 2. if it is an operator,
// 2.1 if it is the first operator, push into `s` stack
// 2.2 if `s` stack already got element, compare the weight of opreators
// 2.3 pop `s` stack until the top most one got smaller precedence
// 3. if it is a bracket, push into stack `s`,
// 3. if it is a closing bracket, pop all element from `s` stack back to postfix,
// else return the result
// Function to convert infix expression to postfix expression
std::string infixToPostfix(const std::string &infix)
{
std::stack<char> s;
std::string postfix;
std::map<char, int> precedence = {{'+', 1}, {'-', 1}, {'*', 2}, {'/', 2}, {'(', 0}};
for (char c : infix)
{
if (std::isalnum(c))
{ // 1. if operand, add to postfix
postfix += c;
}
// 其實括號入面同括號出面嘅數你當佢兩條式計就得
else if (c == '(')
{ // 2. if '(', push to stack
s.push(c);
}
// 咁佢呢度關括號呀嘛,
// 咁佢咪會由個 stack 嗰度 port 返曬啲operator出嚟囉, 變咗可以 14+5*
else if (c == ')')
{ // 3. if ')', pop until '('
while (!s.empty() && s.top() != '(')
{
postfix += s.top();
s.pop();
}
if (!s.empty())
s.pop(); // pop '('
}
else
{ // 4. if operator, pop operators with greater or equal precedence
while (!s.empty() && precedence[c] <= precedence[s.top()])
{
postfix += s.top();
s.pop();
}
s.push(c); // push current operator
}
}
// 5. pop remaining operators, LIFO
while (!s.empty())
{
postfix += s.top();
s.pop();
}
return postfix;
}
// Function to evaluate postfix expression
// 2. pseudo code of the algorithm
// evaluate a postfix expression use stack,
// if it is an operand, push it into stack,
// if it is an operator, pop the top two elements, apply the operator, and push the result,
// if it is a bracket, pop until the matched bracket, then push it into stack,
// if the input is end, return the last element in stack,
// else error
int evaluatePostfix(const std::string &postfix)
{
std::stack<int> s;
for (char c : postfix)
{
if (std::isdigit(c))
{ // 1. if operand, push to stack
s.push(c - '0');
}
else
{ // 2. if operator, pop two operands, evaluate and push result
int op2 = s.top();
s.pop();
int op1 = s.top();
s.pop();
switch (c)
{
case '+':
s.push(op1 + op2);
break;
case '-':
s.push(op1 - op2);
break;
case '*':
s.push(op1 * op2);
break;
case '/':
s.push(op1 / op2);
break;
}
}
}
return s.top(); // 3. result is the only element in stack
}
int main()
{
std::string infix;
// std::cout << "Enter an arithmetic expression: ";
// std::getline(std::cin, infix);
// --- part 2 iteration:
// 1,4,5,*,+
// 1,20,+
// 21
std::cout << "e.g. 1 1+4*5" << std::endl;
infix = "1+4*5";
std::string postfix = infixToPostfix(infix);
std::cout << "Postfix expression: " << postfix << std::endl;
int result = evaluatePostfix(postfix);
std::cout << "Result: " << result << std::endl;
// --- part 2 iteration:
// 1,4,+,5,*
// 5,5,*
// 25
std::cout << "e.g. 2 (1+4)*5" << std::endl;
infix = "(1+4)*5";
postfix = infixToPostfix(infix);
std::cout << "Postfix expression: " << postfix << std::endl;
result = evaluatePostfix(postfix);
std::cout << "Result: " << result << std::endl;
// --- part 2 iteration:
// 1,4,+,5,-
// 5,5,-
// 0
std::cout << "e.g. 3 1+4-5" << std::endl;
infix = "1+4-5";
postfix = infixToPostfix(infix);
std::cout << "Postfix expression: " << postfix << std::endl;
result = evaluatePostfix(postfix);
std::cout << "Result: " << result << std::endl;
// --- part 2 iteration:
// 1,4,5,*,+,5,*
// 1,20,+,5,*
// 21,5,*
// 105
std::cout << "e.g. 4 (1+4*5)*5" << std::endl;
infix = "(1+4*5)*5";
postfix = infixToPostfix(infix);
std::cout << "Postfix expression: " << postfix << std::endl;
result = evaluatePostfix(postfix);
std::cout << "Result: " << result << std::endl;
// --- part 2 iteration:
// 1,4,5,*,+,2,5,*,-
// =>1,20,+,2,5,*,-
// =>21,2,5,*,-
// =>21,10,-
// =>11
std::cout << "e.g. 5 (1+4*5)-2*5" << std::endl;
infix = "(1+4*5)-2*5";
postfix = infixToPostfix(infix);
std::cout << "Postfix expression: " << postfix << std::endl;
result = evaluatePostfix(postfix);
std::cout << "Result: " << result << std::endl;
return 0;
}

BIN
marissa.sam/task2/deliver_1/1721723283615.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
marissa.sam/task2/deliver_1/chrome_RlDTbVJXwU.png (Stored with Git LFS) Normal file

Binary file not shown.

View File

@@ -0,0 +1,363 @@
#include <iostream>
#include <stack>
#include <string>
#include <cctype>
#include <map>
#include <string.h>
#include <stdio.h>
#include <queue>
#include <algorithm>
#include <cassert>
// 1. Part 1 can create correct post-fix expression from the input
// (The operators are addition and multiplication.
// All the operands are positive integers between 1 and 9) (10%)
// 2. Part 2 can evaluate the simple post-fix expression correctly
// (The operators are addition and multiplication,
// all the operands are positive integers between 1 and 9.
// In the post-fix expression, all the operands and operators are separated by a “ ”.) (10%)
// 3. The operands can be any positive integers. (5%)
// 4. The operands can be negative integers. (5%)
// 5. The operands can be float numbers. (5%)
// 6. The operators can be “-, /, ^, (, )”. (5%)
// 2. pseudo code of the algorithm
// convert to postfix expression use stack,
// 1. if it is an operand, add to postfix,
// 2. if it is an operator,
// 2.1 if it is the first operator, push into `s` stack
// 2.2 if `s` stack already got element, compare the weight of opreators
// 2.3 pop `s` stack until the top most one got smaller precedence
// 3. if it is a bracket, push into stack `s`,
// 3. if it is a closing bracket, pop all element from `s` stack back to postfix,
// else return the result
/**
* check if a string is a number
* @param str_test the string to check
* @return true if the string is a number, false otherwise
*/
bool checkIsNumber(std::string &str_test)
{
bool output = true;
// check if the string is longer than 1 character
if (str_test.length() > 1)
{
// check each character in the string
for (auto &c : str_test)
{
// if the character is not a digit, a dot or a minus sign
if (!(('1' <= c && c <= '9') || c == '.' || c == '-'))
{
// set the output to false
output = false;
// break the loop
break;
}
}
}
else
{
// if the string is only one character long
char c = str_test[0];
// if the character is not a digit
if (!(('1' <= c && c <= '9')))
{
// set the output to false
output = false;
}
}
return output;
}
// 1. Part 1 can create correct post-fix expression from the input
std::queue<std::string> infixToPostfix(const std::string &infix)
{
std::queue<std::string> output;
std::stack<std::string> ss;
std::queue<std::string> q_postfix;
std::string postfix_temp;
std::map<std::string, int> q_precedence = {{"+", 1}, {"-", 1}, {"*", 2}, {"/", 2}, {"(", 0}, {"^", 3}};
std::stack<char *> sc;
std::queue<std::string> q;
const char s_delimit[2] = " ";
char *token;
// seperated by delimiter space
token = strtok(const_cast<char *>(infix.c_str()), s_delimit);
while (token != NULL)
{
q.push(token);
token = strtok(NULL, s_delimit);
}
while (!q.empty())
{
std::string temp = q.front();
q.pop();
if (checkIsNumber(temp))
{
q_postfix.push(temp);
}
// 其實括號入面同括號出面嘅數你當佢兩條式計就得
else if (temp.compare("(") == 0)
{
// 2. if '(', push to stack
ss.push(temp);
}
// 咁佢呢度關括號呀嘛,
// 咁佢咪會由個 stack 嗰度 port 返曬啲operator出嚟囉, 1+4*5 變咗 145*+
// 永遠解開 operator都係會向由左向右
else if (temp.compare(")") == 0)
{
while (!ss.empty() && ss.top() != "(")
{
q_postfix.push(ss.top());
ss.pop();
}
if (!ss.empty())
ss.pop(); // pop '('
}
else
{
// 4. if operator, pop operators with greater or equal precedence
// 比較兩個 operand 之間個先後次序, 數字大嘅計先
while (!ss.empty() && q_precedence[temp] <= q_precedence[ss.top()])
{
q_postfix.push(ss.top());
ss.pop();
}
ss.push(temp); // push current operator
}
}
// 5. pop last operator
while (!ss.empty())
{
q_postfix.push(ss.top());
ss.pop();
}
return q_postfix;
}
float evaluatePostfix(std::queue<std::string> &postfix)
{
std::stack<float> s;
std::string element = postfix.front();
std::cout << "Corresponding postfix expression: ";
while (!postfix.empty())
{
// 獨立 pop 番佢出嚟
std::string element = postfix.front();
postfix.pop();
std::cout << element << ' ';
if (checkIsNumber(element))
{ // 1. if operand, push to stack
s.push(std::stof(element));
}
else
{
// 如果見到係operator, 攞返嗰兩粒數字出嚟計計佢
float op2 = s.top();
s.pop();
float op1 = s.top();
s.pop();
switch (element[0])
{
case '+':
s.push(op1 + op2);
break;
case '-':
s.push(op1 - op2);
break;
case '*':
s.push(op1 * op2);
break;
case '/':
s.push(op1 / op2);
break;
case '^':
float temp = op1;
for (float i = 0; i < op2 - 1; i++)
{
op1 = op1 * temp;
}
s.push(op1);
break;
}
}
}
std::cout << std::endl;
return s.top();
}
int main()
{
std::queue<std::string> q_temp;
std::string infix;
float result = 0;
infix = "1 + 2";
std::cout << "e.g. " << infix << std::endl;
q_temp = infixToPostfix(infix);
result = evaluatePostfix(q_temp);
std::cout << "result: " << result << std::endl;
assert(result == 3);
// 3. The operands can be any positive integers. (5%)
// 6. The operators can be “-, /, ^, (, )”. (5%)
infix = "1 + 2 - 3 + 4 - 5 + 6 - 7 + 8 - 9";
std::cout << "e.g. " << infix << std::endl;
q_temp = infixToPostfix(infix);
result = evaluatePostfix(q_temp);
std::cout << "result: " << result << std::endl;
assert(result == -3);
// 3. The operands can be any positive integers. (5%)
// 6. The operators can be “-, /, ^, (, )”. (5%)
infix = "1 + 2 - 3 * 4 / 5 + 6 - 7 * 8 + 9";
std::cout << "e.g. " << infix << std::endl;
q_temp = infixToPostfix(infix);
result = evaluatePostfix(q_temp);
std::cout << "result: " << result << std::endl;
assert(std::abs(result - -40.4) < 0.0001);
// 4. The operands can be negative integers. (5%)
infix = "-1 + 2";
std::cout << "e.g. " << infix << std::endl;
q_temp = infixToPostfix(infix);
result = evaluatePostfix(q_temp);
std::cout << "result: " << result << std::endl;
assert(result == 1);
// 4. The operands can be negative integers. (5%)
infix = "1 + -2";
std::cout << "e.g. " << infix << std::endl;
q_temp = infixToPostfix(infix);
result = evaluatePostfix(q_temp);
std::cout << "result: " << result << std::endl;
assert(result == -1);
// 4. The operands can be negative integers. (5%)
infix = "-1 + -2";
std::cout << "e.g. " << infix << std::endl;
q_temp = infixToPostfix(infix);
result = evaluatePostfix(q_temp);
std::cout << "result: " << result << std::endl;
assert(result == -3);
// 5. The operands can be float numbers. (5%)
infix = "1.1 + 2.1";
std::cout << "e.g. " << infix << std::endl;
q_temp = infixToPostfix(infix);
result = evaluatePostfix(q_temp);
std::cout << "result: " << result << std::endl;
assert(std::abs(result - 3.2) < 0.001);
// 5. The operands can be float numbers. (5%)
// 4. The operands can be negative integers. (5%)
infix = "-1.1 + 2.3";
std::cout << "e.g. " << infix << std::endl;
q_temp = infixToPostfix(infix);
result = evaluatePostfix(q_temp);
std::cout << "result: " << result << std::endl;
assert(std::abs(result - 1.2) < 0.001);
// 5. The operands can be float numbers. (5%)
infix = "1.1 * 6";
std::cout << "e.g. " << infix << std::endl;
q_temp = infixToPostfix(infix);
result = evaluatePostfix(q_temp);
std::cout << "result: " << result << std::endl;
assert(std::abs(result - 6.6) < 0.001);
//
// 6. The operators can be “-, /, ^, (, )”. (5%)
infix = "1 + 2 ^ 3";
std::cout << "e.g. " << infix << std::endl;
q_temp = infixToPostfix(infix);
result = evaluatePostfix(q_temp);
std::cout << "result: " << result << std::endl;
assert(result == 9);
//
// 6. The operators can be “-, /, ^, (, )”. (5%)
infix = "( 1 + 4 ) * 5";
std::cout << "e.g. " << infix << std::endl;
q_temp = infixToPostfix(infix);
result = evaluatePostfix(q_temp);
assert(result == 25);
std::cout << "result: " << result << std::endl;
//
// 6. The operators can be “-, /, ^, (, )”. (5%)
infix = "( 1 + 4 * 5 ) * 5";
std::cout << "e.g. " << infix << std::endl;
q_temp = infixToPostfix(infix);
result = evaluatePostfix(q_temp);
assert(result == 105);
std::cout << "result: " << result << std::endl;
//
// 6. The operators can be “-, /, ^, (, )”. (5%)
infix = "( 1 + 4 * 5 ) - 2 * 5";
std::cout << "e.g. " << infix << std::endl;
q_temp = infixToPostfix(infix);
result = evaluatePostfix(q_temp);
assert(result == 11);
std::cout << "result: " << result << std::endl;
//
// 6. The operators can be “-, /, ^, (, )”. (5%)
infix = "( 11 + 4 * 5 ) - 6 * 5";
std::cout << "e.g. " << infix << std::endl;
q_temp = infixToPostfix(infix);
result = evaluatePostfix(q_temp);
assert(result == 1);
std::cout << "result: " << result << std::endl;
//
// 5. The operands can be float numbers. (5%)
// 6. The operators can be “-, /, ^, (, )”.
infix = "( 1.1 + 4 ) * 2";
std::cout << "e.g. " << infix << std::endl;
q_temp = infixToPostfix(infix);
result = evaluatePostfix(q_temp);
assert(std::abs(result - 10.2) < 0.001);
std::cout << "result: " << result << std::endl;
//
// 5. The operands can be float numbers. (5%)
// 6. The operators can be “-, /, ^, (, )”.
infix = "( 1 + 2 * 3 ) * 2";
std::cout << "e.g. " << infix << std::endl;
q_temp = infixToPostfix(infix);
result = evaluatePostfix(q_temp);
assert(std::abs(result - 10.2) < 0.001);
std::cout << "result: " << result << std::endl;
std::cout << "Enter an arithmetic expression: ";
std::getline(std::cin, infix);
q_temp = infixToPostfix(infix);
result = evaluatePostfix(q_temp);
std::cout << "result: " << result << std::endl;
std::cout << "done" << std::endl;
return 0;
}

View File

@@ -0,0 +1,7 @@
git status .
@pause
git add .
git commit -m"update marissa.sam task2,"
start git push

View File

@@ -0,0 +1,8 @@
#include <iostream>
int main()
{
std::cout<<"Hello World";
return 0;
}

363
marissa.sam/task2/main.cpp Normal file
View File

@@ -0,0 +1,363 @@
#include <iostream>
#include <stack>
#include <string>
#include <cctype>
#include <map>
#include <string.h>
#include <stdio.h>
#include <queue>
#include <algorithm>
#include <cassert>
// 1. Part 1 can create correct post-fix expression from the input
// (The operators are addition and multiplication.
// All the operands are positive integers between 1 and 9) (10%)
// 2. Part 2 can evaluate the simple post-fix expression correctly
// (The operators are addition and multiplication,
// all the operands are positive integers between 1 and 9.
// In the post-fix expression, all the operands and operators are separated by a “ ”.) (10%)
// 3. The operands can be any positive integers. (5%)
// 4. The operands can be negative integers. (5%)
// 5. The operands can be float numbers. (5%)
// 6. The operators can be “-, /, ^, (, )”. (5%)
// 2. pseudo code of the algorithm
// convert to postfix expression use stack,
// 1. if it is an operand, add to postfix,
// 2. if it is an operator,
// 2.1 if it is the first operator, push into `s` stack
// 2.2 if `s` stack already got element, compare the weight of opreators
// 2.3 pop `s` stack until the top most one got smaller precedence
// 3. if it is a bracket, push into stack `s`,
// 3. if it is a closing bracket, pop all element from `s` stack back to postfix,
// else return the result
/**
* check if a string is a number
* @param str_test the string to check
* @return true if the string is a number, false otherwise
*/
bool checkIsNumber(std::string &str_test)
{
bool output = true;
// check if the string is longer than 1 character
if (str_test.length() > 1)
{
// check each character in the string
for (auto &c : str_test)
{
// if the character is not a digit, a dot or a minus sign
if (!(('1' <= c && c <= '9') || c == '.' || c == '-'))
{
// set the output to false
output = false;
// break the loop
break;
}
}
}
else
{
// if the string is only one character long
char c = str_test[0];
// if the character is not a digit
if (!(('1' <= c && c <= '9')))
{
// set the output to false
output = false;
}
}
return output;
}
// 1. Part 1 can create correct post-fix expression from the input
std::queue<std::string> infixToPostfix(const std::string &infix)
{
std::queue<std::string> output;
std::stack<std::string> ss;
std::queue<std::string> q_postfix;
std::string postfix_temp;
std::map<std::string, int> q_precedence = {{"+", 1}, {"-", 1}, {"*", 2}, {"/", 2}, {"(", 0}, {"^", 3}};
std::stack<char *> sc;
std::queue<std::string> q;
const char s_delimit[2] = " ";
char *token;
// seperated by delimiter space
token = strtok(const_cast<char *>(infix.c_str()), s_delimit);
while (token != NULL)
{
q.push(token);
token = strtok(NULL, s_delimit);
}
while (!q.empty())
{
std::string temp = q.front();
q.pop();
if (checkIsNumber(temp))
{
q_postfix.push(temp);
}
// 其實括號入面同括號出面嘅數你當佢兩條式計就得
else if (temp.compare("(") == 0)
{
// 2. if '(', push to stack
ss.push(temp);
}
// 咁佢呢度關括號呀嘛,
// 咁佢咪會由個 stack 嗰度 port 返曬啲operator出嚟囉, 1+4*5 變咗 145*+
// 永遠解開 operator都係會向由左向右
else if (temp.compare(")") == 0)
{
while (!ss.empty() && ss.top() != "(")
{
q_postfix.push(ss.top());
ss.pop();
}
if (!ss.empty())
ss.pop(); // pop '('
}
else
{
// 4. if operator, pop operators with greater or equal precedence
// 比較兩個 operand 之間個先後次序, 數字大嘅計先
while (!ss.empty() && q_precedence[temp] <= q_precedence[ss.top()])
{
q_postfix.push(ss.top());
ss.pop();
}
ss.push(temp); // push current operator
}
}
// 5. pop last operator
while (!ss.empty())
{
q_postfix.push(ss.top());
ss.pop();
}
return q_postfix;
}
float evaluatePostfix(std::queue<std::string> &postfix)
{
std::stack<float> s;
std::string element = postfix.front();
std::cout << "Corresponding postfix expression: ";
while (!postfix.empty())
{
// 獨立 pop 番佢出嚟
std::string element = postfix.front();
postfix.pop();
std::cout << element << ' ';
if (checkIsNumber(element))
{ // 1. if operand, push to stack
s.push(std::stof(element));
}
else
{
// 如果見到係operator, 攞返嗰兩粒數字出嚟計計佢
float op2 = s.top();
s.pop();
float op1 = s.top();
s.pop();
switch (element[0])
{
case '+':
s.push(op1 + op2);
break;
case '-':
s.push(op1 - op2);
break;
case '*':
s.push(op1 * op2);
break;
case '/':
s.push(op1 / op2);
break;
case '^':
float temp = op1;
for (float i = 0; i < op2 - 1; i++)
{
op1 = op1 * temp;
}
s.push(op1);
break;
}
}
}
std::cout << std::endl;
return s.top();
}
int main()
{
std::queue<std::string> q_temp;
std::string infix;
float result = 0;
infix = "1 + 2";
std::cout << "e.g. " << infix << std::endl;
q_temp = infixToPostfix(infix);
result = evaluatePostfix(q_temp);
std::cout << "result: " << result << std::endl;
assert(result == 3);
// 3. The operands can be any positive integers. (5%)
// 6. The operators can be “-, /, ^, (, )”. (5%)
infix = "1 + 2 - 3 + 4 - 5 + 6 - 7 + 8 - 9";
std::cout << "e.g. " << infix << std::endl;
q_temp = infixToPostfix(infix);
result = evaluatePostfix(q_temp);
std::cout << "result: " << result << std::endl;
assert(result == -3);
// 3. The operands can be any positive integers. (5%)
// 6. The operators can be “-, /, ^, (, )”. (5%)
infix = "1 + 2 - 3 * 4 / 5 + 6 - 7 * 8 + 9";
std::cout << "e.g. " << infix << std::endl;
q_temp = infixToPostfix(infix);
result = evaluatePostfix(q_temp);
std::cout << "result: " << result << std::endl;
assert(std::abs(result - -40.4) < 0.0001);
// 4. The operands can be negative integers. (5%)
infix = "-1 + 2";
std::cout << "e.g. " << infix << std::endl;
q_temp = infixToPostfix(infix);
result = evaluatePostfix(q_temp);
std::cout << "result: " << result << std::endl;
assert(result == 1);
// 4. The operands can be negative integers. (5%)
infix = "1 + -2";
std::cout << "e.g. " << infix << std::endl;
q_temp = infixToPostfix(infix);
result = evaluatePostfix(q_temp);
std::cout << "result: " << result << std::endl;
assert(result == -1);
// 4. The operands can be negative integers. (5%)
infix = "-1 + -2";
std::cout << "e.g. " << infix << std::endl;
q_temp = infixToPostfix(infix);
result = evaluatePostfix(q_temp);
std::cout << "result: " << result << std::endl;
assert(result == -3);
// 5. The operands can be float numbers. (5%)
infix = "1.1 + 2.1";
std::cout << "e.g. " << infix << std::endl;
q_temp = infixToPostfix(infix);
result = evaluatePostfix(q_temp);
std::cout << "result: " << result << std::endl;
assert(std::abs(result - 3.2) < 0.001);
// 5. The operands can be float numbers. (5%)
// 4. The operands can be negative integers. (5%)
infix = "-1.1 + 2.3";
std::cout << "e.g. " << infix << std::endl;
q_temp = infixToPostfix(infix);
result = evaluatePostfix(q_temp);
std::cout << "result: " << result << std::endl;
assert(std::abs(result - 1.2) < 0.001);
// 5. The operands can be float numbers. (5%)
infix = "1.1 * 6";
std::cout << "e.g. " << infix << std::endl;
q_temp = infixToPostfix(infix);
result = evaluatePostfix(q_temp);
std::cout << "result: " << result << std::endl;
assert(std::abs(result - 6.6) < 0.001);
//
// 6. The operators can be “-, /, ^, (, )”. (5%)
infix = "1 + 2 ^ 3";
std::cout << "e.g. " << infix << std::endl;
q_temp = infixToPostfix(infix);
result = evaluatePostfix(q_temp);
std::cout << "result: " << result << std::endl;
assert(result == 9);
//
// 6. The operators can be “-, /, ^, (, )”. (5%)
infix = "( 1 + 4 ) * 5";
std::cout << "e.g. " << infix << std::endl;
q_temp = infixToPostfix(infix);
result = evaluatePostfix(q_temp);
assert(result == 25);
std::cout << "result: " << result << std::endl;
//
// 6. The operators can be “-, /, ^, (, )”. (5%)
infix = "( 1 + 4 * 5 ) * 5";
std::cout << "e.g. " << infix << std::endl;
q_temp = infixToPostfix(infix);
result = evaluatePostfix(q_temp);
assert(result == 105);
std::cout << "result: " << result << std::endl;
//
// 6. The operators can be “-, /, ^, (, )”. (5%)
infix = "( 1 + 4 * 5 ) - 2 * 5";
std::cout << "e.g. " << infix << std::endl;
q_temp = infixToPostfix(infix);
result = evaluatePostfix(q_temp);
assert(result == 11);
std::cout << "result: " << result << std::endl;
//
// 6. The operators can be “-, /, ^, (, )”. (5%)
infix = "( 11 + 4 * 5 ) - 6 * 5";
std::cout << "e.g. " << infix << std::endl;
q_temp = infixToPostfix(infix);
result = evaluatePostfix(q_temp);
assert(result == 1);
std::cout << "result: " << result << std::endl;
//
// 5. The operands can be float numbers. (5%)
// 6. The operators can be “-, /, ^, (, )”.
infix = "( 1.1 + 4 ) * 2";
std::cout << "e.g. " << infix << std::endl;
q_temp = infixToPostfix(infix);
result = evaluatePostfix(q_temp);
assert(std::abs(result - 10.2) < 0.001);
std::cout << "result: " << result << std::endl;
//
// 5. The operands can be float numbers. (5%)
// 6. The operators can be “-, /, ^, (, )”.
infix = "1 + ( 1 + ( 1 + 2 ^ 3 ) * 3 ^ 2 ) * 2";
std::cout << "e.g. " << infix << std::endl;
q_temp = infixToPostfix(infix);
result = evaluatePostfix(q_temp);
std::cout << "result: " << result << std::endl;
assert(std::abs(result - 165) < 0.001);
std::cout << "Enter an arithmetic expression: ";
std::getline(std::cin, infix);
q_temp = infixToPostfix(infix);
result = evaluatePostfix(q_temp);
std::cout << "result: " << result << std::endl;
std::cout << "done" << std::endl;
return 0;
}

View File

@@ -0,0 +1,30 @@
我跟住佢 part1 part2 個方向去做個 source code, 但係 report 嗰部分我就唔需要做, 我諗你係咁嘅意思係咪?
# NOTES
The project requires you to write a simple calculator to evaluate arithmetic expressions. It
includes two parts:
- In the first part, you need to convert the input to a postfix expression.
- In the second part, you need to evaluate the postfix expression that you get in the first
part.
- You need to implement your algorithms using c++. Submit your code to Moodle.
- You also need to write a report explaining the following items.
## Part 1: Convert an expression to postfix expression using stack in STL
1. how to read and store the input?
2. pseudo code of the algorithm
3. data structures used in the algorithm
4. time complexity
5. space complexity
6. how to store the postfix expression?
## Part 2: Use stack of STL to evaluate a postfix expression
1. how to read the postfix expression
2. pseudo code of the algorithm
3. data structures used in the algorithm
4. time complexity
5. space complexity
6. how to output the final result?

BIN
marissa.sam/task2/output Normal file

Binary file not shown.

View File

@@ -0,0 +1,22 @@
#include <string.h>
#include <stdio.h>
int main()
{
char str[80] = "This is - www.tutorialspoint.com - website";
const char s_delimit[2] = "-";
char *token;
/* get the first token */
token = strtok(str, s_delimit);
/* walk through other tokens */
while (token != NULL)
{
printf("%s\n", token);
token = strtok(NULL, s_delimit);
}
return (0);
}

BIN
marissa.sam/task2/temp/1721723283615.jpg (Stored with Git LFS) Normal file

Binary file not shown.

View File

@@ -0,0 +1,123 @@
#include <iostream>
#include <stack>
#include <cctype>
#include <cstring>
// 1. how to read and store the input?
std::string readInput()
{
std::string input;
char c;
while (std::cin.get(c)) {
if (c == '\n')
break;
input += c;
}
return input;
}
// 2. pseudo code of the algorithm
// convert to postfix expression use stack,
// 1. if it is an operand, push it into stack,
// 2. if it is an operator, pop the top two elements and push the operator result,
// 3. if it is a bracket, pop until the matched bracket, then push it into stack,
// 4. if the input is end, pop all the remaining elements,
// 5. if the last element is a bracket, return error,
// else return the result
std::string infixToPostfix(const std::string& input)
{
std::stack<char> stack;
std::string postfix;
const char* precedence = "+-*/()";
for (const auto& c : input) {
if (std::isalpha(c) || std::isdigit(c)) {
postfix += c;
} else if (c == '(') {
stack.push(c);
} else if (c == ')') {
while (!stack.empty() && stack.top() != '(') {
postfix += stack.top();
stack.pop();
}
if (!stack.empty()) {
stack.pop();
}
} else {
while (!stack.empty() && precedence[c] <= precedence[stack.top()]) {
postfix += stack.top();
stack.pop();
}
stack.push(c);
}
}
while (!stack.empty()) {
postfix += stack.top();
stack.pop();
}
return postfix;
}
// 3. data structures used in the algorithm
// stack
// string
// 4. time complexity
// O(n), where n is the length of input
// 5. space complexity
// O(n), where n is the length of input
// 6. how to store the postfix expression?
// using string
int main()
{
std::string input = readInput();
std::string postfix = infixToPostfix(input);
std::cout << postfix << std::endl;
// 1. how to read the postfix expression
// read postfix expression from standard input stream and store them into string
// 2. pseudo code of the algorithm
// evaluate a postfix expression use stack,
// if it is an operand, push it into stack,
// if it is an operator, pop the top two elements, apply the operator, and push the result,
// if it is a bracket, pop until the matched bracket, then push it into stack,
// if the input is end, return the last element in stack,
// else error
std::stack<int> stack;
const char* operators = "+-*/";
for (const auto& c : postfix) {
if (std::isalpha(c) || std::isdigit(c)) {
stack.push(c - '0');
} else if (std::strchr(operators, c)) {
int operand2 = stack.top();
stack.pop();
int operand1 = stack.top();
stack.pop();
switch (c) {
case '+':
stack.push(operand1 + operand2);
break;
case '-':
stack.push(operand1 - operand2);
break;
case '*':
stack.push(operand1 * operand2);
break;
case '/':
stack.push(operand1 / operand2);
break;
default:
return -1;
}
} else {
return -1;
}
}
std::cout << stack.top() << std::endl;
return 0;
}

View File

@@ -0,0 +1,121 @@
#include <iostream>
#include <stack>
#include <string>
#include <cctype>
#include <map>
// Function to convert infix expression to postfix expression
std::string infixToPostfix(const std::string &infix)
{
std::stack<char> s;
std::string postfix;
std::map<char, int> precedence = {{'+', 1}, {'-', 1}, {'*', 2}, {'/', 2}, {'(', 0}};
for (char c : infix)
{
if (std::isalnum(c))
{ // 1. if operand, add to postfix
postfix += c;
}
else if (c == '(')
{ // 2. if '(', push to stack
s.push(c);
}
else if (c == ')')
{ // 3. if ')', pop until '('
while (!s.empty() && s.top() != '(')
{
postfix += s.top();
s.pop();
}
if (!s.empty())
s.pop(); // pop '('
}
else
{ // 4. if operator, pop operators with greater or equal precedence
while (!s.empty() && precedence[c] <= precedence[s.top()])
{
postfix += s.top();
s.pop();
}
s.push(c); // push current operator
}
}
// 5. pop remaining operators
while (!s.empty())
{
postfix += s.top();
s.pop();
}
return postfix;
}
// Function to evaluate postfix expression
int evaluatePostfix(const std::string &postfix)
{
std::stack<int> s;
for (char c : postfix)
{
if (std::isdigit(c))
{ // 1. if operand, push to stack
s.push(c - '0');
}
else
{ // 2. if operator, pop two operands, evaluate and push result
int op2 = s.top();
s.pop();
int op1 = s.top();
s.pop();
switch (c)
{
case '+':
s.push(op1 + op2);
break;
case '-':
s.push(op1 - op2);
break;
case '*':
s.push(op1 * op2);
break;
case '/':
s.push(op1 / op2);
break;
}
}
}
return s.top(); // 3. result is the only element in stack
}
int main()
{
std::string infix;
// std::cout << "Enter an arithmetic expression: ";
// std::getline(std::cin, infix);
std::cout << "e.g. 1 1+4*5" << std::endl;
infix = "1+4*5";
std::string postfix = infixToPostfix(infix);
std::cout << "Postfix expression: " << postfix << std::endl;
int result = evaluatePostfix(postfix);
std::cout << "Result: " << result << std::endl;
std::cout << "e.g. 2 (1+4)*5" << std::endl;
infix = "(1+4)*5";
postfix = infixToPostfix(infix);
std::cout << "Postfix expression: " << postfix << std::endl;
result = evaluatePostfix(postfix);
std::cout << "Result: " << result << std::endl;
std::cout << "e.g. 3 1+4-5" << std::endl;
infix = "1+4-5";
postfix = infixToPostfix(infix);
std::cout << "Postfix expression: " << postfix << std::endl;
result = evaluatePostfix(postfix);
std::cout << "Result: " << result << std::endl;
return 0;
}

View File

@@ -0,0 +1,103 @@
#include <iostream>
#include <stack>
#include <string>
#include <cctype>
#include <map>
// Function to convert infix expression to postfix expression
std::string infixToPostfix(const std::string &infix)
{
std::stack<char> s;
std::string postfix;
std::map<char, int> precedence = {{'+', 1}, {'-', 1}, {'*', 2}, {'/', 2}, {'(', 0}};
for (char c : infix)
{
if (std::isalnum(c))
{ // 1. if operand, add to postfix
postfix += c;
}
else if (c == '(')
{ // 2. if '(', push to stack
s.push(c);
}
else if (c == ')')
{ // 3. if ')', pop until '('
while (!s.empty() && s.top() != '(')
{
postfix += s.top();
s.pop();
}
if (!s.empty())
s.pop(); // pop '('
}
else
{ // 4. if operator, pop operators with greater or equal precedence
while (!s.empty() && precedence[c] <= precedence[s.top()])
{
postfix += s.top();
s.pop();
}
s.push(c); // push current operator
}
}
// 5. pop remaining operators
while (!s.empty())
{
postfix += s.top();
s.pop();
}
return postfix;
}
// Function to evaluate postfix expression
int evaluatePostfix(const std::string &postfix)
{
std::stack<int> s;
for (char c : postfix)
{
if (std::isdigit(c))
{ // 1. if operand, push to stack
s.push(c - '0');
}
else
{ // 2. if operator, pop two operands, evaluate and push result
int op2 = s.top();
s.pop();
int op1 = s.top();
s.pop();
switch (c)
{
case '+':
s.push(op1 + op2);
break;
case '-':
s.push(op1 - op2);
break;
case '*':
s.push(op1 * op2);
break;
case '/':
s.push(op1 / op2);
break;
}
}
}
return s.top(); // 3. result is the only element in stack
}
int main()
{
std::string infix;
std::cout << "Enter an arithmetic expression: ";
std::getline(std::cin, infix);
std::string postfix = infixToPostfix(infix);
std::cout << "Postfix expression: " << postfix << std::endl;
int result = evaluatePostfix(postfix);
std::cout << "Result: " << result << std::endl;
return 0;
}

View File

@@ -0,0 +1,9 @@
#!/usr/bin/env bash
set -ex
g++ -o output helloworld.cpp
echo "build done"
./output

BIN
marissa.sam/task2/temp/chrome_OQtOQkTD0a.png (Stored with Git LFS) Normal file

Binary file not shown.

View File

@@ -0,0 +1,185 @@
#include <iostream>
#include <stack>
#include <string>
#include <cctype>
#include <map>
// 2. pseudo code of the algorithm
// convert to postfix expression use stack,
// 1. if it is an operand, add to postfix,
// 2. if it is an operator,
// 2.1 if it is the first operator, push into `s` stack
// 2.2 if `s` stack already got element, compare the weight of opreators
// 2.3 pop `s` stack until the top most one got smaller precedence
// 3. if it is a bracket, push into stack `s`,
// 3. if it is a closing bracket, pop all element from `s` stack back to postfix,
// else return the result
// Function to convert infix expression to postfix expression
std::string infixToPostfix(const std::string &infix)
{
std::stack<char> s;
std::string postfix;
std::map<char, int> precedence = {{'+', 1}, {'-', 1}, {'*', 2}, {'/', 2}, {'(', 0}};
for (char c : infix)
{
if (std::isalnum(c))
{ // 1. if operand, add to postfix
postfix += c;
}
// 其實括號入面同括號出面嘅數你當佢兩條式計就得
else if (c == '(')
{ // 2. if '(', push to stack
s.push(c);
}
// 咁去呢度關括號呀嘛,
// 咁佢咪會由個 stack 嗰度 port 返曬啲operator出嚟囉, 變咗可以 14+5*
else if (c == ')')
{ // 3. if ')', pop until '('
while (!s.empty() && s.top() != '(')
{
postfix += s.top();
s.pop();
}
if (!s.empty())
s.pop(); // pop '('
}
else
{ // 4. if operator, pop operators with greater or equal precedence
while (!s.empty() && precedence[c] <= precedence[s.top()])
{
postfix += s.top();
s.pop();
}
s.push(c); // push current operator
}
}
// 5. pop remaining operators, LIFO
while (!s.empty())
{
postfix += s.top();
s.pop();
}
return postfix;
}
// Function to evaluate postfix expression
// 2. pseudo code of the algorithm
// evaluate a postfix expression use stack,
// if it is an operand, push it into stack,
// if it is an operator, pop the top two elements, apply the operator, and push the result,
// if it is a bracket, pop until the matched bracket, then push it into stack,
// if the input is end, return the last element in stack,
// else error
int evaluatePostfix(const std::string &postfix)
{
std::stack<int> s;
for (char c : postfix)
{
if (std::isdigit(c))
{ // 1. if operand, push to stack
s.push(c - '0');
}
else
{ // 2. if operator, pop two operands, evaluate and push result
int op2 = s.top();
s.pop();
int op1 = s.top();
s.pop();
switch (c)
{
case '+':
s.push(op1 + op2);
break;
case '-':
s.push(op1 - op2);
break;
case '*':
s.push(op1 * op2);
break;
case '/':
s.push(op1 / op2);
break;
}
}
}
return s.top(); // 3. result is the only element in stack
}
int main()
{
std::string infix;
// std::cout << "Enter an arithmetic expression: ";
// std::getline(std::cin, infix);
// --- part 2 iteration:
// 1,4,5,*,+
// 1,20,+
// 21
std::cout << "e.g. 1 1+4*5" << std::endl;
infix = "1+4*5";
std::string postfix = infixToPostfix(infix);
std::cout << "Postfix expression: " << postfix << std::endl;
int result = evaluatePostfix(postfix);
std::cout << "Result: " << result << std::endl;
// --- part 2 iteration:
// 1,4,+,5,*
// 5,5,*
// 25
std::cout << "e.g. 2 (1+4)*5" << std::endl;
infix = "(1+4)*5";
postfix = infixToPostfix(infix);
std::cout << "Postfix expression: " << postfix << std::endl;
result = evaluatePostfix(postfix);
std::cout << "Result: " << result << std::endl;
// --- part 2 iteration:
// 1,4,+,5,-
// 5,5,-
// 0
std::cout << "e.g. 3 1+4-5" << std::endl;
infix = "1+4-5";
postfix = infixToPostfix(infix);
std::cout << "Postfix expression: " << postfix << std::endl;
result = evaluatePostfix(postfix);
std::cout << "Result: " << result << std::endl;
// --- part 2 iteration:
// 1,4,5,*,+,5,*
// 1,20,+,5,*
// 21,5,*
// 105
std::cout << "e.g. 4 (1+4*5)*5" << std::endl;
infix = "(1+4*5)*5";
postfix = infixToPostfix(infix);
std::cout << "Postfix expression: " << postfix << std::endl;
result = evaluatePostfix(postfix);
std::cout << "Result: " << result << std::endl;
// --- part 2 iteration:
// 1,4,5,*,+,2,5,*,-
// =>1,20,+,2,5,*,-
// =>21,2,5,*,-
// =>21,10,-
// =>11
std::cout << "e.g. 5 (1+4*5)-2*5" << std::endl;
infix = "(1+4*5)-2*5";
postfix = infixToPostfix(infix);
std::cout << "Postfix expression: " << postfix << std::endl;
result = evaluatePostfix(postfix);
std::cout << "Result: " << result << std::endl;
return 0;
}

View File

@@ -0,0 +1,185 @@
#include <iostream>
#include <stack>
#include <string>
#include <cctype>
#include <map>
// 2. pseudo code of the algorithm
// convert to postfix expression use stack,
// 1. if it is an operand, add to postfix,
// 2. if it is an operator,
// 2.1 if it is the first operator, push into `s` stack
// 2.2 if `s` stack already got element, compare the weight of opreators
// 2.3 pop `s` stack until the top most one got smaller precedence
// 3. if it is a bracket, push into stack `s`,
// 3. if it is a closing bracket, pop all element from `s` stack back to postfix,
// else return the result
// Function to convert infix expression to postfix expression
std::string infixToPostfix(const std::string &infix)
{
std::stack<char> s;
std::string postfix;
std::map<char, int> precedence = {{'+', 1}, {'-', 1}, {'*', 2}, {'/', 2}, {'(', 0}};
for (char c : infix)
{
if (std::isalnum(c))
{ // 1. if operand, add to postfix
postfix += c;
}
// 其實括號入面同括號出面嘅數你當佢兩條式計就得
else if (c == '(')
{ // 2. if '(', push to stack
s.push(c);
}
// 咁佢呢度關括號呀嘛,
// 咁佢咪會由個 stack 嗰度 port 返曬啲operator出嚟囉, 變咗可以 14+5*
else if (c == ')')
{ // 3. if ')', pop until '('
while (!s.empty() && s.top() != '(')
{
postfix += s.top();
s.pop();
}
if (!s.empty())
s.pop(); // pop '('
}
else
{ // 4. if operator, pop operators with greater or equal precedence
while (!s.empty() && precedence[c] <= precedence[s.top()])
{
postfix += s.top();
s.pop();
}
s.push(c); // push current operator
}
}
// 5. pop remaining operators, LIFO
while (!s.empty())
{
postfix += s.top();
s.pop();
}
return postfix;
}
// Function to evaluate postfix expression
// 2. pseudo code of the algorithm
// evaluate a postfix expression use stack,
// if it is an operand, push it into stack,
// if it is an operator, pop the top two elements, apply the operator, and push the result,
// if it is a bracket, pop until the matched bracket, then push it into stack,
// if the input is end, return the last element in stack,
// else error
int evaluatePostfix(const std::string &postfix)
{
std::stack<int> s;
for (char c : postfix)
{
if (std::isdigit(c))
{ // 1. if operand, push to stack
s.push(c - '0');
}
else
{ // 2. if operator, pop two operands, evaluate and push result
int op2 = s.top();
s.pop();
int op1 = s.top();
s.pop();
switch (c)
{
case '+':
s.push(op1 + op2);
break;
case '-':
s.push(op1 - op2);
break;
case '*':
s.push(op1 * op2);
break;
case '/':
s.push(op1 / op2);
break;
}
}
}
return s.top(); // 3. result is the only element in stack
}
int main()
{
std::string infix;
// std::cout << "Enter an arithmetic expression: ";
// std::getline(std::cin, infix);
// --- part 2 iteration:
// 1,4,5,*,+
// 1,20,+
// 21
std::cout << "e.g. 1 1+4*5" << std::endl;
infix = "1+4*5";
std::string postfix = infixToPostfix(infix);
std::cout << "Postfix expression: " << postfix << std::endl;
int result = evaluatePostfix(postfix);
std::cout << "Result: " << result << std::endl;
// --- part 2 iteration:
// 1,4,+,5,*
// 5,5,*
// 25
std::cout << "e.g. 2 (1+4)*5" << std::endl;
infix = "(1+4)*5";
postfix = infixToPostfix(infix);
std::cout << "Postfix expression: " << postfix << std::endl;
result = evaluatePostfix(postfix);
std::cout << "Result: " << result << std::endl;
// --- part 2 iteration:
// 1,4,+,5,-
// 5,5,-
// 0
std::cout << "e.g. 3 1+4-5" << std::endl;
infix = "1+4-5";
postfix = infixToPostfix(infix);
std::cout << "Postfix expression: " << postfix << std::endl;
result = evaluatePostfix(postfix);
std::cout << "Result: " << result << std::endl;
// --- part 2 iteration:
// 1,4,5,*,+,5,*
// 1,20,+,5,*
// 21,5,*
// 105
std::cout << "e.g. 4 (1+4*5)*5" << std::endl;
infix = "(1+4*5)*5";
postfix = infixToPostfix(infix);
std::cout << "Postfix expression: " << postfix << std::endl;
result = evaluatePostfix(postfix);
std::cout << "Result: " << result << std::endl;
// --- part 2 iteration:
// 1,4,5,*,+,2,5,*,-
// =>1,20,+,2,5,*,-
// =>21,2,5,*,-
// =>21,10,-
// =>11
std::cout << "e.g. 5 (1+4*5)-2*5" << std::endl;
infix = "(1+4*5)-2*5";
postfix = infixToPostfix(infix);
std::cout << "Postfix expression: " << postfix << std::endl;
result = evaluatePostfix(postfix);
std::cout << "Result: " << result << std::endl;
return 0;
}

Binary file not shown.

View File

@@ -0,0 +1,31 @@
# Task
I want you to write a simple calculator to evaluate arithmetic expressions.
Solve your problem step by step with C++.
Please consider the precedence of operators as well.
## It includes two parts:
- In the first part, you need to convert the input to a postfix expression.
- In the second part, you need to evaluate the postfix expression that you get in the first
part.
- You need to implement your algorithms using c++.
- You also need to write comment inside the code explaining the following items.
## Part 1: Convert an expression to postfix expression using stack in STL
1. how to read and store the input?
2. pseudo code of the algorithm
3. data structures used in the algorithm
4. time complexity
5. space complexity
6. how to store the postfix expression?
## Part 2: Use stack of STL to evaluate a postfix expression
1. how to read the postfix expression
2. pseudo code of the algorithm
3. data structures used in the algorithm
4. time complexity
5. space complexity
6. how to output the final result?