|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 KrAu 于 2020-4-19 21:52 编辑
代码如下
#include<iostream>
#include<fstream>
using namespace std;
struct letter{
char a[9];
int b;
};
int main()
{
char x[9];
letter n;
int c=0,end=1;
ifstream ip;
while(end){
cout<<"请输入成语:"<<endl;
cin>>x;
ip.open("dataa.txt",ios::in);
ip>>n.a>>n.b;
while(n.b==0){
if(n.a[0]==x[6]&&n.a[1]==x[7]) //首尾相等
{
cout<<n.a<<endl;
n.b=0;
break;
}
ip>>n.a>>n.b;
}
if(n.b==1){
cout<<"you win (1) countinue (0) end"<<endl;
cin>>end;
}
}
ip.close();
return 0;
}
我这只能玩一次成语接龙,第二次就不能再玩了,而且还会有很高的cpu负载。
请各位指教哪里可以改进!
dataa.txt我会在下面放出
本帖最后由 superbe 于 2020-4-21 16:41 编辑
修改了下,基本能满足要求了。char数组改成了string,这样就不担心数组越界了,还可以使用不是四个字的成语。
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- struct letter {
- string a;
- int b;
- };
- int main()
- {
- string x;
- letter n;
- int turn = 0;
- ifstream ip;
- ip.open("dataa.txt", ios::in);
- cout << "请输入成语: " << endl;
- cin >> x;
- while (1) {
- if (turn == 0) {
- ip >> n.a >> n.b;
- while (n.a.substr(0, 2).compare(x.substr(x.size() - 2, 2))) {
- if (n.b == 1) {
- cout << "你赢了!" << endl;
- return 0;
- }
- else
- ip >> n.a >> n.b;
- }
- cout << n.a << " 请接龙(q-认输):" << endl;
- turn = 1;
- }
- else {
- cin >> x;
- while (x.substr(0, 2).compare(n.a.substr(n.a.size() - 2, 2))) {
- if (x[0] == 'q') {
- cout << "你输了!" << endl;
- return 0;
- }
- else {
- cout << "不符合要求, 请重新输入!" << endl;
- cin >> x;
- }
- }
- turn = 0;
- ip.seekg(0, ios::beg);
- }
- }
- ip.close();
- return 0;
- }
复制代码
|
|