如何扫描 N 个字符串(使用结构),然后反向输出该字符串列表
How do I scan in an N number of strings (using structs) and then output that list of strings in reverse?
使用以下特定结构,
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;
}
相关文章:
- 在 c++ 中多次更改字符串列表
- C++未知长度字符串的数组,其行为类似于 Python 字符串列表
- 如何将包含括号的字符串列表平展为单个列表?
- 如何存储字符串列表
- 声明变量以保存字符串列表时的内存分配
- C 给出了一个字符串列表,如何从类中获取各个变量地址
- 合并两个字符串列表
- 寻找一种更快的方法来帮助减少/创建大量字符串列表
- 在字符串列表中查找字符串,然后将字符串设置为找到的字符串?
- 字符串列表中的部分搜索
- 如何在运行时显示QT中的字符串列表
- 如何扫描 N 个字符串(使用结构),然后反向输出该字符串列表
- c++搜索字符串列表中的特定字符串
- 从大向量中删除字符串列表的有效方法
- C++来比较两个字符串列表
- C++如何将用户输入与具有严格排序的字符串列表进行比较
- search用字符串列表输入数字(类似于t9字典)
- 如何在C++中从向量中读取字符串列表
- 是否有Qt对话框或小部件可以交互式地对字符串列表进行排序
- 将字符串列表插入 2D 矢量