北林oj代码C++实验七

Reading papers

描述

论文 "Fair Allocation of Scarce Medical Resources in the Time of Covid-19" 探讨了Covid-19疫情中稀缺医疗资源分配的公平性问题。现摘取其中一段,请使用C++字符串流对其进行分析,完成以下功能:

  • 统计这段文字的单词总数(以空格分隔为准)
  • 统计这段文字的标点总数(仅考虑半角句号、逗号、双引号)

注意本题要求:

  • main函数已写好如下,只提交readPapers()函数
  • 头文件需由自己包含
int main() {
    std::string content;
    std::getline(std::cin, content, '\n');
    readPapers(content);
    return 0;
}

输入

一段不换行、含句号、逗号、双引号的英文文字。

输出

分别输出单词总数和标点总数,如

30,6

输入样例 1 

No matter what difficulties we encounter, don't be afraid and face them with a smile. The best way to eliminate fear is to face the fear itself. "Persistence is victory".

输出样例 1

30,6

提示

  • 根据英文书写规则,标点符号与单词之间可能没有空格。
  • 双引号为 " "
  • 利用stringstream可快速分割字符串
#include<iostream>
#include<string>
using namespace std;
void readPapers(string content)
{
    int count = 0;
    int i = 0;
    int j = 0;
    while (i <= content.length())
    {
        if (content[i] == ' ')
            count++;
        if ((content[i] == ',') || (content[i] == '.') || (content[i] == '"'))
            j++;
        i++;
    }
    count++;
    cout << count << "," << j << endl;
}

Counting words

描述

在上一题基础上,试图进一步对论文进行词频分析。现摘取其中一段,请使用C++字符串流结合常用STL容器完成以下功能:

  • 统计这段文字的不重复的单词个数(即多次出现的单词只计为一个)
  • 纯数字的单字不作为单词

注意本题要求:

  • main函数已写好如下,只提交termFrequency()和alphabetSortedFrequency()函数
  • 头文件需由自己包含
int main() {

    // 从标准输入获取文本串
    std::string content;
    std::getline(std::cin, content, '\n');

    map<string, unsigned> msu;

    // 要求termFrequency实现分词,去掉标点
    // 获取单词存放在map中,记录词频(出现次数)
    // 最后返回不重复的单词数量    
    unsigned nWords = termFrequency(content, msu);

    // 按首字母A-Z排序一行一词输出词频
    alphabetSortedFrequency(msu);

    return 0;
}

输入

一段不换行、含句号、逗号、双引号的英文文字。

输出

按单词首字母A-Z排序,一行一词输出小写单词和频数,如

apple:10
banana:5
cherry:1

输入样例 1 

No matter what difficulties we encounter, don't be afraid and face them with a smile. The best way to eliminate fear is to face the fear itself. "Persistence is victory".

输出样例 1

a:1
afraid:1
and:1
be:1
best:1
difficulties:1
don't:1
eliminate:1
encounter:1
face:2
fear:2
is:2
itself:1
matter:1
no:1
persistence:1
smile:1
the:2
them:1
to:2
victory:1
way:1
we:1
what:1
with:1

提示

  • 仅考虑半角句号、逗号、双引号 " " 三种标点
  • 应考虑大小写差异,可包含头文件用transform函数进行大小写转换
  • 可使用stringstream类判断字符串是否表示数字

#include <bits/stdc++.h>
#include <map>
#include <string>
#include <sstream> 
#include<iostream>
#include<algorithm>
using namespace std;
unsigned termFrequency(string content, map<string, unsigned>& msu) {
    int L = content.length();
    for (int i = 0; i < L; i++) {
        if (content[i] == '.' || content[i] == ',' || content[i] == '"')
            content[i] = ' ';
    }
    string word;
    stringstream ss(content);
    while (ss >> word)
    {
        int flag = 0;
        for (int i = 0; i < word.size(); i++)
        {
            if (word[i] < '0' || word[i]>'9') {
                flag = 1;
                break;
            }
        }
        if (flag == 1)
        {
            transform(word.begin(), word.end(), word.begin(), ::tolower);
            map<string, unsigned>::iterator ite = msu.find(word);
            if (msu.count(word) == 0)
                msu.insert(map<string, unsigned>::value_type(word, 1));
            else
                ite->second++;
        }
    }
    return msu.size();
}
void alphabetSortedFrequency(map<string, unsigned> msu) {
    map<string, unsigned>::iterator it;
    for (it = msu.begin(); it != msu.end(); it++) {
        cout << it->first << ":" << it->second << endl;
    }
}
int main() {

    // 从标准输入获取文本串
    std::string content;
    std::getline(std::cin, content, '\n');

    map<string, unsigned> msu;

    // 要求termFrequency实现分词,去掉标点
    // 获取单词存放在map中,记录词频(出现次数)
    // 最后返回不重复的单词数量    
    unsigned nWords = termFrequency(content, msu);

    // 按首字母A-Z排序一行一词输出词频
    alphabetSortedFrequency(msu);

    return 0;
}

Point cloud

描述

逗号分隔值(Comma-Separated Values,CSV)文件以纯文本形式存储类似表格形式的数据(数字和文本)。points.csv存放了一组由三维坐标 [x,y,z] 表示的点云信息,每行表示一个点,每个点由3个浮点数分别表示x、y和z坐标(单位为米),3个整型数表示颜色RGB信息,均以半角逗号分隔。首行为文件头信息。请使用C++文件流实现以下功能:

  • 读入点云信息
  • 统计这些点的重心位置(xyz分别求平均值),按相同坐标格式 [xxxx,yyyy,zzzz] 写入points.csv最后一行
  • 将所有点朝x正方向偏移100米(即x=x+100),y负方向偏移50米,按原顺序、格式写入points_offset.csv

