淼52 2024-12-01 16:15 采纳率: 100%
浏览 74
已结题

找佬协助我完成Cpp课题(大一)

img


找佬帮我完成这个问题,自己想用可视化界面,但是学校没有讲,对easyx是0基础,想找个佬教教我可视化这方面的知识

  • 写回答

30条回答 默认 最新

  • Minuw 2024-12-02 08:48
    关注

    参考gpt
    为了创建一个简单的英语单词学习系统,我们可以使用C++和EasyX库。这个系统将包括一个词典,支持单词的增加、删除、修改和查找等功能,同时实现一个包括答题游戏的功能。下面是这个项目的基本设计思路及示例代码。

    设计思路

    1. 类设计

      • Word类:表示一个单词的结构,包括单词的英文、中文含义。
      • Dictionary类:管理词典,提供添加、删除、修改、查找等功能。
      • User类:表示用户信息,包括用户名和得分。
      • Game类:管理答题游戏的逻辑和进行。
    2. 文件操作

      • 使用文件来持久保存词典中的单词信息。
    3. 菜单系统

      • 提供用户界面,允许用户选择不同的功能。
    4. 答题游戏

      • 随机选择单词提问,记录得分,最后显示排行榜。

    示例代码

    以下是项目的一部分示例代码,展示了基本结构和实现。

    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    #include <algorithm>
    #include <random>
    #include <iomanip>
    #include <conio.h>
    #include "graphics.h"  // EasyX图形库头文件
    
    using namespace std;
    
    // 单词类
    class Word {
    public:
        string english;
        string chinese;
    
        Word(string en, string ch) : english(en), chinese(ch) {}
    };
    
    // 字典类
    class Dictionary {
    private:
        vector<Word> words;
    
    public:
        Dictionary() {
            loadFromFile();
        }
    
        void loadFromFile() {
            ifstream inFile("dictionary.txt");
            string en, ch;
            while (inFile >> en >> ch) {
                words.push_back(Word(en, ch));
            }
            inFile.close();
        }
    
        void saveToFile() {
            ofstream outFile("dictionary.txt");
            for (const auto &word : words) {
                outFile << word.english << " " << word.chinese << endl;
            }
            outFile.close();
        }
    
        void addWord(string en, string ch) {
            words.push_back(Word(en, ch));
            saveToFile();
        }
    
        void deleteWord(string en) {
            words.erase(remove_if(words.begin(), words.end(), [&](Word& w) { return w.english == en; }), words.end());
            saveToFile();
        }
    
        bool modifyWord(string en, string newCh) {
            for (auto &word : words) {
                if (word.english == en) {
                    word.chinese = newCh;
                    saveToFile();
                    return true;
                }
            }
            return false;
        }
    
        string findWord(string en) {
            for (const auto &word : words) {
                if (word.english == en) {
                    return word.chinese;
                }
            }
            return "Not found";
        }
    
        void displayWords() {
            for (const auto &word : words) {
                cout << word.english << " - " << word.chinese << endl;
            }
        }
    };
    
    // 用户类
    class User {
    public:
        string username;
        int score;
    
        User(string name) : username(name), score(0) {}
    };
    
    // 游戏类
    class Game {
    private:
        Dictionary &dictionary;
        vector<User> leaderboard;
    
    public:
        Game(Dictionary &dict) : dictionary(dict) {}
    
        void startGame(string username, int questionCount) {
            User user(username);
            vector<string> questions;
    
            // 随机选择单词作为问题
            for (int i = 0; i < questionCount; i++) {
                int index = rand() % dictionary.words.size();
                questions.push_back(dictionary.words[index].english);
            }
    
            // 提问
            for (const auto &word : questions) {
                cout << "Translate: " << word << endl;
                string answer;
                cin >> answer;
    
                if (dictionary.findWord(word) == answer) {
                    user.score++;
                    cout << "Correct!" << endl;
                } else {
                    cout << "Wrong! Correct answer is: " << dictionary.findWord(word) << endl;
                }
            }
    
            // 显示得分
            cout << "Game Over! Score: " << user.score << endl;
            leaderboard.push_back(user);
        }
    
        void displayLeaderboard() {
            cout << "Leaderboard:" << endl;
            sort(leaderboard.begin(), leaderboard.end(), [](User &a, User &b) { return a.score > b.score; });
            for (int i = 0; i < min(10, (int)leaderboard.size()); i++) {
                cout << i + 1 << ". " << leaderboard[i].username << ": " << leaderboard[i].score << endl;
            }
        }
    };
    
    // 主菜单
    void showMenu(Dictionary &dictionary, Game &game) {
        while (true) {
            cout << "1. Add Word" << endl;
            cout << "2. Delete Word" << endl;
            cout << "3. Modify Word" << endl;
            cout << "4. Find Word" << endl;
            cout << "5. Display Words" << endl;
            cout << "6. Start Game" << endl;
            cout << "7. Show Leaderboard" << endl;
            cout << "8. Exit" << endl;
            int choice;
            cin >> choice;
    
            if (choice == 1) {
                string en, ch;
                cout << "Enter English Word and Chinese Meaning: ";
                cin >> en >> ch;
                dictionary.addWord(en, ch);
            } else if (choice == 2) {
                string en;
                cout << "Enter English Word to Delete: ";
                cin >> en;
                dictionary.deleteWord(en);
            } else if (choice == 3) {
                string en, newCh;
                cout << "Enter English Word and New Chinese Meaning: ";
                cin >> en >> newCh;
                dictionary.modifyWord(en, newCh);
            } else if (choice == 4) {
                string en;
                cout << "Enter English Word to Find: ";
                cin >> en;
                cout << dictionary.findWord(en) << endl;
            } else if (choice == 5) {
                dictionary.displayWords();
            } else if (choice == 6) {
                string username;
                int questionCount;
                cout << "Enter your username: ";
                cin >> username;
                cout << "Enter number of questions: ";
                cin >> questionCount;
                game.startGame(username, questionCount);
            } else if (choice == 7) {
                game.displayLeaderboard();
            } else if (choice == 8) {
                break;
            } else {
                cout << "Invalid selection! Try again." << endl;
            }
        }
    }
    
    int main() {
        Dictionary dictionary;
        Game game(dictionary);
        showMenu(dictionary, game);
        return 0;
    }
    

    说明

    1. 字典功能: Dictionary类提供单词的管理,包括文件的读写操作。
    2. 游戏功能: Game类负责游戏逻辑。用户可以输入用户名和希望答题的数量。
    3. 菜单交互: 在showMenu函数中实现了一个简单的文本菜单,允许用户选择不同的功能。

    注意

    • 你需确保安装EasyX图形库并正确配置。这段代码主要是文本和基本控制,若要加入图形界面,可以在graphics.h的基础上扩展。
    • 需要处理边界情况和错误输入的处理,例如文件读取失败,用户输入错误等。

    总结

    这段代码为你提供了一个英语单词学习系统的基础框架。你可以根据需要添加更多的词汇和扩展功能,例如界面美化、游戏逻辑的复杂性等。希望这对你的项目有所帮助!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(29条)

报告相同问题?

问题事件

  • 系统已结题 12月10日
  • 已采纳回答 12月2日
  • 创建了问题 12月1日