C++

[C++] λͺ…ν’ˆ c++ 6μž₯

Meaning_ 2022. 12. 4. 01:44
728x90
λ°˜μ‘ν˜•

πŸ₯‘ λ“€μ–΄κ°€κΈ° 전에

 

static λ©€λ²„λŠ” λ°˜λ“œμ‹œ 전역곡간에 μ„ μ–Έν•¨μœΌλ‘œμ¨ λ³€μˆ˜κ³΅κ°„μ„ ν• λ‹Ήν•΄μ•Ό ν•œλ‹€.

static λ©€λ²„ν•¨μˆ˜λŠ” 였직 static λ©€λ²„λ“€λ§Œ 접근이 κ°€λŠ₯ν•˜λ‹€.

non-static λ©€λ²„ν•¨μˆ˜λŠ” static멀버,non-static 멀버 λͺ¨λ‘ 접근이 κ°€λŠ₯ν•˜λ‹€.

thisμ‚¬μš© ν•  수 μ—†λ‹€. (객체가 생기기 μ „λΆ€ν„° 호좜이 κ°€λŠ₯ν•˜κΈ° λ•Œλ¬Έ)

 

2번

 

#include <iostream>
#include <string>
#include<cmath>
#include <ctime>
#include<algorithm>

using namespace std;

class Person {
	int id;
	double weight;
	string name;

public:
	Person() {
		id = 1;
		name = "Grace";
		weight = 20.5;
	};
	Person(int id,string name) {
		this->id = id;
		this->name = name;
		weight = 20.5;

	}
	Person(int id, string name, double weight) {
		this->id = id;
		this->name = name;
		this->weight = weight;

	}
	void show() {
		cout << id << ' ' << weight << ' ' << name << endl;
	}
};

int main() {

	Person grace = Person(); 
	Person ashely = Person(2, "Ashely");
	Person helen=Person(3, "Helen", 32.5);
	grace.show();
	ashely.show();
	helen.show();
}

(2) λ””ν΄νŠΈ λ§€κ°œλ³€μˆ˜ 썼을 λ•Œ 

#include <iostream>
#include <string>
#include<cmath>
#include <ctime>
#include<algorithm>

using namespace std;

class Person {
	int id;
	double weight;
	string name;

public:
	Person(int id = 1, string name = "Grace", double weight = 20.5);
	void show() {
		cout << id << ' ' << weight << ' ' << name << endl;
	}
};

Person::Person(int id, string name, double weight) {
	this->id = id;
	this->name = name;
	this->weight = weight;
}

int main() {

	Person grace = Person(); 
	Person ashely = Person(2, "Ashely");
	Person helen=Person(3, "Helen", 32.5);
	grace.show();
	ashely.show();
	helen.show();
}

μš°μ„  λ””ν΄νŠΈ λ§€κ°œλ³€μˆ˜λ‘œ μ •μ˜ν•΄μ€€ λ‹€μŒμ—

λ‚˜μ€‘μ— λ³€μˆ˜λ‘œ λ“€μ–΄μ˜¬ 애듀을 μœ„ν•΄μ„œ 전역에 λ‹€μ‹œ μ •μ˜!

 

λͺ¨λ“  default parameterλŠ” 였λ₯Έμͺ½λΆ€ν„° μ§€μ •ν•΄μ•Ό ν•œλ‹€. κ·Έλž˜μ„œ λ‹€μŒμ€ ν—ˆμš©λ˜μ§€ μ•ŠλŠ”λ‹€.

void printValue(int x=10, int y); // not allowed

 

5번

 

#include <iostream>
#include <string>
#include<cmath>
#include <ctime>
#include<algorithm>

using namespace std;

class ArrayUtility {

public:
	
	static void intToDouble(int source[], double dest[], int size);
	static void doubleToInt(double source[],int dest[], int size);

};

void ArrayUtility::intToDouble(int source[], double dest[], int size) {
	for (int i = 0; i < size; i++) {
		dest[i] = (double)source[i];
	}
}

void ArrayUtility::doubleToInt(double source[], int dest[], int size) {
	for (int i = 0; i < size; i++) {
		dest[i] = (int)source[i];
	}
}



int main() {
	int x[] = { 1,2,3,4,5 };
	double y[5];
	double z[] = { 9.9,8.8,7.7,6.6,5.6 };
	ArrayUtility::intToDouble(x, y, 5);
	for (int i = 0; i < 5; i++) {
		cout << y[i] << ' ';
	}
	cout << endl;

	ArrayUtility::doubleToInt(z, x, 5);
	for (int i = 0; i < 5; i++) {
		cout << x[i] << ' ';
	}
	cout << endl;
	

}

 

 

7번

 

#include <iostream>
#include <string>
#include<cmath>
#include <ctime>
#include<algorithm>

