2017년 5월 30일 화요일

[Open S/W] Source codes we worked on May 30

Source codes

[store.c]===========================================================



#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "mylib.h"

struct address* store(i, top)
struct address* i;
struct address* top;
{

}

[mylib.h]===========================================================

#ifndef MYLIB_H_
#define MYLIB_H_

struct address {
    char name[40];
    char zip[10];
    struct address *next;
} list_entry;

extern struct address* start;
extern struct address* last;

int  menuSelect(void);
void enter(void);
struct address* store(struct address* i, struct address* top);

#endif

[main.c]===========================================================

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mylib.h"

int main(void)
{
    int choice;

    for(;;) {
       
        choice = menuSelect();
        
        switch(choice) {
            case 1:
                   enter();
                   break;
            case 2:
                   break;
            case 7:
                   exit(0);
                   break;
        }

    }
    return 0;
}

[enter.c]===========================================================

#include <stdio.h>
#include "mylib.h"

void enter(void)
{
    printf("\n enter() is called ....\n");
}

[menu.c]===========================================================

#include <stdio.h>
#include <stdlib.h>
#include "mylib.h"

int menuSelect(void)
{
    char select[80];
    int  num;

    do {
           printf("=================================\n");
           printf("Welcome to the PNU Mailing System\n");
           printf("1. Enter a name\n");
           printf("2. Delete a name\n");
           printf("7. Exit\n"); 
           printf("=================================\n");

           printf(">>> Enter your choice: ");
           fgets(select, 80, stdin);
           num = atoi(select);

    } while (num < 0 || num > 7);

    return num;
}

[Makefile]======================================================

all: mailing

mailing: main.c menu.o enter.o store.o
 gcc -o mailing main.c menu.o enter.o store.o

menu.o:
 gcc -c menu.c 

enter.o:
 gcc -c enter.c

store.o:
 gcc -c store.c

clean:
 rm -rf *.o



2017년 5월 25일 목요일

[Open S/W] Source codes (May 25)

[Makefile] ===========================================

all: main

main: main.c display.o search.o
    gcc -o addrman main.c display.o search.o

display.o:
    gcc -c display.c

search.o:
    gcc -c search.c

clear:
    rm -rf *.o

[mylib.h] ===========================================

#ifndef MYLIB_H_
#define MYLIB_H_

struct address {
    char name[40];
    char zip[40];
    struct address *next;
} list_entry;

extern struct address *start;
extern struct address *last;

void display (struct address *top);
struct address* search(struct address *top, char *);

#endif  // MYLIB_H_

[main.c] ===========================================

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mylib.h"

struct address *start;
struct address *last;

int main()
{
    struct address *info;
    struct address *info2;

    // Initialize list point: empty state
    start = last = NULL;

    info = (struct address*)malloc(sizeof(list_entry));
    strcpy(info->name, "Tom");
    strcpy(info->zip, "613");
    info->next = NULL;

    start = info;

    info2 = (struct address*)malloc(sizeof(list_entry));
    info->next = info2;
    strcpy(info2->name, "Steve");
    strcpy(info2->zip, "513");
    info2->next = NULL;

    display(start);

    struct address* info3;

    info3  = search(start, "Steve");

    if (info3) {
        display(info3);
    }
    else {
        printf("Not Found!!\n");
    }
   
    free(info);
    free(info2);

    return 0;
}                                                   

[display.c] ===========================================

#include <stdio.h>
#include "mylib.h"

void display(struct address *top)
{
    while(top) {
       printf("Name: %s\n", top->name);
       printf("Zip: %s\n", top->zip);
       top = top->next;
    }
}

[search.c] ===========================================

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "mylib.h"

struct address* search(struct address *top, char *name)
{
    while(top) {
        // if successful
        if (!strcmp(name, top->name))
            return top;

        top = top->next;
    }
   
    return NULL;
}

2017년 5월 23일 화요일

[C++] MyString Copy Assignment (May 23)

#include <iostream>
#include <string>
#include <cstring>

using namespace std;

