十二章

//

//  main.cpp

//  C++PrimerPlusTwelve

//

//  Created by amengdev on 16/3/29.

//  Copyright © 2016 amengdev. All rights reserved.

//

 

#include "iostream"

using namespace std;

typedef int Item;

class Queue

{

private:

    struct Node{

        Item item;

        struct Node *next;

    };

    enum{SIZE=10};

    Node *front;

    Node *rear;

    int items;

    const int qsize;

public:

    //构造函数和析构函数

    Queue(int size=SIZE);

    ~Queue();

    //普通函数

    bool isEmpty();

    bool isFull();

    int queueCount();

    bool enqueue(const Item & item);

    bool dequeue(Item &item);

    //运算符重载函数

};

 

//构造函数和析构函数

Queue::Queue(int size):qsize(size)

{

    front=rear=nullptr;

    items=0;

}

Queue::~Queue()

{

    

}

//普通函数

bool Queue::isEmpty()

{

    return (items==0);

}

bool Queue::isFull()

{

    return (items==qsize);

}

int Queue::queueCount()

{

    return items;

}

bool Queue::enqueue(const Item & item)

{

    if(isFull())

        return false;

    Node *add=new Node;

    add->item=item;

    add->next=nullptr;

    if(isEmpty())

    {

        front=add;

    }

    else

    {

        rear->next=add;

        rear=add;

    }

    items++;

    return true;

}

bool Queue::dequeue(Item &item)

{

    if(isEmpty())

        return false;

    item=front->item;

    front=front->next;

    items--;

    return true;

}

//运算符重载函数

 

int main()

{

    Queue q1;

    int temp;

    for(int i=0;i<8;i++)

    {

        q1.enqueue(i);

    }

    for(int i=0;i<8;i++)

    {

        cout<<"i:"<<i<<endl;

        q1.dequeue(temp);

        cout<<temp<<endl;

    }

    return 0;

}

 

 

/*

 //习题4:栈模拟

typedef unsigned int Item;

 

class Stack

{

private:

    enum{MAX=10};

    Item *pitem;

    int size;

    int top;

public:

    //构造函数和析构函数

    Stack(int n=MAX);

    Stack(const Stack &st);

    ~Stack();

    //普通函数

    bool isEmpty() const;

    bool isFull() const;

    bool push(const Item & item);

    bool pop(Item & item);

    //运算符重载函数

    Stack & operator=(const Stack & st);

};

 

Stack::Stack(int n)

{

    pitem=new Item[n];

    size=n;

    top=0;

}

 

Stack::Stack(const Stack &st)

{

    pitem=new Item[st.size];

    size=st.size;

    top=st.top;

    for(int i=0;i<top;i++)

    {

        pitem[i]=st.pitem[i];

    }

}

Stack::~Stack()

{

    delete [] pitem;

}

//普通函数

bool Stack::isEmpty() const

{

    if(top==0)

        return true;

    return false;

}

bool Stack::isFull() const

{

    if((top+1)==size)

        return true;

    return false;

}

bool Stack::push(const Item & item)

{

    if(isFull())

        return false;

    pitem[top]=item;

    top++;

    return true;

}

bool Stack::pop(Item & item)

{

    if(isEmpty())

        return false;

    item=pitem[--top];

    return true;

}

 

Stack & Stack::operator=(const Stack & st)

{

    if(this==&st)

        return *this;

    size=st.size;

    top=st.top;

    pitem=new Item[size];

    for(int i=0;i<top;i++)

    {

        pitem[i]=st.pitem[i];

    }

    return *this;

}

int main()

{

    

//    Stack(int n=MAX);

//    Stack(const Stack &st);

//    ~Stack();

//    //普通函数

//    bool isEmpty() const;

//    bool isFull() const;

//    bool push(const Item & item);

//    bool pop(Item & item);

//    //运算符重载函数

//    Stack & operator=(const Stack & st);

    Stack s1;

    Item temp;

    if(s1.isEmpty())

    {

        cout<<"empty"<<endl;

    }

//    s1.push(0);

//    s1.pop(temp);

//    cout<<temp<<endl;

    for(int i=0;i<9;i++)

    {

        s1.push(i);

    }

    if(s1.isFull())

    {

        cout<<"full"<<endl;

    }

    for(int i=0;i<9;i++)

    {

        s1.pop(temp);

        cout<<temp<<endl;

    }

    return 0;

}

*/

 

