美文网首页
解析整数范围(《c++程序设计》在线作业)

解析整数范围(《c++程序设计》在线作业)

作者: YorigamiShion | 来源:发表于2019-03-21 23:51 被阅读0次

    该作业来自中国mooc《c++程序设计(面向对象进阶)》(北京邮电大学,崔毅东)
    用户从键盘输入形式如 12-45 这样的“数值范围”字符串,代表从12到45这个范围。
    请你编写一个类 Parse,可以解析这个字符串。然后提供两个函数,能够获取字符串中的第一个整数和第二个整数。(10分)
    题目内容:

    Parse类要提供一个有参构造函数,接收一个字符串参数;

    Parse类要提供一个 int getFirst() 函数,返回“数值范围”字符串中的前面的整数;

    Parse类要提供一个 int getLast() 函数,返回“数值范围”字符串中的后面的整数;

    程序的主函数如下,请将主函数代码拷贝到你的开发环境中,但是不要修改主函数内部的代码:

    #include <iostream>
    #include <string>
    int main() {
      std::string s{};
      std::cin >> s;  // 用户输入一个范围字符串
      Parse p(s);     // 构造Parse对象p,同时解析字符串 s
      std::cout << p.getFirst() << ' ' << p.getLast(); // 中间是两个单引号括起来的一个空格字符
      return 0;
    }
    

    要求:
    不要使用C语言的字符串处理函数
    使用string类自带的函数,或者标准库中的函数
    提示:

    在string中找'-'字符,可以使用string类中的find函数,或者find_first_of()函数。find_first_of()的说明:https://zh.cppreference.com/w/cpp/string/basic_string/find_first_of
    当然,你也可以用 find_last_of()或者rfind()函数。find_last_of()的说明:https://zh.cppreference.com/w/cpp/string/basic_string/find_last_of;rfind()的说明:https://zh.cppreference.com/w/cpp/string/basic_string/rfind
    将字符串的一部分提取出来,需要使用 substr() 成员函数。substr()的说明:https://zh.cppreference.com/w/cpp/string/basic_string/substr
    将字符串转换为整数,可以使用标准库的 std::stoi() 函数。std::stoi的说明:https://zh.cppreference.com/w/cpp/string/basic_string/stol

    输入格式:
    用英文连字符分隔的两个整数构成的字符串

    输出格式:
    两个整数,中间用1个空格分隔。第二个整数后面没有空格或者换行

    输入样例:
    12345-54321
    输出样例:
    12345 54321

    我的答案如下:

    #include <iostream>
    #include <string>
    class Parse {
    private:
        std::string str;
    public:
        Parse(std::string s);
        int getFirst();
        int getLast();
        ~Parse();
    };
    Parse::Parse(std::string s) {
        str = s;
    }
    Parse::~Parse(){
    }
    int Parse::getFirst() {
        int a=str.find("-");//string类的方法,返回参数的位置
        std::string b = str.substr(0, a);//string类的方法,复制从0到a的一段字符串
        int res = std::stoi(b);//将字符串转换成整数。
        return res;
    }
    
    int Parse::getLast(){
        int a = str.find("-");
        std::string b = str.substr(a+1);
        int res = std::stoi(b);
        return res;
    }
    int main() {
        std::string s{};
        std::cin >> s;  // 用户输入一个范围字符串
        Parse p(s);     // 构造Parse对象p,同时解析字符串 s
        std::cout << p.getFirst() << ' ' << p.getLast(); // 中间是两个单引号括起来的一个空格字符
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:解析整数范围(《c++程序设计》在线作业)

          本文链接:https://www.haomeiwen.com/subject/wkckvqtx.html