C++第14周项目7——体验文件操作

来源:互联网 发布:win7旗舰版自带sql吗 编辑:程序博客网 时间:2024/05/18 15:04

课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759


【项目7-体验文件操作】

  (1-预备)阅读并理解附后的《文件操作初体验》(必要时运行这些程序)。
  (2-热身)从键盘读入10名学生的英语成绩,编程求出这次考试的平均成绩,并统计输出优秀人数和不及格人数。

#include <fstream>   //操作文件必写#include<iostream>#include<cstdlib>using namespace std;int main( ){    int i,s;    int a=0, b=0;//分别代表优秀、不及格人数、总人数    double sum=0,ave; //s: 成绩和,ave: 平均分    //以输入的方式(ios::in)打开文件    for(i=0; i<10; i++)    {        cin>>s;        sum+=s;        if(s>=90)            a++;        else if(s<60)            b++;    }    //下面输出结果    ave=sum/10;    cout<<"平均成绩为:"<<ave<<endl;    cout<<"优秀人数:"<<a<<endl;    cout<<"不及格人数:"<<b<<endl;    return 0;}

  (3-实战)文件english.dat(BB平台下载,该文件要和源程序在同一文件夹中)中已经有了学生的成绩数据,请改编(2)中的程序,编程求出这次考试的平均成绩,并统计输出优秀人数和不及格人数。

#include <fstream>   //操作文件必写#include<iostream>#include<cstdlib>using namespace std;int main( ){    int s; //读入的成绩    int a=0, b=0,count=0;//分别代表优秀、不及格人数、总人数    double sum=0,ave; //sum: 成绩和,ave: 平均分    //以输入的方式(ios::in)打开文件    ifstream infile("english.dat",ios::in);    if(!infile)    {        cerr<<"open error!"<<endl;        exit(1);    }    while(infile>>s)   //当读取成功……    {        count++;        sum+=s;        if(s>=90)            a++;        else if(s<60)            b++;    }    infile.close();  //读入完毕要关闭文件    //下面输出结果    ave=sum/count;    cout<<"总人数为:"<<count<<endl;    cout<<"平均成绩为:"<<ave<<endl;    cout<<"优秀人数:"<<a<<endl;    cout<<"不及格人数:"<<b<<endl;    return 0;}

  (4-实战)编程求出这次考试的最高成绩,以及得最高成绩的学生的学号(设学号即是相应数组元素的下标)。

#include <fstream>   //操作文件必写#include<iostream>#include<cstdlib>using namespace std;int main( ){    int s[10000],max=-1; //读入的成绩    int i,count=0;//分别代表优秀、不及格人数、总人数    //以输入的方式(ios::in)打开文件    ifstream infile("english.dat",ios::in);    if(!infile)    {        cerr<<"open error!"<<endl;        exit(1);    }    while(infile>>s[count])   //当读取成功……    {        if(s[count]>max)            max=s[count];        count++;    }    infile.close();  //读入完毕要关闭文件    //下面输出结果    cout<<"总人数为:"<<count<<endl;    cout<<"最高分为:"<<max<<endl;    cout<<"得最高分的同学的学号为:";    for(i=0; i<count; i++)        if(s[i]==max)            cout<<i<<" ";    cout<<endl;    return 0;}

  (5-实战)据统计,这次考试成绩均分为71.49,标准偏差为10.33,请编程将成绩转换为标准分,并将转换后的成绩保存到文件english2.dat中。
    标准分算法:Z=(X-A)/S,其中:X为原始分,A为全体考生的平均分,S为该次考试分数的标准偏差。标准分T=500+100Z。
    求平均和求标准偏差的工作可以使用项目3中已经编制好的函数完成,为简单起见,也可以直接用题目中给出的数据。

#include <fstream>   //操作文件必写#include<iostream>#include<cstdlib>#include<cmath>using namespace std;double get_avg(int s[], int n);double get_stdev(int s[], int n);int main( ){    int i=0,ss,s[10000]; //读入的成绩    int count=0;//分别代表优秀、不及格人数、总人数    double ave,stdev;    //以输入的方式(ios::in)打开文件    ifstream infile("english.dat",ios::in);    if(!infile)    {        cerr<<"open error!"<<endl;        exit(1);    }    while(infile>>ss)    {        s[i]=ss;        i++;    }    count=i;    infile.close();  //读入完毕要关闭文件    ave=get_avg(s,count);    //求平均,可以直接赋值题目所给数据71.49    stdev=get_stdev(s,count);//求平均,可以直接用题目所给数据10.33    //转换成标准分    for(i=0; i<count; i++)        s[i]=500+100*(s[i]-ave)/stdev;    //输出到文件    //以输出的方式(ios::out)打开文件    ofstream outfile("english2.dat",ios::out);    if(!outfile)    {        cerr<<"open error!"<<endl;        exit(1);    }    for(i=0; i<count; i++)        outfile<<s[i]<<endl;    outfile.close();    cout<<"处理完毕!"<<endl;    return 0;}/*get_avg函数的功能是求出num名同学成绩中的平均成绩 *入口参数:     s - 存放成绩的数组 n - 学生人数 *返回值:平均成绩 */double get_avg(int s[], int n){    double sum = 0;    int i;    for(i=0; i<n; i++)        sum+=s[i];    return sum/n;}/* get_ stdev函数的功能是求出num名同学成绩的标准偏差 *入口参数:     s - 存放成绩的数组 n - 学生人数 *返回值:标准偏差 */double get_stdev(int s[], int n){    double sum = 0,mean_score, x;    int i;    mean_score =get_avg(s,n);  //此处通过调用函数求均值,体会函数的意义    for(i=0; i<n; i++)    {        x=s[i]-mean_score;        sum+=x*x;    }    return sqrt(sum/(n-1));}

  (6-实战)读取english2.dat中的数据,用项目4中定义的排序函数对数据进行排序,输出完成排序用了多长时间。可以分别调用冒泡排序和选择排序,比较两种算法哪个更快。

