“可接受的”是指“可接受的”。使用休息 [英] An "acceptable" use of break

查看:73
本文介绍了“可接受的”是指“可接受的”。使用休息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。我正在参加C和我的一个
任务中的第一个课程

i需要打印一个可能有0到100的数组

正整数(负整数用于停止输入),

,我必须每行打印8个整数。我工作的代码,

并且确实如此,但我感到有点不安,因为我使用

两次休息。我想听听有关这种方法的任何意见,并且

我可以做些什么来使代码更优雅/可读。谢谢

预付款。


/ *输出输入的数字。每行只打印8个数字* /

printf(输入的数字:\ n);

for(i = 0; i< MAX / 8; ++ i){

for(j = pos; j< pos + 8; ++ j){

if(list [j]> = 0)

printf("% - 6d",list [j]);

else

break;

}

if(list [j]< 0){

printf(" \ n");

break; < br $>
}

else {

printf(" \ n");

pos + = 8; < br $>
}

}


-Chris Potter

注意事项:如果你不喜欢介意,那没关系

解决方案

Chris Potter< ki ********** @ yahoo .COM>写道:

大家好。我正在接受我的第一个C课程和我的一项任务
我需要打印一个数组,其中可能有0到100个正整数的数组(负整数是用来停止输入),
我必须每行打印8个整数。我工作的代码,确实如此,但我觉得有点不安,因为我正在使用
两个休息时间。我想听听有关这种方法的任何意见,以及我可以做些什么来使代码更优雅/可读。谢谢
提前。
/ *输出输入的数字。每行仅打印8个数字* /
printf(输入的数字:\ n);
(i = 0; i< MAX / 8; ++ i){
for(j = pos; j< pos + 8; ++ j){
if(list [j]> = 0)
printf("% - 6d",list [ j]);

打破;
}
if(list [j]< 0){
printf(" \ n");
break;
}
else {
printf(" \ n");
pos + = 8;
}
}




怎么样


for(i = 0;(i< MAX)& ;&(list [i]> = 0); i ++)

{

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

if((i + 1)%8 == 0)

putchar(''\ n'');

}


if((i + 1)%8!= 0)

putchar(''\ n'');

Alex


Chris Potter写道:

大家好。我正在接受我的第一个C课程和我的一项任务
我需要打印一个数组,其中可能有0到100个正整数的数组(负整数是用来停止输入),
我必须每行打印8个整数。我工作的代码,确实如此,但我觉得有点不安,因为我正在使用
两个休息时间。




Just为了清楚地定义你的代码必须做什么:


*根据你的代码,零可以打印并且不会终止

列表(也就是说,你的问题定义中的正面应该是
''非负面'')。这是正确的吗?

*是否已经保证该列表最后会包含一个否定的

数字?在这种情况下,你的''list''数组应该是

能够容纳101个值,并且与MAX的比较是不必要的。

*应该在打印换行符时该列表包含零非负的

(或正数)元素?

*如果在非负数的情况下在最后打印换行符号
$列表中的b $ b(或正数)元素不能被8整除?

*当

非负数时,必须打印多少换行符列表中的(或正数)元素不为零,但

可被8整除?


这些练习中的很大一部分学习经验是

考虑这些边界情况,并确保这些正确处理
。如果你提供答案,我会尝试提出最小的

C程序,这可能是使用一个''for''循环和零

' 突破的。这样,对我来说这也是一个很好的运动:-)


祝你好运,


Sidney


Chris Potter写道:

大家好。我正在接受我的第一个C课程和我的一项任务
我需要打印一个数组,其中可能有0到100个正整数的数组(负整数是用来停止输入),
我必须每行打印8个整数。我工作的代码,确实如此,但我觉得有点不安,因为我正在使用
两个休息时间。我想听听有关这种方法的任何意见,以及我可以做些什么来使代码更优雅/可读。谢谢
advance。

/ *输出输入的数字。每行仅打印8个数字* /
printf(输入的数字:\ n);
(i = 0; i< MAX / 8; ++ i){
for(j = pos; j< pos + 8; ++ j){
if(list [j]> = 0)
printf("% - 6d",list [ j]);

打破;
}
if(list [j]< 0){
printf(" \ n");
break;
}
else {
printf(" \ n");
pos + = 8;
}
} < -Chris Potter
注意事项:如果你不介意,那就无所谓了



i'这也是C的新手,但是这段代码怎么样:

是可接受的,有效的......等等。 thanx。


#include< stdio.h>


#define MAX 101

main()

{

int list [MAX];

int counter = 0;

printf("输入数字来自0-100(-1或> 100)退出:");

执行{

scanf("%d",& list [counter] );;

} while((list [counter]> = 0)&&(list [counter]< = MAX-1)&&(counter ++< MAX)) ;


int j;

for(j = 0; j< MAX; j ++){

if(j%) 8 == 0)

putchar(''\ n'');

if(j> 0&& list [j]< = 0 || list [j]> MAX -1)

break;

printf("% - 6d",list [j]);

}

