C 打印序数
在这里,我试图创建一个函数,它接受n个整数,并将它们相加。。。但我很难让它打印正确的序号。我是否使用了错误的循环C 打印序数,c,C,在这里,我试图创建一个函数,它接受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 numbe
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;
它打印正确的和,但我希望它打印每行的正确序号。。例如,第二……第三。可能不必使用数组那么实际上,您必须实现更多的
,否则
条件。你应该:
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;
}
对于(i=0;i请查看以下代码是否可以帮助您
#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;
}
在英语中,序数的规则相当简单(注意:不是“简单”,而是“相当简单”):
零下,你只能靠自己了。有些人可能会应用正数规则,另一些人说尝试负序数没有意义
大多数序数以“th”结尾:
- 第四
- 三十九
以1、2、3结尾的数字不同:
- 首先
- 二十二
- 一百八十三
除了以11、12、13结尾的数字是“th”数字,因为英语对待青少年的方式不同(6个青少年对26个)。因此:
- 第十一
- 一百一十二
- 十三
一般来说,只有书呆子才会认为“第零个”很有趣。但它仍然是第十个,所以它会得到默认的待遇
代码:
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"
;
}
}
if(n<0){/*规则1:祝你好运*/}
后缀=“th”/*规则2:默认值=th*/
如果(1这可以通过隔离最后一个十进制数字来实现,例如,42
将是42nd
。“青少年”是需要考虑的特殊情况,例如12nd
,而不是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);
}
#包括
int main()
{
整数i、计数、总和、数字、顺序、小数;
字符*序数[]={“st”、“nd”、“rd”、“th”};
总和=0;
计数=0;
printf(“请指出整数的数目:”);
scanf(“%d”、&count);
对于(i=0;i3 | |(十几岁>=10岁&十几岁<20岁)){
顺序=3;
}
printf(“请输入%d%s编号:”,i+1,序号[订单]);
scanf(“%d”和编号);
总和=总和+数字;
}
printf(“总和为%d\n”,总和);
}
请注意,“适当”的程序应该检查来自scanf
的返回值。这很复杂,因为没有直接的数学关系,因为顺序后缀是自然语言发展的结果,而不是一致的数学关系
基本上,所有数字的后缀都是“th”,除非最低有效数字是1到3,并且数字不在每个百年纪念的第二个十年(即“十几岁”)。这可以被编码到一个函数中,为任何正整数返回适当的后缀(负序数实际上没有多大意义):
下面是一个变体,它使用一个序数数组,然后使用一个简单的if
和开关来处理所有值(您可以调整为处理数字>100)
该例程相当简单。对于所有内容(值10-13除外)都遵循正常的顺序规则。因此,您只需设置一个if
块来处理奇怪的值,然后设置一个开关,该开关带有剩余值的mod
,例如
编辑:-对于每个请求,您可以在get_ordinal
顶部添加一个检查,通过依次减去100
将大于100
的值缩放到其等效的0-99
范围(您可以添加更多检查以优化大于1000的值,等等),例如
对于大于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:
您输入了什么值?以及scanf()
返回了什么?答案的可能副本是用C#写的,但将其移植到C.或者简单地将其移植到char*ordinals[]={“st”、“nd”、“rd”、“th”}应该很简单;
然后简单地使用number%place
创建正确序号的索引。@Groo:如果语言不同(即使熟悉两种语言的人可以很容易地修改C#解决方案),这也不是重复的,当然不需要进行密切投票。当输入为42时会发生什么?那么“42次”代码将变得非常庞大。实际上不会,我应该重新编写。您可能应该使用一个函数对其进行泛化,以便像其他答案一样为特定整数返回适当的后缀。在我的测试中,它为除1、2、101、102等之外的所有整数生成rd
后缀。听起来像是您颠倒了一个条件或其他什么。我的错误-抱歉;我将您的代码复制并粘贴到我的答案中的ordinal_suffix()
函数中,并返回suffix
。然而,我将suffix
设置为静态,并在每次调用中都将其初始化为“th”。效果很好。AddOrdinal()
危险地返回指向非静态局部变量的指针。str[12]
对于最长的32位有符号整数来说太短,int
在任何情况下都可能大于32位。否则它就可以工作。这是一个错误的尝试。C#中的字符串是不可变的、堆分配的、垃圾收集的对象。此函数所做的是返回一个指向基于堆栈的数组的指针,该数组超出了范围呃,函数已返回。这是未定义的行为,因此,如果此操作在您的计算机上运行,则它是意外运行的。@Clifford我试图根据您的注释更正代码。@Groo请查看是否更喜欢我的更新答案。现在它不是线程安全的,并且使用全局(名称不正确)。例如,121st如何?将序数
数组与get_ordinal()
分离不是很有凝聚力的设计。序数的责任和知识在main()
和get_ordinal()
之间分配。您只需在函数顶部添加一个重复的10除法,以缩放任意数字y
#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);
}
#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
#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:
$ ./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: