๐ŸŒˆ๋ช…ํ’ˆ C++ ํ”„๋กœ๊ทธ๋ž˜๋ฐ 2์žฅ๐Ÿง‘โ€๐Ÿ’ป

Se0ng_1lยท2022๋…„ 6์›” 17์ผ
0

๋ช…ํ’ˆCPPํ”„๋กœ๊ทธ๋ž˜๋ฐ

๋ชฉ๋ก ๋ณด๊ธฐ
1/10
post-thumbnail

2์žฅ ์š”์ 

ํ™”๋ฉด์ž…์ถœ๋ ฅ

์ „์ฒ˜๋ฆฌ๊ธฐ

#include : ์†Œ์ŠคํŒŒ์ผ ์ปดํŒŒ์ผ ํ•˜๊ธฐ ์ „ ํ—ค๋”ํŒŒ์ผ ์ฝ๊ธฐ ์œ„ํ•œ ์ง€์‹œ๋ฌธ

ํ—ค๋”ํŒŒ์ผ

<iostream> ์ž…์ถœ๋ ฅ ๊ด€๋ จ ํ—ค๋”ํŒŒ์ผ

์ถœ๋ ฅ

std::cout << (์ถœ๋ ฅํ•˜๊ณ  ์‹ถ์€ ๋ฐ์ดํ„ฐ);

์ž…๋ ฅ

std::cin >> (๋ณ€์ˆ˜);

namespace

์œ„์— std:: ์ฒ˜๋Ÿผ ์‚ฌ์šฉ
๊ฐœ๋ฐœ์ž๊ฐ€ ์ž์‹ ๋งŒ์˜ ๊ณ ์œ ํ•œ ์ด๋ฆ„ ๊ณต๊ฐ„์„ ์ƒ์„ฑํ•ด ์‹๋ณ„์„ฑ์„ ์œ„ํ•ด ์‚ฌ์šฉ

using

std::์™€ ๊ฐ™์ด namespace ์ ‘๋‘์‚ฌ ์—†์• ๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ
std::cin => cin

๋ฌธ์ž์—ด

<cstring> or <string.h> ํ—ค๋”ํŒŒ์ผ 
strcmp, strcpy, strlen ๋“ฑ C๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํ•จ์ˆ˜ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ํ—ค๋”ํŒŒ์ผ

<string> ํ—ค๋”ํŒŒ์ผ (string)
c++์—์„œ ์ƒˆ๋กœ ์ถ”๊ฐ€๋œ ๋ฌธ์ž์—ด ํƒ€์ž…์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ํ—ค๋”ํŒŒ์ผ

๊ณต๋ฐฑ์ž…๋ ฅ

charํ˜•์˜ ๋ฐฐ์—ด์ผ ๊ฒฝ์šฐ 
	
    cin.getline(๋ฐฐ์—ด์ด๋ฆ„, ํฌ๊ธฐ, ๊ตฌ๋ถ„์ž)

stringํƒ€์ž…์˜ ๋ณ€์ˆ˜์ผ ๊ฒฝ์šฐ
	
    getline(cin, ๋ณ€์ˆ˜์ด๋ฆ„)

๋ช…ํ’ˆ C++ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ฑ…
https://book.naver.com/bookdb/book_detail.naver?bid=13395206

โš ๏ธ ์ฃผ์˜ โš ๏ธ
1. ์—ฌ๊ธฐ์„œ๋ถ€ํ„ฐ๋Š” ์‹ค์Šต๋ฌธ์ œ ์ •๋‹ต์ž…๋‹ˆ๋‹ค.
2.์ œ๊ฐ€ ์ž‘์„ฑํ•œ ์ •๋‹ต์ด ํ‹€๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ˜น์‹œ ํ‹€๋ฆฐ๊ฒŒ ์žˆ๋‹ค๋ฉด ๋Œ“๊ธ€๋กœ ๋‚จ๊ฒจ์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํžˆ ์ˆ˜์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
3. C++๋ฅผ ๊ณต๋ถ€ํ•˜์‹œ๋Š” ํ•™์ƒ์ด์‹œ๋ผ๋ฉด ๋ณด์‹œ๊ธฐ ์ „์— ์ง์ ‘ ๊ผญ ํ•œ ๋ฒˆ ํ’€์–ด๋ณด์„ธ์š”!

