批评我的计划! [英] critique my program!

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

问题描述

您好C-landers,您能对这个

计划找到任何改进或批评吗?


#include< stdio.h>

#define ZER 0

#define LIM 7

/ *根据其增加的大小排序数字* /

void e (void);

void printar(void);

void pline(void);


int array [LIM];

int main()

{

int i,temporal,rounds;

rounds = ZER;

array [0] = 99;阵列[1] = 0;阵列[2] = 55; array [3] = 18;

array [4] = 2;阵列[5] = 67;阵列[6] = 0; array [7] = 9;


e(); printar(); E(); PLINE(); e();


while(rounds< =(LIM + 1)){

for(i = ZER; i< LIM; ++ i){


if(array [i] array [i + 1]){

printf("%2d%2d",array [ i],array [i + 1]);


temporal = array [i + 1];

array [i + 1] = array [i ];

array [i] = temporal;

printar(); e();

}

}

++轮次;

}

pline(); E(); printar(); E(); e();

返回0;

}

void e(无效)

{

printf(" \ n");

}

void printar(无效)

{

int i;

for(i = ZER; i< = LIM; ++ i)

printf(" \t%2d", array [i]);

}

void pline(void)

{


printf (QUOT; ----------------------------------------------- -------------------------------");

}

Hello C-landers, can you find any improvement or critique to this
program?

#include <stdio.h>
#define ZER 0
#define LIM 7
/* Sort numbers according to its increasing size */
void e(void);
void printar(void);
void pline(void);

int array[LIM];
int main()
{
int i, temporal, rounds;
rounds = ZER;
array[0]=99; array[1]=0; array[2]=55; array[3]=18;
array[4]=2; array[5]=67; array[6]=0; array[7]=9;

e(); printar(); e(); pline(); e();

while (rounds <= (LIM+1)) {
for ( i = ZER ;i < LIM; ++i) {

if (array[i] array[i+1]) {
printf("%2d %2d ",array[i], array[i+1]);

temporal = array[i+1];
array[i+1] = array[i];
array[i] = temporal;
printar(); e();
}
}
++rounds;
}
pline(); e(); printar(); e(); e();
return 0;
}
void e(void)
{
printf("\n");
}
void printar(void)
{
int i;
for (i = ZER; i <= LIM; ++i)
printf("\t%2d ",array[i]);
}
void pline(void)
{

printf("------------------------------------------------------------------------------");
}

推荐答案

8月10日晚上10点40分,vonbres ... @ gmail.com写道:
On Aug 10, 10:40 pm, vonbres...@gmail.com wrote:

你好C-landers,你能找到对这个

计划的任何改进或批评吗?


#include< stdio.h>

#define ZER 0

#define LIM 7

/ *根据其增加的大小排序数字* /

无效e(void);

void printar(void);

void pline(void);


int array [LIM] ;

int main()

{

int i,temporal,rounds;

rounds = ZER;

array [0] = 99;阵列[1] = 0;阵列[2] = 55; array [3] = 18;

array [4] = 2;阵列[5] = 67;阵列[6] = 0;阵列[7] = 9;
Hello C-landers, can you find any improvement or critique to this
program?

#include <stdio.h>
#define ZER 0
#define LIM 7
/* Sort numbers according to its increasing size */
void e(void);
void printar(void);
void pline(void);

int array[LIM];
int main()
{
int i, temporal, rounds;
rounds = ZER;
array[0]=99; array[1]=0; array[2]=55; array[3]=18;
array[4]=2; array[5]=67; array[6]=0; array[7]=9;



通过为第8个元素(array [7])分配一些

,你可以超越数组界限。 br />
此外,打印线和打印换行的功能并不像内联那样好。


因为你构建你的程序的方式并不是可以重复使用的。


我会建议更多这样的内容:

#include< stdlib.h>

/ * etype的定义应该来自包含文件或

命令行#define等。 * /

typedef int etype;


void quadratic_sort_bi(etype array [],const size_t lo,

const size_t hi)

{

size_t i,

j,

h,

l;

for(i = lo + 1; i< = hi; i ++){

const etype tmp = array [i];

for(l = lo - 1,h = i; h - l 1;){

j =(h + l)/ 2;

if(tmp< array [j] )

h = j;

else

l = j;

}

for (j = i; jh; j--)

array [j] = array [j - 1];

array [h] = tmp;

}

}


#ifdef UNIT_TEST


#include< stdio.h>

#include< time.h>


/ *由于促销加倍,翻斗车应该可以工作很多

积分和浮点类型* /

void dump_arr(etype array [],size_t size)

{

size_t index;

for(i ndex = 0;指数<尺寸; index ++)

printf(" array [%u] =%f \ n",(unsigned)index,(double)

array [index]); < br $>
}


void init_arr(etype array [],size_t size)

{

size_t index ;

for(index = 0; index< size; index ++)

array [index] =(etype)rand();

}

int main(void)

{

int array [8];

size_t arr_size = sizeof array / sizeof array [0];

srand((unsigned)time(NULL));


init_arr(array,arr_size);

puts(" before sort:");

dump_arr(array,arr_size);

quadratic_sort_bi(array,0,arr_size - 1);

put(" after sort:");

dump_arr(array,arr_size);


返回0;

}


#endif

Right off the bat, you exceed the array bounds, by assigning something
to the 8th element (array[7]).
Also, the functions for printing a line and printing a newline are not
as good as just doing it inline.

Because of the way you have constructed your program the sort is not
reusable.

I would suggest something more along these lines:

#include <stdlib.h>
/* The definition for etype should come form an include file or
command line #define or the like. */
typedef int etype;

void quadratic_sort_bi(etype array[], const size_t lo,
const size_t hi)
{
size_t i,
j,
h,
l;
for (i = lo + 1; i <= hi; i++) {
const etype tmp = array[i];
for (l = lo - 1, h = i; h - l 1;) {
j = (h + l) / 2;
if (tmp < array[j])
h = j;
else
l = j;
}
for (j = i; j h; j--)
array[j] = array[j - 1];
array[h] = tmp;
}
}

#ifdef UNIT_TEST

#include <stdio.h>
#include <time.h>

/* Because of promotion to double, the dumper should work for many
integral and floating point types */
void dump_arr(etype array[], size_t size)
{
size_t index;
for (index = 0; index < size; index++)
printf("array[%u]=%f\n", (unsigned) index, (double)
array[index]);
}

void init_arr(etype array[], size_t size)
{
size_t index;
for (index = 0; index < size; index++)
array[index] = (etype) rand();
}
int main(void)
{
int array[8];
size_t arr_size = sizeof array / sizeof array[0];
srand((unsigned)time(NULL));

init_arr(array, arr_size);
puts("before sort:");
dump_arr(array, arr_size);
quadratic_sort_bi(array, 0, arr_size - 1);
puts("after sort:");
dump_arr(array, arr_size);

return 0;
}

#endif


2007年8月10日星期五22:40:46 -0700,vonbreslau写道:
On Fri, 10 Aug 2007 22:40:46 -0700, vonbreslau wrote:

你好C-landers,你能找到对这个

计划有什么改进或批评吗?


#include< stdio.h> ;

#define ZER 0
Hello C-landers, can you find any improvement or critique to this
program?

#include <stdio.h>
#define ZER 0



是否有任何理由使用三次击键而不是一次

想写0,考虑比1)编译器

本身(即解析预处理代码的那个)甚至不会意识到这一点,2)这并没有提高可读性

无论如何,以及3)它的价值永远不会改变?

