2023.5.8
1 #include <iostream> 2 using namespace std; 3 #include <fstream> 4 #include <iomanip> 5 void test() 6 { 7 int num; 8 cout << "请输入一个十进制整数:" << endl; 9 cin >> num; 10 //十进制输出 11 cout << "十进制输出为:" << num << endl; 12 //八进制输出 13 cout << "八进制输出为:"<<oct << num << endl; 14 //十六进制输出 15 cout << "十六进制输出为:" << hex << num << endl; 16 } 17 int main() 18 { 19 test(); 20 return 0; 21 }
1 //11-9 2 #include <iostream> 3 using namespace std; 4 #include <fstream> 5 #include <iomanip> 6 #include <string> 7 void test() 8 { 9 ofstream ofs; 10 ofs.open("test1.txt", ios::out); 11 ofs << "我说我不再拥有" << endl; 12 ofs << "你说你还爱我" << endl; 13 ofs << "我说我说我开不了口" << endl; 14 ofs.close(); 15 } 16 void test01() 17 { 18 string buf; 19 int counter = 0; 20 ifstream ifs("test1.txt",ios::in); 21 ofstream ofs("test2.txt",ios::out); 22 if (!ifs.is_open()) 23 { 24 cout << "文件打开失败" << endl; 25 return; 26 } 27 while (getline(ifs, buf)) 28 { 29 ofs << ++counter << "." << buf << endl; 30 } 31 ifs.close(); 32 ofs.close(); 33 } 34 int main() 35 { 36 test(); 37 test01(); 38 return 0; 39 }
1 //11-10 2 #include <iostream> 3 using namespace std; 4 #include <fstream> 5 #include <iomanip> 6 #include <string> 7 void test() 8 { 9 int num = 0; 10 char c; 11 char a ='我'; 12 ifstream ifs; 13 ifs.open("test1.txt", ios::in); 14 if (!ifs.is_open()) 15 { 16 cout << "文件打开失败" << endl; 17 } 18 19 while ((c = ifs.get()) != EOF) 20 { 21 22 if (a == c) 23 { 24 num++; 25 } 26 } 27 ofstream ofs; 28 ofs.open("test2.txt", ios::out); 29 ofs <<"‘我’的个数为:" << num << endl; 30 ofs.close(); 31 ifs.close(); 32 } 33 int main() 34 { 35 test(); 36 return 0; 37 }