%if if(),confused(help) [英] % inside if(), confused (help)

查看:61
本文介绍了%if if(),confused(help)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



请查看以下代码:

static char * text [] = {" th"," st"," nd"," ; rd"};

char * ordinal_text(int number)

{


if((number = 100) > 9&& number< 20)||(number%= 10)> 3)

number = 0;

return text [number];

}


#include< stdio.h>

int main(无效)

{

int i;

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

printf("%d% s \ n",i,ordinal_text(i));

返回0;

}

根据上面的代码,输出是如下:


0th

1st

2nd

3rd

4

5

6th

7th

8th

9th
10th

11th

12th

13th

14th

15日

16th

17th

18th

19th

2 0th

21

22nd

23th

24th

25th


这正是预期的结果。但是不明白输出。怎么来?

输出按预期发生。


if(((number%= 100)> 9&&数字< 20)|| (编号%= 10)> 3)


数字应该小于20但大于9.但是数组迭代中每个

元素的打印方式是相应的。任何人都可以帮助我了解这个简单的程序吗?谢谢。


-

组合是国际象棋的核心


A.Alekhine


邮寄地址:

sathyashrayan at gmail DOT com

解决方案

我觉得你在这里感到困惑。这段代码没问题。在此代码中

有两个条件。


首先是(数字%= 100)> 9&&数字< 20

- 在这种情况下,他们会照顾11,12,13 ......

和第二个必要的条件。


我想你得到了答案。


- Puneet


文章< 36 *************@individual.net> ;,

sathyashrayan< lo **************** @ nomail。 COM>写道:

:if(((number%= 100)> 9&& number< 20)||(number%= 10)> 3)

:number = 0;


该代码似乎违反了ANSI标准。 X3.159-1989第3.3节:


在上一个和下一个序列点之间,一个对象应该通过评估最多修改一次其存储值<
br />
表达式。此外,先前的值应为

只能访问确定的存储空间。


并且根据3.6,表达控制''if' '声明

是一个完整表达。它后面有一个序列点,但

附加到3.3节的注释使得它很清楚,例如

在完整表达式中没有任何序列点。

-

沃霍尔定律:每个Usenet用户都有权获得他或她自己的

十五分钟的火焰 - Squoire




sathyashrayan写道:

请查看以下代码:

