需要帮助!! [英] Help needed!!

查看:60
本文介绍了需要帮助!!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!


以下是我的代码,它应该执行以下操作:

1)将一行的每个标记存储为向量的元素,并将此

矢量存储在另一个矢量中。


2)如果数据是> 0,它应该存储为1,否则,它应该存储为

为零,在另一个名为binaryVec的向量中。


它正确编译,但是,对于向量中的每个元素

,输出始终为1,即使它是< 0.有人可以告诉我我在做什么吗?b $ b做错了吗?


#include< fstream>

# include< string>

#include< iomanip>

#include< iostream>

#include< istream>

#include< cstdlib>

#include< vector>

#include< sstream>

using namespace std ;


typedef vector< string>行;


int main()

{

ifstream inFile;

inFile.open( " data.txt");


if(inFile.fail())

{

cout<< \ n文件未成功打开 <<结束;

退出(1);

}


vector< lines> SourceVector;

string one_line;

char token [128];

istringstream lineStream(one_line);


vector< bool> binaryVec;

bool binary;


while(getline(inFile,one_line,''\ n''))

{

cout<< 从文件中读取: << one_line<< endl;

vector< string> text;

istringstream lineStream(one_line);


while(lineStream>> token)

{

cout<< "令牌: <<令牌<< endl;

text.push_back(token); //在文本向量中存储标记。

double newToken = atof(标记);

if(newToken> = 0)

{二进制=真; }


else

{binary = false; }


binaryVec.push_back(二进制);

}


SourceVector.push_back(text); //将文本向量存储在

SourceVector。


//测试binaryVec的输出

for(int i = 1; i < binaryVec.size(); i ++)

{

cout<< 二元元素 << i<< "是 << binaryVec [i]<<

endl;

cout<< text.front()<<结束;

}

}

系统(PAUSE);

返回0;

}


顺便说一下,文件看起来像:


A 0.1 0.2 0.3 0.4

B 0.2 0 0.4 0

..


提前付款!

Hello!

Below is my code which is supposed to do the following:
1) store each token of a line as elements of a vector, and store this
vector in another vector.

2) if the data is > 0, it should be stored as 1, else, it should be stored
as zero, in another vector called binaryVec.

It compiles correctly, however, the output is always 1 for every element
in the vector, even if it is < 0. Can someone please tell me what i''m
doing wrong?

#include <fstream>
#include <string>
#include <iomanip>
#include <iostream>
#include <istream>
#include <cstdlib>
#include <vector>
#include <sstream>
using namespace std;

typedef vector<string> lines;

int main()
{
ifstream inFile;
inFile.open("data.txt");

if (inFile.fail())
{
cout <<"\nThe file was not successfully opened" << endl;
exit(1);
}

vector<lines> SourceVector;
string one_line;
char token[128];
istringstream lineStream(one_line);

vector<bool> binaryVec;
bool binary;

while (getline(inFile, one_line, ''\n''))
{
cout << "Read from file: " << one_line << endl;
vector<string> text;
istringstream lineStream( one_line );

while( lineStream >> token )
{
cout << " Token: " << token << endl;
text.push_back( token ); // stores tokens in text vector.
double newToken = atof(token);
if ( newToken >= 0)
{ binary = true; }

else
{ binary = false; }

binaryVec.push_back(binary);
}

SourceVector.push_back( text ); // stores text vector in
SourceVector.

//Testing output for binaryVec
for( int i = 1; i < binaryVec.size(); i++ )
{
cout << "Binary element " << i << " is " << binaryVec[i] <<
endl;
cout << text.front() << endl;
}
}
system("PAUSE");
return 0;
}

by the way, the file looks something like:

A 0.1 0.2 0.3 0.4
B 0.2 0 0.4 0
..

Thanx in advance!

推荐答案



kittykat写道:

kittykat wrote:
您好!

以下是我的代码,应该执行以下操作:
1)将一行的每个标记存储为向量的元素,并将该
向量存储在另一个向量中。

2)如果数据是> 0,它应该存储为1,否则,它应该是
存储为零,在另一个名为binaryVec的向量中。

它正确编译,但是,每个$的输出总是1矢量中的b $ b元素,即使它是< 0.有人可以告诉我,我做错了什么?

[snip]

顺便说一下,文件看起来像是这样的: />
A 0.1 0.2 0.3 0.4
B 0.2 0 0.4 0
Hello!

Below is my code which is supposed to do the following:
1) store each token of a line as elements of a vector, and store this
vector in another vector.

2) if the data is > 0, it should be stored as 1, else, it should be stored as zero, in another vector called binaryVec.

It compiles correctly, however, the output is always 1 for every element in the vector, even if it is < 0. Can someone please tell me what i''m
doing wrong?

