第13周 项目6-4 文件的读取与读入

来源:互联网 发布:手机日常生活必备软件 编辑:程序博客网 时间:2024/05/22 17:06
问题及代码:
/*。*Copyright(c)2014,烟台大学计算机学院*All right reserved,*文件名:test.cpp*作者:liu_feng_zi_*完成日期:2014年X月X日*版本号:v1.0*问题描述:从文件读取数据,每个数据增加20%后,在输出到另一个文件*输入描述:*程序输出:*/#include <fstream>#include<iostream>#include<cstdlib>using namespace std;int main( ){    float a[500],t;    int i=0,j,n;    ifstream infile("salary.txt",ios::in);    if(!infile)    {        cerr<<"open error!"<<endl;        exit(1);    }    while(infile>>a[i]) i++;    infile.close();    n=i;    for(i=0; i<n; i++)        a[i]*=1.2;    for(j=0; j<n-1; j++)        for(i=0; i<n-j-1; i++)            if (a[i]>a[i+1])            {                t=a[i];                a[i]=a[i+1];                a[i+1]=t;            }    ofstream outfile("ordered_salary.txt",ios::out);    if(!outfile)    {        cerr<<"open error!"<<endl;        exit(1);    }    for(i=0; i<n; i++)    {        outfile<<a[i]<<endl;    }    outfile.close();    cout<<"好了"<<endl;    return 0;}
运行结果:
0 0
原创粉丝点击