c语言通过菜单选择按照工资卡号还是姓名浏览。如果按照卡号浏览,则显示的记录按照
时间: 2024-03-12 21:46:46 浏览: 16
按照工资卡号浏览记录时,可以使用冒泡排序(Bubble Sort)将记录按照工资卡号从小到大排序,然后再输出。
以下是一个简单的C语言代码示例,仅供参考:
```c
#include <stdio.h>
#include <string.h>
struct Employee {
char name[20];
int salary;
int card_number;
};
void sort_by_card_number(struct Employee e[], int n) {
int i, j;
struct Employee temp;
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (e[j].card_number > e[j+1].card_number) {
temp = e[j];
e[j] = e[j+1];
e[j+1] = temp;
}
}
}
}
void sort_by_name(struct Employee e[], int n) {
int i, j;
struct Employee temp;
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (strcmp(e[j].name, e[j+1].name) > 0) {
temp = e[j];
e[j] = e[j+1];
e[j+1] = temp;
}
}
}
}
void display(struct Employee e[], int n) {
int i;
for (i = 0; i < n; i++) {
printf("Employee name: %s\n", e[i].name);
printf("Employee salary: %d\n", e[i].salary);
printf("Employee card number: %d\n", e[i].card_number);
printf("\n");
}
}
int main() {
struct Employee e[100];
int n, i, choice;
printf("Enter number of employees: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Enter details of employee %d:\n", i+1);
printf("Name: ");
scanf("%s", e[i].name);
printf("Salary: ");
scanf("%d", &e[i].salary);
printf("Card number: ");
scanf("%d", &e[i].card_number);
}
printf("Enter 1 to sort by card number, 2 to sort by name: ");
scanf("%d", &choice);
if (choice == 1) {
sort_by_card_number(e, n);
} else {
sort_by_name(e, n);
}
printf("Sorted employee details:\n");
display(e, n);
return 0;
}
```
以上代码实现了按照工资卡号或姓名浏览员工记录的功能,并且按照工资卡号从小到大排序后输出。您可以根据实际需求进行扩展和修改。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)