###### tags: `c++_beginner` # Week 03 > 這周4字串和檔案,我也不太熟QQ,一起加油。 第一個教學影片是一個合籍,請把系列看完,這樣test的部分會比較好懂。 1. [Link(String)](https://www.youtube.com/playlist?list=PLqjW-ORyj-hLKFq_ESmFpXDnaLKaTCMio)<iframe width="560" height="315" src="https://www.youtube.com/embed/videoseries?si=Bgr31iA_yEDGEO36&amp;list=PLqjW-ORyj-hLKFq_ESmFpXDnaLKaTCMio" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe> 2. [Link(File I/O)](https://youtu.be/EaHFhms_Shw?si=tKe6fIUyXvt866Dz)<iframe width="560" height="315" src="https://www.youtube.com/embed/EaHFhms_Shw?si=tKe6fIUyXvt866Dz" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe> ## String ### 講解 一定要去看影片:),這樣Test第一題會輕鬆很多。 A string variable contains a collection of characters surrounded by double quotes: ```cpp= // Include the string library #include <string> // Create a string variable string greeting = "Hello"; ``` ### 題目 1. [Strings](https://www.hackerrank.com/challenges/c-tutorial-strings/problem?isFullScreen=true) 2. [StringStream](https://www.hackerrank.com/challenges/c-tutorial-stringstream/problem?isFullScreen=true)(裡面有我沒寫上去的東西,也沒有在影片裡,但題目上解釋得頗清楚,所以就仔細看題吧哈哈) ## File I/O ### 講解 請去看影片:),我就懶。 就是檔案的操作:D ```cpp= #include<fstream>//標頭檔 ``` * write ⇢ 會蓋掉之前的文件內容 * append ⇢ 保留之前的內容,並接著新增內容(不會自動換行) * read ⇢ 讀取 ```cpp= fstream myFile; myFile.open("test.txt",ios::out);//write if(myFile.is_open()){ myFile << "Hello\n"; myFile << "This is second line.\n"; } myFile.close(); myFile.open("test.txt",ios::app);//append if(myFile.is_open()){ myFile << "Hello2\n"; } myFile.close(); string file_str=""; myFile.open("test.txt",ios::in);//read if(myFile.is_open()){ string line; while (getline(myFile, line)){ file_str+=line+"\n"; } } cout<< file_str; myFile.close(); ``` ### 題目 想不到吧,沒有線上題喔!請跟著 CodeBeauty 一起實作。 1. Ascii與字元轉換(文件檔裡應該是一堆數字,在CMD裡是可讀寫的字串) [Answer](https://github.com/Benny0w0Liu/CPlusPlus_practice/tree/main/Week03/VideoTask01) 2. 因為還沒學到Structure(這個應該在下禮拜),所以先跳過。 ## Test ### 01 : 解密碼 腫模半QQ邦邦太久沒去射箭隊,忘記大門密碼了。密碼只能口頭傳輸,但邦邦怕被別人聽到,所以想請一層用加密的方式說給邦邦聽。 1. 輸入一整數n,代表有多少種字元可能會被使用。 2. 輸入n個原始字元及其加密過後的樣子,格式為"字元":"加密過後的字元"。 3. 最後輸入一段加密過後的字串input(含空格)。 4. 加密過後的字元不會是input的子字串。 5. 輸出解密過後的字串。 Sample01(Morse Code): Input ``` 54 a:.- b:-... c:-.-. d:-.. e:. f:..-. g:--. h:.... i:.. j:.--- k:-.- l:.-.. m:-- n:-. o:--- p:.--. q:--.- r:.-. s:... t:- u:..- v:...- w:.-- x:-..- y:-.-- z:--.. 0:----- 1:.---- 2:..--- 3:...-- 4:....- 5:..... 6:-.... 7:--... 8:---.. 9:----. .:.-.-.- ,:--..-- ?:..--.. ':.----. !:-.-.-- /:-..-. (:-.--. ):-.--.- &:.-... ::---... ;:-.-.-. =:-...- +:.-.-. -:-....- _:..--.- ":.-..-. $:...-..- @:.--.-. .--. .--. ``` Output ``` pp ``` Sample02: Input ``` 12 a:Engineering c:0205 e:CSE f:lin g:Science h:Computer m:0505 n:Vocal o:0413 r:liu s:and t:lai 0205 0413 0505 CSE 0413 Vocal lin 0413 liu lai Computer CSE Science and Engineering lai ``` Output ``` come on for the gsat ``` > 學測加油 ### 02 : 抽獎活動 聖誕舞會有抽獎活動ㄧㄝˊ。來做一個抽獎遊戲吧! 首先我們有一個名單的文件檔: [Name.txt](https://hackmd.io/QtqztVJGTe6odqw_omzMFA#Nametxt%E5%85%A7%E5%AE%B9) 我們將從裡面隨機選出10個得4獎(禮物:Red Bull)的人,6個得三獎(禮物:Exchange coupon)的人,2個得二獎(禮物:Champagne)的人,以及一位獲得大獎的人(禮物:Polaroid)。 請將得獎名單輸出至CMD,並將每個人獲得的物品輸出成一個txt檔(檔名:"[Result.txt](https://hackmd.io/QtqztVJGTe6odqw_omzMFA#Resulttxt-%E5%85%A7%E5%AE%B9)")。 > *Hint:* > 亂數生成方法 > ```cpp= > #include<iostream> > #include<cstdlib> > #include<ctime> > > using namespace std; > > int main(){ > srand( time(NULL) ); > cout<<rand()%10 //範圍0~9 > } > ``` Sample CMD output: ``` Fourth Prize: 1:Bea 2:Kate 3:Abeque 4:Citali 5:Bardan 6:Adaline 7:Dru 8:Cheyanne 9:Halyn 10:Citali Third Prize: 1:Kael 2:Eric 3:Greenleigh 4:Dandre 5:Aria 6:Countee Second Prize: 1:Beachy 2:Greenleigh First Prize: Jamaica! ``` ### 03 : 計算機 https://youtu.be/j4zHXeulgNY?si=D7shLRmlAncWrgPB 顧名思義做一個計算機 Input ``` 70+(2*(2+5)-9/(1-4))*2-2*3 ``` Output ``` 98.000000 ``` ##### Name.txt內容 ``` Farren Joziah Maxton Elijah Liam Lucas Aace Aby Bardalph Dandre Eulyses Evans Darold Jaxton Jayleen Jalen Jacoby Arnelle Cheyanne Kaydence Lilith Amelia Aria Halyn Kachina Alchemy Erlina Erma Stetson Tiauna Stormin Barbi Citali Aviana Calla Ignacio Naveli Nimah Mia Abriella Barbie Bea Frisa Friday Fairen Kael Aiden Emma Grayson Beale Daryle Addisyn Bohdi Eric Beacher Beachy Dru Cimarron Oriana Mahela Jamaica Liseli Magaska Nevaeh Greenleigh Grecia Kaydee Isabella Noah Olivia Abeque Adaline Evolet Felisha Jackson Oliver Riley Bard Jace Darrien Caden Mason Acalan Bailei Banner Banning Bardan Bartel Satchel Countee Scout Bick Cimberleigh Clarisa Cleeve Cody Dalinda Dawnyelle Eorkonweald Benny Kate ``` ##### Result.txt 內容 ``` Farren Joziah Maxton Elijah Liam Lucas Aace Aby Bardalph Dandre,Exchange coupon Eulyses Evans Darold Jaxton Jayleen Jalen Jacoby Arnelle Cheyanne,Red Bull Kaydence Lilith Amelia Aria,Exchange coupon Halyn,Red Bull Kachina Alchemy Erlina Erma Stetson Tiauna Stormin Barbi Citali,Red Bull Aviana Calla Ignacio Naveli Nimah Mia Abriella Barbie Bea,Red Bull Frisa Friday Fairen Kael,Exchange coupon Aiden Emma Grayson Beale Daryle Addisyn Bohdi Eric,Exchange coupon Beacher Beachy,Champagne Dru,Red Bull Cimarron Oriana Mahela Jamaica,Polaroid Liseli Magaska Nevaeh Greenleigh,Champagne,Exchange coupon Grecia Kaydee Isabella Noah Olivia Abeque,Red Bull Adaline,Red Bull Evolet Felisha Jackson Oliver Riley Bard Jace Darrien Caden Mason Acalan Bailei Banner Banning Bardan,Red Bull Bartel Satchel Countee,Exchange coupon Scout Bick Cimberleigh Clarisa Cleeve Cody Dalinda Dawnyelle Eorkonweald Benny Kate,Red Bull ``` {%hackmd @25H8NTF5SwujU3QUSA0oog/BkiNnsk56 %}