使用下面的特定结构
struct Student
{
char first[50];
char last[50];
char id[20];
};
如何扫描N个字符串,包括名字,姓氏和ID号,然后向后输出整个列表?
例如:
输入:
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 %s\n", NAME.first[i], NAME.last[i], NAME.id[i]);
printf("%s %s %s\n", NAME.first[i], NAME.last[i], NAME.id[i]);
printf("%s %s %s\n", NAME.first[i], NAME.last[i], NAME.id[i]);
return 0;
}
答案 0 :(得分:3)
如果要以相反的方式打印列表(数组),
for(i=N-1; i>=0; i--){
printf("%s %s %s\n", NAME.first[i], NAME.last[i], NAME.id[i]);
}
这解决了逆转问题,尽管代码中还有一个问题。您已声明struct的成员为string
类型,并且在main
函数中,您将它们视为字符串数组。这不会奏效。您可能想要一个struct对象数组。这是怎么回事:
#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 %s\n", NAME[i].first, NAME[i].last, NAME[i].id);
}
return 0;
}
指向ideone的链接:http://ideone.com/hgPjjn
答案 1 :(得分:2)
从控制台攻击学生时,你犯了一点错误。首先,为了让所有学生都必须创建一个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 %s\n", students[i].first, students[i].last, students[i].id);
}
return 0;
}
答案 2 :(得分:2)
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
答案 3 :(得分:1)
这将有效
#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 %s\n", NAME[i].first, NAME[i].last, NAME[i].id);
return 0;
}