第十五周实验报告3

 

第15周报告3
实验目的:学会ASCII文件的操作
实验内容:从文件中读入数据,排序并输出到另外一个文件中

* 程序头部注释开始
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:                             
* 作    者:王引琳                            
* 完成日期:   2011      年12       月     
* 版 本号:         

* 对任务及求解方法的描述部分
* 输入描述:文件salary.txt
* 问题描述:从文件salary.txt中读入500名工人的工资,全部增加100元后进行排序(好事,涨工资了),将排序后的结果在屏幕上输出,并保存到文件ordered_salary.txt中
* 程序输出:ordered_salary.txt
* 程序头部的注释结束
//下面是程序
#include <fstream>
#include <iostream>
using namespace std;
void ordered_salary_putout(double a[],int num);       //输出工资
void ordered_salary_puton(double a[],int num);        //输入
void ordered_salary_save(double a[],int num);         //保存
void ordered_salary_add(double a[],int num);          //增加
void ordered_salary_bubble(double a[],int num);       //排序
int main()
{
 double a[500];
 int num=500;
 ordered_salary_puton(a,num);
 cout<<"原员工工资为:"<<endl;
 ordered_salary_putout(a,num);
 ordered_salary_add(a,num);
 ordered_salary_bubble(a,num);
 cout<<"加工资排序后员工工资:"<<endl;
 ordered_salary_putout(a,num);
 ordered_salary_save(a,num);
 return 0;
}
void ordered_salary_putout(double a[],int num)
{
 int i;
 for(i=0;i<num;i++)
 {
  cout<<a[i]<<" ";
  if((i+1)%9==0)
   cout<<endl;
 }
 cout<<endl;
}
void ordered_salary_puton(double a[],int num)
{
 int i;
 ifstream infile("salary.txt",ios::in);
 if(!infile)
 {
  cerr<<"open error!"<<endl;
  exit(1);
 }
 for(i=0;i<num;i++)
 {
  infile>>a[i];
 }
 infile.close();
}
void ordered_salary_save(double a[],int num)
{
 int i;
 ofstream outfile("ordere_salary.txt",ios::out);
 if(!outfile)
 {
  cerr<<"open error!"<<endl;
  exit(1);
 }
 for(i=0;i<num;i++)
 {
  outfile<<a[i];
 }
 outfile.close();
}
void ordered_salary_add(double a[],int num)
{
 int i;
 for(i=0;i<num;i++)
 {
  a[i]=a[i]+100;
 }
}
void ordered_salary_bubble(double a[],int num)
{
 int i,j;
 double t;
 for(i=0;i<num-1;i++)
  for(j=0;j<num-i-1;j++)
  if(a[j]>a[j+1])
  {
   t=a[j];
   a[j]=a[j+1];
   a[j+1]=t;
  }
}


运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值