標籤:

C++上機作業

#include<iostream>

#include<vector>

using namespace std;

template<typename T>

void sort(T a[], int n){

int i, j, k;

for (i = 1; i < n; i++)

{

for (j = i - 1; j >= 0; j--)

if (a[j] < a[i])

break;

if (j != i - 1)

{

int temp = a[i];

for (k = i - 1; k > j; k--)

a[k + 1] = a[k];

a[k + 1] = temp;

}

}

}

template<typename T>

int length(T *a){

int i = 0;

while(*( a + i ))

{

i++;

}

return i;

}

int main(){

int array[4] = {1 , 8 , 33 , 4};

sort(array , length(array));

for(int i = 0; i < 4;i++){

cout<<array[i]<<endl;

}

return 0;

}

#include<vector>

#include<string>

#include<iostream>

using namespace std;

template <typename T>

class Stack{

private:

vector<T> elems;

public:

void push(T const&);

void pop();

T top() const;

bool empty() const{

return elems.empty();

}

};

template <typename T>

void Stack<T>::push(T const & elem){

elems.push_back(elem);

}

template <typename T>

void Stack<T>::pop(){

elems.pop_back();

}

template <typename T>

T Stack<T>::top() const{

if(elems.empty())

{

throw "woc";

}

else{

return elems.back();

}

}

int main(){

try{

Stack<int> intStack;

Stack<string> StringStack;

intStack.push(7);

cout<<intStack.top()<<endl;

StringStack.push("wowowowow");

cout<<StringStack.top()<<endl;

}

catch(std::exception const& ex){

cerr << ex.what() << endl;

return 1;

}

return 0;

}

#include<iostream>

#include<string>

using namespace std;

class node{

public:

node*next;

node*front;

int x;

friend class list;

};

class list{

private:

node *head;

node*tail;

node*temp;

public:

void add(int);

list();

~list();

void show();

};

class set :public list{

private:

node *head, *tail, *temp;

int len;

public:

void add(int);

void remove(int);

node*find(int);

void show();

set::set();

set::~set();

int getnum();

};

int set::getnum(){

return len;

}

void set::show(){

node *t = head;

while (t){

cout << t->x << " ";

t = t->next;

}

cout << endl;

}

set::set(){

head = tail = temp = NULL;

len = 0;

}

set::~set(){

while (head){

node*t = head;

t = t->next;

delete head;

head = t;

}

}

void set::add(int x){

temp = new node;

temp->x = x;

temp->front = NULL;

temp->next = NULL;

if (!head){

head = tail = temp;

len = 1;

}

else{

node *t = head;

len++;

while (t){

if (x > tail->x){

tail->next = temp;

temp->front = tail;

tail = temp;

break;

}

if (t != head)

{

if (x < t->x){

if (x == t->front->x){

len--;

break;

}

temp->next = t;

t->front->next = temp;

t->front = temp;

break;

}

}

else{

if (x < t->x){

temp->next = t;

t->front = temp;

head = temp;

break;

}

}

t = t->next;

}

}

}

list::list(){

head = tail = temp = NULL;

}

list::~list(){

while (head){

node*t = head;

t = t->next;

delete head;

head = t;

}

}

void list::add(int x){

temp = new node;

temp->x = x;

temp->next = NULL;

if (tail == NULL){

head = tail = temp;

}

else{

tail->next = temp;

tail = temp;

}

}

void list::show(){

node *t = head;

while (t){

cout << t->x << " ";

t = t->next;

}

cout << endl;

}

void set::remove(int x){

node*t = head;

bool flag = false;

while (t){

if (t->x == x){

flag = true;

len--;

if (t == head){

t = head->next;

delete head;

head = t;

break;

}

else{

t->front->next = t->next;

delete t;

break;

}

}

t = t->next;

}

if (!flag)cout << "mistake" << endl;

}

node* set::find(int x){

node*t = head;

bool flag = false;

while (t){

if (t->x == x){

flag = true;

return t;

}

t = t->next;

}

if (!flag)cout << "mistake" << endl;

return NULL;

}

int main(){

list lis;

lis.add(1);

lis.add(2);

lis.add(3);

lis.show();

set se;

for (int i = 0; i < 5; i++){

se.add(i);

}

se.add(3);

se.show();

cout << se.getnum() << endl;

se.remove(3);

se.show();

node *t = se.find(2);

cout << t->x << endl;

cout << se.getnum() << endl;

}

#include<iostream>

#include<string>

using namespace std;

class vehicle{

public:

virtual void show(){

cout << "vehicle" << endl;

}

virtual ~vehicle(){}

};

class car :public vehicle{

public:

void show();

};

void car::show(){

cout << "car " << endl;

}

class truck :public vehicle{

public:

void show();

};

void truck::show(){

cout << "truck" << endl;

}

class boat :public vehicle{

public:

void show();

};

void boat :: show(){

cout << "boat" << endl;

}

int main(){

vehicle v;

car c;

truck t;

boat b;

vehicle *p[4];

p[0] = &v;

p[1] = &c;

p[2] = &t;

p[3] = &b;

for (int i = 0; i < 4; i++){

p[i]->show();

}

}

#include<iostream>

#include<cmath>

#define pi acos(-1.0)

using namespace std;

class shape{

public:

virtual double getarea() = 0;

virtual void show(){};

virtual void showname(){};

};

class twoshape:public shape{

public:

void show(){

cout << getarea() << endl;

}

};

class threeshape:public shape{

public:

virtual double getvolum() = 0;

void show(){

cout << getarea() << endl << getvolum() << endl;

}

};

class square :public twoshape{

private:

double size;

public:

square(double sizes) :size(sizes){}

double getarea(){

return size*size;

}

void showname(){ cout << "square" << endl; }

};

class triangle :public twoshape{

private:

double a, b, c;

public:

triangle(double d, double e, double f) :a(d), b(e), c(f){}

double getarea(){

double s = (a + b + c) / 2;

return s*(s - a)*(s - b)*(s - c);

}

void showname(){

cout << "truangle" << endl;

}

};

class circle :public twoshape{

private:

double r;

public:

circle(double rr) :r(rr){}

double getarea(){

return pi*r*r;

}

void showname(){

cout << "circle" << endl;

}

};

class sphere :public threeshape{

private:

double r;

public:

sphere(double rr) :r(rr){}

double getarea(){

return pi * 4 * r*r;

}

double getvolum(){

return pi * 4 / 3 * r*r*r;

}

void showname(){

cout << "sphere" << endl;

}

};

class cube :public threeshape{

private:

double a, b, c;

public:

cube(double d, double e, double f) :a(d), b(e), c(f){}

double getarea(){

return 2 * (a*b + b*c + c*a);

}

double getvolum(){

return a*b*c;

}

void showname(){

cout << "cube" << endl;

}

};

int main(){

shape*arr[5];

square squ(4);

triangle tri(3, 4, 5);

circle ci(4);

sphere sp(4);

cube cub(3, 4, 5);

arr[0] = &squ;

arr[1] = &tri;

arr[2] = &ci;

arr[3] = &sp;

arr[4] = &cub;

for (int i = 0; i < 5; i++){

arr[i]->showname();

arr[i]->show();

}

}


推薦閱讀:

惟江上之清風,與山間錕斤銬。
Python3 基礎知識整理
[數據結構]表達式樹——手動eval()
網路工程師的Python學習筆記+乾貨
編程中所講的「思維深度」的本質是什麼?

TAG:作業 | 編程 |