대학교 수업/C++ 프로그래밍

[C++] 명품 C++ 프로그래밍 2장

Jongung 2022. 4. 10. 22:52

1번

#include <iostream>
using namespace std;
int main(){
    for(int i = 1; i <= 100; i++){
        cout << i << " ";
        if(i%10 == 0){
            cout << endl;
        }
    }
}

1번

 

2번

#include <iostream>
using namespace std;
int main(){
    for(int i = 1; i <= 9; i++){
        for(int j = 1; j <=9; j++){
            cout << j << "x" << i << "=" << j*i << " ";
        }
        cout << "\n";
    }
}

2번

 

3번

#include <iostream>
using namespace std;
int main(){
    cout << "두 수를 입력하라";
    int n, m;
    cin >> n  >> m;
    cout << "큰 수 = " << (n > m ? n:m);
}

 

3번

 

4번

#include <iostream>
using namespace std;
int main(){
    cout << "5개의 실수를 입력하라";
    double max, temp;
    cin >> max;
    for(int i = 1; i < 5; i++){
        cin >> temp;
        if(max < temp){
            max = temp;
        }
    }
    cout << max;
}

4번

 

5번 

#include <iostream>
using namespace std;
int main(){
    char str[100];
    int cnt = 0;
    cin.getline(str, 100);
    
    for(int i = 0; i<strlen(str); i++){
        if(str[i] == 'x'){
            cnt++;
        }
    }
    cout << "x의 개수는 " << cnt;
}

5번

 

6번

#include <iostream>
using namespace std;
int main(){
    string str1, str2;
    cout << "새 암호를 입력하세요>>";
    cin >> str1;
    cout << "새 암호를 다시 한 번 입력하세요>>";
    cin >> str2;
    if(str1 == str2){
        cout << "같습니다";
    }
    else{
        cout << "다릅니다";
    }
}

6번

 

7번

#include <iostream>
using namespace std;
int main(){
    while(1){
        char str[100];
        cout << "종료하고싶으면 yes를 입력하세요";
        cin.getline(str, 100);
        if(!strcmp(str, "yes")){
            break;
        }
    }
}

7번

 

8번

#include <iostream>
using namespace std;
int main(){
    for(int i = 1; i <= 5; i++){
        char str[100];
        cin.getline(str, 100, ';');
        cout << i << " : " << str << endl;
    }
}

8번

 

9번

#include <iostream>
using namespace std;
int main(){
    char str1[100], str2[100], str3[100];
    cout << "이름은?";
    cin.getline(str1,100);
    cout << "주소는?";
    cin.getline(str2, 100);
    cout << "나이는?";
    cin.getline(str3,100);
    cout << str1 << ", " << str2 << ", " << str3;
}

9번

 

10번

#include <iostream>
using namespace std;
int main(){
    char str[100];
    cout << "문자열 입력";
    cin >> str;
    for(int i = 0; i < strlen(str); i++){
        for(int j = 0; j <= i; j++){
            cout << str[j];
        }
        cout << "\n";
    }
}

10번

 

11번

#include <iostream>
using namespace std;
int main(){
    int k, n=0;
    int sum=0;
    cout << "끝 수를 입력하세요>>";
    cin >> n;
    for(k = 1; k <= n; k++){
        sum += k;
    }
    cout << "1에서 " << n << "까지의 합은 " << sum << "입니다.";
}

11번

 

12번

#include <iostream>
using namespace std;
int sum(int a, int b){
    int k, res=0;
    for(k=a; k<=b; k++){
        res += k;
    }
    return res;
}

int main(){
    int n = 0;
    cout << "끝 수를 입력하세요 >>";
    cin >> n;
    cout << "1에서 " << n << "까지의 합은 " << sum(1,n) << "입니다" << endl;
}

12번

 

13번

 

#include <iostream>
using namespace std;

int main(){
    cout << "****** 승리장에 오신 것을 환영합니다. ******" << endl; 
    for(int i = 0; i <4; i++){
        int input;
        cout << "짬뽕 1, 짜장 2, 군만두 3, 종료 4>> ";
        cin >> input;
        if(input == 1){
            cout << "몇인분?";
            cin >> input;
            cout << "짬뽕 " << input << "인분 나왔습니다." << endl;
        }
        else if(input == 2){
            cout << "몇인분?";
            cin >> input;
            cout << "짜장 " << input << "인분 나왔습니다." << endl;
        }
        else if(input == 3){
            cout << "몇인분?";
            cin >> input;
            cout << "군만두 " << input << "인분 나왔습니다." << endl;
        }
        else if(input == 4){
            cout << "오늘 영업 끝났습니다!";
            break;
        }
        else{
            cout << "다시 주문하세요!!" << endl;
        }
    }
}

13번

 

14번

#include <iostream>
using namespace std;

int main(){
    cout << "에스프레소 2000원, 아메리카노 2300원, 카푸치노 2500원입니다." << endl;
    int sum = 0;
    while(1){
        char str[100];
        int a;

        cout << "주문 >> ";
        if(sum >= 20000){
            cout << "오늘 " << sum << "원을 판매하여 카페를 닫습니다. 내일봐요~~~";
            break;
        }
        
        cin >> str >> a;
        if(strcmp(str, "에스프레소") == 0){
            cout << 2000 * a << "원입니다. 맛있게 드세요" << endl;
            sum += 2000 * a;
        }
        else if(strcmp(str, "카푸치노") == 0){
            cout << 2500 * a << "원입니다. 맛있게 드세요" << endl;
            sum += 2500 * a;
        }
        else if(strcmp(str, "아메리카노") == 0){
            cout << 2300 * a << "원입니다. 맛있게 드세요" << endl;
            sum += 2300 * a;
        }
        else{
            cout << "뭐냐 닌" << endl;
        }
    }
}

14번

 

15번

#include <iostream>
using namespace std;

int main() 
{
	int a, c;
	char b;
	while (true) 
	{
		cout << "? ";
		cin >> a >> b >> c;
		switch (b) {
		    case '+':
            cout << a << " " << b << " " << c << " = " << a + c << endl; 
            break;
		    case '-':
            cout << a << " " << b << " " << c << " = " << a - c << endl; 
            break;
		    case '*':
            cout << a << " " << b << " " << c << " = " << a * c << endl; 
            break;
		    case '/':
            cout << a << " " << b << " " << c << " = " << a / c << endl; 
            break;
		    case '%':
            cout << a << " " << b << " " << c << " = " << a % c << endl; 
            break;
		    default: 
            cout << "잘못된 연산자 입니다." << endl;
		}
	}
}

15번

 

16번

#include<iostream>
#include<cstring>
using namespace std;

int main() 
{
	char c[10001];
	int cnt[26] = { 0, };
	cin.getline(c, 10000, ';');
	
	for (int i = 0; i < strlen(c); i++)
	{
		cnt[int(c[i])-97]++;
	}
	for (int i = 0; i < 26; i++)
	{
		cout << char(i + 97) << " (" << cnt[i] << "): ";
		for (int j = 0; j < cnt[i]; j++)
		{
			cout << "*";
		}
		cout << endl;
	}
}

16번