【实验7】流类库和输入输出

基础练习:

(1)教材习题11-7

#include<iostream>
using namespace std;

int main(){
    ios_base::fmtflags original_flags = cout.flags();        //保存此格式化参数设置
    cout << 812 << '|';
    cout.setf(ios_base::left,ios_base::adjustfield);         //把对齐方式改成左对齐
    cout.width(10);                                          //把宽度设置为10
    cout << 813 << 815 << '\n';
    cout.unsetf(ios_base::uppercase|ios_base::scientific);   //改成浮点数的显示设置
    cout << 831.0;
    cout.flags(original_flags);                              //恢复保存的格式化参数设置
    return 0;
}

(2)教材习题11-3

#include<iostream> 
#include<fstream>
using namespace std;

int main(){
    ofstream filel("test.txt");
    filel << "已成功写入文件!";
    filel.close();
    return 0;
}

(3)教材习题11-4

#include<iostream>
#include<fstream>
using namespace std;
int main(){
    char s;
    ifstream in ("test.txt");
    if (!in) {
        cout  <<  "fail to open."  <<  endl;
        return 1;
    }
    in >> s;
    cout << s << endl;
    in.close() ;
    return 0;
} 

应用练习:

(1)

#include<iostream>
#include<fstream>
#include<string>
#include<stdlib.h>
#include<ctime> 
using namespace std;

struct student
{
    int num;
    string ID;
    string name;
    string clas;
}stu[100];

int main(){ 
    ifstream in ("list.txt");
    if (!in){
        cout << "fail to open." << endl;
        return 1;
    }
    int i = 0;
    while (in >> stu[i].num >> stu[i].ID >> stu[i].name >> stu[i].clas)
        i++; //统计有几位同学 
    in.close ();
    ofstream out ("roll.txt");       //存放被点的人 
    srand((unsigned)time(NULL));    //利用时间随机 
    for ( int n = 1; n <= 5; n++ )  //随机五位 
    {
        int a = 1 + rand() % ( i + 1 ) ;
        cout <<stu[a].num  << " "
             <<stu[a].ID   << " "
             <<stu[a].name << " "
             <<stu[a].clas << endl;
        out  <<stu[a].num  << " "
             <<stu[a].ID   << " "
             <<stu[a].name << " "
             <<stu[a].clas << endl;
    }
    out.close();
    return 0;
}

不知道为什么输出的全是0……看了看好像好几个同学都这样啊…求大佬解释……

(2)

#include<iostream>
#include<fstream>
using namespace std;

bool str(char a){   //判断是否是字符
    if((a>='A'&&a<='z')||(a>='0'&&a<='9'))
        return true;
    else
        return false;
}

int main(){
    int str_number = 0;     //统计字符数
    int word_number = 0;    //统计单词数
    int line_number = 1;    //统计行数
    ifstream in("data.txt");
    if(!in){
        cout << "fail to open." << endl;
        return 1;
    }
    while(in){
        char a;
        in.read(reinterpret_cast<char*>(&a),sizeof(a));
        if(str(a))
            str_number++;
        if(a==' '||a=='.'||a==','||a=='!'||a=='?'||a=='"'||a=='('||a==')'||a==':')
            word_number++;
        if(a=='\n')
            line_number++;
    }
    in.close();
    cout << "字符数:" << str_number << endl;
    cout << "单词数:" << word_number << endl;
    cout << "行数:" << line_number << endl;
    return 0;
}

 思路:

        字符数:用一个bool类型函数判断是否是字符。

        单词数:我是用统计标点符号和空格的数目来间接统计单词数,但是我有个疑问,如果一个单词后面跟着两个或以上标点比如说“Then...”,这样就不准了,不知道怎么解决。

        行数:统计换行符的个数。

猜你喜欢

转载自www.cnblogs.com/Werol714/p/9203708.html