using namespace std;

class Random {
public:
	static void seed() {
		srand((unsigned)time(0));

	}

	static int nextInt(int min = 0, int max = 32767);
	static char nextAlphabet();
	static double nextDouble();
};


//void Random::seed() {}; // μ΄λ ‡κ²Œ μž¬μ •μ˜ν•˜λ©΄ μ•ˆλ¨ 이미 ν΄λž˜μŠ€μ—μ„œ μ„ μ–Έλœ 애이기 λ•Œλ¬Έ
int Random::nextInt(int min,int max) { // λ””ν΄νŠΈ λ§€κ°œλ³€μˆ˜λ„ μž¬μ •μ˜ ν•˜λ©΄ μ•ˆλ¨! κ·Έλƒ₯ min,max둜 λ‘˜κ²ƒ
	return rand() % (max - min + 1) + min; //srandκ°€ μ•„λ‹ˆλΌ randλ‹€!
};
char Random::nextAlphabet() {
	//A 65~90 a 97~122
	int randArr[2];
	randArr[0] = rand() % (90 - 65+1) + 65; //rand()%(b-a+1)+a
	randArr[1] = rand() % (122 - 97+1) + 97;
	int randIdx;
	randIdx = rand() % 2;


	return randArr[randIdx];

};
double Random::nextDouble() {

	//μ‹€μˆ˜ λ‚œμˆ˜
	//RAND_MAXλž€ rand ν•¨μˆ˜μ—μ„œ λ°˜ν™˜ν•  수 μžˆλŠ” μ΅œλŒ€κ°’

	double d = Random::nextInt();
	return d / RAND_MAX;
};



int main() {
	cout << "1μ—μ„œ 100κΉŒμ§€ λžœλ€ν•œ μ •μˆ˜ 10개λ₯Ό 좜λ ₯ν•©λ‹ˆλ‹€" << endl;
	Random::seed();
	for (int i = 0; i < 10; i++) {
		cout << Random::nextInt(1, 100) << ' ';
	}
	cout << endl;

	cout << "μ•ŒνŒŒλ²³μ„ λžœλ€ν•˜κ²Œ 10개λ₯Ό 좜λ ₯ν•©λ‹ˆλ‹€." << endl;
	for (int i = 0; i < 10; i++) {
		cout << Random::nextAlphabet() << ' ';
	}
	cout << endl;

	cout << "λžœλ€ν•œ μ‹€μˆ˜ 10개λ₯Ό 좜λ ₯ν•©λ‹ˆλ‹€." << endl;
	for (int i = 0; i < 5; i++) {
		cout << Random::nextDouble() << ' ';
	}
	cout << endl;
	for (int i = 0; i < 5; i++) {
		cout << Random::nextDouble() << ' ';
	}

}

 

 

1번

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <string>
#include<cmath>
#include <ctime>
#include<algorithm>
 
using namespace std;
 
int add(int* arr, int n) {
    int sum = 0;
    for (int i = 0; i < n; i++) {
        sum += arr[i];
    }
 
    return sum;
}
 
int add(int* arr, int n, int* arr2) {
 
    return add(arr, n) + add(arr2, n);
 
}
 
int main() {
    
    int a[] = { 1,2,3,4,5 };
    int b[] = { 6,7,8,9,10 };
 
    int c = add(a, 5);
    int d = add(a, 5, b);
    cout << c << endl;//15
    cout << d << endl;//55
}
cs

 

λ„ˆλ¬΄ λ‹Ήμ—°ν•œ μ†Œλ¦¬μΈλ° addν•¨μˆ˜ mainν•¨μˆ˜ 밖에 썼어야 ν–ˆλ‹€. 계속 μžλ°”λ§Œ μ“°λ‹€λ³΄λ‹ˆ μ–΄μ²˜κ΅¬λ‹ˆ μ—†λŠ” 짓도 ν•˜λŠ”λ“―..γ…Ž

 

 

2번

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <string>
#include<cmath>
#include <ctime>
#include<algorithm>
 
using namespace std;
 
int big(int num1, int num2) {
    int m = (num1 > num2) ? num1 : num2;
    if (m > 100) {
        return 100;
    }
    else {
        return m;
    }
}
 
int big(int num1, int num2, int num3) {
    return big(num1, num2) > num3 ? num3 : big(num1, num2);
}
 
 
int main() {
    int x = big(35);
    int y = big(30060);
    int z = big(306050);
 
    cout << x << ' ' << y << ' ' << z << endl;
}
cs

 

 

 

4번

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <string>
#include<cmath>
#include <ctime>
#include<algorithm>
 
using namespace std;
 
class MyVector {
    int* mem;
    int size;
 
public:
    
    MyVector(int n=100int val=0);
    ~MyVector() { delete[] mem; }
 
