程式作業
程式作業
程式作業
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);
}
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();
}
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>
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();
s2.push(1);
return 0;
}
//=== 貼上程式碼
////////////////////////////header////////////////////////////////
#ifndef STACK_HPP_INCLUDED
#define STACK_HPP_INCLUDED
#include<iostream>
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"
int main()
{
MyStack<int> s1;
MyStack<double> s2;
s1.pop();
s2.push(1);
return 0;
}
#include <iostream>
#include <cstring> // for using strcpy in C++
#include <string>
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;
}
}
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};
ary1.sort();
ary1.show(); newline();
newline();
ary2.sort(1);
ary2.swap();
ary2.show(); newline();
return 0;
}