快速提问。我正在为单一作业做我的投资组合C ++问题。这是标准偏差。我的问题是从文件中读取多个字符串,请参阅此处的文字;
设计并编写一个c ++程序,从文件
scored.dat
中读取一组分数,并将其均值和标准差输出到cout
。
我不会打扰实际的等式,我的那部分差不多整齐了。我的查询直接基于将从文件读取的文本输出到字符串中。例如,如果文档有这三个分数:
10
15
11
不是按原样输出文本,而是将它们分成三个字符串;
Score_One(这将是10)
Score_Two(这将是15)
Score_Three(将是11)
我希望我在这里有意义。感谢。
答案 0 :(得分:1)
这是一个解决方案,它写起来很有趣,而且很难找到我希望出现的解决方案。它也可能具有一些娱乐和/或教育价值。基本思想是将值写为
std::cout << 10 << 15 << 11 << reset << '\n';
std::cout << 1 << 2 << 3 << reset << '\n';
要实现这一目标,需要一些机器,但实际上并不是那么糟糕。代码很好:
#include <locale>
#include <iostream>
#include <algorithm>
static int index(std::ios_base::xalloc());
static std::string const names[] = { "One", "Two", "Three", "Four", "Five" };
static std::string const score("Score_");
static std::string const other(" (Which would be ");
std::ostream& reset(std::ostream& out)
{
out.iword(index) = 0;
return out;
}
struct num_put
: std::num_put<char>
{
iter_type do_put(iter_type to, std::ios_base& fmt, char_type fill,
long v) const {
to = std::copy(score.begin(), score.end(), to);
if (fmt.iword(index) < 5) {
to = std::copy(names[fmt.iword(index)].begin(),
names[fmt.iword(index)].end(), to);
++fmt.iword(index);
}
else {
throw std::runtime_error("index out of range!");
}
to = std::copy(other.begin(), other.end(), to);
to = this->std::num_put<char>::do_put(to, fmt, fill, v);
*to++ = ')';
*to++ = ' ';
return to;
}
};
int main()
{
std::cout.imbue(std::locale(std::locale(), new num_put));
std::cout << 10 << 15 << 11 << reset << '\n';
std::cout << 1 << 2 << 3 << reset << '\n';
}
答案 1 :(得分:1)
你需要做这样的事情:
int raw_score_one = 11; //you have already set this but for the sake of clarity
std::stringstream output;
output << raw_score_one;
std::string Score_One = output.str();
每个分数......