注意本题要求:

  • 使用题目下方数据保存为points.csv,在本地测试正确后提交
  • main函数已写好如下,只提交processPoints()函数
  • 头文件需由自己包含
  • 绝不可在你的代码中进行屏幕输出!否则影响OJ判题,无法AC!在本地测好即可

    int main() {
        std::cout << "Point cloud in processing..." << endl;
        processPoints();
        return 0;
    }
    
    

输入

将以下内容保存为points.csv作为你的本地测试数据:

X,Y,Z,R,G,B
244407.100,6010942.604,19.256,140,131,124
244407.097,6010942.547,19.244,142,131,126
244407.080,6010942.541,19.242,144,135,128
244407.124,6010942.599,19.253,144,131,126
244407.120,6010942.553,19.240,140,130,124
244407.125,6010942.565,19.241,144,133,128
244407.090,6010942.570,19.249,142,131,126
244407.072,6010942.575,19.253,145,135,126
244407.119,6010942.576,19.246,140,130,124
244407.079,6010942.575,19.248,161,151,147
244407.096,6010942.581,19.250,142,133,126
244407.089,6010942.604,19.255,140,131,124
244407.066,6010942.598,19.253,144,135,128
244407.112,6010942.599,19.252,137,128,121
244407.089,6010942.598,19.255,138,130,124
244407.067,6010942.569,19.247,149,142,133
244407.103,6010942.524,19.238,147,137,130
244407.057,6010942.512,19.240,161,153,144
244407.127,6010942.525,19.235,144,135,128
244407.074,6010942.524,19.241,154,142,135

输出

Point cloud in processing...

输入样例 1 

输出样例 1

Point cloud in processing...

提示

  • 文件保存在本地磁盘时,注意路径的书写方式,既可以使用相对路径,亦可使用绝对路径。路径中使用双斜杠避免字符被转义。
  • 可运用stringstream类将字符串转为数字类型
  • 文件流输出可用setprecision函数控制小数点后位数,如ofs << fixed << setprecision(3);
  • 向文件末尾附加数据时,注意末尾是否已有换行符,若无可事先在原始文件内加入

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
void processPoints()
{
	double points_x, points_y, points_z, point_r, point_g, point_b;
	double sum_x = 0, sum_y = 0, sum_z = 0;
	int pr = 0, ps = 0, pb = 0;
	double X[20], Y[20];
	double x[] = { 244407.100,244407.097,244407.080,244407.124,244407.120,244407.125,244407.090,244407.072,244407.119,244407.079,244407.096,244407.089,244407.066,244407.112,244407.089,244407.067,244407.103,244407.057,244407.127,244407.074 };
	double y[] = { 6010942.604,6010942.547,6010942.541,6010942.599,6010942.553,6010942.565,6010942.570,6010942.575,6010942.576,6010942.575,6010942.581,6010942.604,6010942.598,6010942.599,6010942.598,6010942.569,6010942.524,6010942.512,6010942.525,6010942.524 };
	double z[] = { 19.256,19.244,19.242,19.253,19.240,19.241,19.249,19.253,19.246,19.248,19.250,19.255,19.253,19.252,19.255,19.247,19.238,19.240,19.235,19.241 };
	int R[] = { 140,142,144,144,140,144,142,145,140,161,142,140,144,137,138,149,147,161,144,154 };
	int G[] = { 131,131,135,131,130,133,131,135,130,151,133,131,135,128,130,142,137,153,135,142 };
	int B[] = { 124,126,128,126,124,128,126,126,124,147,126,124,128,121,124,133,130,144,128,135 };
	fstream myfile;
	myfile.open("D:\\points.csv", ios::out | ios::in | ios::trunc);
	if (!myfile) {
		exit(0);
	}
	myfile << "X, Y, Z, R, G, B\n";
	for (int i = 0; i < 20; i++)
	{
		myfile << fixed << setprecision(3) << x[i] << "," << y[i] << "," << z[i] << "," << R[i] << "," << G[i] << "," << B[i] << endl;
	}
	for (int i = 0; i < 20; i++)
	{
		sum_x += x[i];
		sum_y += y[i];
		sum_z += z[i];
		pr += R[i];
		ps += G[i];
		pb += B[i];
	}
	points_x = sum_x / 20;
	points_y = sum_y / 20;
	points_z = sum_z / 20;
	point_r = pr / 20;
	point_g = ps / 20;
	point_b = pb / 20;
	myfile << fixed << setprecision(3) << points_x << "," << points_y << "," << points_z << "," << point_r << "," << point_g << "," << point_b;
	myfile.close();
	myfile.open("D:\\points_offset.csv", ios::trunc | ios::app);
	for (int i = 0; i < 20; i++)
	{
		X[i] = x[i] + 100;
		Y[i] = y[i] - 50;
	}
	myfile << "X, Y, Z, R, G, B\n";
	for (int i = 0; i < 20; i++)
	{
		myfile << fixed << setprecision(3) << X[i] << "," << Y[i] << "," << z[i] << "," << R[i] << "," << G[i] << "," << B[i] << endl;
	}
	myfile.close();
}
int main() {
	std::cout << "Point cloud in processing..." << endl;
	processPoints();
	return 0;
}

2020/6/26

  • 12
    点赞
  • 46
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值