#include <stdio.h>
#include <stdlib.h>
#define EMP_NUM 10
typedef struct employee {
int age;
char name[20];
unsigned salary;
}*PEMP;
typedef enum sel_analy{
TOTAL,
AVERAGE
}sel_c;
void update_msg(PEMP company[]);
void display_msg(PEMP company[]);
void salary_analysis(PEMP company[], sel_c which);
int main(int argc, char *argv[])
{
int i = 0;
PEMP company[EMP_NUM];
char select;
sel_c which;
memset(company, NULL, sizeof(company));
do
{
printf("
<-----工资统计系统------->/n");
printf("1:更新职工信息
2:工资求和/n");
printf("3:工资求平均值
4:显示职工信息/n");
printf("0:退出/n");
select = getch();
switch(select){
case '1':
update_msg(company);
break;
case '2':
salary_analysis(company, TOTAL);
break;
case '3':
salary_analysis(company, AVERAGE);
break;
case '4':
display_msg(company);
break;
default:
break;
};
}while(select != '0');
return 0;
}
void update_msg(PEMP company[])
{
int sel = 0;
int id;
while(1){
PEMP new_emp = (PEMP)malloc(sizeof(*new_emp));
printf("请输入员工信息,当需要退出输入时设ID为-1:/n");
printf("ID:");
scanf("%d", &id);
while((id<-1) || (id>EMP_NUM-1)){
printf("输入信息错误,输入的ID范围应为0到%d之间的数:/n", EMP_NUM-1);
printf("ID:");
scanf("%d", &id);
}
if(id == -1){
free(new_emp);
return;
}
printf("姓名:");
scanf("%s", new_emp->name);
printf("年龄:");
scanf("%d", &new_emp->age);
while((new_emp->age<0) || (new_emp->age>60)){
printf("输入信息错误,输入的年龄范围应为0到60之间的数:/n");
printf("年龄:");
scanf("%d", &new_emp->age);
}
printf("薪水:");
scanf("%d", &new_emp->salary);
while((new_emp->age<0) || (new_emp->age>20000)){
printf("输入信息错误,输入的薪水范围应为0到20000之间的数:/n");
printf("薪水:");
scanf("%d", &new_emp->salary);
}
company[id] = new_emp;
}
}
void salary_analysis(PEMP company[], sel_c which)
{
char sel=0;
int age1, age2;
int low_age, high_age, i, j=0;
unsigned long sum = 0;
while(1){
if(which == TOTAL)
printf("1:计算某一年龄段员工的薪水总和
2:计算全部员工薪水总和/n");
else
printf("1:计算某一年龄段员工的薪水平均值
2:计算全部员工薪水平均值/n");
printf("0:返回上一层/n");
sel = getch();
if((sel != '0') && (sel != '1') && (sel != '2')){
printf("输入错误,请重新输入:/n");
continue;
}
if(sel == '0')
return;
if(sel == '1'){
printf("开始年龄:");
scanf("%d", &age1);
while(age1<0 || age1>60){
printf("输入错误,请重新输入/n");
printf("开始年龄:");
scanf("%d", &age1);
}
printf("结束年龄:");
scanf("%d", &age2);
while(age2<0 || age2>60){
printf("输入错误,请重新输入/n");
printf("结束年龄:");
scanf("%d", &age2);
}
if(age1>age2){
low_age = age2;
high_age = age1;
}
else{
low_age = age1;
high_age = age2;
}
for(i=0; i<EMP_NUM; i++){
if(company[i])
if((company[i]->age>=low_age) && (high_age>=company[i]->age)){
sum += company[i]->salary;
j++;
}
}
if(sum != 0)
if(which == TOTAL)
printf("从%d到%d年龄段的员工薪水总和为%ld/n", low_age, high_age, sum);
else
printf("从%d到%d年龄段的员工薪水平均值为%ld/n", low_age, high_age, (sum/j));
else
printf("没有该年龄段的员工!/n");
return ;
}
if(sel == '2'){
for(i=0; i<EMP_NUM; i++){
if(company[i]){
sum += company[i]->salary;
j++;
}
}
if(sum != 0)
if(which == TOTAL)
printf("所有员工薪水总和为%ld/n", sum);
else
printf("所有员工薪水平均值为%ld/n", sum/j);
else
printf("没有员工信息!/n");
return;
}
}
}
void display_msg(PEMP company[])
{
int i;
for(i=0; i<EMP_NUM; i++)
{
if(company[i])
printf("ID:%d
姓名:%s
年龄:%d
薪水:%d/n", i, company[i]->name,/
company[i]->age, company[i]->salary );
}
}