代码审查:平均程序 [英] Code Review: Averaging program

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

问题描述

我一直在努力学习C语言。但是我有麻烦

,因为缺少优质的在线文本(其中一些没问题)。但我终于

买了一本关于C,Practical C的书。我非常喜欢它。我非常熟悉

C基础知识,

因为我一直试图学习它。但是任何人,我写了一个程序

来平均命令行给出的任意数量的参数。我想b / b
可能会对我的编码风格或我做过的任何事情有所帮助

错误。它使用gcc(使用-Wall -ansi -pedantic)编译得很好。无论如何,这里

它是:


/ * average.c - 平均几个数字在一起。 * /

/ *版权所有(C)2003 Mark A. Nicolosi * /


#include< stdio.h>

#include< malloc.h>


浮点平均值(int * numbers,int how_many);


int main(int argc, char ** argv)

{

int how_many; / *要平均的数字数。 * /

int i; / * for循环索引。 * /

int * number; / *要平均的数字数组。 * /

浮动答案; / *变量来保持答案。 * /


how_many = argc - 1; / * argv [0]之后的每个参数应该是

平均数* /

if(how_many == 0)/ *如果为TRUE则不用任何参数调用。

* /

{

printf("用法:%s< num1>< num2> ... \ n" ,argv [0]);

返回1;

}


/ *分配足够的内存来保存数字数组平均为
。 * /

numbers = malloc(sizeof(int)* how_many);

/ *将argv [i + 1]中的字符串数组传送给

整合。 * /

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

{

sscanf(argv [i + 1]," ;%d",& numbers [i]);

}


/ *现在我们已经有了一系列的平均积分,平均''em。

* /

答案=平均值(数字,how_many);

printf("%g \ n" ,回答);


/ *释放数字数组。 * /


免费(数字);


返回0;

}

浮动平均值(int *数字,int how_many)

{

float answer = 0.0; / *变量来保持答案。 * /

int i; / * for循环索引。 * /


/ *循环添加number []数组中的所有数字。 * /

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

{

answer = answer + numbers [i]; < br $>
}


/ *现在除以数字的数量。 * /

answer = answer / how_many;


返回答案;

}


我将其复制并粘贴到链接中,所以我不确定它是否会看起来好像

点亮:)

谢谢。


-

Mark A. Nicolosi

I''ve been trying to learn C for quite a while. But I''ve had trouble
with the
lack of good quality online text (some of it''s alright). But I finally
bought
a book on C, Practical C. I like it alot. I''m pretty familiar with the
C basics,
because I''ve been trying to learn it for a while. But anyone, I wrote
a program
to average any number of arguments given at the command line. I
thought it might
be useful to get feedback about my coding style or anything I''ve done
wrong. It
compile just fine with gcc (with -Wall -ansi -pedantic). Anyways, here
it is:

/* average.c - Average several numbers together. */
/* Copyright (C) 2003 Mark A. Nicolosi */

#include <stdio.h>
#include <malloc.h>

float average (int *numbers, int how_many);

int main (int argc, char **argv)
{
int how_many; /* Number of numbers to average. */
int i; /* Index for for loop. */
int *numbers; /* Array of numbers to be averaged. */
float answer; /* Variable to hold the answer. */

how_many = argc - 1; /* Every argument after argv[0] should be
number to average */
if (how_many == 0) /* If TRUE then not called with any arguments.
*/
{
printf ("Usage: %s <num1> <num2> ...\n", argv[0]);
return 1;
}

/* Allocate enough memory to hold the array of numbers to be
averaged. */
numbers = malloc (sizeof (int) * how_many);
/* Transfer the string arrays in argv[i + 1] to an array of
integars. */
for (i = 0; i < how_many; i++)
{
sscanf (argv[i + 1], "%d", &numbers[i]);
}

/* Now that we''ve got an array of integars to average, average ''em.
*/
answer = average (numbers, how_many);
printf ("%g\n", answer);

/* Free the array of numbers. */

free (numbers);

return 0;
}

float average (int *numbers, int how_many)
{
float answer = 0.0; /* Variable to hold the answer. */
int i; /* Index for for loop. */

/* Loop to add all the numbers in the number[] array. */
for (i = 0; i < how_many; i++)
{
answer = answer + numbers[i];
}

/* Now divide by the number of numbers. */
answer = answer / how_many;

return answer;
}