putchar(''\ n'');

}


Hello everyone. I am taking my first course in C and in one of my
assignments
i need to print out an array that could have anywhere from 0 to 100
positive integers in it (a negative integer is used to stop input),
and i have to print 8 integers per line. The code that i have works,
and does exactly that, but i feel a little uneasy because i am using
two breaks. I would like to hear any comments about this approach, and
what i might do to make the code more elegant/readable. Thanks in
advance.

/* output the numbers entered. only print 8 numbers per line */
printf ("Numbers Entered:\n");
for (i = 0; i < MAX/8; ++i) {
for (j = pos; j < pos + 8; ++j) {
if (list[j] >= 0)
printf ("%-6d ", list[j]);
else
break;
}
if (list[j] < 0) {
printf ("\n");
break;
}
else {
printf ("\n");
pos += 8;
}
}

-Chris Potter
"Mind over matter: if you don''t mind, then it doesn''t matter"

解决方案

Chris Potter <ki**********@yahoo.com> wrote:

Hello everyone. I am taking my first course in C and in one of my
assignments
i need to print out an array that could have anywhere from 0 to 100
positive integers in it (a negative integer is used to stop input),
and i have to print 8 integers per line. The code that i have works,
and does exactly that, but i feel a little uneasy because i am using
two breaks. I would like to hear any comments about this approach, and
what i might do to make the code more elegant/readable. Thanks in
advance. /* output the numbers entered. only print 8 numbers per line */
printf ("Numbers Entered:\n");
for (i = 0; i < MAX/8; ++i) {
for (j = pos; j < pos + 8; ++j) {
if (list[j] >= 0)
printf ("%-6d ", list[j]);
else
break;
}
if (list[j] < 0) {
printf ("\n");
break;
}
else {
printf ("\n");
pos += 8;
}
}



How about:

for(i = 0; (i < MAX) && (list[i] >= 0); i++)
{
printf("\t%d", list[i]);
if((i + 1) % 8 == 0)
putchar(''\n'');
}

if((i + 1) % 8 != 0)
putchar(''\n'');
Alex


Chris Potter wrote:

Hello everyone. I am taking my first course in C and in one of my
assignments
i need to print out an array that could have anywhere from 0 to 100
positive integers in it (a negative integer is used to stop input),
and i have to print 8 integers per line. The code that i have works,
and does exactly that, but i feel a little uneasy because i am using
two breaks.



Just to get a clear definition of what your code must do:

* according to your code, a zero can be printed and does not terminate
the list (i.e., the ''positive'' in your problem definition should read
''non-negative''). Is this correct?
* is it already guaranteed that the list will contain a negative
number at the end? In that case, your ''list'' array should be
able to hold 101 values, and the compare with MAX is unnecessary.
* should a newline be printed when the list contains zero non-negative
(or positive) elements?
* should a newline be printed at the end when the number of non-negative
(or positive) elements in your list is not divisible by eight?
* how many newlines must be printed at the end when the number of
non-negative (or positive) elements in your list is non-zero, but
divisible by eight?

A big part of the learning experience in exercises like these is
thinking about these boundary cases, and making sure these are properly
handled. If you provide answers, I''ll try to come up with the smallest
C program that does this, probably using one ''for'' loop and zero
''break''s. That way, it''s a nice exercise for me as well :-)

Best regards,

Sidney


Chris Potter wrote:

Hello everyone. I am taking my first course in C and in one of my
assignments
i need to print out an array that could have anywhere from 0 to 100
positive integers in it (a negative integer is used to stop input),
and i have to print 8 integers per line. The code that i have works,
and does exactly that, but i feel a little uneasy because i am using
two breaks. I would like to hear any comments about this approach, and
what i might do to make the code more elegant/readable. Thanks in
advance.

/* output the numbers entered. only print 8 numbers per line */
printf ("Numbers Entered:\n");
for (i = 0; i < MAX/8; ++i) {
for (j = pos; j < pos + 8; ++j) {
if (list[j] >= 0)
printf ("%-6d ", list[j]);
else
break;
}
if (list[j] < 0) {
printf ("\n");
break;
}
else {
printf ("\n");
pos += 8;
}
}

-Chris Potter
"Mind over matter: if you don''t mind, then it doesn''t matter"


i''m kind of new to C also, but how about this code:
is it acceptable, efficient... and so on. thanx.

#include <stdio.h>

#define MAX 101
main()
{
int list[MAX];
int counter = 0;
printf("Enter numbers from 0-100 (-1 or > 100) to exit: ");
do {
scanf("%d", &list[counter]);
}while((list[counter] >= 0)&&(list[counter]<=MAX-1)&&(counter++ < MAX));

int j;
for (j = 0; j < MAX; j++) {
if (j % 8 == 0)
putchar(''\n'');
if (j > 0 && list[j] <= 0 || list[j] > MAX -1)
break;
printf("%-6d", list[j]);
}
putchar(''\n'');
}


这篇关于“可接受的”是指“可接受的”。使用休息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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