叽里咕噜卡卡 2013-09-19 05:14 采纳率: 0%
浏览 1294

C++实现命令行功能:`ls`,`cd`,`cat`, `add`, `remove`

#题目 要求

Browse and modify a JSON data structure with ls,cd,cat, add, remove

#实现功能

  • Let's say we have this address entry data structure in JSON

    {
        "entries": {
            "lilei" : {
                "age": 27,
                "mobile" : "13700000000",
                "address" : "Earth somewhere"
            }, 
            "hanmeimei" : {
                "age": 26,
                "mobile" : "13700000001",
                "address" : "Earth somewhere else"
            }
        }
    }
    
  • start interactive command line application

    sh> ./ab

    ab> _

  • use ls command to list the items in current position

    ab> ls

    ab> entries

  • use cd command to go to the entry like go to a directory

    ab> cd entries

    ab> ls

    ab> lilei hanmeimei

  • use cat command to display th item data

    ab> cat lilei

    ab> "lilei" : { "age": 27, "mobile" : "13700000000", "address" : "Earth somewhere" }

  • use add command to add new address entry to JSON

    ab> cd entries

    ab> add

    key: xiaoming

    value: { "age": 28, "mobile" : "13700000002", "address" : "Earth somewhere too" }

    address entry added

    ab> ls

    ab> lilei hanmeimei xiaoming

    Note: the key should be identical, if trying to add duplicated key, just display an error message

  • use remove command to get one or more address entries

    ab> cd entries

    ab> remove

    please give the key: xiaoming

    xiaoming was deleted from JSON

    Note: if trying to delete an non-exist key, just display an error message

  • get help

    ab> !help

    then help message should be displayed, and when encoutering invalid command, help message
    should also be displayed.

  • quit from the application

    ab> !quit

    sh> _

  • 写回答

1条回答

  • ChatGPTᴼᴾᴱᴺᴬᴵ 2023-02-16 13:02
    关注

    该回答引用ChatGPT

    这是一个比较大的项目,需要使用C++的一些基本操作,例如文件读写、字符串处理、JSON解析等,同时还需要实现一个命令行交互界面。以下是一个实现思路,可以供参考。

    • 首先,需要使用一个JSON库来处理JSON数据。C++有许多JSON库可供选择,例如nlohmann/json、rapidjson等。这里以nlohmann/json为例。
    • 实现一个CommandLine类,作为命令行交互的主要逻辑。在CommandLine类中,需要实现以下函数:
    1. ls:列出当前位置的所有条目,如果当前位置是一个JSON对象,则列出其中的所有键;如果当前位置是一个JSON数组,则列出数组中的所有元素。

    2. cd:进入到某个条目中,如果条目是一个JSON对象,则进入其中;如果条目是一个JSON数组,则进入其中的某个元素。

    3. cat:查看当前位置的某个条目,输出该条目的完整JSON内容。

    4. add:向当前位置的JSON对象中添加一个新的键值对。

    5. remove:删除当前位置的某个键值对。

    • 在CommandLine类中,需要使用nlohmann/json库读取和写入JSON文件。可以使用std::fstream来读写JSON文件。
    • 在CommandLine类中,需要使用字符串处理函数将用户输入的命令解析成对应的函数调用。可以使用std::string中的find、substr等函数来解析字符串。
    • 在CommandLine类中,需要使用std::vector来保存当前位置的JSON对象和JSON数组。每次进入一个新的JSON对象或JSON数组时,将其压入vector中;每次离开一个JSON对象或JSON数组时,将其弹出vector。
    • 在CommandLine类中,需要处理一些错误情况,例如用户输入无效命令、用户进入了一个不存在的条目等情况。可以使用try-catch语句来处理一些异常情况。
    • 在main函数中,创建一个CommandLine对象并调用其run函数,从而启动命令行交互界面。
    • 实现帮助命令和退出命令,帮助命令应当列出所有可用的命令和其用法。退出命令可以使用std::exit函数来结束程序。

    注意,这只是一个实现思路,具体实现过程可能会因为库的选择、具体需求等因素而有所不同。

    评论

报告相同问题?

悬赏问题

  • ¥15 求解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果