gpt4 book ai didi

c - 使用循环打印结构

转载 作者:行者123 更新时间:2023-11-30 21:27:53 25 4
gpt4 key购买 nike

如何使用for-loop打印结构体内容?如果是指针,如何将指针分配给结构体?我附上了我制作的代码,该代码使用用户输入的数据创建一个结构,并以有序的方式打印出来。我的问题是您必须编写大量 printf()gets_s() 语句来接收和打印输入。我觉得使用 for-loop 来做到这一点会更容易。我尝试使用指针创建一个 for 循环,正如您在代码中看到的那样,但它无法编译,而且我很确定这是将指针分配给结构的错误方法。

TL;DR:如何使用for循环打印结构体内容?如果是指针,如何将指针分配给结构体?

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <conio.h>

#define maxName 50
#define maxNation 50
#define maxAge 100

struct astronaut
{
char firstName[maxName];
char lastName[maxName];
char nation[maxNation];
int age = 0;
char missionName[maxAge];
int missionYear[50];
};

int main()
{
int i = 0;
struct astronaut candidate1;
struct astronaut candidate2;
struct astronaut candidate3;
struct astronaut mission1;
struct astronaut mission2;
struct astronaut mission3;

printf("Please enter the first name of the candidate: ");
gets_s(candidate1.firstName);
printf("Please enter the last name of the candidate: ");
gets_s(candidate1.lastName);
printf("Please enter the nationality of the candidate: ");
gets_s(candidate1.nation);
printf("Please enter the age of the candidate: ");
scanf("%d", &candidate1.age);

printf("Please enter the first name of the candidate: ");
gets_s(candidate2.firstName);
printf("Please enter the last name of the candidate: ");
gets_s(candidate2.lastName);
printf("Please enter the nationality of the candidate: ");
gets_s(candidate2.nation);
printf("Please enter the age of the candidate: ");
scanf("%d", &candidate2.age);

printf("Please enter the first name of the candidate: ");
gets_s(candidate3.firstName);
printf("Please enter the last name of the candidate: ");
gets_s(candidate3.lastName);
printf("Please enter the nationality of the candidate: ");
gets_s(candidate3.nation);
printf("Please enter the age of the candidate: ");
scanf("%d", &candidate3.age);

printf("Enter a Mission Name: ");
gets_s(mission1.missionName);
printf("Enter the year the mission was conducted: ");
scanf("%d", &mission1.missionYear);

printf("Enter a Mission Name: ");
gets_s(mission2.missionName);
printf("Enter the year the mission was conducted: ");
scanf("%d", &mission2.missionYear);

printf("Enter a Mission Name: ");
gets_s(mission3.missionName);
printf("Enter the year the mission was conducted: ");
scanf("%d", &mission3.missionYear);

struct astronaut *ptr;
struct candidate *ptr;

// printf("\n\tAstronaut Candidate Database\n");
// printf("Name: %s %s \t Age: %d \t Nationality: %s\n", candidate1.firstName, candidate1.lastName, candidate1.age, candidate1.nation);
// printf("Name: %s %s \t Age: %d \t Nationality: %s\n", candidate2.firstName, candidate2.lastName, candidate2.age, candidate2.nation);
// printf("Name: %s %s \t Age: %d \t Nationality: %s\n", candidate3.firstName, candidate3.lastName, candidate3.age, candidate3.nation);
// printf("\n\tAstronaut Mission Database\n");
// printf("Mission Name: %s \t Year: %d\n", mission1.missionName, mission1.missionYear);
// printf("Mission Name: %s \t Year: %d\n", mission2.missionName, mission2.missionYear);
// printf("Mission Name: %s \t Year: %d\n", mission3.missionName, mission3.missionYear);

for (int index = 0; index < 5; index++)
{
printf("Name: %s %s \t Age: %d \t Nationality: %s\n", *ptr.firstName, *ptr.lastName, *ptr.age, *ptr.nation);
ptr++;
}

_getch();
return 0;
}

最佳答案

我认为你需要 2 个结构。 宇航员任务不是一回事。现在,您有几个未使用的字段,具体取决于您输入的是宇航员数据还是任务数据。你可以这样做:

#include <stdio.h>

#define maxName 50
#define maxNation 50
#define maxAge 100

struct astronaut
{
char firstName[maxName];
char lastName[maxName];
char nation[maxNation];
int age;
};

struct mission
{
char missionName[maxAge];
int missionYear; // why do you have an array of 50 ints for the missionYear?
};

void enter_candidate_data(struct candidate* cand)
{
// not famliar with gets_s .. I would use fgets here, you can read the manpage on that if you desire
printf("Please enter the first name of the candidate: ");
gets_s(cand->firstName);
printf("Please enter the last name of the candidate: ");
gets_s(cand->lastName);
printf("Please enter the nationality of the candidate: ");
gets_s(cand->nation);
printf("Please enter the age of the candidate: ");
scanf("%d", &(cand->age));
}

void enter_mission_data(struct mission* mis)
{
printf("Enter a Mission Name: ");
gets_s(mis->missionName);
printf("Enter the year the mission was conducted: ");
scanf("%d", &(mis->missionYear));
}

int main()
{
int i;
struct astronaut candidates[3];
struct mission missions[3];

for (i=0; i<3; i++)
{
enter_candidate_data(&(candidates[i]));
// you can put this in a separate loop if you want to enter all
// candidate data first
enter_mission_data(&(missions[i]));
}

// you could also write functions to print the data instead, depends on
// how you want it all presented

for (int i=0; i<3; i++)
{
printf("Name: %s %s \t Age: %d \t Nationality: %s\n",
candidates[i].firstName, candidates[i].lastName, candidates[i].nation);
printf("Mission Name: %s, year %d\n", missions[i].missionName,
missions[i].missionYear);
}

return 0;
}

您可能想要与特定宇航员相关的多个任务(甚至只是一个)。如果是这样,我会像这样定义数据结构

#define MAX_ASTRONAUT_MISSIONS 20

struct mission
{
char missionName[maxAge];
int missionYear;
};

struct astronaut
{
char firstName[maxName];
char lastName[maxName];
char nation[maxNation];
int age;
struct mission missions[MAX_ASTRONAUT_MISSIONS];
};

这将允许您将 MAX_ASTRONAUT_MISSION 任务与每位宇航员关联起来。或者,更现实地说,一项任务可能与多名宇航员相关。在这种情况下,您可能需要更像这样的数据结构

struct mission
{
char missionName[maxAge];
int missionYear;
};

struct astronaut
{
char firstName[maxName];
char lastName[maxName];
char nation[maxNation];
int age;
// using a pointer to missions will allow you to create one mission, and
// all the astronauts on that mission could get a pointer to it,
// designating they were all on that singular mission.
struct mission* missions[MAX_ASTRONAUT_MISSIONS];
};

关于c - 使用循环打印结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47626434/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com