새소식

🏫 School/[23-2] 객체지향프로그래밍

[C++] Lab #01&02

  • -

[기초 1]

문제 | 아래 프로그램을 작성하시오. (/*구현*/ 부분을 채울 것)

#include <iostream>
using namespace std;

int main (void)
{
    int x, y;
    x = 10;
    y= 20;
    cout << x<< endl;
    cout << y << endl;
    
}

실행 결과 |

[기초 2]

문제 | 아래 프로그램을 작성하시오. (/*구현*/ 부분을 채울 것)
- sizof() 함수를 사용할 것, sizeof()의 기능을 main함수 상단에 주석으로 남길 것

#include <iostream>
using namespace std;

/* sizeof() 함수는 프로그래머가 만든 소스코드에서 메모리 공간을 실제 몇 byte를 잡아먹고 있는지 확인하기 위한 함수이다.*/
int main (void)
{
    unsigned short siX;
    unsigned iX;
    long liX;
    long long lliX;
    cout << "sizeof(siX) : "<< sizeof(siX) << endl;
    cout << "sizeof(iX) : "<< sizeof(iX) << endl;
    cout << "sizeof(liX) : "<< sizeof(liX) << endl;
    cout << "sizeof(lliX) : "<< sizeof(lliX) << endl;
}

실행 결과 |

[기초 3]

문제 | 아래 프로그램을 작성하시오. (/*구현*/ 부분을 채울 것)

#include <iostream>
using namespace std;

int main (void)
{
    cout<<"(7==5) : "<< ((7==5) ? 1:0)<< endl;
    cout<<"(7>=5) : "<< ((7>=5) ? 1:0)<< endl;
    cout<<"(7!=5) : "<< ((7!=5) ? 1:0)<< endl;
    cout<<"(7<=5) : "<< ((7<=5) ? 1:0)<< endl;
    cout<<"(7>=5 ? 100:-100) : "<< (7>=5 ? 100:-100)<< endl;
}

실행 결과 |

[기초 4]

문제 | 아래 프로그램을 작성하시오. (/*구현*/ 부분을 채울 것)

#include <iostream>
using namespace std;

int main (void)
{
    const double PI = 3.14159;
    char ch1 = 65;
    char ch2 = ch1 + 32;
    cout << PI << endl;
    cout << ch1 << endl;
    cout << ch2 << endl;
}

실행 결과 |

[기초 5]

문제 | 아래 프로그램을 작성하시오. (/*구현*/ 부분을 채울 것)

#include <iostream>
using namespace std;

int main (void)
{
    int x, y, sum, mult;
    float div;
    cin >> x >> y ;
    sum = x + y;
    mult = x * y;
    div = float(x)/ float(y);
    cout << x << '\t' <<y << endl;
    cout << "x + y = " << sum << endl;
    cout << "x * y = " << mult << endl;
    cout << "x / y = " << div << endl;
}

실행 결과 |


[응용 1]

문제 | 당신의 이름을 화면에 출력하는 프로그램을 작성하세요. (Write a program that prints your name on the screen.)

#include <iostream>
using namespace std;

int main (void)
{
    cout<<"My name is 권세나"<<endl;
}

시행 결과 |

[응용 2]

문제 | 사용자로부터 두 개의 정수인 A B를 입력 받은 후, 다음의 결과 값을 화면에 출력하는 프로그램을 작성하세요. (After receiving the two integers A and B from the user, Write a program that prints the following result on the screen.)

#include <iostream>
using namespace std;

int main (void)
{
    int A, B;
    cout << "Please enter two integer values : " ;
    cin >> A >> B;
    cout<< "A + B = " << A+B << endl;
    cout<< "A - B = " << A-B << endl;
    cout<< "A * B = " << A*B << endl;
    cout<< "A / B = " << A/B << endl;
    cout<< "A % B = " << A%B << endl;
}

시행 결과 |

[응용 3]

문제 | main() 안에서 아래의 변수를 선언하고, 다음의 결과를 계산하여 출력하는 프로그램을 작성하세요. (Write a program that computes and outputs the following results.)

#include <iostream>
using namespace std;

int main (void)
{
    int i1 =2, i2=5, i3 =-3;
    double d1=2.0, d2=5.0, d3=-0.5;

    cout<<i1+(i2*i3)<<endl;
    cout<< i1 * (i2+i3) <<endl;
    cout<< i1 / (i2+i3) <<endl;
    cout<< i1 / i2 + i3 <<endl;
    cout<< 3+ 4 + 5 /3 <<endl;
    cout<< (3+ 4 + 5) /3 <<endl;
    cout<< d1 + (d2*d3) <<endl;
    cout<< d1 + d2*d3 <<endl;
    cout<< d1/d2-d3 <<endl;
    cout<< d1/ (d2-d3) <<endl;
    cout<< d1+d2+d3/3 <<endl;
    cout<< (d1+d2+d3)/3 <<endl;
}

시행 결과 |

[응용 4]

문제 | 두 개의 정수 A B를 입력받고 A B의 변수 값을 서로 바꾸어 출력하세요. (Write two integers A and B, and then output A and B with the values of the variables interchanged.)

#include <iostream>
using namespace std;

int main (void)
{
    int A, B;
    cout << "Please enter two integer values : "<< endl;
    cout << "A : ";
    cin >> A;
    cout << "B : ";
    cin >> B;
    cout << "value of A is : "<< A << endl;
    cout << "value of B is : "<< B << endl;
}

시행 결과 |

[응용 5]

문제 | 사용자로부터 입력 받은 화씨온도(Fahrenheit) 값을 섭씨온도(Celsius) 값으로 변환하여 출력하는 프로그램을 작성하세요. (Write a program that converts the Fahrenheit calue received from the user into the Celsius value and outputs it.)
- 변환 계산 시에 5/9 가 아닌 5.0/9.0으로 int형이 아님을 명시해야 합니다. 5.0f, 5.0d 같이 형을 정확히 명시하면 더 좋습니다.

#include <iostream>
using namespace std;

int main (void)
{
    double C,F;
    cout << " Please enter Fahrenheit value : ";
    cin >> F;
    C = (5.0f)/(9.0f) * (F -32);
    cout << "Celcius value is " << C << endl;
}

시행 결과 |

'🏫 School > [23-2] 객체지향프로그래밍' 카테고리의 다른 글

[C++] Software Component (2)  (0) 2023.11.16
[C++] Software Component (1)  (0) 2023.11.16
[C++] Pointer & Vector & Array  (0) 2023.10.16
[C++] String & File  (0) 2023.10.16
[C++] 조건문과 반복문 (2)  (0) 2023.10.04
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.