class MyString {

public:
    char* Buffer;
    //string Buffer;

public:
    /*
    // constructor
    MyString(string mystring) {
        cout << "Constructor: creating new MyString" << endl;
        Buffer = mystring;
        cout << "Buffer points to: 0x" << (void *)&Buffer[0] << endl;
    }
    */

    MyString(const char* mystring) {
        if (mystring != NULL) {
            Buffer = new char [strlen(mystring) + 1];
            strcpy(Buffer, mystring);

            // Display memory address pointed by local buffer
            cout << "Buffer points to: 0x" << hex;
            cout << (unsigned int*)Buffer << endl;
        }
        else
            Buffer = NULL;
    }

    // Copy Constructor
    MyString(const MyString& CopySource) {
        cout << "Copy Constructor" << endl;
        if (CopySource.Buffer != NULL) {
            Buffer = new char[strlen(CopySource.Buffer) + 1];
            strcpy(Buffer, CopySource.Buffer);

            cout << "CopySource Buffer points to: 0x" << hex;
            cout << (unsigned int*)CopySource.Buffer << endl;

            cout << "Buffer points to: 0x" << hex;
            cout << (unsigned int*)Buffer << endl;
        }
    }

    // Copy assignment operator
    MyString& operator= (const MyString& CopySource) {
        if ((this != &CopySource) && (CopySource.Buffer != NULL))
        {
            if (Buffer != NULL)
                delete[] Buffer;
            Buffer = new char [strlen(CopySource.Buffer) + 1];
            strcpy(Buffer, CopySource.Buffer);
        }
        return *this;
    }

    ~MyString() {

        cout << "Invoking destructor" << endl;

        if (Buffer != NULL)
            delete [] Buffer;
    }

    const char* GetString() {
        return Buffer;
    }

    int GetLength() {
        return strlen(Buffer);
    }

    operator const char*() {
        return Buffer;
    }
};

/*
void UseMyString(MyString Input)
{
    cout << "UseMyString Buffer points to : 0x" << (void *)&Input.Buffer[0] << endl;
    cout << "length: " << Input.Buffer.length();
    cout << "Buffer contains: " << Input.Buffer.c_str() << endl;

    return;
}
*/
int main()
{

    MyString SayHello("SayHello from the class");
    MyString String1("Hello");
    MyString String2("World");

    cout << String1 << String2 << endl;

    // UseMyString(SayHello);

    // cout << SayHello.GetString() << endl;

    return 0;
}

[C++] MyString Sample Codes (May 23)

#include <iostream>
#include <string>
#include <cstring>

using namespace std;

class MyString {

public:
    // char* Buffer;
    string Buffer;

public:
    // constructor
    MyString(string mystring) {
        cout << "Constructor: creating new MyString" << endl;
        Buffer = mystring;
        cout << "Buffer points to: 0x" << (void *)&Buffer[0] << endl;
    }


    /*
    MyString(const char* mystring) {
        if (mystring != NULL) {
            Buffer = new char [strlen(mystring) + 1];
            strcpy(Buffer, mystring);

            // Display memory address pointed by local buffer
            cout << "Buffer points to: 0x" << hex;
            cout << (unsigned int*)Buffer << endl;
        }
        else
            Buffer = NULL;
    }

    // Copy Constructor
    MyString(const MyString& CopySource) {
        cout << "Copy Constructor" << endl;
        if (CopySource.Buffer != NULL) {
            Buffer = new char[strlen(CopySource.Buffer) + 1];
            strcpy(Buffer, CopySource.Buffer);

            cout << "CopySource Buffer points to: 0x" << hex;
            cout << (unsigned int*)CopySource.Buffer << endl;

            cout << "Buffer points to: 0x" << hex;
            cout << (unsigned int*)Buffer << endl;
        }
    }
*/
    ~MyString() {

        cout << "Invoking destructor" << endl;

        /*
        if (Buffer != NULL)
            delete [] Buffer;
        */
    }
/*
    const char* GetString() {
        return Buffer;
    }

    int GetLength() {
        return strlen(Buffer);
    }
*/
};

void UseMyString(MyString Input)
{
    cout << "UseMyString Buffer points to : 0x" << (void *)&Input.Buffer[0] << endl;
    cout << "length: " << Input.Buffer.length();
    cout << "Buffer contains: " << Input.Buffer.c_str() << endl;

    return;
}

