C语言输入1.十个职工的姓名和职工号
时间: 2024-05-01 11:21:08 浏览: 14
可以使用数组和循环实现输入十个职工的姓名和职工号,示例代码如下:
```c
#include <stdio.h>
int main() {
char names[10][20]; // 存储十个职工的姓名,每个姓名最长不超过20个字符
int ids[10]; // 存储十个职工的职工号
// 循环输入十个职工的姓名和职工号
for (int i = 0; i < 10; i++) {
printf("请输入第%d个职工的姓名:", i + 1);
scanf("%s", names[i]); // 使用 %s 格式化字符输入职工姓名
printf("请输入第%d个职工的职工号:", i + 1);
scanf("%d", &ids[i]); // 使用 %d 格式化输入职工号
}
// 输出输入的十个职工的姓名和职工号
printf("\n输入的十个职工的姓名和职工号如下:\n");
for (int i = 0; i < 10; i++) {
printf("%s\t%d\n", names[i], ids[i]); // 使用 %s 和 %d 格式化输出职工姓名和职工号
}
return 0;
}
```
相关问题
C语言输入1.十个职工的姓名和职工号 2.按照职工号由大到小顺序排序,姓名顺序也随之调整; 3.要求输入一个职工号,用折半查找法找出该职工的姓名,从主函数输入要查找的职工号,输出该职工姓名
下面是一个可能的解决方案:
```c
#include <stdio.h>
#include <string.h>
#define MAX_NAME_LEN 20
#define MAX_EMPLOYEE_NUM 10
struct Employee {
char name[MAX_NAME_LEN];
int id;
};
void sort_employees(struct Employee employees[], int n);
int binary_search(const struct Employee employees[], int n, int target_id);
int main() {
struct Employee employees[MAX_EMPLOYEE_NUM];
int i, n, target_id, index;
printf("请输入%d个职工的姓名和职工号:\n", MAX_EMPLOYEE_NUM);
for (i = 0; i < MAX_EMPLOYEE_NUM; i++) {
printf("职工%d的姓名:", i + 1);
scanf("%s", employees[i].name);
printf("职工%d的职工号:", i + 1);
scanf("%d", &employees[i].id);
}
sort_employees(employees, MAX_EMPLOYEE_NUM);
printf("\n按照职工号排序的结果:\n");
for (i = 0; i < MAX_EMPLOYEE_NUM; i++) {
printf("%d\t%s\n", employees[i].id, employees[i].name);
}
printf("\n请输入要查找的职工号:");
scanf("%d", &target_id);
index = binary_search(employees, MAX_EMPLOYEE_NUM, target_id);
if (index == -1) {
printf("没有找到职工号为%d的职工\n", target_id);
} else {
printf("职工号为%d的职工是:%s\n", target_id, employees[index].name);
}
return 0;
}
void sort_employees(struct Employee employees[], int n) {
int i, j;
struct Employee temp;
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (employees[i].id < employees[j].id) {
temp = employees[i];
employees[i] = employees[j];
employees[j] = temp;
}
}
}
}
int binary_search(const struct Employee employees[], int n, int target_id) {
int low = 0, high = n - 1, mid;
while (low <= high) {
mid = (low + high) / 2;
if (employees[mid].id == target_id) {
return mid;
} else if (employees[mid].id < target_id) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return -1;
}
```
这个程序定义了一个 `Employee` 结构体,其中包含职工的姓名和职工号。首先用户需要输入十个职工的姓名和职工号,然后程序会按照职工号从大到小排序并输出结果。最后用户可以输入要查找的职工号,程序会使用折半查找法在已排序的员工数组中查找该职工号,并输出该职工的姓名。
c语言输入10个职工的姓名和职工号
### 回答1:
可以使用C语言中的结构体来实现。下面是一个示例代码:
```c
#include <stdio.h>
struct Employee {
char name[20];
int id;
};
int main() {
struct Employee employees[10];
printf("请输入10个职工的姓名和职工号:\n");
for (int i = 0; i < 10; i++) {
printf("请输入第%d个职工的姓名:", i+1);
scanf("%s", employees[i].name);
printf("请输入第%d个职工的职工号:", i+1);
scanf("%d", &employees[i].id);
}
printf("\n职工信息如下:\n");
for (int i = 0; i < 10; i++) {
printf("姓名:%s\t职工号:%d\n", employees[i].name, employees[i].id);
}
return 0;
}
```
运行程序后,就可以输入10个职工的姓名和职工号了。程序会将输入的信息存储在一个包含10个Employee结构体的数组中,然后依次输出所有职工的信息。
### 回答2:
在C语言中,我们可以使用数组和循环结构实现输入10个职工的姓名和职工号。下面是一个示例代码:
```c
#include <stdio.h>
#define SIZE 10 // 职工数量
int main() {
char names[SIZE][100]; // 姓名数组,每个姓名最多100个字符
int numbers[SIZE]; // 职工号数组
int i;
printf("请输入10个职工的姓名和职工号:\n");
for (i = 0; i < SIZE; i++) {
printf("请输入第%d个职工的姓名:", i + 1);
scanf("%s", names[i]);
printf("请输入第%d个职工的职工号:", i + 1);
scanf("%d", &numbers[i]);
}
printf("\n您输入的10个职工的姓名和职工号为:\n");
for (i = 0; i < SIZE; i++) {
printf("姓名:%s\t职工号:%d\n", names[i], numbers[i]);
}
return 0;
}
```
以上代码会在控制台依次读取10个职工的姓名和职工号,并将其存储在对应的数组中。最后,再通过循环遍历数组,将输入的姓名和职工号输出到控制台上。