static char * text [] = {" th,st,nd,rd};
char * ordinal_text(int number)
{

if(((number%= 100)> 9&& number< 20)||(number%= 10)>
3)number = 0;
返回文本[编号];
}
#include< stdio.h>
int main(void)
{
int i ;
for(i = 0; i< 26; ++ i)
printf("%d%s \ n,i ,ordinal_text(i));
返回0;
}

根据上面的代码,输出如下:

0th > 1st
2nd
3rd
4th
5th
6th
7th
8th
9th
10th > 11th
12th
13th
14th
15th
16th
17th
18th
19th
20th > 21
22th
23th
24th

这究竟是什么预期。但是不懂输出。如何
输出按预期发生。

if(((number = 100)> 9&&数字< 20)|| (编号%= 10)> 3)

数字应该小于20但大于9.但是数组迭代中每个元素的
如何相应地打印。任何一个
可以帮助我理解这个简单的程序吗?谢谢。

-
组合是国际象棋的核心

A.Alekhine

邮寄地址: sathyashrayan AT gmail DOT com



if(((number%= 100)> 9&& number< 20)||(number%= 10)> 3 )

number = 0;


''if''条件检查数字是否符合th条件。后缀。

满足的数字是4(第四,第十四,第二十四等)

到9(第九)每10个数字块。最后一位数字1,

2和三位有特殊后缀。但是从11(第11个)

到19(第19个)的数字是这个规则的一个例外。


所以''if''循环检查是否:

1.单位数字是否大于3(如果是这样,请附上''th''前缀

设置数= 0。)

2. OR是9到20之间的单位数字(再次选择''th''前缀)

3.如果上述两个条件都不正确,那么后缀应该是

由(number = number%10)的结果表示。


例如:

如果你通过21,那么前两个条件不满意。但是当执行''if''循环次数=数字%10时,执行了

,其中
将number的值更改为1.因此该方法返回1. printf

只需将数字与数组中的文本附加位置+

(sizeof(element)+ 1)即'st''。因此打印''21''。


这有帮助吗?


- EnTee



Please look at the following code:
static char *text[] = {"th", "st", "nd", "rd"};
char *ordinal_text(int number)
{

if (((number %= 100) > 9 && number < 20) || (number %= 10) > 3)
number = 0;
return text[number];
}

#include <stdio.h>
int main(void)
{
int i;
for (i = 0; i < 26; ++i)
printf("%d%s\n", i, ordinal_text(i));
return 0;
}
According the code above the out put is as follows:

0th
1st
2nd
3rd
4th
5th
6th
7th
8th
9th
10th
11th
12th
13th
14th
15th
16th
17th
18th
19th
20th
21st
22nd
23rd
24th
25th

Which is exactly what is expected.But don''t understand output.How come
the output occurred as expected.

if (((number %= 100) > 9 && number < 20) || (number %= 10) > 3)

The number should be less than 20 but greater than 9. But how each
element in the iteration of array prints accordingly. Can any one help
me to understand this simple program? Thanks.

--
"combination is the heart of chess"

A.Alekhine

Mail to:
sathyashrayan AT gmail DOT com

解决方案

I think you are getting confused here. This code is ok. In this code
there is two conditions.

first is (number %= 100) > 9 && number < 20
-- in this condition they are caring for numbers like 11,12, 13 ...
and second condition ofcourse necessary .

I think you got the answer .

-- Puneet


In article <36*************@individual.net>,
sathyashrayan <lo****************@nomail.com> wrote:
: if (((number %= 100) > 9 && number < 20) || (number %= 10) > 3)
: number = 0;

That code appears to violate ANSI standards. X3.159-1989 section 3.3:

Between the previous and next sequence points an object shall
have its stored value modified at most once by the evaluation
of an expression. Furthermore, the prior value shall be
accessed only to determined teh avlaue to be stored.

and according to 3.6, the expression controlling an ''if'' statement
is a "full expression" that has a sequence point after it, but
the note attached to section 3.3 makes it fairly clear by example
that within a full expression there aren''t any sequence points.
--
Warhol''s Law: every Usenet user is entitled to his or her very own
fifteen minutes of flame -- The Squoire



sathyashrayan wrote:

Please look at the following code:
static char *text[] = {"th", "st", "nd", "rd"};
char *ordinal_text(int number)
{

if (((number %= 100) > 9 && number < 20) || (number %= 10) > 3) number = 0;
return text[number];
}

#include <stdio.h>
int main(void)
{
int i;
for (i = 0; i < 26; ++i)
printf("%d%s\n", i, ordinal_text(i));
return 0;
}
According the code above the out put is as follows:

0th
1st
2nd
3rd
4th
5th
6th
7th
8th
9th
10th
11th
12th
13th
14th
15th
16th
17th
18th
19th
20th
21st
22nd
23rd
24th
25th

Which is exactly what is expected.But don''t understand output.How come the output occurred as expected.

if (((number %= 100) > 9 && number < 20) || (number %= 10) > 3)

The number should be less than 20 but greater than 9. But how each element in the iteration of array prints accordingly. Can any one help me to understand this simple program? Thanks.

--
"combination is the heart of chess"

A.Alekhine

Mail to:
sathyashrayan AT gmail DOT com


if (((number %= 100) > 9 && number < 20) || (number %= 10) > 3)
number = 0;

The ''if'' condition checks if the number qualifies for a "th" suffix.
The numbers which satify are 4 (fourth, fourteenth, twenty-fourth etc)
to 9 (ninth) in every block of 10 numbers. Numbers with last digits 1,
2 and three have special suffixes. However numbers from 11 (eleventh)
to 19 (nineteenth) are an exception to this rule.

So the ''if'' loops checks whether:
1. Is the units-digit greater than 3 (if so attach a ''th'' prefix by
setting number = 0.)
2. OR Is the units-digit between 9 and 20 (again chose the ''th'' prefix)
3. If both above conditions are not true, then suffix should be the one
indicated by result of (number = number %10).

For Example:
If you pass 21, then first two conditions are not satisfied. But while
executing the ''if'' loop number = number % 10 got executed, which
changed the value of number to 1. So the method returns 1. The printf
simply appends number with the text in the array at location base +
(sizeof(element) + 1) which is ''st''. Therefore ''21st'' is printed.

Does this help?

- EnTee


这篇关于%if if(),confused(help)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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