• c++实验7


    11-7

     1 #include<iostream>
     2 using namespace std;
     3 int main() {
     4     ios_base::fmtflags original_flags = cout.flags();//保存现在的格式化参数的设置
     5     cout << 812 << '|';
     6     cout.setf(ios_base::left, ios_base::adjustfield);//转为左对齐
     7     cout.width(10);//设置输出宽度为10
     8     cout << 813 << 815 << '
    ';
     9     cout.unsetf(ios_base::adjustfield);//取消之前调用setf()的效果
    10     cout.precision(2);//设置精度2
    11     cout.setf(ios_base::uppercase | ios_base::scientific);//使用科学技术法输出,且对于16进制输出时,使用大写字母,E表示。
    12     cout << 831.0;
    13     cout.flags(original_flags);//恢复初始参数
    14     return 0;
    15 }

     11-3

     1 #include<fstream>  
     2 #include<iostream>
     3 using namespace std;
     4 
     5 int main() {
     6     ofstream outf("d://out.txt");
     7     outf << "已成功写入文件" << endl;
     8     outf.close();
     9     return 0;
    10 }

    11-4

     1 #include<fstream>  //ifstream
     2 #include<iostream>
     3 #include<string>     //包含getline()
     4 using namespace std;
     5 
     6 
     7 int main() {
     8     string s;
     9     ifstream inf("d://out.txt");        
    10                              //打开文件
    11     
    12     while (getline(inf, s)) //逐行读取,直到结束
    13     {
    14         cout << s << endl;
    15     }
    16     inf.close();
    17     return 0;
    18 }

    应用练习(1)

     1 #include<iostream>
     2 #include<fstream>
     3 #include<string>
     4 #include<cstdlib>
     5 #include<ctime>
     6 const int MAX = 83;
     7 using namespace std;
     8 
     9 int main() {
    10     ifstream inf("d://list.txt");//打开文件
    11     if (!inf) {
    12         cout << "ERROR" << endl;
    13         return 1;
    14     }
    15     string STU[MAX];
    16     string str;
    17     int i=0;
    18     while (getline(inf, str)) //逐行读取,直到结束
    19     {
    20         STU[i]=str;
    21         i++;
    22     }
    23     ofstream outf("d://out.txt");
    24     srand(time(NULL));//设置种子值
    25     int a;
    26     for (int i = 0; i<5; i++)
    27     {
    28         a = rand() % MAX + 1;//生成随机数
    29         cout << STU[a] << endl;
    30         outf << STU[a] << endl;
    31     }
    32     inf.close();
    33     outf.close();
    34     return 0;
    35 }

    应用练习(2)

     1 #include<iostream>
     2 #include<string>
     3 #include<fstream>
     4 using namespace std;
     5 int main()
     6 {
     7     string pas;
     8     cout << "输入地址文件名:" << endl;
     9     cin >> pas;
    10     ifstream fin(pas);
    11     if (!fin) {
    12         cout << "打开错误" << endl;
    13         return 1;
    14     }
    15     long line = 1, word = 0, ch = 0;//行数,单词数,字符数 
    16     char c;
    17     while (fin.get(c))
    18     {
    19             ch++;
    20             if ((c < 'A' || c > 'Z'&&c < 'a' || c > 'z')&&c!='
    ')
    21                 ++word;
    22             if (c == '
    ')
    23                 ++line;
    24     }
    25     cout << "字符数:" << ch << endl << "单词数:" << word << endl << "行数:" << line<<endl;
    26     fin.close();
    27     return 0;
    28 }

  • 相关阅读:
    Mybatis(spring)(多个参数)(插入数据返回id)
    乱码的情况
    struts2常用类型的Result
    struts2中的session使用
    linux查看端口占用程序
    webservice笔记
    Java读文件夹
    JSON笔记
    about Base64
    【转载】MyEclipse6.5 KeyGen
  • 原文地址:https://www.cnblogs.com/zhibifenli/p/9203557.html
Copyright © 2020-2023  润新知