ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 실습 예제
    명품 c++ 공부 2020. 8. 24. 18:08

    문제5.<Enter> 키가 입력될 때까지 문자들을 읽고, 입력된 문자 'x' 의 개수를 화면에 출력하라.

     

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

     

    문제15번.덧셈(+),뺼셈(-),곱셈(*),나눗셈(/),나머지(%)의 정수 5칙 연산을 할 수 있는 프로그램을 작성하라.

    #include<iostream>
    using namespace std;
    
    int main()
    {
    	char* context;
    	char arr[100];
    	int result = 0;
    	cout << "?";
    	cin.getline(arr, 100, '\n');
    	char* first = strtok_s(arr, " ", &context);
    	char* token = strtok_s(NULL, " ", &context);
    	char* end = strtok_s(NULL, " ", &context);
    
    	int firstint = atoi(first);
    	int endint = atoi(end);
    
    
    	if (*token == '+')
    	{
    		result = firstint + endint;
    	}
    	else if (*token == '-')
    	{
    		result = firstint - endint;
    	}
    	else if (*token == '*')
    	{
    		result = firstint * endint;
    	}
    	else if (*token == '/')
    	{
    		result = firstint / endint;
    	}
    	else if (*token == '%')
    	{
    		result = firstint % endint;
    	}
    	cout << firstint << token;
    	cout << endint << "=";
    	cout << result;
    }

     

     

    '명품 c++ 공부' 카테고리의 다른 글

    vector STL  (0) 2021.03.10
    실습예제2-16(난이도8)  (0) 2020.08.25
    #include 와 전처리기  (0) 2020.08.24
    String 으로 문자열 입력받기  (0) 2020.08.24
    cin과 >> 연산자로 문자열을 입력 받을 때의 허점  (0) 2020.08.24
Designed by Tistory.