This commit is contained in:
zhuyijun 2021-09-05 23:52:20 +08:00
parent 6701a8994a
commit 2bc5789ae7
5 changed files with 185 additions and 1 deletions

View File

@ -60,4 +60,6 @@ add_executable(uses_stuc reusing/uses_stuc.cpp)
add_executable(stacktp stl/stacktp.cpp)
add_executable(stacktp1 stl/stacktp1.cpp)
add_executable(arraytp stl/arraytp.cpp)
add_executable(tv friend/tv.cpp friend/use_tv.cpp friend/tvfm.h)
add_executable(tv friend/tv.cpp friend/use_tv.cpp friend/tvfm.h)
add_executable(queuetp friend/queuetp.cpp)
add_executable(error1 exception/error1.cpp)

32
base/exception/error1.cpp Normal file
View File

@ -0,0 +1,32 @@
//
// Created by nicemoe on 2021/9/5.
//
#include <iostream>
#include <cstdlib>
using namespace std;
double hmean(double a, double b);
int main() {
double x, y, z;
cout << "Enter two numbers: ";
while (cin >> x >> y) {
z = hmean(x, y);
cout << "Harmonice mean of " << x << " and " << y << " is " << z << endl;
cout << "Rnter next set of numbers <q to quit>:";
}
cout << "Bye!\n";
return 0;
}
double hmean(double a, double b) {
if (a == -b) {
cout << "untenable argument to hmean()\n";
abort();
}
return 2.0 * a * b / (a + b);
}

28
base/friend/queuetp.cpp Normal file
View File

@ -0,0 +1,28 @@
//
// Created by nicemoe on 2021/9/5.
//
#include <iostream>
#include <string>
#include "queuetp.h"
using namespace std;
int main() {
QueueTp<string> cs(5);
string temp;
while (!cs.isFull()){
cout <<"Please enter your name. You will be served in the order of arrival.\n";
getline(cin,temp);
cs.enQueue(temp);
}
cout <<"The queue is full. Processing begins!\n";
while (!cs.isEmpty()){
cs.deQueue(temp);
cout <<"Now processing " << temp<<"...\n";
}
return 0;
}

89
base/friend/queuetp.h Normal file
View File

@ -0,0 +1,89 @@
//
// Created by nicemoe on 2021/9/5.
//
#ifndef BASE_QueueTp_H
#define BASE_QueueTp_H
template<class Item>
class QueueTp {
private:
enum {
Q_SIZE = 10
};
class Node {
public:
Item item;
Node *next;
Node(const Item &i) : item(i), next(0) {}
};
Node *front;
Node *rear;
int items;
const int qsize;
QueueTp(const QueueTp &q) : qsize(0) {}
QueueTp &operator=(const QueueTp &q) {
return *this;
}
public:
QueueTp(int qs = Q_SIZE);
~QueueTp(){};
bool isEmpty() const {
return items == 0;
}
bool isFull() const {
return items == qsize;
}
int queueCount() const {
return items;
}
bool enQueue(const Item &item);
bool deQueue(Item &item);
};
template<class Item>
QueueTp<Item>::QueueTp(int qs):qsize(qs) {
front = rear = 0;
items = 0;
}
template<class Item>
bool QueueTp<Item>::enQueue(const Item &item) {
if (isFull()) return false;
Node *add = new Node(item);
items++;
if (front == 0) {
front = add;
} else {
rear->next = add;
}
rear = add;
return true;
}
template<class Item>
bool QueueTp<Item>::deQueue(Item &item) {
if (front == 0) return false;
item = front->item;
items--;
Node *temp = front;
front = front->next;
delete temp;
if (items == 0) rear = 0;
return true;
}
#endif //BASE_QueueTp_H

33
base/friend/use_tvfm.cpp Normal file
View File

@ -0,0 +1,33 @@
//
// Created by zhuyi on 2021/9/3.
//
#include <iostream>
#include "tvfm.h"
int main(){
using std::cout;
using std::endl;
Tv s42;
cout <<"Initial settings for 42\" TV:\n";
s42.settings();
s42.onoff();
s42.chanup();
cout <<"\nAdjusted settings for 42\" TV\n";
s42.chanup();
cout <<"\nAdjusted settings for 42\" TV:\n";
s42.settings();
Remote grey;
grey.set_chan(s42,10);
grey.volup(s42);
grey.volup(s42);
cout <<"\n42\" settings after usinfg remote:\n";
s42.settings();
Tv s58(Tv::ON);
s58.set_mode();
grey.set_chan(s58,28);
cout <<"\n58\" settings:\n";
s58.settings();
return 0;
}