實驗7
阿新 • • 發佈:2018-06-21
CI 之前 space CA stream img list flags 成功
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);//輸出域寬度改為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("test1.txt"); file<<"已成功寫入文件!"<<endl; file.close(); return 0; }
文件截圖
11-4
#include<iostream> #include<fstream> using namespace std; int main(){ ifstream file; file.open("test1.txt"); if(!file){cout<<"fail to open"<<endl;} char ch; while((ch=file.get())!=EOF){cout.put(ch);} file.close(); if(cin>>ch); return 0; }
運行截圖
2.1 代碼
#include<iostream> #include<fstream> #include<ctime> #include<cstdlib> #include<string> using namespace std; class Dice { public: Dice(int a) {sides = a;} int cast() { int b; b = rand() % sides + 1; return b;} private: int sides;}; int main() { Dice random(83); srand(time(NULL)); ifstream list; list.open("list.txt"); if(!list) cout<<"fail to open"<<endl; string info[83],str; int i=0,stu; while(getline(list,str)){ info[i]=str; i++; } list.close(); ofstream roll; roll.open("roll.txt"); for(i=0;i<5;i++){ stu=random.cast(); cout<<info[stu-1]<<endl; roll<<info[stu-1]<<endl; } roll.close(); return 0; }
運行截圖
實驗7