#include <fstream>   //操作文件必写#include<iostream>#include<cstdlib>#include<ctime>using namespace std;void bubble_sort(int arr[], int num);int main( ){    int s[10000],ss,count=0,i; //读入的成绩    long t1,t2;    //以输入的方式(ios::in)打开文件    ifstream infile("english2.dat",ios::in);    if(!infile)    {        cerr<<"open error!"<<endl;        exit(1);    }    while(infile>>ss)    {        s[count]=ss;        count++;    }    infile.close();  //读入完毕要关闭文件    for(i=0; i<count; i++)    {        if(s[i]>1000)            cout<<i<<" "<<s[i]<<endl;    }    t1=time(0);    bubble_sort(s, count);    //冒泡排序    t2=time(0);    if(t2-t1==0)        cout<<"太快了,1秒内完成排序!"<<endl;    else        cout<<"冒泡排序费时"<<t2-t1<<"秒"<<endl;    //排序结果输出到文件,每行输出10个,每两个间用空格隔开    //以输出的方式(ios::out)打开文件    ofstream outfile("english3.dat",ios::out);    if(!outfile)    {        cerr<<"open error!"<<endl;        exit(1);    }    for(i=0; i<count; i++)    {        outfile<<s[i]<<" ";        if((i+1)%10==0)            outfile<<endl;    }    outfile.close();    cout<<"处理完毕!"<<endl;    return 0;}void bubble_sort(int arr[], int num){    int i,j;    int t;    for(j=0; j<num-1; j++)   //共进行num-1趟比较        for(i=0; i<num-j-1; i++) //在每趟中要进行num-j次两两比较            if (arr[i]<arr[i+1]) //如果前面的数小于后面的数            {                t=arr[i]; //交换两个数的位置,使小数下沉                arr[i]=arr[i+1];                arr[i+1]=t;            }    return;}

  采用选择排序的解法:

#include <fstream>#include<iostream>#include<cstdlib>#include<ctime>using namespace std;void select_sort(int array[],int n);int main( ){    int s[10000],ss,count=0,i; //读入的成绩    long t1,t2;    //以输入的方式(ios::in)打开文件    ifstream infile("english2.dat",ios::in);    if(!infile)    {        cerr<<"open error!"<<endl;        exit(1);    }    while(infile>>ss)    {        s[count]=ss;        count++;    }    infile.close();  //读入完毕要关闭文件     t1=time(0);    select_sort(s, count);   //调用选择排序    t2=time(0);    if(t2-t1==0)        cout<<"太快了,1秒内完成排序!"<<endl;    else        cout<<"冒泡排序费时"<<t2-t1<<"秒"<<endl;    //排序结果输出到文件,每行输出10个,每两个间用空格隔开    //以输出的方式(ios::out)打开文件    ofstream outfile("english3.dat",ios::out);    if(!outfile)    {        cerr<<"open error!"<<endl;        exit(1);    }    for(i=0; i<count; i++)    {        outfile<<s[i]<<" ";        if((i+1)%10==0)            outfile<<endl;    }    outfile.close();    cout<<"处理完毕!"<<endl;    return 0;}void select_sort(int array[],int n) //形参array是数组名{    int i,j,k,t;    for(i=0; i<n-1; i++)    {        k=i;  //先设第i个就为最小        for(j=i+1; j<n; j++)            if(array[j]<array[k])                k=j;   //通过循环,得到k为最小        t=array[k];    //交换a[i]和a[k]        array[k]=array[i];        array[i]=t;    }    return;}



==================== 迂者 贺利坚 CSDN博客专栏=================

|==  IT学子成长指导专栏  专栏文章分类目录(不定期更新)    ==|

|== C++ 课堂在线专栏   贺利坚课程教学链接(分课程年级)   ==|

======== 为IT菜鸟起飞铺跑道,和学生一起享受快乐和激情的大学 =======



原创粉丝点击