Is there any reason for using three keystrokes instead than one
whenever you want to write 0, considering than 1) the compiler
proper (i.e. the one which parses preprocessed code) will not even
be aware of that, 2) this doesn''t improves readability in no way
whatsoever, and 3) its value will never change?


#define LIM 7

/ *根据其增加的大小排序数字* / void e(void); void

printar(void);

void pline(void);


int array [LIM];
#define LIM 7
/* Sort numbers according to its increasing size */ void e(void); void
printar(void);
void pline(void);

int array[LIM];



int array [LIM] = {99,0,55,18,2,67,0,9}

int array[LIM] = {99, 0, 55, 18, 2, 67, 0, 9}


int main()

{

int i,temporal,rounds;

rounds = ZER;

array [0] = 99;阵列[1] = 0;阵列[2] = 55;阵列[3] = 18; array [4] = 2;

array [5] = 67;阵列[6] = 0;阵列[7] = 9;
int main()
{
int i, temporal, rounds;
rounds = ZER;
array[0]=99; array[1]=0; array[2]=55; array[3]=18; array[4]=2;
array[5]=67; array[6]=0; array[7]=9;



您也可以在申报时初始化它,见上文。

You can also initialize it when you declare it, see above.


e(); printar(); E(); PLINE(); e();


while(rounds< =(LIM + 1)){

for(i = ZER; i< LIM; ++ i){


if(array [i] array [i + 1]){

printf("%2d%2d",array [ i],array [i + 1]);


temporal = array [i + 1];

array [i + 1] = array [i ];

array [i] = temporal;

printar(); e();

}
e(); printar(); e(); pline(); e();

while (rounds <= (LIM+1)) {
for ( i = ZER ;i < LIM; ++i) {

if (array[i] array[i+1]) {
printf("%2d %2d ",array[i], array[i+1]);

temporal = array[i+1];
array[i+1] = array[i];
array[i] = temporal;
printar(); e();
}



查找选择排序,比bubblesort快得多,它是

就像它一样简单。

Look up selection sort, is much faster than bubblesort and it is
about as simple as it.


}

++轮次;

}

pline(); E(); printar(); E(); e();

返回0;

}

void e(无效)

{

printf(" \ n");

}

void printar(void)
}
++rounds;
}
pline(); e(); printar(); e(); e();
return 0;
}
void e(void)
{
printf("\n");
}
void printar(void)



您可能希望将数组的地址和大小作为

参数传递。

You might want to pass the address and size of the array as
parameters.


{

int i;

for(i = ZER; i< = LIM; ++ i)

printf(" \t%2d",array [i]);

}

void pline(void)

{


printf(" ------------------------- -------------------------------------------------- ---");

}
{
int i;
for (i = ZER; i <= LIM; ++i)
printf("\t%2d ",array[i]);
}
void pline(void)
{

printf("------------------------------------------------------------------------------");
}



-

Army1987(替换NOSPAM与电子邮件)

没有人通过辞职来赢得比赛。 - S. Tartakower

--
Army1987 (Replace "NOSPAM" with "email")
No-one ever won a game by resigning. -- S. Tartakower


Army1987写道:
Army1987 wrote:

星期五,2007年8月10日22 :40:46 -0700,vonbreslau写道:
On Fri, 10 Aug 2007 22:40:46 -0700, vonbreslau wrote:

>你好C-landers,你能找到任何改进或批评这个
程序吗? />
#include< stdio.h>
#define ZER 0
>Hello C-landers, can you find any improvement or critique to this
program?

#include <stdio.h>
#define ZER 0



是否有任何理由使用三次击键而不是一次

每当你想写0时,考虑到1)编译器

正确(即解析预处理代码的那个)甚至不会是b $ b b意识到这一点,2)这并没有提高可读性

,3)它的价值永远不会改变?

Is there any reason for using three keystrokes instead than one
whenever you want to write 0, considering than 1) the compiler
proper (i.e. the one which parses preprocessed code) will not even
be aware of that, 2) this doesn''t improves readability in no way
whatsoever, and 3) its value will never change?


> #define LIM 7
/ *根据数量的增加对数字进行排序* / void e(void); void
printar(void);
void pline(void);

int array [LIM];
>#define LIM 7
/* Sort numbers according to its increasing size */ void e(void); void
printar(void);
void pline(void);

int array[LIM];



int array [LIM] = {99,0,55,18,2,67,0,9}

int array[LIM] = {99, 0, 55, 18, 2, 67, 0, 9}



你留下了分号并初始化了一个到多个项目。


以这种方式初始化数组的一个好处是编译器将

能够发现错误!


-

Ian Collins。

You left off the semicolon and initialised one to many items.

One good thing about initialising an array this way is the compiler will
be able to spot the error!

--
Ian Collins.


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

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