์‹ค์Šต ๋ฌธ์ œ 2 - 1
๋ฌธ์ œ : 1๋ถ€ํ„ฐ 100๊นŒ์ง€ ์ •์ˆ˜ ์ถœ๋ ฅํ•˜๊ธฐ, ๊ฐ ์ •์ˆ˜๋Š” ํƒญ์œผ๋กœ ๊ตฌ๋ถ„ํ•œ๋‹ค.

#include <iostream>
using namespace std;

int main()
{
    for(int i = 1; i <= 100; i++)
    {
        cout << i;
        if(i % 10 != 0)
            cout << '\t';
        else
            cout << '\n';
    }
}

์‹ค์Šต ๋ฌธ์ œ 2 - 2
๋ฌธ์ œ : ๊ตฌ๊ตฌ๋‹จ ์ถœ๋ ฅํ•˜๊ธฐ

#include <iostream>

int main()
{
    std::cout << "์ปดํ“จํ„ฐ๊ณตํ•™๊ณผ" << std::endl;
    std::cout << "21์„ธ" << std::endl;
    std::cout << "๋Œ€ํ†ต๋ น" << std::endl;
    return 0;
}

์‹ค์Šต ๋ฌธ์ œ 2 - 3
๋ฌธ์ œ : ๋‘ ์ •์ˆ˜๋ฅผ ์ฝ๊ณ  ํฐ ์ˆ˜ ์ถœ๋ ฅํ•˜๊ธฐ

#include <iostream>
using namespace std;

int main()
{
    int a, b;
    cout << "๋‘ ์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜๋ผ>>";
    cin >> a >> b;
    cout << "ํฐ ์ˆ˜ = " << ((a > b) ? a : b);
}

์‹ค์Šต ๋ฌธ์ œ 2 - 4
๋ฌธ์ œ : ์†Œ์ˆ˜์ ์„ ๊ฐ€์ง€๋Š” 5๊ฐœ์˜ ์‹ค์ˆ˜๋ฅผ ์ž…๋ ฅ ๋ฐ›์•„ ์ œ์ผ ํฐ ์ˆ˜๋ฅผ ํ™”๋ฉด์— ์ถœ๋ ฅํ•˜๋ผ

#include <iostream>
using namespace std;

int main()
{
    cout << "5 ๊ฐœ์˜ ์‹ค์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜๋ผ>>";
    double arr[5];
    for(int i = 0; i < 5; i++)
    {
        cin >> arr[i];
    }

    double max = arr[0];
    for(int i = 0; i < 5; i++)
    {
        if(max < arr[i])
            max = arr[i];
    }
    cout << "์ œ์ผ ํฐ ์ˆ˜ = " << max << endl;
}

์‹ค์Šต ๋ฌธ์ œ 2 - 5
๋ฌธ์ œ : ์ž…๋ ฅ๋œ ๋ฌธ์ž์—ด์—์„œ 'x'์˜ ๊ฐœ์ˆ˜ ์ถœ๋ ฅํ•˜๊ธฐ

#include <iostream>

using namespace std;

int main()
{
    cout << "๋ฌธ์ž๋“ค์„ ์ž…๋ ฅํ•˜๋ผ(100๊ฐœ ๋ฏธ๋งŒ)." << endl;
    char arr[100];
    cin.getline(arr, 100, '\n');
    int result = 0;
    for(int i = 0; i < 100; i++)
    {
        if(arr[i] == 'x')
            result++;
    }
    cout << "x์˜ ๊ฐœ์ˆ˜๋Š” " << result;
}

์‹ค์Šต ๋ฌธ์ œ 2 - 6
๋ฌธ์ œ : ๋‘ ๋ฌธ์ž์—ด์ด ๋™์ผํ•œ์ง€ ์—ฌ๋ถ€ ํŒ๋‹จํ•˜๊ธฐ

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

int main()
{
    string a, b;
    cin >> a >> b;
    if(a == b)
        cout << "๊ฐ™์Šต๋‹ˆ๋‹ค";
    else
        cout << "๊ฐ™์ง€ ์•Š์Šต๋‹ˆ๋‹ค.";
}

