面向对象程序设计作业(4)

第四次作业

GitHub:pullself

基本信息

在主函数中加入了命令行判断。以及一个简陋的菜单。

代码行数 调试bug 编码时间
1032行 暂无 2h

程序总共由5个文件组成:

  • main.cpp:主函数,程序的入口。
  • elevator.h:定义了电梯类,包含两个电梯成员的封闭类以及两个派生类。
  • elevator.cpp:电梯类方法的实现。
  • ask.h:定义了请求类。
  • ask.cpp:请求类方法的实现。

编码过程

简介

  1. 再稍微了解命令行之后,大致觉得命令行的处理就是对字符串的处理,通过字符串来引导程序的运行。
  2. 在实现作业给出的两种输入之外,加入了一个循环的系统来引导提示输入,确保程序的输入输出目录合理。

问题

1.命令行的处理是一定要放在main函数中吗?说实话这是个疑问。

bug修复

暂无


具体代码与效果

功能介绍

  1. 在直接只输入input地址,会在input地址下生成三个output文件来存储。
  2. 直接输入input地址以及任意(3以下)output地址,按照给的地址输入输出,未给出的output地址默认input目录。
  3. 输入任意其他内容,会给出相应提示,并且进入引导状态。
  4. 具有五个功能:1.input地址输入。2.output地址输入。3.run:程序运行。4.view:查看四个文件地址。5.end:中止程序。

具体代码

if (argc == 2)
    {
        if (strcmp(argv[1], "run") == 0)
        {
            cout << "   错误:缺少input文件地址" << endl;
            cout << "   需要更多帮助请输入help" << endl;
            error = 1;
        }
        else if (strcmp(argv[1], "help") == 0)
        {
            cout << "   *:\\****\\input.txt:输入input文件地址" << endl;
            cout << "   *:\\****\\outputi.txt:输入output文件地址,i=1,2,3,若不给出默认为input文件目录" << endl;
            cout << "   run:运行程序" << endl;
            cout << "   view:查看文件地址" << endl;
            cout << "   end:中止程序" << endl;
            error = 1;
        }
        else if (strcmp(argv[1], "view") == 0)
        {
            cout << "   input:缺失" << endl;
            cout << "   output1:当前目录" << endl;
            cout << "   output2:当前目录" << endl;
            cout << "   output3:当前目录" << endl;
            error = 1;
        }
        else if (strcmp(argv[1], "end") == 0)
        {
            cout << "   运行终止" << endl;
            return -1;
        }
        else if (strstr(argv[1], "input") != NULL)
        {
            address_change(address[1], argv[1]);
            for (int i = 2; i < 5; i++)
            {
                address_copy(address[i], address[1], i - 1);
            }
            run_fun();
            cout << "   运行结束" << endl;
            return 0;
        }
        else if (strstr(argv[1], "output") != NULL)
        {
            cout << "   错误:缺少input文件地址" << endl;
            out_change(argv[1]);
            cout << "   output地址已存储" << endl;
            cout << "   需要更多帮助请输入help" << endl;
            error = 1;
        }
        else
        {
            cout << "   错误:非法输入" << endl;
            cout << "   需要更多帮助请输入help" << endl;
            error = 1;
        }
    }
    else
    {
        if (argc == 1)
        {
            strcpy_s(address[1], "input.txt");
            strcpy_s(address[2], "output1.txt");
            strcpy_s(address[3], "output2.txt");
            strcpy_s(address[4], "output3.txt");
            run_fun();
            cout << "   运行结束" << endl;
            return 0;
        }
        else if (argc > 2)
        {
            if (strstr(argv[1], "input") == NULL)
            {
                cout << "   错误:非法输入" << endl;
                cout << "   需要更多帮助请输入help" << endl;
                error = 1;
            }
            else
            {
                address_change(address[1], argv[1]);
                for (int i = 2; i < 5; i++)
                {
                    address_copy(address[i], address[1], i - 1);
                }
                for (int i = 2; i < argc; i++)
                {
                    out_change(argv[i]);
                }
                run_fun();
                cout << "   运行结束" << endl;
                return 0;
            }
        }
    }
    while (error == 1)
    {
        cin >> order;
        if (strcmp(order, "run") == 0)
        {
            if (address[1][0] == '\0')
            {
                cout << "   错误:缺少input文件地址" << endl;
                cout << "   需要更多帮助请输入help" << endl;
            }
            else
            {
                for (int i = 2; i < 5; i++)
                {
                    if (address[i][0] == '\0') address_copy(address[i], address[1], i - 1);
                }
                run_fun();
                cout << "   运行结束" << endl;
                return 0;
            }
        }
        else if (strcmp(order, "help") == 0)
        {
            cout << "   *:\\****\\input.txt:输入input文件地址" << endl;
            cout << "   *:\\****\\outputi.txt:输入output文件地址,i=1,2,3,若不给出默认为input文件目录" << endl;
            cout << "   run:运行程序" << endl;
            cout << "   view:查看文件地址" << endl;
            cout << "   end:中止程序" << endl;
        }
        else if (strcmp(order, "view") == 0)
        {
            if (address[1][0] == '\0') cout << "   input:缺失" << endl;
            else cout << "   input:" << address[1] << endl;
            for (int i = 2; i < 5; i++)
            {
                if (address[i][0] == '\0') cout << "   output" << i - 1 << ":当前目录" << endl;
                else cout << "   output" << i - 1 << ":" << address[i] << endl;
            }
        }
        else if (strcmp(order, "end") == 0)
        {
            cout << "   运行终止" << endl;
            return -1;
        }
        else if (strstr(order, "input") != NULL)
        {
            address_change(address[1], order);
            for (int i = 2; i < 5; i++)
            {
                if (address[i][0] == '\0')
                {
                    address_copy(address[i], address[1], i - 1);
                }
            }
            cout << "   已获取input地址,程序可运行" << endl;
        }
        else if (strstr(order, "output") != NULL)
        {
            out_change(order);
            cout << "   output地址已存储" << endl;
            if (address[1][0] == '\0') cout << "   警告:缺少input文件地址" << endl;
        }
        else
        {
            cout << "   错误:非法输入" << endl;
            cout << "   需要更多帮助请输入help" << endl;
        }
    }

实现效果


猜你喜欢

转载自www.cnblogs.com/pullself/p/9092327.html