数据信息 [英] data message

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

问题描述

喜。我需要帮助我的任务之一。

i需要将stdin双精度存储在一个数组中。如果用户输入两次返回键(意味着一次在数字之后,另一次之后),则需要必须自动停止输入。但是,用户可以输入任何值,最多可以有1000个数字。同时,当我编译它时,我应该能够重定向到带有数字的文本文件文档,文件中的数字应该像stdin一样。如果使用fopen fclose,那就错了。

例如。 a.out -gt< /usr/units/ep100/ass_sup.062/test_values1


gt:函数

test_value1:带有数字的文本文件每一行

hi. i need help with one of my assignment.
i need to store stdin doubles in an array.at the same time if the user enter the return key twice(meaning once after the number and another after) the program must stop its input automatically. however the user may enter any value and can have up to 1000 numbers. at the same time i should be able to redirect to a text file document with numbers when i compile it and the numbers in the file should act just like stdin. if fopen fclose is used it is being done wrong.
eg. a.out -gt </usr/units/ep100/ass_sup.062/test_values1

gt: the function
test_value1: text file with numbers on each line

推荐答案

发布您的代码,以便我们可以看到您在做什么。请指出代码中出现错误的位置,以便我们更容易发现。
Post your code so we can see what you are doing. Please point out where the errors occur in your code, to make it easier for us to spot.



发布您的代码,以便我们可以看到你在做什么。请指出代码中出现错误的位置,以便我们更容易发现。
Post your code so we can see what you are doing. Please point out where the errors occur in your code, to make it easier for us to spot.


展开 | 选择 | Wrap | 行号


首先,将其分解为函数。所有这一切都在一个功能中是疯狂的。这使得管理和阅读变得困难。如果你以后写这样的东西,那么你和那些需要通过代码的人一样会把你的头发拉出来。细分,你可以发送相关的代码。


//这是不可移植的,据我所知它是一个gnu扩展名。不一定是错的,但你应该知道它。

double sorted [number_of_value + 1];


//这还不错。我个人把fprint放在外面,但这并不重要。我也会参加你的测试 i< number_of_value&& !feof(输入)

for(i = 0; i< number_of_value; i ++)

{

fprintf(输出, 请输入值:);


fscanf(输入,"%lf",& sorted [i]);

}


至于双重回报问题。定义char returnsReceived [5] = {}并使用fscanf(输入,"%4 [^ \ n \\\ r]",returnsReceived);在你的fscanf()之后。然后检查returnReceived是否有两个''\\ n'字符。在Unix中,它们将一个接一个地出现,在Windows中,它们之间会出现'\ r。在退出测试中添加两个''\\'n'的测试结果。


希望这会有所帮助。



Adrian
First off, break it down into functions. All of this in one function is crazy. It makes it hard to manage and read. If you write things like this later, you will be pulling your hair out as will those around you who need to go through your code. Broken down, you could send just the relevant code.

// This is not portable, as far as I know it is a gnu extension. Not necessarily wrong, but you should be aware of it.
double sorted[number_of_value+1];

// This is not bad. I''d personally put the fprint outside, but it doesn''t really matter. I''d also put in your test i<number_of_value && !feof(input)
for (i=0;i<number_of_value;i++)
{
fprintf(output,"Please enter values: ");

fscanf(input,"%lf",&sorted[i]);
}

As for the double return issue. Define char returnsReceived[5]={} and use fscanf(input, "%4[^\n\r]", returnsReceived); after your fscanf(). Then check returnsReceived for two ''\n'' characters. In Unix they will appear one after the other, in Windows they will appear with a ''\r'' between them. Add the result of your test for two ''\n''''s as part of your exit test.

Hope this helps.


Adrian


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

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