2024.11.20(周三)

在“虚拟聊天室”实例中增加一个新的具体聊天室类和一个新的具体会员类,要求如下:

1. 新的具体聊天室中发送的图片大小不得超过20M。

2. 新的具体聊天室中发送的文字长度不得超过100个字符。

3. 新的具体会员类可以发送图片信息和文本信息。

4. 新的具体会员类在发送文本信息时,可以在信息后加上时间。

修改客户端测试类,注意原有系统类代码和客户端代码的修改。

实验要求:

1.     画出对应的类图;

2.     提交源代码;

3. 注意编程规范。

 

 

1、类图、

 

2、源代码

#include<iostream>

#include<map>

#include<ctime>

#include<sstream>

using namespace std;

class AbstractChatroom;

string m_replace(string strSrc,const string &oldStr, const string &newStr,int count=-1)

{

    string strRet=strSrc;

    size_t pos = 0;

    int l_count=0;

    if(-1 == count) //替换所有

        count = strRet.size();

    while ((pos = strRet.find(oldStr, pos)) != string::npos)

    {

        strRet.replace(pos, oldStr.size(), newStr);

        if(++l_count >= count) break;

        pos += newStr.size();

    }

    return strRet;

}

class Member {

protected :

    AbstractChatroom *chatroom;

    string name;

public:

    Member(string name) {

        this->name=name;

    }

    AbstractChatroom* getChatroom() {

        return chatroom;

    }

    void setChatroom(AbstractChatroom *chatroom) {

        this->chatroom = chatroom;

    }

    string getName() {

        return name;

    }

    void setName(string name) {

        this->name = name;

    }

    virtual void sendText(string to,string message)=0;

    virtual void sendImage(string to,string image)=0;

    void receiveText(string from,string message) {

        cout<<from<<"发送文本给"<<this->name<<",内容为:"<<message<<endl;

    }

    void receiveImage(string from,string image) {

        cout<<from<<"发送图片给"<<this->name<<",内容为:"<<image<<endl;

    }

};

class AbstractChatroom {

public:

    virtual void registers(Member *member)=0;

    virtual void sendText(string from,string to,string message)=0;

    virtual void sendImage(string from,string to,string image)=0;

};

class ChatGroup: public AbstractChatroom{

private:

    map<string, Member*> members;

    map<string, Member*>::iterator iter;

public:

    void registers(Member *member) {

        iter=members.find(member->getName());

        if(iter==members.end()) {

            members.insert(map<string, Member*>::value_type(member->getName(),member));

            member->setChatroom(this);

        }

    }

    void sendText(string from, string to, string message) {

        Member *member=members.find(to)->second;

        string newMessage=message;

        newMessage=m_replace(message,"日", "*");

        member->receiveText(from, newMessage);

    }

    void sendImage(string from, string to, string image) {

        Member *member= members.find(to)->second;

        if(image.length()>5) {

           cout<<"图片太大,发送失败"<<endl;

        }

        else {

            member->receiveImage(from, image);

        }

    }

};

class NewChat:public AbstractChatroom{

private:

    map<string, Member*> members;

    map<string, Member*>::iterator iter;

public:

    void registers(Member *member) {

       iter=members.find(member->getName());

        if(iter==members.end()) {

            members.insert(map<string, Member*>::value_type(member->getName(),member));

            member->setChatroom(this);

        }

    }

    void sendText(string from, string to, string message) {

        Member *member=members.find(to)->second;

        string newMessage=message;

        newMessage=m_replace(message,"日", "*");

        if(message.length()>100) {

            cout<<"发送的文字长度不得超过100个字符,发送失败"<<endl;

        }

        else {

            member->receiveText(from, newMessage);

        }

    }

    void sendImage(string from, string to, string image) {

        Member *member=members.find(to)->second;

        if(image.length()>20) {

            cout<<"图片大小不得超过20M,发送失败"<<endl;

        }

        else {

            member->receiveImage(from, image);

        }

    }

};

class CommonMember :public Member{

protected :

    string name;

public:

    CommonMember(string name):Member(name) {

        this->name=name;

    }

    void sendText(string to, string message) {

        cout<<"普通会员发送消息:"<<endl;

        chatroom->sendText(this->name, to, message);

    }

 

    void sendImage(string to, string image) {

        cout<<"普通会员不能发送图片!"<<endl;

    }

 

};

class DiamondMember :public Member{

protected :

    string name;

public:

    DiamondMember(string name):Member(name){

        this->name=name;

        }

    void sendText(string to, string message) {

        cout<<"钻石会员发送消息:"<<endl;

        chatroom->sendText(name,to, message);

    }

    void sendImage(string to, string image) {

        cout<<"钻石会员发送图片:"<<endl;

        chatroom->sendImage(name, to, image);

    }

};

class CrownMember :public Member{

protected :

    string name;

public:

    CrownMember(string name):Member(name){

        this->name=name;

    }

    void sendText(string to, string message) {

        time_t now = time(NULL);

        tm* t = localtime(&now);

        // 将信息输出到字符串流

        stringstream ss;

        stringstream ss2;

        ss << t->tm_year + 1900 << "." << t->tm_mon + 1 << "." <<

            t->tm_mday << ".." << t->tm_hour << "." << t->tm_min << "." << t->tm_sec;

        cout<<"皇冠会员发送消息:"<<endl;

        chatroom->sendText(name,to,message+"  现在时间"+ss.str());

    }

    void sendImage(string to, string image) {

        cout<<"皇冠会员发送图片:"<<endl;

        chatroom->sendImage(name, to, image);

    }

};

int main(){

        //AbstractChatroom *happyChat=new ChatGroup();

        AbstractChatroom *newChat=new NewChat();

 

        stringstream ss;//生成100个以上的字符

 

        Member *member1,*member2,*member3,*member4,*member5,*member6;

        member1=new DiamondMember("张三");

        member2=new DiamondMember("李四");

        member3=new CommonMember("王五");

        member4=new CommonMember("小芳");

        member5=new CommonMember("小红");

        member6=new CrownMember("雨轩");

 

        newChat->registers(member1);

        newChat->registers(member2);

        newChat->registers(member3);

        newChat->registers(member4);

        newChat->registers(member5);

        newChat->registers(member6);

 

 

        member1->sendText("李四","李四,你好!");

        member2->sendText("张三","张三,你好!");

        member1->sendText("李四","今天天气不错,有日!");

        member2->sendImage("张三","一个很大很大的太阳");

        member2->sendImage("张三","太阳");

        member3->sendText("小芳","还有问题吗?");

        member3->sendText("小红","还有问题吗?");

        member4->sendText("王五","没有了,谢谢!");

        member5->sendText("王五","我也没有了!");

        member5->sendImage("王五","谢谢");

        member6->sendText("王五","今天老师的作业怎么没有完成");

        member6->sendImage("王五", "..................................................");

        member6->sendText("王五", "今天老师的作业怎么没有完成,你知道老师有多么的容易吗,不知道体谅一下吗");

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

            ss <<"-";

        }

        member6->sendText("王五",ss.str());

}

posted @ 2024-11-25 08:52  记得关月亮  阅读(4)  评论(0编辑  收藏  举报