Machineboy空

C++ 문법 기초다지기 예제 모음2 - 연산 본문

Computer/Coding Test

C++ 문법 기초다지기 예제 모음2 - 연산

안녕도라 2024. 10. 23. 23:34

예제 7. 직사각형 방의 면적

 

조건)
1feet = 0.3048m

출력)
What is the length of the room in feet? 15
What is the width of the room in feet? 20
You entered dimensions of 15 feet by 20 feet
The area is
300 squre feet
22.871 square meters
#include <iostream>
#include <string>
using namespace std;

int length, width;

int main() {

    cout << "What is the length of the room in feet?";
    cin >> length;
    cout << "What is the width of the room in feet?";
    cin >> width;

    cout << "You entered dimensions of " + to_string(length) + " feet by " + to_string(width) + " feet";
    cout << "The area is " + to_string(length * width) + " square feet\n";
    cout << to_string(length * 0.3048 * width * 0.3048) + "square meters";

}


예제 8. 피자 파티

출력 예)
How many people? 8
How many pizzas do you have? 2

How many pieces are in a pizza? 8
8 people with 2 pizzas
Each person gets 2 pieces of pizza.
There are 0 leftover pieces.
#include <iostream>
#include <string>

using namespace std;

int people, pizza, pieces;

int main() {
    cout << "How many people?";
    cin >> people;
    cout << "How many pizzas do you have?";
    cin >> pizza;

    cout << "How many pieces are in a pizza?";
    cin >> pieces;

    int eachP = pizza * pieces / people;
    int leftover = pizza * pieces - people * eachP;

    cout << to_string(people) + " people with " + to_string(pizza) + " of pizza\n";
    cout << "Each person gets " + to_string(eachP) + " pieces of pizza\n";
    cout << "There are " + to_string(leftover) + " leftover pieces";

    
}


예제 9. 페인트 계산기

문제)
1리터에 9m2을 칠한다고 가정할 때, 천장을 칠하는 데 필요한 페인트의 양을 구하는 프로그램을 작성하라.
페인트의 양은 정수로 표현하라.

입력)
길이와 폭

출력)
You will need to purchase 2 liters of 
paint to cover 10 square meters.
#include <iostream>
#include <string>

using namespace std;

int people, pizza, pieces;

int main() {
    cout << "How many people?";
    cin >> people;
    cout << "How many pizzas do you have?";
    cin >> pizza;

    cout << "How many pieces are in a pizza?";
    cin >> pieces;

    int eachP = pizza * pieces / people;
    int leftover = pizza * pieces % people;

    cout << to_string(people) + " people with " + to_string(pizza) + " of pizza\n";
    cout << "Each person gets " + to_string(eachP) + " pieces of pizza\n";
    cout << "There are " + to_string(leftover) + " leftover pieces";
}


예제 10. 셀프계산대

출력 예)
Price of item 1: 25
Quantity of item 1: 2
Price of item 2: 10
Quantity of item 2: 1
Price of item 3: 4
Quantity of item 3: 1
Subtotal: $64.00
Tax: $3.52
Total: $67.52
#include <iostream>
#include <string>

using namespace std;

float subTotal, tax, total;
int price[3], quantity[3];

int main() {
    cout << "Price of item 1: ";
    cin >> price[0];
    cout << "Quantity of item 1: ";
    cin >> quantity[0];
    cout << "Price of item 2: ";
    cin >> price[1];
    cout << "Quantity item 2: ";
    cin >> quantity[1];
    cout << "Price of item 3: ";
    cin >> price[2];
    cout << "Quantity item 3: ";
    cin >> quantity[2];

    for(int i = 0; i < sizeof(price); i++){
        subTotal += price[i] * quantity[i];
    }

    
    cout << "Subtotal: $";
    printf("%.2f", subTotal);
    cout << "\n";


    cout << "Tax: $";
    cin >> tax;
    cout << "Total: $" ;
    printf("%.2f", subTotal + tax);
}


예제 11. 환율 변환

환율 계산 식)
amount to = amount from X rate from / rate to

• amoun to는 변환될 미국 달러 가격이다.
• amount from은 유로 가격이다.
• rate from은 현재의 유로 환율이다.
• rate to는 현재의 미국 달러 환율이다.

출력 예)
How many Euros are you exchanging? 81
What is the exchange rate? 137.51
81 Euros at an exchange rate of 137.51 is 111.38 dollars
#include <iostream>
#include <string>

using namespace std;

int euros;
float exchangeRate;

int main() {
    cout << "How many Euros are you exchanging? ";
    cin >> euros;

    cout << "What is the exchange rate?";
    cin >> exchangeRate;

    cout << to_string(euros) + " Euros at an exchange rate of ";
    printf("%.2f",exchangeRate);
    cout << "is ";
    printf("%.2f", euros * exchangeRate/ 100);
    cout << " dollars";
}


정리

#include <iostream>
#include <string>

// 소숫점 n째자리 까지 표시하기 위해 printf("%.2f", int형)

float exchangeRate;
printf("%.2f",exchangeRate);

float principal, interest, yearNum;
printf("After %.0f years at %.1f%%, the investment will be worth $%.2f\n", 
           yearNum, interest, principal * (1 + interest * yearNum));