1: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
问题
经典问题----strlen was not declared in thos scope
#include<iostream>
#include<string>
#include<vector>
#include <sstream>
#include<cstring>//**<string.h>是不包括strlen的,要使用cstring**
using namespace std;
void readPapers(string content)
{
int symbolCount = 0;
vector<string> arr;
istringstream ss(content);
string word;
while (ss >> word) {
arr.push_back(word);
}
int lenth = strlen(content.c_str());
const char *str = content.c_str();
char ch = *str++;
for (int i = 0; i < lenth; i++)
{
ch = *str++;
if (content[i]== ','|| content[i] == '"' || content[i] == '.')
{
symbolCount++;
}
}
/* for (size_t i = 0; i < arr.size(); i++) {
cout << arr[i] << endl;
}*/
cout << arr.size() <<","<< symbolCount<<endl;
}
2: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;
}
问题
map根据key查找value
map<string, unsigned> msu;
map< string, unsigned>::iterator iter;
iter = msu.find(arr[i]);
msu.insert(make_pair(arr[i], iter->second+1));
#include<iostream>
#include<string>
#include<vector>
#include <sstream>
#include<cstring>
#include<map>
#include<algorithm>
using namespace std;
unsigned termFrequency(string content, map<string, unsigned>& msu)
{
//content = "12 45 5 No matter no difficulties we encounter, don't be no and face No NO a smile. The best way to eliminate fear is to face the fear itself. \"Persistence is victory\".56 8 1 ";
//content = "Chinese people often take a yingwen ming or 123 English name in order to help foreign friends or potential employers who struggle to pronounce or remember their Mandarin ones.";
int lenth = strlen(content.c_str());
for (int i = 0; i < lenth; i++)
{
if (content[i] == ',' || content[i] == '"' || content[i] == '.')
{
content[i] = ' ';
}
}
vector<string> arr;
istringstream ss(content);
string word;
while (ss >> word) {
stringstream sin(word);
double d;
char c;
if (!(sin >> d))
{
transform(word.begin(), word.begin() + 1, word.begin(), ::tolower);//首字母大写的就变成小写
arr.push_back(word);
}
if (sin >> c)
{
transform(word.begin(), word.begin() + 1, word.begin(), ::tolower);//首字母大写的就变成小写
arr.push_back(word);
}
}
for (size_t i = 0; i < arr.size(); i++) {
if (msu.count(arr[i]) == 0)
{
//transform(arr[i].begin(), arr[i].begin() + 1, arr[i].begin(), ::tolower);//首字母大写的就变成小写
msu.insert(make_pair(arr[i], 1));
}
else
{
//transform(arr[i].begin(), arr[i].begin() + 1, arr[i].begin(), ::tolower);//首字母大写的就变成小写
map< string, unsigned>::iterator iter;
iter = msu.find(arr[i]);
iter->second = iter->second + 1;
}
}
return msu.size();
}
int cmp(const pair<string, unsigned>& x, const pair<string, unsigned>& y)
{
return x.first < y.first;
}
void sortMapByValue(map<string, unsigned>& tMap, vector<pair<string, unsigned> >& tVector)
{
for (map<string, unsigned>::iterator curr = tMap.begin(); curr != tMap.end(); curr++)
tVector.push_back(make_pair(curr->first, curr->second));
sort(tVector.begin(), tVector.end(), cmp);
}
void alphabetSortedFrequency(map<string, unsigned> &msu)
{
vector<pair<string, unsigned>> tVector;
sortMapByValue(msu, tVector);
for (int i = 0; i < tVector.size(); i++)
cout << tVector[i].first << ":" << tVector[i].second << endl;
/*map< string, unsigned>::iterator iter;
for (iter = msu.begin(); iter != msu.end(); iter++) {
cout << iter->first << ":" << iter->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;
}
3: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()函数
头文件需由自己包含
绝不可在你的代码中进行屏幕输出!
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
提示
文件保存在本地磁盘时,注意路径的书写方式,既可以使用相对路径,亦可使用绝对路径。路径中使用双斜杠避免字符被转义。
可运用stringstream类将字符串转为数字类型
文件流输出可用setprecision函数控制小数点后位数,如ofs << fixed << setprecision(3);
向文件末尾附加数据时,注意末尾是否已有换行符,若无可事先在原始文件内加入
AC代码
#include<iostream>
#include<string>
#include<vector>
#include <sstream>
#include<cstring>
#include<map>
#include<algorithm>
#include <fstream>
#include <iomanip>
using namespace std;
class point {
public :
double x,y,z;
int r, g, b;
};
void processPoints()
{
vector<point>po;
ifstream inFile("points.csv", ios::in);
string line;
string field;
while (getline(inFile, line))//getline(inFile, line)表示按行读取CSV文件中的数据
{
string field;
istringstream sin(line); //将整行字符串line读入到字符串流sin中
point point1;
getline(sin, field, ','); //将字符串流sin中的字符读入到field字符串中,以逗号为分隔符
point1.x = (double)atof(field.c_str()) ;//将刚刚读取的字符串转换成int
getline(sin, field, ','); //将字符串流sin中的字符读入到field字符串中,以逗号为分隔符
point1.y = (double)atof(field.c_str());
getline(sin, field, ','); //将字符串流sin中的字符读入到field字符串中,以逗号为分隔符
point1.z = (double)atof(field.c_str());
getline(sin, field, ','); //将字符串流sin中的字符读入到field字符串中,以逗号为分隔符
point1.r = atoi(field.c_str());
getline(sin, field, ','); //将字符串流sin中的字符读入到field字符串中,以逗号为分隔符
point1.g = atoi(field.c_str());
getline(sin, field, ','); //将字符串流sin中的字符读入到field字符串中,以逗号为分隔符
point1.b = atoi(field.c_str());
po.push_back(point1);
}
inFile.close();
double sumx = 0.0, sumy = 0.0, sumz = 0.0;
double avex = 0.0, avey = 0.0, avez = 0.0;
for (int i = 0; i < po.size(); i++)
{
sumx += po[i].x;
sumy += po[i].y;
sumz += po[i].z;
}
avex = sumx / (double)po.size();
avey = sumy / (double)po.size();
avez = sumz / (double)po.size();
ofstream outFile("points.csv", ios::out);
for (int i = 0; i < po.size(); i++)
{
outFile << fixed << setprecision(3) << po[i].x << "," << fixed << setprecision(3) << po[i].y << "," << fixed << setprecision(3) << po[i].z << "," << po[i].r << "," << po[i].g << "," << po[i].b << endl;
}
outFile << fixed << setprecision(3) << avex << "," << fixed << setprecision(3) << avey << "," << fixed << setprecision(3) << avez << endl;
outFile.close();
ofstream outFile2("points_offset.csv", ios::out);
for (int i = 0; i < po.size(); i++)
{
outFile2 << fixed << setprecision(3) << po[i].x+100 << "," << fixed << setprecision(3) << po[i].y-50 << "," << fixed << setprecision(3) << po[i].z << "," << po[i].r << "," << po[i].g << "," << po[i].b << endl;
}
outFile2.close();
}