【项目4-算工资】
从文件salary.txt中读入工人的工号、基本工资、奖金,将奖金全部增加20%(好事)后,将工号、基本工资、奖金和应发工资(前项目之和)保存到文件salarylist.txt中。
[参考解答]
#include <stdio.h> #include <stdlib.h> int main() { int num; //读入的工号 double salary, award; //工资、奖金 FILE *fpin, *fpout; fpin=fopen("salary.txt","r"); if(fpin==NULL) { printf("salary file open error!\n"); exit(1); } fpout=fopen("salarylist.txt","w"); if(fpout==NULL) { printf("cannot write to file!\n"); exit(1); } while(fscanf(fpin, "%d %lf %lf", &num, &salary, &award)!=EOF) //当读取成功…… { award *=1.2; fprintf(fpout, "%d %.2f %.2f %.2f\n", num, salary, award, salary+award ); } printf("thanks, byebye!\n"); fclose(fpout); fclose(fpin); //读入完毕要关闭文件 return 0; }
时间: 2024-09-21 15:30:20