介绍性 C++:将字符串与文件行中的数字分开并将这些数字相加
Posted
技术标签:
【中文标题】介绍性 C++:将字符串与文件行中的数字分开并将这些数字相加【英文标题】:Introductory C++: Separating strings from digits in a file line and summing up those digits 【发布时间】:2016-04-01 03:06:17 【问题描述】:我是 C++ 新手,正在编写一个本质上是成绩册的程序,其中给我一个包含学生的文件和他们的成绩列表。以下是文件外观的示例:
“约翰·史密斯 34 29 10 49
汉娜·琼斯 45 48 34 46"
等等
所有成绩都满分 50。我必须创建两个向量,一个用于姓名,一个用于每个学生的分数总和(他们的成绩百分比)。向量按最高等级排序。我想我已经弄清楚了这部分并为它创建了一个函数。
我不知道如何将分数的总和相加。我知道我需要将名称与文件中的数字分开,然后为文件中的每一行添加这些数字。我只是不知道我将如何去做。我尝试了一些事情,但它还没有奏效。到目前为止,这是我的代码:
#include <iostream>;
#include <vector>;
#include <fstream>;
#include <string>;
#include <sstream>;
using namespace std;
void sort_vectors(vector<string>& n, vector<double>& p)
// selection sort algorithm
// sorting the percentages
for (int i = 0; i < p.size() - 1; i++)
int min = i;
for (int j = i + 1; j < p.size(); j++)
if (p[j] < p[min])
min = j;
int temp = p[i];
string ntemp = n[i];
p[i] = p[min];
p[min] = temp;
n[i] = n[min];
n[min] = ntemp;
char percent2grade(double p)
if (p >= 90)
return 'A';
else if (90 > p >= 80)
return 'B';
else if (80 > p >= 70)
return 'C';
else if (70 > p >= 60)
return 'D';
else
return 'E';
int main()
// Ask the user for a filename and open the file in read mode.
cout << "Please enter a file name + .txt: ";
string filename;
cin >> filename;
ifstream in_file;
in_file.open(filename);
//While file DNE
while (in_file.fail())
//ask user to enter valid filename until user enters valid filename.
in_file.clear();
cout << "This file doesn't exist. Please enter a valid file name + .txt: ";
cin >> filename;
in_file.open(filename);
//Creating a new file
ofstream out_file;
out_file.open("output.txt");
//Creating the vectors and string for reading the file
vector<string> students;
vector<double> grades;
string line;
//reading the file one line at a time
while (getline(in_file, line))
string name;
double score;
// Splitting the lines between names and grades
//locate the first digit
int i = 0;
while (!isdigit(line[i]))
i++;
//Locate the end of the name
int j = i - 1;
while (isspace(line[j]))
j--;
// Extract name
name = line.substr(0, j + 1);
students.push_back(name);
// Extract grades
istringstream stream;
stream.str(line.substr(i));
// As you can see here, I tried to make an array.
// It didn't exactly work, but I'll leave it this way for now.
const int CAPACITY = 100;
double gradesum[CAPACITY];
for (int k = 0; k < (line.substr(i)).size(); k++)
stream >> gradesum[k];
score = gradesum[k];
system("pause");
return 0;
所以,如您所见,我尝试创建一个数组并将名称后的所有数字都放入数组中。我还没有尝试将它们加在一起,但我想看看当我看到数组时是否可以。这不好。看起来真的很丑。我完全迷路了,所以如果有人可以帮助我,我会非常感激。
我意识到我的代码中的错误可能比我预期的要多,但我现在正在尽力而为!如果您看到任何其他可以改进的地方,请告诉我。
谢谢。
【问题讨论】:
【参考方案1】:#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
void sort_vectors(vector<string>& n, vector<double>& p)
// selection sort algorithm
// sorting the percentages
for (int i = 0; i < p.size() - 1; i++)
int min = i;
for (int j = i + 1; j < p.size(); j++)
if (p[j] < p[min])
min = j;
int temp = p[i];
string ntemp = n[i];
p[i] = p[min];
p[min] = temp;
n[i] = n[min];
n[min] = ntemp;
char percent2grade(double p)
if (p >= 90)
return 'A';
else if (90 > p >= 80)
return 'B';
else if (80 > p >= 70)
return 'C';
else if (70 > p >= 60)
return 'D';
else
return 'E';
int main()
// Ask the user for a filename and open the file in read mode.
cout << "Please enter a file name + .txt: ";
string filename;
cin >> filename;
ifstream in_file;
in_file.open(filename.c_str());
//While file DNE
while (in_file.fail())
//ask user to enter valid filename until user enters valid filename.
in_file.clear();
cout << "This file doesn't exist. Please enter a valid file name + .txt: ";
cin >> filename;
in_file.open(filename.c_str());
//Creating a new file
ofstream out_file;
out_file.open("output.txt");
//Creating the vectors and string for reading the file
vector<string> students;
vector<double> grades;
string line;
//reading the file one line at a time
while (getline(in_file, line))
string name;
double score;
// Splitting the lines between names and grades
//locate the first digit
int i = 0;
while (!isdigit(line[i]))
i++;
//Locate the end of the name
int j = i - 1;
while (isspace(line[j]))
j--;
// Extract name
name = line.substr(0, j + 1);
students.push_back(name);
// Extract grades
istringstream stream;
stream.str(line.substr(i));
// This gives you the mean score in percent
int total=0;
j=0;
while(stream>>i)
total+=i;
j++;
score=total*2/j;
//system("pause");
return 0;
【讨论】:
以上是关于介绍性 C++:将字符串与文件行中的数字分开并将这些数字相加的主要内容,如果未能解决你的问题,请参考以下文章
如何在以前读过的行中找到一些单词并在输出中删除它 - C++ 中的读/写字符串