码迷,mamicode.com
首页 > 其他好文 > 详细

实验七

时间:2018-06-19 16:11:44      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:img   null   close   图片   清除   test   表示   flag   oid   

实验七

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);//设置数据域宽度 
    cout<<813<<815<<‘\n‘;
    cout.unsetf(ios_base::adjustfield);//清除标志位 
    cout.precision(2);//设置显示精度 
    cout.setf(ios_base::uppercase|ios_base::scientific);//设置标志位 ,十六进制,浮点数方式输出 
    cout<<813.0;
    cout.flags(original_flags);
    return 0;
} 

技术分享图片

11-3

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
    ofstream out("test1.txt");
    if(!out)
    {
        cout<<"fail to open"<<endl;
        return 1;
    }
    out<<"已成功写入文件";
    out.close();
    return 0;
} 

技术分享图片

11-4

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

技术分享图片

7-1

#include<iostream>
#include<vector>
#include<time.h>
#include<fstream>
#include<stdlib.h>
using namespace std;
struct student{
    int num;
    string number,name,cla;
}stu;
void output(student &stu)
{
    cout<<stu.num<<" "<<stu.number<<" "<<stu.name<<" "<<stu.cla<<endl;
}
int main()
{
    int i;
    ifstream in("list.txt");
    if(!in){
        cout<<"fail to open"<<endl;
        return 1;
    }
    vector<student>a;
    for(i=0;i<83;i++)
    {
        in>>stu.num>>stu.number>>stu.name>>stu.cla;
        a.push_back(stu);
    }
    srand(unsigned(time(NULL)));
    for(i=0;i<5;i++)
    {
        int j=rand()%83;
        output(a[j]);
        cout<<endl;
    }
    in.close(); 
    return 0;
}

我在这里输出来的全是0,不懂为什么用下标表示出来是不行的

7-2

#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
int main()
{
    ifstream in("english.txt");
    if(!in){
        cout<<"fail to open"<<endl;
        return 1;
    }
    int words=0,lines=0,charnum=0;
    string w;
    while(getline(in,w)){
         const char *p=w.c_str();
         for(int i=0;i<w.length();i++)
         {
            if(p[i]==‘ ‘) words++;
         }
         words++;
         lines++;
         charnum+=w.size();
    }
    cout<<"单词数:"<<words<<" "<<"行数:"<<lines<<" "<<"字符数:"<<charnum<<endl;
    in.close();
    return 0;
}

技术分享图片

实验七

标签:img   null   close   图片   清除   test   表示   flag   oid   

原文地址:https://www.cnblogs.com/zengiai/p/9198438.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!