需要帮忙 ... [英] need help ...

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

问题描述

我需要一个从txt文件中读取的函数,并随机选择一行

从中检索字符串(带空格)并将其返回给main函数。


thx

解决方案

Nikola写道:

我需要一个从txt读取的函数文件并随机选择一行
从中检索字符串(带空格)并将其返回到main



函数。


我认为这是学生的问题。尝试解决问题。您可以

在您知道有多少行之前不要选择随机行。所以你

必须从计算线开始。要计算行数,你必须阅读

行。要读取行,您必须打开文件。要打开

文件,您必须知道文件名。等等。用一些

形式写下来可以提供很大的帮助。首选方法称为伪代码,但任何

形式都比仅仅是烦乱和fla更好。

在comp.lang.ci中的

读取:

我需要一个从txt文件中读取的函数,并随机选择一行从中检索字符串(带空格)并将其返回给main函数。



i赌你做。你的作业什么时候到期?


-

a签名


osmium写道:

Nikola写道:

我需要一个从txt文件中读取的函数,并随机选择一行从中检索字符串的行(带有空格)并将其返回到主



函数。

我认为这是学生的问题。尝试解决问题。在知道有多少行之前,你可以不选择随机行。




有很好的方法随机选择一行
事先不知道有多少。事实上,你可以在没有预先知识的情况下随机选择N行的b
;

情况N == 1特别容易,但N> 1并不困难。参见

Knuth计算机程序设计的艺术,第二卷:精美数据库

算法,第3.4.2节,算法R.


(嗯,我想你确实需要*一些*先进的知识。

具体来说,你需要知道随机数的来源

可以为文件中的每一行提供不同的值,而不会重复
。一个PRNG将提供20亿不同的

好的数字很容易用便携式C写出来; 40亿美元也很容易在一些小的善良中牺牲。所以提前

知识相当于知道该文件持有的行数少于4亿美元;我认为这可能被认为是简单家庭作业的

背景下的公理。) />

-
Er ********* @ sun.com


I need a function that reads from a txt file and randomly chooses a line
from which retrieves a string (with spaces) and returns it to main function.

thx

解决方案

Nikola writes:

I need a function that reads from a txt file and randomly chooses a line
from which retrieves a string (with spaces) and returns it to main


function.

I assume this is a student problem. Try breaking the problem down. You can
not choose a random line until you know how many lines there are. So you
must start by counting the lines. To count the lines you will have to read
the lines. To read the lines you will have to open the file. To open the
file you must know the file name. And so on. Writing things down in some
form can be a great help. The preferred method is called pseudocode but any
form at all is better than just fussing and flailing.


in comp.lang.c i read:

I need a function that reads from a txt file and randomly chooses a line
from which retrieves a string (with spaces) and returns it to main function.



i bet you do. when is your homework due?

--
a signature


osmium wrote:

Nikola writes:

I need a function that reads from a txt file and randomly chooses a line
from which retrieves a string (with spaces) and returns it to main



function.

I assume this is a student problem. Try breaking the problem down. You can
not choose a random line until you know how many lines there are.



There are perfectly good ways to choose a line at random
without knowing in advance how many there are. In fact, you can
choose a random sample of N lines without advance knowledge; the
case N==1 is particularly easy, but N>1 isn''t difficult. See
Knuth "The Art of Computer Programming, Volume II: Seminumerical
Algorithms," section 3.4.2, Algorithm R.

(Well, I guess you do in fact need *some* advance knowledge.
Specifically, you need to know that the source of random numbers
can deliver a different value for every line in the file, without
repetitions. A PRNG that will deliver two billion distinct
"good" numbers is easy to write in portable C; four billion is
also easy at some small sacrifice of "goodness." So the advance
knowledge amounts to knowing that the file holds fewer than four
billion lines; I think this may be considered axiomatic in the
context of a simple homework assignment.)

--
Er*********@sun.com


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

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