String library
method
#include <string>
#include <iostream>
using namespace std;
int main(){
string word = "good";
word.length(); // ๊ธธ์ด ๋ฆฌํด
word.empty(); // ๋น ๋ฌธ์์ด์ธ์ง 1, 0๊ฐ ๋ฆฌํด
word.clear(); // ๋ฌธ์์ด ์ญ์
word += "-bye"; // ๋ํ๊ธฐ ๊ฐ๋ฅ
word[0]; // h
word[word.length() - 1]; // ๋ง์ง๋ง ๋จ์ด i
word.find("a"); // ํด๋น ๋ฌธ์์ด์ด ์์ผ๋ฉด string::npos ๋ฆฌํด
return 0;
}substr
string word = "good-bye";
word.substr(2, 5);
rand()
rand()๋ ๋์๋ฅผ ๋ฐ์์ํค๋ ํจ์์ด๋ค.
ํ์ง๋ง ์ฌ์ค ์์ฐ์ ์ผ๋ก ๋ฐ์ํ๋ ๋๋ค์ด๋ผ๋ ํ์์ ์ปดํจํฐ ๋ด์์ ๊ตฌ์ฑํ๊ธฐ๋ ์ฝ์ง ์๋ค.
๋ฐ๋ผ์ ์ด๋ฏธ ์ ์ฅ๋ ๋์ํ๋ก ๋ถํฐ ์ด ๊ฐ๋ค์ ๋ถ๋ฌ์ค๊ฒ ๋๋๋ฐ, ์ด๊ฒ์ ๊ธฐ๋ฐ์ผ๋ก ๊ฐ์ ๋ถ๋ฌ์ค๋ ๊ฒ์ดย rand()ย ์ด๋ค.
#include <iostream>
using namespace std;
int main(){
cout << rand() << endl;
cout << rand() << endl;
cout << rand() << endl;
cout << rand() << endl;
return 0;
}// output
41
18467
6334
26500
์๋ฌด๋ฐ ์ธํ ์ ํ์ง ์๋๋ค๋ฉด ๊ธฐ๋ณธ ์ ์ฅ๋ ๋์ํ์์ ์ด๊ฒ์ ๊ฐ์ ธ์จ๋ค. ์ด ๋์ํ๋ฅผ ์ธํ ํ๋ ๊ฒ์ seed๋ฅผ ์ ํ๋ค๊ณ ํ๋๋ฐ, ๊ธฐ๋ณธ ๊ฐ์ 1์ด๋ค.
๋ฐ๋ผ์ ๋ด๊ฐย rand()ย ๋ฅผ ์ฌ๋ฌ๋ฒ ์น๋๋ผ๋ ํญ์ ๊ฐ์ ๊ฐ์ด ์์ฐจ์ ์ผ๋ก ์ถ๋ ฅ๋ ๊ฒ์ด๋ค.
๊ทธ๋ฌ๋ฉด ์ ๋ง ๋๋ค์ ์ด๋ป๊ฒ ๋ง๋ค ์ ์์๊น? ์ด ์๋๊ฐ ์์ฒด๋ฅผ ๊ณ์ํด์ ์๋ก์ด ๊ฐ์ ๋ฃ์ด์ฃผ๋ฉด ๋๋ค.
๊ทธ๋ฌ๋ฉด ๊ณ์ํด์ ์๋ก์ด ๊ฐ์ ์ฃผ๋ ๋ฐฉ๋ฒ์๋ ์ด๋ค๊ฒ์ด ์์๊น? ์ด ์๋๊ฐ์ย ํ์ฌ ์๊ฐ(์ด)ย ๋ฅผ ๋์ ํด์ฃผ๋ ๊ฒ์ด๋ค.
์ด ๋ย ctime libraryย ๋ฅผ ์ฌ์ฉํ๋๋ฐ, time()ย ํจ์๋ฅผ ์ฌ์ฉํ๋ฉด, 1970๋
1์ 1์ผ๋ถํฐ ํ์ฌ๊น์ง์ ๋์ ์๊ฐ(์ด)๋ฅผ ๋ฐํํ๊ธฐ ๋๋ฌธ์ ํ๋ก๊ทธ๋จ ์คํ์๋ง๋ค ์ ํ ์๋ก์ด ์๊ฐ๊ฐ์ ์
๋ ฅํ ์ ์๋ค.
srand()
์ด ์๋๊ฐ์ ์ค์ ํ ์ ์๊ฒ ํด์ฃผ๋ ํจ์๋ย srand()ย ์ด๋ค. (set random) ์ฝ๋๋ฅผ ๋ง๋ค์ด๋ณด๋ฉด,
#include <iostream>
#include <ctime>
using namespace std;
int main(){
srand((unsigned int)time(0));
cout << rand() << endl;
cout << rand() << endl;
cout << rand() << endl;
cout << rand() << endl;
return 0;
}srand()ย ์ ํ๋ผ๋ฏธํฐ๋ย unsigned intย ํ์
์ ๊ฐ์ด๋ค. ๊ทธ๋ ๊ธฐ ๋๋ฌธ์ ๋ฐํ๋ย time()ย ์ ๊ฐ์ ํ๋ณํ ํด์ค์ผ ํ๋ค.
์ํ๋ ๋ฒ์์ ์ ์ป๊ธฐ
#include <iostream>
#include <ctime>
using namespace std;
int main(){
srand((unsigned int)time(0));
// 0 ~ 99 ์ ๋์
int nRandom0 = rand() % 100;
// 1 ~ 100 ์ ๋์
int nRandom1 = rand() % 100 + 1;
return 0;
}๋ชจ๋๋ฌ ์ฐ์ฐ์ ํตํด์ ์ํ๋ ๋ฒ์์ ๊ฐ์ ์ป์!
์ฑ์ ๊ณ์ฐ ํ ์ถ๋ ฅํ๋ ํ๋ก๊ทธ๋จ
- ๋ฐ์ดํฐ ํ์ผ๋ก ์ ์ฅ๋์ด ์๋ ํ์ผ์ ์ฝ์ด์
- ํ๊ท ์ ๋ธ ๋ค,
- ๋ฒ์์ ๋ฐ๋ผ ์ฑ์ ์ ์ฐ์ถํ๋ ํ๋ก๊ทธ๋จ์ ๋ง๋ ๋ค.
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
bool getStu(ifstream &fin, int &id, int &exam1, int &exam2, int &exam3){
fin >> id >> exam1 >> exam2 >> exam3;
if(!fin)
return false;
return true;
}
void calcAvgGrade(int exam1, int exam2, int exam3, int &avg, char &grade){
avg = (exam1 + exam2 + exam3)/3;
if(avg >= 90)
grade = 'A';
else if(avg >= 80)
grade = 'B';
else
grade = 'F';
}
void writeStu(ofstream &fout, int &id, int &avg, char &grade){
fout << setw(4) << id << setw(4) << avg << setw(4) << grade << endl;
}
int main(){
// ํ์ผ์ ์ฝ๊ณ , ๊ฐ๊ฐ์ ๊ฐ์ ๋ฐ๋๋ฐ,
// ํ์ผ์์๋ id exam1 exam2 exam3 ์์ผ๋ก ์ ์ฅ๋์ด ์์
// ํ๊ท 1+ 2 + 3/ 3 grade ๊ณ์ฐ
// id avg grade ์ ์ฅ
ifstream fin("ch7STUFL.DAT");
ofstream fout("output.txt");
int id, exam1, exam2, exam3, avg;
char grade;
while(getStu(fin, id, exam1, exam2, exam3)){
calcAvgGrade(exam1, exam2, exam3, avg, grade);
writeStu(fout, id, avg, grade);
}
fin.close();
fout.close();
return 0;
}output.txt
90 90 A
89 89 B
81 81 B
79 79 F
69 69 F
60 60 F
59 59 F