0% found this document useful (0 votes)
40 views

程式作業

The document defines a template class called Array that can store and manipulate arrays of different data types. The Array class has methods to display the array, find the maximum/minimum value, sort the array, calculate the sum and average, and more. It is tested in main() by creating Array objects to hold integer and float arrays, and calling various methods like getMax(), sort(), show(), etc.

Uploaded by

才阿
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

程式作業

The document defines a template class called Array that can store and manipulate arrays of different data types. The Array class has methods to display the array, find the maximum/minimum value, sort the array, calculate the sum and average, and more. It is tested in main() by creating Array objects to hold integer and float arrays, and calling various methods like getMax(), sort(), show(), etc.

Uploaded by

才阿
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

#include <iostream>

#include <cstring> // for using strcpy in C++


#include <string>

using namespace std;

void markline(int len=25, char mark='-') {


cout << endl;
for (int k = 0; k < len; ++k)
cout << mark;
cout << endl;
}

class Menu {
protected:
char shopName[30]; // What's the difference between char* shopName and char
shopName[30].
const char *coffee[4] = {"cappuccino", "latte", "espresso", "americano"}; // literal content

public:
Menu(char *shopname) {
strcpy(shopName, shopname);
}

virtual void showMenu() {


cout << shopName << endl;
for (int k = 0; k < 4; ++k) {
cout << k+1 << ") " << coffee[k] << endl;
}
}
};

class MyMenu : public Menu {


// derived : base
//* move below out of the MyMenu class
char *coffeeCName[4] = {
"卡布奇諾咖啡",
"拿鐵咖啡",
"義式濃縮",
"美式黑咖啡" };
//*/
//char **coffeeCName;
public:
// 所以至少要宣告一個建構函式!同時要負責建構 base 類別的物件!
MyMenu(char *shopname) : Menu(shopname) {
}

void showMenuC(){
for (int k = 0; k < 4; ++k) {
cout << k+1 << ") " << coffeeCName[k] << endl;
}
}
void showMenuCE(){
for (int k = 0; k < 4; ++k) {
cout << k+1 << ") " << coffeeCName[k] << "(" << coffee[k] << ")" <<
endl;
}
}
void showMenuEC(){
for (int k = 0; k < 4; ++k) {
cout << k+1 << ") " << coffee[k] << "(" << coffeeCName[k] << ")" <<
endl;
}
}
void showMenu() override {
cout << "C" <<endl;
showMenuC();
cout << "CE" <<endl;
showMenuCE();
cout << "EC" <<endl;
showMenuEC();
}

friend ostream &operator<< (ostream &s, MyMenu m);


};
ostream &operator<< (ostream &s, MyMenu m){
s << "底下是我們的 menu:" << endl;
for (int k = 0; k < 4; ++k) {
s << k+1 << ") " << m.coffeeCName[k] << endl;
}
return s;
}

int main() {
//*
Menu menu("北科咖啡");
menu.showMenu();
markline();
//*/

//*
MyMenu mymenu("我加咖啡");
mymenu.showMenu();
//*/
cout << mymenu;
return 0;
}

#include <iostream>
#include <cstring> // for using strcpy in C++
#include <string>

using namespace std;

template <class T>


class MyStack {
private:
T *value;
int index = -1; // 指向 stack 的頂端。
int size;
public:
MyStack(int size=10) {
if (size <= 0) size = 10;
value = new T[size];
if (value == NULL) {
cout << "Memory allocation failed!";
exit(-1);
}
this->size = size;
}
// deconstructor解構函式: 在原類別名稱前加上 ~,
// 這裡是需要的,用來釋放配置的記憶體。
~MyStack() {
delete [] value;
}

void push(T value) {


if ( ! isFull() ) {
this->value[++index] = value;
}
else {
cout << "Warning! Stack is FULL!" << endl;
}
}

int pop() {
if ( ! isEmpty() ) {
return value[--index];
}
else {
cout << "Warning! Stack is EMPTY!" << endl;
}
}

bool isEmpty() {
return (index == -1) ? true : false;
}

bool isFull() {
// DIY!
return (index == 9) ? true : false;
}

void showStack() {
if ( isEmpty() ) {
cout << "It is empty!" << endl;
return;
}
cout << "Content in the stack: (top-down)" << endl;
// show the stack's content below,
// DIY!
for (int k = index; k >= 0; --k) {
cout << value[k] << " ";
}
cout << endl;
}
};
int main(){
MyStack<int> s1;
MyStack<double> s2;
s1.pop();

for (double i = 1.1; i < 11; i ++){


s2.push(i);
s2.showStack();
}

s2.push(1);

return 0;
}

