实践练习七

教材习题11-7

此题只要求注释

先贴运行结果截图

注释

#include<iostream>
using namespace std;
int main(){
    ios_base::fmtflags original_flags=cout.flags();
    //定义一个名为 original_flags的 fmtflags类型的变量, cout.flags()无参数,故返回当前 flag值
    //无参返回当前 flag值是我百度来的,我也没看懂 
    cout<<812<<'|';
    //输出数字812和字符| 
    cout.setf(ios_base::left,ios_base::adjustfield);
    // ios_base::left表示下一次输出时要左对齐
    // ios_base::adjustfield表示取消之前设置的对齐方式 
    cout.width(10);
    //指定输出宽度为 10 
    cout<<813<<815<<'\n';
    //输出 813,815并换行 
    //其中 ios_base::left仅生效一次 
    cout.unsetf(ios_base::adjustfield);
    cout.precision(2);
    //保留小数点后 2位 
    cout.setf(ios_base::uppercase|ios_base::scientific);
    // ios_base::scientific指使用科学记数法
    // ios_base::uppercase指使用科学记数法时显示大写字母 E 
    cout<<813.0;
    //输出数 813.0 
    cout.flags(original_flags);
    //返回当前 flag值 
    return 0;
}

教材习题11-3

源代码

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

int main()
{
    ofstream file("text1.txt");
    if(!file){//用于查错 
        cout<<"failed"<<endl;
        return 1;
    }
    file<<"已成功写入文件!"<<endl;
    file.close();
//  ofstream file;
//  file.open("text1.txt", ios_base::out);
//  file << "已成功写入文件!";
//  file.close();
    return 0;
}

运行结果截图

cmd窗口截图

文件地址截图及txt文件截图

教材习题11-4

源代码

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

int main(){
    string temp;
    ifstream a("text1.txt");
    if(!a){
        cout<<"failed"<<endl;
    }
    a >> temp;
    cout<<temp<<endl;
    a.close();
    return 0;
}

结果截图

应用练习一

源代码

#include <bits/stdc++.h>
using namespace std;

struct classmates {     //结构体定义
    int number;         //序号
    long long id;       //学号
    string name;        //姓名
    string classname;   //班级
};

int main()
{
    vector <classmates> temp;
    int all = 0;    //计算总人数 
    //打开文件 
    ifstream file("list.txt");
    if (!file) {
        cout << "open file error!" << endl;
        return 1;
    }
    //cout << "OpenFinish" << endl;
    //读入内容 
    classmates x;
    while (file >> x.number >> x.id >> x.name >> x.classname) {
        temp.push_back(x);
    /*  cout << temp[all].number << "  " << temp[all].id << "  "
             << temp[all].name << "  "<< temp[all].classname << endl;//验证是否正确录入 */
        ++all;
    }
    //cout << "inFinish" << endl;
    file.close();
    //输出内容(提前准备) 
    ofstream outfile("roll.txt");
    if(!outfile){
        cout<<"creat outfile failed"<<endl;
        return 1;
    }
    //随即生成五个数
    struct timeb timeSeed;
    ftime(&timeSeed);
    srand(timeSeed.time * 1000 + timeSeed.millitm);  // 毫秒 
    double result[5];
    for (int j = 0; j < 5; j++) {
        result[j] = rand() % all;
        //验证正确性 
        //cout << result[all] << endl;
        cout << temp[result[j]].number << "  " << temp[result[j]].id << "  "
             << temp[result[j]].name << "  " << temp[result[j]].classname << endl;
        outfile << temp[result[j]].number << "  " << temp[result[j]].id << "  "
                << temp[result[j]].name << "  " << temp[result[j]].classname << endl;
    }
    outfile.close();
    return 0;
}

cmd窗口截图

文件地址及txt文件截图

应用联系二

源代码

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
    //打开文件
    ifstream a("experiment.txt");
    //用于计数,字符、单词、行数
    int number_of_character = 0, number_of_word = 0, number_of_line = 0;
    string str;
    //按行读取
    while (getline(a, str)) {
        //cout << "read a line" << endl;
        char x[1000];
        strcpy_s(x, str.c_str());
        for (int i = 0; i < 1000; i++) {
            //从简:字符只计算字母数,不计算逗号句号等
            if ((x[i] >= 'a' && x[i] <= 'z') || (x[i] >= 'A' && x[i] <= 'Z')) {
                ++number_of_character;
            }
            //用空格数计算单词数
            else if (x[i] == ' ') {
                ++number_of_word;
            }
        }
        //n个空格实质代表n+1个单词
        ++number_of_word;
        //循环一次读一行
        ++number_of_line;
    }
    a.close();
    //输出
    cout << "The number of character is " << number_of_character << endl
        << "The number of word is " << number_of_word << endl
        << "The number of line is " << number_of_line << endl;
    return 0;
}

运行结果截图

txt文本内容

cmd命令窗口截图

猜你喜欢

转载自www.cnblogs.com/MrWang-nextdoor/p/9203502.html