Jam's story

2장 틀렸던 문제 다시풀어보기 본문

2021-2학기/C++

2장 틀렸던 문제 다시풀어보기

애플쩀 2021. 10. 24. 21:15

2장 실15

#include <iostream>
#include <string>

using namespace std;

void add(int a, int b) {
	int res = a + b;
	cout << a << "+" << b << "=" << res;
}
void minus1(int a, int b) {
	int res = a - b;
	cout << a << " - " << b << " = " << res;
}
void mul(int a, int b) {
	int res = a * b;
	cout << a << " * " << b << " = " << res;
}
void div1(int a, int b) {
	int res = a / b;
	cout << a << " / " << b << " = " << res;
}
void left(int a, int b) {
	int res = a % b;
	cout << a << " %" << b << " = " << res;
}
int main() {
	while (true) {

		cout << "?";
		int a, b;
		char c;
		cin >> a >> c >> b;

		switch (c) {
		case '+':
			add(a, b);
			break;
		case '-':
			minus1(a, b);
			break;
		case '*':
			mul(a, b);
			break;
		case '/':
			div1(a, b);
			break;
		case '%':
			left(a, b);
			break;
		}
		cout << endl;
	}

	


	return 0;
}

 

실 15 atoi 써서 다시풀어보기 

어렵다... 이해불가 

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

// 연산자 추출
char choiceOP(string str, int count)
{
    if (str[count] == ' ')
        return str[count + 1];
    else
        return choiceOP(str, count + 1);
}

// 숫자 변환
void transValue(string str, int& val1, int& val2)
{
    int len = str.length();
    int jmpCount = 0; // 두번째 숫자 위치로 뛰어넘기 위함

    for (int i = 0; i < len; i++)
    {
        // 첫번째 숫자
        if (jmpCount == 0 && str[i] == ' ')
        {
            string s = str.substr(0, i);
            const char* c1 = s.c_str();
            val1 = atoi(c1);
            jmpCount = i + 3; // 두번째 숫자로 위치 뛰어넘기
        }

        // 두번째 숫자
        if (jmpCount != 0 && str[i] == ' ')
        {
            string s = str.substr(jmpCount, i - jmpCount);
            const char* c2 = s.c_str();
            val2 = atoi(c2);
            return;
        }
    }
}

// 연산자 선택
void setOper(string str, int count, int& val1, int& val2)
{
    count = 0;
    char op = choiceOP(str, count); // 연산자 설정
    switch (op)
    {
    case '+':
        cout << val1 << ' ' << op << ' ' << val2 << " = " << val1 + val2 << endl;
        return;
    case '-':
        cout << val1 << ' ' << op << ' ' << val2 << " = " << val1 - val2 << endl;
        return;
    case '*':
        cout << val1 << ' ' << op << ' ' << val2 << " = " << val1 * val2 << endl;
        return;
    case '/':
        cout << val1 << ' ' << op << ' ' << val2 << " = " << val1 / val2 << endl;
        return;
    default:
        cout << val1 << ' ' << op << ' ' << val2 << " = " << val1 % val2 << endl;
        return;
    }
}

// main
int main()
{
    string equation;
    int count = 0;
    int val1, val2;

    while (1)
    {
        cout << "? ";
        getline(cin, equation);
        transValue(equation, val1, val2);
        setOper(equation, count, val1, val2);
    }

    return 0;
}

16번 

#include<iostream>
#include<cstring>
#include<cstdlib>
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:4996)
using namespace std;
//알파벳수 세는것, 알파벳이 각자 몇개인지 , 모든 알파벳 소문자로 

int main() {
    cout << "영문 텍스트를 입력하세용 히스토그램을 그립니다." << endl;
    cout << "텍스트의 끝은 ; 입니다. 10000개 까지 가능합니다."<<endl;
    char a[10000];
    cin.getline(a, 10000, ';');
    int ind;
    int alphabet[26] = { 0 };
    cout << "총 알파벳수 : " << strlen(a) << endl;
    //알파벳 수 세기    
    for (int i = 0; i < strlen(a); i++) {
        if ('A' <= a[i] <= 'Z') {
            a[i] = tolower(a[i]);
        }

        ind = (int)a[i] - 'a'; //그 알파벳이 몇번째 알파벳인지
        alphabet[ind]++; //그 알파벳의 갯수 추가 
    }

    for (int i = 0; i < 26; i++) {
        cout <<(char)('a' + i) << "("<<alphabet[i]<<"):";
        for (int j = 0; j < alphabet[i]; j++) {
            cout << "*";
        }
        cout << endl;
    }

    return 0;
}

'2021-2학기 > C++' 카테고리의 다른 글

c++기본  (0) 2021.10.26
c4996 에러 해결방법  (0) 2021.10.24
[명품 c++ programming] 5장 open challenge  (0) 2021.10.10
[C++] 함수의 참조, 복사 생성자  (0) 2021.10.10
[C++ programming ] 실습문제 6장  (0) 2021.10.08
Comments