์‹ค์Šต ๋ฌธ์ œ 2 - 7
๋ฌธ์ œ : ์ž…๋ ฅํ•œ ๋ฌธ์ž์—ด์ด yes์ผ ๋•Œ๊นŒ์ง€ ์ž…๋ ฅ๋ฐ›๊ธฐ

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

int main()
{
    char answer[4] = {'y', 'e', 's'};
    while(true)
    {
        char arr[100];
        cout << "์ข…๋ฃŒํ•˜๊ณ  ์‹ถ์œผ๋ฉด yes๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”>>";
        cin.getline(arr, 100, '\n');
        if(strcmp(answer, arr) == 0)
        {
            break;
        }
    }
    cout << "์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค...";
}

์‹ค์Šต ๋ฌธ์ œ 2 - 8
๋ฌธ์ œ : ์†Œ์ˆ˜์ ์„ ๊ฐ€์ง€๋Š” 5๊ฐœ์˜ ์‹ค์ˆ˜๋ฅผ ์ž…๋ ฅ ๋ฐ›์•„ ์ œ์ผ ํฐ ์ˆ˜๋ฅผ ํ™”๋ฉด์— ์ถœ๋ ฅํ•˜๋ผ

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

int main()
{
    char name[100];
    char result[100];
    cout << "5๋ช…์˜ ์ด๋ฆ„์„ ';'์œผ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์ž…๋ ฅํ•˜์„ธ์š”" << endl << ">>";
    unsigned int max = 0;
    for(int i = 1; i <= 5; i++)
    {
        cin.getline(name, 100, ';');
        cout << i << " : "<< name << endl;
        if(max < strlen(name))
        {
            max = strlen(name);
            strcpy(result,name);
        }
    }
    cout << "๊ฐ€์žฅ ๊ธด ์ด๋ฆ„์€ " << result;
}

์‹ค์Šต ๋ฌธ์ œ 2 - 9
๋ฌธ์ œ : ์ด๋ฆ„, ์ฃผ์†Œ, ๋‚˜์ด๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ

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

int main()
{
    string people[3];
    char division[] = {',', ' '};
    for(int i = 0; i < 3; i++)
    {
        if(i == 0)
            cout << "์ด๋ฆ„์€?";
        else if(i == 1)
            cout << "์ฃผ์†Œ๋Š”?";
        else if(i == 2)
            cout << "๋‚˜์ด๋Š”?";
        getline(cin, people[i]);
    }
    cout << people[0] << ", " << people[1] << ", " << people[2] << "์„ธ";
}

์‹ค์Šต ๋ฌธ์ œ 2 - 10
๋ฌธ์ œ : ๋ฌธ์ž์—ด์„ ์ž…๋ ฅ๋ฐ›์•„ ํ•œ๊ธ€์ž์”ฉ ๋Š˜๋ ค๊ฐ€๋ฉฐ ์ถœ๋ ฅํ•˜๊ธฐ

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

int main()
{
   string arr;
   cout << "๋ฌธ์ž์—ด ์ž…๋ ฅ>>";
   cin >> arr;
   unsigned int len = arr.length();

   for(int i = 0; i < len + 1; i++)
   {
       if(i != 0)
       {
           for(int j = 0; j < i; j++)
           {
               cout << arr[j];
           }
           if(i != len)
                cout << endl;
       }
   }
}

์‹ค์Šต ๋ฌธ์ œ 2 - 11
๋ฌธ์ œ : C์ฝ”๋“œ -> C++ ์ฝ”๋“œ

#include <stdio.h> // C

int main()
{
    int k, n = 0;
    int sum = 0;
    printf("๋ ์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”>>");
    scanf("%d", &n);
    for(k = 1; k <= n; k++){
        sum += k;
    }
    printf("1์—์„œ %d๊นŒ์ง€์˜ ํ•ฉ์€ %d ์ž…๋‹ˆ๋‹ค.\n", n, sum);
    return 0;
}
#include <iostream> // C++
using namespace std;

int main()
{
    cout << "5 ๊ฐœ์˜ ์‹ค์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜๋ผ>>";
    double arr[5];
    for(int i = 0; i < 5; i++)
    {
        cin >> arr[i];
    }

    double max = arr[0];
    for(int i = 0; i < 5; i++)
    {
        if(max < arr[i])
            max = arr[i];
    }
    cout << "์ œ์ผ ํฐ ์ˆ˜ = " << max << endl;
}