int main()
{

    MyString SayHello("SayHello from the class");

    UseMyString(SayHello);

    // cout << SayHello.GetString() << endl;

    return 0;
}

2017년 5월 18일 목요일

[C++] Source codes we worked on May 18

#include <iostream>
#include <string>
#include <sstream>   // string stream
#include <memory>   // Include memory to use std::unique_ptr

using namespace std;

template <typename T>
class smart_pointer {
private:
    T* m_pRawPointer;
public:
    // Construcotr
    smart_pointer(T* pData) : m_pRawPointer(pData) {};
    // Destructor
    ~smart_pointer() {
        cout << "Smart pointer destructor is invoked" << endl;
        delete m_pRawPointer;
    }
    // Operator *
    T& operator* () const {
        return *(m_pRawPointer);
    }
    // Operator ->
    T* operator-> () const {
        return m_pRawPointer;
    }
};

class Date {
private:
    int Day;
    int Month;
    int Year;

    string DateString; // class

public:
    Date (int D, int M, int Y)
        : Day(D), Month(M), Year(Y) {};

    Date operator - (int DaystoSub) {

        return Date(Day - DaystoSub, Month, Year);

    }

    // Implement Conversion operator const char *
    operator const char*() {
        ostringstream formattedDate;

        formattedDate << Day << "/" << Month << "/" << Year;

        DateString = formattedDate.str();
        return DateString.c_str();
    }

    void DisplayDate() {
        cout <<  Day << "/" << Month << "/" << Year;
    }
};

int main()
{
    Date today(18, 5, 2017);
    Date yesterday(today - 1);

   //  yesterday = today - 1;
    cout << yesterday << endl;

    /*
    cout << "Today is on: " << today << endl;
    // smart pointer
    // supports automatic deallocation() , which means delete (in C++)
    // Actually, delete implies free() in C language
    smart_pointer<int> pDynamicAllocInteger(new int);
    *pDynamicAllocInteger = 42;

    cout << *pDynamicAllocInteger << endl;

    // local variable => stack => automatically poped (removed) when main() returns
    // The destructor is invoked automatically
    // That's why it is called smart_pointer
    // How can we verify this?
    smart_pointer<Date> tomorrow(new Date(19, 5, 2017));
    tomorrow->DisplayDate();
    */
    return 0;
}

[Source codes] We worked on May 18

#include <stdio.h>

int main()
{
    /*
    char animal[2][10] = {
        {'c', 'a', 't', '\0'},
        {'d', 'o', 'g', '\0'}};
    */

    char animal[][10] = {
        "cat",
        "dog",
        "tiger"};
   
    char* ptr_array[2] = {
        "cat",
        "dog"
    };

    int i = 10;
    int count;
    char *msg = "Test";
  
    int *pi = &i;
    int **ppi = &pi;
    int ***pppi = &ppi;

    printf("i: %d, pi: %d, ppi: %d, pppi: %d\n", i, *pi, **ppi, ***pppi);

    int arr1[4] = {1, 2, 3, 4};
    int arr2[4] = {11, 12, 13,14};
    int arr3[4] = {21, 22, 23, 24};

    int *ptr_ary[3] = {arr1, arr2, arr3};

    printf("ptr_ary: %d\n", ptr_ary[2][2]);

    printf("Total Array Size: %ld\n", sizeof(animal));
    printf("Partial Array  Size(column): %ld\n", sizeof(animal[0]));

    count = sizeof(animal) / sizeof(animal[0]);
    for (i = 0; i < count; i++) {
        msg = animal[i];
        printf("animal: %s\n", msg);
    }

    for (i = 0; i < 2; i++) {
        printf("animal: %s\n", ptr_array[i]);
    }
    return 0;
}

2017년 5월 17일 수요일

[C++]Homework #7 (June 2)

HW#7

Please submit your HW to the report box in front of NC Lab, where located at office #318, 자연대연구실험동 (Building# 313). Due time: by 7:00 pm (or much earlier is possible).


2017년 5월 16일 화요일

