1)设计一个结构体struct strustudentInfo,内有元素int age, char name[16],设计信息录入子程序,该子程序实现功能如下: 提示用户: "Please input the name:" 接收用户的输入,保存到变量name 提示用户: "Please input the age:" 接收用户的输入,保存到变量age 将name,age的值保存到结构体的对应元素中,主函数通过调用该程序得到学生信息于结构体struct struStudentInfo中。 请写出子程序完整代码(该子程序被下面的_tmain函数调用)
时间: 2023-06-04 13:02:39 浏览: 85
下面是符合要求的子程序代码:
```
#include <stdio.h>
#include <string.h>
struct struStudentInfo {
int age;
char name[16];
};
void getInput(struct struStudentInfo *info) {
char name[16];
int age;
printf("Please input the name: ");
scanf("%s", name);
printf("Please input the age: ");
scanf("%d", &age);
strcpy(info->name, name);
info->age = age;
}
```
该子程序通过指针参数接收结构体指针,并通过提示和 scanf 函数获取用户的输入,并将输入值保存到结构体的相应元素中。主函数可以通过调用该子程序来实现输入学生信息的功能,如下所示:
```
int _tmain(int argc, _TCHAR* argv[])
{
struct struStudentInfo info;
// 获取学生信息
getInput(&info);
// 输出学生信息
printf("Name: %s\n", info.name);
printf("Age: %d\n", info.age);
return 0;
}
```
相关问题
编写程序,定义一个结构体类型,包含char name[20]成绩 int score子元素,定义10个元素的结构体数组,输入10个同学的信息,按成绩由大到小排序
可以使用以下代码定义一个包含 `char name[20]` 和 `int score` 成员的结构体类型,并定义一个包含 10 个元素的结构体数组。
```c
#include <stdio.h>
#include <string.h>
struct Student {
char name[20];
int score;
};
int main() {
struct Student students[10];
int i, j, max_idx;
struct Student temp;
// 输入学生信息
for (i = 0; i < 10; i++) {
printf("请输入第 %d 个学生的姓名和成绩:", i+1);
scanf("%s %d", students[i].name, &students[i].score);
}
// 按成绩由大到小排序
for (i = 0; i < 10; i++) {
max_idx = i;
for (j = i + 1; j < 10; j++) {
if (students[j].score > students[max_idx].score) {
max_idx = j;
}
}
if (max_idx != i) {
temp = students[i];
students[i] = students[max_idx];
students[max_idx] = temp;
}
}
// 输出排序后的学生信息
printf("按成绩由大到小排序后的学生信息:\n");
for (i = 0; i < 10; i++) {
printf("%s %d\n", students[i].name, students[i].score);
}
return 0;
}
```
在这个程序中,我们使用了选择排序算法,逐步找到数组中最大元素的下标,然后将其移动到正确的位置。最后,按顺序输出排序后的学生信息。
(1)建立二叉树,节点的结构体为: typedef struct { int num; /学号 char name[10]; /姓名 } student; (2)完成前、中、后二叉树非递归遍历程序; (3)从上至下、从左至右层次遍历程序;从上至下、从右至左层次遍历程序;
以下是基于C语言的实现,包括节点结构体定义、二叉树的创建、前、中、后序非递归遍历以及从上至下、从左至右层次遍历程序和从上至下、从右至左层次遍历程序。
```
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int num; // 学号
char name[10]; // 姓名
struct node* left; // 左子树
struct node* right; // 右子树
} TreeNode, *TreeNodePtr;
// 创建二叉树
TreeNodePtr createBinaryTree() {
TreeNodePtr root = NULL, current = NULL;
int num;
char name[10];
printf("请输入根节点的学号和姓名:");
scanf("%d%s", &num, name);
if (num == 0) {
return root;
}
root = (TreeNodePtr)malloc(sizeof(TreeNode));
root->num = num;
strcpy(root->name, name);
root->left = NULL;
root->right = NULL;
current = root;
while (1) {
printf("请输入要插入节点的学号和姓名:");
scanf("%d%s", &num, name);
if (num == 0) {
break;
}
TreeNodePtr node = (TreeNodePtr)malloc(sizeof(TreeNode));
node->num = num;
strcpy(node->name, name);
node->left = NULL;
node->right = NULL;
while (1) {
if (num < current->num) {
if (current->left == NULL) {
current->left = node;
break;
}
else {
current = current->left;
}
}
else {
if (current->right == NULL) {
current->right = node;
break;
}
else {
current = current->right;
}
}
}
current = root;
}
return root;
}
// 前序非递归遍历
void preOrder(TreeNodePtr root) {
if (root == NULL) {
return;
}
TreeNodePtr stack[100];
int top = -1;
stack[++top] = root;
while (top >= 0) {
TreeNodePtr node = stack[top--];
printf("%d %s ", node->num, node->name);
if (node->right != NULL) {
stack[++top] = node->right;
}
if (node->left != NULL) {
stack[++top] = node->left;
}
}
}
// 中序非递归遍历
void inOrder(TreeNodePtr root) {
TreeNodePtr stack[100];
int top = -1;
TreeNodePtr node = root;
while (top >= 0 || node != NULL) {
while (node != NULL) {
stack[++top] = node;
node = node->left;
}
if (top >= 0) {
node = stack[top--];
printf("%d %s ", node->num, node->name);
node = node->right;
}
}
}
// 后序非递归遍历
void postOrder(TreeNodePtr root) {
if (root == NULL) {
return;
}
TreeNodePtr stack1[100], stack2[100];
int top1 = -1, top2 = -1;
stack1[++top1] = root;
while (top1 >= 0) {
TreeNodePtr node = stack1[top1--];
stack2[++top2] = node;
if (node->left != NULL) {
stack1[++top1] = node->left;
}
if (node->right != NULL) {
stack1[++top1] = node->right;
}
}
while (top2 >= 0) {
TreeNodePtr node = stack2[top2--];
printf("%d %s ", node->num, node->name);
}
}
// 层次遍历
void levelOrder(TreeNodePtr root) {
if (root == NULL) {
return;
}
TreeNodePtr queue[100];
int front = -1, rear = -1;
queue[++rear] = root;
while (front != rear) {
TreeNodePtr node = queue[++front];
printf("%d %s ", node->num, node->name);
if (node->left != NULL) {
queue[++rear] = node->left;
}
if (node->right != NULL) {
queue[++rear] = node->right;
}
}
}
// 从上至下、从左至右层次遍历
void levelOrderLeftToRight(TreeNodePtr root) {
if (root == NULL) {
return;
}
TreeNodePtr queue[100];
int front = -1, rear = -1;
queue[++rear] = root;
while (front != rear) {
int count = rear - front;
for (int i = 0; i < count; i++) {
TreeNodePtr node = queue[++front];
printf("%d %s ", node->num, node->name);
if (node->left != NULL) {
queue[++rear] = node->left;
}
if (node->right != NULL) {
queue[++rear] = node->right;
}
}
printf("\n");
}
}
// 从上至下、从右至左层次遍历
void levelOrderRightToLeft(TreeNodePtr root) {
if (root == NULL) {
return;
}
TreeNodePtr queue[100];
int front = -1, rear = -1;
queue[++rear] = root;
while (front != rear) {
int count = rear - front;
for (int i = 0; i < count; i++) {
TreeNodePtr node = queue[++front];
if (node->left != NULL) {
queue[++rear] = node->left;
}
if (node->right != NULL) {
queue[++rear] = node->right;
}
}
for (int i = 0; i < count; i++) {
TreeNodePtr node = queue[front + i + 1];
printf("%d %s ", node->num, node->name);
}
printf("\n");
}
}
int main() {
TreeNodePtr root = createBinaryTree();
printf("前序遍历结果:");
preOrder(root);
printf("\n中序遍历结果:");
inOrder(root);
printf("\n后序遍历结果:");
postOrder(root);
printf("\n层次遍历结果:");
levelOrder(root);
printf("\n从上至下、从左至右层次遍历结果:\n");
levelOrderLeftToRight(root);
printf("从上至下、从右至左层次遍历结果:\n");
levelOrderRightToLeft(root);
return 0;
}
```
阅读全文