Advertisement
hamaXD

CPT lab05-02

Sep 21st, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.79 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. struct employee {
  4.   char name[128];
  5.   float salary;
  6. };
  7.  
  8. typedef struct employee Employee;
  9. float total_salary(Employee all[], int len);
  10. int main() {
  11.    
  12.     int num=3,i,j=0;
  13.     Employee *all;
  14.     FILE *file;
  15.     file=fopen("employee.bin","r");
  16.     i=0;
  17.     all = (struct Employee *) malloc(sizeof(Employee)*3) ;
  18.     while(!feof(file)){
  19.         fscanf(file,"%c",&(*all).name[i]);
  20.         printf("%c",((*all).name[i]));
  21.         if((*all).name[i]==':'){
  22.             fscanf(file,"%f",&(*(all+j)).salary);
  23.             printf("%.2f",(*(all+j)).salary);  
  24.             j++;
  25.         }
  26.     i++;
  27.     }
  28.     printf("\n=%.1f\n", total_salary(all, num));   
  29.     fclose(file);
  30.     return 0;  
  31. }
  32. float total_salary(Employee all[], int len){
  33.     int i=0;
  34.     float sum;
  35.     while(i<len){
  36.         sum += (*(all+i)).salary;
  37.         i++;
  38.     }
  39.     return sum;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement