我正在尝试制作一个程序,我可以输入有关“员工”的信息,并能够通过我的代码操作输入的信息。 如果您查看我的菜单代码中的菜单选项,您将看到我要特别做的事情。 我知道案例5-8没有得到满足。但是现在,我无法输出我的代码来测试代码是否与我到目前为止的代码一起工作。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
/*#include "mylib.h"*/
struct xrec
{
char name[11];
char ssn [12];
char g;
double age, hours, rate, salary;
};
typedef struct xrec Employee;
void printEmployee (Employee * a,int n);
double computeSalary (Employee * a, int n);
void printFemale (Employee * a, int n);
int displayMenu (void);
int search (Employee * a,int n, char * tssn);
void doSort (Employee * a, int n);
int main()
{
Employee temp;
Employee x[20];
char tssn[12];
int size =0, com , d,i;
FILE * infile;
infile = fopen ("Data.txt","r");
if (infile == NULL)
{
printf("File Not found\n");
exit (0);
}
while (fscanf(infile, "10s %s %c %lf %lf %lf",temp.name, temp.ssn,&temp.age,&temp.hours,&temp.rate) !=EOF)
{
temp.name[10]='\0'; temp.ssn[11]='\0';
x[size++] = temp;
}
fclose (infile);
computeSalary(&x[0],size);
do{
com = displayMenu();
switch(com)
{
case 1 : printEmployee (x,size); break;
case 2 : printFemale (x,size); break;
case 3 : printf("name:");
fflush (stdin);
gets (temp.name);
printf("SSN:");
fflush (stdin);
gets (temp.ssn); fflush (stdin);
printf("Gender:");
scanf("%c",&temp.g);
printf("Enter age, hours, rate:");
scanf("%lf %lf %lf",&temp.age,&temp.hours,&temp.rate);
x[size++] = temp;
computeSalary (x,size);
doSort (x,size);
break;
case 4 : printf ("Enter SSN:\n");
fflush (stdin);
gets(tssn);
d = search (x,size, tssn);
if(d < 0)
{
printf("Student not in the List \n");
}
else{
x[d] = x [--size];
}
break;
case 5 : doSort (x,size); break;
case 8 : infile=fopen("data.txt","w");
for (i=0; i <size; i++)
{
fprintf(infile,"%10s %11s %c %lf %lf %lf %lf\n", x[i].name, x[i].ssn, x[i].g, x[i].age,x[i].hours,x[i].rate,x[i].salary);
}
fclose (infile);
printf ("File is Updated now...\n");
;break;
default: ;
}
}while (com !=8);
return 0;
}
void doSort (Employee * a, int n)
{
int i,j;Employee temp;
for (i=0; i < n-1; i++)
for(j=i+1; j < n ; j++)
if(strcmp(a[i].name,a[j].name)>0)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
int displayMenu(void)
{
int c;
printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
printf("@ 1. List of Employee @@@\n");
printf("@ 2. List of Female Employee @@@\n");
printf("@ 3. Add Employee @@@\n");
printf("@ 4. Delete Employee @@@\n");
printf("@ 5. Employee Sorted By name @@@\n");
printf("@ 6. Highest Salary @@@\n");
printf("@ 7. Overtime Employee @@@\n");
printf("@ 8. Exit @@@\n");
printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
printf("Your Choice:");
scanf("%d",&c);
return c;
}
void printFemale (Employee * a, int n)
{
int i;
for(a[i].g='f';i < n;i++)
{
printf("%-10s \n",a[i].name);
}
}
double computeSalary (Employee * a, int n)
{
int i;
double total;
for (i=0; i < n; i++)
{
if ( a[i].hours <= 40){
total = a[i].hours * a[i].rate;
}else if (a[i].hours > 40){
total = (a[i].rate*1.5) * a[i].hours;}
}
return total;
}
void printEmployee (Employee * a,int n)
{
int i;
double total;
for(i=0 ; i < n; i++)
{
printf ("%-10s %11s %c %lf %lf %lf %lf \n",a[i].name, a[i].ssn,a[i].g, a[i].age, a[i].hours, a[i].rate, total);
}
}
int search(Employee * a,int n, char * tssn)
{
int i;
for(i=0 ; i < n; i++)
if(strcmp(a[i].ssn,tssn)==0)
return i;
return -1;
}