본문 바로가기

카테고리 없음

C++ 답안

<01-1>

문제1 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
 
using namespace std;
 
int main() {
    int i;
    int j;
    int num = 0;
    for (i=1; i <=5; i++) {
        cout << i<<"번째 정수 입력:";
        cin >> j;
        num += j;
    }
 
    cout << "합계: " << num << endl;
    return 0;
}
cs


문제2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
 
using namespace std;
 
int main() {
 
    char name[100];
    char phone[100];
    cout << "이름 입력: ";
    cin >> name;
    cout << "전화번호 입력: ";
    cin >> phone;
 
    cout << "이름:" << name << endl;
    cout << "전화번호:" << phone << endl;
 
    return 0;
}
cs

문제3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
 
using namespace std;
 
int main() {
    int num;
    cout << "원하는 구구단을 입력하시오";
    cin >> num;
 
    for ( int i=1 ; i < 10 ; i++)
    {
        cout << num << "x" << i << "="<<num*<< endl;
    }
 
}
cs

문제4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
 
using namespace std;
 
int main() {
    
    for (; ; ) {
        cout << "판매금액을 만원 단위로 입력(-1 to end)";
        int sale;
            cin >>sale;
            if (sale == -1)
                break;
 
            cout << "이번달 급여: " << 50 + sale*0.12 <<"만원"<<endl;
    }
}
cs


<문제 01-2>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void swap(int* _num1, int* _num2)
{
    int temp = *_num1;
    *_num1 = *_num2;
    *_num2 = temp;
}
 
void swap(char* _num1, char* _num2)
{
    char temp = *_num1;
    *_num1 = *_num2;
    *_num2 = temp;
}
 
void swap(double* _num1, double* _num2)
{
    double temp = *_num1;
    *_num1 = *_num2;
    *_num2 = temp;
}
cs



<문제 01-3>


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>
 
using std::endl;
using std::cout;
 
 
 
int main() {
    cout << "[3,3,3]: " << BoxVolume(333<< endl;
    cout << "[5,5,D]: " << BoxVolume(55<< endl;
    cout << "[7,D,D]: " << BoxVolume(7<< endl;
    //cout << "[D,D,D]" << BoxVolume() << endl;
 
 
    return 0;
}
 
int BoxVolume(int length, int width, int height)
{
    return length*width*height;
}
int BoxVolume(int length, int width)
{
    return length*width;
}
int BoxVolume(int length)
{
    return length;
}
 
cs


-------------------------------------------------------------------------------------------------------------


<문제 02-1>


문제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
#include <iostream>
 
using std::cout;
using std::cin;
using std::endl;
 
void AA(int &_num)
{
    _num += 1;
}
 
void BB(int &_num)
{
    _num *= -1;
}
 
int main()
{
    int i = 10;
    AA(i);
    cout << i << endl;
 
    BB(i);
    cout << i << endl;
    return 0;
}
cs


문제2


매개변수로 받고 있는 인자가 상수 참조는 상수로 초기화 불가


문제3

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
#include <iostream>
using namespace std;
 
void SwapPointer(int* &_ptr1, int* &_ptr2);
 
int main(){
    int num1 = 5;
    int *ptr1 = &num1;
    int num2 = 10;
    int *ptr2 = &num2;
 
    cout << *ptr1 << endl;
    cout << *ptr2 << endl;
 
    SwapPointer(ptr1, ptr2);
    cout << *ptr1 << endl;
    cout << *ptr2 << endl;
}
 
 
void SwapPointer(int* &_ptr1, int* &_ptr2)
{
    int *temp = _ptr1;
    _ptr1 = _ptr2;
    _ptr2 = temp;
}

cs


<문제 02-2>


1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;
 
int main()
{
    const int num = 12;
    const int* ptr = &num;
    const int &dptr = *ptr;
 
    cout << *ptr << endl;
    cout << dptr << endl;
 
}
cs



------------------------------------------------------------------------------------------

문제 03-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
34
35
36
37
#include <iostream>
#include <stdlib.h>
using namespace std;
 
typedef struct Point
{
    int xpos;
    int ypos;
 
    void MovePos(int x, int y) {
        xpos += x;
        ypos += y;
    }
    void AddPoint(const Point &Pos) {
        xpos+=Pos.xpos;
        ypos+=Pos.ypos;
    }
    void ShowPosition() {
        cout << xpos << endl;
        cout << ypos << endl;
    }
}Point;
 
int main()
{
    Point pos1 = { 12,4 };
    Point pos2 = { 20,30 };
 
    pos1.MovePos(-710);
    pos1.ShowPosition(); // [5,14]출력
 
    pos1.AddPoint(pos2);
    pos1.ShowPosition(); // [25,44]출력
    return 0;
}
 
 
cs




문제 03-2


문제 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
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
#include <iostream>
#include <stdlib.h>
using namespace std;
 
class Calculator {
private:
    double x;
    double y;
    int numOfAdd;
    int numOfDiv;
    int numOfMin;
    int numOfMul;
public:
 
    void Init() {
        double _x = x;
        double _y = y;
        numOfAdd = 0;
        numOfDiv = 0;
        numOfMin = 0;
        numOfMul = 0;
    }
    void ShowOpCount() {
        cout << "덧셈: " << numOfAdd << " ";
        cout << "뺄셈: " << numOfMin << " ";
        cout << "곱셈: " << numOfMul << " ";
        cout << "나눗셈: " << numOfDiv << " ";
    }
    double Mul(double _x, double _y) {
        numOfMul++;
        return _x*_y;
    }
    double Add(double _x,double _y) {
        numOfAdd++;
        return _x+_y;
    }
    double Div(double _x, double _y) {
        numOfDiv++;
        return _x / _y;
    }
    double Min(double _x, double _y) {
        numOfMin++;
        return _x - _y;
    }
};
 
int main()
{
    Calculator cal;
    cal.Init();
    cout << "3.2 + 2.4 = " << cal.Add(3.22.4<< endl;
    cout << "3.5 / 1.7 = " << cal.Div(3.51.7<< endl;
    cout << "2.2 - 1.5 = " << cal.Min(2.21.5<< endl;
    cout << "4.9 / 1.2 = " << cal.Div(4.91.2<< endl;
    cal.ShowOpCount();
    return 0;
}
 
cs


`


문제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
#include <iostream>
#include <stdlib.h>
using namespace std;
 
class Printer {
private:
    char str[50];
 
public:
    char SetString(char* p) { // 문자열 저장
        strcpy_s(str, p);
        return 0;
    }
    char ShowString() { // 문자열 출력
        cout << str << endl;
        return 0;
    }
};
 
int main()
{
    Printer pnt;
    pnt.SetString("Hello World");
    pnt.ShowString();
 
    pnt.SetString("I Love C++");
    pnt.ShowString();
    return 0;
}
cs