新概念2 介词搭配练习程序

· · 学习·文化课

由于作者对介词搭配一窍不通,故写此程序辅助练习。

比较容易魔改,拓展性(应该?)比较强。 像依托答辩

数据库

1.空格使用/表示,如果真的需要显示/,请使用//;

2.保存名称和代码第 26 & 134 行 fopen 里的名称一致,和代码放同一个文件夹即可;

3.建议备份;

4.目前只有 Lesson 22 的内容

10
{"of","accuse","approve","assure","beware","boast","complain","consist","convince","cure","despair","dream","expect","hear","get/rid","smell","suspect","think","tire//be/tired","warn","demand"}END
{"from","borrow","defend","demand","differ","dismiss","draw","emerge","escape","excuse","hinder","prevent","prohibit","protect","receive","separate","suffer","hear","expect","withdraw"}END
{"in","believe","delight","employ","encourage","engage","experience","fail","help","include","indulge","instruct","interest","invest","involve","persist","perform","share"}END
{"on","act","base","call","comment","concentrate","congratulate","consult","count","decide","depend","economize","embark","experiment","insist","lean","live","operate","perform","pride","rely","vote","write"}END
{"to"}END
{"at"}END
{"for","excuse","vote"}END
{"with","help"}END
{"about","boast","complain","convince","dream","think","consult","write"}END
{"against","warn","defend","protect","lean"}END

{"KeyWords"}END

代码:

Windows 版

//Lesson 22/46/70
#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
using namespace std;
string t[20];
map<string,set<string>>mp;
vector<string>mwds[20];
set<string>wds,kwds;
int readi(FILE* file){
    char c;
    while(!isdigit(c=getc(file)));
    int res=c^48;
    while(isdigit(c=getc(file)))res=(res<<3)+(res<<1)+(c^48);
    return res;
}
string Trans(const string &x){
    string res;
    for(int i=0;i<x.size();i++){
        if(x[i]!='/')res+=x[i];
        else if(i==x.size()-1 || x[i+1]!='/')res+=' ';
        else i++,res+='/';
    }
    return res;
}
bool checkinput(char c){return isalpha(c) || c=='/' || isdigit(c);}
string read(FILE* file){
    char c;
    while(!checkinput(c=getc(file)));
    string s;
    s+=c;
    while(checkinput(c=getc(file)))s+=c;
    return s;
}
void ScreenPrint(const string &s){
    for(char c:s)putchar(c);
}
void print(const string &s,FILE* file){
    for(int i=0;i<s.size();i++){
        if(s[i]==' ')fputc('/',file);
        else if(s[i]=='/')fprintf(file,"//");
        else fputc(s[i],file);
    }
}


void Prepare(){
    puts("Preparing...");
    auto file=fopen("介词搭配数据库.txt","r");
    int n=readi(file);
    for(int i=0;i<n;i++){
        t[i]=read(file);
        string s;
        while((s=read(file))!="END"){
            s=Trans(s);
            if(!mp.count(s))wds.insert(s);
            mwds[i].push_back(s),mp[s].insert(t[i]);
        }
    }
    string s=read(file);
    while((s=read(file))!="END"){
        s=Trans(s);
        if(!kwds.count(s))kwds.insert(s);
    }
    fclose(file);
    system("cls");
}


bool checkword(string s,int &tot,int &Correct,int &Wrong,bool b){
    system("cls");
    printf("已测试 %d 个,正确 %d 个,错误 %d 个,正确率为 %.1lf\%\n",tot,Correct,Wrong,!tot?100:100.0*Correct/tot);
    int sz=mp[s].size();
    ScreenPrint(s),printf("(共有 %d 种搭配)\n输入exit退出\n\n",sz);
    set<string>userin;
    while(sz--){
        string g=read(stdin);
        if(g=="exit")return 1;
        userin.insert(g);
    }
    tot++;
    if(userin==mp[s]){
        puts("Correct"),Correct++;
        if(b){
            puts("是否移出易错词单?(y/n)");
            puts("注意:从下轮开始生效,本轮仍有可能出现");
            if(read(stdin)=="y")kwds.erase(s); 
        }
        Sleep(1000);
    }
    else{
        Wrong++;
        printf("Wrong\nThe answer(s):");
        for(string g:mp[s])ScreenPrint(g),putchar(' ');
        puts("");
        if(!b){
            puts("是否加入易错词单?(y/n)");
            if(read(stdin)=="y")kwds.insert(s);
        }
        Sleep(1000);
    }
    return 0;
}
void Test(const set<string>&st,bool b){
    system("cls");
    if(st.empty()){
        puts("本词库为空!");
        Sleep(1000);
        return ;
    }
    vector<string>v;
    for(auto s:st)v.push_back(s);
    random_shuffle(v.begin(),v.end());
    puts("1:无限模式\n2:随机排列模式(不会考到重复的)\ne:退出");
    string x=read(stdin);
    BEGIN:;
    if(x=="1"){
        int tot=0,Correct=0,Wrong=0;
        while(1){
            int x=rand()%v.size();
            if(checkword(v[x],tot,Correct,Wrong,b))break;
        }
    }
    else if(x=="2"){
        int tot=0,Correct=0,Wrong=0;
        for(int i=0;i<v.size();i++)if(checkword(v[i],tot,Correct,Wrong,b))break;
    }
    else if(x=="e")return ;
    else goto BEGIN;
}


