Experiments eight - input and output streams

[Problems] Description 
programmed, for K = . 1 , 2 , . 3 , ..., 14 , 15 , respectively, calculated by the following equation 15 group (i, d, c): = an integer of I 2 * - K- . 1 ; d = real number * K + - K-K 9.8 ; character = C ' H ' + K. And by using the operator "<< saved to disk-type text file in the current directory ft.txt custom" these 15 sets of data. 
[Input] forms 
    without 
[form] the output 
    file content requires: one row for each output in each line is divided by a space. 
Sample input [] 
   No 
[] sample output 
file content format follows: 
. 1 - 7.8 the I
 . 3 - 3.8 J
 . 5  2.2 K
 . 7 10.2 L 
.... omitted below ...
first question
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
    ofstream fout;
    fout.open("ft.txt");
    for(int k=1;k<=15;k++)
    {
        fout<<2*k-1<<" "<<1.*k*k+k-9.8<<" "<<char('H'+k)<<endl;
    }
    fout.close();
    return 0;
}
 
 
2 . 
[Description problem 
by using the operator " >>" and "<< " for text-type disk file operation specified custom follows. 
( 1 ) for = I 1 , 2 , . 3 , ..., 15 , is calculated * I + I = D 0.5 , and the result is written in 15 to a disk file type text in a custom f1.txt (note, f1 file the data in ascending order); 
( 2 ) for = I . 1 , 2 , . 3 , ..., 10 , calculated = D 10 * I + 0.5 , and 10 write the results to custom text type disk f2.txt the file, and then write the number 11: 357.9 (Note that the data file f2 is also ordered from small to large); 
( 3 ), and reads out the data file f1.txt f2.txt in, with forming text type disk file f3.txt, but requires the data stored in the still f3.txt ordered (ascending order); 
[] input forms
    No 
[] output format 
   output file to f3.txt (row, the interval between a data space) 
[] sample input 
    None 
[output] Sample 
    1.5  4.5  9.5  10.5  16.5 ... (hereinafter, abbreviated)
The second question
#include<iostream>
#include<cstdio>
#include<cstring>
#include<fstream>
#include<algorithm>
using namespace std;

double a[30];

int main()
{
    ofstream fout("f1.txt");
    for(int i=1;i<=15;i++) fout<<(a[i]=1.*i*i+0.5)<<" ";
    fout.close();
    fout.open("f2.txt");
    for(int i=1;i<=10;i++) fout<<(a[i+15]=10.*i+0.5)<<" ";
    fout<<(a[26]=357.9);
    fout.close();
    ifstream fin1("f1.txt");
    ifstream fin2("f2.txt");
    for(int i=1;i<=15;i++) fin1>>a[i];
    for(int i=16;i<=26;i++) fin2>>a[i];
    sort(a+1,a+26+1);
    fout.open("f3.txt");
    for(int i=1;i<=26;i++) fout<<a[i]<<" ";
    fout.close();
    fin1.close();
    fin2.close();
    return 0;
}

 

3 . 
[Problem Description] 
using getline member function reads a text file (f5.txt) "article" (as may be a C ++ source file name from the keyboard input by the user), and after each blank line of the file as well as the program line comments are deleted (think of the line from the first double prime symbol " // " until all of the symbols at the end of the line began to comment), and delete the comment after the result rows write to another file (f6.txt) in. 
[Enter] in the form of 
progressive reading a file f5.txt content. 
[Output] form 
[] sample input 
content file f5.txt follows: 
        // This IS Test 
        #include <the iostream> int main () 
        { 
          COUT << " the Hello World " << endl;
           return 0 ; 
        } 
[Sample ] output 
contents of the file f6.txt follows: 
        #include <the iostream>
         int

        
 
main () 
        { 
          COUT << " the Hello World " << endl;
           return  0 ; 
        } 
[] Example Description read the file line by line, do not use the EOF end of file (), used while (getline (file, str) ) The this access to the file line data of type string variable str.
The third question
#include<iostream>
#include<cstdio>
#include<fstream>
#include<iomanip>
#include<cstring>
#include<algorithm>
using namespace std;

char s[101];

int main()
{
    ifstream fin("f5.txt");
    ofstream fout("f6.txt");
    while(fin.getline(s,100))
    {
        int len=strlen(s);
        0|| s [0(s ==IF]=='/') continue;
        for(int i=0;i<len;i++)
        {
            if(s[i]=='/') break;
            fout<<s[i];
        }
        fout<<endl;
    }
    fin.close();
    fout.close();
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/zzyh/p/11987786.html