类似于这种DS的题蛮多的,写了一个模板,没准以后用得到
具体要求:
编完整程序,包括职工信息,定义(职工号,姓名,性别,年龄,工龄)
1.输入职工人数,5项信息
2.统计年龄超过60岁的人数
3.查找(输职工号,输出员工)
4.排序(按工龄降序排序)
5.输出所有职工信息
程序输出结果:
源代码:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 5 //职工人数
struct staff {
int id; //职工号
char name[1024]; //姓名
char sex[100]; //性别
int age; //年龄
int workage; //工龄
};
void input(struct staff* p) { //输入职工信息
struct staff* q = p;
for (int i = 1; p < q + N; ++i, ++p) {
printf("请输入第%d个职工的职工号、姓名、性别、年龄、工龄(按顺序,用空格隔开)\n", i);
scanf("%d%s%s%d%d", &p->id, p->name, p->sex, &p->age, &p->workage);
}
}
void sort(struct staff* p) {
char temp[1024];
int t;
struct staff* q = p;
for (; p < q + N; ++p) {
for (struct staff* h = p + 1; h < q + N; ++h) {
if (p->workage > h->workage) {
p->workage = p->workage ^ h->workage;
h->workage = p->workage ^ h->workage;
p->workage = p->workage ^ h->workage;
//交换名字
strcpy(temp, p->name);
strcpy(p->name, h->name);
strcpy(h->name, temp);
//交换职工号
t = p->id;
p->id = h->id;
h->id = t;
//交换性别
strcpy(temp, p->sex);
strcpy(p->sex, h->sex);
strcpy(h->sex, temp);
//交换年龄
t = p->age;
p->age = h->age;
h->age = t;
}
}
}
}
void main() {
int n;
int a=0; //统计年龄超过60岁的员工数,并输出
struct staff staff[N];
struct staff* p;
p = staff;
input(p);
printf("\n按工龄降序排序(包含所有员工信息): ");
sort(p);
//输出所有员工
printf("\n\n");
for (p=staff+N-1; p >staff-1; p--) {
printf("%d\t%s\t%s\t%d\t%d\n", p->id, p->name,p->sex,p->age,p->workage);
}
p = staff;
printf("年龄超过60岁的员工: \n\n");
for (; p < staff + N; ++p) {
if (p->age > 60) {
printf("%d\t%s\t%s\t%d\t%d\n", p->id, p->name,p->sex,p->age,p->workage);
a++;
}
}
printf("\n年龄超过60岁的员工数: %d\n\n", a);
//
p = staff;
printf("请输入需要查询的员工号: ");
scanf("%d", &n);
for (; p < staff + N; ++p) {
if (p->id == n) {
printf("\n职工号为%d的职工, 姓名为:%s\n\n", p->id, p->name);
break;
}
}
if (p == staff + N) {
printf("没有职工号为%d的员工\n\n", n);
}
system("pause");
}