Beilin oj code C + + Experiment 7

Reading papers

describe

Thesis“ Fair Allocation of Scarce Medical Resources in the Time of Covid-19 "This paper discusses the fairness of the allocation of scarce medical resources in Covid-19 epidemic. Now take one of the paragraphs and analyze it with C + + string stream to complete the following functions:

  • Count the total number of words in this paragraph (subject to space separation)
  • Count the total number of punctuation points in this paragraph (only half angle period, comma and double quotation marks are considered)

Note the requirements of this question:

  • The main function has been written as follows, and only the readPapers() function is submitted
  • The header file needs to be included by itself
int main() {
    std::string content;
    std::getline(std::cin, content, '\n');
    readPapers(content);
    return 0;
}

input

A paragraph of English text without line breaks, including periods, commas and double quotation marks.

output

Output the total number of words and punctuation respectively, such as

30,6

Input sample 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".

Output sample 1

30,6

Tips

  • According to English writing rules, there may be no spaces between punctuation marks and words.
  • Double quotes are ''
  • String stream can be used to quickly segment strings
#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

describe

On the basis of the previous question, this paper attempts to further analyze the word frequency of the paper. Now extract one of the paragraphs. Please use C + + string stream combined with common STL containers to complete the following functions:

  • Count the number of non repeated words in this paragraph (that is, only one word appears multiple times)
  • Words that are pure numbers are not words

Note the requirements of this question:

  • The main function has been written as follows. Only the termFrequency() and alphabetSortedFrequency() functions are submitted
  • The header file needs to be included by itself
int main() {

    // Get text string from standard input
    std::string content;
    std::getline(std::cin, content, '\n');

    map<string, unsigned> msu;

    // termFrequency is required to implement word segmentation and remove punctuation
    // Get the word, store it in the map, and record the word frequency (occurrence times)
    // Finally, the number of words that are not repeated is returned    
    unsigned nWords = termFrequency(content, msu);

    // Sort by the first letter A-Z, one line and one word output word frequency
    alphabetSortedFrequency(msu);

    return 0;
}

input

A paragraph of English text without line breaks, including periods, commas and double quotation marks.

output

Sort by the first letter of the word A-Z, and output lowercase words and frequency one by one, such as

apple:10
banana:5
cherry:1

Input sample 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".

Output sample 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

Tips

  • Only half angle period, comma and double quotation mark "" are considered
  • The case difference should be considered. The header file can be included and the case conversion can be performed with the transform function
  • You can use the stringstream class to determine whether a string represents a number

 #include <bits/stdc++.h>
 #include <map>
 #include <string>
 #include <sstream> 
 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;
  }
 }

Point cloud

describe

Comma separated values (CSV) files store tabular data (numbers and text) in plain text. points.csv stores a set of point cloud information represented by three-dimensional coordinates [x,y,z]. Each line represents a point. Each point is represented by three floating-point numbers representing x, y and z coordinates (in meters), and three integer numbers represent color RGB information, which are separated by half width commas. First line header information. Please use C + + file stream to realize the following functions:

  • Read in point cloud information
  • The center of gravity positions of these points are counted (xyz is averaged respectively), and the points are written in the same coordinate format [XXXX, yyyy, ZZZZZZ] CSV last line
  • Offset all points by 100m in the positive x direction (i.e. x=x+100) and 50m in the negative y direction, and write points in the original order and format_ offset. csv

Note the requirements of this question:

  • Save the data under the title as points CSV, submit after the local test is correct
  • The main function has been written as follows, and only the processPoints() function is submitted
  • The header file needs to be included by itself
  • Never screen output in your code! Otherwise, OJ judgment will be affected and AC will not be available! It can be measured locally

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

input

Save the following as points CSV as your local test data:

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

output

Point cloud in processing...

Input sample 1

nothing

Output sample 1

Point cloud in processing...

Tips

  • When saving the file on the local disk, pay attention to the writing method of the path. You can use either relative path or absolute path. Double slashes are used in the path to avoid escaping characters.
  • You can use the stringstream class to convert a string to a numeric type
  • The number of decimal places after the decimal point can be controlled by setprecision function for file stream output, such as ofs < < fixed < < setprecision (3);
  • When attaching data to the end of the file, pay attention to whether there is a line break at the end. If not, it can be added in the original file in advance

#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

Added by tysoncane on Sun, 23 Jan 2022 08:12:30 +0200