void Back(){
    auto file=fopen("介词搭配数据库.txt","w");
    int cnt=0;
    for(int i=0;!t[i].empty();i++)cnt++;
    fprintf(file,"%d\n",cnt);
    for(int i=0;i<cnt;i++){
        fprintf(file,"{\"");
        print(t[i],file);
        fputc('\"',file);
        for(auto s:mwds[i])fprintf(file,",\""),print(s,file),fputc('\"',file);
        fprintf(file,"}END\n");
    }
    fprintf(file,"\n\n");
    fprintf(file,"{\"KeyWords\"");
    for(auto s:kwds)fprintf(file,",\""),print(s,file),fputc('\"',file);
    fprintf(file,"}END");
    fclose(file);
}


void mian(){
    while(1){
        system("cls");
        puts("\
**************************\n\
***欢迎来到介词搭配练习***\n\
**************************\n\n\n");
        puts("请输入字符选择模式:");
        puts("1: 所有单词随机测试");
        puts("2: 关键(易错)词随机测试");
        puts("3: 列出所有易错词");
        puts("e: 退出");
        string x=read(stdin);
        if(x=="1")Test(wds,0);
        else if(x=="2")Test(kwds,1);
        else if(x=="3"){
            system("cls");
            for(auto s:kwds){
                ScreenPrint(s),puts(":");
                for(auto g:mp[s])ScreenPrint(g),puts("");
                puts("");
            }
            system("pause");
        }
        else if(x=="e")break;
        Back();
    }
    system("cls");
}
int main(){
    srand(time(0));
    Prepare();//准备工作 
    mian();//主程序
    Back();//更新数据库 
}

Linux 版 未完成(文件路径最好设成英文,不然某些编译器编译不了)

//Lesson 22/46/70
#include<bits/stdc++.h>
using namespace std;
string t[20];
map<string,set<string>>mp;
set<string>wds,kwds;
int readi(FILE* file){
    char c;
    while(!isdigit(c=getc(file)));
    int res=c^48;
    while(isdigit(c=getc(file)))res=(res<<3)+(res<<1)+(c^48);
    return res;
}
string Trans(const string &x){
    string res;
    for(int i=0;i<x.size();i++){
        if(x[i]!='/')res+=x[i];
        else if(i==x.size()-1 || x[i+1]!='/')res+=' ';
        else i++,res+='/';
    }
    return res;
}
bool checkinput(char c){return isalpha(c) || c=='/' || isdigit(c);}
string read(FILE* file){
    char c;
    while(!checkinput(c=getc(file)));
    string s;
    s+=c;
    while(checkinput(c=getc(file)))s+=c;
    return Trans(s);
}
void Prepare(){
    puts("Preparing...");
    srand(time(0));
    auto file=fopen("preps.txt","r");
    int n=readi(file);
    for(int i=0;i<n;i++){
        t[i]=read(file);
        string s;
        while((s=read(file))!="END"){
            if(!mp.count(s))wds.insert(s);
            mp[s].insert(t[i]);
        }
    }
    string s=read(file);
    while((s=read(file))!="END"){
        if(!kwds.count(s))kwds.insert(s);
    }
    fclose(file);
    system("clear");
}
void Test(const set<string>&st){
    system("clear");
    vector<string>v;
    for(auto s:st)v.push_back(s);
    random_shuffle(v.begin(),v.end());
}
void Back(){}
void mian(){
    while(1){
        system("clear");
        puts("\
**************************\n\
***欢迎来到介词搭配练习***\n\
**************************\n\n\n");
        puts("请输入字符选择模式:");
        puts("1: 所有单词随机测试");
        puts("2: 关键(易错)词随机测试");
        puts("e: 退出");
        string x=read(stdin);
        if(x=="1")Test(wds);
        else if(x=="2")Test(kwds);
        else if(x=="e")break;
        Back();
    }
    system("clear");
}
int main(){
    Prepare();//准备工作
    mian();//主程序
    Back();//更新数据库
}