C++第14周项目5——涨工资了

课程首页地址:http://blog.csdn.net/sxhelijian/article/details/7910565


【项目5 - 涨工资了 】从文件salary.txt中读入500名工人的工资,全部增加100元后进行排序(好事,涨工资了;坏事,排序不要用冒泡,用选择法),将排序后的结果在屏幕上输出,并保存到文件ordered_salary.txt中。(salary.txt中内容可以从本文后面复制,自建文本文件。)
  提示:做第11周项目5,学习对文件的操作。

#include <fstream>
#include<iostream>
using namespace std;
int main( )
{
	float a[500],t;
	int i,j;
	ifstream infile("salary.txt",ios::in);  //以输入的方式打开文件
	if(!infile)       //测试是否成功打开
	{
		cerr<<"open error!"<<endl;
		exit(1);
	}
	for(i=0;i<500;i++)
		infile>>a[i];   //从文件中读取数据
	infile.close();
	for(i=0;i<10;i++)
		a[i]+=100; //涨工资了
	//排序,用冒泡法,其他法类似
	for(j=0;j<500-2;j++)     
		for(i=0;i<500-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<500;i++)
			{
				outfile<<a[i]<<endl;
				if(i%5==0&&i!=0) cout<<endl;
				cout<<a[i]<<"\t";
			}
			outfile.close();     //关闭文件。用完了必须关闭,否则会丢失数据
			return 0;
}


附:

1648.4
619.54
1763.34
2772.59
858.74
723.49
2391.0
876.95
2551.5
613.30
2711.12
850.4
909.68
1213.95
1099.51
1115.36
962.57
2325.6
804.20
1900.77
2940.78
2085.27
1119.57
1784.22
1348.9
949.99
1579.13
1346.39
1822.32
678.67
1984.87
817.20
850.81
2121.39
985.94
2386.22
1112.86
1604.15
1013.89
2968.97
622.48
699.68
1034.7
1741.93
862.48
2879.14
2982.70
2513.58
2192.81
2640.22
2162.98
589.97
2277.5
1605.55
1688.42
1886.9
544.67
2410.92
1220.87
1892.87
1326.63
1696.38
1239.96
1895.55
2550.13
1946.68
2827.11
2009.47
2106.57
1401.49
2102.52
2580.97
2772.85
2809.58
2530.81
1132.66
1663.38
1974.22
1564.16
2226.63
771.30
2194.15
2620.15
2424.46
1903.66
950.45
2984.95
1942.62
1309.49
1760.42
2346.91
680.80
1525.74
1145.34
2102.63
1515.47
1324.28
1687.41
1387.96
1290.2
2491.6
1874.49
2958.17
1006.20
2258.94
2814.89
2857.60
763.77
2874.9
2777.54
895.16
2909.22
2359.26
2773.98
2416.17
1003.43
599.77
1810.6
2620.87
2306.0
1168.84
2240.99
2161.6
2627.62
681.29
998.22
1506.61
2152.61
2221.71
648.86
1800.54
1045.9
2904.98
2656.0
778.31
1385.12
2446.31
2648.55
845.14
1115.57
972.92
2554.82
1358.76
1870.13
1066.63
2962.72
1646.94
1115.63
2831.31
729.15
2052.36
1768.74
2457.61
1718.82
1522.40
2340.57
2709.33
1597.74
2906.75
2846.23
1990.25
2441.10
1005.61
2026.19
2914.69
2084.51
1939.63
782.3
2641.32
2211.90
2359.40
2071.62
2875.25
1187.33
2886.68
2375.89
1639.24
955.17
2383.71
802.77
1091.78
2504.25
1843.52
779.92
1399.35
1194.94
1792.99
2669.59
1326.54
1821.47
2112.30
596.16
2001.91
614.22
2835.94
710.45
1711.96
2497.9
1459.25
851.57
844.49
1496.64
972.81
2743.18
1529.11
1440.24
1810.68
2495.5
1446.58
1675.24
1591.71
2434.72
1608.76
955.71
938.28
1231.82
1971.24
1336.68
2912.41
1869.9
792.32
1026.28
592.22
2906.93
1449.49
1254.24
554.3
1934.49
757.65
2851.86
901.26
1558.9
2741.95
2716.12
1050.54
1011.89
993.84
2836.74
2065.60
1228.0
897.91
2704.35
2838.80
2564.65
2985.84
1906.40
1893.97
2138.85
1190.11
1313.75
1280.23
2638.86
1311.32
2798.32
2203.48
2717.51
1495.6
1349.45
1679.8
1207.1
2544.43
1952.96
2118.2
2478.74
1082.87
1136.91
833.42
1287.69
1296.38
1946.62
1327.6
2292.62
1254.64
2787.2
853.4
2360.12
2390.40
589.43
1118.11
2945.1
1636.80
2369.1
1091.62
1194.80
825.90
2145.99
2792.31
2145.1
1955.57
832.11
1658.92
1005.34
1267.37
2249.50
548.71
1576.12
2767.94
1904.86
1170.91
1166.78
2092.66
1608.52
1886.45
1638.25
2143.46
1904.57
1199.42
2839.73
1652.41
695.48
831.11
1231.26
2450.49
2607.83
2398.59
962.11
666.83
1864.86
572.72
1568.50
1180.60
2252.79
2349.14
909.41
2700.53
2487.88
2885.38
560.24
1265.45
2934.8
2048.96
2334.86
2265.70
1707.79
1332.45
1707.64
2344.90
2037.31
2170.71
2861.65
2809.59
748.5
2124.58
1432.14
1068.69
643.84
2841.68
2947.46
2027.8
2047.84
1680.9
1760.89
902.29
688.48
2158.61
2560.7
1646.79
1826.21
2708.74
2815.13
828.64
1562.86
1599.24
611.29
2521.74
872.61
1664.4
1145.24
2011.1
1297.42
2705.8
1024.76
1885.23
2854.60
1103.17
705.92
899.71
741.90
2702.70
791.95
1003.34
928.37
975.23
2439.71
1163.87
2041.49
2666.2
1245.72
1360.18
2400.2
2714.84
1194.68
2152.35
1613.66
728.13
2341.73
1069.9
2144.40
2890.26
862.85
773.56
940.34
1990.59
862.27
1222.80
2187.4
1488.51
2183.80
2745.52
2958.93
1938.33
2310.37
2739.34
1197.41
1147.89
553.65
864.92
1306.81
2626.10
2650.92
1755.86
1404.53
573.10
2600.42
978.89
1236.46
1143.64
534.43
2019.30
1980.74
2822.33
2206.63
1641.16
640.75
1298.99
1848.92
1854.94
1765.32
1628.1
2536.18
2149.21
2409.58
2292.48
585.25
2735.2
2080.98
1872.97
2673.31
2629.71
2538.14
1848.40
2507.35
2029.45
634.22
2816.2
1228.94
962.40
1423.20
2434.65
906.43
1659.35
1578.49
2520.23
1900.84
1729.38
1489.57
1457.61
1842.54
980.55
2919.21
1529.37
1061.66
2178.65
2738.90
1887.33
1329.64
746.5
2832.47
2233.84
1072.80
2981.50
1181.16
1782.77
2145.54
881.12
2540.76
1621.71
2376.48
2627.47
1748.40
694.8
1795.20
1268.82
2930.51
1005.74
2384.95
1835.38
2905.76
831.39
2308.74



你可能感兴趣的:(C++第14周项目5——涨工资了)