实验七

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 << 831.0;
    cout.flags(original_flags);
    //恢复原来的格式设置 

    
    return 0;
} 
复制代码

 

 

11-3

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

 

 

11-4

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

 

list

复制代码
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<iomanip> 
#include<cstdlib>
#include<time.h>
using namespace std;

class students {
public:
    int n;
    long long int num;
    string name;
    string cla;
};

int main() {
    int t;
    ifstream infile;
    infile.open("list.txt");
    if (!infile) {
        cout << "fail to open" << endl;
        return 1;
    }

    vector<students> stu;
    students stu1;
    
    while (infile >> stu1.n >> stu1.num >> stu1.name >> stu1.cla) {
        stu.push_back(stu1);
    }
    
    cout<<stu.size()<<endl; 
    
    infile.close();
    
    ofstream outfile;
    outfile.open("roll.txt",ios::app);
    if (!outfile) {
       cout << "fail to open" << endl;
       return 1;
    }
    
    srand(time(0));
    for (int i = 0; i < 5; i++) {
        t=rand()%stu.size();
        
        cout << setfill(' ') << left << setw(4) << stu[t].n 
        << setfill(' ') << right << setw(11) << stu[t].num << ' ' 
        << setfill(' ') << left << setw(8) << stu[t].name
        << stu[t].cla << endl;
        
        outfile << setfill(' ') << left << setw(4) << stu[t].n 
        << setfill(' ') << right << setw(11) << stu[t].num << ' ' 
        << setfill(' ') << left << setw(8) << stu[t].name
        << stu[t].cla << endl;
    }
    
    char ch='Y';
    while(ch=='Y'){
        cout<<"是否继续点名?Yes(Y) or No(N)" << endl;
        while(cin>>ch){
           if(ch=='Y'){
              t=rand()%stu.size();
           
              cout << setfill(' ') << left << setw(4) << stu[t].n 
              << setfill(' ') << right << setw(11) << stu[t].num << ' ' 
              << setfill(' ') << left << setw(8) << stu[t].name
              << stu[t].cla << endl;
           
              outfile << setfill(' ') << left << setw(4) << stu[t].n 
              << setfill(' ') << right << setw(11) << stu[t].num << ' ' 
              << setfill(' ') << left << setw(8) << stu[t].name
              << stu[t].cla << endl;
           }
           else
             break;
        }
    }
    outfile.close();

    return 0;
}
复制代码

 

English

复制代码
#include<fstream>
#include<iostream>
#include<string.h>
#define N 1000 
using namespace std;
int main(){
    ifstream infile;
    infile.open("English.txt");
    
    if(!infile){
        cout<<"fail to open"<<endl;
        return 1;
    }
    
    int zifu=0,words=0,lines=0;
    
    char ch[N];
    while(infile.getline(ch,N)){
        for(int i=0;i<strlen(ch);i++){
            ++zifu;
            if(ch[i]=='.'||ch[i]==' ')
            ++words;
        }
        ++lines;
    }
    

    cout<<"num of zifu is "<<zifu<<endl
    <<"num of words is "<<words<<endl
    <<"num of lines is "<<lines;
    
    return 0;
}
复制代码

 

 

这几天忙着刷oj,做的比较匆忙,有些附加没来及做,后面会带着补充的

posted @   千秋诗  阅读(121)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示