这里我试图创建一个接受n个整数并将它们加在一起的函数......但是我在打印正确的序数时遇到了麻烦。我使用了错误的循环吗?
int i, count, sum, number;
sum = 0;
count = 0;
printf("Please indicate the number of integers:");
scanf("%d", &count);
for (i=0; i<count; i++){
printf("Please input the %dst number:", i+1);
scanf("%d", &number);
sum = sum + number;
}
printf("sum is %d\n", sum);
return 0;
例如,如果我输入count = 5,它将打印
Please input the 1st number:
Please input the 2st number:
Please input the 3st number:
Please input the 4st number:
Please input the 5st number:
sum is 15
它打印正确的总和,但我希望它为每一行打印正确的序号...例如,2nd..3rd。可能无需使用数组
答案 0 :(得分:2)
在英语中,序数规则相当简单(注意:不是&#34;简单,&#34;但是&#34;相当简单&#34;):
零以下,你自己。有些人可能会使用正数规则,有些人则认为尝试否定序数是没有意义的。
大多数序数以“&#39;
结尾以1,2,3结尾的数字不同:
除了以11,12,13结尾的数字是&#39;&#39;数字,因为英语对青少年的看法不同(六岁青少年与二十六岁)。所以:
一般情况下,只有书呆子才会认为&#34; zeroth&#34;很有趣。但它仍然是一个,所以它得到默认治疗。
在代码中:
if (n < 0) { /* Rule 1: good luck */ }
suffix = "th"; /* Rule 2: default = th */
if (1 <= n%10 && n%10 <= 3) { /* Rule 3: 1st, 2nd, 3rd */
if (n%100 < 10 || n%100 > 20) { /* Rule 4: 11th-13th */
suffix = (n%10 == 1) ? "st"
: (n%10 == 2) ? "nd"
: "rd"
;
}
}
答案 1 :(得分:1)
这可以通过隔离最后一个十进制数字来完成,例如42
将是42nd
。“青少年”是需要考虑的特殊情况,例如{{1} },而不是12th
。
12nd
请注意,“正确”程序应检查#include <stdio.h>
int main()
{
int i, count, sum, number, order, teens;
char *ordinal[] = {"st", "nd", "rd", "th" };
sum = 0;
count = 0;
printf("Please indicate the number of integers: ");
scanf("%d", &count);
for (i = 0; i < count; i++) {
teens = i % 100;
order = i % 10;
if(order > 3 || (teens >= 10 && teens < 20)) {
order = 3;
}
printf("Please input the %d%s number: ", i+1, ordinal[order]);
scanf("%d", &number);
sum = sum + number;
}
printf("sum is %d\n", sum);
}
的返回值。
答案 2 :(得分:1)
以下是使用序数数组的变体,然后只需if
和switch
来处理所有值(您可以调整处理数字&gt; 100)。
这个例程很简单。对于一切(值10-13除外)遵循正常的序数规则。因此,您只需设置一个if
块来处理奇数值,然后设置一个switch
,其余值为mod
,例如
修改: - 根据请求,您可以在get_ordinal
的顶部添加一项检查,通过连续减去100
,将大于0-99
的值缩放到等效的100
范围(并且您可以添加更多检查以优化大于1000的值等),例如
#include <stdio.h>
char *get_ordinal (char **ordinals, int value)
{
value %= 100; /* normalize values between 0-100 */
if (3 < value && value < 21)
return ordinals[3];
switch (value % 10) {
case 1 : return ordinals[0];
break;
case 2 : return ordinals[1];
break;
case 3 : return ordinals[2];
break;
default: return ordinals[3];
break;
}
}
int main (void) {
char *ordinals[] = { "st", "nd", "rd", "th" };
for (int i = 1; i < 30; i++)
printf ("Please enter the %d%s value:\n",
i, get_ordinal (ordinals, i));
return 0;
}
示例使用/输出
$ ./bin/getordinals
Please enter the 1st value:
Please enter the 2nd value:
Please enter the 3rd value:
Please enter the 4th value:
Please enter the 5th value:
Please enter the 6th value:
Please enter the 7th value:
Please enter the 8th value:
Please enter the 9th value:
Please enter the 10th value:
Please enter the 11th value:
Please enter the 12th value:
Please enter the 13th value:
Please enter the 14th value:
Please enter the 15th value:
Please enter the 16th value:
Please enter the 17th value:
Please enter the 18th value:
Please enter the 19th value:
Please enter the 20th value:
Please enter the 21st value:
Please enter the 22nd value:
Please enter the 23rd value:
Please enter the 24th value:
Please enter the 25th value:
Please enter the 26th value:
Please enter the 27th value:
Please enter the 28th value:
Please enter the 29th value:
对于大于100的值,例如
$ ./bin/getordinals
Please enter the 90th value:
Please enter the 91st value:
Please enter the 92nd value:
Please enter the 93rd value:
Please enter the 94th value:
Please enter the 95th value:
Please enter the 96th value:
Please enter the 97th value:
Please enter the 98th value:
Please enter the 99th value:
Please enter the 100th value:
Please enter the 101st value:
Please enter the 102nd value:
Please enter the 103rd value:
Please enter the 104th value:
Please enter the 105th value:
Please enter the 106th value:
Please enter the 107th value:
Please enter the 108th value:
Please enter the 109th value:
Please enter the 110th value:
Please enter the 111th value:
Please enter the 112th value:
Please enter the 113th value:
Please enter the 114th value:
Please enter the 115th value:
Please enter the 116th value:
Please enter the 117th value:
Please enter the 118th value:
Please enter the 119th value:
Please enter the 120th value:
Please enter the 121st value:
Please enter the 122nd value:
Please enter the 123rd value:
Please enter the 124th value:
Please enter the 125th value:
Please enter the 126th value:
Please enter the 127th value:
Please enter the 128th value:
Please enter the 129th value:
答案 3 :(得分:0)
那么你实际上必须实现更多if-else
条件。你应该:
for (i=0; i<count; i++){
if(i<100){
if(i+1==11 || i+1==12 || i+1==13){
printf("Please input the %dth number:", i+1);
scanf("%d", &number);
}
else if((i+1)%10 == 1){
printf("Please input the %dst number:", i+1);
scanf("%d", &number);
}
else if((i+1)%10 == 2){
printf("Please input the %dnd number:", i+1);
scanf("%d", &number);
}
else if((i+1)%10 == 3){
printf("Please input the %drd number:", i+1);
scanf("%d", &number);
}
else{
printf("Please input the %dth number:", i+1);
scanf("%d", &number);
}
}
sum = sum + number;
}
答案 4 :(得分:0)
这很复杂,因为没有直接的数学关系,因为序数后缀是自然语言发展的结果,而不是一致的数学关系。
基本上所有数字都以“th”为后缀,除非最低有效数字为1到3且数字不在每个百年的第二个十年(即“青少年”)。这可以被编码成一个函数,为任何正整数返回适当的后缀(负序数并没有多大意义):
#include <stdio.h>
char* ordinal_suffix( int i )
{
static const char* ord[] = { "st", "nd", "rd", "th" } ;
int ordidx = 3 ;
int imod100 = i % 100 ;
if( imod100 < 11 || imod100 > 13 )
{
ordidx = (i % 10) - 1 ;
if( ordidx > 3 || ordidx < 0 )
{
ordidx = 3 ;
}
}
return ord[ordidx] ;
}
int main()
{
for( int i = 1; i < 30; i++ )
{
printf( "%d%s\n", i, ordinal_suffix( i ) ) ;
}
for( int i = 99; i < 130; i++ )
{
printf( "%d%s\n", i, ordinal_suffix( i ) ) ;
}
return 0;
}
输出:
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
26th
27th
28th
29th
99th
100th
101st
102nd
103rd
104th
105th
106th
107th
108th
109th
110th
111th
112th
113th
114th
115th
116th
117th
118th
119th
120th
121st
122nd
123rd
124th
125th
126th
127th
128th
129th
答案 5 :(得分:-1)
请查看以下代码是否可以为您提供帮助。
#include <stdio.h>
#include <memory.h>
char str[64];
char *AddOrdinal(int num) {
sprintf(str, "%d", num);
switch (num % 100) {
case 11:
case 12:
case 13:
strcat(str, "th ");
return str;
}
switch (num % 10) {
case 1:
strcat(str, "st ");
return str;
case 2:
strcat(str, "nd ");
return str;
case 3:
strcat(str, "rd ");
return str;
default:
strcat(str, "th ");
return str;
}
}
int main(void) {
int i, count, sum, number;
sum = 0;
count = 0;
printf("Please indicate the number of integers:");
scanf("%d", &count);
for (i = 0; i < count; i++) {
printf("\nPlease input the %s number:", AddOrdinal(i + 1));
scanf("%d", &number);
sum = sum + number;
}
printf("\nsum is %d\n", sum);
return 0;
}
测试
Please indicate the number of integers:3
Please input the 1st number:2
Please input the 2nd number:3
Please input the 3rd number:4
sum is 9
Process finished with exit code 0