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;
}

๋ชจ๋“ˆ๋Ÿฌ ์—ฐ์‚ฐ์„ ํ†ตํ•ด์„œ ์›ํ•˜๋Š” ๋ฒ”์œ„์˜ ๊ฐ’์„ ์–ป์ž!

์„ฑ์  ๊ณ„์‚ฐ ํ›„ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ

  1. ๋ฐ์ดํ„ฐ ํŒŒ์ผ๋กœ ์ €์žฅ๋˜์–ด ์žˆ๋Š” ํŒŒ์ผ์„ ์ฝ์–ด์„œ
  2. ํ‰๊ท ์„ ๋‚ธ ๋’ค,
  3. ๋ฒ”์œ„์— ๋”ฐ๋ผ ์„ฑ์ ์„ ์‚ฐ์ถœํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ ๋‹ค.
#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