如何扫描 N 个字符串(使用结构),然后反向输出该字符串列表

How do I scan in an N number of strings (using structs) and then output that list of strings in reverse?

本文关键字:字符串 列表 然后 输出 结构 何扫描 扫描      更新时间:2023-10-16

使用以下特定结构,

struct Student
{
    char first[50];
    char last[50];
    char id[20];
};

如何扫描由名字、姓氏和 ID 号组成的 N 个字符串,然后向后输出整个列表?

例如:

输入:

3
Dent Arthur 12345ABC
Prefect Ford 54321CBA
McMillan Tricia AB9876

输出:

McMillan Tricia AB9876
Prefect Ford 54321CBA
Dent Arthur 12345ABC  

这是我到目前为止所拥有的

#include <stdio.h>
#include <string.h>

struct Student
 {
    char first[50];
    char last[50];
    char id[20];
};

int main( )
{
    int N, i;
    scanf("%d", &N);
    struct Student NAME;
    for(i=0; i<N; i++){
        scanf("%s %s %s", NAME.first[i], NAME.last[i], NAME.id[i]);
    }
    /*struct Student prefect;
    scanf("%s %s %s", &prefect.first, &prefect.last, &prefect.id);

    struct Student mcmillan;
    scanf("%s %s %s", &mcmillan.first, &mcmillan.last, &mcmillan.id);*/
    printf("n");
    printf("%s %s %sn", NAME.first[i], NAME.last[i], NAME.id[i]);
    printf("%s %s %sn", NAME.first[i], NAME.last[i], NAME.id[i]);
    printf("%s %s %sn", NAME.first[i], NAME.last[i], NAME.id[i]);
    return 0;
}
如果要

以相反的方式打印列表(数组),

for(i=N-1; i>=0; i--){
    printf("%s %s %sn", NAME.first[i], NAME.last[i], NAME.id[i]);
}

这修复了反向问题,尽管代码中还有一个问题。您已将结构体的成员声明为 string 类型,并且在 main 函数中将它们视为字符串数组。这是行不通的。您可能希望有一个结构对象数组。这是如何去做的:

#include <stdio.h>
#include <string.h>
struct Student
{
char first[50];
char last[50];
char id[20];
};

int main( )
{
int N, i;
scanf("%d", &N);
struct Student NAME[10];
for(i=0; i<N; i++){
    scanf("%s %s %s", NAME[i].first, NAME[i].last, NAME[i].id);
}
/*struct Student prefect;
scanf("%s %s %s", &prefect.first, &prefect.last, &prefect.id);

struct Student mcmillan;
scanf("%s %s %s", &mcmillan.first, &mcmillan.last, &mcmillan.id);*/
printf("n");
 for(i=N-1; i>=0; i--){
printf("%s %s %sn", NAME[i].first, NAME[i].last, NAME[i].id);
}
return 0;
}

链接到 ideone: http://ideone.com/hgPjjn

suggest modifying the struct to include a 'previous' and 'next' pointers 
and putting the structs into a linked list.
perhaps by malloc'ing the struct instances, as each data becomes available.
linking the malloc'd struct onto the end of the linked list.
then to output,
walk down to the end of the list
then
printing current value,
step to previous list entry,
print value, etc
until back at the head pointer for the list.

could also insert each struct entry at the beginning of
the linked list, 
that would avoid having to walk down the list
to find the last struct, before being able to print 

您在从控制台跟踪学生时犯了一个错误。首先,要带所有学生,您必须创建一个struct Student数组 -

 struct Student students[100];  

然后你可以像这样从用户那里获取输入 -

for(i=0; i<N; i++){
            scanf("%s %s %s", students[i].first, students[i].last, students[i].id);
        }

在那之后,一切都保持不变。当我们编写完整的代码时,它将是 -

#include <stdio.h>
#include <string.h>
struct Student
 {
    char first[50];
    char last[50];
    char id[20];
};
int main( )
{
    int N, i;
    scanf("%d", &N);
    struct Student students[100];
    for(i=0; i<N; i++){
        scanf("%s %s %s", students[i].first, students[i].last, students[i].id);
   }
    printf("n");
    for(i=N-1; i>=0; i--){
     printf("%s %s %sn", students[i].first, students[i].last, students[i].id);
    }
    return 0;
}

这将起作用

#include <stdio.h>
#include <string.h>
struct Student {
    char first[50];
    char last[50];
    char id[20];
};
int main() {
    int N, i;
    scanf("%d", &N);
    struct Student NAME[N];
    for (i = 0; i < N; ++i)
        scanf("%s %s %s", NAME[i].first, NAME[i].last, NAME[i].id);
    printf("n");
    for (i = N - 1; i >= 0; --i)
        printf("%s %s %sn", NAME[i].first, NAME[i].last, NAME[i].id);
    return 0;
}