/*

 //习题1

class Cow{

private:

    char name[20];

    char *hobby;

    double weight;

public:

    Cow();

    Cow(const char *nm,const char * ho,double wt);

    Cow(const Cow &c);

    ~Cow();

    Cow & operator=(const Cow & c);

    void showCow() const;

};

Cow::Cow()

{

    name[0]='\0';

    hobby=nullptr;

    weight=0;

}

Cow::Cow(const char *nm,const char * ho,double wt)

{

    strcpy(name, nm);

    hobby=new char(strlen(ho)+1);

    strcpy(hobby, ho);

    weight=wt;

}

Cow::Cow(const Cow &c)

{

    strcpy(name, c.name);

    hobby=new char(strlen(c.hobby)+1);

    strcpy(hobby, c.hobby);

    weight=c.weight;

}

Cow::~Cow()

{

    delete [] hobby;

}

 

Cow & Cow::operator=(const Cow & c)

{

    if(this==&c)

    {

        return *this;

    }

    strcpy(name, c.name);

    hobby=new char(strlen(c.hobby)+1);

    strcpy(hobby, c.hobby);

    weight=c.weight;

    return *this;

}

void Cow::showCow() const

{

    if(hobby!=nullptr)

    {

        cout<<"name:"<<name<<endl;

        cout<<"hobby:"<<hobby<<endl;

        cout<<"weight:"<<weight<<endl;

    }

    else

    {

        cout<<"NULL"<<endl;

    }

}

 

int main()

{

    

//public:

//    Cow();

//    Cow(const char *nm,const char * ho,double wt);

//    Cow(const Cow &c);

//    ~Cow();

//    Cow & operator=(const Cow & c);

//    void showCow() const;

    Cow c1("name1","hobby1",10.4);

    Cow c2(c1);

    Cow c3;

    c1.showCow();

    c2.showCow();

    c3.showCow();

    c3=c1;

    c3.showCow();

    return 0;

}

*/

 

 

/*

 //自定义String字符串

class String

{

private:

    char *name;

    int len;

public:

    //构造函数和析构函数

    String();

    String(const String & str);

    String(const char *str);

    ~String();

    //普通函数

    //运算符重载函数

        //<,>,==运算符

    bool operator<(const String & str);

    bool operator>(const String & str);

    bool operator==(const String & str);

        //<<,>>运算符

    friend istream& operator>>(istream & is,String & str);

    friend ostream& operator<<(ostream & os,String & str);

        //=,[]运算符

    String & operator=(const String & str);

    char & operator[](int i);

    const char & operator[](int i) const;

    

    //习题2

    String operator+(const String & str);

    void Stringlow();

    void Stringbig();

    int Tongji(char c);

};

 

 

//构造函数和析构函数

String::String()

{

    len=0;

    name=nullptr;

}

String::String(const String & str)

{

    len=str.len;

    name=new char[len+1];

    strcpy(name, str.name);

}

String::String(const char *str)

{

    len=strlen(str);

    name=new char[len+1];

    strcpy(name, str);

}

String::~String()

{

    delete [] name;

}

//普通函数

//运算符重载函数

    //<,>,==运算符

bool String::operator<(const String & str)

{

    return (strcmp(name, str.name)<0);

}

bool String::operator>(const String & str)

{

    return (strcmp(name, str.name)>0);

}

bool String::operator==(const String & str)

{

    return (strcmp(name, str.name)==0);

}

    //<<,>>运算符

istream& operator>>(istream & is,String & str)

{

    char temp[20];

    is>>temp;

    int i=0;

    while (temp[i]!='\n') {

        i++;

    }

    str.name=new char[i+1];

    strcpy(str.name, temp);

    return is;

}

ostream& operator<<(ostream & os,String & str)

{

    os<<str.name;

    return os;

}

    //=,[]运算符

String& String::operator=(const String & str)

{

    if(this==&str)

        return *this;

    delete [] name;

    len=str.len;

    name=new char[len+1];

    strcpy(name, str.name);

    return *this;

}

char & String::operator[](int i)

{

    return name[i];

}

const char & String::operator[](int i) const

{

    return name[i];

}

 

 

//习题2

String String::operator+(const String & str)

{

    String s;

    s.len=len+str.len;

    s.name=new char[s.len+1];

    strcpy(s.name, name);

    strcat(s.name, str.name);

    return s;

}

void String::Stringlow()

{

    for(int i=0;i<len;i++)

    {

        name[i]=tolower(name[i]);

    }

}

void String::Stringbig()

{

    for(int i=0;i<len;i++)

    {

        name[i]=toupper(name[i]);

    }

}

int String::Tongji(char c)

{

    int count=0;

    for(int i=0;i<len;i++)

    {

        if(name[i]==c)

            count++;

    }

    return count;

}

 

int main()

{

    String str1("Str1");

    String str2("Str2");

    String str3=str1+str2;

    cout<<str3<<endl;

    str3.Stringlow();

    cout<<str3<<endl;

    str3.Stringbig();

    cout<<str3<<endl;

    cout<<str3.Tongji('S')<<endl;

    return 0;

}

 

*/

posted on 2016-04-21 09:22  W.C  阅读(142)  评论(0编辑  收藏  举报

导航