Skip to content

Instantly share code, notes, and snippets.

@jjangga0214
Last active April 15, 2018 14:56
Show Gist options
  • Save jjangga0214/649c8048f79cdfb1f492f3333c91d0b4 to your computer and use it in GitHub Desktop.
Save jjangga0214/649c8048f79cdfb1f492f3333c91d0b4 to your computer and use it in GitHub Desktop.

실습

mock.h
//
// Created by user on 18. 4. 12.
//

#ifndef XPP_MOCK_H
#define XPP_MOCK_H

#endif //XPP_MOCK_H

int sizeOfStr(char str[]);

class MyString {
private:
    char memory[80];

public:

    MyString(char str[] = "") {
        for (int i = 0; i < str[i] != '\0' && i < 80; ++i) {
            memory[i] = str[i];
        }
    }

    int size() {
        return sizeOfStr(memory);
    }

    void cpy(char str[]) {
        int i;
        for (i = 0; str[i] != '\0' && i < 80; ++i) {
            memory[i] = str[i];
        }
        memory[i] = '\0';
    }

    void cat(char str[]) {
        int length = size();
        for (int i = 0; str[i] != '\0' && length < 80; ++i) {
            memory[length++] = str[i];
        }
        memory[length] = '\0';
    }

    int cmp(char str[]) {
        int i;
        for (i = 0; i < size(); ++i) {
            if (str[i] != memory[i]) {
                return memory[i] - str[i] > 0 ? 1 : -1;
            }
        }
        return str[i] != '\0' ? -1 : 0;//str 의 크기가 memory의 크기보다 크면 -1을 반환
    }

    std::string tostring(){
        return memory;
    }
};

int sizeOfStr(char str[]) {
    int counter = 0;
    char *m = str;
    while (*(m++) != '\0') {
        counter++;
    }

    return counter;
}
main.cpp
#include <iostream>
#include "mock.h"

using namespace std;


int main() {

    MyString s1("abcde"); //abcde
    cout<<s1.tostring()<<'\n'; //abcde 출력
    cout<<s1.size()<<'\n'; //5 출력
    s1.cat("fg");//abcdefg
    cout<<s1.tostring()<<'\n';//abcdefg 출력
    cout<<s1.size()<<'\n';// 7출력
    cout<<s1.cmp("abced")<<'\n';// -1 출력
    s1.cpy("hello there");
    cout<<s1.tostring()<<'\n';// hello there 출력

    return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment