我的任务是在C ++中创建一个小程序,它加载日志和文本文件并用它们的值分隔它们的参数。
输入文件的结构如下:
#CURRENT_OPTIMIZED_BVH_NODES
210023
#CURRENT_OPTIMIZED_BVH_COST
118.856
#CURRENT_OPTIMIZED_BVH_COST_RATIO
1
#ANIMATION_TIME
0.0321091
#REFIT_TIME
0.0411972
#UPDATE_TIME
0.000135373
#UPDATE_TIME_SUM
0.000135373
#BUILD_TIME_SUM
0.000135373
#UPDATE_REFIT_RATIO
0.00328597
...
我的任务是输出,当用户写入某个参数的名称时,该输出将显示在cmd中。 像这样:
#CURRENT_OPTIMIZED_BVH_NODES
210023
210024
210065
210047
265400
....
我的程序现在看起来像这样。我有两个问题 - 我不知道如何使用向量,但我需要它们因为动态数组(没有人知道文件中有多少不同的参数以及单个参数有多少值)然后如何使用argc, argv为我的输出。 我找到了许多建议,但没有一种做法,我可以用于我的代码:
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <stdio.h>
#include <vector>
using namespace std;
struct ParStruct
{
string name;
vector<double> value;
double average;
double min;
double max;
double median;
};
void WriteParameter(ParStruct Parameter, int SizeOfValueArr)
{
cout << Parameter.name << endl;
for (int i = 0; i < SizeOfValueArr; i++){
cout << Parameter.value[i] << endl;;
}
}
int main(int argc, char *argv[])
{
vector <ParStruct> Parameter;
Parameter.push_back(ParStruct());
Parameter[0].value.push_back(double());
string s = argv[0];
argv[1];
int LogNumber, LineNumber = 1, optionnumber;
ifstream LogFile;
double ParValue;
string MemoryLog, options, fileline;
string LogFilePath;
cout << "Path: ";
getline(cin, LogFilePath);
LogFile.open(LogFilePath);
while (LogFile.good()){
while ((getline(LogFile, fileline)) && !(LogFile.eof())) {
if (fileline[0] == '#'){ //Parameter
MemoryLog = fileline;
for (int i = 0; i < Parameter.size(); i++) {
if ((fileline != Parameter[i].name) && Parameter[i].name.size() < 2) {
Parameter[i].name = fileline;
}
else
{
Parameter.push_back(ParStruct());
Parameter[i].name = fileline;
}
}
}
else {
istringstream Number(fileline);
Number >> ParValue;
for (int i = 0; i < Parameter.size(); i++) {
if (Parameter[i].name == MemoryLog) {
for (int m = 0; m < Parameter[i].value.size(); m++) {
if (Parameter[i].value[m] <= 0) {
Parameter[i].value[m] = ParValue;
}
}
}
}
}
}
}
for (int i = 0; i < Parameter.size(); i++){
WriteParameter(Parameter[i], Parameter[i].value.size());
}
LogFile.close();
return 0;
}
答案 0 :(得分:0)
好的,我明白了,这是我的整个程序,它完美无缺。
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
struct ParStruct
{
string name;
vector<double> value;
double average;
double min;
double max;
double median;
};
double average(ParStruct Parameter)
{
double average = 0;
for (int i = 0; i < Parameter.value.size(); i++){
average += Parameter.value[i];
}
average = (average / Parameter.value.size());
return average;
};
double min(ParStruct Parameter){
double min = Parameter.value[0];
for (int i = 0; i < Parameter.value.size(); i++) {
if (Parameter.value[i] <= min) {
min = Parameter.value[i];
}
}
return min;
};
double max(ParStruct Parameter){
double max = Parameter.value[0];
for (int i = 0; i < Parameter.value.size(); i++) {
if (Parameter.value[i] >= max) {
max = Parameter.value[i];
}
}
return max;
};
double Median(ParStruct Parameter)
{
int middle = (Parameter.value.size() / 2);
double median;
//sort numbers
sort(Parameter.value.begin(), Parameter.value.end());
//
if (Parameter.value.size() % 2) {
median = Parameter.value[middle];
}
else {
median = (Parameter.value[middle] + Parameter.value[middle + 1]) / 2;
}
return median;
}
int main(int argc, char *argv[])
{
vector <ParStruct> Parameter;
Parameter.push_back(ParStruct());
int LogNumber;
ifstream LogFile;
double ParValue;
string MemoryLog, fileline;
string LogFilePath;
bool writtenName;
//
// Check the number of parameters
if (argc < 3) {
// Tell the user how to run the program
cerr << "Usage: <" << argv[0] << "> <Path to the file> <Parameter Name>" << endl;
/* "Usage messages" are a conventional way of telling the user
* how to run a program if they enter the command incorrectly.
*/
return 1;
}
// Print the user's name:
LogFile.open(argv[1]);
if (!(LogFile.is_open())){
cout << argv[1] << " File opening FAILED";
}
while (LogFile.good()) {
while ((getline(LogFile, fileline)) && !(LogFile.eof())) {
if (fileline[0] == '#'){ //Parameter
MemoryLog = fileline;
for (int i = 0; i < Parameter.size(); i++) {
if ((fileline != Parameter[i].name) && Parameter[i].name.size() < 2) {
Parameter[i].name = fileline;
writtenName = true;
}
else if (fileline == Parameter[i].name)
break;
}
if (writtenName){
Parameter.push_back(ParStruct());
writtenName = false;
}
}
else { //Number
istringstream Number(fileline);
Number >> ParValue;
for (int i = 0; i < Parameter.size(); i++) {
if (Parameter[i].name == MemoryLog) {
Parameter[i].value.push_back(ParValue);
}
}
}
}
}
for (int i = 0; i < Parameter.size() - 1; i++) {
Parameter[i].average = average(Parameter[i]);
Parameter[i].min = min(Parameter[i]);
Parameter[i].max = max(Parameter[i]);
Parameter[i].median = Median(Parameter[i]);
//TO DO ANOTHER CALC
}
for (int i = 0; i < Parameter.size(); i++) {
if (Parameter[i].name == argv[2]) {
cout << "Parameter: " << Parameter[i].name << endl;
cout << "AVERAGE: " << Parameter[i].average << endl;
cout << "MIN: " << Parameter[i].min << endl;
cout << "MAX: " << Parameter[i].max << endl;
cout << "MEDIAN: " << Parameter[i].median << endl;
cout << "SIZE: " << Parameter[i].value.size() << endl;
for (int n = 0; n < Parameter[i].value.size(); n++)
{
cout << Parameter[i].value[n] << endl;
}
}
}
LogFile.close();
cin.get();
return 0;
}