如何计算C中的一些分布缩写 [英] How to calculate some distribution abbreviation in C

查看:92
本文介绍了如何计算C中的一些分布缩写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个包含多个文件的目录。每个文件都有一列值。

我编写一个代码打开目录并逐个打开文件,计算Min,Max和Average然后转到下一个文件。

但有一个问题我无法得到它?!!

这是我的代码:



Hi,
I have a directory which has multiple files. Each file has a column of values.
I write a code which open the directory and open files one by one, calculate Min, Max and Average and then goes to the next file.
But there is a problem I can't get it?!!
Here is my code:

static void scan_dir(const char *dir)
    {
        struct dirent * entry;
        DIR *d = opendir( dir );
    
        if (d == 0) {
            perror("opendir");
            return;
        }
    
        while ((entry = readdir(d)) != 0)
        {
            FILE *sensor;
            int num, min, max, sum, count, first;
            sensor = fopen( entry->d_name, "r");
            if (sensor != NULL)
            {
                for (sum = count = max = min = first = 0;
                fscanf(sensor, "%d", &num) == 1; sum += num, ++count)
                {
                    if (!first)
                    {
                        min = max = num; first = 1;
                    }
                    else if (num > max) max = num;
                    else if (num < min) min = num;
    	    }
               
                printf("count = %d, min = %d, max = %d, avg = %.1lf\n", count, min, max, sum / (double) count);
            }
        }
        closedir(d);
    }
    
    
    int main(int argc, char ** argv)
    {
        scan_dir(argv[1]);
        return 0;
    }





提前致谢,

Kambiz



Thanks in advance,
Kambiz

推荐答案

我们不做你的功课,所以我们不打算为你解决 - 这是一项重要的开发技巧,叫做调试,你只能通过使用而不是通过观察来学习。



所以使用你的调试器。

在scan_dir函数的第一行放一个断点,然后逐步查看代码,看看究竟发生了什么。弄清楚每一行在执行之前应该发生什么,并比较发生了什么。如果它达到了你的预期,那么它很好,继续下一步。如果没有,为什么不呢?出了什么问题?是代码还是您的期望?



尝试一下:这是一项有价值的技能,在你完成复杂的任务之前,最好在这样的简单任务中学习!



但我建议如果你改变你的编码风格会容易得多:将scan_dir分成不同的任务,所以它处理一个目录,并为每个文件调用一个单独的函数。然后每个文件通过另一种方法处理一组行。

这样你就简化了代码并分离了问题,它应该更容易调试。



[edit]


原始OP问题:

我有一个目录有多个文件。

每个文件都有一列值。

我写一个代码打开目录并逐个打开文件,计算最小值,最大值和平均值¬

然后转到下一个文件。

但有一个问题我无法得到它?





这是我的代码:



We don't do your homework, so we aren't going to fix it for you - that's an important development skill called debugging, which you only learn by using, not by watching.

So use your debugger.
Put a breakpoint on the first line of the scan_dir function, and step through the code looking at what exactly is going on. Work out what should happen as a result of each line before it is executed, and compare what did happen to that. If it did what you expected, then it;s fine, move on to the next. If it didn't, why not? What was the problem? Was it the code, or your expectations?

Try it: this is a valuable skill, and one which is best learned on simple tasks like this before you get to complicated tasks!

But I'd suggest that it would be a lot easier if you change your coding style: break scan_dir into separate tasks, so it handles a directory, and calls a separate function for each file. Then each file processes a set of lines via another method.
That way you are simplifying the code and separating the concerns and it should be easier to debug.

[edit]

Original OP question:
Hi, I have a directory which has multiple files.
Each file has a column of values.
I write a code which open the directory and open files one by one, calculate Min, Max and Average ¬
and then goes to the next file.
But there is a problem I can't get it?
!
!
Here is my code:

static void scan_dir(const char *dir)
{
struct dirent * entry;
DIR *d = opendir( dir );
 
if (d == 0) {
perror("opendir");
return;
}
 
while ((entry = readdir(d)) != 0)
{
printf("%s\n", entry->d_name);
FILE *sensor;
int num, min, max, sum, count, first;
sensor = fopen( entry->d_name, "r");
if (sensor != NULL)
{
for (sum = count = max = min = first = 0;
fscanf(sensor, "%d", &num) == 1; sum += num, ++count)
{
if (!first)
{
min = max = num; first = 1;
}
else if (num > max) max = num;
else if (num < min) min = num;
}
 
printf("count = %d, min = %d, max = %d, avg = %.1lf\n", count, min, max, sum / (double) count);
fclose(sensor);
}
else
{
printf("Unable to read file '%s'\n", entry->d_name );
}
}
closedir(d);
}
 
 
int main(int argc, char ** argv)
{
scan_dir(argv[1]);
return 0;
}





这是输出:





and here is the output:

1 file 1
2 count = 41, min = 1254, max = 1361, avg = 1298.3
3 ..
4 count = 0, min = 0, max = 0, avg = -nan
5 file 2
6 count = 41, min = 1865245, max = 1936860, avg = 1875714.9
7 file 3
8 count = 41, min = 0, max = 0, avg = 0.0
9 .
10 count = 0, min = 0, max = 0, avg = -nan
11 file 4
12 count = 41, min = 1, max = 1, avg = 1.0







以上,我不知道为什么印有3,4,9,10的线? br $> b $ b!

实际上它们甚至与任何文件无关!



所以有人可以帮我纠正代码?

[/ edit]


问题在于:此脚本读取目录中的所有文件;所以如果有一些隐藏文件,输出就不是我的预期。所以我在我的if命令中放了一个过滤器来读取.txt文件; - )
The problem was that: This script read all files within the directory; So if there is some hidden files, the output is not what I expected. So I put a filter in my "if" command to just read the ".txt" files ;-)


这篇关于如何计算C中的一些分布缩写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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