* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:读取500名工人工资
* 作 者: 任小宁
* 完成日期: 2012 年6 月 4日
* 版 本 号: 201158504431
* 对任务及求解方法的描述部分
#include<iostream>
#include<fstream>
using namespace std;
class Salary
{
public:
void ordered_salary_add();
void ordered_salary();
void input_salary();
void ordered_salary_txt();
private:
double salary[500];
int number;
};
int main()
{
Salary s1;
//cout<<"500人的原始工资为:"<<endl;
s1.input_salary();
//cout<<endl;
cout<<"排序后工资为:";
cout<<endl;
s1.ordered_salary();
cout<<endl;
s1.ordered_salary_txt();
system("PAUSE");
return 0;
}
void Salary::input_salary()
{
int i=0;
ifstream infile("salary.txt",ios::in);
if (!infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
for (i=0;i<500;i++)
infile>>salary[i];
infile.close();
//for(i=0;i<500;i++)
//cout<<salary[i]<<" ";
//cout<<endl;
number=500;
}
void Salary::ordered_salary_add()
{
int i;
for (i=0;i<number;i++)
{
salary[i]=salary[i]+100;
//cout<<salary[i]<<" ";
}
return;
}
void Salary::ordered_salary()
{
int i,j;
double t;
for(i=0;i<number-1;i++)
{
for(j=0;j<number-i-1;j++)
{
if(salary[j]>salary[j+1])
{
t=salary[j];
salary[j]=salary[j+1];
salary[j+1]=t;
}
}
}
for (i=0;i<number;i++)
{
cout<<salary[i]<<" ";
}
return;
}
void Salary::ordered_salary_txt()
{
ofstream outfile("ordered_salary.txt",ios::out);
if(!outfile)
{
cerr<<"open error!"<<endl;
exit(1);
}
for(int i=0;i<500;i++)
outfile<<salary[i]<<"\n";
outfile.close();
return ;
}
排序后工资为:
534.43 544.67 548.71 553.65 554.3 560.24 572.72 573.1 585.25 589.43 58
9.97 592.22 596.16 599.77 611.29 613.3 614.22 619.54 622.48 634.22 640
.75 643.84 648.86 666.83 678.67 680.8 681.29 688.48 694.8 695.48 699.6
8 705.92 710.45 723.49 728.13 729.15 741.9 746.5 748.5 757.65 763.77
771.3 773.56 778.31 779.92 782.3 791.95 792.32 802.77 804.2 817.2 825.
9 828.64 831.11 831.39 832.11 833.42 844.49 845.14 850.4 850.81 851.57
853.4 858.74 862.27 862.48 862.85 864.92 872.61 876.95 881.12 895.16
897.91 899.71 901.26 902.29 906.43 909.41 909.68 928.37 938.28 940.34
949.99 950.45 955.17 955.71 962.11 962.4 962.57 972.81 972.92 975.23
978.89 980.55 985.94 993.84 998.22 1003.34 1003.43 1005.34 1005.61 1005
.74 1006.2 1011.89 1013.89 1024.76 1026.28 1034.7 1045.9 1050.54 1061.6
6 1066.63 1068.69 1069.9 1072.8 1082.87 1091.62 1091.78 1099.51 1103.17
1112.86 1115.36 1115.57 1115.63 1118.11 1119.57 1132.66 1136.91 1143.6
4 1145.24 1145.34 1147.89 1163.87 1166.78 1168.84 1170.91 1180.6 1181.1
6 1187.33 1190.11 1194.68 1194.8 1194.94 1197.41 1199.42 1207.1 1213.95
1220.87 1222.8 1228 1228.94 1231.26 1231.82 1236.46 1239.96 1245.72 1
254.24 1254.64 1265.45 1267.37 1268.82 1280.23 1287.69 1290.2 1296.38 1
297.42 1298.99 1306.81 1309.49 1311.32 1313.75 1324.28 1326.54 1326.63
1327.6 1329.64 1332.45 1336.68 1346.39 1348.9 1349.45 1358.76 1360.18 1
385.12 1387.96 1399.35 1401.49 1404.53 1423.2 1432.14 1440.24 1446.58 1
449.49 1457.61 1459.25 1488.51 1489.57 1495.6 1496.64 1506.61 1515.47 1
522.4 1525.74 1529.11 1529.37 1558.9 1562.86 1564.16 1568.5 1576.12 157
8.49 1579.13 1591.71 1597.74 1599.24 1604.15 1605.55 1608.52 1608.76 16
13.66 1621.71 1628.1 1636.8 1638.25 1639.24 1641.16 1646.79 1646.94 164
8.4 1652.41 1658.92 1659.35 1663.38 1664.4 1675.24 1679.8 1680.9 1687.4
1 1688.42 1696.38 1707.64 1707.79 1711.96 1718.82 1729.38 1741.93 1748.
4 1755.86 1760.42 1760.89 1763.34 1765.32 1768.74 1782.77 1784.22 1792.
99 1795.2 1800.54 1810.6 1810.68 1821.47 1822.32 1826.21 1835.38 1842.5
4 1843.52 1848.4 1848.92 1854.94 1864.86 1869.9 1870.13 1872.97 1874.49
1885.23 1886.45 1886.9 1887.33 1892.87 1893.97 1895.55 1900.77 1900.84
1903.66 1904.57 1904.86 1906.4 1934.49 1938.33 1939.63 1942.62 1946.62
1946.68 1952.96 1955.57 1971.24 1974.22 1980.74 1984.87 1990.25 1990.5
9 2001.91 2009.47 2011.1 2019.3 2026.19 2027.8 2029.45 2037.31 2041.49
2047.84 2048.96 2052.36 2065.6 2071.62 2080.98 2084.51 2085.27 2092.66
2102.52 2102.63 2106.57 2112.3 2118.2 2121.39 2124.58 2138.85 2143.46
2144.4 2145.1 2145.54 2145.99 2149.21 2152.35 2152.61 2158.61 2161.6 21
62.98 2170.71 2178.65 2183.8 2187.4 2192.81 2194.15 2203.48 2206.63 221
1.9 2221.71 2226.63 2233.84 2240.99 2249.5 2252.79 2258.94 2265.7 2277.
5 2292.48 2292.62 2306 2308.74 2310.37 2325.6 2334.86 2340.57 2341.73
2344.9 2346.91 2349.14 2359.26 2359.4 2360.12 2369.1 2375.89 2376.48 23
83.71 2384.95 2386.22 2390.4 2391 2398.59 2400.2 2409.58 2410.92 2416.1
7 2424.46 2434.65 2434.72 2439.71 2441.1 2446.31 2450.49 2457.61 2478.7
4 2487.88 2491.6 2495.5 2497.9 2504.25 2507.35 2513.58 2520.23 2521.74
2530.81 2536.18 2538.14 2540.76 2544.43 2550.13 2551.5 2554.82 2560.7
2564.65 2580.97 2600.42 2607.83 2620.15 2620.87 2626.1 2627.47 2627.62
2629.71 2638.86 2640.22 2641.32 2648.55 2650.92 2656 2666.2 2669.59 267
3.31 2700.53 2702.7 2704.35 2705.8 2708.74 2709.33 2711.12 2714.84 2716
.12 2717.51 2735.2 2738.9 2739.34 2741.95 2743.18 2745.52 2767.94 2772.
59 2772.85 2773.98 2777.54 2787.2 2792.31 2798.32 2809.58 2809.59 2814.
89 2815.13 2816.2 2822.33 2827.11 2831.31 2832.47 2835.94 2836.74 2838.
8 2839.73 2841.68 2846.23 2851.86 2854.6 2857.6 2861.65 2874.9 2875.25
2879.14 2885.38 2886.68 2890.26 2904.98 2905.76 2906.75 2906.93 2909.22
2912.41 2914.69 2919.21 2930.51 2934.8 2940.78 2945.1 2947.46 2958.17
2958.93 2962.72 2968.97 2981.5 2982.7 2984.95 2985.84
请按任意键继续. . .
每一个打开的文件都有一个文件指针,该指针的初始位置有I/O方式指定。每次读写都从文件指针的当前位置开始。没读入一个字节,指针就后移一个字节。当文件指针移到最后,就会遇到文件结束EOF,此时流对象的成员函数EOF的值为非0值,表示文件结束。