1. 程式人生 > >十五週專案一工資資料的輸入(freopen)

十五週專案一工資資料的輸入(freopen)

#include<cstdio>
#include<iostream>
using namespace std;
int main( )
{
    float salary[500],t;
    int i,j,n=0;
    freopen("salary.txt","r",stdin);  //以輸入的方式開啟檔案
    freopen("ordered_salary.txt","w",stdout); //輸出結果
    while(cin>>salary[n]) n++;   //從檔案中讀取資料,n為職工人數
    for(j=0; j<n-1; j++)//排序,用冒泡法,其他法類似
        for(i=0; i<n-j-1; i++)
            if (salary[i]<salary[i+1])
            {t=salary[i];salary[i]=salary[i+1];salary[i+1]=t;}
   for(i=0; i<n; i++)
    {cout<<salary[i]<<endl;}
    fclose(stdin);
    fclose(stdout);//關閉檔案。用完了必須關閉,否則會丟失資料
    return 0;
}