I copy and pasted that in to links, so I''m not sure if it''ll look
alight :)
Thanks.

--
Mark A. Nicolosi

推荐答案

Mark A. Nicolosi< ma * ***@foobox.net>写道:

[...]
Mark A. Nicolosi <ma****@foobox.net> wrote:
[...]
/ * average.c - 平均几个数字在一起。 * /
/ *版权所有(C)2003 Mark A. Nicolosi * /

#include< stdio.h>
#include< malloc.h>


malloc.h是一个非标准的标题 - 要获得malloc的声明,你只需要#include< stdlib.h>


浮点平均值(int * numbers,int how_many);


一般来说,你应该选择加倍。对于浮点数,

除非你有特定的理由选择浮动。

int main(int argc,char ** argv)
{
int how_many; / *要平均的数字数。 * /
int i; / * for循环索引。 * /
int *数字; / *要平均的数字数组。 * /
浮动答案; / *变量来保持答案。 * /

how_many = argc - 1; / * argv [0]之后的每个参数都应该是
数字* /
if(how_many == 0)/ *如果为TRUE则不用任何参数调用。
* /


argc可能为零,在这种情况下,这个if分支将不会被取消。

{
printf(" Usage:%s< num1>< num2> ... \ n",argv [0]);


....这是一件好事,因为在这种情况下argv [0]将为NULL并且

你会遇到问题。

返回1;
}
/ *分配足够的内存来保存数字数组的平均值。 * /
numbers = malloc(sizeof(int)* how_many);


您应该检查malloc()在这里返回NULL,并报告错误。

由于您的程序当前已写入,如果malloc失败,它仍然会尝试

通过指针数字访问内存。

/ *将argv [i + 1]中的字符串数组转移到
integars数组中。 * /
for(i = 0; i< how_many; i ++)
{
sscanf(argv [i + 1],"%d",& numbers [i]) ;
}


您应该检查sscanf的返回值 - 它会告诉您它是否已成功执行转换。如果它没有,那么

number [i]中的值将是垃圾,导致垃圾结果(正式地说,这个例子中程序的行为是

)是未定义的,因为它在计算中使用

一个不确定的值。


或者你可以用0.0值预先填充数字数组。

/ *现在我们已经得到了一系列的平均值,平均值为''em。
* /
answer = average(数字,how_many);
printf ("%g\ n",answer);

/ *释放数字数组。 * /

免费(数字);

返回0;
}

浮动平均值(int *数字,int how_many)
{
浮点数= 0.0; / *变量来保持答案。 * /
int i; / * for循环索引。 * /

/ *循环添加number []数组中的所有数字。 * /
for(i = 0; i< how_many; i ++)
{
answer = answer + numbers [i];
}

/ *现在除以数字的数量。 * /
answer = answer / how_many;

返回答案;
}
/* average.c - Average several numbers together. */
/* Copyright (C) 2003 Mark A. Nicolosi */

#include <stdio.h>
#include <malloc.h>
malloc.h is a non-standard header - to get a declaration of malloc, you
only need to #include <stdlib.h>.
float average (int *numbers, int how_many);
In general, you should choose "double" for floating point numbers,
unless you have a specific reason for choosing "float".
int main (int argc, char **argv)
{
int how_many; /* Number of numbers to average. */
int i; /* Index for for loop. */
int *numbers; /* Array of numbers to be averaged. */
float answer; /* Variable to hold the answer. */

how_many = argc - 1; /* Every argument after argv[0] should be
number to average */
if (how_many == 0) /* If TRUE then not called with any arguments.
*/
It is possible for argc to be zero, in which case this if branch will
not be taken.
{
printf ("Usage: %s <num1> <num2> ...\n", argv[0]);
....which is a good thing, because in that case argv[0] would be NULL and
you would have a problem here.
return 1;
}

/* Allocate enough memory to hold the array of numbers to be
averaged. */
numbers = malloc (sizeof (int) * how_many);
You should check for malloc() returning NULL here, and report an error.
As your program is currently written, if malloc fails, it will still try
to access memory through the pointer "numbers".
/* Transfer the string arrays in argv[i + 1] to an array of
integars. */
for (i = 0; i < how_many; i++)
{
sscanf (argv[i + 1], "%d", &numbers[i]);
}
You should check that the return value of sscanf - it tells you if it
successfully performed the conversion. If it didn''t, the value in
numbers[i] will be garbage, leading to a garbage result (formally, the
behaviour of the program in this instance is undefined, because it uses
an indeterminate value in a calculation).

Alternatively you can pre-fill the numbers array with 0.0 values.
/* Now that we''ve got an array of integars to average, average ''em.
*/
answer = average (numbers, how_many);
printf ("%g\n", answer);

/* Free the array of numbers. */

free (numbers);

return 0;
}