//=== 貼上程式碼
////////////////////////////header////////////////////////////////
#ifndef STACK_HPP_INCLUDED
#define STACK_HPP_INCLUDED
#include<iostream>

using namespace std;

template <class T>


class MyStack {
private:
T *value;
int index = -1; // 指向 stack 的頂端。
int size;
public:
MyStack(int size=10) {
if (size <= 0) size = 10;
value = new T[size];
if (value == NULL) {
cout << "Memory allocation failed!";
exit(-1);
}
this->size = size;
}
// deconstructor解構函式: 在原類別名稱前加上 ~,
// 這裡是需要的,用來釋放配置的記憶體。
~MyStack() {
delete [] value;
}

void push(T value) {


if ( ! isFull() ) {
this->value[++index] = value;
}
else {
cout << "Warning! Stack is FULL!" << endl;
}
}

int pop() {
if ( ! isEmpty() ) {
return value[--index];
}
else {
cout << "Warning! Stack is EMPTY!" << endl;
}
}

bool isEmpty() {
return (index == -1) ? true : false;
}

bool isFull() {
// DIY!
return (index == 9) ? true : false;
}

void showStack() {
if ( isEmpty() ) {
cout << "It is empty!" << endl;
return;
}
cout << "Content in the stack: (top-down)" << endl;
// show the stack's content below,
// DIY!
for (int k = index; k >= 0; --k) {
cout << value[k] << " ";
}
cout << endl;
}
};

#endif // STACK_HPP_INCLUDED

////////////////////////////////////////main///////////////////////////////////////
#include <iostream>
#include "stack.hpp"

using namespace std;

int main()
{
MyStack<int> s1;
MyStack<double> s2;
s1.pop();

for (double i = 1.1; i < 11; i ++){


s2.push(i);
s2.showStack();
}

s2.push(1);

return 0;
}
#include <iostream>
#include <cstring> // for using strcpy in C++
#include <string>

using namespace std;

void newline(int rows=1) {


for(int k = 0; k < rows; ++k) {
cout << endl;
}
}

template<class T>
class Array { // 對類別期望的清單!
private:
T *ary = NULL; // 在此類別中要處理的資料!(主角)
int len;
public:
// 建立此類別物件時,需要的初始設定。
Array(T *ary, int len) : ary(ary), len(len) {
// 底下的部分重複做了!
this->ary = ary;

if ( this->ary != NULL )
this->len = len;
else
this->len = 0;
}
// 在這個類別中,你希望提供的功能或服務!
void show(int cols=3) {
for (int k = 0; k < len; ++k) {
cout << ary[k] << " ";
if ((k+1)%cols == 0) {
cout << endl;
}
}
}

int getMax() {
if ( ary ) {
T maximum = ary[0];
for (int k = 1; k < len; ++k) {
if (maximum < ary[k]) {
maximum = ary[k];
}
}
return maximum;
}
else {
cout << "empty array!" << endl;
return 0;
}
}

int getMin() {
if ( ary ) {
T manimum = ary[0];
for (int k = 1; k < len; ++k) {
if (manimum > ary[k]) {
manimum = ary[k];
}
}
return manimum;
}
else {
cout << "empty array!" << endl;
return 0;
}
}

void swap() {
T t;
for (int k = 0; k < len / 2; ++k) {
t = ary[k];
ary[k] = ary[len-1-k];
ary[len-1-k] = t;
}
}

void sort(bool dir=0) { // default 0 for ascending order


for (int p = 0; p < len-1; ++p) {
for (int q = p +1; q < len; ++q) {
if (ary[p] > ary[q]) {
T t = ary[p];
ary[p] = ary[q];
ary[q] = t;
}
}
}
if (dir == 1) swap();
}

int sum() {
if (ary) {
T total = 0;
for (int k = 0; k < len; ++k) {
total += ary[k];
}
return total;
}
else {
cout << "empty array!" << endl;
return 0;
}
}

double average() {
if (ary) {
return (double)sum() / len;
}
else {
cout << "empty array!" << endl;
return 0;
}
}
};

int main(){
int iary[] = {2, 5, 8, 1, 4, 9, 3};
float fary[] = {1.1, 7.7, 2.2, 5, 4.4, 8.8};

int ilen = sizeof(iary) / sizeof(iary[0]);


int flen = sizeof(fary) / sizeof(fary[0]);

Array<int> ary1(iary, ilen);


ary1.show(); newline();

cout << "ary1.getMax()=" << ary1.getMax() << endl;

ary1.sort();

ary1.show(); newline();
newline();

Array<float> ary2(fary, flen);


ary2.show(); newline();

cout << "ary2.getMin()=" << ary2.getMin() << endl;

ary2.sort(1);
ary2.swap();

ary2.show(); newline();

return 0;
}

You might also like