diff --git a/kumalok/56059df2-1e9d-4f8b-8178-6d8063a507bd.jpeg b/kumalok/56059df2-1e9d-4f8b-8178-6d8063a507bd.jpeg new file mode 100644 index 00000000..c411409c Binary files /dev/null and b/kumalok/56059df2-1e9d-4f8b-8178-6d8063a507bd.jpeg differ diff --git a/kumalok/81e5e634-0c2b-4058-92eb-375e632c21fb.jpeg b/kumalok/81e5e634-0c2b-4058-92eb-375e632c21fb.jpeg new file mode 100644 index 00000000..7d2a0ef7 Binary files /dev/null and b/kumalok/81e5e634-0c2b-4058-92eb-375e632c21fb.jpeg differ diff --git a/kumalok/Book1.xlsx b/kumalok/Book1.xlsx new file mode 100644 index 00000000..da438993 Binary files /dev/null and b/kumalok/Book1.xlsx differ diff --git a/kumalok/NOTES.md b/kumalok/NOTES.md new file mode 100644 index 00000000..c094bf98 --- /dev/null +++ b/kumalok/NOTES.md @@ -0,0 +1,91 @@ +## requirement + +你好,中學clerk想整到個別學生考試時間表,出到准考證咁嘅樣 + +會有考生係要特別考試安排,例如 + - 加時、每45分鐘要放break 5分鐘、要轉筆作答等 + - 我想個solution係可以例個name list入去 + - 每科嘅考試原本嘅時間同長度 + - 但個VBA要計到哂呢啲加時、放break嘅考試時間 + +### Tasks (@HKD50 each): + +所以其實幾個tasks + + 1. 計每份卷嘅調整時間 (加時,加時及放breaks); + 1. output到每個有調整時間嘅考生個人嘅准考證; + 1. output到每日每份卷響個個房有咩學生要考 + +## 例子: +### 例1: +例如一般考生數學考2小時15分鐘咁 + +特別考試安排嘅考生會獲加時25% +即 135*1.25 = 168.75 分鐘 + +會 round down 至168分鐘作答時間 + + +### 例2: +例如一般考生數學考2小時15分鐘咁 +如果呢個考生加時以外,仲有「放break」的安排嘅話,佢就會每45分鐘暫停一次,有個5分鐘嘅小休,先再繼續作答 + +特別考試安排嘅考生會獲加時25% +即 135*1.25 = 168.75 分鐘 + +咁呢個情況,168分鐘作答時間就會斬成咁: + +45+(5)+45+(5)+45+(5)+18 完卷 + + + +## output: +係想出咁樣樣: + +### 考生1: (student1_draft.ods) +![Alt text](<_images/Screenshot from 2023-09-15 14-20-01.png>) + +- 6月1日 0810-0920 Rm.209 English (I) +- 6月3日 0810-0955 Rm.202 English (III) +- 6月3日 1025-1135 Rm.209 ICT(I) + + +### 考生2: (student2_draft.ods) +![Alt text](<_images/Screenshot from 2023-09-15 14-20-20.png>) + +- 6月1日 0810-0930 Rm.209 English(I) +- 6月3日 0810-0955 Rm.202 English (III) + - Supervised break(s): + - 0855-0900 + - 0945-0950 + + + + +### conversation + +2023-Jul-04 + +你好呀,你比左個訂 (HKD50) +不過基於私隱問題,我唔係咁方便去攞個現有嘅考試證去參考 + +如果方便的話,你不如手畫,好 rough/簡簡單單 嘅都可以 + 1. 一個表係張紙度,係張 table 大概嘅樣 (有D咩 field) + 2. 一張 form 係張紙度,係張考試證 (想要D咩 field) + +將上面呢兩張野影個相比我 +我大概整左個樣/logic/process 先, +然後再改好冇? + + +2023-jun-29 first order + + +2023-Sep-15 +你好呀,唔好意思,你比左個訂 (HKD50) +係關於 "自動砌準考證"。 +但係好對唔住我大意跟甩左。 +我想問下仲想唔想繼續整落去? +如果唔整落去的話我退番個訂比你。 + +唔好意思,對唔住係我疏忽。 diff --git a/kumalok/_images/Screenshot from 2023-09-15 14-20-01.png b/kumalok/_images/Screenshot from 2023-09-15 14-20-01.png new file mode 100644 index 00000000..00378e38 --- /dev/null +++ b/kumalok/_images/Screenshot from 2023-09-15 14-20-01.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:61c4401d76a20cbecf9d607357f8c7994a7f44fe9bb57209fd764315ffdd9693 +size 31264 diff --git a/kumalok/_images/Screenshot from 2023-09-15 14-20-20.png b/kumalok/_images/Screenshot from 2023-09-15 14-20-20.png new file mode 100644 index 00000000..3361461e --- /dev/null +++ b/kumalok/_images/Screenshot from 2023-09-15 14-20-20.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4b860a1cb3ae6bb4499cca77ff5e59fa186fc1d510e0963699187f3f122f7a82 +size 33446 diff --git a/kumalok/_output/student1_draft.ods b/kumalok/_output/student1_draft.ods new file mode 100644 index 00000000..09335f6d Binary files /dev/null and b/kumalok/_output/student1_draft.ods differ diff --git a/kumalok/_output/student2_draft.ods b/kumalok/_output/student2_draft.ods new file mode 100644 index 00000000..b8af1512 Binary files /dev/null and b/kumalok/_output/student2_draft.ods differ diff --git a/kumalok/gitUpdate.bat b/kumalok/gitUpdate.bat new file mode 100644 index 00000000..77400cb6 --- /dev/null +++ b/kumalok/gitUpdate.bat @@ -0,0 +1,7 @@ +git status . + +@pause + +git add . +git commit -m"update kumalok," +start git push \ No newline at end of file diff --git a/kumalok/improvement.md b/kumalok/improvement.md new file mode 100644 index 00000000..038c9e5f --- /dev/null +++ b/kumalok/improvement.md @@ -0,0 +1,8 @@ +1. office 2016 / office 2019 ? +1. 我會首先比一個最簡單嘅 excel 表 +1. 入面有一個最簡單嘅 vba +1. 首先要令到呢個 excel 表係你度同係我度都行得郁先 +1. 咁之後,我會企係呢個 excel 表度再落 code. +1. 咁我會再比份約莫做完嘅 code 你試,你 comment 一下 +1. 咁我跟個 comment 改 +1. 收貨,禮成 diff --git a/kumalok/output.ods b/kumalok/output.ods new file mode 100644 index 00000000..29ab3d6c Binary files /dev/null and b/kumalok/output.ods differ diff --git a/kumalok/package.json b/kumalok/package.json new file mode 100644 index 00000000..5fd5642a --- /dev/null +++ b/kumalok/package.json @@ -0,0 +1,13 @@ +{ + "name": "kumalok", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "gitUpdate": "git add . && git commit -m\"update kumalok,\"" + }, + "keywords": [], + "author": "", + "license": "ISC" +} diff --git a/marissa.sam/.gitignore b/marissa.sam/.gitignore new file mode 100644 index 00000000..e69de29b diff --git a/marissa.sam/_poc/helloworld.cpp b/marissa.sam/_poc/helloworld.cpp new file mode 100644 index 00000000..dad00c39 --- /dev/null +++ b/marissa.sam/_poc/helloworld.cpp @@ -0,0 +1,45 @@ +#include +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; +} diff --git a/marissa.sam/_prompts/helloworld copy.md b/marissa.sam/_prompts/helloworld copy.md new file mode 100644 index 00000000..ee3a265a --- /dev/null +++ b/marissa.sam/_prompts/helloworld copy.md @@ -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. diff --git a/marissa.sam/_prompts/helloworld.md b/marissa.sam/_prompts/helloworld.md new file mode 100644 index 00000000..ca7052b9 --- /dev/null +++ b/marissa.sam/_prompts/helloworld.md @@ -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. diff --git a/marissa.sam/_prompts/step1/step1.cpp b/marissa.sam/_prompts/step1/step1.cpp new file mode 100644 index 00000000..adea8f9e --- /dev/null +++ b/marissa.sam/_prompts/step1/step1.cpp @@ -0,0 +1,41 @@ +#include +#include + +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; +} diff --git a/marissa.sam/_prompts/step1/step1.md b/marissa.sam/_prompts/step1/step1.md new file mode 100644 index 00000000..1e216523 --- /dev/null +++ b/marissa.sam/_prompts/step1/step1.md @@ -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 + + + diff --git a/marissa.sam/_prompts/step2/prompt.md b/marissa.sam/_prompts/step2/prompt.md new file mode 100644 index 00000000..5a37d710 --- /dev/null +++ b/marissa.sam/_prompts/step2/prompt.md @@ -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 + +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 + + + diff --git a/marissa.sam/_prompts/step2/test.cpp b/marissa.sam/_prompts/step2/test.cpp new file mode 100644 index 00000000..28756149 --- /dev/null +++ b/marissa.sam/_prompts/step2/test.cpp @@ -0,0 +1,106 @@ +#include +#include +#include + +struct MarksRecord { + std::string name; + int maths_marks, chinese_marks, english_marks; +}; + +std::vector 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; +} diff --git a/marissa.sam/_prompts/step3/prompt.md b/marissa.sam/_prompts/step3/prompt.md new file mode 100644 index 00000000..c8a963ed --- /dev/null +++ b/marissa.sam/_prompts/step3/prompt.md @@ -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 +``` + + diff --git a/marissa.sam/_prompts/step3/test.txt b/marissa.sam/_prompts/step3/test.txt new file mode 100644 index 00000000..f32bc4c5 --- /dev/null +++ b/marissa.sam/_prompts/step3/test.txt @@ -0,0 +1,3 @@ +burger 15 +fries 11 +ice-cream 9 diff --git a/marissa.sam/_prompts/step3/write_file.cpp b/marissa.sam/_prompts/step3/write_file.cpp new file mode 100644 index 00000000..e8a19337 --- /dev/null +++ b/marissa.sam/_prompts/step3/write_file.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +#include + +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; +} diff --git a/marissa.sam/_prompts/step4.md b/marissa.sam/_prompts/step4.md new file mode 100644 index 00000000..bfb80b2e --- /dev/null +++ b/marissa.sam/_prompts/step4.md @@ -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 + + + diff --git a/marissa.sam/_prompts/step5.md b/marissa.sam/_prompts/step5.md new file mode 100644 index 00000000..bfb80b2e --- /dev/null +++ b/marissa.sam/_prompts/step5.md @@ -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 + + + diff --git a/marissa.sam/gitUpdate.bat b/marissa.sam/gitUpdate.bat new file mode 100644 index 00000000..ac02b560 --- /dev/null +++ b/marissa.sam/gitUpdate.bat @@ -0,0 +1,7 @@ +git status . + +@pause + +git add . +git commit -m"update marissa.sam," +start git push \ No newline at end of file diff --git a/marissa.sam/meta.md b/marissa.sam/meta.md new file mode 100644 index 00000000..70b1594b --- /dev/null +++ b/marissa.sam/meta.md @@ -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 diff --git a/marissa.sam/package.json b/marissa.sam/package.json new file mode 100644 index 00000000..36942d81 --- /dev/null +++ b/marissa.sam/package.json @@ -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" +} diff --git a/marissa.sam/task1/NOTES.md b/marissa.sam/task1/NOTES.md new file mode 100644 index 00000000..bb9491c8 --- /dev/null +++ b/marissa.sam/task1/NOTES.md @@ -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 + diff --git a/marissa.sam/task1/_ref/C++ Project.pdf b/marissa.sam/task1/_ref/C++ Project.pdf new file mode 100644 index 00000000..a052f949 Binary files /dev/null and b/marissa.sam/task1/_ref/C++ Project.pdf differ diff --git a/marissa.sam/task1/_ref/Chapter01/Chapter 01.pdf b/marissa.sam/task1/_ref/Chapter01/Chapter 01.pdf new file mode 100644 index 00000000..22f59b90 Binary files /dev/null and b/marissa.sam/task1/_ref/Chapter01/Chapter 01.pdf differ diff --git a/marissa.sam/task1/_ref/Chapter02/Chapter 02.pdf b/marissa.sam/task1/_ref/Chapter02/Chapter 02.pdf new file mode 100644 index 00000000..7efb91d6 Binary files /dev/null and b/marissa.sam/task1/_ref/Chapter02/Chapter 02.pdf differ diff --git a/marissa.sam/task1/_ref/Chapter02/helloworld.cpp b/marissa.sam/task1/_ref/Chapter02/helloworld.cpp new file mode 100644 index 00000000..e6970ddc --- /dev/null +++ b/marissa.sam/task1/_ref/Chapter02/helloworld.cpp @@ -0,0 +1,19 @@ +#include +#include + +using namespace std; + +int main() +{ + std::cout << "Price list of a restaurant"< +#include +#include +#include + +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; +} diff --git a/marissa.sam/task1/_ref/Chapter03/price.txt b/marissa.sam/task1/_ref/Chapter03/price.txt new file mode 100644 index 00000000..f32bc4c5 --- /dev/null +++ b/marissa.sam/task1/_ref/Chapter03/price.txt @@ -0,0 +1,3 @@ +burger 15 +fries 11 +ice-cream 9 diff --git a/marissa.sam/task1/_ref/Chapter04/Chapter 04.pdf b/marissa.sam/task1/_ref/Chapter04/Chapter 04.pdf new file mode 100644 index 00000000..77ceca78 Binary files /dev/null and b/marissa.sam/task1/_ref/Chapter04/Chapter 04.pdf differ diff --git a/marissa.sam/task1/_ref/Chapter04/helloworld.cpp b/marissa.sam/task1/_ref/Chapter04/helloworld.cpp new file mode 100644 index 00000000..e69de29b diff --git a/marissa.sam/task1/_ref/Chapter05/Chapter 05.pdf b/marissa.sam/task1/_ref/Chapter05/Chapter 05.pdf new file mode 100644 index 00000000..518a0d7b Binary files /dev/null and b/marissa.sam/task1/_ref/Chapter05/Chapter 05.pdf differ diff --git a/marissa.sam/task1/_ref/Chapter06/Chapter 06.pdf b/marissa.sam/task1/_ref/Chapter06/Chapter 06.pdf new file mode 100644 index 00000000..20757ce5 Binary files /dev/null and b/marissa.sam/task1/_ref/Chapter06/Chapter 06.pdf differ diff --git a/marissa.sam/task1/_ref/Chapter07/Chapter 07.pdf b/marissa.sam/task1/_ref/Chapter07/Chapter 07.pdf new file mode 100644 index 00000000..58964b4b Binary files /dev/null and b/marissa.sam/task1/_ref/Chapter07/Chapter 07.pdf differ diff --git a/marissa.sam/task1/_ref/Chapter08/Chapter 08.pdf b/marissa.sam/task1/_ref/Chapter08/Chapter 08.pdf new file mode 100644 index 00000000..cc082439 Binary files /dev/null and b/marissa.sam/task1/_ref/Chapter08/Chapter 08.pdf differ diff --git a/marissa.sam/task1/_ref/Chapter09/Chapter 09.pdf b/marissa.sam/task1/_ref/Chapter09/Chapter 09.pdf new file mode 100644 index 00000000..5b7d560c Binary files /dev/null and b/marissa.sam/task1/_ref/Chapter09/Chapter 09.pdf differ diff --git a/marissa.sam/task1/_ref/Chapter10/Chapter 10.pdf b/marissa.sam/task1/_ref/Chapter10/Chapter 10.pdf new file mode 100644 index 00000000..033f0a48 Binary files /dev/null and b/marissa.sam/task1/_ref/Chapter10/Chapter 10.pdf differ diff --git a/marissa.sam/task1/src - Copy/main copy.cpp b/marissa.sam/task1/src - Copy/main copy.cpp new file mode 100644 index 00000000..0c3c7535 --- /dev/null +++ b/marissa.sam/task1/src - Copy/main copy.cpp @@ -0,0 +1,297 @@ +#include +#include +#include +#include +#include + +using namespace std; + +struct MarksRecord { + string name; + int maths_marks, chinese_marks, english_marks; + string grade; +}; + +vector 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; +} + diff --git a/marissa.sam/task1/src - Copy/main.cpp b/marissa.sam/task1/src - Copy/main.cpp new file mode 100644 index 00000000..14e410ec --- /dev/null +++ b/marissa.sam/task1/src - Copy/main.cpp @@ -0,0 +1,299 @@ +#include +#include +#include +#include +#include + +using namespace std; + +struct MarksRecord { + string name; + int maths_marks, chinese_marks, english_marks; + string grade; +}; + +vector 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; +} + diff --git a/marissa.sam/task1/src - Copy/mark_list.txt b/marissa.sam/task1/src - Copy/mark_list.txt new file mode 100644 index 00000000..39137f12 --- /dev/null +++ b/marissa.sam/task1/src - Copy/mark_list.txt @@ -0,0 +1,5 @@ +Ivy 86 96 83 +Anson 55 70 67 +Mark 56 89 74 +Candy 78 63 97 +END diff --git a/marissa.sam/task1/src-with-comment/bread_example.cpp b/marissa.sam/task1/src-with-comment/bread_example.cpp new file mode 100644 index 00000000..390d1084 --- /dev/null +++ b/marissa.sam/task1/src-with-comment/bread_example.cpp @@ -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 +#include +#include +#include + +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; +} diff --git a/marissa.sam/task1/src-with-comment/main.cpp b/marissa.sam/task1/src-with-comment/main.cpp new file mode 100644 index 00000000..7521577d --- /dev/null +++ b/marissa.sam/task1/src-with-comment/main.cpp @@ -0,0 +1,403 @@ +#include +#include +#include +#include +#include + +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 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; +} + diff --git a/marissa.sam/task1/src-with-comment/mark_list.txt b/marissa.sam/task1/src-with-comment/mark_list.txt new file mode 100644 index 00000000..39137f12 --- /dev/null +++ b/marissa.sam/task1/src-with-comment/mark_list.txt @@ -0,0 +1,5 @@ +Ivy 86 96 83 +Anson 55 70 67 +Mark 56 89 74 +Candy 78 63 97 +END diff --git a/marissa.sam/task1/src/main copy.cpp b/marissa.sam/task1/src/main copy.cpp new file mode 100644 index 00000000..0c3c7535 --- /dev/null +++ b/marissa.sam/task1/src/main copy.cpp @@ -0,0 +1,297 @@ +#include +#include +#include +#include +#include + +using namespace std; + +struct MarksRecord { + string name; + int maths_marks, chinese_marks, english_marks; + string grade; +}; + +vector 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; +} + diff --git a/marissa.sam/task1/src/main.cpp b/marissa.sam/task1/src/main.cpp new file mode 100644 index 00000000..14e410ec --- /dev/null +++ b/marissa.sam/task1/src/main.cpp @@ -0,0 +1,299 @@ +#include +#include +#include +#include +#include + +using namespace std; + +struct MarksRecord { + string name; + int maths_marks, chinese_marks, english_marks; + string grade; +}; + +vector 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; +} + diff --git a/marissa.sam/task1/src/mark_list.txt b/marissa.sam/task1/src/mark_list.txt new file mode 100644 index 00000000..39137f12 --- /dev/null +++ b/marissa.sam/task1/src/mark_list.txt @@ -0,0 +1,5 @@ +Ivy 86 96 83 +Anson 55 70 67 +Mark 56 89 74 +Candy 78 63 97 +END diff --git a/marissa.sam/task2/41807bc9-5583-4771-9a94-3c314843429a_thumbnail.jpeg b/marissa.sam/task2/41807bc9-5583-4771-9a94-3c314843429a_thumbnail.jpeg new file mode 100644 index 00000000..095139b8 Binary files /dev/null and b/marissa.sam/task2/41807bc9-5583-4771-9a94-3c314843429a_thumbnail.jpeg differ diff --git a/marissa.sam/task2/Project requirement.pdf b/marissa.sam/task2/Project requirement.pdf new file mode 100644 index 00000000..f693f511 Binary files /dev/null and b/marissa.sam/task2/Project requirement.pdf differ diff --git a/marissa.sam/task2/_ref/Lecture 1 Introduction.pdf b/marissa.sam/task2/_ref/Lecture 1 Introduction.pdf new file mode 100644 index 00000000..8bb24da3 Binary files /dev/null and b/marissa.sam/task2/_ref/Lecture 1 Introduction.pdf differ diff --git a/marissa.sam/task2/_ref/Lecture 10 Heap.pdf b/marissa.sam/task2/_ref/Lecture 10 Heap.pdf new file mode 100644 index 00000000..7b24a963 Binary files /dev/null and b/marissa.sam/task2/_ref/Lecture 10 Heap.pdf differ diff --git a/marissa.sam/task2/_ref/Lecture 11 Hash.pdf b/marissa.sam/task2/_ref/Lecture 11 Hash.pdf new file mode 100644 index 00000000..df2d98d4 Binary files /dev/null and b/marissa.sam/task2/_ref/Lecture 11 Hash.pdf differ diff --git a/marissa.sam/task2/_ref/Lecture 2 Vector.pdf b/marissa.sam/task2/_ref/Lecture 2 Vector.pdf new file mode 100644 index 00000000..2088ff60 Binary files /dev/null and b/marissa.sam/task2/_ref/Lecture 2 Vector.pdf differ diff --git a/marissa.sam/task2/_ref/Lecture 3 Stack and Queue.pdf b/marissa.sam/task2/_ref/Lecture 3 Stack and Queue.pdf new file mode 100644 index 00000000..86e5063b Binary files /dev/null and b/marissa.sam/task2/_ref/Lecture 3 Stack and Queue.pdf differ diff --git a/marissa.sam/task2/_ref/Lecture 4 Sorting.pdf b/marissa.sam/task2/_ref/Lecture 4 Sorting.pdf new file mode 100644 index 00000000..e8a47627 Binary files /dev/null and b/marissa.sam/task2/_ref/Lecture 4 Sorting.pdf differ diff --git a/marissa.sam/task2/_ref/Lecture 5 Searching Algorithms.pdf b/marissa.sam/task2/_ref/Lecture 5 Searching Algorithms.pdf new file mode 100644 index 00000000..057d77e6 Binary files /dev/null and b/marissa.sam/task2/_ref/Lecture 5 Searching Algorithms.pdf differ diff --git a/marissa.sam/task2/_ref/Lecture 6 Tree.pdf b/marissa.sam/task2/_ref/Lecture 6 Tree.pdf new file mode 100644 index 00000000..af90a1d2 Binary files /dev/null and b/marissa.sam/task2/_ref/Lecture 6 Tree.pdf differ diff --git a/marissa.sam/task2/_ref/Lecture 7 BST.pdf b/marissa.sam/task2/_ref/Lecture 7 BST.pdf new file mode 100644 index 00000000..b1ee5abe Binary files /dev/null and b/marissa.sam/task2/_ref/Lecture 7 BST.pdf differ diff --git a/marissa.sam/task2/_ref/Lecture 8 Graph.pdf b/marissa.sam/task2/_ref/Lecture 8 Graph.pdf new file mode 100644 index 00000000..ce643d54 Binary files /dev/null and b/marissa.sam/task2/_ref/Lecture 8 Graph.pdf differ diff --git a/marissa.sam/task2/_ref/Lecture 9 Graph algorithms.pdf b/marissa.sam/task2/_ref/Lecture 9 Graph algorithms.pdf new file mode 100644 index 00000000..4fd1d9ff Binary files /dev/null and b/marissa.sam/task2/_ref/Lecture 9 Graph algorithms.pdf differ diff --git a/marissa.sam/task2/build.sh b/marissa.sam/task2/build.sh new file mode 100644 index 00000000..dc55335f --- /dev/null +++ b/marissa.sam/task2/build.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash + +set -ex + +# g++ -o output helloworld.cpp +g++ -o output main.cpp + +echo "build done" + +./output diff --git a/marissa.sam/task2/chrome_RlDTbVJXwU.png b/marissa.sam/task2/chrome_RlDTbVJXwU.png new file mode 100644 index 00000000..e53368b7 --- /dev/null +++ b/marissa.sam/task2/chrome_RlDTbVJXwU.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4d114c2f286b8b893e9b2264f63ecc7a8c73ebe9460de32787d4e6132cdf8e60 +size 36988 diff --git a/marissa.sam/task2/deliver/1721723283615.jpg b/marissa.sam/task2/deliver/1721723283615.jpg new file mode 100644 index 00000000..117e66d6 --- /dev/null +++ b/marissa.sam/task2/deliver/1721723283615.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:79cd9ecc20463eaaffc13f9abb082f2bec377e42c3433c95c28487b71af2d311 +size 1533692 diff --git a/marissa.sam/task2/deliver/chrome_OQtOQkTD0a.png b/marissa.sam/task2/deliver/chrome_OQtOQkTD0a.png new file mode 100644 index 00000000..8c597328 --- /dev/null +++ b/marissa.sam/task2/deliver/chrome_OQtOQkTD0a.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:feb926e4928cdb5d7f34e2b9b6007d8ee364ceb3c5268456e872e9fd8c5b256b +size 11394 diff --git a/marissa.sam/task2/deliver/main.cpp b/marissa.sam/task2/deliver/main.cpp new file mode 100644 index 00000000..5741de50 --- /dev/null +++ b/marissa.sam/task2/deliver/main.cpp @@ -0,0 +1,185 @@ +#include +#include +#include +#include +#include + +// 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 s; + std::string postfix; + std::map 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 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; +} diff --git a/marissa.sam/task2/deliver_1/1721723283615.jpg b/marissa.sam/task2/deliver_1/1721723283615.jpg new file mode 100644 index 00000000..117e66d6 --- /dev/null +++ b/marissa.sam/task2/deliver_1/1721723283615.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:79cd9ecc20463eaaffc13f9abb082f2bec377e42c3433c95c28487b71af2d311 +size 1533692 diff --git a/marissa.sam/task2/deliver_1/chrome_RlDTbVJXwU.png b/marissa.sam/task2/deliver_1/chrome_RlDTbVJXwU.png new file mode 100644 index 00000000..e53368b7 --- /dev/null +++ b/marissa.sam/task2/deliver_1/chrome_RlDTbVJXwU.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4d114c2f286b8b893e9b2264f63ecc7a8c73ebe9460de32787d4e6132cdf8e60 +size 36988 diff --git a/marissa.sam/task2/deliver_1/main.cpp b/marissa.sam/task2/deliver_1/main.cpp new file mode 100644 index 00000000..90681604 --- /dev/null +++ b/marissa.sam/task2/deliver_1/main.cpp @@ -0,0 +1,363 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// 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 infixToPostfix(const std::string &infix) +{ + std::queue output; + std::stack ss; + std::queue q_postfix; + std::string postfix_temp; + std::map q_precedence = {{"+", 1}, {"-", 1}, {"*", 2}, {"/", 2}, {"(", 0}, {"^", 3}}; + std::stack sc; + std::queue q; + + const char s_delimit[2] = " "; + char *token; + // seperated by delimiter space + token = strtok(const_cast(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 &postfix) +{ + std::stack 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 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; +} diff --git a/marissa.sam/task2/gitUpdate.bat b/marissa.sam/task2/gitUpdate.bat new file mode 100644 index 00000000..3ed105c8 --- /dev/null +++ b/marissa.sam/task2/gitUpdate.bat @@ -0,0 +1,7 @@ +git status . + +@pause + +git add . +git commit -m"update marissa.sam task2," +start git push diff --git a/marissa.sam/task2/helloworld.cpp b/marissa.sam/task2/helloworld.cpp new file mode 100644 index 00000000..030f1fc8 --- /dev/null +++ b/marissa.sam/task2/helloworld.cpp @@ -0,0 +1,8 @@ +#include + +int main() +{ + std::cout<<"Hello World"; + + return 0; +} diff --git a/marissa.sam/task2/main.cpp b/marissa.sam/task2/main.cpp new file mode 100644 index 00000000..9ef3a5fa --- /dev/null +++ b/marissa.sam/task2/main.cpp @@ -0,0 +1,363 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// 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 infixToPostfix(const std::string &infix) +{ + std::queue output; + std::stack ss; + std::queue q_postfix; + std::string postfix_temp; + std::map q_precedence = {{"+", 1}, {"-", 1}, {"*", 2}, {"/", 2}, {"(", 0}, {"^", 3}}; + std::stack sc; + std::queue q; + + const char s_delimit[2] = " "; + char *token; + // seperated by delimiter space + token = strtok(const_cast(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 &postfix) +{ + std::stack 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 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; +} diff --git a/marissa.sam/task2/notes.md b/marissa.sam/task2/notes.md new file mode 100644 index 00000000..a6d8c662 --- /dev/null +++ b/marissa.sam/task2/notes.md @@ -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? diff --git a/marissa.sam/task2/output b/marissa.sam/task2/output new file mode 100644 index 00000000..b012502d Binary files /dev/null and b/marissa.sam/task2/output differ diff --git a/marissa.sam/task2/strtok_tryout.cpp b/marissa.sam/task2/strtok_tryout.cpp new file mode 100644 index 00000000..f068c197 --- /dev/null +++ b/marissa.sam/task2/strtok_tryout.cpp @@ -0,0 +1,22 @@ +#include +#include + +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); +} diff --git a/marissa.sam/task2/temp/1721723283615.jpg b/marissa.sam/task2/temp/1721723283615.jpg new file mode 100644 index 00000000..117e66d6 --- /dev/null +++ b/marissa.sam/task2/temp/1721723283615.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:79cd9ecc20463eaaffc13f9abb082f2bec377e42c3433c95c28487b71af2d311 +size 1533692 diff --git a/marissa.sam/task2/temp/_draft/helloworld copy 2.cpp b/marissa.sam/task2/temp/_draft/helloworld copy 2.cpp new file mode 100644 index 00000000..8a3ae7ce --- /dev/null +++ b/marissa.sam/task2/temp/_draft/helloworld copy 2.cpp @@ -0,0 +1,123 @@ +#include +#include +#include +#include + +// 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 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 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; +} diff --git a/marissa.sam/task2/temp/_draft/helloworld copy 3.cpp b/marissa.sam/task2/temp/_draft/helloworld copy 3.cpp new file mode 100644 index 00000000..7077d3d7 --- /dev/null +++ b/marissa.sam/task2/temp/_draft/helloworld copy 3.cpp @@ -0,0 +1,121 @@ +#include +#include +#include +#include +#include + +// Function to convert infix expression to postfix expression +std::string infixToPostfix(const std::string &infix) +{ + std::stack s; + std::string postfix; + std::map 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 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; +} diff --git a/marissa.sam/task2/temp/_draft/helloworld copy.cpp b/marissa.sam/task2/temp/_draft/helloworld copy.cpp new file mode 100644 index 00000000..1c6668d5 --- /dev/null +++ b/marissa.sam/task2/temp/_draft/helloworld copy.cpp @@ -0,0 +1,103 @@ +#include +#include +#include +#include +#include + +// Function to convert infix expression to postfix expression +std::string infixToPostfix(const std::string &infix) +{ + std::stack s; + std::string postfix; + std::map 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 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; +} diff --git a/marissa.sam/task2/temp/build.sh b/marissa.sam/task2/temp/build.sh new file mode 100644 index 00000000..8e9e13be --- /dev/null +++ b/marissa.sam/task2/temp/build.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +set -ex + +g++ -o output helloworld.cpp + +echo "build done" + +./output diff --git a/marissa.sam/task2/temp/chrome_OQtOQkTD0a.png b/marissa.sam/task2/temp/chrome_OQtOQkTD0a.png new file mode 100644 index 00000000..8c597328 --- /dev/null +++ b/marissa.sam/task2/temp/chrome_OQtOQkTD0a.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:feb926e4928cdb5d7f34e2b9b6007d8ee364ceb3c5268456e872e9fd8c5b256b +size 11394 diff --git a/marissa.sam/task2/temp/helloworld.cpp b/marissa.sam/task2/temp/helloworld.cpp new file mode 100644 index 00000000..261e12d5 --- /dev/null +++ b/marissa.sam/task2/temp/helloworld.cpp @@ -0,0 +1,185 @@ +#include +#include +#include +#include +#include + +// 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 s; + std::string postfix; + std::map 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 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; +} diff --git a/marissa.sam/task2/temp/main.cpp b/marissa.sam/task2/temp/main.cpp new file mode 100644 index 00000000..5741de50 --- /dev/null +++ b/marissa.sam/task2/temp/main.cpp @@ -0,0 +1,185 @@ +#include +#include +#include +#include +#include + +// 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 s; + std::string postfix; + std::map 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 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; +} diff --git a/marissa.sam/task2/temp/output b/marissa.sam/task2/temp/output new file mode 100644 index 00000000..74586ebe Binary files /dev/null and b/marissa.sam/task2/temp/output differ diff --git a/marissa.sam/task2/temp/prompt.md b/marissa.sam/task2/temp/prompt.md new file mode 100644 index 00000000..4a6742d0 --- /dev/null +++ b/marissa.sam/task2/temp/prompt.md @@ -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? +