์‹ค์Šต ๋ฌธ์ œ 2 - 12
๋ฌธ์ œ : C์ฝ”๋“œ -> C++ ์ฝ”๋“œ

#include <stdio.h> // C

int sum();

int main()
{
    int n = 0;
    printf("๋ ์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”>>");
    scanf("%d", &n);
    printf("1์—์„œ %d๊นŒ์ง€์˜ ํ•ฉ์€ %d ์ž…๋‹ˆ๋‹ค.\n", n, sum(1, n));
    return 0;
}

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

using namespace std;

int sum(int a, int b);

int main() {
    int n = 0;
    cout << "๋ ์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”>>";
    cin >> n;
    cout << "1์—์„œ " << n << "๊นŒ์ง€์˜ ํ•ฉ์€ " << sum(1, n) << " ์ž…๋‹ˆ๋‹ค." << endl;
    return 0;
}

int sum(int a, int b){
    int k, res = 0;
    for(k = a; k <= b; k++)
    {
        res += k;
    }
    return res;
}

์‹ค์Šต ๋ฌธ์ œ 2 - 13
๋ฌธ์ œ : ์ค‘์‹๋‹น์—์„œ ์ฃผ๋ฌธํ•˜๊ธฐ

#include <iostream>
using namespace std;

int main()
{
    cout << "***** ์Šน๋ฆฌ์žฅ์— ์˜ค์‹  ๊ฒƒ์„ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค. *****" << endl;
    int input;
    int cnt;
	while(true)
    {
        cout << "์งฌ๋ฝ•:1, ์งœ์žฅ:2, ๊ตฐ๋งŒ๋‘:3, ์ข…๋ฃŒ:4>> ";
        cin >> input;
        if(input < 1 || input > 4){
            cout << "๋‹ค์‹œ ์ฃผ๋ฌธํ•˜์„ธ์š”!!" << endl;
            continue;
        }
        else if(input == 4)
        {
            cout << "์˜ค๋Š˜ ์˜์—…์€ ๋๋‚ฌ์Šต๋‹ˆ๋‹ค.";
            break;
        }
        else
        {
            cout << "๋ช‡์ธ๋ถ„?";
            cin >> cnt;
            if(input == 1)
                cout << "์งฌ๋ฝ• " << cnt << "์ธ๋ถ„ ๋‚˜์™”์Šต๋‹ˆ๋‹ค." << endl;
            else if(input == 2)
                cout << "์งœ์žฅ " << cnt << "์ธ๋ถ„ ๋‚˜์™”์Šต๋‹ˆ๋‹ค." << endl;
            else
                cout << "๊ตฐ๋งŒ๋‘ " << cnt << "์ธ๋ถ„ ๋‚˜์™”์Šต๋‹ˆ๋‹ค." << endl;
        }
    }
}

์‹ค์Šต ๋ฌธ์ œ 2 - 14
๋ฌธ์ œ : ์นดํŽ˜ ์šด์˜ํ•˜๊ธฐ 20000์› ์ด์ƒ ๋ฒŒ๋ฉด ์˜์—… ์ข…๋ฃŒ

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

int main()
{
    int total = 0;

    cout << "์—์Šคํ”„๋ ˆ์†Œ 2000์›, ์•„๋ฉ”๋ฆฌ์นด๋…ธ 2300์›, ์นดํ‘ธ์น˜๋…ธ 2500์›์ž…๋‹ˆ๋‹ค." << endl;

    string coffee;
    int cnt;
    while(true)
    {
        cout << "์ฃผ๋ฌธ>> ";

        int result = 0;
        cin >> coffee >> cnt;
        if(coffee == "์—์Šคํ”„๋ ˆ์†Œ")
        {
            result = 2000 * cnt;
            cout << result << "์ž…๋‹ˆ๋‹ค. ๋ง›์žˆ๊ฒŒ ๋“œ์„ธ์š”" << endl;
        }
        else if(coffee == "์•„๋ฉ”๋ฆฌ์นด๋…ธ")
        {
            result = 2300 * cnt;
            cout << result << "์ž…๋‹ˆ๋‹ค. ๋ง›์žˆ๊ฒŒ ๋“œ์„ธ์š”" << endl;
        }
        else if(coffee == "์นดํ‘ธ์น˜๋…ธ")
        {
            result = 2500 * cnt;
            cout << result << "์ž…๋‹ˆ๋‹ค. ๋ง›์žˆ๊ฒŒ ๋“œ์„ธ์š”" << endl;
        }

        total += result;

        if(total >= 20000)
        {
            cout << "์˜ค๋Š˜ " << total << "์›์„ ํŒ๋งคํ•˜์—ฌ ์นดํŽ˜๋ฅผ ๋‹ซ์Šต๋‹ˆ๋‹ค. ๋‚ด์ผ๋ด์š”~~~";
            break;
        }
    }
}

