國立屏東大學 資訊工程系 物件導向程式設計
本次作業繳交將以資料夾的形式繳交,需要為每一題建立一個資料夾(資料夾名稱為該題題目前方之代號,第一題為「p1」,第二題為「p2」,餘以此類推)。
繳交說明可參考【Turnin 作業 4】中 p6 到 p10
任何未依照正確繳交格式的檔案將以 0 分計。
本文使用「▴」及「↵」代表「空白字元」與「Enter 換行字元」,並且將使用者輸入的部份使用灰階方式顯示。
另外,題目的執行結果中,如果出現「(」、「)」、「:」、「;」、「.」與「,」等符號,皆為英文半形!
Jepsen 想要撰寫一個能夠計算圓柱體與正四角柱(底面為正方形的柱體)這兩種容器表面積以及體積的程式。她想要讓使用者先選擇想要計算的容器:若選擇圓柱體(Cylinder),程式會向使用者詢問底面圓形的半徑(radius)以及柱體高度(height);若選擇正四角柱(Square prism),則會向使用者詢問底面正方形的邊長(side)以及柱體高度(height)。最後,請幫助 Jepsen 完成透過使用者輸入的內容計算出柱體的表面積與體積。
請參考下列的 main.cpp 程式(請依據檔名旁的路徑至 ws 取得程式碼):
#include "container.h"
#include <iostream>
using namespace std;
double get_input_greater_than_0()
{
double number;
bool first_time = true;
do
{
if (!first_time)
{
cout << "Value must be greater than 0!!! Please re-enter: ";
}
first_time = false;
cin >> number;
} while (number <= 0);
return number;
}
int main()
{
double input;
Container container;
Container *ptr = &container;
do
{
cout << "Please select a container(1 for Cylinder, 2 for Square Prism): ";
cin >> input;
} while ((input != 1) && (input != 2));
container.type = (input == 1) ? CYLINDER : SQUARE_PRISM;
if (container.type == CYLINDER)
{
cout << "Please input the radius of the base: ";
container.radius = get_input_greater_than_0();
}
else
{
cout << "Please input the side length of the square base: ";
container.side = get_input_greater_than_0();
}
cout << "Enter the height of the container: ";
container.height = get_input_greater_than_0();
calculate(ptr);
cout << "surface area = " << container.surface_area << endl;
cout << "volume = " << container.volume << endl;
return 0;
}
請完成名為 container.cpp 與 container.h 的 C++ 語言程式,其中分別包含 calculate 函式的 Implementation 與其 Prototype 宣告。
相關的使用者自訂資料型態請參考如下 required.h 檔案,請於「container.h」檔案中載入 required.h 標頭檔:
#define PI 3.14
enum Type
{
CYLINDER,
SQUARE_PRISM
};
struct Container
{
Type type;
union
{
double side;
double radius;
};
double height;
double surface_area;
double volume;
};
本題的相關程式將使用以下的 Makefile 進行編譯:
all: main.cpp container.o g++ main.cpp container.o container.o: container.cpp container.h g++ -c container.cpp clean: rm -f *.o *~ *.*~ a.out
此題的執行結果可參考如下:
[3:23▴user@ws▴p1]▴./a.out↵
Please▴select▴a▴container(1▴for▴Cylinder,▴2▴for▴Square▴Prism):▴1↵
Please▴input▴the▴radius▴of▴the▴base:▴10↵
Enter▴the▴height▴of▴the▴container:▴10↵
surface▴area▴=▴1256↵
volume▴=▴3140↵
[3:23▴user@ws▴p1]▴./a.out↵
Please▴select▴a▴container(1▴for▴Cylinder,▴2▴for▴Square▴Prism):▴2↵
Please▴input▴the▴side▴length▴of▴the▴square▴base:▴10↵
Enter▴the▴height▴of▴the▴container:▴10↵
surface▴area▴=▴600↵
volume▴=▴1000↵
[3:23▴user@ws▴p1]▴
請設計一個 C++ 語言程式,此程式將接收使用者輸入的一個整數 $n$,請配置(new)一個大小為 $n$ 的整數陣列,並找出前 $n$ 個質數並存入該陣列中。
請參考下列的 main.cpp 程式(可依據檔名旁的路徑至 ws 取得程式碼):
#include "prime_number.h"
#include <iostream>
using namespace std;
int get_input_greater_than_0()
{
int number;
bool first_time = true;
do
{
if (!first_time)
{
cout << "Your input must greater than 0!!! Please re-enter: ";
}
first_time = false;
cin >> number;
} while (number <= 0);
return number;
}
int main()
{
int prime_num_quantity;
int *ptr;
cout << "How many prime numbers do you want to display? ";
prime_num_quantity = get_input_greater_than_0();
allocate_for_prime_num(&ptr, prime_num_quantity);
for (int i = 0; i < prime_num_quantity; i++)
{
if (i != prime_num_quantity - 1)
{
cout << ptr[i] << ", ";
}
else
{
cout << ptr[i] << endl;
}
}
delete ptr;
return 0;
}
請完成名為 prime_number.cpp 與 prime_number.h 的 C++ 語言程式,其中分別包含 allocate_for_prime_num 函式的 Implementation 與其 Prototype 宣告。
本題的相關程式將使用以下的 Makefile 進行編譯:
all: main.cpp prime_number.o g++ main.cpp prime_number.o prime_number.o: prime_number.cpp prime_number.h g++ -c prime_number.cpp clean: rm -f *.o *~ *.*~ a.out
此題的執行結果可參考如下:
[3:23▴user@ws▴p2]▴./a.out↵
How▴many▴prime▴numbers▴do▴you▴want▴to▴display?▴10↵
2,▴3,▴5,▴7,▴11,▴13,▴17,▴19,▴23,▴29↵
[3:23▴user@ws▴p2]▴./a.out↵
How▴many▴prime▴numbers▴do▴you▴want▴to▴display?▴15↵
2,▴3,▴5,▴7,▴11,▴13,▴17,▴19,▴23,▴29,▴31,▴37,▴41,▴43,▴47↵
[3:23▴user@ws▴p2]▴
請設計一個 C++ 語言的程式,能夠對通訊錄的資料進行增刪查改。
請參考下列的 main.cpp 程式(請依據檔名旁的路徑至 ws 取得程式碼):
#include "contacts.h"
void help(){
cout << "------ manual ------" << endl;
cout << "a: add a contact" << endl;
cout << "d: delete a contact" << endl;
cout << "s: sort by name" << endl;
cout << "f: find a contact" << endl;
cout << "l: list all contact" << endl;
cout << "q: terminate this program" << endl;
cout << "h: show this manual" << endl;
}
int main()
{
bool quit = false;
char cmd = '\0';
int data_count = 0;
Contacts *data = nullptr;
Contacts *ptr = data;
while (!quit)
{
cout << ">> ";
cin >> cmd;
switch (cmd)
{
case 'h':
{
help();
break;
}
case 'a':
{
add_a_contact(&data, &data_count);
break;
}
case 'd':
{
delete_a_contact(&data, &data_count);
break;
}
case 's':
{
sort_by_name(&data, &data_count);
break;
}
case 'f':
{
find_a_contact(&data, &data_count);
break;
}
case 'l':
{
list_data(&data, &data_count);
break;
}
case 'q':
{
quit = true;
cout << "Bye!" << endl;
break;
}
default :
{
cout << "Unknown command. Please type \'h\' for help." << endl;
break;
}
}
}
delete[] data;
return 0;
}
請完成名為 contacts.cpp 與 contacts.h 的 C++ 語言程式,其中分別包含 add_a_contact、delete_a_contact、sort_by_name、find_a_contact 以及 list_data 函式的 Implementation 與其 Prototype 宣告,函式的實作要求將詳述如下:
相關的使用者自訂資料型態請參考如下 required.h 檔案,請於「contacts.h」檔案中載入 required.h 標頭檔:
#include <string>
using namespace std;
struct Contacts
{
string name;
struct Birthday
{
int year;
int month;
int day;
} birthday;
string phone_number;
string email;
};
本題的相關程式將使用以下的 Makefile 進行編譯:
all: main.cpp contacts.o g++ main.cpp contacts.o contacts.o: contacts.cpp contacts.h g++ -c contacts.cpp clean: rm -f *.o *~ *.*~ a.out
此題的執行結果可參考如下:
[3:23▴user@ws▴p3]▴./a.out↵
>>▴l↵
No▴contacts▴available.↵
>>▴a↵
Enter▴name:▴Frank↵
Enter▴birthday(year▴month▴day):▴3452▴34▴92↵
Enter▴phone▴number:▴0987654321↵
Enter▴email:▴frank@home↵
Contact▴added▴successfully!↵
>>▴a↵
Enter▴name:▴dennis↵
Enter▴birthday(year▴month▴day):▴9234▴23▴12↵
Enter▴phone▴number:▴0912345678↵
Enter▴email:▴dennis@wii.com↵
Contact▴added▴successfully!↵
>>▴a↵
Enter▴name:▴adam↵
Enter▴birthday(year▴month▴day):▴8341▴2▴19↵
Enter▴phone▴number:▴8472940573↵
Enter▴email:▴adam@adam↵
Contact▴added▴successfully!↵
>>▴a↵
Enter▴name:▴amy↵
Enter▴birthday(year▴month▴day):▴8934▴1▴1↵
Enter▴phone▴number:▴9254431333↵
Enter▴email:▴amy@school.tw↵
Contact▴added▴successfully!↵
>>▴l↵
▴▴Name▴▴▴▴▴Birthday▴▴▴Phone▴Number▴▴▴▴▴Email↵
[Frank][3452-34-92][0987654321][frank@home]↵
[dennis][9234-23-12][0912345678][dennis@wii.com]↵
[adam][8341-2-19][8472940573][adam@adam]↵
[amy][8934-1-1][9254431333][amy@school.tw]↵
>>▴s↵
Contacts▴sorted▴by▴name!↵
>>▴l↵
▴▴Name▴▴▴▴▴Birthday▴▴▴Phone▴Number▴▴▴▴▴Email↵
[Frank][3452-34-92][0987654321][frank@home]↵
[adam][8341-2-19][8472940573][adam@adam]↵
[amy][8934-1-1][9254431333][amy@school.tw]↵
[dennis][9234-23-12][0912345678][dennis@wii.com]↵
>>▴d↵
Enter▴the▴name▴of▴the▴contact▴to▴delete:▴emily↵
Contact▴not▴found!↵
>>▴d↵
Enter▴the▴name▴of▴the▴contact▴to▴delete:▴frank↵
Contact▴not▴found!↵
>>▴d↵
Enter▴the▴name▴of▴the▴contact▴to▴delete:▴Frank↵
Contact▴deleted▴successfully!↵
>>▴d↵
Enter▴the▴name▴of▴the▴contact▴to▴delete:▴adam↵
Contact▴deleted▴successfully!↵
>>▴l↵
▴▴Name▴▴▴▴▴Birthday▴▴▴Phone▴Number▴▴▴▴▴Email↵
[amy][8934-1-1][9254431333][amy@school.tw]↵
[dennis][9234-23-12][0912345678][dennis@wii.com]↵
>>▴d↵
Enter▴the▴name▴of▴the▴contact▴to▴delete:▴amy↵
Contact▴deleted▴successfully!↵
>>▴d↵
Enter▴the▴name▴of▴the▴contact▴to▴delete:▴dennis↵
Contact▴deleted▴successfully!↵
>>▴l↵
No▴contacts▴available.↵
>>▴s↵
Nothing▴to▴sort!↵
>>▴d↵
Nothing▴to▴delete!↵
>>▴q↵
Bye!↵
[3:23▴user@ws▴p3]▴
[3:23▴user@ws▴p3]▴./a.out↵
>>▴f↵
Nothing▴to▴find!↵
>>▴a↵
Enter▴name:▴Frank↵
Enter▴birthday(year▴month▴day):▴3452▴34▴92↵
Enter▴phone▴number:▴0912345678↵
Enter▴email:▴frank@home↵
Contact▴added▴successfully!↵
>>▴f↵
Enter▴the▴name▴of▴the▴contact▴to▴find:▴Frank↵
Name:▴Frank↵
Birthday:▴3452-34-92↵
Phone▴Number:▴0912345678↵
Email:▴frank@home↵
>>▴a↵
Enter▴name:▴dennis↵
Enter▴birthday(year▴month▴day):▴9234▴23▴12↵
Enter▴phone▴number:▴0912345678↵
Enter▴email:▴dennis@wii.com↵
Contact▴added▴successfully!↵
>>▴f↵
Enter▴the▴name▴of▴the▴contact▴to▴find:▴Dennis↵
Contact▴not▴found!↵
>>▴f↵
Enter▴the▴name▴of▴the▴contact▴to▴find:▴dennis↵
Name:▴dennis↵
Birthday:▴9234-23-12↵
Phone▴Number:▴0912345678↵
Email:▴dennis@wii.com↵
>>▴d↵
Enter▴the▴name▴of▴the▴contact▴to▴delete:▴dennis↵
Contact▴deleted▴successfully!↵
>>▴l↵
▴▴Name▴▴▴▴▴Birthday▴▴▴Phone▴Number▴▴▴▴▴Email↵
[Frank][3452-34-92][0912345678][frank@home]↵
>>▴d↵
Enter▴the▴name▴of▴the▴contact▴to▴delete:▴Frank↵
Contact▴deleted▴successfully!↵
>>▴f↵
Nothing▴to▴find!↵
>>▴d↵
Nothing▴to▴delete!↵
>>▴s↵
Nothing▴to▴sort!↵
>>▴q↵
Bye!↵
[3:23▴user@ws▴p3]▴
在我們日常生活中常常需要用手機 app 或是提款機查看帳戶裡的餘額、與朋友吃飯的時候會麻煩朋友先墊付之後再轉帳、現金不夠用的時候需要到 ATM 提款……在本題我們將依照這些日常生活設計一個 BankAccount 類別(Class)。
請參考下列的 main.cpp 程式(請依據檔名旁的路徑至 ws 取得程式碼):
#include "bank.h"
void show_div()
{
cout << "----------------------" << endl;
}
int main()
{
BankAccount Bob("Bob");
BankAccount Justin("Justin");
BankAccount Amy("Amy");
show_div();
Bob.show_balance();
Justin.show_balance();
Amy.show_balance();
show_div(); // ------------------
Bob.save(1945);
Justin.save(476);
Amy.save(1453);
show_div(); // ------------------
Bob.withdraw(1900);
Bob.transfer(46, Justin);
Bob.transfer(45, Amy);
Bob.show_balance();
show_div(); // ------------------
Amy.show_balance();
Amy.withdraw(15);
Amy.show_balance();
Amy.withdraw(2000);
Amy.withdraw(1500);
Amy.withdraw(1400);
Amy.show_balance();
show_div(); // ------------------
Justin.transfer(1453, Amy);
Justin.transfer(12, Amy);
Justin.show_balance();
Amy.show_balance();
return 0;
}
請完成名為 bank.cpp 與 bank.h 的 C++ 語言程式,其中分別包含 BankAccount 類別以及其成員函式 show_balance、save、withdraw 與 transfer 的 Implementation:
本題的相關程式將使用以下的 Makefile 進行編譯:
all: main.cpp g++ main.cpp clean: rm -f *.o *~ *.*~ a.out
此題的執行結果可參考如下:
[3:23▴user@ws▴p4]▴./a.out↵
----------------------↵
Bob's▴current▴balance:▴0↵
Justin's▴current▴balance:▴0↵
Amy's▴current▴balance:▴0↵
----------------------↵
Bob▴saved▴\$1945↵
Justin▴saved▴\$476↵
Amy▴saved▴\$1453↵
----------------------↵
Bob▴withdrew▴\$1900↵
Bob▴doesn't▴have▴enough▴money▴to▴transfer!↵
Bob▴transfered▴\$45▴to▴Amy.↵
Bob's▴current▴balance:▴0↵
----------------------↵
Amy's▴current▴balance:▴1498↵
Amy▴withdrew▴\$15↵
Amy's▴current▴balance:▴1483↵
Amy▴doesn't▴have▴enough▴money▴to▴withdraw!↵
Amy▴doesn't▴have▴enough▴money▴to▴withdraw!↵
Amy▴withdrew▴\$1400↵
Amy's▴current▴balance:▴83↵
----------------------↵
Justin▴doesn't▴have▴enough▴money▴to▴transfer!↵
Justin▴transfered▴\$12▴to▴Amy.↵
Justin's▴current▴balance:▴464↵
Amy's▴current▴balance:▴95↵
[3:23▴user@ws▴p4]▴
請撰寫一個 C++ 程式幫助助教計算全班的平均,並找出平均分數最高的學生。
請參考下列的 main.cpp 程式(請依據檔名旁的路徑至 ws 取得程式碼):
#include "student.h"
int main()
{
double class_average = 0;
Student student[] = {
{"Alex", "cbb999001", 82, 75, 93},
{"Bob", "cbb999002", 40, 73, 96},
{"Charlie", "cbb999003", 88, 76, 57},
{"Dennis", "cbb999004", 82, 21, 93},
{"Emily", "cbb999005", 82, 73, 76},
{"Frank", "cbb999006", 90, 94, 98},
{"George", "cbb999007", 82, 90, 93},
{"Hawk", "cbb999008", 90, 70, 93},
{"Iris", "cbb999009", 82, 73, 93},
{"Jessica", "cbb999010", 82, 33, 97}
};
Student highest = student[0];
for (int i = 0; i < 10; i++)
{
student[i].calculate_average();
if (student[i].average > highest.average)
{
highest = student[i];
}
class_average += student[i].average;
}
class_average /= 10.0;
cout << "Class Average = " << class_average << endl;
cout << "The student with the highest average score: "
<< highest.name << "(" << highest.stu_id << "): " << highest.average << endl;
return 0;
}
請完成名為 student.h 的 C++ 語言程式,其中分別包含 Student 類別中的成員函式 calculate_average 的 Implementation:
本題的相關程式將使用以下的 Makefile 進行編譯:
all: main.cpp g++ main.cpp clean: rm -f *.o *~ *.*~ a.out
此題的執行結果可參考如下:
[3:23▴user@ws▴p5]▴./a.out↵
Class▴Average▴=▴78.5↵
The▴student▴with▴the▴highest▴average▴score:▴Frank(cbb999006):▴94↵
[3:23▴user@ws▴p5]▴