    void show();
};
 
 
 
 
MyVector::MyVector(int n, int val) {
    mem = new int[n];
    size = n;
    for (int i = 0; i < size; i++) {
        mem[i] = val;
    }
}
 
void MyVector::show() {
    for (int i = 0; i < size; i++) {
        cout << mem[i] << endl;
    }
}
 
int main() {
    MyVector a;
    MyVector b(3,10);
    a.show();
    b.show();
 
}
cs

 

μœ„μ—μ„œ 선언할땐 λ””ν΄νŠΈ λ§€κ°œλ³€μˆ˜ 써주고, μ •μ˜ν• λ•ŒλŠ” μ•ˆμ¨μ£ΌλŠ”κ²Œ 정석인것 κ°™λ‹€!

 

 

 

6번

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <string>
#include<cmath>
#include <ctime>
#include<algorithm>
 
using namespace std;
 
class ArrayUtility2 {
 
public:
    //
    static int* concat(int s1[], int s2[], int size);
    
    static int* remove(int s1[], int s2[],int size, int& retSize);
 
};
 
int* ArrayUtility2::remove(int s1[], int s2[], int size,int& retSize) {
 
    retSize = size;
    int idx = 0;
 
    int* arr = new int[size];
 
    for (int i = 0; i < size; i++) {
        int std = s1[i];
        for (int j = 0; j < size; j++) {
            if (std == s2[j]) {
                
                retSize--;
                
                break;
            }
            
            
        }
        arr[idx] = std;
        idx++;
        
    }
    
    return arr;
 
}
 
int* ArrayUtility2::concat(int s1[], int s2[], int size) {
    int* arr=new int[size*2];
 
    int i;
    for (i = 0; i < 5; i++) {
        arr[i] = s1[i];
    }
 
    for (int j = 0; j < 5; j++) {
        arr[i + j] = s2[j];
    }
    
    return arr;
}
 
 
 
int main() {
 
    int x[5], y[5];
 
    cout << "μ •μˆ˜λ₯Ό 5 κ°œ μž…λ ₯ν•˜λΌ. λ°°μ—΄ x에 μ‚½μž…ν•œλ‹€>>";
    for (int i = 0; i < 5; i++) cin >> x[i];
 
    cout << "μ •μˆ˜λ₯Ό 5 κ°œ μž…λ ₯ν•˜λΌ. λ°°μ—΄ y에 μ‚½μž…ν•œλ‹€>>";
    for (int i = 0; i < 5; i++) cin >> y[i];
 
    int* p = ArrayUtility2::concat(x, y, 5);
    cout << "ν•©μΉœ μ •μˆ˜ λ°°μ—΄μ„ μΆœλ ₯ν•œλ‹€" << endl;
    for (int i = 0; i < 10; i++) cout << p[i] << ' ';
    cout << endl;
 
    int retSize;
    int* q = ArrayUtility2::remove(x, y, 5, retSize);
    cout << "λ°°μ—΄ x[]μ—μ„œ y[]λ₯Ό λΊ€ κ²°κ³Όλ₯Ό μΆœλ ₯ν•œλ‹€. κ°œμˆ˜λŠ” " << retSize << endl;
    for (int i = 0; i < retSize; i++) cout << q[i] << ' ';
    cout << endl;
 
    delete[] p;
    delete[] q;
 
 
 
 
 
}
cs

9번

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <string>
#include<cmath>
#include <ctime>
#include<algorithm>
 
using namespace std;
 
class Board {
    
public:
    static string* text;
    static int size;
    static void add(string s);
    static void print();
};
 
int Board::size = 0;
string* Board::text = new string[100];
 
 
 
void Board::add(string s) {
    text[size= s;
    size++;
     
}
 
void Board::print() {
 
    cout<<"********** "<<"κ²Œμ‹œνŒμž…λ‹ˆλ‹€."<<" **********"<<endl;
    for (int i = 0; i < size; i++) {
        cout << i << ": ";
        cout << text[i] << endl;
    }
}
 
 
 
int main() {
    Board::add("μ€‘κ°„κ³ μ‚¬λŠ” κ°λ… μ—†λŠ” μžμœ¨ μ‹œν—˜μž…λ‹ˆλ‹€.");
    Board::add("μ½”λ”© λΌμš΄μ§€ λ§Žμ΄ μ΄μš©ν•΄ μ£Όμ„Έμš”.");
    Board::print();
    Board::add("μ§„μ†Œλ¦° ν•™μƒμ΄ κ²½μ§„λŒ€νšŒ μž…μƒν•˜μ˜€μŠ΅λ‹ˆλ‹€. μΆ•ν•˜ν•΄μ£Όμ„Έμš”.");
    Board::print();
}
cs

 

728x90
λ°˜μ‘ν˜•