《C语言及程序设计》实践参考——算工资

返回:贺老师课程教学链接  项目要求


【项目4-算工资】
从文件salary.txt中读入工人的工号、基本工资、奖金,将奖金全部增加20%(好事)后,将工号、基本工资、奖金和应发工资(前项目之和)保存到文件salarylist.txt中。

[参考解答]

#include 
#include 
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;
}


你可能感兴趣的:(c/c++)