float average (int *numbers, int how_many)
{
float answer = 0.0; /* Variable to hold the answer. */
int i; /* Index for for loop. */

/* Loop to add all the numbers in the number[] array. */
for (i = 0; i < how_many; i++)
{
answer = answer + numbers[i];
}

/* Now divide by the number of numbers. */
answer = answer / how_many;

return answer;
}




您可以重写此程序,它根本不需要任何动态内存

分配,因为你不需要存储每一个数字 -

你可以保持一个总计。这是你的程序的另一个版本

,它使用''argv''数组终止的事实

带有NULL指针:


#include< stdio.h>

#include< stdlib.h>


int main(int argc,char * * argv)

{

double total = 0.0;

int how_many = 0;

int n; <如果(sscanf(* argv,"%d",& n)== 1){<$ s $ br />
总计+ = n;

how_many ++;

}

}


if(how_many> 0){

printf("%g\ n",total / how_many);

返回0;

}


if(argv [0]){

printf(" Usage:%s< num1>< num2> .. 。\ n",argv [0]);

}


返回1;

}



You can rewrite this program so that it doesn''t need any dynamic memory
allocation at all, because you don''t need to store every single number -
you can just keep a running total. Here is an alternative version of
your program, that uses the fact that the ''argv'' array is terminated
with a NULL pointer:

#include <stdio.h>
#include <stdlib.h>

int main (int argc, char **argv)
{
double total = 0.0;
int how_many = 0;
int n;

while (*++argv) {
if (sscanf(*argv, "%d", &n) == 1) {
total += n;
how_many++;
}
}

if (how_many > 0) {
printf("%g\n", total / how_many);
return 0;
}

if (argv[0]) {
printf ("Usage: %s <num1> <num2> ...\n", argv[0]);
}

return 1;
}


Mark A. Nicolosi写道:
Mark A. Nicolosi wrote:
我一直在努力学习C语言同时。但是我在使用缺乏优质在线文本(其中一些没问题)时遇到了麻烦。但我终于买了一本关于C,Practical C的书。我非常喜欢它。我非常熟悉基础知识,
因为我一直试图学习它。但是任何人,我都写了一个程序来平均命令行给出的任意数量的参数。我认为它可能对获得有关我的编码风格或我所做的任何错误的反馈非常有用。它用gcc编译就好了(使用-Wall -ansi -pedantic)。无论如何,这里是:

/ * average.c - 平均几个数字在一起。 * /
/ *版权所有(C)2003 Mark A. Nicolosi * /

#include< stdio.h>
#include< malloc.h>

浮点平均值(int *数字,int how_many);
int main(int argc,char ** argv)
{
int how_many; / *要平均的数字数。 * /
int i; / * for循环索引。 * /
int *数字; / *要平均的数字数组。 * /
浮动答案; / *变量来保持答案。 * /

how_many = argc - 1; / * argv [0]之后的每个参数都应该是
数字* /
if(how_many == 0)/ *如果为TRUE则不用任何参数调用。
* /
{
printf(" Usage:%s< num1>< num2> ... \ n",argv [0]);
返回1;
}

/ *分配足够的内存来保持数字数组的平均值。 * /
numbers = malloc(sizeof(int)* how_many);

/ *将argv [i + 1]中的字符串数组传递给
整数数组。 * /
for(i = 0; i< how_many; i ++)
{
sscanf(argv [i + 1],"%d",& numbers [i]) ;
}
/ /现在我们已经得到了一系列的平均值,平均值为''em。
* /
答案=平均值(数字) ,how_many);
printf("%g\ n",answer);

/ *释放数字数组。 * /

免费(数字);

返回0;
}

浮动平均值(int *数字,int how_many)
{
浮点数= 0.0; / *变量来保持答案。 * /
int i; / * for循环索引。 * /

/ *循环添加number []数组中的所有数字。 * /
for(i = 0; i< how_many; i ++)
{
answer = answer + numbers [i];
}

/ *现在除以数字的数量。 * /
answer = answer / how_many;

返回答案;

我将其复制并粘贴到链接中,所以我是不确定它是否会看起来很好..... />谢谢。
I''ve been trying to learn C for quite a while. But I''ve had trouble
with the
lack of good quality online text (some of it''s alright). But I finally
bought
a book on C, Practical C. I like it alot. I''m pretty familiar with the
C basics,
because I''ve been trying to learn it for a while. But anyone, I wrote
a program
to average any number of arguments given at the command line. I
thought it might
be useful to get feedback about my coding style or anything I''ve done
wrong. It
compile just fine with gcc (with -Wall -ansi -pedantic). Anyways, here
it is:

/* average.c - Average several numbers together. */
/* Copyright (C) 2003 Mark A. Nicolosi */

#include <stdio.h>
#include <malloc.h>

float average (int *numbers, int how_many);

int main (int argc, char **argv)
{
int how_many; /* Number of numbers to average. */
int i; /* Index for for loop. */
int *numbers; /* Array of numbers to be averaged. */
float answer; /* Variable to hold the answer. */

how_many = argc - 1; /* Every argument after argv[0] should be
number to average */
if (how_many == 0) /* If TRUE then not called with any arguments.
*/
{
printf ("Usage: %s <num1> <num2> ...\n", argv[0]);
return 1;
}

/* Allocate enough memory to hold the array of numbers to be
averaged. */
numbers = malloc (sizeof (int) * how_many);
/* Transfer the string arrays in argv[i + 1] to an array of
integars. */
for (i = 0; i < how_many; i++)
{
sscanf (argv[i + 1], "%d", &numbers[i]);
}

/* Now that we''ve got an array of integars to average, average ''em.
*/
answer = average (numbers, how_many);
printf ("%g\n", answer);

/* Free the array of numbers. */

free (numbers);

return 0;
}

float average (int *numbers, int how_many)
{
float answer = 0.0; /* Variable to hold the answer. */
int i; /* Index for for loop. */

/* Loop to add all the numbers in the number[] array. */
for (i = 0; i < how_many; i++)
{
answer = answer + numbers[i];
}

/* Now divide by the number of numbers. */
answer = answer / how_many;

return answer;
}

I copy and pasted that in to links, so I''m not sure if it''ll look
alight :)
Thanks.




它会起作用,但是

1.更好地使用atoi()而不是sscanf()

2.有很多不必要的操作,你真的不需要

分配另一个数组来计算平均值。例如


#include< stdlib.h>

#include< stdio.h>

int main(int argc ,char ** argv)

{

int i,n;

float answer = 0;

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

answer =(answer * i + atoi(argv [i]))/(i + 1);

printf(...回答......);

返回0;

}



It will work, but
1. better use atoi() than sscanf()
2. There are a lot of unnecessary manipulations, you really do not need to
allocate another array to compute the average. e.g.

#include <stdlib.h>
#include <stdio.h>
int main (int argc, char **argv)
{
int i, n;
float answer=0;
for (i=0; i < argc; i++)
answer = (answer*i+atoi(argv[i]))/(i+1);
printf (... answer ...);
return 0;
}


我写道:

[...]
I wrote:
[...]
#include< stdio.h>
#include< stdlib.h>

int main (int argc,char ** argv)
{
double total = 0.0;
int how_many = 0;
int n;

while(* ++ argv){
#include <stdio.h>
#include <stdlib.h>

int main (int argc, char **argv)
{
double total = 0.0;
int how_many = 0;
int n;

while (*++argv) {




当然我在这里犯了一个经典的错误 - 假设argc更大

比0还要修改argv然后尝试获得原来的argv [0]




应该是


char *程序;


if(argc< 2){

if(argc< 1){

program =" average";

} else {

program = argv [0];

}


printf("用法:%s< num1> < NUM2> ... \ n",program);

返回1;

}


while(* ++ argv) {

/ * ... * /


然后从底部删除类似的printf。


- 凯文。



Of course I made a classic blunder here - assuming that argc is greater
than 0. And I modify argv and then try to get the original argv[0]
later.

That should be

char *program;

if (argc < 2) {
if (argc < 1) {
program = "average";
} else {
program = argv[0];
}

printf ("Usage: %s <num1> <num2> ...\n", program);
return 1;
}

while (*++argv) {
/* ... */

Then remove the similar printf from the bottom.

- Kevin.


这篇关于代码审查:平均程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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