实验七
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<iostream>
#include<fstream>
using namespace std;
int main(){
ofstream file("text1.txt");
if(!file){
cout<<"文件打开失败..."<<endl;
}
file<<"已成功写入文件"<< endl;
file.close();
return 0;
}
11-4
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main(){
ifstream file("text1.txt");
if(!file){
cout<<"文件打开失败..."<<endl;
}
string s1;
file>>s1;
cout<<s1<<endl;
file.close();
return 0;
}
list.cpp
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<iomanip>
#include<ctime>
using namespace std;
class Student{
public:
int num;
long long int number;
string names;
string classes;
};
int main(){
srand(time(NULL));
ifstream list("C:\\Users\\Admin\\Desktop\\fishc.c\\list.txt");
ofstream roll("C:\\Users\\Admin\\Desktop\\fishc.c\\roll.txt");
if(!list){
cout<<"文件打开失败..."<<endl;
}
if(!roll){
cout<<"文件打开失败..."<<endl;
}
vector<Student> data;
Student newdata;
while(list>>newdata.num>>newdata.number>>newdata.names>>newdata.classes){
data.push_back(newdata);
}
list.close();
roll.close();
char choice;
cout<<"是否开始点名?YES(Y)/NO(N)"<<endl;
while(cin>>choice){
int rollnum;
if(choice=='N'){
break;
}
rollnum=rand()%data.size();
cout<<setw(10)<<left<<data[rollnum].num<<setw(20)<<left<<data[rollnum].number<<setw(10)<<left<<data[rollnum].names<<setw(10)<<left<<data[rollnum].classes<<endl;
cout<<"是否继续点名?YES(Y)/NO(N)"<<endl;
}
return 0;
}
english.cpp
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main(){
string fielnames;
cin>>fielnames;
ifstream ifiel(fielnames);
if(!ifiel){
cout<<"文件打开失败...."<<endl;
}
char ch;
int ch_num=0;
int word_num=0;
int l_num=0;
while(ifiel>>ch){
if(ch==' '||ch==','||ch=='.'||ch=='!'||ch=='?'){
word_num++;
ch_num++;
}else if(ch=='\n'){
l_num++;
}else{
ch_num++;
}
}
cout<<"ch="<<ch_num<<" word="<<word_num<<" line="<<l_num<<endl;
ifiel.close();
return 0;
}