C++程序设计(面向对象进阶) 第四单元测验与作业

1

用户从键盘输入形式如 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:
不要在主函数的返回语句前加 std::cin.get(); 否则无法通过本作业的测试用例

警告2:
请不要使用C++14或者C++17的特性。本OJ还不支持这两个标准。比如将函数返回值写为 auto (C++14的特性)

警告3:
请不要添加额外信息的输出(比如输出提示等)。额外输出的信息会导致OJ系统无法判别你的结果

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

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

输入样例:
12345-54321

输出样例:
12345 54321
时间限制:500ms内存限制:32000kb

//Parse.h
#pragma once

using std::string;
class Parse{
    string s;
    
public:
    Parse(string s_);
    
    int getFirst();
    int getLast();
};

//Parse.cpp
#include <string>
#include "Parse.h"

using std::string;
Parse::Parse(string s_){
    s = s_;
}

int Parse::getFirst(){
    int position = (int)s.find("-");
    string firststr = s.substr(0, position);
    int first = std::stoi(firststr);
    return first;
}

int Parse::getLast(){
    int position = (int)s.find("-");
    string laststr = s.substr(position + 1);
    int last = std::stoi(laststr);
    return last;
}
//mian.cpp
#include <iostream>
#include "Parse.h"

using std::cin;
using std::cout;
using std::endl;

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

2

  1. 用户从键盘输入形式如 #ab!#12-45c#+f 这样的“数值范围”字符串,代表从12到45这个范围。12前面和45后面会有一些不包含数字以及英文连字符的干扰字符。
  2. 请你编写一个类 Parse,可以解析这个字符串。然后提供两个函数,能够获取字符串中的第一个整数和第二个整数。(10分)
//Parse.h
#pragma once

using std::string;
class Parse{
    string s;
    
public:
    Parse(string s_);
    
    int getFirst();
    int getLast();
};
//Parse.cpp
#include <string>
#include "Parse.h"

using std::string;
Parse::Parse(string s_){
    s = s_;
}

int Parse::getFirst(){
    int i = 0;
    int position = (int)s.find("-");
    string firststr = s.substr(0, position);
    for(char ch : s){
        if(0 < (ch - '0') && (ch - '0') < 10){
            break;
        }
        i++;
    }
    int first = std::stoi(firststr.substr(i));
    return first;
}

int Parse::getLast(){
    int position = (int)s.find("-");
    string laststr = s.substr(position + 1);
    int last = std::stoi(laststr);
    return last;
}
//mian.cpp
#include <iostream>
#include "Parse.h"

using std::cin;
using std::cout;
using std::endl;

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

3

  1. 编写类Circle。
  2. 编写主函数,利用 std::array 创建包含10个对象的Circle数组并初始化。
  3. 用基于范围的for循环遍历Circle数组,求10个Circie对象的面积之和(10分)
    题目内容:
    类Circle有两个构造函数。无参构造函数没有函数体,使用default关键字声明。有参构造函数接收一个double类型参数作为Circle的半径。Circle类的半径的默认值是1.0

类Circle有一个 double getArea() 函数,返回圆的面积。圆周率的值取 3.14

在主函数中用 std::array 声明一个有10个Circle对象的数组。其中前5个Circle对象的半径分别设置为1.0、2.0、3.0、4.0、5.0。后5个对象使用Circle的默认构造函数初始化。

在主函数中,用基于范围的for循环遍历Circle数组,求10个Circie对象的面积之和

在主函数中,输出所求出的面积和。输出结果为双精度浮点数。使用系统默认精度。无需做格式和精度控制

输入格式:
无输入

输出格式:
一个浮点数。浮点数前后没有空格和换行

输入样例:

输出样例:

时间限制:500ms内存限制:32000kb

//Circle.h
#pragma once

class Circle{
    double radius = 1.0;
public:
    Circle() = default;
    Circle(double radius_);
    
    double getArea();
};
//Circel.cpp
#include <iostream>
#include "Circle.h"

Circle::Circle(double radius_){
    radius = radius_;
}

double Circle::getArea(){
    return (3.14 * radius * radius);
}
//main.cpp
#include <iostream>
#include "Circle.h"
#include <array>

using std::array;
using std::cout;
using std::endl;

int main()
{
    double sum = 0;
    array<Circle, 10> group{1.0, 2.0, 3.0, 4.0, 5.0};
    for(auto i : group){
        sum += i.getArea();
    }
    cout << sum;
    
    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

比巧克力巧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值