用c语言的结构体编写简单程序
时间: 2023-10-26 11:06:22 浏览: 42
好的,以下是一个用 C 语言的结构体编写的简单程序,演示了如何定义结构体、声明结构体变量、给结构体成员赋值以及输出结构体成员的值:
```c
#include <stdio.h>
// 定义一个结构体
struct Person {
char name[20];
int age;
float height;
};
int main() {
// 声明一个结构体变量
struct Person person1;
// 给结构体成员赋值
strcpy(person1.name, "Tom");
person1.age = 20;
person1.height = 1.75;
// 输出结构体成员的值
printf("Name: %s\n", person1.name);
printf("Age: %d\n", person1.age);
printf("Height: %.2f\n", person1.height);
return 0;
}
```
输出结果为:
```
Name: Tom
Age: 20
Height: 1.75
```
相关问题
用c语言结构体编写孔融让梨程序
以下是用C语言结构体编写的孔融让梨程序:
```c
#include <stdio.h>
typedef struct {
int value; // 剩余资源数
struct process *L; // 等待队列
} semaphore;
typedef struct process {
int id; // 进程ID
struct process *next; // 下一个进程
} process;
void wait(semaphore *S, int id) {
S->value--;
if (S->value < 0) {
process *newProcess = (process *)malloc(sizeof(process));
newProcess->id = id;
newProcess->next = NULL;
if (S->L == NULL) {
S->L = newProcess;
} else {
process *current = S->L;
while (current->next != NULL) {
current = current->next;
}
current->next = newProcess;
}
}
}
void signal(semaphore *S) {
S->value++;
if (S->value <= 0) {
process *releasedProcess = S->L;
S->L = S->L->next;
free(releasedProcess);
}
}
int main() {
semaphore S;
S.value = 1;
S.L = NULL;
// 创建5个进程
process *p1 = (process *)malloc(sizeof(process));
p1->id = 1;
p1->next = NULL;
process *p2 = (process *)malloc(sizeof(process));
p2->id = 2;
p2->next = NULL;
process *p3 = (process *)malloc(sizeof(process));
p3->id = 3;
p3->next = NULL;
process *p4 = (process *)malloc(sizeof(process));
p4->id = 4;
p4->next = NULL;
process *p5 = (process *)malloc(sizeof(process));
p5->id = 5;
p5->next = NULL;
// 进程申请资源
wait(&S, p1->id);
wait(&S, p2->id);
wait(&S, p3->id);
wait(&S, p4->id);
wait(&S, p5->id);
// 输出等待队列中的进程ID
process *current = S.L;
while (current != NULL) {
printf("Process ID: %d\n", current->id);
current = current->next;
}
// 释放资源
signal(&S);
signal(&S);
signal(&S);
signal(&S);
signal(&S);
return 0;
}
```
用C语言结构体编写程序实现二叉排序树查找
下面是一个用C语言结构体编写的二叉排序树查找程序:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义二叉排序树的结构体
struct BSTNode {
int data;
struct BSTNode *left;
struct BSTNode *right;
};
// 插入节点
struct BSTNode* insert(struct BSTNode *node, int data) {
// 如果当前节点为空,创建一个新节点
if (node == NULL) {
struct BSTNode *temp = (struct BSTNode*) malloc(sizeof(struct BSTNode));
temp->data = data;
temp->left = NULL;
temp->right = NULL;
return temp;
}
// 如果当前节点不为空,比较插入值和当前节点的值
if (data <= node->data) {
node->left = insert(node->left, data);
} else {
node->right = insert(node->right, data);
}
return node;
}
// 查找节点
struct BSTNode* search(struct BSTNode *node, int data) {
// 如果当前节点为空或者当前节点的值就是要查找的值,返回当前节点
if (node == NULL || node->data == data) {
return node;
}
// 如果要查找的值小于当前节点的值,递归查找左子树
if (data < node->data) {
return search(node->left, data);
}
// 如果要查找的值大于当前节点的值,递归查找右子树
return search(node->right, data);
}
int main() {
struct BSTNode *root = NULL;
int n, i, x;
printf("请输入节点个数:");
scanf("%d", &n);
printf("请输入节点值:");
for (i = 0; i < n; ++i) {
scanf("%d", &x);
root = insert(root, x);
}
printf("请输入要查找的值:");
scanf("%d", &x);
struct BSTNode *result = search(root, x);
if (result == NULL) {
printf("未找到该节点\n");
} else {
printf("找到该节点,值为 %d\n", result->data);
}
return 0;
}
```
程序中定义了一个`BSTNode`结构体,其中包含了节点的值、左子树指针和右子树指针。`insert`函数实现了插入节点的功能,`search`函数实现了查找节点的功能。在`main`函数中,首先输入节点个数和节点值,然后输入要查找的值,最后调用`search`函数查找节点并输出结果。