Exercisec About FILES (ifstream)

FILE EXERCISES

student.txt file contains student number, mt1, mt2 and final exam results

980000 50 40 60
980001 38 90 93
980007 33 42 45
980016 44 57 27
980017 46 92 86
980025 82 33 85
980031 59 70 49
980033 74 72 57
980035 28 42 33
980036 29 64 30
980039 55 49 41
980044 25 32 80
980049 35 30 82
980050 62 84 57
980057 30 27 55
980067 47 78 20
980069 58 41 45
980079 73 96 91
980089 91 93 37
980097 81 85 73
 

EXERCISES 1

//open the file in constructor

class student
{  int mt1,mt2,final;
    long sno;
    public:
    student()
    {     ifstream in("student.txt");
           ofstream out("average.txt");
            in>>sno>>mt1>>mt2>>final;
            out<<sno<<" "<<aver()<<"
";
    }
   
    print()
    { cout<<sno<<" "<<aver()<<"
"; }

    float aver()
    { return mt1*0.30+mt2*0.30+final*0.4; }

};

 

#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
#include"student1.h"
main()
{    student S1,S2,S3;    }

 

Contents of the average.txt  file

980000 51
 

*****************************************************

EXERCISES 2

//open the file in constructor

class student
{     int mt1,mt2,final;
       long sno;
        public:
        student()
        {     ifstream in("student.txt");
               ofstream out("average.txt");
               while(in>>sno>>mt1>>mt2>>final)
                {     if(aver()>50)
                       out<<sno<<" "<<aver()<<"
";
                }
                in.close();
                out.close();
           }

        float aver()
        {     return mt1*0.30+mt2*0.30+final*0.4; }

        ~student(){}
};
 

 

#include<iostream.h>
#include<fstream.h>
#include"student1.h"
main()
{    student S1;    }

 

Contents of the average.txt  file

980000 51
980001 75.6
980017 75.8
980025 68.5
980031 58.3
980033 66.6
980049 52.3
980050 66.6
980079 87.1
980089 70
980097 79

 

EXERCISES 3

//open the file outside the constructor

ifstream in("student.txt");
ofstream out("average.txt");
class student
{  int mt1,mt2,final;
    long sno;
    public:
    student()
    {    in>>sno>>mt1>>mt2>>final;
          out<<sno<<" "<<aver()<<"
";
    }
  
    float aver()
    { return mt1*0.30+mt2*0.30+final*0.4; }
   

    ~student()

    {    in.close(); out.close();    }  
};
 

#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
#include"student1.h"
main()
{    student S1,S2,S3;    }
     
Contents of the average.txt file

980000 51
980001 75.6
980007 40.5

Kaynak: sct.emu.edu.tr/csit341
belgesi-1133

Belgeci , 2280 belge yazmış

Cevap Gönderin