This design completes a small design of wage statistics, involving the key knowledge of C language:
1. struct
2. struct pointer Array
3. Enumeration type
1.1 Design Questions
This course design requires the design of a company staff data structure, and use a structure pointer array to store staff information, statistics of the company staff total wages and average wages.
1.2 design requirements
The following functions are required:
(1) Use a structure pointer array to design the data structure of a company employee and use the following structure definitions:
Typedef structemployee {
Int age;
Char * name;
Doublesalary;
} * PEMP;
(2) construct a pointer array company in the main function to store employee information.
(3) design an update function to assign values to company.
The four parameters of the update (company, id, age, name, salary) function are:
Company: Structure pointer Array
Id: subscript of company
Age: age, integer type
Salary: salary, real number
(4) Design a readin function and assign values directly by calling the update function. For example: update (company, 2, 30, "li ming", 3000.0 );
(5) write the total function to sum the salary.
This function should be able to sum the salaries of all employees and employees of a certain age group.
(6) Compile the mean function to calculate the average salary.
This function should be able to sum the salaries of all employees and employees greater than a certain age group and calculate the corresponding average value.
2. Design Code
# 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 ("<----- wage statistics system -------> \ n ");
Printf ("1: Update employee information 2: wage sum \ n ");
Printf ("3: Average salary 4: Display employee information \ n ");
Printf ("0: Exit \ 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 ("Enter employee information. If you need to exit the input, Set ID to-1: \ n ");
Printf ("ID :");
Scanf ("% d", & id );
While (id <-1) | (id> EMP_NUM-1 )){
Printf ("input information error, input ID range should be between 0 and % d Number: \ n", EMP_NUM-1 );
Printf ("ID :");
Scanf ("% d", & id );
}
If (id =-1 ){
Free (new_emp );
Return;
}
Printf ("name :");
Scanf ("% s", new_emp-> name );
Printf ("Age :");
Scanf ("% d", & new_emp-> age );
While (new_emp-> age <0) | (new_emp-> age> 60 )){
Printf ("incorrect input information. The input age range should be 0 to 60: \ n ");
Printf ("Age :");
Scanf ("% d", & new_emp-> age );
}
Printf ("Salary :");
Scanf ("% d", & new_emp-> salary );
While (new_emp-> age <0) | (new_emp-> age> 20000 )){
Printf ("incorrect input information. The input salary range should be 0 to 20000: \ n ");
Printf ("Salary :");
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: calculate the total salaries of employees of a certain age group 2: calculate the total salaries of all employees \ n ");
Else
Printf ("1: calculate the average salary of employees of a certain age group 2: calculate the average salary of all employees \ n ");
Printf ("0: return the previous layer \ n ");
Sel = getch ();
If (sel! = '0') & (sel! = '1') & (sel! = '2 ')){
Printf ("input error, please input again: \ n ");
Continue;
}
If (sel = '0 ')
Return;
If (sel = '1 '){
Printf ("START age :");
Scanf ("% d", & age1 );
While (age1 <0 | age1> 60 ){
Printf ("input error, please input \ n again ");
Printf ("START age :");
Scanf ("% d", & age1 );
}
Printf ("end age :");
Scanf ("% d", & age2 );
While (age2 <0 | age2> 60 ){
Printf ("input error, please input \ n again ");
Printf ("end age :");
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 ("total salaries of employees from % d to % d: % ld \ n", low_age, high_age, sum );
Else
Printf ("the average salary of employees from % d to % d is % ld \ n", low_age, high_age, (sum/j ));
Else
Printf ("no employees of this age group! \ 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 ("total salaries of all employees: % ld \ n", sum );
Else
Printf ("average salaries of all employees: % ld \ n", sum/j );
Else
Printf ("No employee information! \ N ");
Return;
}
}
}
Void display_msg (PEMP company [])
{
Int I;
For (I = 0; I <EMP_NUM; I ++)
{
If (company [I])
Printf ("ID: % d name: % s age: % d salary: % d \ n", I, company [I]-> name ,\
Company [I]-> age, company [I]-> salary );
}
}
From K-Style technology house