[Open S/W] Source code we worked on May 16

#include <stdio.h>

#define PRPTR(p) printf(#p " = %p\n", p)
#define PRCHR(p) printf(#p " = %c\n", p)

int main()
{
    char msg[] = "Good bye yellow brick road~~\n";
    char *myMsg = "PNU Festival is getting started!!\n";
    char *temp;

    temp = msg;

    msg[0] = 'K';
    printf("msg: %s", msg);

    PRPTR(msg + 1);
    PRPTR(msg + 2);
    PRCHR(msg[0]);
    PRCHR(*(temp + 1)) ;
    
   // *myMsg = 'k';
    temp = myMsg;

    myMsg = "Let's have an open Makgulli bar today\n";

    printf("OLD myMsg: %s", temp);
    printf("NEW myMsg: %s", myMsg);

   
    return 0;
}

2017년 5월 12일 금요일

[Open S/W] Source code we worked on May 9 and 11

#include <stdio.h>

int main()
{
    int i;
    int *j;
    int one;
    int to_one;

    to_one = &one;

    j = &i;

    i = 20;

    printf("j value: %d\n", *j);

    return 0;
}

==================================================
#include <stdio.h>

void printAB(int* a, int* b)
{
    printf("a: %d, b: %d\n", *a, *b);
}

void swap(int* a, int* b)
{
    int temp;

    temp = *a;
    *a = *b;
    *b = temp;

    printAB(a, b);
}

int main()
{
    int a = 2, b = 3;
    int array[10];
    int *parray = array;

    // parray = array;
 
    swap(&a, &b);
    printAB(&a, &b);

    for (int i = 0; i < 10; i++) {
        array[i] = i;
    }

    parray = parray + 2;  // parray[2] -> array[2]
    *(parray + 3) = 10;   // parray[3] -> array[3]
    parray = array;
    parray++;
    parray++;
    parray--;
    parray++;
    *parray = 20;
 
    for (int i = 0; i < 10; i++) {
        printf("array[%d]: %d\n", i, array[i]);
    }

     
    return 0;
}


[C++] Source codes used in class (April 27 through May 11)

code 1

code 2



2017년 5월 4일 목요일

[Tips for HW# 6] Code for homework 6!!

Some students ask me a question related to homework 6..
Let me give you some tips ....
Try it and figure it out!!

// From Lecture notes : Inheritance part 1
// Title: [Implementing inheritance] - part 1: basics of inheritance
// pages 13 ~ 14 ..

#include <iostream>

using namespace std;

class A {

protected:
int i;
int j;

public:
A(int tempi, int tempj) {
i = tempi;
j = tempj;
}
};

// A possible way that passes parameters to the base class A
class aa : public A {

protected:
int k;

public:
aa(int aatempi, int aatempj, int aatempk) : A(aatempi, aatempj) {
k = aatempk;
}

void displayaa() {

cout << "i: " << i << endl;
cout << "j: " << j << endl;
cout << "k: " << k << endl;
}

};


int main()
{

aa myaa(2, 3, 5);

myaa.displayaa();

return 0;
}


2017년 5월 3일 수요일

[C++]Review Questions for specialization in a derived class

1. Guess the outcome generated by the following code and run it to verify your outcome!!

#include <iostream>

using namespace std;

class A {

public:
    void  foo() {
        cout << "foo() is called in class A" << endl;
    }

    void  foo(int i) {
        cout << "foo(int i) is called in class A" << endl;
        cout << "i's value: " << i << endl;
    }

};

class aa : public A {
};

int main()
{
   aa myaa;

   myaa.foo();
   myaa.foo(100);
}

2. What's wrong with the following code

#include <iostream>

using namespace std;

class A {

public:
    void  foo() {
        cout << "foo() is called in class A" << endl;
    }

    void  foo(int i) {
        cout << "foo(int i) is called in class A" << endl;
        cout << "i's value: " << i << endl;
    }

};

class aa : public A {
public:
    void foo() {
        cout << "foo() is called in class aa" << endl;
    }
};

int main()
{
   aa myaa;

   myaa.foo();
   myaa.foo(100);
}