用C语言写一下
时间: 2023-08-16 12:09:11 浏览: 88
写给大家看的C语言
好的,以下是用C语言实现的代码示例:
```c
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 10
// 青年类
typedef struct {
char name[20];
int age;
char gender[10];
char profession[20];
int achievement;
} Youth;
// 哈希表节点
typedef struct {
char key[20];
Youth value;
} HashTableNode;
// 哈希表
HashTableNode hashTable[MAX_SIZE];
// 哈希函数
unsigned int hash(char *key) {
unsigned int hashVal = 0;
while (*key != '\0') {
hashVal = *key + 31 * hashVal;
key++;
}
return hashVal % MAX_SIZE;
}
// 添加青年信息
void addYouthInfo(char *name, int age, char *gender, char *profession, int achievement) {
unsigned int index = hash(name);
strcpy(hashTable[index].key, name);
strcpy(hashTable[index].value.name, name);
hashTable[index].value.age = age;
strcpy(hashTable[index].value.gender, gender);
strcpy(hashTable[index].value.profession, profession);
hashTable[index].value.achievement = achievement;
}
// 删除青年信息
void deleteYouthInfo(char *name) {
unsigned int index = hash(name);
if (strcmp(hashTable[index].key, name) == 0) {
hashTable[index].key[0] = '\0';
hashTable[index].value.name[0] = '\0';
}
}
// 查找青年信息
Youth *findYouthInfo(char *name) {
unsigned int index = hash(name);
if (strcmp(hashTable[index].key, name) == 0) {
return &hashTable[index].value;
} else {
return NULL;
}
}
// 更新青年信息
void updateYouthInfo(char *name, int age, char *gender, char *profession, int achievement) {
unsigned int index = hash(name);
if (strcmp(hashTable[index].key, name) == 0) {
hashTable[index].value.age = age;
strcpy(hashTable[index].value.gender, gender);
strcpy(hashTable[index].value.profession, profession);
hashTable[index].value.achievement = achievement;
}
}
// 排名青年信息
void rankYouthInfo() {
Youth topTen[10];
int i, j, k;
for (i = 0; i < 10; i++) {
topTen[i].achievement = -1;
}
for (i = 0; i < MAX_SIZE; i++) {
if (hashTable[i].key[0] != '\0') {
for (j = 0; j < 10; j++) {
if (hashTable[i].value.achievement > topTen[j].achievement) {
for (k = 9; k > j; k--) {
topTen[k] = topTen[k - 1];
}
topTen[j] = hashTable[i].value;
break;
}
}
}
}
printf("排名前十的青年信息:\n");
for (i = 0; i < 10; i++) {
printf("%d. %s, %d岁, %s, %s, 成就:%d\n", i + 1, topTen[i].name, topTen[i].age, topTen[i].gender, topTen[i].profession, topTen[i].achievement);
}
}
int main() {
// 添加青年信息
addYouthInfo("张三", 25, "男", "程序员", 100);
addYouthInfo("李四", 28, "男", "产品经理", 90);
addYouthInfo("王五", 23, "女", "市场营销", 95);
// 查找青年信息
Youth *youth = findYouthInfo("张三");
if (youth != NULL) {
printf("%s, %d岁, %s, %s, 成就:%d\n", youth->name, youth->age, youth->gender, youth->profession, youth->achievement);
}
// 更新青年信息
updateYouthInfo("张三", 26, "男", "架构师", 120);
youth = findYouthInfo("张三");
if (youth != NULL) {
printf("%s, %d岁, %s, %s, 成就:%d\n", youth->name, youth->age, youth->gender, youth->profession, youth->achievement);
}
// 删除青年信息
deleteYouthInfo("李四");
youth = findYouthInfo("李四");
if (youth == NULL) {
printf("李四的信息已被删除!\n");
}
// 排名青年信息
rankYouthInfo();
return 0;
}
```
以上是一个简单的实现过程,具体实现可能需要根据具体需求进行调整和优化。
阅读全文