diff --git a/2-1/.DS_Store b/2-1/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..94f2d2bb98bc3e9c053d6cc84f71fbe9626ed4e5 Binary files /dev/null and b/2-1/.DS_Store differ diff --git a/2-1/Data Structure/.DS_Store b/2-1/Data Structure/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..119d9236caffea9db767b915fcd6220ff68ea790 Binary files /dev/null and b/2-1/Data Structure/.DS_Store differ diff --git a/2-1/Data Structure/201721083_KTS_W1/.DS_Store b/2-1/Data Structure/201721083_KTS_W1/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..78b39977a8a2106f97ee35f26ba125612e4e743c Binary files /dev/null and b/2-1/Data Structure/201721083_KTS_W1/.DS_Store differ diff --git "a/2-1/Data Structure/201721083_KTS_W1/Exercise 1/ex1_\354\213\244\355\226\211\355\231\224\353\251\264.JPG" "b/2-1/Data Structure/201721083_KTS_W1/Exercise 1/ex1_\354\213\244\355\226\211\355\231\224\353\251\264.JPG" new file mode 100644 index 0000000000000000000000000000000000000000..e26bfefd96214d6671d10527a4b1ce4f6ca810ba Binary files /dev/null and "b/2-1/Data Structure/201721083_KTS_W1/Exercise 1/ex1_\354\213\244\355\226\211\355\231\224\353\251\264.JPG" differ diff --git "a/2-1/Data Structure/201721083_KTS_W1/Exercise 1/ex1_\354\275\224\353\223\234.c" "b/2-1/Data Structure/201721083_KTS_W1/Exercise 1/ex1_\354\275\224\353\223\234.c" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/2-1/Data Structure/201721083_KTS_W1/Exercise 2/ex2.1_\354\213\244\355\226\211\355\231\224\353\251\264.JPG" "b/2-1/Data Structure/201721083_KTS_W1/Exercise 2/ex2.1_\354\213\244\355\226\211\355\231\224\353\251\264.JPG" new file mode 100644 index 0000000000000000000000000000000000000000..cd257161357ff0a8202e6724fdab573c5dc869a3 Binary files /dev/null and "b/2-1/Data Structure/201721083_KTS_W1/Exercise 2/ex2.1_\354\213\244\355\226\211\355\231\224\353\251\264.JPG" differ diff --git "a/2-1/Data Structure/201721083_KTS_W1/Exercise 2/ex2.1_\354\275\224\353\223\234.c" "b/2-1/Data Structure/201721083_KTS_W1/Exercise 2/ex2.1_\354\275\224\353\223\234.c" new file mode 100644 index 0000000000000000000000000000000000000000..b8a6dfcb4d1a38a9c6740dd7e47b34666bacefb9 --- /dev/null +++ "b/2-1/Data Structure/201721083_KTS_W1/Exercise 2/ex2.1_\354\275\224\353\223\234.c" @@ -0,0 +1,22 @@ +#pragma warning(disable: 4996) +#include<stdio.h> + +struct st { + int a; + char ch; +}; + +int main() +{ + struct st obj; + struct st* stobj = &obj; + + stobj->a = 5; + stobj->ch = 'a'; + + printf("\n [%d] [%c]\n", stobj->a, stobj->ch); + + getchar(); + + return 0; +} \ No newline at end of file diff --git "a/2-1/Data Structure/201721083_KTS_W1/Exercise 2/ex2.2_\354\213\244\355\226\211\355\231\224\353\251\264.JPG" "b/2-1/Data Structure/201721083_KTS_W1/Exercise 2/ex2.2_\354\213\244\355\226\211\355\231\224\353\251\264.JPG" new file mode 100644 index 0000000000000000000000000000000000000000..66e9d11bb607d1de824ee6c099b6a97ecdc61281 Binary files /dev/null and "b/2-1/Data Structure/201721083_KTS_W1/Exercise 2/ex2.2_\354\213\244\355\226\211\355\231\224\353\251\264.JPG" differ diff --git "a/2-1/Data Structure/201721083_KTS_W1/Exercise 2/ex2.2_\354\275\224\353\223\234.c" "b/2-1/Data Structure/201721083_KTS_W1/Exercise 2/ex2.2_\354\275\224\353\223\234.c" new file mode 100644 index 0000000000000000000000000000000000000000..b202d7023ac7d91387e0a1e257667b52c6dff672 --- /dev/null +++ "b/2-1/Data Structure/201721083_KTS_W1/Exercise 2/ex2.2_\354\275\224\353\223\234.c" @@ -0,0 +1,40 @@ +#pragma warning(disable : 4996) +#include<stdio.h> + +long long fact(int); + +int main() +{ + int n; + long long result; + + printf("Input 'N'\n"); + scanf_s("%d", &n); + + result = fact(n); + + printf("\n\n factorial(%d) = %ld.\n",n, result); + + getchar(); getchar(); +} + +long long fact(int n) +{ + long long value; + + if(n <= 1) + { + printf("\n fact(1) called!"); + printf("\n fact(1) returned!!"); + + return 1; + } + else { + printf("\n fact(%d) called!", n); + + value = (n * fact(n - 1)); + + printf("\n fact(%d) returned value : %d!!", n, value); + return value; + } +} \ No newline at end of file diff --git "a/2-1/Data Structure/201721083_KTS_W1/Exercise 3/ex3_\354\213\244\355\226\211\355\231\224\353\251\264.JPG" "b/2-1/Data Structure/201721083_KTS_W1/Exercise 3/ex3_\354\213\244\355\226\211\355\231\224\353\251\264.JPG" new file mode 100644 index 0000000000000000000000000000000000000000..2c9b08ed29537a555c09d82d3822e3c8f364dace Binary files /dev/null and "b/2-1/Data Structure/201721083_KTS_W1/Exercise 3/ex3_\354\213\244\355\226\211\355\231\224\353\251\264.JPG" differ diff --git "a/2-1/Data Structure/201721083_KTS_W1/Exercise 3/ex3_\354\275\224\353\223\234.c" "b/2-1/Data Structure/201721083_KTS_W1/Exercise 3/ex3_\354\275\224\353\223\234.c" new file mode 100644 index 0000000000000000000000000000000000000000..70ba5983c804a6cb38a38c00469084fd70091c61 --- /dev/null +++ "b/2-1/Data Structure/201721083_KTS_W1/Exercise 3/ex3_\354\275\224\353\223\234.c" @@ -0,0 +1,27 @@ +#pragma warning(disable : 4996) +#include <stdio.h> + +void hanoi(int n, char start, char middle, char last) +{ + if (n == 1) + { + printf("move object %d from %c to %c\n", n, start, last); + } + else { + hanoi(n-1, start, last, middle); + printf("move object %d from %c to %c\n", n, start, last); + hanoi(n - 1, middle, start, last); + } +} + +int main() +{ + int n; + printf("Number of disk : "); + scanf_s("%d", &n); + + hanoi(n, 'A', 'B', 'C'); + + getchar(); + return 0; +} \ No newline at end of file diff --git "a/2-1/Data Structure/201721083_KTS_W1/Exercise 4/ex4_\354\213\244\355\226\211\355\231\224\353\251\264.JPG" "b/2-1/Data Structure/201721083_KTS_W1/Exercise 4/ex4_\354\213\244\355\226\211\355\231\224\353\251\264.JPG" new file mode 100644 index 0000000000000000000000000000000000000000..b89728d59bfc185edbfc6b23fc1bdcd07f062853 Binary files /dev/null and "b/2-1/Data Structure/201721083_KTS_W1/Exercise 4/ex4_\354\213\244\355\226\211\355\231\224\353\251\264.JPG" differ diff --git "a/2-1/Data Structure/201721083_KTS_W1/Exercise 4/ex4_\354\275\224\353\223\234.c" "b/2-1/Data Structure/201721083_KTS_W1/Exercise 4/ex4_\354\275\224\353\223\234.c" new file mode 100644 index 0000000000000000000000000000000000000000..975690053219a0ae2110e970683e8b81fcdc5453 --- /dev/null +++ "b/2-1/Data Structure/201721083_KTS_W1/Exercise 4/ex4_\354\275\224\353\223\234.c" @@ -0,0 +1,50 @@ +#pragma warning(disable: 4996) +#include<stdio.h> +#include<time.h> +#define MAX_LENGTH 51 + +unsigned long long recursive_fibo(unsigned int n) +{ + if (n == 0) return 0; + if (n == 1) return 1; + return recursive_fibo(n - 1) + recursive_fibo(n - 2); +} +unsigned long long iterative_fibo(unsigned int n) +{ + unsigned long long previous = 0; + unsigned long long current = 1; + unsigned long long next = 1; + int i; + if (n == 0) return 0; + for (int i = 1; i < n; ++i) + { + next = previous + current; + previous = current; + current = next; + } + return current; +} + +int main() { + unsigned int length; + double begin, end; + double t1, t2; + unsigned long long val; + + for (length = 1; length <= MAX_LENGTH; length += 5) + { + begin = clock(); + val = recursive_fibo(length); + end = clock(); + t1 = (end - begin) / CLOCKS_PER_SEC; + + begin = clock(); + val = iterative_fibo(length); + end = clock(); + t2 = (end - begin) / CLOCKS_PER_SEC; + + printf("[Length : %3d] Recursive : %.3lf sec, Iterative : %.3lf sec\n", length, t1, t2); + } + getchar(); + return 0; +} \ No newline at end of file diff --git a/2-1/Data Structure/201721083_KTS_W10_exercise1.c b/2-1/Data Structure/201721083_KTS_W10_exercise1.c new file mode 100644 index 0000000000000000000000000000000000000000..7d7607971ef7903d3f3b5088945a6efc130bd8b0 --- /dev/null +++ b/2-1/Data Structure/201721083_KTS_W10_exercise1.c @@ -0,0 +1,100 @@ +#include <stdio.h> +#include <limits.h> +#define TRUE 1 +#define FALSE 0 +#define MAX_VERTICES 5 +#define INF 10000 + + + +int weight[MAX_VERTICES][MAX_VERTICES]={ + {0,10,5,INF,INF}, + {INF,0,2,1,INF}, + {INF,3,0,9,2}, + {INF,INF,INF,0,4}, + {7,INF,INF,6,0} +}; + +int distance[MAX_VERTICES]; +int S[MAX_VERTICES]; + +int printStep(int step){ + int i; + printf("\n %3d step : S = {",step); + for(i=0; i<MAX_VERTICES;i++){ + if(S[i]==TRUE) + printf("%3c",i+65); + } + if(step<1) + printf("}\t\t\t\t"); + else if(step<3) + printf("}\t\t\t"); + else if(step<4) + printf("}\t\t"); + else + printf("}\t"); + printf("distance : ["); + for(i=0; i<MAX_VERTICES; i++){ + if(distance[i]==10000) + printf("%4c",'*'); + else + printf("%4d",distance[i]); + + } + printf("%4c",']'); + return ++step; +} + +int choose(int n){ + int i,min,minPos; + min = INT_MAX; + minPos = -1; + for(i=0;i<n;i++){ + if((distance[i])<min&&!S[i]){ + min = distance[i]; + minPos = i; + } + } + return minPos; +} + +void Dijkstra(int start, int n){ + int i,u,w,step = 0; + for(i=1;i<n;i++){ + distance[i]=weight[start][i]; + S[i]=FALSE; + } + S[start] = TRUE; + distance[start]=0; + step = printStep(0); + for(i=0;i<n-1;i++){ + u=choose(n); + S[u]=TRUE; + for(w=0;w<n;w++){ + if(!S[w]){ + if(distance[u]+weight[u][w]<distance[w]){ + distance[w]=distance[u]+weight[u][w]; + } + } + } + step = printStep(step); + } +} + + +int main(void){ + int i,j; + printf("\n**********adj matrix**********\n\n"); + for(i=0;i<MAX_VERTICES;i++){ + for(j=0;j<MAX_VERTICES;j++){ + if(weight[i][j]==10000) + printf("%4c",'*'); + else + printf("%4d",weight[i][j]); + } + printf("\n\n"); + } + printf("\n**********Dijkstra shortest path**********\n\n"); + Dijkstra(0,MAX_VERTICES); +} + diff --git a/2-1/Data Structure/201721083_KTS_W10_exercise2.cpp b/2-1/Data Structure/201721083_KTS_W10_exercise2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d397e9615cc35bc3ad39f73d15f718340037a42f --- /dev/null +++ b/2-1/Data Structure/201721083_KTS_W10_exercise2.cpp @@ -0,0 +1,91 @@ +#include <stdio.h> + +#define INT_MAX 9999 + +struct Edge{ + int src,dest,weight; +}; + +struct Graph{ + int V,E; + struct Edge* edge; +}; + +struct Graph* createGraph(int V, int E){ + struct Graph* graph = new Graph; + graph->V=V; + graph->E=E; + graph->edge = new Edge; + return graph; +} + +void printArr(int dist[], int n){ + printf("Vertex Distance from Source\n"); + for(int i =0; i<n;++i){ + printf("%d \t\t %d\n",i,dist[i]); + } +} + +void BellmanFord(struct Graph* graph, int src){ + int V = graph->V; + int E = graph->E; + int dist[5]; + for(int i=0; i<V;i++){ + dist[i] = INT_MAX; + } + dist[src] = 0; + for(int i=1;i<=V-1;i++){ + for(int j=0; j<E; j++){ + int u= graph->edge[j].src; + int v = graph->edge[j].dest; + int weight = graph->edge[j].weight; + if(dist[u]!=INT_MAX&&dist[u]+weight<dist[v]) + dist[v]=dist[u]+weight; + } + } + for(int i=0;i<E;i++){ + int u = graph->edge[i].src; + int v = graph->edge[i].dest; + int weight = graph->edge[i].weight; + if(dist[u]!=INT_MAX&&dist[u]+weight<dist[v]) + printf("Graph contains negative weight cycle"); + } + printArr(dist,V); + return; +} + + + +int main() { + int V = 5; + int E = 8; + struct Graph* graph = createGraph(V, E); + + graph->edge[0].src = 0; + graph->edge[0].dest = 1; + graph->edge[0].weight = -1; + graph->edge[1].src = 0; + graph->edge[1].dest = 2; + graph->edge[1].weight = 4; + graph->edge[2].src = 1; + graph->edge[2].dest = 2; + graph->edge[2].weight = 3; + graph->edge[3].src = 1; + graph->edge[3].dest = 3; + graph->edge[3].weight = 2; + graph->edge[4].src = 1; + graph->edge[4].dest = 4; + graph->edge[4].weight = 2; + graph->edge[5].src = 3; + graph->edge[5].dest = 2; + graph->edge[5].weight = 5; + graph->edge[6].src = 3; + graph->edge[6].dest = 1; + graph->edge[6].weight = 1; + graph->edge[7].src = 4; + graph->edge[7].dest = 3; + graph->edge[7].weight = -3; + BellmanFord(graph, 0); + getchar(); + return 0; +} diff --git a/2-1/Data Structure/201721083_KTS_W11_exercise1.c b/2-1/Data Structure/201721083_KTS_W11_exercise1.c new file mode 100644 index 0000000000000000000000000000000000000000..6f4e21688acdbf0c1bc2f3bed1abf3c998848ae4 --- /dev/null +++ b/2-1/Data Structure/201721083_KTS_W11_exercise1.c @@ -0,0 +1,33 @@ +#include <stdio.h> + +int size; + +void insertionSort(int a[],int size){ + int i,j,t,temp; + printf("\nInit Array "); + for(t=0;t<size;t++){ + printf("%d ",a[t]); + } + printf("\n\n<<<<<<<insertion Sorting>>>>>>>"); + for(i=1;i<size;i++){ + temp=a[i]; + j=i; + while((j>0)&&(a[j-1]>temp)){ + a[j]=a[j-1]; + j=j-1; + } + a[j]=temp; + printf("\nStep %d:",i+1); + for(t=0;t<size;t++){ + printf("%3d",a[t]); + } + } +} + +void main(){ + int list[8] ={69,10,30,2,16,8,31,22}; + size = 8; + insertionSort(list,size); + getchar(); +} + diff --git a/2-1/Data Structure/201721083_KTS_W11_exercise2.c b/2-1/Data Structure/201721083_KTS_W11_exercise2.c new file mode 100644 index 0000000000000000000000000000000000000000..485a94d1da3d9238c2ca71e558aba78bedb907c9 --- /dev/null +++ b/2-1/Data Structure/201721083_KTS_W11_exercise2.c @@ -0,0 +1,64 @@ +#include <stdio.h>; +#define MAX 30 +typedef int element; +int size; +element sorted[MAX]; + + +void merge(element a[],int m,int middle,int n){ + int i,j,k,t; + i = m; + j=middle+1; + k=m; + while(i<=middle && j<=n){ + if(a[i]<=a[j]){ + sorted[k]=a[i]; + i++; + } + else{ + sorted[k]=a[j]; + j++; + } + k++; + } + if(i>middle){ + for(t=j;t<=n;t++,k++){ + sorted[k]=a[t]; + } + } + else{ + for(t=i;t<=middle;t++,k++){ + sorted[k]=a[t]; + } + } + + for(t=m;t<=n;t++){ + a[t]=sorted[t]; + } + printf("\n Merge Sort >>"); + for(t=0;t<size;t++){ + printf("%4d",a[t]); + } +} + +void mergeSort(element a[],int m, int n){ + int middle; + if(m<n){ + middle = (m+n)/2; + mergeSort(a,m,middle); + mergeSort(a,middle+1,n); + merge(a,m,middle,n); + } +} +void main(){ + int t; + element list[8] = {69,10,30,2,16,8,31,22}; + size = 8; + printf("\n Items>>"); + for (t = 0; t < size; t++) {printf("%4d ", list[t]);} + printf("\n\n<<<<<<<<<< Execute Merge Sort >>>>>>>>>>\n"); + mergeSort(list, 0, size - 1); + getchar(); + } + + diff --git a/2-1/Data Structure/201721083_KTS_W11_exercise3.c b/2-1/Data Structure/201721083_KTS_W11_exercise3.c new file mode 100644 index 0000000000000000000000000000000000000000..b41518a2e24d98814a7f804fde1ad656912c54bd --- /dev/null +++ b/2-1/Data Structure/201721083_KTS_W11_exercise3.c @@ -0,0 +1,54 @@ +#include <stdio.h> +typedef int element; +int size, i = 0; + +int partition(element a[],int begin,int end){ + int pivot,L,R,t; + element temp; + L=begin; + R=end+1; + pivot =begin; + printf("\n [Step %d: pivot=%d ] \n", ++i, a[pivot]); + do { + do{ + L++; + }while((a[L]<a[pivot])&&(L<=R)); + do{ + R--; + }while((a[R]>a[pivot])&&(L<=R)); + if(L<R){ + temp = a[L]; + a[L]=a[R]; + a[R]=temp; + } + } while(L<R); + temp = a[pivot]; + a[pivot]=a[R]; + a[R]=temp; + + + for (t = 0; t<size; t++) printf(" %d", a[t]);{ + printf("\n"); + } + return R;// return pivot location + +} +void quickSort(element a[], int begin, int end){ + int p; + if (begin<end){ + p = partition(a,begin,end); + quickSort(a,begin,p-1); + quickSort(a,p+1,end); + } +} + + +void main() { + element list[8] = { 69, 10, 30, 2, 16, 8, 31, 22 }; + size = 8; + printf("\n [ Initialization ] \n"); + for (int i = 0; i < size; i++) + printf(" %d", list[i]); printf("\n"); + quickSort(list, 0, size - 1); + getchar(); +} diff --git a/2-1/Data Structure/201721083_KTS_W2/Exercise1/exercise1.JPG b/2-1/Data Structure/201721083_KTS_W2/Exercise1/exercise1.JPG new file mode 100644 index 0000000000000000000000000000000000000000..53de62302b0a8552539e526a075498de93c21e30 Binary files /dev/null and b/2-1/Data Structure/201721083_KTS_W2/Exercise1/exercise1.JPG differ diff --git a/2-1/Data Structure/201721083_KTS_W2/Exercise1/exercise1.c b/2-1/Data Structure/201721083_KTS_W2/Exercise1/exercise1.c new file mode 100644 index 0000000000000000000000000000000000000000..89b7df16a9cb907b0ef9a43a8a29e2346b50e1b3 --- /dev/null +++ b/2-1/Data Structure/201721083_KTS_W2/Exercise1/exercise1.c @@ -0,0 +1,25 @@ +#include <stdio.h> + +void main() { + int i; + char string1[20] = "Dreams come true!", string2[20], * ptr1; + ptr1 = string1; + printf("\n %u", string1); + printf("\n %u", ptr1); + printf("\n %s", string1); + printf("\n %s", ptr1); + printf("\n\n %s\n", (ptr1 + 7)); + + for (i = 16; i >= 0; i--) { + putchar(*(ptr1 + i)); + } + for (i = 0; i < 20; i++) { + string2[i] = *(ptr1 + i); + } + printf("\n %s", string2); + *(ptr1 + 0) = 'P'; + printf("\n ptr1 = %s", ptr1); + *(ptr1 + 1) = 'e'; + printf("\n ptr1 = %s", ptr1); + getchar(); +} \ No newline at end of file diff --git a/2-1/Data Structure/201721083_KTS_W2/Exercise2/exercise2.JPG b/2-1/Data Structure/201721083_KTS_W2/Exercise2/exercise2.JPG new file mode 100644 index 0000000000000000000000000000000000000000..b325de34a3b994b3fc2e3376bb113533f7bc5aeb Binary files /dev/null and b/2-1/Data Structure/201721083_KTS_W2/Exercise2/exercise2.JPG differ diff --git a/2-1/Data Structure/201721083_KTS_W2/Exercise2/exercise2.c b/2-1/Data Structure/201721083_KTS_W2/Exercise2/exercise2.c new file mode 100644 index 0000000000000000000000000000000000000000..e856b9ff027af128bca5f256f0af527e1eacc079 --- /dev/null +++ b/2-1/Data Structure/201721083_KTS_W2/Exercise2/exercise2.c @@ -0,0 +1,16 @@ +#include <stdio.h> + +void main() { + int i; + char* ptrArray[4] = { {"Korea"},{"Suwon"},{"AjouSoftware"},{"DataStructures"} }; + for (i = 0; i < 4; i++) { + printf("\n %u", &ptrArray[i]); + } + + ptrArray[3] = "Human-Computer Interaction"; + printf("\n\n"); + for (i = 0; i < 4; i++) { + printf("\n %s", ptrArray[i]); + } + getchar(); +} \ No newline at end of file diff --git a/2-1/Data Structure/201721083_KTS_W2/Exercise3/exercise3.JPG b/2-1/Data Structure/201721083_KTS_W2/Exercise3/exercise3.JPG new file mode 100644 index 0000000000000000000000000000000000000000..339bfe22f6a99c29dec679febbc665f7da71279f Binary files /dev/null and b/2-1/Data Structure/201721083_KTS_W2/Exercise3/exercise3.JPG differ diff --git a/2-1/Data Structure/201721083_KTS_W2/Exercise3/exercise3.c b/2-1/Data Structure/201721083_KTS_W2/Exercise3/exercise3.c new file mode 100644 index 0000000000000000000000000000000000000000..70db489973cba60d0c04e62a7af6959ff82e2c3e --- /dev/null +++ b/2-1/Data Structure/201721083_KTS_W2/Exercise3/exercise3.c @@ -0,0 +1,34 @@ +#include <stdio.h> + +void main() { + char* ptrArray[2]; + char** ptrptr; + int i; + + ptrArray[0] = "Korea"; + ptrArray[1] = "Suwon"; + + ptrptr = ptrArray; + + printf("\n %u", &ptrArray[0]); + printf("\n %c", ptrArray[0]); + printf("\n %c", ptrArray[0][0]); + printf("\n %c", ptrArray[0][1]); + printf("\n %s \n\n", ptrArray[0]); + + printf("\n %u", &ptrArray[1]); + printf("\n %c", ptrArray[1]); + printf("\n %c", ptrArray[1][0]); + printf("\n %c", ptrArray[1][1]); + printf("\n %s \n\n", ptrArray[1]); + + printf("\n %u", &ptrptr); + printf("\n %u", ptrptr); + printf("\n %c", *ptrptr); + printf("\n %c", *(ptrptr[0] + 0)); + printf("\n %c", *(ptrptr[0] + 1)); + printf("\n %s", *ptrptr); + + + +} \ No newline at end of file diff --git a/2-1/Data Structure/201721083_KTS_W2/Exercise4/exercise4.JPG b/2-1/Data Structure/201721083_KTS_W2/Exercise4/exercise4.JPG new file mode 100644 index 0000000000000000000000000000000000000000..fab1ed653228077bed4ea3b0e6cac448e3f73a99 Binary files /dev/null and b/2-1/Data Structure/201721083_KTS_W2/Exercise4/exercise4.JPG differ diff --git a/2-1/Data Structure/201721083_KTS_W2/Exercise4/exercise4.c b/2-1/Data Structure/201721083_KTS_W2/Exercise4/exercise4.c new file mode 100644 index 0000000000000000000000000000000000000000..bb00f416aaf676245fd12b02e5b7307e0a8e09f3 --- /dev/null +++ b/2-1/Data Structure/201721083_KTS_W2/Exercise4/exercise4.c @@ -0,0 +1,22 @@ +#include <stdio.h> +#include <string.h> + +struct employee { + char name[10]; + int year; + int pay; +}; + +void main() { + int i; + struct employee em[2] = { + {"Jack", 2014, 4200}, + {"Jone", 2015, 3300} + }; + for (i = 0; i < 2; i++) { + printf("\n Name : %s", em[i].name); + printf("\n Year : %d", em[i].year); + printf("\n Salary : %d \n", em[i].pay); + } + getchar(); +} \ No newline at end of file diff --git a/2-1/Data Structure/201721083_KTS_W2/Exercise5/exercise5.JPG b/2-1/Data Structure/201721083_KTS_W2/Exercise5/exercise5.JPG new file mode 100644 index 0000000000000000000000000000000000000000..f43c4a0b6d576de2b1cb04ce1aa7a47f019b2922 Binary files /dev/null and b/2-1/Data Structure/201721083_KTS_W2/Exercise5/exercise5.JPG differ diff --git a/2-1/Data Structure/201721083_KTS_W2/Exercise5/exercise5.c b/2-1/Data Structure/201721083_KTS_W2/Exercise5/exercise5.c new file mode 100644 index 0000000000000000000000000000000000000000..c226dc5a40799f6f34666e1c0e5b97f62b406747 --- /dev/null +++ b/2-1/Data Structure/201721083_KTS_W2/Exercise5/exercise5.c @@ -0,0 +1,22 @@ +#include <stdio.h> +#include <string.h> + +struct employee { + char name[10]; + int year; + int pay; +}; + +void main() { + struct employee em1; + struct employee* em2 = &em1; + strcpy(em2->name, "You"); + em2->year = 2015; + em2->pay = 5900; + printf("\n Name : %s", em2->name); + printf("\n Year : %d", em2->year); + printf("\n Salary : %d", em2->pay); + printf("\n Name : %s", em1.name); + printf("\n Year : %d", em1.year); + printf("\n Salary : %d", em1.pay); +} \ No newline at end of file diff --git a/2-1/Data Structure/201721083_KTS_W3_exercise1.c b/2-1/Data Structure/201721083_KTS_W3_exercise1.c new file mode 100644 index 0000000000000000000000000000000000000000..eb562ed2fb0426cc83b0be070272833ef3c69e7c --- /dev/null +++ b/2-1/Data Structure/201721083_KTS_W3_exercise1.c @@ -0,0 +1,61 @@ +#include <stdio.h> +#define MAX(a,b) ((a>b)?a:b) +#define MAX_DEGREE 50 + +typedef struct { + int degree; + float coef[MAX_DEGREE]; +} polynomial; + +polynomial addPoly(polynomial, polynomial); +void printPoly(polynomial); + +void main() { + polynomial A = { 3,{4,3,5,0} }; + polynomial B = { 4,{3,1,0,2,1} }; + + polynomial C; + C = addPoly(A, B); + + printf("\nA(x) = "); printPoly(A); + printf("\nB(x) = "); printPoly(B); + printf("\nC(x) = "); printPoly(C); + + getchar(); +} + +polynomial addPoly(polynomial A, polynomial B) { + polynomial C; + int A_index = 0, B_index = 0, C_index = 0; + int A_degree = A.degree, B_degree = B.degree; + C.degree = MAX(A.degree, B.degree); + + while (A_index <= A.degree && B_index <= B.degree) { + if (A_degree == B_degree) { + C.coef[C_index++] = A.coef[A_index++] + B.coef[B_index++]; + A_degree--; + B_degree--; + } + else if (A_degree > B_degree) + { + C.coef[C_index++] = A.coef[A_index++]; + A_degree--; + } + else { + C.coef[C_index++] = B.coef[B_index++]; + B_degree--; + } + } + return C; +} + +void printPoly(polynomial P) { + int i, degree; + degree = P.degree; + + for (i = 0; i <= P.degree; i++) { + printf("%.2fx^%d +", P.coef[i], degree); + degree--; + } + printf("\n"); +} \ No newline at end of file diff --git a/2-1/Data Structure/201721083_KTS_W3_exercise2.c b/2-1/Data Structure/201721083_KTS_W3_exercise2.c new file mode 100644 index 0000000000000000000000000000000000000000..5f56350e25b53e4df9ba4e4b6d1f20623619c636 --- /dev/null +++ b/2-1/Data Structure/201721083_KTS_W3_exercise2.c @@ -0,0 +1,58 @@ +#include<stdio.h> +#include<string.h> + +int failure[20] = { 0, }; + +void fail(char* pat) { + int i, j; + int n = strlen(pat); + failure[0] = -1; + for (j = 1; j < n; j++) { + i = failure[j - 1]; + while (pat[j] != pat[i + 1] && i >= 0) + i = failure[i]; + if (pat[j] == pat[i + 1]) + failure[j] = i + 1; + else + { + failure[j] = -1; + } + } +} + +int pmatch(char* string, char* pat) { + int i = 0; + int j = 0; + int lens = strlen(string); + int lenp = strlen(pat); + + while (i < lens && j < lenp) { + if (string[i] == pat[j]) { + i++; + j++; + } + else if (j == 0) + i++; + else + j = failure[j - 1] + 1; + } + + return j == lenp ? i - lenp : -1; +} + +int main() { + char string[] = "Ajou university is the best in the world."; + char* pat = "best"; + + fail(pat); + int matched_count = pmatch(string, pat); + + if (matched_count == -1) { + printf("No pattern found"); + } + else { + printf("Index of the first pattern found: %d", matched_count); + } + + getchar(); +} \ No newline at end of file diff --git a/2-1/Data Structure/201721083_KTS_W4_exercise1.c b/2-1/Data Structure/201721083_KTS_W4_exercise1.c new file mode 100644 index 0000000000000000000000000000000000000000..bee0af8531e21a8b3518c53ff13d6fc78b952d2e --- /dev/null +++ b/2-1/Data Structure/201721083_KTS_W4_exercise1.c @@ -0,0 +1,92 @@ +#include <stdio.h> +#include <stdlib.h> +#include<string.h> +#include <stdbool.h> + +typedef enum { lparen, rparen, plus, minus, times, divide, mod, eos, operand } precedence; + +int isp[] = { 0,19,12,12,13,13,13,0 }; +int icp[] = { 20,19,12,12,13,13,13,0 }; + +precedence stack[10]; + +char expression[] = "(a/(b-c+d))*(e-a)*c"; + + +void push(int* top, precedence token) { + stack[++(*top)] = token; +} + +precedence pop(int* top) { + return stack[(*top)--]; +} + + + +precedence getToken(char* symbol, int* n) +{ + *symbol = expression[(*n)++]; + + switch (*symbol) { + case'(': return lparen; + case')': return rparen; + case'+': return plus; + case'-': return minus; + case'*': return times; + case'/': return divide; + case'%': return mod; + case'\0': return eos; + default: return operand; + } +} +void printToken(precedence token) { + char* a; + switch (token) { + case lparen: a = "("; break; + case rparen: a = ")"; break; + case plus: a = "+"; break; + case minus: a = "-"; break; + case times: a = "*"; break; + case divide: a = "/"; break; + case mod: a = "%"; + default: a = "\0"; + } + printf("%c", *a); +} +void postfix() { + char symbol; + precedence token; + + int n = 0; + int top = 0; + stack[0] = eos; + for (token = getToken(&symbol, &n); token != eos; token = getToken(&symbol, &n)) { + if (token == operand) { + printf("%c", symbol); + } + else if (token == rparen) + { + while (stack[top] != lparen) { + printToken(pop(&top)); + } + pop(&top); + } + else { + while (isp[stack[top]] >= icp[token]) { + printToken(pop(&top)); + } + push(&top, token); + } + } + while ((token = pop(&top)) != eos) { + printToken(token); + } + printf("\n"); +} +int main() { + printf("�й� 201721083 �̸� ���¼�\n\n"); + postfix(); + getchar(); + + return 0; +} \ No newline at end of file diff --git a/2-1/Data Structure/201721083_KTS_W4_exercise2.c b/2-1/Data Structure/201721083_KTS_W4_exercise2.c new file mode 100644 index 0000000000000000000000000000000000000000..7a8427d006dc378986710be72e17f4efea6cba07 --- /dev/null +++ b/2-1/Data Structure/201721083_KTS_W4_exercise2.c @@ -0,0 +1,140 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <stdbool.h> + +#define MAX_STACK_SIZE 100 +#define MAZE_SIZE 6 + +typedef struct StackObjectRec { + short r; + short c; +} StackObject; + +StackObject stack[MAX_STACK_SIZE]; +int top = -1; + +void initialize() { + top = -1; +} + +bool isEmpty() { + return(top == -1); +} +bool isFull() { + return (top == (MAX_STACK_SIZE - 1)); +} +void push(StackObject item) { + if (isFull()) { + printf("stack is full\n"); + exit(-1); + } + else + stack[++top] = item; +} + +StackObject pop() { + if (isEmpty()) { + printf("stack is empty\n"); + exit(-1); + } + else + return stack[top--]; +} + +void printStack() { + int i; + for (i = 5; i > top; i--) + printf("l l\n"); + for (i = top; i >= 0; i--) + printf("l(%01d,%01d)l\n", stack[i].r, stack[i].c); + printf("-------\n\n"); + printf("====================\n"); +} + +StackObject here = { 0,0 }; +StackObject entry = { 0,0 }; + +char maze[MAZE_SIZE][MAZE_SIZE] = { + {'e','0','1','1','1','1'}, + {'1','0','1','0','0','0'}, + {'1','0','0','0','1','0'}, + {'1','0','1','1','0','0'}, + {'1','0','1','0','0','1'}, + {'1','1','1','0','0','x'}, +}; + +void printMaze(char m[MAZE_SIZE][MAZE_SIZE]) { + int r, c; + printf("\n"); + for (r = 0; r < MAZE_SIZE; r++) { + for (c = 0; c < MAZE_SIZE; c++) { + if (c == here.c && r == here.r) + printf("m"); + else { + if (m[r][c] == 0) printf("0"); + else printf("%c", m[r][c]); + } + } + printf("\n"); + } + printf("\n"); +} + +void pushLoc(int r, int c) { + if (r < 0 || c < 0) + return; + if (maze[r][c] != '1' && maze[r][c] != '.') { + StackObject tmp; + tmp.r = r; + tmp.c = c; + push(tmp); + } +} +bool search_maze() { + int r, c; + while (maze[here.r][here.c] != 'x') { + printMaze(maze); + r = here.r; + c = here.c; + maze[r][c] = '.'; + pushLoc(r - 1, c); + pushLoc(r + 1, c); + pushLoc(r, c - 1); + pushLoc(r, c + 1); + if (isEmpty()) { + return false; + } + else + here = pop(); + } + return true; +} + +int main() { + bool result; + printf("�й� 201721083 �̸� ���¼�\n\n"); + + here = entry; + printf("====================\n"); + printf("Start\n"); + printf("====================\n"); + printMaze(maze); + printStack(); + + result = search_maze(); + + printf("====================\n"); + if (result == true) { + printf("Success!!\n"); + } + else { + printf("Fail!!\n"); + } + printf("====================\n"); + printMaze(maze); + printStack(); + + getchar(); + return 0; +} \ No newline at end of file diff --git a/2-1/Data Structure/201721083_KTS_W5_exercise1.c b/2-1/Data Structure/201721083_KTS_W5_exercise1.c new file mode 100644 index 0000000000000000000000000000000000000000..bee0af8531e21a8b3518c53ff13d6fc78b952d2e --- /dev/null +++ b/2-1/Data Structure/201721083_KTS_W5_exercise1.c @@ -0,0 +1,92 @@ +#include <stdio.h> +#include <stdlib.h> +#include<string.h> +#include <stdbool.h> + +typedef enum { lparen, rparen, plus, minus, times, divide, mod, eos, operand } precedence; + +int isp[] = { 0,19,12,12,13,13,13,0 }; +int icp[] = { 20,19,12,12,13,13,13,0 }; + +precedence stack[10]; + +char expression[] = "(a/(b-c+d))*(e-a)*c"; + + +void push(int* top, precedence token) { + stack[++(*top)] = token; +} + +precedence pop(int* top) { + return stack[(*top)--]; +} + + + +precedence getToken(char* symbol, int* n) +{ + *symbol = expression[(*n)++]; + + switch (*symbol) { + case'(': return lparen; + case')': return rparen; + case'+': return plus; + case'-': return minus; + case'*': return times; + case'/': return divide; + case'%': return mod; + case'\0': return eos; + default: return operand; + } +} +void printToken(precedence token) { + char* a; + switch (token) { + case lparen: a = "("; break; + case rparen: a = ")"; break; + case plus: a = "+"; break; + case minus: a = "-"; break; + case times: a = "*"; break; + case divide: a = "/"; break; + case mod: a = "%"; + default: a = "\0"; + } + printf("%c", *a); +} +void postfix() { + char symbol; + precedence token; + + int n = 0; + int top = 0; + stack[0] = eos; + for (token = getToken(&symbol, &n); token != eos; token = getToken(&symbol, &n)) { + if (token == operand) { + printf("%c", symbol); + } + else if (token == rparen) + { + while (stack[top] != lparen) { + printToken(pop(&top)); + } + pop(&top); + } + else { + while (isp[stack[top]] >= icp[token]) { + printToken(pop(&top)); + } + push(&top, token); + } + } + while ((token = pop(&top)) != eos) { + printToken(token); + } + printf("\n"); +} +int main() { + printf("�й� 201721083 �̸� ���¼�\n\n"); + postfix(); + getchar(); + + return 0; +} \ No newline at end of file diff --git a/2-1/Data Structure/201721083_KTS_W5_exercise2.c b/2-1/Data Structure/201721083_KTS_W5_exercise2.c new file mode 100644 index 0000000000000000000000000000000000000000..7a8427d006dc378986710be72e17f4efea6cba07 --- /dev/null +++ b/2-1/Data Structure/201721083_KTS_W5_exercise2.c @@ -0,0 +1,140 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <stdbool.h> + +#define MAX_STACK_SIZE 100 +#define MAZE_SIZE 6 + +typedef struct StackObjectRec { + short r; + short c; +} StackObject; + +StackObject stack[MAX_STACK_SIZE]; +int top = -1; + +void initialize() { + top = -1; +} + +bool isEmpty() { + return(top == -1); +} +bool isFull() { + return (top == (MAX_STACK_SIZE - 1)); +} +void push(StackObject item) { + if (isFull()) { + printf("stack is full\n"); + exit(-1); + } + else + stack[++top] = item; +} + +StackObject pop() { + if (isEmpty()) { + printf("stack is empty\n"); + exit(-1); + } + else + return stack[top--]; +} + +void printStack() { + int i; + for (i = 5; i > top; i--) + printf("l l\n"); + for (i = top; i >= 0; i--) + printf("l(%01d,%01d)l\n", stack[i].r, stack[i].c); + printf("-------\n\n"); + printf("====================\n"); +} + +StackObject here = { 0,0 }; +StackObject entry = { 0,0 }; + +char maze[MAZE_SIZE][MAZE_SIZE] = { + {'e','0','1','1','1','1'}, + {'1','0','1','0','0','0'}, + {'1','0','0','0','1','0'}, + {'1','0','1','1','0','0'}, + {'1','0','1','0','0','1'}, + {'1','1','1','0','0','x'}, +}; + +void printMaze(char m[MAZE_SIZE][MAZE_SIZE]) { + int r, c; + printf("\n"); + for (r = 0; r < MAZE_SIZE; r++) { + for (c = 0; c < MAZE_SIZE; c++) { + if (c == here.c && r == here.r) + printf("m"); + else { + if (m[r][c] == 0) printf("0"); + else printf("%c", m[r][c]); + } + } + printf("\n"); + } + printf("\n"); +} + +void pushLoc(int r, int c) { + if (r < 0 || c < 0) + return; + if (maze[r][c] != '1' && maze[r][c] != '.') { + StackObject tmp; + tmp.r = r; + tmp.c = c; + push(tmp); + } +} +bool search_maze() { + int r, c; + while (maze[here.r][here.c] != 'x') { + printMaze(maze); + r = here.r; + c = here.c; + maze[r][c] = '.'; + pushLoc(r - 1, c); + pushLoc(r + 1, c); + pushLoc(r, c - 1); + pushLoc(r, c + 1); + if (isEmpty()) { + return false; + } + else + here = pop(); + } + return true; +} + +int main() { + bool result; + printf("�й� 201721083 �̸� ���¼�\n\n"); + + here = entry; + printf("====================\n"); + printf("Start\n"); + printf("====================\n"); + printMaze(maze); + printStack(); + + result = search_maze(); + + printf("====================\n"); + if (result == true) { + printf("Success!!\n"); + } + else { + printf("Fail!!\n"); + } + printf("====================\n"); + printMaze(maze); + printStack(); + + getchar(); + return 0; +} \ No newline at end of file diff --git a/2-1/Data Structure/201721083_KTS_W6_exercise1.c b/2-1/Data Structure/201721083_KTS_W6_exercise1.c new file mode 100644 index 0000000000000000000000000000000000000000..c19fe6d7be428b6a6caaaf6d004ae74699790204 --- /dev/null +++ b/2-1/Data Structure/201721083_KTS_W6_exercise1.c @@ -0,0 +1,78 @@ +#include <stdio.h> +#include <stdlib.h> +#include<string.h> + +typedef int element; + +typedef struct stackNode { + element data; + struct stackNode* link; +}stackNode; + +stackNode* top; + +int isEmpty() { + if (top == NULL) return 1; + else return 0; +} + +void push(element item) { + stackNode* temp = (stackNode*)malloc(sizeof(stackNode)); + temp->data = item; + temp->link = top; + top = temp; +} + +element pop() { + element item; + stackNode* temp = top; + if (top == NULL) { + printf("\n\n Stack is empty !\n"); + return 0; + } + else { + item = temp->data; + top = temp->link; + free(temp); + return item; + } +} + +void printStack() { + stackNode* p = top; + printf("\n STACK ["); + while (p) { + printf("%d", p->data); + p = p->link; + } + printf("]"); +} + +void main(void) { + element item; + top = NULL; + printf("\n** Linked Stack **\n"); + printStack(); + push(4); printStack(); + push(3); printStack(); + push(1); printStack(); + push(7); printStack(); + push(5); printStack(); + + item = pop(); + printStack(); + printf("\t pop => %d", item); + item = pop(); + printStack(); + printf("\t pop => %d", item); + item = pop(); + printStack(); + printf("\t pop => %d", item); + item = pop(); + printStack(); + printf("\t pop => %d", item); + item = pop(); + printStack(); + printf("\t pop => %d", item); + getchar(); +} \ No newline at end of file diff --git a/2-1/Data Structure/201721083_KTS_W6_exercise2.c b/2-1/Data Structure/201721083_KTS_W6_exercise2.c new file mode 100644 index 0000000000000000000000000000000000000000..ae39038777c7b2a5ab5ffa51a63dad0eeac5afcc --- /dev/null +++ b/2-1/Data Structure/201721083_KTS_W6_exercise2.c @@ -0,0 +1,88 @@ +#include <stdio.h> +#include<malloc.h> + +typedef char element; +typedef struct QNode { + element data; + struct QNode* link; +}QNode; + +typedef struct { + QNode* front, * rear; +}LQueueType; + +LQueueType *createLinkedQueue(){ + LQueueType* LQ; + LQ = (LQueueType*)malloc(sizeof(LQueueType)); + LQ->front = NULL; + LQ->rear = NULL; + return LQ; +} + +int isEmpty(LQueueType* LQ) { + if (LQ->front == NULL) { + printf("Linked Queue is empty!"); + return 1; + } + else return 0; +} + +void enQueue(LQueueType* LQ, element item) { + QNode* newNode = (QNode*)malloc(sizeof(QNode)); + newNode->data = item; + newNode->link = NULL; + + if (LQ->front == NULL) { + LQ->front = newNode; + LQ->rear = newNode; + } + else { + LQ->rear ->link = newNode; + LQ->rear = newNode; + } +} + +element deQueue(LQueueType* LQ) { + QNode* old = LQ->front; + element item; + if (isEmpty(LQ)) return 0; + else { + item = old->data; + LQ->front = old->link; + free(old); + return item; + } +} + +void printLQ(LQueueType* LQ) { + QNode* temp = LQ->front; + printf("Linked Queue: ["); + while (temp) { + printf("%3c", temp->data); + temp = temp->link; + } + printf("]"); +} + +void main(void) { + LQueueType* LQ = createLinkedQueue(); + element data; + + printf("\n***** Queue *****\n"); + printf("\n enqueue C >>>"); enQueue(LQ, 'C'); printLQ(LQ); + printf("\n enqueue A >>>"); enQueue(LQ, 'A'); printLQ(LQ); + printf("\n enqueue B >>>"); enQueue(LQ, 'B'); printLQ(LQ); + printf("\n enqueue D >>>"); enQueue(LQ, 'D'); printLQ(LQ); + printf("\n enqueue E >>>"); enQueue(LQ, 'E'); printLQ(LQ); + + printf("\n dequeue >>>"); data = deQueue(LQ); printLQ(LQ); + printf("\tdequeued data: %c", data); + printf("\n dequeue >>>"); data = deQueue(LQ); printLQ(LQ); + printf("\tdequeued data: %c", data); + printf("\n dequeue >>>"); data = deQueue(LQ); printLQ(LQ); + printf("\tdequeued data: %c", data); + printf("\n dequeue >>>"); data = deQueue(LQ); printLQ(LQ); + printf("\tdequeued data: %c", data); + printf("\n dequeue >>>"); data = deQueue(LQ); printLQ(LQ); + printf("\tdequeued data: %c", data); +} \ No newline at end of file diff --git a/2-1/Data Structure/201721083_KTS_W7_exercise1.c b/2-1/Data Structure/201721083_KTS_W7_exercise1.c new file mode 100644 index 0000000000000000000000000000000000000000..2809dbfa2816dbac65c77112565ef88c7fc05548 --- /dev/null +++ b/2-1/Data Structure/201721083_KTS_W7_exercise1.c @@ -0,0 +1,112 @@ +// +// main.m +// exercise1 +// +// Created by 김태석 on 2020/04/27. +// Copyright © 2020 김태석. All rights reserved. +// + +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +typedef struct ListNode{ + struct ListNode* llink; + char data[4]; + struct ListNode* rlink; +}listNode; + +typedef struct { + listNode* head; +}linkedList_h; + +linkedList_h* createLinkedList_h(void){ + linkedList_h* DL; + DL = (linkedList_h*)malloc(sizeof(linkedList_h)); + DL->head = NULL; + return DL; +} +void printList(linkedList_h* DL){ + listNode* p; + printf("DL = ("); + p=DL->head; + while(p !=NULL){ + printf("%s", p->data); + p = p->rlink; + if(p!=NULL) printf(","); + } + printf(")\n"); +} + +void insertNode(linkedList_h*DL, listNode *pre, const char*x){ + listNode* newNode; + newNode =(listNode*)malloc(sizeof(listNode)); + strcpy(newNode->data,x); + if(DL->head==NULL){ + newNode->rlink=NULL; + newNode->llink = NULL; + DL->head = newNode; + } + else{ + newNode->rlink=pre->rlink; + pre->rlink=newNode; + newNode->llink=pre; + if(newNode->rlink != NULL){ + newNode->rlink->llink=newNode; + } + } +} + +void deleteNode(linkedList_h*DL, listNode* old){ + if(DL->head == NULL) return; + else if(old==NULL) return; + else{ + old->llink->rlink = old->rlink; + old->rlink->llink = old->llink; + free(old); + } +} +listNode* searchNode(linkedList_h*DL, const char* x){ + listNode *temp; + temp=DL->head; + while(temp!=NULL){ + if(strcmp(temp->data,x)==0) return temp; + else temp =temp->rlink; + } + return temp; +} + +int main(){ + linkedList_h*DL; + listNode*p; + DL = createLinkedList_h(); + printf("(1) create a doubly linked list. \n"); + printList(DL); + getchar(); + + printf("(2) Insert A. \n"); + insertNode(DL,NULL,"A"); + printList(DL); + getchar(); + + printf("(3) Insert B after A. \n"); + p = searchNode(DL,"A"); + insertNode(DL,p,"B"); + printList(DL); + getchar(); + + printf("(4) Insert C after B. \n"); + p=searchNode(DL,"B"); + insertNode(DL,p,"C"); + printList(DL); + getchar(); + + printf("(5) Delete B.\n"); + p=searchNode(DL,"B"); + deleteNode(DL,p); + printList(DL); + getchar(); + + return 0; +} diff --git a/2-1/Data Structure/201721083_KTS_W7_exercise2.c b/2-1/Data Structure/201721083_KTS_W7_exercise2.c new file mode 100644 index 0000000000000000000000000000000000000000..6db3517ee4716631760cc5d83913ea46197e666d --- /dev/null +++ b/2-1/Data Structure/201721083_KTS_W7_exercise2.c @@ -0,0 +1,76 @@ +// +// main.c +// exercise2 +// +// Created by 김태석 on 2020/04/27. +// Copyright © 2020 김태석. All rights reserved. +// + +#include <stdio.h> +#include <stdbool.h> +#include <stdlib.h> + +#define MAX_SIZE 100 +#define MALLOC(p,s) (p) = (nodePointer) malloc((s)); + +typedef struct node *nodePointer; +typedef struct node{ + int data; + nodePointer link; +}; + +int main(){ + bool out[MAX_SIZE]; + nodePointer seq[MAX_SIZE]; + nodePointer x,y,top; + int i,j,n; + + printf("Enter the number of polygons(<=%d)",MAX_SIZE); + scanf("%d", &n); + for(i=0; i<n;i++){ + out[i] = true; + seq[i] = NULL; + } + printf("Enter a pair of numbers(-1 -1 to quit): "); + while(1){ + scanf("%d %d", &i, &j); + if(i<0||j<0){ + break; + } + MALLOC(x,sizeof(x)); + x->data = j; x->link = seq[i]; seq[i]=x; + MALLOC(x,sizeof(x)); + x->data = i; x->link = seq[j]; seq[j]=x; + printf("Enter a pair of numbers(-1 -1 to quit): "); + }; + for(i=0;i<n;i++){ + if(out[i]==true){ + printf("\n New class : %5d", i); + out[i]=false; + x=seq[i]; + top = NULL; + + for(;;){ + while(x){ + j=x->data; + if(out[j]==true){ + printf("%5d",j); + out[j]=false; + y=x->link; + x->link=top; + top=x; + x=y; + } + else{ + x=x->link; + } + } + if(!top) break; + x=seq[top->data]; + top=top->link; + } + } + } + getchar(); + printf("\n\nEnter 0 to end"); +} diff --git a/2-1/Data Structure/201721083_KTS_W7_exercise3.c b/2-1/Data Structure/201721083_KTS_W7_exercise3.c new file mode 100644 index 0000000000000000000000000000000000000000..a6d413ed492997eea60974e5424e1c7c141fc37f --- /dev/null +++ b/2-1/Data Structure/201721083_KTS_W7_exercise3.c @@ -0,0 +1,72 @@ +// +// main.m +// exercise3 +// +// Created by 김태석 on 2020/04/27. +// Copyright © 2020 김태석. All rights reserved. +// + +#include<stdbool.h> +#include<stdio.h> +#include <stdlib.h> +#include <memory.h> + +typedef struct treeNode{ + char data; + struct treeNode *left; + struct treeNode *right; +} treeNode; +treeNode* makeNode(char data, treeNode* leftNode, treeNode* rightNode){ + treeNode* root =(treeNode*)malloc(sizeof(treeNode)); + root->data = data; + root->left = leftNode; + root ->right = rightNode; + return root; +} +void Preorder(treeNode* root){ + if(root != NULL){ + printf("[%c]", root->data); + Preorder(root->left); + Preorder(root->right); + } +} +void inorder(treeNode * root){ + if(root!=NULL){ + inorder(root->left); + printf("[%c]", root->data); + inorder(root->right); + } +} + +void Postorder(treeNode* root){ + if(root!= NULL){ + Postorder(root->left); + Postorder(root->right); + printf("[%c]", root->data); + } +} + +int main(){ + treeNode* n7 = makeNode('D',NULL,NULL); + treeNode* n6 = makeNode('C',NULL,NULL); + treeNode* n5 = makeNode('B',NULL,NULL); + treeNode* n4 = makeNode('A',NULL,NULL); + treeNode* n3 = makeNode('/',n6,n7); + treeNode* n2 = makeNode('*',n4,n5); + treeNode* n1 = makeNode('-',n2,n3); + + printf("Preorder : "); + Preorder(n1); + printf("\n"); + + printf("inorder : "); + inorder(n1); + printf("\n"); + + printf("postorder : "); + Postorder(n1); + printf("\n"); + + getchar(); + return 0; +} diff --git a/2-1/Data Structure/201721083_KTS_W8_exercise.c b/2-1/Data Structure/201721083_KTS_W8_exercise.c new file mode 100644 index 0000000000000000000000000000000000000000..cac2aba288ce58d5f8a9a3b569f45ddad10fa7da --- /dev/null +++ b/2-1/Data Structure/201721083_KTS_W8_exercise.c @@ -0,0 +1,89 @@ +#include <stdio.h> +#include <stdlib.h> +#include <memory.h> + +typedef struct treeNode{ + char data; + struct treeNode *left; + struct treeNode *right; + int isThreadRight; +}treeNode; + +treeNode* makeNode(char data, treeNode* leftNode,treeNode* rightNode,int isThreadRight){ + treeNode*node=(treeNode*)malloc(sizeof(treeNode)); + node->data = data; + node->left=leftNode; + node->right = rightNode; + node->isThreadRight = isThreadRight; + return node; +} + +treeNode* findThreadPredecessor(treeNode*p){ + treeNode *q = p->left; + if(q->isThreadRight ==1) return q; + if(q==NULL) return q; + while(q->right!=NULL) q= q->right; + return q; +} + +void*insertLeft(treeNode*p,treeNode *r){ + treeNode* temp; + r->left=p->left; + r->right=p; + r->isThreadRight = 1; + if(r->left !=NULL){ + temp = findThreadPredecessor(r); + temp->right = r; + temp->isThreadRight = 1; + } + return NULL; +} + +void*insertRight(treeNode*p,treeNode*r){ + treeNode* temp; + r->right = p->right; + r->isThreadRight=0; + p->right = r; + return NULL; +} +treeNode *findThreadSuccessor(treeNode*p){ + treeNode *q = p->right; + if(p->isThreadRight==1) return q; + if(q==NULL) return q; + while(q->left != NULL)q=q->left; + return q; +} + +void threadInorder(treeNode*root){ + treeNode *q; + q=root; + while(q->left) q=q->left; + do{ + printf("%3c",q->data); + q=findThreadSuccessor(q); + }while(q); +} + + +int main(){ + treeNode*n7=makeNode('G',NULL,NULL,0); + treeNode*n6=makeNode('f',NULL,NULL,1); + treeNode*n5=makeNode('E',NULL,NULL,1); + treeNode*n4=makeNode('D',NULL,NULL,1); + treeNode*n3=makeNode('C',n6,n7,0); + treeNode*n2=makeNode('B',n4,n5,0); + treeNode*n1=makeNode('A',n2,n3,0); + + n4->right=n2; + n5->right=n1; + n6->right=n3; + printf("\n Inorder of a threaded binary tree : \n"); + threadInorder(n1); + treeNode* r1 = makeNode('H',NULL,NULL,1); + insertLeft(n2,r1); + treeNode* r2=makeNode('I',NULL,NULL,1); + insertRight(n3,r2); + printf("\n Inorder of a threaded binary tree : \n"); + threadInorder(n1); + getchar(); +} diff --git a/2-1/Data Structure/201721083_KTS_W9_exercise1.c b/2-1/Data Structure/201721083_KTS_W9_exercise1.c new file mode 100644 index 0000000000000000000000000000000000000000..5ab4666314b12c8ab85937376bb9ebf31f69ab50 --- /dev/null +++ b/2-1/Data Structure/201721083_KTS_W9_exercise1.c @@ -0,0 +1,80 @@ +#include <stdio.h> +#include <stdlib.h> +#define MAX_VERTEX 30 + +typedef struct graphType{ + int n; + int adjMatrix[MAX_VERTEX][MAX_VERTEX]; +}graphType; + +void createGraph(graphType* g){ + int i, j; + g->n = 0; + for(i=0;i<MAX_VERTEX;i++){ + for(j=0;j<MAX_VERTEX; j++) + g->adjMatrix[i][j]=0; + } +} + +void insertVertex(graphType* g, int v){ + if(((g->n)+1)>MAX_VERTEX){ + printf("\n Graph Full!"); + return; + } + g->n++; +} + +void insertEdge(graphType* g, int u, int v){ + if(u>=g->n|v>=g->n){ + printf("no vertex existed"); + return; + } + g->adjMatrix[u][v]=1; +} + +void print_adjMatrix(graphType* g){ + int i, j; + for(i=0; i<(g->n); i++){ + printf("\n\t\t"); + for(j=0;j<(g->n); j++) + printf("%2d", g->adjMatrix[i][j]); + } +} + +int main(){ + int i; + graphType *G1, *G2; + G1=(graphType*)malloc(sizeof(graphType)); + G2=(graphType*)malloc(sizeof(graphType)); + createGraph(G1); + createGraph(G2); + for(i=0; i<4;i++) + insertVertex(G1,i); + insertEdge(G1,0,1); + insertEdge(G1,0,3); + insertEdge(G1,1,0); + insertEdge(G1,1,2); + insertEdge(G1,1,3); + insertEdge(G1,2,1); + insertEdge(G1,2,3); + insertEdge(G1,3,0); + insertEdge(G1,3,1); + insertEdge(G1,3,2); + printf("\n G1 : "); + print_adjMatrix(G1); + + for(i=0;i<3;i++) + insertVertex(G2,i); + insertEdge(G2,0,1); + insertEdge(G2,0,2); + insertEdge(G2,1,0); + insertEdge(G2,1,2); + insertEdge(G2,2,0); + insertEdge(G2,2,1); + + printf("\n G2 : "); + print_adjMatrix(G2); + + getchar(); +} + diff --git a/2-1/Data Structure/201721083_KTS_W9_exercise2.c b/2-1/Data Structure/201721083_KTS_W9_exercise2.c new file mode 100644 index 0000000000000000000000000000000000000000..f02136a56ebd07f3fbbe5623a9fb5394360ca9a5 --- /dev/null +++ b/2-1/Data Structure/201721083_KTS_W9_exercise2.c @@ -0,0 +1,91 @@ +#include <stdio.h> +#include <stdlib.h> +#define MAX_VERTEX 30 + +typedef struct graphNode{ + int vertex; + struct graphNode* link; +}graphNode; + +typedef struct graphType{ + int n; + graphNode* adjList_H[MAX_VERTEX]; +}graphType; + +void createGraph(graphType* g){ + int v; + g->n=0; + for(v=0; v<MAX_VERTEX; v++) + g->adjList_H[v]=NULL; +} + +void insertVertex(graphType* g, int v){ + if(((g->n)+1)>MAX_VERTEX){ + printf("\n Graph Full!"); + return; + } + g->n++; +} +void insertEdge(graphType* g, int u, int v){ + graphNode* node; + if(u>=g->n|v>=g->n){ + printf("no vertex existed"); + return; + } + node = (graphNode*)malloc(sizeof(graphNode)); + node->vertex = v; + node->link = g->adjList_H[u]; + g->adjList_H[u] =node; +} + +void print_adjList(graphType* g){ + int i; + graphNode* p; + for(i=0;i<g->n;i++){ + printf("\n\t\t %c", i+65); + p=g->adjList_H[i]; + while(p){ + printf("->%c", p->vertex+65); + p=p->link; + } + } +} + +int main(){ + int i; + graphType *G1, *G2; + G1=(graphType*)malloc(sizeof(graphType)); + G2=(graphType*)malloc(sizeof(graphType)); + createGraph(G1); + createGraph(G2); + + for(i=0;i<4;i++) + insertVertex(G1,i); + insertEdge(G1,0,3); + insertEdge(G1,0,1); + insertEdge(G1,1,3); + insertEdge(G1,1,2); + insertEdge(G1,1,0); + insertEdge(G1,2,3); + insertEdge(G1,2,1); + insertEdge(G1,3,2); + insertEdge(G1,3,1); + insertEdge(G1,3,0); + printf("\n G1 : "); + print_adjList(G1); + + for(i=0;i<3;i++) + insertVertex(G2,i); + + insertEdge(G2,0,2); + insertEdge(G2,0,1); + insertEdge(G2,1,2); + insertEdge(G2,1,0); + insertEdge(G2,2,1); + insertEdge(G2,2,0); + printf("\n G2 : "); + print_adjList(G2); + getchar(); + return 0; + +} diff --git a/2-1/OOP/.DS_Store b/2-1/OOP/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..6a8ff5c86cdf9e05d266730e39a4e436533eb9c8 Binary files /dev/null and b/2-1/OOP/.DS_Store differ diff --git a/2-1/OOP/OOP_HW1_201721083/OOP_HW1_201721083.pdf b/2-1/OOP/OOP_HW1_201721083/OOP_HW1_201721083.pdf new file mode 100644 index 0000000000000000000000000000000000000000..61b4aee147141011df6803220206e3f28d0920fb Binary files /dev/null and b/2-1/OOP/OOP_HW1_201721083/OOP_HW1_201721083.pdf differ diff --git a/2-1/OOP/OOP_HW1_201721083/test1.jar b/2-1/OOP/OOP_HW1_201721083/test1.jar new file mode 100644 index 0000000000000000000000000000000000000000..d5d2fec7d4d44163d3291c2be61e55ec172076ad Binary files /dev/null and b/2-1/OOP/OOP_HW1_201721083/test1.jar differ diff --git a/2-1/OOP/OOP_HW1_201721083/test2.jar b/2-1/OOP/OOP_HW1_201721083/test2.jar new file mode 100644 index 0000000000000000000000000000000000000000..3b6e66ae46cce6ca698837391a85b0e222815ebf Binary files /dev/null and b/2-1/OOP/OOP_HW1_201721083/test2.jar differ diff --git a/2-1/OOP/OOP_HW1_201721083/test3.jar b/2-1/OOP/OOP_HW1_201721083/test3.jar new file mode 100644 index 0000000000000000000000000000000000000000..9437ae352487ca3e8d3da8fa1a219d707a1e5dda Binary files /dev/null and b/2-1/OOP/OOP_HW1_201721083/test3.jar differ diff --git a/2-1/OOP/OOP_HW1_201721083/test4.jar b/2-1/OOP/OOP_HW1_201721083/test4.jar new file mode 100644 index 0000000000000000000000000000000000000000..9c468ad0f55653ec16c992b3d8b3723584bee81b Binary files /dev/null and b/2-1/OOP/OOP_HW1_201721083/test4.jar differ diff --git a/2-1/OOP/OOP_HW2_201721083/.DS_Store b/2-1/OOP/OOP_HW2_201721083/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..dba3d037d9d7f53ba55a1116032706dd37c2051f Binary files /dev/null and b/2-1/OOP/OOP_HW2_201721083/.DS_Store differ diff --git a/2-1/OOP/OOP_HW2_201721083/OOP_HW2_201721083.pdf b/2-1/OOP/OOP_HW2_201721083/OOP_HW2_201721083.pdf new file mode 100644 index 0000000000000000000000000000000000000000..8d8cd66593e4261c35b3fcc99924ecbea26e8887 Binary files /dev/null and b/2-1/OOP/OOP_HW2_201721083/OOP_HW2_201721083.pdf differ diff --git a/2-1/OOP/OOP_HW2_201721083/accounts.jar b/2-1/OOP/OOP_HW2_201721083/accounts.jar new file mode 100644 index 0000000000000000000000000000000000000000..14e91d9d0b3a2e777e7fc7fb50f96f4fc9533ce2 Binary files /dev/null and b/2-1/OOP/OOP_HW2_201721083/accounts.jar differ diff --git a/2-1/OOP/OOP_HW3_201721083/OOP_HW3_201721083.pdf b/2-1/OOP/OOP_HW3_201721083/OOP_HW3_201721083.pdf new file mode 100644 index 0000000000000000000000000000000000000000..3279ecd13261c4644aa3682dafd2660af2622777 Binary files /dev/null and b/2-1/OOP/OOP_HW3_201721083/OOP_HW3_201721083.pdf differ diff --git a/2-1/OOP/OOP_HW3_201721083/sheet.jar b/2-1/OOP/OOP_HW3_201721083/sheet.jar new file mode 100644 index 0000000000000000000000000000000000000000..17dee1028340474b118af7c95f35a541c4ff6255 Binary files /dev/null and b/2-1/OOP/OOP_HW3_201721083/sheet.jar differ diff --git a/2-1/OOP/lab_10_201721083.pdf b/2-1/OOP/lab_10_201721083.pdf new file mode 100644 index 0000000000000000000000000000000000000000..c2e42d1d581a7eeb61385fce6c0af2ecc9cc7ff2 Binary files /dev/null and b/2-1/OOP/lab_10_201721083.pdf differ diff --git a/2-1/OOP/lab_11_201721083.pdf b/2-1/OOP/lab_11_201721083.pdf new file mode 100644 index 0000000000000000000000000000000000000000..2b08baab3015bb4625f33118d8cdcf8778bce27d Binary files /dev/null and b/2-1/OOP/lab_11_201721083.pdf differ diff --git a/2-1/OOP/lab_12_201721083.pdf b/2-1/OOP/lab_12_201721083.pdf new file mode 100644 index 0000000000000000000000000000000000000000..87faa7a529f85728cf9708eb76f399016c0a2356 Binary files /dev/null and b/2-1/OOP/lab_12_201721083.pdf differ diff --git a/2-1/OOP/lab_4_201721083.pdf b/2-1/OOP/lab_4_201721083.pdf new file mode 100644 index 0000000000000000000000000000000000000000..e844ae758fb77008e66771c847afddf1ce367e2c Binary files /dev/null and b/2-1/OOP/lab_4_201721083.pdf differ diff --git a/2-1/OOP/lab_6_201721083.pdf b/2-1/OOP/lab_6_201721083.pdf new file mode 100644 index 0000000000000000000000000000000000000000..018e43b31f389ee8c1bbbdbe21b5f5327bf7aca7 Binary files /dev/null and b/2-1/OOP/lab_6_201721083.pdf differ diff --git a/2-1/OOP/lab_7_201721083.pdf b/2-1/OOP/lab_7_201721083.pdf new file mode 100644 index 0000000000000000000000000000000000000000..afd02f221ae7ffb321414dfe83d82a18b7adcf31 Binary files /dev/null and b/2-1/OOP/lab_7_201721083.pdf differ diff --git a/2-1/OOP/lab_8_201721083.pdf b/2-1/OOP/lab_8_201721083.pdf new file mode 100644 index 0000000000000000000000000000000000000000..7312290badd777fcbb4a5a776d2491d5789a0a09 Binary files /dev/null and b/2-1/OOP/lab_8_201721083.pdf differ diff --git a/2-1/OOP/lab_9_201721083.pdf b/2-1/OOP/lab_9_201721083.pdf new file mode 100644 index 0000000000000000000000000000000000000000..d463a20c23d73d2457ce6015676a21966b42aebf Binary files /dev/null and b/2-1/OOP/lab_9_201721083.pdf differ diff --git a/2-1/OOP/src_318_1_201721083/src_318_1_201721083.docx b/2-1/OOP/src_318_1_201721083/src_318_1_201721083.docx new file mode 100644 index 0000000000000000000000000000000000000000..7afec6076f8be6cdcd7d82293888424030225152 Binary files /dev/null and b/2-1/OOP/src_318_1_201721083/src_318_1_201721083.docx differ diff --git a/2-1/OOP/src_318_1_201721083/src_318_1_201721083.txt b/2-1/OOP/src_318_1_201721083/src_318_1_201721083.txt new file mode 100644 index 0000000000000000000000000000000000000000..10a55d2af50521c568b79a559516f80690a70023 --- /dev/null +++ b/2-1/OOP/src_318_1_201721083/src_318_1_201721083.txt @@ -0,0 +1,161 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <time.h> + +typedef struct Card { + int suit; + int value; +}CARD; + +typedef struct Player { + char name[20]; + int balance; + CARD hand; +}PLAYER; + +PLAYER players[2]; +CARD deck[52]; + +void initialize(PLAYER *players) { + printf("INPUT PLAYER1'S NAME\n"); + scanf("%s", players[0].name); + printf("INPUT PLAYER2'S NAME\n"); + scanf("%s", players[1].name); + printf("INPUT PLAYER1'S BALANCE\n"); + scanf("%d", &players[0].balance); + printf("INPUT PLAYER2'S BALANCE\n"); + scanf("%d", &players[1].balance); +} +void carddeck(CARD* deck) { + int i = 0; + for (int a = 1; a <= 4; a++) { + for (int b = 2; b <= 14; b++) { + deck[i].suit = a; + deck[i].value = b; + i++; + } + } +} + +void swap(CARD* A, CARD* B) { + CARD temp; + temp = *A; + *A = *B; + *B = temp; +} + +void shuffle(CARD* deck) { + for (int i = 0; i <= 51; i++) { + int rand1 = rand() % 52; + int rand2 = rand() % 52; + swap(&deck[rand1], &deck[rand2]); + } +} +void Myprint(PLAYER* players, int bet_money,int win) { + char* shape1; + char* shape2; + char* shape3; + char* shape4; + if (players[0].hand.suit == 4) shape1 = "Spade"; + else if (players[0].hand.suit == 3) shape1 = "Diamond"; + else if (players[0].hand.suit == 2) shape1 = "Heart"; + else if (players[0].hand.suit == 1) shape1 = "Club"; + + if (players[1].hand.suit == 4) shape2 = "Spade"; + else if (players[1].hand.suit == 3) shape2 = "Diamond"; + else if (players[1].hand.suit == 2) shape2 = "Heart"; + else if (players[1].hand.suit == 1) shape2 = "Club"; + + if (players[0].hand.value == 1) shape3 = "1"; + else if (players[0].hand.value == 2) shape3 = "2"; + else if (players[0].hand.value == 3) shape3 = "3"; + else if (players[0].hand.value == 4) shape3 = "4"; + else if (players[0].hand.value == 5) shape3 = "5"; + else if (players[0].hand.value == 6) shape3 = "6"; + else if (players[0].hand.value == 7) shape3 = "7"; + else if (players[0].hand.value == 8) shape3 = "8"; + else if (players[0].hand.value == 9) shape3 = "9"; + else if (players[0].hand.value == 10) shape3 = "10"; + else if (players[0].hand.value == 11) shape3 = "Jack"; + else if (players[0].hand.value == 12) shape3 = "Queen"; + else if (players[0].hand.value == 13) shape3 = "King"; + else if (players[0].hand.value == 14) shape3 = "Ace"; + + if (players[1].hand.value == 1) shape4 = "1"; + else if (players[1].hand.value == 2) shape4 = "2"; + else if (players[1].hand.value == 3) shape4 = "3"; + else if (players[1].hand.value == 4) shape4 = "4"; + else if (players[1].hand.value == 5) shape4 = "5"; + else if (players[1].hand.value == 6) shape4 = "6"; + else if (players[1].hand.value == 7) shape4 = "7"; + else if (players[1].hand.value == 8) shape4 = "8"; + else if (players[1].hand.value == 9) shape4 = "9"; + else if (players[1].hand.value == 10) shape4 = "10"; + else if (players[1].hand.value == 11) shape4 = "Jack"; + else if (players[1].hand.value == 12) shape4 = "Queen"; + else if (players[1].hand.value == 13) shape4 = "King"; + else if (players[1].hand.value == 14) shape4 = "Ace"; + + printf("%s : %s of %s, %s : %s of %s --->%s won %d\n", players[0].name, shape1, shape3, players[1].name, shape2, shape4, players[win].name, bet_money); +} +void playGames(PLAYER* players) { + CARD deck[52]; + carddeck(deck); + int End = 0; + int bet_money; + int win = 0; + printf("plz input betting amount \n"); + scanf("%d", &bet_money); + while (!End) { + shuffle(deck); + for (int i = 0; i < 52; i = i + 2) { + if (players[0].balance < bet_money || players[1].balance < bet_money) { + End = 1; + break; + } + players[0].hand = deck[i]; + players[1].hand = deck[i + 1]; + if (players[0].hand.value > players[1].hand.value) { + players[0].balance += bet_money; + players[1].balance -= bet_money; + win = 0; + } + else if (players[1].hand.value > players[0].hand.value) { + players[0].balance -= bet_money; + players[1].balance += bet_money; + win = 1; + } + else { + if (players[0].hand.suit > players[1].hand.suit) { + players[0].balance += bet_money; + players[1].balance -= bet_money; + win = 0; + } + else { + players[0].balance -= bet_money; + players[1].balance += bet_money; + win = 1; + } + } + Myprint(players, bet_money, win); + } + if (players[0].balance < players[1].balance) { + win = 1; + printf("%s's balance : %d, %s's balance : %d\nSo winner is %s\n", players[0].name, players[0].balance, players[1].name, players[1].balance, players[win].name); + } + else if (players[0].balance > players[1].balance) { + win = 0; + printf("%s's balance : %d, %s's balance : %d\nSo winner is %s\n", players[0].name, players[0].balance, players[1].name, players[1].balance, players[win].name); + } + else { + printf("%s's balance : %d %s's balance : %d\n Draw\n", players[0].name, players[0].balance, players[1].name, players[1].balance); + } + } +} +void main(){ + srand((int)time(NULL)); + initialize(players); + printf("PLAYER1's name : %s\nPLAYER2's name : %s\n", players[0].name, players[1].name); + playGames(players); +} diff --git a/2-1/OOP/src_318_2_201721083/src_318_2_201721083.pdf b/2-1/OOP/src_318_2_201721083/src_318_2_201721083.pdf new file mode 100644 index 0000000000000000000000000000000000000000..ea884d84f43f94f6f92a7b3ed4c1e93b8e0cff0b Binary files /dev/null and b/2-1/OOP/src_318_2_201721083/src_318_2_201721083.pdf differ diff --git a/2-1/OOP/src_318_2_201721083/src_318_2_201721083.txt b/2-1/OOP/src_318_2_201721083/src_318_2_201721083.txt new file mode 100644 index 0000000000000000000000000000000000000000..dcdeebc48d3b1fb83191f1eb685e475fb85d0cab --- /dev/null +++ b/2-1/OOP/src_318_2_201721083/src_318_2_201721083.txt @@ -0,0 +1,157 @@ +import java.util.Scanner; +import java.util.Random; + +class Card { + public int suit; + public int value; +} + +class Player { + public String name; + public double balance; + public Card hand; +} + +class CardGame{ + static Card[] deck = new Card[52]; + static Player[] player = new Player[2]; + public static void main(String[] args) { + int win = 0; + int End = 0; + int bet_money; + initialize(); + DECK(); + Scanner in = new Scanner(System.in); + while (End < 1) { + shuffle(); + System.out.print("Enter the bet money\n"); + bet_money = in.nextInt(); + for (int i = 0; i < 52; i = i + 2) { + if (player[0].balance < bet_money || player[1].balance < bet_money) { + End = 1; + break; + } + player[0].hand = deck[i]; + player[1].hand = deck[i + 1]; + if (player[0].hand.value > player[1].hand.value) { + player[0].balance += bet_money; + player[1].balance -= bet_money; + win = 0; + } + else if (player[1].hand.value > player[0].hand.value) { + player[0].balance -= bet_money; + player[1].balance += bet_money; + win = 1; + } + else { + if (player[0].hand.suit > player[1].hand.suit) { + player[0].balance += bet_money; + player[1].balance -= bet_money; + win = 0; + } + else { + player[0].balance -= bet_money; + player[1].balance += bet_money; + win = 1; + } + } + My_Print(bet_money,win); + } + if(player[0].balance<player[1].balance) { + win=1; + System.out.print(player[0].name+"'s balance : "+player[0].balance+", "+player[1].name+"'s balance : "+player[1].balance+"\nSo winner is "+player[win].name+"\n"); + } + else if (player[0].balance > player[1].balance) { + win=0; + System.out.print(player[0].name+"'s balance : "+player[0].balance+", "+player[1].name+"'s balance : "+player[1].balance+"\nSo winner is "+player[win].name+"\n"); + } + else { + System.out.print(player[0].name+"'s balance : "+player[0].balance+", "+player[1].name+"'s balance : "+player[1].balance+"\nSo Draw\n"); + } + } +} + + + public static void DECK() { + int i = 0; + for (int a = 1; a <= 4; a++) { + for (int b = 2; b <= 14; b++) { + deck[i]= new Card(); + deck[i].suit = a; + deck[i].value = b; + i++; + } + } + } + + public static void shuffle() { + for (int i = 0; i < 52; i++) { + int t = (int)(Math.random()*deck.length); + int j = (int)(Math.random()*deck.length); + Card temp = deck[t]; + deck[t] = deck[j]; + deck[j] = temp; + } + } + + public static void initialize() { + player[0] = new Player(); + player[1]= new Player(); + player[0].name = "TaeSeok"; + player[1].name = "DaHee"; + player[0].balance = 5000; + player[1].balance = 3000; + System.out.print(" PLAYER1's NAME : " + player[0].name); + System.out.print("\n PLAYER2's NAME : " + player[1].name); + System.out.print("\n PLAYER1's BALANCE : " + player[0].balance); + System.out.println("\n PLAYER2's BALANCE : " + player[1].balance); + } + + public static void My_Print(int bet_money,int win) { + String shape1 = new String(); + String shape2 = new String(); + String shape3 = new String(); + String shape4 = new String(); + if (player[0].hand.suit == 4) shape1 = "Spade"; + else if (player[0].hand.suit == 3) shape1 = "Diamond"; + else if (player[0].hand.suit == 2) shape1 = "Heart"; + else if (player[0].hand.suit == 1) shape1 = "Club"; + + if (player[1].hand.suit == 4) shape2 = "Spade"; + else if (player[1].hand.suit == 3) shape2 = "Diamond"; + else if (player[1].hand.suit == 2) shape2 = "Heart"; + else if (player[1].hand.suit == 1) shape2 = "Club"; + + if (player[0].hand.value == 1) shape3 = "1"; + else if (player[0].hand.value == 2) shape3 = "2"; + else if (player[0].hand.value == 3) shape3 = "3"; + else if (player[0].hand.value == 4) shape3 = "4"; + else if (player[0].hand.value == 5) shape3 = "5"; + else if (player[0].hand.value == 6) shape3 = "6"; + else if (player[0].hand.value == 7) shape3 = "7"; + else if (player[0].hand.value == 8) shape3 = "8"; + else if (player[0].hand.value == 9) shape3 = "9"; + else if (player[0].hand.value == 10) shape3 = "10"; + else if (player[0].hand.value == 11) shape3 = "Jack"; + else if (player[0].hand.value == 12) shape3 = "Queen"; + else if (player[0].hand.value == 13) shape3 = "King"; + else if (player[0].hand.value == 14) shape3 = "Ace"; + + if (player[1].hand.value == 1) shape4 = "1"; + else if (player[1].hand.value == 2) shape4 = "2"; + else if (player[1].hand.value == 3) shape4 = "3"; + else if (player[1].hand.value == 4) shape4 = "4"; + else if (player[1].hand.value == 5) shape4 = "5"; + else if (player[1].hand.value == 6) shape4 = "6"; + else if (player[1].hand.value == 7) shape4 = "7"; + else if (player[1].hand.value == 8) shape4 = "8"; + else if (player[1].hand.value == 9) shape4 = "9"; + else if (player[1].hand.value == 10) shape4 = "10"; + else if (player[1].hand.value == 11) shape4 = "Jack"; + else if (player[1].hand.value == 12) shape4 = "Queen"; + else if (player[1].hand.value == 13) shape4 = "King"; + else if (player[1].hand.value == 14) shape4 = "Ace"; + + System.out.print(player[0].name + " : " + shape1 + " of " + shape3 + ", " + player[1].name + " : " + shape2 + " of " + shape4 + "--->" + player[win].name + " won " + bet_money+"\n"); + } +} diff --git a/2-1/OOP/src_318_3_201721083/src_318_3_1_201721083.txt b/2-1/OOP/src_318_3_201721083/src_318_3_1_201721083.txt new file mode 100644 index 0000000000000000000000000000000000000000..1df918f422e4dcadbe626fed5e840f5ad30e3104 --- /dev/null +++ b/2-1/OOP/src_318_3_201721083/src_318_3_1_201721083.txt @@ -0,0 +1,36 @@ +import java.time.LocalDateTime; +public class ex1 { + public static void main(String[] args) { + //1-1 + LocalDateTime currentDateTime = LocalDateTime.now(); + int year = currentDateTime.getYear(); + int month = currentDateTime.getMonthValue(); + int date = currentDateTime.getDayOfMonth(); + int hour = currentDateTime.getHour(); + int minute = currentDateTime.getMinute(); + int second = currentDateTime.getSecond(); + System.out.println("Year : "+year); + System.out.println("Month: "+month); + System.out.println("Date: "+date); + System.out.println("Hour: "+hour); + System.out.println("Minute: "+minute); + System.out.println("Second: "+second); + //1-2 + LocalDateTime InputDate = LocalDateTime.of(2018,1,4,10,30,27); + int Input_year = InputDate.getYear(); + int Input_month = InputDate.getMonthValue(); + int Input_date = InputDate.getDayOfMonth(); + int Input_hour = InputDate.getHour(); + int Input_minute = InputDate.getMinute(); + int Input_second = InputDate.getSecond(); + System.out.println("\nYear : "+Input_year); + System.out.println("Month: "+Input_month); + System.out.println("Date: "+Input_date); + System.out.println("Hour: "+Input_hour); + System.out.println("Minute: "+Input_minute); + System.out.println("Second: "+Input_second); + //1-3 + System.out.println(currentDateTime.toString()); + System.out.println(InputDate.toString()); + } +} diff --git a/2-1/OOP/src_318_3_201721083/src_318_3_201721083.pdf b/2-1/OOP/src_318_3_201721083/src_318_3_201721083.pdf new file mode 100644 index 0000000000000000000000000000000000000000..7b655db4db874094b76d301f5bd90539acb5f052 Binary files /dev/null and b/2-1/OOP/src_318_3_201721083/src_318_3_201721083.pdf differ diff --git a/2-1/OOP/src_318_3_201721083/src_318_3_Memo_201721083.txt b/2-1/OOP/src_318_3_201721083/src_318_3_Memo_201721083.txt new file mode 100644 index 0000000000000000000000000000000000000000..0835a920bb98318deeabbf95f847d04dd884346e --- /dev/null +++ b/2-1/OOP/src_318_3_201721083/src_318_3_Memo_201721083.txt @@ -0,0 +1,44 @@ +package ex2; +import java.time.LocalDateTime; +import java.lang.StringBuilder; +import java.lang.String; +import java.util.Scanner; + +class Memo{ + public String Title; + public String MAIN; + public String O_MAIN; + public String Create_Time,Change_Time; + public StringBuilder sb ; + + public void memo(String t, String m) { + Title = t; + MAIN = m; + O_MAIN = m; + Create_Time = LocalDateTime.now().toString(); + Change_Time = Create_Time; + sb = new StringBuilder(MAIN); + } + public void CHANGE_TIME() { + Change_Time = LocalDateTime.now().toString(); + } + public String getName() { + return Title; + } + public String getMain() { + return MAIN; + } + public void Change_Main(int a, int b, String str) { + MAIN = sb.replace(a,b,str).toString(); + } + public void Append_Main(String str) { + MAIN = sb.append("\n"+str).toString(); + } + public void Insert_Main(int a, String str) { + MAIN = sb.insert(a,str).toString(); + } + public void Delete_Main(int a, int b) { + MAIN = sb.delete(a,b).toString(); + } + + } diff --git a/2-1/OOP/src_318_3_201721083/src_318_3_Memotest_201721083.txt b/2-1/OOP/src_318_3_201721083/src_318_3_Memotest_201721083.txt new file mode 100644 index 0000000000000000000000000000000000000000..c1e7b47605dc3dce435389d771507080a8e4c691 --- /dev/null +++ b/2-1/OOP/src_318_3_201721083/src_318_3_Memotest_201721083.txt @@ -0,0 +1,71 @@ +package ex2; +import java.time.LocalDateTime; +import java.lang.StringBuilder; +import java.lang.String; +import java.util.Scanner; +public class Memotest { + public static void main(String[] args) { + int End = 0; + int Choice,p1,p2; + String str; + Scanner in = new Scanner(System.in); + Memo MEMO = new Memo(); + MEMO.memo("Couple", "TaeSeok and DaHee are couple"); + System.out.print("Title is : "+MEMO.Title+"\nCurrent time is : "+MEMO.Create_Time+"\nModificated time is : "+MEMO.Change_Time+"\nMEMO is : "+MEMO.MAIN); + while(End<1) { + System.out.println("\nIf you wand to insert the memo, Please press 1"); + System.out.println("If you wand to replace the memo, Please press 2"); + System.out.println("If you wand to append the memo, Please press 3"); + System.out.println("If you wand to delete the memo, Please press 4"); + Choice = in.nextInt(); + if(Choice==1) { + System.out.print("Press insertion place\n"); + p1 = in.nextInt(); + System.out.print("Write your memo\n"); + str = in.nextLine(); + str = in.nextLine(); + System.out.print("\nBefore\n\nTitle is : "+MEMO.Title+"\nCurrent time is : "+MEMO.Create_Time+"\nModificated time is : "+MEMO.Change_Time+"\nMEMO is : "+MEMO.MAIN); + MEMO.Insert_Main(p1, str); + MEMO.CHANGE_TIME(); + System.out.print("\nAfter\n\nTitle is : "+MEMO.Title+"\nCurrent time is : "+MEMO.Create_Time+"\nModificated time is : "+MEMO.Change_Time+"\nMEMO is : "+MEMO.MAIN); + } + else if(Choice ==2) { + System.out.print("Press replacement place _ to _\n"); + p1 = in.nextInt(); + p2 = in.nextInt(); + System.out.print("Write your memo\n"); + str = in.nextLine(); + str = in.nextLine(); + System.out.print("\nBefore\n\nTitle is : "+MEMO.Title+"\nCurrent time is : "+MEMO.Create_Time+"\nModificated time is : "+MEMO.Change_Time+"\nMEMO is : "+MEMO.MAIN); + MEMO.Change_Main(p1, p2, str); + MEMO.CHANGE_TIME(); + System.out.print("\nAfter\n\nTitle is : "+MEMO.Title+"\nCurrent time is : "+MEMO.Create_Time+"\nModificated time is : "+MEMO.Change_Time+"\nMEMO is : "+MEMO.MAIN); + } + else if(Choice ==3) { + System.out.print("Add your memo\n"); + str = in.nextLine(); + str = in.nextLine(); + System.out.print("\nBefore\n\nTitle is : "+MEMO.Title+"\nCurrent time is : "+MEMO.Create_Time+"\nModificated time is : "+MEMO.Change_Time+"\nMEMO is : "+MEMO.MAIN); + MEMO.Append_Main(str); + MEMO.CHANGE_TIME(); + System.out.print("\nAfter\n\nTitle is : "+MEMO.Title+"\nCurrent time is : "+MEMO.Create_Time+"\nModificated time is : "+MEMO.Change_Time+"\nMEMO is : "+MEMO.MAIN); + } + else if(Choice ==4) { + System.out.print("Press deletion part _ to _\n"); + p1 = in.nextInt(); + p2 = in.nextInt(); + System.out.print("\nBefore\n\nTitle is : "+MEMO.Title+"\nCurrent time is : "+MEMO.Create_Time+"\nModificated time is : "+MEMO.Change_Time+"\nMEMO is : "+MEMO.MAIN); + MEMO.Delete_Main(p1, p2); + MEMO.CHANGE_TIME(); + System.out.print("\nAfter\n\nTitle is : "+MEMO.Title+"\nCurrent time is : "+MEMO.Create_Time+"\nModificated time is : "+MEMO.Change_Time+"\nMEMO is : "+MEMO.MAIN); + } + else { + System.out.print("Error!!!"); + break; + } + System.out.print("\nKeep going?\nYes : 0 , No : 1\n"); + End = in.nextInt(); + } + in.close(); + } +} diff --git a/2-1/OOP/src_318_5_201721083.pdf b/2-1/OOP/src_318_5_201721083.pdf new file mode 100644 index 0000000000000000000000000000000000000000..b40661278ad22f5e187da52d9ed56c8408969f26 Binary files /dev/null and b/2-1/OOP/src_318_5_201721083.pdf differ diff --git a/2-2/.DS_Store b/2-2/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..feaedc7b2f18cf76f56e4bf229c0ab38bdf53a14 Binary files /dev/null and b/2-2/.DS_Store differ diff --git a/2-2/Algorithm/Algorithm_201721083_KTS_Huffman.c b/2-2/Algorithm/Algorithm_201721083_KTS_Huffman.c new file mode 100644 index 0000000000000000000000000000000000000000..268168a273f842567c8d335b33f1f69e02a778d3 --- /dev/null +++ b/2-2/Algorithm/Algorithm_201721083_KTS_Huffman.c @@ -0,0 +1,470 @@ +//파일 경로설정 : Xcode의 Product->Scheme->Edit Scheme->Option->Working Directory 소스파일의 위치 +//인코딩 혹은 디코딩 할 파일 설정 :Xcode의 Product->Scheme->Edit Scheme->Arguments +//인코딩 : -encoding _____.txt +//디코딩 : -decoding _____.bin + + + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +#define BUF_SZ 100 +#define NUM_ASCII 256 + +struct node{ + char c; + int frequency; + struct node *left; + struct node *right; +}; + +struct node **heap; +int lastHeapIdx =0; +char codeBuf[100]; +int codeBufIdx = -1; + +int charFreq[NUM_ASCII]; +char *symCode[NUM_ASCII]; + +void Encoding (char *fName); +void Decoding (char *fName); +void showCharFrequency(void); +int countNonZeroCharacters(void); +void addToHeap(struct node *cur); +struct node* deleteFromHeap(void); +void traverse(struct node *cur,char c); +void just_traverse(struct node *cur,char c); + +int main(int argc, char* argv[]){ + if(strcmp(argv[1],"-encoding")==0){ + //huffman coing + printf("Start encoding\n"); + Encoding(argv[2]); + } + else if (strcmp(argv[1],"-decoding")==0){ + //decoding + printf("Start decoding\n"); + Decoding(argv[2]); + } + return 0; +} +//인코더 함수 +void Encoding(char *fName){ + FILE *fin=0; + char buf[BUF_SZ]; + + fin = fopen(fName,"rt"); + if(fin==0){ + printf("file is not exist\n"); + return; + } + + memset(charFreq, 0, NUM_ASCII*sizeof(int)); + while(fgets(buf, BUF_SZ, fin)!=0){ + int len = strlen(buf); + for(int i=0;i<len;i++){ + charFreq[(int)buf[i]]++; + } + } + + fclose(fin); + + int cnt = countNonZeroCharacters(); + heap = (struct node **)malloc((cnt+1)*sizeof(struct node *)); + memset(heap,0,(cnt+1)*sizeof(struct node *)); + + for (int i=0;i<NUM_ASCII;i++){ + if(charFreq[i]>0){ + struct node *cur = (struct node *)malloc(sizeof(struct node)); + cur->c = (char)i; + cur->frequency = charFreq[i]; + cur->left = cur->right =0; + addToHeap(cur); + } + } + + struct node *first =0; + struct node *second = 0; + + while (1){ + first = deleteFromHeap(); + second = deleteFromHeap(); + + if(second==0){ + printf("Huffman tree building ended\n"); + break; + } + + struct node *newOne =(struct node *)malloc(sizeof(struct node)); + newOne->c = 0; + newOne->frequency = first->frequency + second->frequency; + newOne->left = first; + newOne->right = second; + + addToHeap(newOne); + } + + memset(symCode, 0, sizeof(symCode)); + + traverse(first->left,'0'); + traverse(first->right,'1'); + int numOfSym = 0; + + for(int i=0;i<NUM_ASCII;i++){ + if(symCode[i]!=0){ + numOfSym++; + } + } + printf("Number of character is %d\n",numOfSym); + + char outputFileName[100]; + char *period = strchr(fName,(int)'.'); + strncpy(outputFileName, fName,(int)(period-fName)); + outputFileName[(int)(period-fName)]=0; + strcat(outputFileName,".bin"); + + printf("Output file name is %s\n",outputFileName); + + FILE *fout =0; + fout=fopen(outputFileName,"wb"); + if(fout !=0){ + fwrite(&numOfSym, sizeof(numOfSym),1,fout); + + char writeBuf[100]; + for(int i=0; i<NUM_ASCII;i++){ + if(symCode[i]!=0){ + + writeBuf[0]=(char)i; + writeBuf[1]=(char)strlen(symCode[i]); + strcpy(&writeBuf[2],symCode[i]); + fwrite(writeBuf, sizeof(char),2+strlen(symCode[i]),fout); + } + } + + FILE *fin; + fin=fopen(fName,"rt"); + if(fin!=0){ + + int locTotalNumBit; + locTotalNumBit=(int)ftell(fout); + + if(fseek(fout, 4, SEEK_CUR)!=0){ + printf("Fail to move the file pointer\n"); + fclose(fin); + fclose(fout); + return; + } + + char bitBuf[BUF_SZ]; + int bitBufIdx = 0; + int bitCount=7; + int totalBitNum =0; + char flag =0; + + + memset(bitBuf,0,BUF_SZ); + + + while(fgets(buf,BUF_SZ,fin)!=0){ + int len=strlen(buf); + for(int i=0;i<len; i++){ + char *huffmanCode = symCode[(int)buf[i]]; + int f=strlen(huffmanCode); + for(int j=0;j<f;j++){ + char val = 0; + if(huffmanCode[j]=='0'){ + val = 0; + } + else if (huffmanCode[j]=='1'){ + val = 1; + } + else{ + printf("ERROR!!!!!!!!!!!"); + exit(0); + } + + val=val<<bitCount; + bitCount--; + + bitBuf[bitBufIdx] |= val; + flag = 1; + totalBitNum++; + if(bitCount < 0){ + bitCount = 7; + bitBufIdx++; + if(bitBufIdx>=BUF_SZ){ + fwrite(bitBuf,1,BUF_SZ, fout); + bitBufIdx=0; + memset(bitBuf,0,BUF_SZ); + } + } + } + } + } + if(bitBufIdx>0){ + fwrite(bitBuf,1,bitBufIdx+1,fout); + } + + if(fseek(fout,locTotalNumBit,SEEK_SET)==0){ + fwrite(&totalBitNum,sizeof(totalBitNum),1,fout); + } + else{ + printf("Error! unable to record total numb of bits\n"); + } + fclose(fin); + } + else{ + printf("Error!! Unable to open this file"); + } + fclose(fout); + } + else{ + printf("Error! Unable to open this file"); + } +} + +void just_traverse(struct node *cur,char c){ + codeBufIdx++; + codeBuf[codeBufIdx]=c; + codeBuf[codeBufIdx+1]=0; + + if(cur->left == 0 && cur->right ==0){ + printf("character %d (%c) : %s\n",(char)cur->c,cur->c, codeBuf); + } + else{ + just_traverse(cur->left,'0'); + just_traverse(cur->right,'1'); + } + codeBuf[codeBufIdx]=0; + codeBufIdx--; + return; +} + +void traverse(struct node *cur,char c){ + codeBufIdx++; + codeBuf[codeBufIdx]=c; + codeBuf[codeBufIdx+1]=0; + + if(cur->left == 0 && cur->right ==0){ + printf("character %d (%c) : %s\n",(char)cur->c,cur->c, codeBuf); + + char *huffCode = (char *)malloc(strlen(codeBuf)+1); + strcpy(huffCode,codeBuf); + symCode[(int)cur->c] = huffCode; + } + else{ + traverse(cur->left,'0'); + traverse(cur->right,'1'); + } + codeBuf[codeBufIdx]=0; + codeBufIdx--; + return; +} + +struct node* deleteFromHeap(void){ + if(lastHeapIdx<=0){ + return 0; + } + struct node *retVal = heap[1]; + + heap[1]=heap[lastHeapIdx]; + lastHeapIdx--; + + int parent = 1; + int left = 2*parent; + int right = left+1; + + while(1){ + if(left>lastHeapIdx){ + break; + } + else if(right>lastHeapIdx){ + if(heap[left]->frequency < heap[parent]->frequency){ + struct node *temp = heap[left]; + heap[left]=heap[parent]; + heap[parent]=temp; + + parent = left; + left =2*parent; + right=left+1; + } + else break; + } + else{ + int smaller; + if(heap[left]->frequency <= heap[parent]->frequency){ + smaller = left; + } + else{ + smaller = right; + } + if(heap[smaller]->frequency < heap[parent]->frequency){ + struct node *temp = heap[smaller]; + heap[smaller]=heap[parent]; + heap[parent]=temp; + + parent = smaller; + left =2*parent; + right=left+1; + } + else break; + } + } + return retVal; +} + +void addToHeap(struct node* cur){ + lastHeapIdx++; + heap[lastHeapIdx] = cur; + int currentIdx = lastHeapIdx; + int parentIdx = currentIdx/2; + + + while(parentIdx >=1){ + if(heap[parentIdx]->frequency > heap[currentIdx]->frequency){ + struct node *temp = heap[parentIdx]; + heap[parentIdx]=heap[currentIdx]; + heap[currentIdx]=temp; + + currentIdx = parentIdx; + parentIdx = currentIdx/2; + } + else break; + } +} + +int countNonZeroCharacters(){ + int cnt =0; + for(int i=0; i<NUM_ASCII;i++){ + if(charFreq[i]>0){ + cnt++; + } + } + return cnt; +} + +void showCharFrequency(void){ + for(int i=0;i<NUM_ASCII;i++){ + if(charFreq[i]>0){ + printf("ASCII code %d ( %c) : %d \n",i,(char)i,charFreq[i]); + } + } +} + + + +void Decoding(char *fName){ + FILE *fin =0; + fin = fopen(fName,"rb"); + + if(fin!=0){ + //허프만트리 재구성 + int numOfSym = 0; + fread(&numOfSym,sizeof(int),1,fin); + + printf("Number of Sybols is %d\n",numOfSym); + + struct node *huffRoot = (struct node *)malloc(sizeof(struct node)); + huffRoot->left = huffRoot->right=0; + struct node *cur=huffRoot; + + for(int i=0;i<NUM_ASCII;i++){ + char symbolAndLen[2]; + fread(symbolAndLen,2,1,fin); + char buf[100]; + fread(buf,1,(int)symbolAndLen[1],fin); + buf[(int)symbolAndLen[1]]=0; + printf("%c (%d) ==> %s\n",symbolAndLen[0],(int)symbolAndLen[1],buf); + + cur=huffRoot; + for(int i=0;i<(int)symbolAndLen[1];i++){ + if(buf[i]=='0'){ + if(cur->left==0){ + cur->left = (struct node *)malloc(sizeof(struct node)); + cur->left->left=0; + cur->left->right=0; + } + cur=cur->left; + } + else if(buf[i]=='1'){ + if(cur->right==0){ + cur->right = (struct node *)malloc(sizeof(struct node)); + cur->right->left=0; + cur->right->right=0; + } + cur=cur->right; + } + else{ + printf("ERROR!!!!!!!!!!!\n"); + exit(0); + } + } + cur->c=symbolAndLen[0]; + + } + codeBufIdx=-1; + just_traverse(huffRoot->left,'0'); + just_traverse(huffRoot->right,'1'); + //디코딩 수행 + FILE *decodedFile; + + char decodedFName[100]; + char *period =strchr(fName, (int)'.'); + strncpy(decodedFName,fName,(int)(period-fName)); + decodedFName[(int)(period-fName)] = 0; + strcat(decodedFName,"_decoded.txt"); + printf("Decoded file name is %s\n",decodedFName); + decodedFile = fopen(decodedFName,"wt"); + if(decodedFile==0){ + printf("Unable to create file"); + exit(0); + } + + + int numBitsToRead=0; + fread(&numBitsToRead,sizeof(int),1,fin); + printf("Total number of bits to read is %d\n",numBitsToRead); + cur=huffRoot; + char buf[BUF_SZ]; + while(1){ + int sz=fread(buf,1,BUF_SZ,fin); + if(sz==0){ + printf("End of file\n"); + break; + } + else{ + for(int i=0;i<sz;i++){ + for(int j=0;j<8;j++){ + if((char)(buf[i]& 0x80)==0){ + cur=cur->left; + } + else{ + cur=cur->right; + } + buf[i]=buf[i]<<1; + numBitsToRead--; + + if(cur->left==0&&cur->right==0){ + //printf("%c",cur->c); + fputc(cur->c,decodedFile); + cur = huffRoot; + } + if(numBitsToRead==0){ + printf("End of decoding\n"); + fclose(decodedFile); + fclose(fin); + return; + } + } + } + } + } + + + fclose(fin); + } + else{ + printf("Error! Unable to open this file"); + } +} diff --git a/2-2/Algorithm/Algorithm_201721083_KTS_Sorting.c b/2-2/Algorithm/Algorithm_201721083_KTS_Sorting.c new file mode 100644 index 0000000000000000000000000000000000000000..6573174f054fd5302fe3b9269cf407cf63b68b0a --- /dev/null +++ b/2-2/Algorithm/Algorithm_201721083_KTS_Sorting.c @@ -0,0 +1,418 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <time.h> +#define name_max 50 +#define nation_max 30 +#define state_max 20 +#define year_max 4 +#define num_max 5 +#define line_max 10000 +#define SWAP(x, y, temp) ( (temp)=(x), (x)=(y), (y)=(temp) ) +#define PRODUCTION_YEAR 0 +#define NUMBER_OF_PRODUCTION 1 +#define THEN_COMPARING 2 +clock_t start, finish =0; + +typedef struct{ + char name[name_max]; + char nation[nation_max]; + char state[state_max]; + char num[num_max]; + char prod_year[year_max]; + int number; + int production_year; +}JET; +void Print_Jet(JET Jet[],int count); +void Merge_Sort(JET Jet[], int start, int last, int which_one); +void Merge(JET Jet[], int start, int middle, int last, int which_one); +void Bubble_Sort(JET Jet[], int count,int which_one){ + JET temp; + if(which_one==PRODUCTION_YEAR){ + for (int i = 0; i < count; i++){ + for (int j = 0; j < count-1; j++){ + if (Jet[j].production_year > Jet[j+1].production_year){ + SWAP(Jet[j], Jet[j+1], temp); + } + } + } + } + else if(which_one==NUMBER_OF_PRODUCTION){ + for (int i = 0; i < count; i++){ + for (int j = 0; j < count-1; j++){ + if (Jet[j].number > Jet[j+1].number){ + SWAP(Jet[j], Jet[j+1], temp); + } + } + } + } + else if(which_one==THEN_COMPARING){ + for (int i = 0; i < count-1; i++){ + for (int j = 0; j < count-1; j++){ + if (Jet[j].production_year > Jet[j+1].production_year){ + SWAP(Jet[j], Jet[j+1], temp); + } + else if(Jet[j].production_year==Jet[j+1].production_year&&Jet[j].number>Jet[j+1].number){ + SWAP(Jet[j], Jet[j+1], temp); + } + } + } + } +} + +void Selection_Sort(JET Jet[],int count, int which_one){ + int i, j, least; + JET temp; + if(which_one==PRODUCTION_YEAR){ + for(i=0; i<count; i++){ + least = i; + for(j=i+1; j<count; j++){ + if(Jet[j].production_year<Jet[least].production_year) least = j; + } + if(i != least) SWAP(Jet[i], Jet[least], temp); + } + } + else if(which_one==NUMBER_OF_PRODUCTION){ + for(i=0; i<count; i++){ + least = i; + for(j=i+1; j<count; j++){ + if(Jet[j].number<Jet[least].number) least = j; + } + if(i != least) SWAP(Jet[i], Jet[least], temp); + } + } + else if(which_one==THEN_COMPARING){ + for(i=0; i<count; i++){ + least = i; + for(j=i+1; j<count; j++){ + if(Jet[j].production_year<Jet[least].production_year) least = j; + else if(Jet[j].production_year==Jet[least].production_year&&Jet[j].number<Jet[least].number) least = j; + } + if(i != least) SWAP(Jet[i], Jet[least], temp); + } + } +} + +void Shell_Sort(JET Jet[],int count,int which_one){ + if(which_one==PRODUCTION_YEAR){ + for(int i=count/2; i>0; i /=2){ + for(int j=0; j<i; j++){ + for(int k=i+j; k<count; k+=i){ + int l = Jet[k].production_year; + JET temp = Jet[k]; + int m = k; + while(m>i-1 && Jet[m-i].production_year > l){ + Jet[m]=Jet[m-i]; + m -= i; + } + Jet[m]=temp; + } + } + } + } + else if(which_one==NUMBER_OF_PRODUCTION){ + for(int i=count/2; i>0; i /=2){ + for(int j=0; j<i; j++){ + for(int k=i+j; k<count; k+=i){ + int l = Jet[k].number; + JET temp = Jet[k]; + int m = k; + while(m>i-1 && Jet[m-i].number > l){ + Jet[m]=Jet[m-i]; + m -= i; + } + Jet[m]=temp; + } + } + } + } + else if(which_one==THEN_COMPARING){ + for(int i=count/2; i>0; i /=2){ + for(int j=0; j<i; j++){ + for(int k=i+j; k<count; k+=i){ + int l = Jet[k].production_year; + JET temp = Jet[k]; + int m = k; + while(m>i-1 && Jet[m-i].production_year > l){ + Jet[m]=Jet[m-i]; + m -= i; + } + Jet[m]=temp; + } + } + } + for(int i=count/2; i>0; i /=2){ + for(int j=0; j<i; j++){ + for(int k=i+j; k<count; k+=i){ + int l = Jet[k].production_year; + int n = Jet[k].number; + JET temp = Jet[k]; + int m = k; + while(m>i-1 && Jet[m-i].production_year == l && Jet[m-i].number>n){ + Jet[m]=Jet[m-i]; + m -= i; + } + Jet[m]=temp; + } + } + } + } +} +void Merge(JET Jet[], int start, int middle, int last, int which_one){ + int i = start, j = middle+1, k = start; + JET temp[last+1]; + if(which_one==PRODUCTION_YEAR){ + while(i<=middle && j<=last){ + if(Jet[i].production_year <= Jet[j].production_year) temp[k++] = Jet[i++]; + else temp[k++] = Jet[j++]; + } + while(i<=middle) temp[k++] = Jet[i++]; + while(j<=last) temp[k++] = Jet[j++]; + for(int m = start; m<=last; m++) Jet[m] = temp[m]; + } + else if(which_one==NUMBER_OF_PRODUCTION){ + while(i<=middle && j<=last){ + if(Jet[i].number <= Jet[j].number) temp[k++] = Jet[i++]; + else temp[k++] = Jet[j++]; + } + while(i<=middle) temp[k++] = Jet[i++]; + while(j<=last) temp[k++] = Jet[j++]; + for(int m = start; m<=last; m++) Jet[m] = temp[m]; + } + else if(which_one==THEN_COMPARING){ + while(i<=middle && j<=last){ + if(Jet[i].production_year < Jet[j].production_year) temp[k++] = Jet[i++]; + else if(Jet[i].production_year==Jet[j].production_year && Jet[i].number<=Jet[j].number){ + temp[k++] = Jet[i++]; + } + else temp[k++] = Jet[j++]; + } + while(i<=middle) temp[k++] = Jet[i++]; + while(j<=last) temp[k++] = Jet[j++]; + for(int m = start; m<=last; m++) Jet[m] = temp[m]; + } +} + + +void Merge_Sort(JET Jet[], int start, int last, int which_one){ + int middle; + if(start<last){ + middle = (start+last)/2; + Merge_Sort(Jet, start , middle, which_one); + Merge_Sort(Jet, middle+1, last, which_one); + Merge(Jet, start, middle, last, which_one); + } +} + + +void split(const char* Expression, char* Dest, +const char* Delimiter, unsigned int Index){ + + size_t expLen=strlen(Expression); + size_t delLen=strlen(Delimiter); + + if(expLen==0 || delLen==0){ + memset(Dest,0,1); + return; + } + + for(unsigned int i=0,count=0,prev=0;i<expLen;i++){ + if(strncmp(Expression+i,Delimiter,delLen)==0){ + if(count==Index){ + if(count==0) { + strncpy(Dest,Expression,i); + memset(Dest+i,0,1); + }else{ + strncpy(Dest,Expression+prev+delLen,i-prev-delLen); + memset(Dest+i-prev-delLen,0,1); + } + return; + } + prev=i; + count++; + } + if(i==expLen-1 && count==Index){ + if(count==0){ + strcpy(Dest,Expression); + }else{ + strncpy(Dest,Expression+prev+delLen,expLen-prev-delLen); + memset(Dest+expLen-prev-delLen,0,1); + } + return; + } + } + memset(Dest,0,1); +} +void INI(JET Jet[],JET original[],int count){ + for(int i=0;i<count;i++){ + Jet[i]=original[i]; + } +} + +void Print_Jet(JET Jet[],int count){ + printf("----------------------------------------------------------------------------------------------------------------------\n"); + for(int i=0;i<count;i++){ + printf("%-50s %-40s %-10d %-10s %-10d\n",Jet[i].name,Jet[i].nation,Jet[i].production_year,Jet[i].state,Jet[i].number); + } + printf("----------------------------------------------------------------------------------------------------------------------\n"); +} + +int main(void){ + FILE *inp; + char line[line_max]; + char temp; + char *t; + int count=1; + int i=0; + inp = fopen("Fighters.csv", "r"); + + if (inp == NULL) { + printf("Cannot open\n"); + return -1; + } + while(fscanf(inp,"%c",&temp)!=EOF){ + if(temp=='\n') count++; + } + + JET *Jet = (JET *)malloc(sizeof(JET)*count); + + rewind(inp); + while (!feof(inp)) { + t=fgets(line,line_max,inp); + split(t,Jet[i].name,",",0); + split(t,Jet[i].nation,",",1); + split(t,Jet[i].prod_year,",",2); + Jet[i].production_year=atoi(Jet[i].prod_year); + split(t,Jet[i].state,",",3); + split(t,Jet[i].num,",",4); + Jet[i].number=atoi(Jet[i].num); + i++; + } + JET original[count]; + for(int i=0;i<count;i++){ + original[i]=Jet[i]; + } + + char keep_going = 'y'; + int choose = 0; + while(keep_going!='y'||keep_going !='Y'){ + printf("Which Sort do you want?\n"); + printf("----------------------------------------\n"); + printf("1. Bubble Sort by production year\n"); + printf("2. Selection Sort by production year\n"); + printf("3. Shell Sort by production year\n"); + printf("4. Merge Sort by production year\n"); + printf("5. Bubble Sort by production year\n"); + printf("6. Selection Sort by production year\n"); + printf("7. Shell Sort by production year\n"); + printf("8. Merge Sort by production year\n"); + printf("9. Bubble Sort by production year\n"); + printf("10. Selection Sort by production year\n"); + printf("11. Shell Sort by production year\n"); + printf("12. Merge Sort by production year\n"); + printf("----------------------------------------\n"); + scanf("%d",&choose); + if(choose==1){ + start = clock(); + Bubble_Sort(Jet,count,PRODUCTION_YEAR); + finish = clock(); + printf("Bubble Sort 생산년도 정렬 시간 : %fms\n",(float)(finish-start)); + Print_Jet(Jet, count); + INI(Jet,original,count); + } + else if(choose==2){ + start = clock(); + Selection_Sort(Jet,count,PRODUCTION_YEAR); + finish = clock(); + printf("Selection Sort 생산년도 정렬 시간 : %fms\n",(float)(finish-start)); + Print_Jet(Jet, count); + INI(Jet,original,count); + } + else if(choose==3){ + start = clock(); + Shell_Sort(Jet, count, PRODUCTION_YEAR); + finish = clock(); + printf("Shell Sort 생산년도 정렬 시간 : %fms\n",(float)(finish-start)); + Print_Jet(Jet, count); + INI(Jet,original,count); + } + else if(choose==4){ + start = clock(); + Merge_Sort(Jet,0,count-1,PRODUCTION_YEAR); + finish = clock(); + printf("Merge Sort 생산년도 정렬 시간 : %fms\n",(float)(finish-start)); + Print_Jet(Jet, count); + INI(Jet,original,count); + } + else if(choose==5){ + start = clock(); + Bubble_Sort(Jet,count,NUMBER_OF_PRODUCTION); + finish = clock(); + printf("Bubble Sort 생산대수 정렬 시간 : %fms\n",(float)(finish-start)); + Print_Jet(Jet, count); + INI(Jet,original,count); + } + else if(choose==6){ + start = clock(); + Selection_Sort(Jet,count,NUMBER_OF_PRODUCTION); + finish = clock(); + printf("Selection Sort 생산대수 정렬 시간 : %fms\n",(float)(finish-start)); + Print_Jet(Jet, count); + INI(Jet,original,count); + } + else if(choose==7){ + start = clock(); + Shell_Sort(Jet, count, NUMBER_OF_PRODUCTION); + finish = clock(); + printf("Shell Sort 생산대수 정렬 시간 : %fms\n",(float)(finish-start)); + Print_Jet(Jet, count); + INI(Jet,original,count); + } + else if(choose==8){ + start = clock(); + Merge_Sort(Jet,0,count-1,NUMBER_OF_PRODUCTION); + finish = clock(); + printf("Merge Sort 생산대수 정렬 시간 : %fms\n",(float)(finish-start)); + Print_Jet(Jet, count); + INI(Jet,original,count); + } + else if(choose==9){ + start = clock(); + Bubble_Sort(Jet,count,THEN_COMPARING); + finish = clock(); + printf("Bubble Sort 생산년도가 같을 경우 생산대수 정렬 시간 : %fms\n",(float)(finish-start)); + Print_Jet(Jet, count); + INI(Jet,original,count); + } + else if(choose==10){ + start = clock(); + Selection_Sort(Jet,count,THEN_COMPARING); + finish = clock(); + printf("Selection Sort 생산년도가 같을 경우 생산대수 정렬 시간 : %fms\n",(float)(finish-start)); + Print_Jet(Jet, count); + INI(Jet,original,count); + } + else if(choose==11){ + start = clock(); + Shell_Sort(Jet, count, THEN_COMPARING); + finish = clock(); + printf("Shell Sort 생산년도가 같을 경우 생산대수 정렬 시간 : %fms\n",(float)(finish-start)); + Print_Jet(Jet, count); + INI(Jet,original,count); + } + else if(choose==12){ + start = clock(); + //Merge_Sort(Jet, 0, count-1, PRODUCTION_YEAR); + Merge_Sort(Jet,0,count-1,THEN_COMPARING); + finish = clock(); + printf("Merge Sort 생산년도가 같을 경우 생산대수 정렬 시간 : %fms\n",(float)(finish-start)); + Print_Jet(Jet, count); + INI(Jet,original,count); + } + getchar(); + printf("Restart : Y OR y EXIT : except Y or y anything"); + scanf("%c",&keep_going); + } + free(Jet); + fclose(inp); +} diff --git a/2-2/Computer Graphics/Computer Graphics_201721083_KTS_Circle.cpp b/2-2/Computer Graphics/Computer Graphics_201721083_KTS_Circle.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c5ad56301404429bdfcc62dbdbb3156fde222703 --- /dev/null +++ b/2-2/Computer Graphics/Computer Graphics_201721083_KTS_Circle.cpp @@ -0,0 +1,98 @@ +// ConsoleApplication1.cpp : 이 파일에는 'main' 함수가 포함됩니다. 거기서 프로그램 실행이 시작되고 종료됩니다. + +#if __APPLE__ +#include <OpenGL/gl3.h> +#else +#include <gl/glew.h> +#include <GL +#endif +#include <GLFW/glfw3.h> +#include <glm/glm.hpp> +#include <math.h> +#include "toys.h" +#include <vector> + +void render (GLFWwindow* window); +void init(); + +int main(int argc, const char * argv[]){ + glfwInit(); + +#ifdef __APPLE__ + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,4); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); + glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE); + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); +#endif + GLFWwindow* window = glfwCreateWindow(700,700,"Test",0,0); + + glfwMakeContextCurrent(window); +//#ifndef __APPLE__ +// glewInit(); +//#endif + + init(); + + while(!glfwWindowShouldClose(window)){ + render(window); + glfwPollEvents(); + } + glfwTerminate(); + return 0; +} + +GLuint triBuffer = 0; +GLuint va = 0; +GLuint vao = 0; +GLuint elementArray = 0; +int how_many = 0; +Program program; + +using namespace glm; +using namespace std; +void init(){ + + + program.loadShaders("shader.vert","shader.frag"); + double rad = 1.0; + std::vector<glm::vec3> triVertData; + std::vector<glm::uvec3> triData; + triVertData.push_back(vec3(0,0,0)); + how_many +=3; + for(double i=0;i<=360;i++){ + double angle = (i/180)*3.149258; + double x = (double)rad*cos(angle); + double y = (double)rad*sin(angle); + triVertData.push_back(vec3(x,y,0)); + triData.push_back(uvec3(0,i,i+1)); + how_many+=3; + } + + glGenBuffers(1,&triBuffer); + glBindBuffer(GL_ARRAY_BUFFER, triBuffer); + glBufferData(GL_ARRAY_BUFFER, triVertData.size()*sizeof(vec3),triVertData.data(),GL_STATIC_DRAW); + + glGenVertexArrays(1,&va); + glBindVertexArray(va); + glBindBuffer(GL_ARRAY_BUFFER,triBuffer); + glEnableVertexAttribArray(0); + glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,nullptr); + + glGenBuffers(1,&elementArray); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementArray); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, triData.size()*sizeof(uvec3),triData.data(), GL_STATIC_DRAW); +} + +void render(GLFWwindow* window){ + int w,h; + glfwGetFramebufferSize(window,&w, &h); + glViewport(0,0,w,h); + glClearColor(0, 0, .5, 1); + glClear(GL_COLOR_BUFFER_BIT); + glUseProgram(program.programID); + glBindVertexArray(va); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,elementArray); + glDrawElements(GL_TRIANGLES,how_many,GL_UNSIGNED_INT,0); + glfwSwapBuffers(window); +} + diff --git a/2-2/Computer Graphics/Computer Graphics_201721083_KTS_Lighting.cpp b/2-2/Computer Graphics/Computer Graphics_201721083_KTS_Lighting.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6d57f48dbd9b498a91d11cdec09ee044acdb93ca --- /dev/null +++ b/2-2/Computer Graphics/Computer Graphics_201721083_KTS_Lighting.cpp @@ -0,0 +1,198 @@ +// ConsoleApplication1.cpp : 이 파일에는 'main' 함수가 포함됩니다. 거기서 프로그램 실행이 시작되고 종료됩니다. +#ifdef WIN32 +// #include + #define GLEW_STATIC + #include <GL/glew.h> + #define GLFW_INCLUDE_NONE +#elif defined __APPLE__ + #pragma clang diagnostic ignored "-Wdocumentation" + #include <OpenGL/gl3.h> +#endif + +#define GLFW_INCLUDE_NONE +#include <GLFW/glfw3.h> +#include "j3a.hpp" +#include "j3a.cpp" + +#ifdef WIN32 +#pragma comment (lib, "glew32s") +#pragma comment (lib, "opengl32") +#pragma comment (lib, "glfw3") +#endif +#include <vector> +#define GLM_EXPERIMENTAL +#include <glm/glm.hpp> +#include <glm/gtx/transform.hpp> +#include <glm/gtc/type_ptr.hpp> +#include "toys.h" +using namespace glm; + +void render (GLFWwindow* window); +void init(); +void mouseButtonCallback(GLFWwindow* window,int button,int action,int mods); +void cursorMotionCallback(GLFWwindow* window,double xpos,double ypos); + +GLuint triangleVBO; +GLuint normalVBO; +GLuint vertexArrayID; +GLuint indexVBOID; +Program program; + +float cameraDistance = 3; +glm::vec3 sceneCenter =glm::vec3(0,0,0); +float cameraYaw = 0.f; +float cameraPitch = 0.f; +float cameraFov = 60.f; +int lastX=0,lastY=0; + + +int main(int argc, const char * argv[]){ + glfwInit(); + +#ifdef __APPLE__ + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,4); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); + glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE); + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); +#endif + GLFWwindow* window = glfwCreateWindow(1000,800,"Test",0,0); + glfwMakeContextCurrent(window); + glfwSetMouseButtonCallback(window, mouseButtonCallback); + glfwSetCursorPosCallback(window, cursorMotionCallback); +#ifdef WIN32 + glewInit(); +#endif + init(); + while(!glfwWindowShouldClose(window)){ + render(window); + glfwPollEvents(); + } + glfwDestroyWindow(window); + glfwTerminate(); +} + +#ifdef WIN32 +#define PATH "../../Assign2/Assign2/" +#else +#define PATH "" +#endif + + +void init(){ + loadJ3A(PATH"bunny.j3a"); + program.loadShaders("shader.vert","shader.frag"); + + glGenVertexArrays(1, &vertexArrayID); + glBindVertexArray(vertexArrayID); + + glGenBuffers(1,&triangleVBO); + glBindBuffer(GL_ARRAY_BUFFER, triangleVBO); + glBufferData(GL_ARRAY_BUFFER, nVertices[0]*sizeof(glm::vec3), vertices[0], GL_STATIC_DRAW); + glEnableVertexAttribArray(0); + glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0); + + glGenBuffers(1,&normalVBO); + glBindBuffer(GL_ARRAY_BUFFER,normalVBO); + glBufferData(GL_ARRAY_BUFFER,nVertices[0]*sizeof(glm::vec3), normals[0],GL_STATIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER,normalVBO); + glEnableVertexAttribArray(1); + glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,0,0); + + glGenBuffers(1,&indexVBOID); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVBOID); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, nTriangles[0]*sizeof(glm::u32vec3), triangles[0], GL_STATIC_DRAW); + + glEnable(GL_DEPTH_TEST); //앞에 것이 먼저 그려져있을 때 뒤에 있는 것을 그리면 안덮어 씌워지게 해주는 것. +} + +float rotAngle = 0; + +void render(GLFWwindow* window){ + int w,h; + glfwGetFramebufferSize(window, &w,&h); + glViewport(0,0,w,h); + glClearColor(0,0,.5,0); + glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); + glUseProgram( program.programID); + glEnable(GL_DEPTH_TEST); + + glUseProgram(program.programID); + mat4 projMat = perspective(cameraFov*3.141592f/180, w/float(h), 0.01f, 100.f); + + vec3 cameraPosition = vec3(0,0,cameraDistance); + cameraPosition = vec3(rotate(cameraPitch,vec3(-1,0,0))*vec4(cameraPosition,1)); + cameraPosition = vec3(rotate(cameraYaw,vec3(0,1,0))*vec4(cameraPosition,1)); + mat4 viewMat = lookAt(cameraPosition, sceneCenter, vec3(0,1,0)); + + rotAngle+=0.1/180.f*3.141592; + + vec3 lightPos = vec3(3,3,3); + vec3 lightColor = vec3(1); + vec4 diffuseMaterial = vec4(0,0.7,0,1); + vec4 specularMaterial = vec4(1); + vec3 ambientLight = vec3(0.1); + float shine = 0.f; + + GLuint loc = glGetUniformLocation (program.programID, "modelMat"); + glUniformMatrix4fv(loc,1,0,value_ptr(rotate(90/180.f*3.14592f,vec3(1,0,0)))); + + loc = glGetUniformLocation (program.programID, "viewMat"); + glUniformMatrix4fv(loc,1,0,value_ptr(viewMat)); + + loc = glGetUniformLocation (program.programID, "projMat"); + glUniformMatrix4fv(loc,1,0,value_ptr(projMat)); + + loc = glGetUniformLocation(program.programID,"cameraPos"); + glUniform3fv(loc,1,value_ptr(cameraPosition)); + + loc = glGetUniformLocation(program.programID,"lightPos"); + glUniform3fv(loc,1,value_ptr(lightPos)); + + loc = glGetUniformLocation(program.programID,"lightColor"); + glUniform3fv(loc,1,value_ptr(lightColor)); + + loc = glGetUniformLocation(program.programID,"diffuseMaterial"); + glUniform4fv(loc,1,value_ptr(diffuseMaterial)); + + loc = glGetUniformLocation(program.programID,"specularMaterial"); + glUniform4fv(loc,1,value_ptr(specularMaterial)); + + loc = glGetUniformLocation(program.programID,"ambientLight"); + glUniform3fv(loc,1,value_ptr(ambientLight)); + + loc = glGetUniformLocation(program.programID,"shine"); + glUniform1f(loc,shine); + + glBindVertexArray(vertexArrayID); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVBOID); + glDrawElements(GL_TRIANGLES, nTriangles[0]*3, GL_UNSIGNED_INT, 0); + + + glfwSwapBuffers(window); +} + +void mouseButtonCallback(GLFWwindow* window,int button,int action,int mods){ + if(button==GLFW_MOUSE_BUTTON_1 && action==GLFW_PRESS){ + double xpos,ypos; + glfwGetCursorPos(window, &xpos, &ypos); + lastX = int(xpos); + lastY = int(ypos); + } + +} + +void cursorMotionCallback(GLFWwindow* window,double xpos,double ypos){ + if(glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_1)==GLFW_PRESS){ + if(glfwGetKey(window, GLFW_KEY_LEFT_CONTROL)==GLFW_PRESS){ + cameraFov+=(ypos-lastY)/100; + + } + else{ + cameraPitch+=(ypos-lastY)/300; + cameraPitch = glm::clamp(cameraPitch,-1.f,1.f); + cameraYaw-=(xpos-lastX)/300; + } + lastX = int(xpos); + lastY = int(ypos); + } +} diff --git a/2-2/Computer Graphics/Computer Graphics_201721083_KTS_Orbit Camera.cpp b/2-2/Computer Graphics/Computer Graphics_201721083_KTS_Orbit Camera.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7c188bb02f176c2d27153f00195bdc8ac728a87f --- /dev/null +++ b/2-2/Computer Graphics/Computer Graphics_201721083_KTS_Orbit Camera.cpp @@ -0,0 +1,156 @@ +// ConsoleApplication1.cpp : 이 파일에는 'main' 함수가 포함됩니다. 거기서 프로그램 실행이 시작되고 종료됩니다. +#ifdef WIN32 +// #include + #define GLEW_STATIC + #include <GL/glew.h> + #define GLFW_INCLUDE_NONE +#elif defined __APPLE__ + #pragma clang diagnostic ignored "-Wdocumentation" + #include <OpenGL/gl3.h> +#endif + +#define GLFW_INCLUDE_NONE +#include <GLFW/glfw3.h> +#include "j3a.hpp" +#include "j3a.cpp" + +#ifdef WIN32 +#pragma comment (lib, "glew32s") +#pragma comment (lib, "opengl32") +#pragma comment (lib, "glfw3") +#endif +#include <vector> +#define GLM_EXPERIMENTAL +#include <glm/glm.hpp> +#include <glm/gtx/transform.hpp> +#include <glm/gtc/type_ptr.hpp> +#include "toys.h" +using namespace glm; + +void render (GLFWwindow* window); +void init(); +void mouseButtonCallback(GLFWwindow* window,int button,int action,int mods); +void cursorMotionCallback(GLFWwindow* window,double xpos,double ypos); + +GLuint triangleVBO; +GLuint vertexArrayID; +GLuint indexVBOID; +Program program; + +float cameraDistance = 3; +glm::vec3 sceneCenter =glm::vec3(0,0,0); +float cameraYaw = 0.f; +float cameraPitch = 0.f; +float cameraFov = 60.f; +int lastX=0,lastY=0; + +int main(int argc, const char * argv[]){ + glfwInit(); + +#ifdef __APPLE__ + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,4); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); + glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE); + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); +#endif + GLFWwindow* window = glfwCreateWindow(1000,800,"Test",0,0); + glfwMakeContextCurrent(window); + glfwSetMouseButtonCallback(window, mouseButtonCallback); + glfwSetCursorPosCallback(window, cursorMotionCallback); +#ifdef WIN32 + glewInit(); +#endif + init(); + while(!glfwWindowShouldClose(window)){ + render(window); + glfwPollEvents(); + } + glfwDestroyWindow(window); + glfwTerminate(); +} + +#ifdef WIN32 +#define PATH "../../Assign2/Assign2/" +#else +#define PATH "" +#endif + + +void init(){ + loadJ3A(PATH"bunny.j3a"); + program.loadShaders("shader.vert","shader.frag"); + + glGenVertexArrays(1, &vertexArrayID); + glBindVertexArray(vertexArrayID); + + glGenBuffers(1,&triangleVBO); + glBindBuffer(GL_ARRAY_BUFFER, triangleVBO); + glBufferData(GL_ARRAY_BUFFER, nVertices[0]*sizeof(glm::vec3), vertices[0], GL_STATIC_DRAW); + glEnableVertexAttribArray(0); + glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0); + + glGenBuffers(1,&indexVBOID); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVBOID); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, nTriangles[0]*sizeof(glm::u32vec3), triangles[0], GL_STATIC_DRAW); +} + +float rotAngle = 0; + +void render(GLFWwindow* window){ + int w,h; + glfwGetFramebufferSize(window, &w,&h); + glViewport(0,0,w,h); + glClearColor(0,0,.5,0); + glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); + glUseProgram( program.programID); + glEnable(GL_DEPTH_TEST); + + mat4 projMat = perspective(cameraFov*3.141592f/180, w/float(h), 0.01f, 100.f); + + vec3 cameraPosition = vec3(0,0,cameraDistance); + cameraPosition = vec3(rotate(cameraPitch,vec3(-1,0,0))*vec4(cameraPosition,1)); + cameraPosition = vec3(rotate(cameraYaw,vec3(0,1,0))*vec4(cameraPosition,1)); + mat4 viewMat = lookAt(cameraPosition, sceneCenter, vec3(0,1,0)); + + rotAngle+=0.1/180.f*3.141592; + GLuint loc = glGetUniformLocation (program.programID, "modelMat"); + //mat4 rotMat = rotate(rotAngle, glm::vec3(0,1,0)); + glUniformMatrix4fv(loc,1,0,value_ptr(rotate(90/180.f*3.14592f,vec3(1,0,0)))); + + loc = glGetUniformLocation (program.programID, "viewMat"); + glUniformMatrix4fv(loc,1,0,value_ptr(viewMat)); + + loc = glGetUniformLocation (program.programID, "projMat"); + glUniformMatrix4fv(loc,1,0,value_ptr(projMat)); + + glBindVertexArray(vertexArrayID); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVBOID); + glDrawElements(GL_TRIANGLES, nTriangles[0]*3, GL_UNSIGNED_INT, 0); + glfwSwapBuffers(window); +} + +void mouseButtonCallback(GLFWwindow* window,int button,int action,int mods){ + if(button==GLFW_MOUSE_BUTTON_1 && action==GLFW_PRESS){ + double xpos,ypos; + glfwGetCursorPos(window, &xpos, &ypos); + lastX = int(xpos); + lastY = int(ypos); + } + +} + +void cursorMotionCallback(GLFWwindow* window,double xpos,double ypos){ + if(glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_1)==GLFW_PRESS){ + if(glfwGetKey(window, GLFW_KEY_LEFT_CONTROL)==GLFW_PRESS){ + cameraFov+=(ypos-lastY)/100; + + } + else{ + cameraPitch+=(ypos-lastY)/300; + cameraPitch = glm::clamp(cameraPitch,-1.f,1.f); + cameraYaw-=(xpos-lastX)/300; + } + lastX = int(xpos); + lastY = int(ypos); + } +} diff --git a/2-2/Computer Graphics/Computer Graphics_201721083_KTS_Rotating Bunny.cpp b/2-2/Computer Graphics/Computer Graphics_201721083_KTS_Rotating Bunny.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9ecfe84cf59fadc0c3d8d32872403d6ea77ae5cd --- /dev/null +++ b/2-2/Computer Graphics/Computer Graphics_201721083_KTS_Rotating Bunny.cpp @@ -0,0 +1,107 @@ +// ConsoleApplication1.cpp : 이 파일에는 'main' 함수가 포함됩니다. 거기서 프로그램 실행이 시작되고 종료됩니다. +#ifdef WIN32 +// #include + #define GLEW_STATIC + #include <GL/glew.h> + #define GLFW_INCLUDE_NONE +#elif defined __APPLE__ + #pragma clang diagnostic ignored "-Wdocumentation" + #include <OpenGL/gl3.h> +#endif + +#define GLFW_INCLUDE_NONE +#include <GLFW/glfw3.h> +#include "j3a.hpp" +#include "j3a.cpp" + +#ifdef WIN32 +#pragma comment (lib, "glew32s") +#pragma comment (lib, "opengl32") +#pragma comment (lib, "glfw3") +#endif +#include <vector> +#define GLM_EXPERIMENTAL +#include <glm/glm.hpp> +#include <glm/gtx/transform.hpp> +#include <glm/gtc/type_ptr.hpp> +#include "toys.h" +using namespace glm; + +void render (GLFWwindow* window); +void init(); + +GLuint triangleVBO; +GLuint vertexArrayID; +GLuint indexVBOID; +Program program; + +int main(int argc, const char * argv[]){ + glfwInit(); + +#ifdef __APPLE__ + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,4); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); + glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE); + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); +#endif + GLFWwindow* window = glfwCreateWindow(1000,800,"Test",0,0); + glfwMakeContextCurrent(window); +#ifdef WIN32 + glewInit(); +#endif + init(); + while(!glfwWindowShouldClose(window)){ + render(window); + glfwPollEvents(); + } + glfwDestroyWindow(window); + glfwTerminate(); +} + +#ifdef WIN32 +#define PATH "../../Assign2/Assign2/" +#else +#define PATH "" +#endif + + +void init(){ + loadJ3A("bunny.j3a"); + program.loadShaders("shader.vert","shader.frag"); + + glGenVertexArrays(1, &vertexArrayID); + glBindVertexArray(vertexArrayID); + + glGenBuffers(1,&triangleVBO); + glBindBuffer(GL_ARRAY_BUFFER, triangleVBO); + glBufferData(GL_ARRAY_BUFFER, nVertices[0]*sizeof(glm::vec3), vertices[0], GL_STATIC_DRAW); + glEnableVertexAttribArray(0); + glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0); + + glGenBuffers(1,&indexVBOID); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVBOID); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, nTriangles[0]*sizeof(glm::u32vec3), triangles[0], GL_STATIC_DRAW); +} + +float rotAngle = 0; + +void render(GLFWwindow* window){ + int w,h; + glfwGetFramebufferSize(window, &w,&h); + glViewport(0,0,w,h); + glClearColor(0,0,.5,0); + glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); + glUseProgram( program.programID); + glEnable(GL_DEPTH_TEST); + + rotAngle+=0.1/180.f*3.141592; + GLuint loc = glGetUniformLocation (program.programID, "modelMat"); + mat4 rotMat = rotate(rotAngle, glm::vec3(0,1,0)); + glUniformMatrix4fv(loc,1,0,value_ptr(rotMat)); + + glBindVertexArray(vertexArrayID); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVBOID); + glDrawElements(GL_TRIANGLES, nTriangles[0]*3, GL_UNSIGNED_INT, 0); + glfwSwapBuffers(window); +} + diff --git a/2-2/Computer Graphics/Computer Graphics_201721083_KTS_Shadow Mapping.cpp b/2-2/Computer Graphics/Computer Graphics_201721083_KTS_Shadow Mapping.cpp new file mode 100644 index 0000000000000000000000000000000000000000..79645a4fffb79e592b1812c837130f0605d155aa --- /dev/null +++ b/2-2/Computer Graphics/Computer Graphics_201721083_KTS_Shadow Mapping.cpp @@ -0,0 +1,308 @@ +#ifdef WIN32 + #define GLEW_STATIC + #include <GL/glew.h> + #define GLFW_INCLUDE_NONE +#elif defined __APPLE__ + #pragma clang diagnostic ignored "-Wdocumentation" + #include <OpenGL/gl3.h> +#endif + +#define GLFW_INCLUDE_NONE +#include <GLFW/glfw3.h> +#include "j3a.hpp" +#include "j3a.cpp" + +#ifdef WIN32 +#pragma comment (lib, "glew32s") +#pragma comment (lib, "opengl32") +#pragma comment (lib, "glfw3") +#endif +#include <vector> +#define GLM_EXPERIMENTAL +#include <glm/glm.hpp> +#include <glm/gtx/transform.hpp> +#include <glm/gtx/projection.hpp> +#include <glm/gtc/type_ptr.hpp> +#include "toys.h" +#define STB_IMAGE_IMPLEMENTATION +#include "stb_image.h" +using namespace glm; + +void render (GLFWwindow* window); +void init(); +void cursorCallback(GLFWwindow* window, double x, double y); +void refreshCallback(GLFWwindow* window); +void scrollCallback(GLFWwindow* window,double dx,double dy); +void buttonCallback(GLFWwindow* window,int button, int action, int mods); + + +GLuint triangleVBO=0; +GLuint normalVBO=0; +GLuint vertexArrayID=0; +GLuint indexVBOID=0; + +GLuint diffTexID=0; +GLuint texCoordVBO=0; +GLuint bumpTexID=0; + + +GLuint shadowTex = 0; +GLuint shadowFBO = 0; +GLuint shadowDepth = 0; + +Program program; +Program shadowprogram; + +float cameraDistance = 8; +float cameraYaw = 0; +float cameraPitch = 0; +float cameraFov = 30; +double lastX, lastY; + +vec3 lightPos = vec3(3,3,3); +vec3 lightColor = vec3(1); +vec4 diffuseMaterial = vec4(0,0.7,0,1); +vec4 specularMaterial = vec4(1); +vec3 ambientLight = vec3(0.1); +float shine = 0.f; + + +int main(int argc, const char * argv[]){ + glfwInit(); + +#ifdef __APPLE__ + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,4); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); + glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE); + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); +#endif + GLFWwindow* window = glfwCreateWindow(640,480,"Test",0,0); + glfwMakeContextCurrent(window); + glfwSetScrollCallback(window, scrollCallback); + glfwSetMouseButtonCallback(window, buttonCallback); + glfwSetCursorPosCallback(window, cursorCallback); + glfwSetWindowRefreshCallback(window, refreshCallback); +#ifdef WIN32 + glewInit(); +#endif + init(); + while(!glfwWindowShouldClose(window)){ + render(window); + glfwPollEvents(); + } + glfwDestroyWindow(window); + glfwTerminate(); +} + + + +void init(){ + loadJ3A("dwarf.j3a"); + int texWidth, texHeight, texChannels; + + void *buffer = stbi_load(diffuseMap[0].c_str(),&texWidth,&texHeight,&texChannels,4); + + glGenTextures(1,&diffTexID); + glBindTexture(GL_TEXTURE_2D,diffTexID); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_REPEAT); + glTexImage2D(GL_TEXTURE_2D,0,GL_SRGB8_ALPHA8,texWidth,texHeight,0,GL_RGBA,GL_UNSIGNED_BYTE,buffer); + glGenerateMipmap(GL_TEXTURE_2D); + + + stbi_image_free(buffer); + + buffer = stbi_load(bumpMap[0].c_str(),&texWidth,&texHeight,&texChannels,4); + + glGenTextures(1,&bumpTexID); + glBindTexture(GL_TEXTURE_2D,bumpTexID); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_REPEAT); + glTexImage2D(GL_TEXTURE_2D,0,GL_SRGB8_ALPHA8,texWidth,texHeight,0,GL_RGBA,GL_UNSIGNED_BYTE,buffer); + glGenerateMipmap(GL_TEXTURE_2D); + + + stbi_image_free(buffer); + + program.loadShaders("shader.vert","shader.frag"); + shadowprogram.loadShaders("shadow.vert","shadow.frag"); + + + glGenVertexArrays(1, &vertexArrayID); + glBindVertexArray(vertexArrayID); + + glGenBuffers(1,&triangleVBO); + glBindBuffer(GL_ARRAY_BUFFER, triangleVBO); + glBufferData(GL_ARRAY_BUFFER, nVertices[0]*sizeof(glm::vec3), vertices[0], GL_STATIC_DRAW); + + glBindBuffer(GL_ARRAY_BUFFER, triangleVBO); + glEnableVertexAttribArray(0); + glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0); + + glGenBuffers(1,&normalVBO); + glBindBuffer(GL_ARRAY_BUFFER,normalVBO); + glBufferData(GL_ARRAY_BUFFER,nVertices[0]*sizeof(glm::vec3), normals[0],GL_STATIC_DRAW); + + glBindBuffer(GL_ARRAY_BUFFER,normalVBO); + glEnableVertexAttribArray(1); + glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,0,0); + + glGenBuffers(1,&texCoordVBO); + glBindBuffer(GL_ARRAY_BUFFER,texCoordVBO); + glBufferData(GL_ARRAY_BUFFER,nVertices[0]*sizeof(glm::vec2), texCoords[0],GL_STATIC_DRAW); + + glBindBuffer(GL_ARRAY_BUFFER,texCoordVBO); + glEnableVertexAttribArray(2); + glVertexAttribPointer(2,2,GL_FLOAT,GL_FALSE,0,0); + + glGenBuffers(1,&indexVBOID); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVBOID); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, nTriangles[0]*sizeof(glm::u32vec3), triangles[0], GL_STATIC_DRAW); + + + + glEnable(GL_DEPTH_TEST);//앞에 것이 먼저 그려져있을 때 뒤에 있는 것을 그리면 안덮어 씌워지게 해주는 것. + glEnable(GL_FRAMEBUFFER_SRGB); + + glGenTextures(1, &shadowTex); + glBindTexture(GL_TEXTURE_2D, shadowTex); + glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB32F, 1024, 1024, 0,GL_RGB, GL_FLOAT, 0); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + + glGenTextures(1, &shadowDepth); + glBindTexture(GL_TEXTURE_2D, shadowDepth); + glTexImage2D(GL_TEXTURE_2D, 0,GL_DEPTH_COMPONENT32F, 1024, 1024, 0,GL_DEPTH_COMPONENT, GL_FLOAT, 0); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + + glGenFramebuffers(1, &shadowFBO); + glBindFramebuffer(GL_FRAMEBUFFER, shadowFBO); + glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, shadowTex, 0); + glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, shadowDepth, 0); + GLenum drawBuffers[] = {GL_COLOR_ATTACHMENT0}; + glDrawBuffers(1, drawBuffers); + if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) printf("FBO Error\n"); + glBindFramebuffer(GL_FRAMEBUFFER, GL_NONE); + + +} + +void render(GLFWwindow* window){ + int w,h; + GLuint loc; + mat4 modelMat = mat4(1); + glfwGetFramebufferSize(window, &w,&h); + glViewport(0,0,w,h); + glClearColor(0,0,.5,0); + glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); + + + glBindFramebuffer(GL_FRAMEBUFFER, shadowFBO); + glViewport(0,0,1024,1024); + glClearColor(1,1,1,1); + glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); + + glUseProgram(shadowprogram.programID); + mat4 shadowProjMat = ortho(-2.f,2.f,-2.f,2.f,0.01f,10.f); + mat4 shadowViewMat = lookAt(lightPos, vec3(0,0,0), vec3(0,1,0)); + mat4 shadowMVP = shadowProjMat * shadowViewMat * modelMat; + loc = glGetUniformLocation (shadowprogram.programID, "shadowMVP"); + glUniformMatrix4fv(loc, 1, 0, value_ptr(shadowMVP)); + + glBindVertexArray(vertexArrayID); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVBOID); + glDrawElements(GL_TRIANGLES, nTriangles[0]*3, GL_UNSIGNED_INT, 0); + glBindFramebuffer(GL_FRAMEBUFFER, GL_NONE); + + + glUseProgram(program.programID); + mat4 projMat = perspective(cameraFov/180.f*3.1415926535f, w/float(h), 0.01f, 100.f); + loc = glGetUniformLocation (program.programID, "projMat"); + glUniformMatrix4fv(loc,1,0,value_ptr(projMat)); + + loc = glGetUniformLocation (program.programID, "viewMat"); + + vec3 cameraPosition = vec3(rotate(cameraYaw,vec3(0,1,0))*rotate(cameraPitch,vec3(1,0,0))*vec4(0,0,cameraDistance,0)); + mat4 viewMat = lookAt(cameraPosition,vec3(0,0,0),vec3(0,1,0)); + glUniformMatrix4fv(loc,1,0,value_ptr(viewMat)); + + loc = glGetUniformLocation(program.programID,"cameraPos"); + glUniform3fv(loc,1,value_ptr(cameraPosition)); + + loc = glGetUniformLocation(program.programID,"lightPos"); + glUniform3fv(loc,1,value_ptr(lightPos)); + + loc = glGetUniformLocation(program.programID,"lightColor"); + glUniform3fv(loc,1,value_ptr(lightColor)); + + loc = glGetUniformLocation(program.programID,"diffuseMaterial"); + glUniform4fv(loc,1,value_ptr(diffuseMaterial)); + + loc = glGetUniformLocation(program.programID,"specularMaterial"); + glUniform4fv(loc,1,value_ptr(specularMaterial)); + + loc = glGetUniformLocation(program.programID,"ambientLight"); + glUniform3fv(loc,1,value_ptr(ambientLight)); + + loc = glGetUniformLocation(program.programID,"shine"); + glUniform1f(loc,shine); + + + mat4 shadowBias = translate(vec3(0.5))*scale(vec3(0.5)); + mat4 shadowBiasMVP = shadowBias*shadowMVP; + loc = glGetUniformLocation (program.programID, "shadowBiasMVP"); + glUniformMatrix4fv(loc,1,0,value_ptr(shadowBiasMVP)); + + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D,diffTexID); + loc = glGetUniformLocation(program.programID,"diffTex"); + glUniform1i(loc,1); + + glActiveTexture(GL_TEXTURE2); + glBindTexture(GL_TEXTURE_2D,bumpTexID); + loc = glGetUniformLocation(program.programID,"bumpTex"); + glUniform1i(loc,2); + + glActiveTexture(GL_TEXTURE3); + glBindTexture(GL_TEXTURE_2D, shadowDepth); + loc = glGetUniformLocation(program.programID, "shadowMap"); + glUniform1i(loc, 3); + + glBindVertexArray(vertexArrayID); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVBOID); + glDrawElements(GL_TRIANGLES, nTriangles[0]*3, GL_UNSIGNED_INT, 0); + + + glfwSwapBuffers(window); +} +void cursorCallback(GLFWwindow* window, double x, double y){ + if(glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_1)){ + cameraPitch-=(y-lastY)/200; + cameraYaw-=(x-lastX)/200; + lastX=x; + lastY=y; + } +} + +void buttonCallback(GLFWwindow* window,int button, int action, int mods){ + if(button==GLFW_MOUSE_BUTTON_1&&action==GLFW_PRESS){ + glfwGetCursorPos(window, &lastX, &lastY); + } +} +void scrollCallback(GLFWwindow* window,double dx,double dy){ + cameraFov-=dy; + if(cameraFov<5) cameraFov=5; + if(cameraFov>170) cameraFov=170; +} +void refreshCallback(GLFWwindow* window){ + render(window); +} diff --git a/2-2/Computer Graphics/Computer Graphics_201721083_KTS_Texture_Bump Mapping.cpp b/2-2/Computer Graphics/Computer Graphics_201721083_KTS_Texture_Bump Mapping.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d8fa815aecb9b378dbfb874d7d9708cd9ab0a369 --- /dev/null +++ b/2-2/Computer Graphics/Computer Graphics_201721083_KTS_Texture_Bump Mapping.cpp @@ -0,0 +1,248 @@ +#ifdef WIN32 + #define GLEW_STATIC + #include <GL/glew.h> + #define GLFW_INCLUDE_NONE +#elif defined __APPLE__ + #pragma clang diagnostic ignored "-Wdocumentation" + #include <OpenGL/gl3.h> +#endif + +#define GLFW_INCLUDE_NONE +#include <GLFW/glfw3.h> +#include "j3a.hpp" +#include "j3a.cpp" + +#ifdef WIN32 +#pragma comment (lib, "glew32s") +#pragma comment (lib, "opengl32") +#pragma comment (lib, "glfw3") +#endif +#include <vector> +#define GLM_EXPERIMENTAL +#include <glm/glm.hpp> +#include <glm/gtx/transform.hpp> +#include <glm/gtc/type_ptr.hpp> +#include "toys.h" +#define STB_IMAGE_IMPLEMENTATION +#include "stb_image.h" +using namespace glm; + +void render (GLFWwindow* window); +void init(); +void mouseButtonCallback(GLFWwindow* window,int button,int action,int mods); +void cursorMotionCallback(GLFWwindow* window,double xpos,double ypos); + +GLuint triangleVBO=0; +GLuint normalVBO=0; +GLuint vertexArrayID=0; +GLuint indexVBOID=0; + +GLuint diffTexID=0; +GLuint texCoordVBO=0; +GLuint bumpTexID=0; + +Program program; + +float cameraDistance = 8; +glm::vec3 sceneCenter =glm::vec3(0,0,0); +float cameraYaw = 0; +float cameraPitch = 0; +float cameraFov = 30; +int lastX=0,lastY=0; + + +int main(int argc, const char * argv[]){ + glfwInit(); + +#ifdef __APPLE__ + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,4); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); + glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE); + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); +#endif + GLFWwindow* window = glfwCreateWindow(1000,800,"Test",0,0); + glfwMakeContextCurrent(window); + glfwSetMouseButtonCallback(window, mouseButtonCallback); + glfwSetCursorPosCallback(window, cursorMotionCallback); +#ifdef WIN32 + glewInit(); +#endif + init(); + while(!glfwWindowShouldClose(window)){ + render(window); + glfwPollEvents(); + } + glfwDestroyWindow(window); + glfwTerminate(); +} + + + +void init(){ + loadJ3A("apple.j3a"); + int texWidth, texHeight, texChannels; + + void *buffer = stbi_load("appleD.jpg",&texWidth,&texHeight,&texChannels,4); + + glGenTextures(1,&diffTexID); + glBindTexture(GL_TEXTURE_2D,diffTexID); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_REPEAT); + glTexImage2D(GL_TEXTURE_2D,0,GL_SRGB8_ALPHA8,texWidth,texHeight,0,GL_RGBA,GL_UNSIGNED_BYTE,buffer); + glGenerateMipmap(GL_TEXTURE_2D); + + + stbi_image_free(buffer); + + buffer = stbi_load("appleD.jpg",&texWidth,&texHeight,&texChannels,4); + + glGenTextures(1,&bumpTexID); + glBindTexture(GL_TEXTURE_2D,bumpTexID); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_REPEAT); + glTexImage2D(GL_TEXTURE_2D,0,GL_SRGB8_ALPHA8,texWidth,texHeight,0,GL_RGBA,GL_UNSIGNED_BYTE,buffer); + glGenerateMipmap(GL_TEXTURE_2D); + + + stbi_image_free(buffer); + + program.loadShaders("shader.vert","shader.frag"); + + glGenVertexArrays(1, &vertexArrayID); + glBindVertexArray(vertexArrayID); + + glGenBuffers(1,&triangleVBO); + glBindBuffer(GL_ARRAY_BUFFER, triangleVBO); + glBufferData(GL_ARRAY_BUFFER, nVertices[0]*sizeof(glm::vec3), vertices[0], GL_STATIC_DRAW); + glEnableVertexAttribArray(0); + glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0); + + glGenBuffers(1,&normalVBO); + glBindBuffer(GL_ARRAY_BUFFER,normalVBO); + glBufferData(GL_ARRAY_BUFFER,nVertices[0]*sizeof(glm::vec3), normals[0],GL_STATIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER,normalVBO); + glEnableVertexAttribArray(1); + glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,0,0); + + glGenBuffers(1,&indexVBOID); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVBOID); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, nTriangles[0]*sizeof(glm::u32vec3), triangles[0], GL_STATIC_DRAW); + + glGenBuffers(1,&texCoordVBO); + glBindBuffer(GL_ARRAY_BUFFER,texCoordVBO); + glBufferData(GL_ARRAY_BUFFER,nVertices[0]*sizeof(glm::vec2), texCoords[0],GL_STATIC_DRAW); + + glBindBuffer(GL_ARRAY_BUFFER,texCoordVBO); + glEnableVertexAttribArray(2); + glVertexAttribPointer(2,2,GL_FLOAT,GL_FALSE,0,0); + + glEnable(GL_DEPTH_TEST);//앞에 것이 먼저 그려져있을 때 뒤에 있는 것을 그리면 안덮어 씌워지게 해주는 것. + glEnable(GL_FRAMEBUFFER_SRGB); +} + +float rotAngle = 0; + +void render(GLFWwindow* window){ + int w,h; + glfwGetFramebufferSize(window, &w,&h); + glViewport(0,0,w,h); + glClearColor(0,0,.5,0); + glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); + glUseProgram( program.programID); + glEnable(GL_DEPTH_TEST); + + glUseProgram(program.programID); + mat4 projMat = perspective(cameraFov*3.141592f/180, w/float(h), 0.01f, 100.f); + + vec3 cameraPosition = vec3(0,0,cameraDistance); + cameraPosition = vec3(rotate(cameraPitch,vec3(-1,0,0))*vec4(cameraPosition,1)); + cameraPosition = vec3(rotate(cameraYaw,vec3(0,1,0))*vec4(cameraPosition,1)); + mat4 viewMat = lookAt(cameraPosition, sceneCenter, vec3(0,1,0)); + + rotAngle+=0.1/180.f*3.141592; + + vec3 lightPos = vec3(3,3,3); + vec3 lightColor = vec3(1); + vec4 diffuseMaterial = vec4(0,0.7,0,1); + vec4 specularMaterial = vec4(1); + vec3 ambientLight = vec3(0.1); + float shine = 0.f; + + GLuint loc = glGetUniformLocation (program.programID, "modelMat"); + glUniformMatrix4fv(loc,1,0,value_ptr(rotate(90/180.f*3.14592f,vec3(1,0,0)))); + + loc = glGetUniformLocation (program.programID, "viewMat"); + glUniformMatrix4fv(loc,1,0,value_ptr(viewMat)); + + loc = glGetUniformLocation (program.programID, "projMat"); + glUniformMatrix4fv(loc,1,0,value_ptr(projMat)); + + loc = glGetUniformLocation(program.programID,"cameraPos"); + glUniform3fv(loc,1,value_ptr(cameraPosition)); + + loc = glGetUniformLocation(program.programID,"lightPos"); + glUniform3fv(loc,1,value_ptr(lightPos)); + + loc = glGetUniformLocation(program.programID,"lightColor"); + glUniform3fv(loc,1,value_ptr(lightColor)); + + loc = glGetUniformLocation(program.programID,"diffuseMaterial"); + glUniform4fv(loc,1,value_ptr(diffuseMaterial)); + + loc = glGetUniformLocation(program.programID,"specularMaterial"); + glUniform4fv(loc,1,value_ptr(specularMaterial)); + + loc = glGetUniformLocation(program.programID,"ambientLight"); + glUniform3fv(loc,1,value_ptr(ambientLight)); + + loc = glGetUniformLocation(program.programID,"shine"); + glUniform1f(loc,shine); + + glActiveTexture(GL_TEXTURE2); + glBindTexture(GL_TEXTURE_2D,diffTexID); + loc = glGetUniformLocation(program.programID,"diffTex"); + glUniform1i(loc,2); + + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D,bumpTexID); + loc = glGetUniformLocation(program.programID,"bump"); + glUniform1i(loc,1); + + + glBindVertexArray(vertexArrayID); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVBOID); + glDrawElements(GL_TRIANGLES, nTriangles[0]*3, GL_UNSIGNED_INT, 0); + + + glfwSwapBuffers(window); +} + +void mouseButtonCallback(GLFWwindow* window,int button,int action,int mods){ + if(button==GLFW_MOUSE_BUTTON_1 && action==GLFW_PRESS){ + double xpos,ypos; + glfwGetCursorPos(window, &xpos, &ypos); + lastX = int(xpos); + lastY = int(ypos); + } + +} + +void cursorMotionCallback(GLFWwindow* window,double xpos,double ypos){ + if(glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_1)==GLFW_PRESS){ + if(glfwGetKey(window, GLFW_KEY_LEFT_CONTROL)==GLFW_PRESS){ + cameraFov+=(ypos-lastY)/100; + + } + else{ + cameraPitch+=(ypos-lastY)/300; + cameraPitch = glm::clamp(cameraPitch,-1.f,1.f); + cameraYaw-=(xpos-lastX)/300; + } + lastX = int(xpos); + lastY = int(ypos); + } +} diff --git a/2-2/Computer Graphics/shader.frag b/2-2/Computer Graphics/shader.frag new file mode 100644 index 0000000000000000000000000000000000000000..b30add125e159f3b48bb8d3d8e5dbb693e58e97d --- /dev/null +++ b/2-2/Computer Graphics/shader.frag @@ -0,0 +1,34 @@ +#version 150 core + +uniform vec3 lightPos; +uniform vec3 lightColor; +uniform vec4 diffuseMaterial; +uniform vec4 specularMaterial; +uniform vec3 ambientLight; +uniform vec3 cameraPos; +uniform float shine; + +in vec3 normal; +in vec3 worldPos; +out vec4 out_Color; + +void main(void) +{ + vec3 L = normalize(lightPos-worldPos); + vec3 N = normalize(normal); + vec3 V = normalize(cameraPos-worldPos); + vec3 R = N*dot(N,L)*2 - L; + + vec3 color = vec3(0.5,0.7,0.3); + + float diffuseFactor = clamp(dot(N,L),0,1); + float specularFactor = pow(clamp(dot(R,V),0,1),10); + color += diffuseMaterial.rgb*diffuseFactor*lightColor; + color += specularMaterial.rgb*specularFactor*lightColor; + color += diffuseMaterial.rgb*ambientLight; + + + out_Color=vec4(color,diffuseMaterial.a); + + +} diff --git a/2-2/Computer Graphics/shader.vert b/2-2/Computer Graphics/shader.vert new file mode 100644 index 0000000000000000000000000000000000000000..583a7d90dccc2be0cbf845a97b350aadc26faad7 --- /dev/null +++ b/2-2/Computer Graphics/shader.vert @@ -0,0 +1,13 @@ +#version 410 core + +layout(location=0) in vec3 in_Position; +uniform mat4 viewMat = mat4(1,0,0,0,0,2,0,0,0,0,1,0,0,0,-3,1); +uniform mat4 projMat = mat4(1.299038, 0,0,0,0,1.732051,0,0,0,0,-1.002002,-1.0,0,0,-0.2002,0); +uniform mat4 modelMat = mat4(1); +out vec3 normal; +void main(void) +{ + vec4 worldPos = modelMat* vec4(int_Position,1.); + gl_Position=projMat*viewMat*worldPos; +} + diff --git a/2-2/Computer Graphics/shadow.frag b/2-2/Computer Graphics/shadow.frag new file mode 100644 index 0000000000000000000000000000000000000000..7e3085afa7372479e296ea2a10aaf9c2a4a34558 --- /dev/null +++ b/2-2/Computer Graphics/shadow.frag @@ -0,0 +1,6 @@ +#version 330 core +layout(location = 0) out vec3 fragmentdepth; +void main(){ + fragmentdepth = vec3(gl_FragCoord.z); +} + diff --git a/2-2/Computer Graphics/shadow.vert b/2-2/Computer Graphics/shadow.vert new file mode 100644 index 0000000000000000000000000000000000000000..7dbe4a71d1da201cf98b4df7e4e2c9518df161d5 --- /dev/null +++ b/2-2/Computer Graphics/shadow.vert @@ -0,0 +1,8 @@ +#version 330 core +layout(location = 0) in vec3 in_Position; +uniform mat4 shadowMVP; +void main(){ + gl_Position = shadowMVP * vec4(in_Position,1); +} + + diff --git "a/2-2/Operating System/Proj00_201721083_KTS/Proj00_201721083_\352\271\200\355\203\234\354\204\235.pdf" "b/2-2/Operating System/Proj00_201721083_KTS/Proj00_201721083_\352\271\200\355\203\234\354\204\235.pdf" new file mode 100644 index 0000000000000000000000000000000000000000..2cf87b27ec4658226c984636a4a2d8d014e96611 Binary files /dev/null and "b/2-2/Operating System/Proj00_201721083_KTS/Proj00_201721083_\352\271\200\355\203\234\354\204\235.pdf" differ diff --git a/2-2/Operating System/Proj00_201721083_KTS/pa0.c b/2-2/Operating System/Proj00_201721083_KTS/pa0.c new file mode 100644 index 0000000000000000000000000000000000000000..b8ee5e6ca15764b77a9d1f1bf535c531c33bf603 --- /dev/null +++ b/2-2/Operating System/Proj00_201721083_KTS/pa0.c @@ -0,0 +1,163 @@ +/********************************************************************** + * Copyright (c) 2020 + * Sang-Hoon Kim <sanghoonkim@ajou.ac.kr> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTIABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + **********************************************************************/ + +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <ctype.h> + +#include "types.h" + +#define MAX_NR_TOKENS 32 /* Maximum number of tokens in a command */ +#define MAX_TOKEN_LEN 64 /* Maximum length of single token */ +#define MAX_COMMAND 256 /* Maximum length of command string */ + +/*********************************************************************** + * parse_command + * + * DESCRIPTION + * Parse @command, put each command token into @tokens[], and + * set @nr_tokens with the number of tokens. + * + * A command token is defined as a string without any whitespace (i.e., *space* + * and *tab* in this programming assignment). Suppose @command as follow; + * + * @command = " Hello world Ajou University!! " + * + * Then, @nr_tokens = 4 and @tokens should be + * + * tokens[0] = "Hello" + * tokens[1] = "world" + * tokens[2] = "Ajou" + * tokens[3] = "University!!" + * + * Another exmaple is; + * command = "ls -al /home/operating_system /hw0 " + * + * then, nr_tokens = 4, and tokens is + * tokens[0] = "ls" + * tokens[1] = "-al" + * tokens[2] = "/home/operating_system" + * tokens[3] = "/hw0" + * + * + * @command can be expressed with double quotation mark(") to quote a string + * that contains whitespaces. Suppose following @command; + * + * @command = " We will get over with the "COVID 19" virus " + * + * Then, @nr_tokens = 8, and @tokens are + * + * tokens[0] = "We" + * tokens[1] = "will" + * tokens[2] = "get" + * tokens[3] = "over" + * tokens[4] = "with" + * tokens[5] = "the" + * tokens[6] = "COVID 19" + * tokens[7] = "virus" + * + * Note that tokens[6] does not contain the quotation marks. + * Also, one @command can have multiple quoted strings, but they will not be + * nested + * (i.e., quote another string in a quote) + * + * This "is a possible" case for a command --> This, is a possible, case, for, + * a, command + * "This " is "what I told you" --> This, is, what I told you + * + * RETURN VALUE + * Return 0 after filling in @nr_tokens and @tokens[] properly + * + */ +static int howlong(char *command){ + int i=0; + while(1){ + i++; + if(command[i]=='\0') break; + } + return i; +} + +static int parse_command(char *command, int* nr_tokens, char *tokens[]) +{ + int i = howlong(command); + int count; + int t=0; + char **temp=(char**)malloc(sizeof(char*)*MAX_NR_TOKENS); + for(int j=0;j<MAX_NR_TOKENS;j++){ + temp[j] = (char*)malloc(sizeof(char)*MAX_TOKEN_LEN); + } + for(count =0; count<i;count++){ + if(command[count]=='\"'){ + count++; + while(command[count]!='\"'){ + temp[*nr_tokens][t]=command[count]; + t++; + count++; + } + if(command[count]=='\"'){ + tokens[*nr_tokens]=temp[*nr_tokens]; + (*nr_tokens)++; + t=0; + } + } + else if(isspace(command[count])==0&&command[count]!='\"'){ + temp[*nr_tokens][t]=command[count]; + t++; + if((isspace(command[count])==0&&isspace(command[count+1])!=0)||command[count+1]=='\"'){ + tokens[*nr_tokens]=temp[*nr_tokens]; + (*nr_tokens)++; + t=0; + } + } + } + return 0; +} + + +/*********************************************************************** + * The main function of this program. + * SHOULD NOT CHANGE THE CODE BELOW THIS LINE + */ +int main(int argc, char *argv[]) +{ + char line[MAX_COMMAND] = { '\0' }; + FILE *input = stdin; + + if (argc == 2) { + input = fopen(argv[1], "r"); + if (!input) { + fprintf(stderr, "No input file %s\n", argv[1]); + return -EINVAL; + } + } + + while (fgets(line, sizeof(line), input)) { + char *tokens[MAX_NR_TOKENS] = { NULL }; + int nr_tokens= 0; + + parse_command(line, &nr_tokens, tokens); + + fprintf(stderr, "nr_tokens = %d\n", nr_tokens); + for (int i = 0; i < nr_tokens; i++) { + fprintf(stderr, "tokens[%d] = %s\n", i, tokens[i]); + } + printf("\n"); + } + + if (input != stdin) fclose(input); + return 0; +}