[snip]

by the way, the file looks something like:

A 0.1 0.2 0.3 0.4
B 0.2 0 0.4 0
.




我没有看到任何负数在你的数据文件中...


-shez-



I don''t see any negative numbers in your data file...

-shez-


kittykat写道:
kittykat wrote:
下面是我的代码,应该执行以下操作:
1)将一行的每个标记存储为向量的元素,并将此
向量存储在另一个向量中。

2)如果数据是> 0,它应该存储为1,否则,它应该存储
^^^

我想提请你注意操作员。 更大。

为零,在另一个名为binaryVec的向量中。

它正确编译,但是,每个元素的输出始终为1
矢量,即使它是< 0.
^^^^^^^^^^^^^^^^^

我也想提请你注意这个即使它是< ; 0"东西。

有人可以告诉我我做错了什么吗?


双newToken = atof(令牌) );
if(newToken> = 0)
{binary = true; }

其他
{binary = false; }


以上四(5)行都是不好的风格。你应该简单地写


binary = newToken ??? 0;


现在,我把???因为运营商似乎与您描述的第2部分中使用的运营商不同。为什么?

binaryVec.push_back(二进制);
}

SourceVector.push_back(text); //将文本向量存储在SourceVector中。

//测试binaryVec的输出
for(int i = 1; i< binaryVec.size(); i ++)


在C ++中,我们计算数组元素,向量等,FROM ZERO。尝试

记住这一点。

{
cout<< 二元元素 << i<< "是 << binaryVec [i]<<
endl;
cout<< text.front()<<系统(暂停);
返回0;
}方式,文件看起来像这样:

A 0.1 0.2 0.3 0.4
B 0.2 0 0.4 0
Below is my code which is supposed to do the following:
1) store each token of a line as elements of a vector, and store this
vector in another vector.

2) if the data is > 0, it should be stored as 1, else, it should be stored ^^^
I''d like to draw your attention to the operator. "Greater".
as zero, in another vector called binaryVec.

It compiles correctly, however, the output is always 1 for every element
in the vector, even if it is < 0. ^^^^^^^^^^^^^^^^^
I''d also like to draw your attention to this "even if it is < 0" thing.
Can someone please tell me what i''m
doing wrong?
[...]

double newToken = atof(token);
if ( newToken >= 0)
{ binary = true; }

else
{ binary = false; }
The four (five) lines above are bad style. You should simply write

binary = newToken ??? 0;

Now, I put ??? there because the operator seems to be different from
the one used in the part 2 of your description. Why is that?

binaryVec.push_back(binary);
}

SourceVector.push_back( text ); // stores text vector in
SourceVector.

//Testing output for binaryVec
for( int i = 1; i < binaryVec.size(); i++ )
In C++ we count elements of arrays, vectors, etc., FROM ZERO. Try to
remember that.
{
cout << "Binary element " << i << " is " << binaryVec[i] <<
endl;
cout << text.front() << endl;
}
}
system("PAUSE");
return 0;
}

by the way, the file looks something like:

A 0.1 0.2 0.3 0.4
B 0.2 0 0.4 0
.




BTW,你的输出是什么样的?你期望它看起来像什么?b $ b喜欢?你说的是即使它是< 0" ;.我没有看到任何数据更少

比0,是吗?


V



BTW, what does your output look like? What do you expect it to look
like? You speak of "even if it is < 0". I don''t see any data less
than 0, do you?

V


kittykat写道:
kittykat wrote:

您好!

以下是我的代码,应该执行以下操作:
1)存储一行的每个标记作为向量的元素,并将此
向量存储在另一个向量中。

2)如果数据是> 0,它应该存储为1,否则,它应该被存储为零,在另一个称为binaryVec的向量中。


这不是你编码的。在这里你说数据必须更大

然后0.0但你编码:大于或等于0.0
它正确编译,但是,每个元素的输出总是1
在向量中,即使它是<有人可以告诉我,我做错了吗?

Hello!

Below is my code which is supposed to do the following:
1) store each token of a line as elements of a vector, and store this
vector in another vector.

2) if the data is > 0, it should be stored as 1, else, it should be stored
as zero, in another vector called binaryVec.
That''s not what you coded. Here you say that the data has to be greater
then 0.0 But you coded: greater or equal 0.0

It compiles correctly, however, the output is always 1 for every element
in the vector, even if it is < 0. Can someone please tell me what i''m
doing wrong?




对不起。我用我的输入文件测试了你的程序,它按预期工作了

(忽略了如何处理0.0的上面的缺陷)


什么输入文件你在用吗?你觉得你的节目在哪里?
错了?


-

Karl Heinz Buchegger
kb******@gascad.at


这篇关于需要帮助!!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