C++/열혈 C++ 프로그래밍(윤성우 저)

열혈 C++ 프로그래밍 2-4 문제

꾸준함. 2017. 5. 28. 22:37

[문제1]

/*

다음 표준함수를 호출하는 예제를 만들되, C++ 헤더를 선언해서 만들어보자.

그리고 예제의 내용은 상관이 없지만, 아래의 함수들을 최소 1 이상 호출해야 한다.

참고로 다음 함수들은 C언어의 경우 <string.h> 선언되어 있다

1.strlen 문자열의 길이 계산

2.strcat 문자열의 뒤에 덧붙이기

3.strcpy 문자열의 복사

4.strcmp 문자열의 비교

*/

#include <iostream>

#include <cstring>

using namespace std;

 

int main(void)

{

        char str1[20] = "I like ";

        char str2[20] = "Gudetama";

        char str3[20];

        int length = strlen(str1);

        cout << "str1 문자열의 길이는 " << length << "이다" << endl;

        strcat(str1, str2);

        cout << "str1 뒤에 str2 붙이면 " << str1 << " 된다" << endl;

        strcpy(str3, str1);

        cout << "str1 문장을 복사한 str3 " << str3 << "이다" << endl;

        if (!strcmp(str1, str3)) //동일할 경우 strcmp 0 반환

        {

               cout << "str1 str3 동일한 문장이다" << endl;

        }

        else

        {

               cout << "str1 str3 동일한 문장이 아니다" << endl;

        }

        return 0;

}

 

[문제2]

/*

다음 세함수를 이용해서 0이상 100미만의 난수를 5 생성하는 예제를 만들되, C++ 헤더를 선언해서 작성해보자

참고로 C언어의 경우 time 함수는 <time.h> 선언되어 있고, rand함수와 srand함수는 <stdlib.h> 선언되어 있다

*/

#include <iostream>

#include <ctime>

#include <cstdlib>

using namespace std;

 

int main(void)

{

        srand((unsigned)time(NULL)); //시드

        for (int i = 0; i < 5; i++)

               cout << i + 1 << "번째 난수는" << rand() % 100 << "이다" << endl;

        return 0;

}

개발 환경:Visual Studio 2017


지적, 조언, 질문 환영입니다! 댓글 남겨주세요~


[참고] 열혈 C++ 프로그래밍 윤성우 저

반응형