大一c++,成语接龙,代码要那种很简单的,满足要求就好,要用类的方法

img


大一c++,成语接龙,代码要那种很简单的,越简单越好,满足要求就好,要用类的方法

可以用这个,和你的要求一样

C++入门编程之成语接龙_叁佰_542586的博客-CSDN博客_成语接龙c++代码 成语接龙问题问题描述:题目描述:成语接龙是中华民族传统的文字游戏。它有着悠久的历史,也有广泛的社会基础,是老少皆宜的民间文化娱乐活动。请设计四字成语接龙游戏。功能需求:(1) 计一个四字成语类Idiom,包含成语内容,首字母的拼音,末尾字母 的拼音。(2) 设计一个成语接龙游戏类Game,系统可以判断用户的回答是否是成语,并且可以按照前一个成语,判断是否接龙成功。如果回答的成语接龙正确,则以新的成语作为新的起点,继续接龙;如果不成功,继续为原有的成语接龙。(3) 设计提示功能,引导用户 https://blog.csdn.net/csq542586/article/details/116329753

使用外部文件储存,创建一个.txt文件来储存成语,组成成语数据库,用fstream类导入成语库。
分别用两个ifstream,声明出两个变量,分别储存成语与拼音,放在Idiom类里。
再定义出一个Game类,声明出6个函数。
 matchIfidiom函数,用来匹配是否为成语。
 matchIfcatch函数,用来检测是否接龙。
 matchIfexist函数,用来检测是否在游戏里存在出现过的成语。
 pointOut函数,用来做提示功能。
 printOut函数,用来输出结果。
 gameStart函数,用来启动游戏。
 String exist成语,用来储存出现过的成语
在gameStart函数里,根据玩家输入1或2来决定是否退出或者提示。 这个项目中最关键的一点就是,第一次输入的成语必须是一个四字成语,否则直接退出。于是,提示功能只能在第二轮输入开始才能使用。在每次输入成语后,printOut函数都会检测是否为四字成语,是否存在出现的成语(当第二次输入时)。当用户需要提示时,输入“2”,printOut函数将输进来的idiom又赋值为上一个成语(因为“2”不是成语,这也是需要第二次输入才能使用提示功能的原因)。gameStart函数里声明出一个变量idiom,用来给用户输入成语,然后传入printOut函数输入结果。由于需要判断是否接龙,于是在函数里面加入判断语句if(),把idiom放入matchIfcatch函数里,如果不接龙,则实现if语句里面的内容。

#include<iostream>
#include<cstring>
#include<fstream>
using namespace std; 
int tag2;

class Idiom{
    public:
    ifstream a;
    ifstream b;
    string allidiom;
    
        Idiom()
        {
            a.open("1.txt");
            b.open("1.txt");
            while(getline(a,idiom1))
            {
                allidiom+=idiom1.substr(0,8);//储存每行的成语
            }
            
            while(getline(b,idiom2,'#'))
            {
                pos1=idiom2.find(":")+2;
                pinyin+=idiom2.substr(pos1);//储存每行的拼音 
            }
            a.close();
            b.close(); 
        }
        
    private:
        string idiom1;
        string idiom2;
        string pinyin; 
        int pos1;
};

class Game:public Idiom{
    public:
        bool matchIfidiom(string,string);//检测是否为四字成语 
        bool matchIfcatch(string,string);//检测是否接龙 
        void gameStart();//开始游戏
        string pointOut(string);//提示功能 
        void printOut(string); //输出是否为四字成语
        bool matchIfexist(string);//检测是否出现同一个成语 
        
        string exist;
};

bool Game::matchIfidiom(string idiom,string eachidiom)//检测是否为四字成语 
{
    if(eachidiom.compare(idiom)==0 )
        return true;
    return false;
}

bool Game::matchIfcatch(string lastidiom,string idiom)//检测是否接龙 
{
    string first_i=idiom.substr(0,2);
    string end_i=lastidiom.substr(6,8);
    if(first_i.compare(end_i)==0)
        return true;
    else 
        return false;
}

bool Game::matchIfexist(string idiom)//检测是否出现同一个成语
{
    int pos=exist.find(idiom);
    if(pos!=string::npos)
        return true;
    return false;
}

void Game::gameStart()//开始游戏 
{
    string idiom;//玩家成语 
    string lastidiom;//储存上一次的成语(从第二个成语开始)
    int tag1=0;//标记从第二个开始 
    cout<<"游戏开始,请输入四字成语。途中若想退出游戏,输入1即可;若想要提示,则输入2即可。"<<endl;
        
    do{
        cin>>idiom;//用户输入成语 
        tag1++;//标记--从第二个成语开始储存 
            
        if(idiom.compare("1")==0) //退出游戏 
        {
            break;
        } 
        
        if(idiom.compare("2")==0)//提示功能 
        {
            idiom=lastidiom;
            string end_in=idiom.substr(6,8);//截取最后的字符 
            
            idiom=pointOut(end_in); 
            
            if(idiom.compare("Endgame")!=0 )
                cout<<idiom<<endl;    
            else
                {
                    cout<<"接龙完毕。";
                    break;
                }
                
        }
        
        
        printOut(idiom);
        
        
            if(tag1>=2&&!matchIfcatch(lastidiom,idiom))
            {
                idiom=lastidiom;//若接龙失败,则为原来的成语接龙 
                cout<<"接龙失败,以上一个的成语继续"<<endl; 
            }
            
         exist.append(idiom);
         
        if(tag2)
        lastidiom=idiom;
    }while(1);
}

void Game::printOut(string idiom)//输出是否为四字成语 
{
    int tag=0; //标记找得到成语 
    ifstream eachIdiom;
    string eachidiom;//每行的单个成语 
    string idiom1;//缓存的整行成语 
    eachIdiom.open("1.txt");
    while(getline(eachIdiom,idiom1)) 
    {
        eachidiom=idiom1.substr(0,8); 
        if(matchIfidiom(idiom,eachidiom)&&!matchIfexist(idiom))
            {
                cout<<"||"<<endl;
                tag=1;
                tag2=1;
                break;
            }
                continue;
    }
                
            if(tag!=1)
            {
                cout<<"非成语或出现重复,请重新输入。"<<endl; 
                tag2=0;
            }
                
    
            eachIdiom.close();
} 
   
string Game::pointOut(string end_in)//提示功能
{
    ifstream eachIdiom1;
    string idiom1;
    string eachidiom; 
    string eachidiom_first;
    int tag=0;
    string Pidiom;//返回的提示成语 
    eachIdiom1.open("1.txt");
    while(getline(eachIdiom1,idiom1)) 
    {
        eachidiom=idiom1.substr(0,8); 
        eachidiom_first=eachidiom.substr(0,2);//首字 
        if(end_in.compare(eachidiom_first)==0)
        {
            Pidiom=eachidiom;
            if(matchIfexist(Pidiom))
                continue;
            tag=1;
            break;
        }
         
    }
        eachIdiom1.close();
        if(tag==1)
            return Pidiom;
        return "Endgame";
}

int main()
{
    Game a;
    a.gameStart();        
    system("pause");
    return 0;
}