์‹ค์Šต ๋ฌธ์ œ 2 - 15
๋ฌธ์ œ : ๋‹ค์„ฏ๊ฐ€์ง€ ์—ฐ์‚ฐ์„ ํ•  ์ˆ˜ ์žˆ๋Š” ์—ฐ์‚ฐ๊ธฐ ๋งŒ๋“ค๊ธฐ

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

int main()
{
    char input[100];
    char op;
    while(true)
    {
        cout << "? ";
        cin.getline(input, 100, ' ');
        int num1 = atoi(input); // atoi๋กœ ๋ฌธ์ž์—ด์—์„œ ์ •์ˆ˜๋กœ ํ˜•๋ณ€ํ™˜
        cin.getline(input, 100, ' ');
        op = input[0];
        cin.getline(input, 100, '\n');
        int num2 = atoi(input); 
        if(op == '+')
        {
            cout << num1 << " + " << num2 << " = " << num1 + num2 << endl;
        }
        else if(op == '-')
        {
            cout << num1 << " - " << num2 << " = " << num1 - num2 << endl;
        }
        else if(op == '*')
        {
            cout << num1 << " * " << num2 << " = " << num1 * num2 << endl;
        }
        else if(op == '/')
        {
            cout << num1 << " / " << num2 << " = " << num1 / num2 << endl;
        }
        else
        {
            cout << num1 << " % " << num2 << " = " << num1 % num2 << endl;
        }
    }
}

์‹ค์Šต ๋ฌธ์ œ 2 - 16
์˜๋ฌธ ํ…์ŠคํŠธ์˜ ์•ŒํŒŒ๋ฒณ์†Œ๋ฌธ์ž ํžˆ์Šคํ† ๊ทธ๋žจ ๋งŒ๋“ค๊ธฐ, ๋Œ€๋ฌธ์ž๋„ ์†Œ๋ฌธ์ž๋กœ ์ทจ๊ธ‰, ๋ฌธ์žฅ์˜ ๋์€ ;

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

int main()
{
    char arr[10001];
    int cnt[26] = {0,};
    cout << "์˜๋ฌธ ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”. ํžˆ์Šคํ† ๊ทธ๋žจ์„ ๊ทธ๋ฆฝ๋‹ˆ๋‹ค." << endl;
    cout << "ํ…์ŠคํŠธ์˜ ๋์€ ; ์ž…๋‹ˆ๋‹ค. 10000๊ฐœ๊นŒ์ง€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค." << endl;
    cin.getline(arr, 10001, ';');

    unsigned int len = strlen(arr);
    int totalAlpha = 0;
    for(int i = 0; i < len; i++)
    {
        if(isalpha(arr[i])){
            arr[i] = tolower(arr[i]);
            cnt[(char)arr[i] - 97]++;
            totalAlpha++;
        }
    }
    cout << "์ด ์•ŒํŒŒ๋ฒณ ์ˆ˜ " << totalAlpha << endl << endl;

    for(int x = 0; x < 26; x++)
    {
        cout << (char)(x + 97) << " ";
        cout << "(" << cnt[x] << ") : ";
        for(int y = 0; y < cnt[x]; y++)
        {
            cout << "*";
        }
        cout << endl;
    }
}
profile
์น˜ํƒ€๊ฐ€ ๋˜๊ณ  ์‹ถ์€ ์ทจ์ค€์ƒ

0๊ฐœ์˜ ๋Œ“๊ธ€