├── .github └── FUNDING.yml ├── .gitignore ├── CSE2003 Introduction to Engineering Design ├── README.md ├── exercise │ ├── 1-1p1.c │ ├── 10p1.c │ ├── 11p1.c │ ├── 12p1.c │ ├── 13p1.c │ ├── 14p1.c │ ├── 15p1.c │ ├── 16p1.c │ ├── 17p1.c │ ├── 18p1.c │ ├── 19p1.c │ ├── 1p1.c │ ├── 20p1.c │ ├── 21p1.c │ ├── 22p1.c │ ├── 23p1.c │ ├── 24p1.c │ ├── 25p1.c │ ├── 26p1.c │ ├── 27-1p1.c │ ├── 27-2p1.c │ ├── 27p1.c │ ├── 28p1.c │ ├── 29Ep1.c │ ├── 29p1.c │ ├── 2p1.c │ ├── 2p2.c │ ├── 30-1p1.c │ ├── 30p1.c │ ├── 31p1.c │ ├── 32p1.c │ ├── 33p1.c │ ├── 35p1.c │ ├── 36p1.c │ ├── 37p1.c │ ├── 39p1.c │ ├── 3p1.c │ ├── 4-1p1.c │ ├── 42p1.c │ ├── 4p1.c │ ├── 5p1.c │ ├── 6p1.c │ ├── 7p1.c │ ├── 8p1.c │ ├── 9p1.c │ ├── README.md │ ├── ap1.c │ ├── ap2.c │ └── ap3.c ├── final │ ├── final-1.c │ ├── final-2.c │ ├── final-3.c │ └── final-4.c ├── midterm │ ├── midterm-1.c │ ├── midterm-2.c │ ├── midterm-3.c │ └── midterm-4.c └── project │ ├── paper.pdf │ └── project.c ├── CSE2035 C Programming ├── README.md ├── exercise │ ├── 10p1.c │ ├── 10p2.c │ ├── 11p1.c │ ├── 11p2.c │ ├── 12p1.c │ ├── 13p1.c │ ├── 13p2.c │ ├── 14p1.c │ ├── 15p1.c │ ├── 15p2.c │ ├── 16p1.c │ ├── 17p1.c │ ├── 17p2.c │ ├── 17p3.c │ ├── 18p1.c │ ├── 18p2.c │ ├── 19p1.c │ ├── 19p2.c │ ├── 19p3.c │ ├── 19p4.c │ ├── 1p1.c │ ├── 1p2.c │ ├── 1p3.c │ ├── 20p1.c │ ├── 20p2.c │ ├── 21p1.c │ ├── 21p2.c │ ├── 22p1.c │ ├── 22p2.c │ ├── 23p1.c │ ├── 23p2.c │ ├── 24p1.c │ ├── 24p2.c │ ├── 2p1.c │ ├── 2p2.c │ ├── 3p1.c │ ├── 3p2.c │ ├── 3p3.c │ ├── 3p4.c │ ├── 4p1.c │ ├── 4p2.c │ ├── 4p3.c │ ├── 5p1.c │ ├── 5p2.c │ ├── 6p1.c │ ├── 6p2.c │ ├── 6p3.c │ ├── 7p1.c │ ├── 7p2.c │ ├── 8p1.c │ ├── 8p2.c │ ├── 8p3.c │ ├── 9p1.c │ ├── 9p2.c │ ├── 9p3.c │ └── README.md ├── final │ ├── final-1.c │ ├── final-2.c │ ├── final-3.c │ └── final-4.c ├── midterm │ ├── midterm-1.c │ ├── midterm-2.c │ ├── midterm-3.c │ └── midterm-4.c └── project │ ├── paper.pdf │ ├── paper_sources │ ├── llncs.cls │ └── paper.tex │ └── project_sources │ ├── flow │ ├── admin │ │ ├── admin_login_page.c │ │ ├── admin_login_page.h │ │ ├── admin_page.c │ │ └── admin_page.h │ ├── main_menu.c │ ├── main_menu.h │ ├── search_product_page.c │ ├── search_product_page.h │ └── user │ │ ├── login_page.c │ │ ├── login_page.h │ │ ├── register_page.c │ │ ├── register_page.h │ │ ├── user_page.c │ │ └── user_page.h │ ├── language.c │ ├── language.h │ ├── makefile │ ├── proj1_20181634.c │ ├── proj1_20181634.h │ ├── types │ ├── linked_list.c │ ├── linked_list.h │ ├── member.c │ ├── member.h │ ├── order.c │ ├── order.h │ ├── product.c │ └── product.h │ └── util │ ├── util_file.c │ ├── util_file.h │ ├── util_io.c │ ├── util_io.h │ ├── util_string.c │ └── util_string.h ├── CSE3080 Data Structures ├── README.md └── openlab │ ├── README.md │ ├── week1 │ ├── README.md │ ├── lab1.data │ └── main.c │ ├── week2 │ ├── A.txt │ ├── B.txt │ ├── C.txt │ ├── README.md │ └── main.c │ ├── week3 │ ├── README.md │ ├── a[3].txt │ └── main.c │ ├── week4 │ ├── README.md │ ├── input[15].txt │ └── main.c │ ├── week5 │ ├── README.md │ ├── input.txt │ └── main.c │ ├── week6 │ ├── A.txt │ ├── B.txt │ ├── C.txt │ ├── README.md │ └── main.c │ ├── week7 │ ├── README.md │ └── main.c │ └── week8 │ ├── README.md │ ├── input.txt │ └── main.c ├── LICENSE ├── MAT2410 Applied Mathematics I ├── README.md ├── homework-1 │ ├── README.md │ ├── header.tex │ ├── hw │ │ ├── 1-1.tex │ │ ├── 1-2.tex │ │ ├── 1-3-diagram.png │ │ ├── 1-3-diagram.svg │ │ ├── 1-3.tex │ │ ├── 2-1.tex │ │ ├── 2-2.tex │ │ ├── 2-3.tex │ │ ├── 3-1.tex │ │ ├── 3-2.tex │ │ ├── 3-3.tex │ │ ├── 4-1.tex │ │ ├── 4-2.tex │ │ ├── 4-3.tex │ │ ├── 4-4.tex │ │ ├── 4-5.tex │ │ ├── 5-1.tex │ │ ├── 5-2.tex │ │ ├── 5-3.tex │ │ ├── 5-4.tex │ │ ├── 7-1.tex │ │ ├── 7-2.tex │ │ ├── 7-3.tex │ │ ├── 7-4.tex │ │ ├── 8-1.tex │ │ ├── 8-2.tex │ │ ├── 8-3.tex │ │ ├── 9-2.tex │ │ ├── 9-3.tex │ │ └── 9-4.tex │ ├── hw1-1.pdf │ ├── hw1-1.tex │ ├── hw1-2.pdf │ ├── hw1-2.tex │ ├── hw1-3.pdf │ ├── hw1-3.tex │ └── llncs.cls ├── midterm │ ├── header.tex │ ├── llncs.cls │ ├── midterm.pdf │ └── midterm.tex └── note.pdf ├── README.md ├── STS2005 Analytic Geometry and Calculus I ├── README.md └── midterm │ ├── header.tex │ ├── llncs.cls │ ├── midterm.pdf │ └── midterm.tex └── STS2006 Analytic Geometry and Calculus II ├── README.md ├── note.pdf └── quiz ├── README.md ├── header.tex ├── llncs.cls ├── quiz1.pdf ├── quiz1.tex ├── quiz2.pdf ├── quiz2.tex ├── quiz3.pdf ├── quiz3.tex ├── quiz4.pdf ├── quiz4.tex ├── quiz5.pdf ├── quiz5.tex ├── quiz6.pdf ├── quiz6.tex ├── quiz7.pdf └── quiz7.tex /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | custom: ['https://paypal.me/shiftpsh'] # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] 13 | -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/README.md: -------------------------------------------------------------------------------- 1 | # 기초공학설계 2 | 3 | CSE2003 - Introduction to Engineering Design 4 | 5 | 2018년 1학기 / 김주호 교수님 6 | 7 | - C언어 프로그래밍 기초를 배웁니다. 8 | 9 | - [실습 코드](exercise) 10 | - [프로젝트 리포트, 코드](project) 11 | - [중간고사 코드](midterm) (문제 미포함) 12 | - [기말고사 코드](final) (문제 미포함) 13 | 14 | ## Exercise 15 | 16 | - 매 수업마다 과제를 주고 실습을 합니다. 제시문에서 구현하라는 대로 구현하면 됩니다. 17 | - 다소 더러운 문제가 나오긴 합니다. 18 | - 실습 문제를 매년 새로 만드는지는 잘 모르겠습니다. 19 | 20 | ## Exam 21 | 22 | - 시험 문제는 아마도 매년 새로 나옵니다. 족보는 별로 도움이 되지 않고 직접 코드를 짜보는 게 제일 도움이 되는 것 같습니다. 23 | 24 | ## Source Code 25 | 26 | - 첨부된 중간고사 소스코드의 점수는 100점입니다. 기말고사나 프로젝트, 실습 점수는 잘 모릅니다. -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/1-1p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | char a; int b; float c; 5 | 6 | printf("Input one character, one integer, one real number : "); 7 | scanf("%c %d %f", &a, &b, &c); 8 | printf("%c %d %f\n", a, b, c); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/10p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void swap (float *x, float *y); 4 | 5 | int main(void) { 6 | float x, y; 7 | 8 | printf("Input two number\n"); 9 | printf(" x : "); 10 | scanf("%f", &x); 11 | printf(" y : "); 12 | scanf("%f", &y); 13 | 14 | printf("------------------------------------\n"); 15 | printf("Before : x [%f] y [%f]\n", x, y); 16 | printf("------------------------------------\n"); 17 | swap(&x, &y); 18 | printf("After : x [%f] y [%f]\n", x, y); 19 | 20 | return 0; 21 | } 22 | 23 | void swap (float *x, float *y) { 24 | float temp = *x; 25 | 26 | *x = *y; 27 | *y = temp; 28 | return; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/11p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int a, b; 4 | 5 | void fourMath (int *c, int *d) ; 6 | void printResult (int *c, int *d); 7 | 8 | int main(void) { 9 | int a, b; 10 | 11 | printf("Input two number : "); 12 | scanf("%d %d", &a, &b); 13 | 14 | fourMath(&a, &b); 15 | printResult(&a, &b); 16 | 17 | return 0; 18 | } 19 | 20 | void fourMath (int *c, int *d) { 21 | // *c = main.&a 22 | // *d = main.&b 23 | 24 | a = *c + *d; 25 | b = *c - *d; 26 | 27 | *c = *c * *d; 28 | 29 | // *c = a * b 30 | // *d = a / b 31 | // = a * b / b / b 32 | // = *c / *d / *d 33 | *d = *c / *d / *d; 34 | 35 | return; 36 | } 37 | 38 | void printResult(int *c, int *d) { 39 | // a = a + b 40 | // b = a - b 41 | // *c = a * b 42 | // *d = a / b 43 | 44 | printf("Result:\n"); 45 | printf("a + b = %10d\n", a); 46 | printf("a - b = %10d\n", b); 47 | printf("a * b = %10d\n", *c); 48 | printf("a / b = %10d\n", *d); 49 | 50 | return; 51 | } 52 | -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/12p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int x = 1; 4 | 5 | int fibonacci (int *y); 6 | 7 | int main(void) { 8 | int x = 0; 9 | 10 | printf("f(1) = %5d\n", fibonacci(&x)); 11 | printf("f(2) = %5d\n", fibonacci(&x)); 12 | printf("f(3) = %5d\n", fibonacci(&x)); 13 | printf("f(4) = %5d\n", fibonacci(&x)); 14 | printf("f(5) = %5d\n", fibonacci(&x)); 15 | printf("f(6) = %5d\n", fibonacci(&x)); 16 | printf("f(7) = %5d\n", fibonacci(&x)); 17 | printf("f(8) = %5d\n", fibonacci(&x)); 18 | printf("f(9) = %5d\n", fibonacci(&x)); 19 | 20 | return 0; 21 | } 22 | 23 | int fibonacci (int *y) { 24 | int temp = x; 25 | 26 | x += *y; 27 | *y = temp; 28 | return x; 29 | } 30 | -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/13p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | float f; 5 | 6 | printf("Input : "); 7 | scanf("%f", &f); 8 | 9 | // round(f) = (int) (f + 0.5) 10 | // floor(f) = (int) f 11 | 12 | // if round(f) > floor(f): 13 | printf("Use \">\" : %d\n", (int)(f + 0.5) > (int)f ); 14 | 15 | // if round(f) - floor(f) >= 1: 16 | printf("Use \">=\" : %d\n", (int)(f + 0.5) - (int)f >= 1 ); 17 | 18 | // if round(f) - floor(f) == 1: 19 | printf("Use \"==\" : %d\n", (int)(f + 0.5) - (int)f == 1 ); 20 | 21 | // if round(f) != floor(f): 22 | printf("Use \"!=\" : %d\n", (int)(f + 0.5) != (int)f ); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/14p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int year; 5 | 6 | printf("Enter the year to be tested : "); 7 | scanf("%d", &year); 8 | 9 | int flag = 0; // 1 if leap, 0 if not leap 10 | 11 | if (year % 4 == 0) flag = 1; 12 | if (year % 100 == 0) flag = 0; 13 | if (year % 400 == 0) flag = 1; 14 | 15 | if (flag) { 16 | printf("Year %d is a leap year.\n", year); 17 | } else { 18 | printf("Year %d is NOT a leap year.\n", year); 19 | 20 | int lastLeapYear = (year / 4) * 4; 21 | if (lastLeapYear % 100 == 0 && lastLeapYear % 400 != 0) lastLeapYear -= 4; 22 | 23 | printf("Last leap year was year %d.\n", lastLeapYear); 24 | } 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/15p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int n; 5 | 6 | printf("Input : "); 7 | scanf("%d", &n); 8 | 9 | printf(n % 2 ? "%d is odd number.\n" : "%d is even number.\n", n); 10 | 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/16p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int a, b; 5 | 6 | printf("Enter 2 numbers : "); 7 | scanf("%d%d", &a, &b); 8 | 9 | int carryOperations = 0; 10 | 11 | if (a % 10 + b % 10 >= 10) { 12 | carryOperations++; 13 | } 14 | 15 | if (a % 100 + b % 100 >= 100) { 16 | carryOperations++; 17 | } 18 | 19 | if (a + b >= 1000) { 20 | carryOperations++; 21 | } 22 | 23 | printf(carryOperations == 1 ? "%d carry operation needed.\n" : "%d carry operations needed.\n", carryOperations); 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/17p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | float avgFunc(float a, float b, float c); 4 | char gradeFunc(float score); 5 | 6 | int main(void) { 7 | float a, b, c; 8 | 9 | printf("Input: "); 10 | scanf("%f%f%f", &a, &b, &c); 11 | printf("Grade: %c\n", gradeFunc(avgFunc(a, b, c))); 12 | scanf("%f", &a); 13 | 14 | return 0; 15 | } 16 | 17 | float avgFunc(float a, float b, float c) { 18 | return (a + b + c) / 3; 19 | } 20 | 21 | char gradeFunc(float score) { 22 | if (score >= 50) { 23 | if (score >= 70) { 24 | if (score >= 80) { 25 | if (score >= 90) { 26 | return 'A'; 27 | } else { 28 | return 'B'; 29 | } 30 | } else { 31 | return 'C'; 32 | } 33 | } else { 34 | return 'D'; 35 | } 36 | } else { 37 | return 'F'; 38 | } 39 | 40 | /* 41 | * Also can be achieved 42 | * w/out else branches: 43 | 44 | if (score >= 90) return 'A'; 45 | if (score >= 80) return 'B'; 46 | if (score >= 70) return 'C'; 47 | if (score >= 50) return 'D'; 48 | return 'F'; 49 | 50 | */ 51 | } -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/18p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void calculate(char op, float a, float b); 4 | void addition(float a, float b); 5 | void subtraction(float a, float b); 6 | void multiplication(float a, float b); 7 | void division(float a, float b); 8 | 9 | int main(void) { 10 | float a, b; 11 | char op; 12 | 13 | printf("Operator (+, -, *, /)\n"); 14 | printf("Please enter the formula. (ex : 12.3 + 5.5)\n"); 15 | scanf("%f %c %f", &a, &op, &b); 16 | calculate(op, a, b); 17 | 18 | return 0; 19 | } 20 | 21 | void calculate(char op, float a, float b) { 22 | switch (op) { 23 | case '+': 24 | addition(a, b); 25 | break; 26 | case '-': 27 | subtraction(a, b); 28 | break; 29 | case '*': 30 | multiplication(a, b); 31 | break; 32 | case '/': 33 | division(a, b); 34 | break; 35 | default: 36 | printf("Invalid operator : '%c'.\n", op); 37 | break; 38 | } 39 | 40 | return; 41 | } 42 | 43 | void addition(float a, float b) { 44 | printf("%.2f + %.2f = %.2f\n", a, b, a + b); 45 | return; 46 | } 47 | 48 | void subtraction(float a, float b) { 49 | printf("%.2f - %.2f = %.2f\n", a, b, a - b); 50 | return; 51 | } 52 | 53 | void multiplication(float a, float b) { 54 | printf("%.2f * %.2f = %.2f\n", a, b, a * b); 55 | return; 56 | } 57 | 58 | void division(float a, float b) { 59 | switch (b == 0) { 60 | case 0: // b != 0 61 | printf("%.2f / %.2f = %.2f\n", a, b, a / b); 62 | break; 63 | case 1: // b == 0 64 | printf("Division by zero.\n"); 65 | break; 66 | } 67 | return; 68 | } -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/19p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int absValue(int n); 4 | void swap(int* a, int* b); 5 | 6 | int main(void) { 7 | int a, b, c; 8 | 9 | printf("Input: "); 10 | scanf("%d%d%d", &a, &b, &c); 11 | 12 | a = absValue(a); 13 | b = absValue(b); 14 | c = absValue(c); 15 | 16 | if (a < c) { 17 | swap(&a, &c); 18 | } 19 | 20 | if (b < c) { 21 | swap(&b, &c); 22 | } 23 | 24 | if (a < b) { 25 | swap(&a, &b); 26 | } 27 | 28 | printf("Result: %d %d %d\n", a, b, c); 29 | 30 | return 0; 31 | } 32 | 33 | int absValue(int n) { 34 | if (n < 0) { 35 | return -n; 36 | } else { 37 | return n; 38 | } 39 | } 40 | 41 | void swap(int* a, int* b) { 42 | int temp = *a; 43 | *a = *b; 44 | *b = temp; 45 | return; 46 | } -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/1p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | printf("Hello, World!\n"); 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/20p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int n, x, s; 5 | int idx = 0, sum = 0, fails = 0; 6 | printf("Number of class : "); 7 | scanf("%d", &n); 8 | printf("Cutline : "); 9 | scanf("%d", &x); 10 | 11 | while (idx < n) { 12 | printf("Input score #%d : ", idx); 13 | scanf("%d", &s); 14 | 15 | sum += s; 16 | if (s < x) { 17 | fails++; 18 | } 19 | 20 | idx++; 21 | } 22 | 23 | if (n != 0) { 24 | printf("Average score : %.2f\n", (double) sum / n); 25 | printf("Number of failures : %d\n", fails); 26 | } else { 27 | printf("Average score : %.2f\n", 0.0); 28 | printf("Number of failures : %d\n", 0); 29 | } 30 | 31 | return 0; 32 | } -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/21p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int n, y = 0; 5 | printf("Size : "); 6 | scanf("%d", &n); 7 | 8 | while (y < n) { 9 | int asterisks = y * 2 + 1; 10 | int spaces = n - y; 11 | 12 | int aidx = 0; 13 | int sidx = 0; 14 | 15 | while (sidx < spaces) { 16 | printf(" "); 17 | sidx++; 18 | } 19 | 20 | while (aidx < asterisks) { 21 | printf("*"); 22 | aidx++; 23 | } 24 | 25 | printf("\n"); 26 | y++; 27 | } 28 | 29 | return 0; 30 | } -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/22p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void swap(int* a, int* b); 4 | int sumInRange(int a, int b); 5 | 6 | int main(void) { 7 | int a, b; 8 | 9 | printf("Input first number : "); 10 | scanf("%d", &a); 11 | printf("Input second number : "); 12 | scanf("%d", &b); 13 | 14 | if (a > b) { 15 | swap(&a, &b); 16 | } 17 | 18 | if (a < 1) { 19 | printf("Input is less than 1\n"); 20 | } else { 21 | printf("Result: %d\n", sumInRange(a, b)); 22 | } 23 | 24 | return 0; 25 | } 26 | 27 | void swap(int* a, int* b) { 28 | int temp = *a; 29 | *a = *b; 30 | *b = temp; 31 | } 32 | 33 | // O(b - a) 34 | int sumInRange(int a, int b) { 35 | int sum = 0; 36 | int i; 37 | for (i = a; i <= b; i++) { 38 | if (i % 2 == 0 || i % 3 == 0) { 39 | sum += i; 40 | } 41 | } 42 | 43 | return sum; 44 | } 45 | 46 | // Also can be achieved in O(1) time w/out for loop: 47 | /* 48 | int sumInRange(int a, int b) { 49 | int a2 = (a - 1) / 2; 50 | int a3 = (a - 1) / 3; 51 | int a6 = (a - 1) / 6; 52 | int b2 = b / 2; 53 | int b3 = b / 3; 54 | int b6 = b / 6; 55 | 56 | int sum_a2 = a2 * (a2 + 1) / 2 * 2; 57 | int sum_a3 = a3 * (a3 + 1) / 2 * 3; 58 | int sum_a6 = a6 * (a6 + 1) / 2 * 6; 59 | int sum_b2 = b2 * (b2 + 1) / 2 * 2; 60 | int sum_b3 = b3 * (b3 + 1) / 2 * 3; 61 | int sum_b6 = b6 * (b6 + 1) / 2 * 6; 62 | 63 | return (sum_b3 + sum_b2 - sum_b6) - (sum_a2 + sum_a3 - sum_a6); 64 | } 65 | */ -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/23p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void func_gcd(int a, int b, int* gcd); 4 | 5 | int main(void) { 6 | int a, b, gcd; 7 | 8 | printf("Input first number : "); 9 | scanf("%d", &a); 10 | printf("Input second number : "); 11 | scanf("%d", &b); 12 | 13 | func_gcd(a, b, &gcd); 14 | 15 | printf("GCD : %d\n", gcd); 16 | return 0; 17 | } 18 | 19 | void func_gcd(int a, int b, int* gcd) { 20 | while (a % b != 0 && b % a != 0) { 21 | if (a > b) { 22 | a %= b; 23 | } else { 24 | b %= a; 25 | } 26 | } 27 | 28 | if (a % b == 0) { 29 | *gcd = b; 30 | } else { 31 | *gcd = a; 32 | } 33 | } -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/24p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int fibo(int n); 4 | 5 | int main() { 6 | int n; 7 | int iterations = 0; 8 | 9 | printf("Input n: "); 10 | scanf("%d", &n); 11 | 12 | if (n >= 0) { 13 | printf("fibo(%d) = %d\n", n, fibo(n)); 14 | } else { 15 | printf("n cannot be a negative number.\n"); 16 | } 17 | 18 | return 0; 19 | } 20 | 21 | int fibo(int n) { 22 | int a = 1, b = 0; 23 | int temp; 24 | int iterations = 0; 25 | 26 | if (n != 0) { 27 | do { 28 | temp = a; 29 | a += b; 30 | b = temp; 31 | 32 | iterations++; 33 | } while (iterations < n); 34 | 35 | return b; 36 | } else { 37 | return 0; 38 | } 39 | } -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/25p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int isDivisor(int m, int n); 4 | int isPrime(int n); 5 | 6 | int main() { 7 | int n, i; 8 | printf("Input : "); 9 | scanf("%d", &n); 10 | 11 | // O(n^2) nooo 12 | for (i = 2; i <= n; i++) { 13 | if (isDivisor(i, n) && isPrime(i)) { 14 | printf("%7d", i); 15 | } 16 | } 17 | 18 | printf("\n"); 19 | 20 | return 0; 21 | } 22 | 23 | int isDivisor(int m, int n) { 24 | if (n % m == 0) { 25 | return 1; 26 | } else { 27 | return 0; 28 | } 29 | } 30 | 31 | int isPrime(int n) { 32 | int flag = 1; 33 | int i; 34 | 35 | for (i = 2; i < n; i++) { 36 | if (isDivisor(i, n)) { 37 | flag = 0; 38 | break; 39 | } 40 | } 41 | 42 | return flag; 43 | } -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/26p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int palindrome(int num, int digit); 4 | 5 | int main() { 6 | int n; 7 | printf("Input : "); 8 | scanf("%d", &n); 9 | 10 | if (n < 0) { 11 | printf("Input number cannot be negative.\n"); 12 | } else if (palindrome(n, 1)) { 13 | printf("%d is a palindrome number.\n", n); 14 | } else { 15 | printf("%d is NOT a palindrome number.\n", n); 16 | } 17 | 18 | return 0; 19 | } 20 | 21 | int palindrome(int num, int digit) { 22 | int len = 1; 23 | int lbase, hbase, ldigit, hdigit; 24 | 25 | if (num != 0) { 26 | // len = 10 ^ (length of num - 1) 27 | while (num > len) { 28 | len *= 10; 29 | } 30 | len /= 10; 31 | } 32 | 33 | lbase = digit; 34 | hbase = len / digit; 35 | 36 | ldigit = (num / lbase) % 10; 37 | hdigit = (num / hbase) % 10; 38 | 39 | if (lbase == hbase) { 40 | // checking center digit if length of n is odd 41 | return 1; 42 | } else if (lbase * 10 == hbase) { 43 | // checking center digit if length of n is even 44 | if (ldigit == hdigit) { 45 | return 1; 46 | } else { 47 | return 0; 48 | } 49 | } else { 50 | if (ldigit == hdigit) { 51 | return palindrome(num, digit * 10); 52 | } else { 53 | return 0; 54 | } 55 | } 56 | } -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/27-1p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int a[5], b[5]; 5 | int i; 6 | int flag = 1; 7 | 8 | for (i = 0; i < 5; i++) { 9 | scanf("%d", &a[i]); 10 | } 11 | 12 | for (i = 0; i < 5; i++) { 13 | scanf("%d", &b[i]); 14 | } 15 | 16 | for (i = 0; i < 5; i++) { 17 | if (a[i] != b[i]) { 18 | flag = 0; 19 | break; 20 | } 21 | } 22 | 23 | if (flag) { 24 | printf("true\n"); 25 | } else { 26 | printf("false\n"); 27 | } 28 | } -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/27-2p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | /* 5 | Since cbrt(2147483647) = 1290.16, 6 | an overflow error will occur when 7 | n >= 1291, thus we need index of 8 | squared and cubed array of at most 1290. 9 | */ 10 | 11 | int MAX_SIZE = 1291; 12 | int n, i; 13 | int sq[1291], cb[1291]; 14 | 15 | scanf("%d", &n); 16 | for (i = 1; i <= n; i++) { 17 | sq[i] = i * i; 18 | cb[i] = i * i * i; 19 | } 20 | 21 | for (i = 1; i <= n; i++) { 22 | printf("%d ", i); 23 | } 24 | printf("\n"); 25 | 26 | for (i = 1; i <= n; i++) { 27 | printf("%d ", sq[i]); 28 | } 29 | printf("\n"); 30 | 31 | for (i = 1; i <= n; i++) { 32 | printf("%d ", cb[i]); 33 | } 34 | printf("\n"); 35 | } -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/27p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int arr[10]; 5 | int i; 6 | int max = -2047483648; 7 | int min = 2147483647; 8 | 9 | for (i = 0; i < 10; i++) { 10 | scanf("%d", &arr[i]); 11 | } 12 | 13 | // Swapping 14 | for (i = 0; i < 5; i++) { 15 | int temp = arr[i]; 16 | arr[i] = arr[9 - i]; 17 | arr[9 - i] = temp; 18 | } 19 | 20 | // Printing & calculating min / max 21 | for (i = 0; i < 10; i++) { 22 | printf("%d ", arr[i]); 23 | if (max < arr[i]) { 24 | max = arr[i]; 25 | } 26 | if (min > arr[i]) { 27 | min = arr[i]; 28 | } 29 | } 30 | printf("\n"); 31 | 32 | printf("Max: %d, Min: %d\n", max, min); 33 | } -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/28p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void toUpperCase(char str[]); 4 | 5 | int main() { 6 | char str[50]; 7 | int i, l = 50; 8 | 9 | for (i = 0; i < 50; i++) { 10 | scanf("%c", &str[i]); 11 | if (str[i] == '\n') { 12 | l = i; 13 | break; 14 | } 15 | } 16 | 17 | toUpperCase(str); 18 | 19 | for (i = 0; i < l; i++) { 20 | printf("%c", str[i]); 21 | } 22 | 23 | printf("\n"); 24 | } 25 | 26 | void toUpperCase(char str[]) { 27 | int i; 28 | 29 | for (i = 0; i < 50; i++) { 30 | if ('a' <= str[i] && str[i] <= 'z') { 31 | str[i] -= 0x20; 32 | } 33 | } 34 | 35 | return; 36 | } -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/29Ep1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void multiply(int matrix[3][4][4]); 4 | 5 | int main() { 6 | int matrix[3][4][4], x, y; 7 | 8 | printf("First matrix:\n"); 9 | for (x = 0; x < 4; x++) for (y = 0; y < 4; y++) { 10 | scanf("%d", &matrix[0][x][y]); 11 | } 12 | 13 | printf("Second matrix:\n"); 14 | for (x = 0; x < 4; x++) for (y = 0; y < 4; y++) { 15 | scanf("%d", &matrix[1][x][y]); 16 | } 17 | 18 | multiply(matrix); 19 | printf("Result:\n"); 20 | 21 | for (x = 0; x < 4; x++) { 22 | for (y = 0; y < 4; y++) { 23 | printf("%8d", matrix[2][x][y]); 24 | } 25 | printf("\n"); 26 | } 27 | 28 | return 0; 29 | } 30 | 31 | void multiply(int matrix[3][4][4]) { 32 | int x, y, i; 33 | 34 | for (x = 0; x < 4; x++) for (y = 0; y < 4; y++) { 35 | matrix[2][x][y] = 0; 36 | for (i = 0; i < 4; i++) { 37 | matrix[2][x][y] += matrix[0][x][i] * matrix[1][i][y]; 38 | } 39 | } 40 | 41 | return; 42 | } -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/29p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | float average(int matrix[100][100], int col, int n); 4 | 5 | int main() { 6 | int matrix[100][100], n, x, y; 7 | 8 | scanf("%d", &n); 9 | 10 | for (x = 0; x < n; x++) for (y = 0; y < n; y++) { 11 | scanf("%d", &matrix[x][y]); 12 | } 13 | 14 | for (x = 0; x < n; x++) { 15 | printf("%.2f ", average(matrix, x, n)); 16 | } 17 | 18 | return 0; 19 | } 20 | 21 | float average(int matrix[100][100], int col, int n) { 22 | int sum = 0, x; 23 | 24 | for (x = 0; x < n; x++) { 25 | sum += matrix[x][col]; 26 | } 27 | 28 | return (float) sum / n; 29 | } -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/2p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int a; 5 | 6 | printf("Input a: "); 7 | scanf("%d", &a); 8 | 9 | printf("a = %d\n", a); 10 | printf("a = a + 1; a = %d\n", ++a); 11 | printf("a = a + 1; a = %d\n", ++a); 12 | printf("a = a - 1; a = %d\n", --a); 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/2p2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int a; 5 | 6 | printf("Input a: "); 7 | scanf("%d", &a); 8 | 9 | printf("a = %d\n", a++); 10 | printf("a = a + 1; a = %d\n", a++); 11 | printf("a = a + 1; a = %d\n", a--); 12 | printf("a = a - 1; a = %d\n", a); 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/30-1p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void snail(int snail[20][20], int n); 4 | void print_snail(int snail[20][20], int n); 5 | 6 | int main() { 7 | int arr[20][20], i, n; 8 | 9 | scanf("%d", &n); 10 | 11 | for (i = n; i > 0; i--) { 12 | snail(arr, i); 13 | print_snail(arr, i); 14 | printf("\n"); 15 | } 16 | 17 | return 0; 18 | } 19 | 20 | void snail(int arr[20][20], int n) { 21 | int dx[4] = {0, 1, 0, -1}; // directions of x 22 | int dy[4] = {1, 0, -1, 0}; // directions of y 23 | int d = 0, x = 0, y = 0, i, j, nx, ny; 24 | 25 | for (i = 0; i < n; i++) for (j = 0; j < n; j++) { 26 | arr[i][j] = -1; // Initalize array 27 | } 28 | 29 | for (i = 1; i <= n * n; i++) { 30 | arr[x][y] = i; 31 | 32 | nx = x + dx[d]; 33 | ny = y + dy[d]; 34 | 35 | if ((nx == -1 || nx == n) || (ny == -1 || ny == n) // new (x, y) out of array bounds 36 | || (arr[nx][ny] != -1)) { // (x, y) already calculated 37 | d++; 38 | d %= 4; 39 | } 40 | 41 | x += dx[d]; 42 | y += dy[d]; 43 | } 44 | } 45 | 46 | void print_snail(int snail[20][20], int n) { 47 | int x, y; 48 | for (x = 0; x < n; x++) { 49 | for (y = 0; y < n; y++) { 50 | printf("%4d ", snail[x][y]); 51 | } 52 | printf("\n"); 53 | } 54 | } -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/30p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | double parseDouble(char str[10]); 4 | 5 | int main() { 6 | char str[10]; 7 | int i; 8 | 9 | for (i = 0; i < 10; i++) { 10 | scanf("%c", &str[i]); 11 | if (str[i] == '\n') break; 12 | } 13 | 14 | printf("Result: %lf\n", parseDouble(str)); 15 | 16 | return 0; 17 | } 18 | 19 | double parseDouble(char str[10]) { 20 | int i, l = 9; // l = last index of str 21 | double res = 0; 22 | int sign = 0, dotPosition = -1; 23 | 24 | for (i = 0; i < 10; i++) { 25 | char c = str[i]; 26 | 27 | if (c == '-') { 28 | sign = -1; 29 | } else if (c == '.') { 30 | dotPosition = i; 31 | } else if ('0' <= c && c <= '9') { 32 | res *= 10; 33 | res += c - '0'; 34 | } else { 35 | l = i - 1; 36 | break; 37 | } 38 | } 39 | 40 | if (dotPosition != -1) { 41 | while (dotPosition < l) { 42 | res /= 10; 43 | dotPosition++; 44 | } 45 | } 46 | 47 | if (sign) res *= -1; 48 | 49 | return res; 50 | } -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/31p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | FILE *aFile, *bFile, *outFile; 5 | char aName[100], bName[100], aChar = ' ', bChar = ' '; 6 | int chars = 0, same = 0; 7 | 8 | printf("Input first filename : "); 9 | scanf("%s", &aName); 10 | printf("Input second filename : "); 11 | scanf("%s", &bName); 12 | 13 | aFile = fopen(aName, "r"); 14 | bFile = fopen(bName, "r"); 15 | outFile = fopen("output", "w"); 16 | 17 | if (aFile == NULL) { 18 | printf("Cannot read first file.\n"); 19 | return 1; 20 | } 21 | 22 | if (bFile == NULL) { 23 | printf("Cannot read second file.\n"); 24 | return 1; 25 | } 26 | 27 | if (outFile == NULL) { 28 | printf("Cannot write to storage.\n"); 29 | return 1; 30 | } 31 | 32 | while (1) { 33 | aChar = fgetc(aFile); 34 | while (aChar == ' ' || aChar == '\n') aChar = fgetc(aFile); 35 | if (aChar == EOF) break; 36 | 37 | bChar = fgetc(bFile); 38 | while (bChar == ' ' || bChar == '\n') bChar = fgetc(bFile); 39 | if (bChar == EOF) break; 40 | 41 | chars++; 42 | if (aChar == bChar) same++; 43 | } 44 | 45 | fprintf(outFile, "Total characters : %d\n", chars); 46 | fprintf(outFile, "Same characters : %d\n", same); 47 | fprintf(outFile, "%.3f%%\n", (float) same / chars * 100); 48 | 49 | fclose(aFile); 50 | fclose(bFile); 51 | fclose(outFile); 52 | 53 | return 0; 54 | } -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/32p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | FILE *inFile, *outFile; 5 | double a, b, c, result; 6 | char op, _; 7 | int res; 8 | 9 | inFile = fopen("input32.txt", "r"); 10 | outFile = fopen("output32.txt", "w"); 11 | 12 | if (inFile == NULL) { 13 | printf("Cannot read file.\n"); 14 | return 1; 15 | } 16 | 17 | if (outFile == NULL) { 18 | printf("Cannot write file.\n"); 19 | return 1; 20 | } 21 | 22 | while (1) { 23 | res = fscanf(inFile, "%lf %c %lf %c %lf", &a, &op, &b, &_, &c); 24 | if (res == EOF) break; 25 | 26 | switch (op) { 27 | case '+': result = a + b; break; 28 | case '-': result = a - b; break; 29 | case '*': result = a * b; break; 30 | case '/': result = a / b; break; 31 | case '%': result = (int) a % (int) b; break; 32 | } 33 | 34 | if (c == result) { 35 | fprintf(outFile, "%.2lf %c %.2lf = %.2lf correct\n", a, op, b, c); 36 | } else { 37 | fprintf(outFile, "%.2lf %c %.2lf = %.2lf incorrect\n", a, op, b, c); 38 | } 39 | } 40 | 41 | fclose(inFile); 42 | fclose(outFile); 43 | 44 | return 0; 45 | } -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/33p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | FILE *inFile, *outFile; 5 | char name[20]; 6 | float midtermScore, finalScore, sum = 0.0, average, totalAverage; 7 | int res, count = 0; 8 | 9 | inFile = fopen("student.txt", "r"); 10 | outFile = fopen("output33.txt", "w"); 11 | 12 | if (inFile == NULL) { 13 | printf("Cannot read file.\n"); 14 | return 1; 15 | } 16 | 17 | if (outFile == NULL) { 18 | printf("Cannot write file.\n"); 19 | return 1; 20 | } 21 | 22 | while (1) { 23 | res = fscanf(inFile, "%s%f%f", &name, &midtermScore, &finalScore); 24 | if (res == EOF) break; 25 | 26 | sum += midtermScore; 27 | sum += finalScore; 28 | count += 2; 29 | } 30 | 31 | totalAverage = sum / count; 32 | fclose(inFile); 33 | 34 | inFile = fopen("student.txt", "r"); 35 | 36 | fprintf(outFile, "student average grade\n"); 37 | 38 | while (1) { 39 | res = fscanf(inFile, "%s%f%f", &name, &midtermScore, &finalScore); 40 | if (res == EOF) break; 41 | 42 | average = (midtermScore + finalScore) / 2; 43 | 44 | if (average >= totalAverage) { 45 | fprintf(outFile, "%-8s %6.2f p\n", name, average); 46 | } else { 47 | fprintf(outFile, "%-8s %6.2f f\n", name, average); 48 | } 49 | } 50 | 51 | fprintf(outFile, "Total average: %.2f\n", totalAverage); 52 | 53 | fclose(inFile); 54 | fclose(outFile); 55 | 56 | return 0; 57 | } -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/35p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int n, pascal[20][20], h, x; 5 | printf("Input size of triangle : "); 6 | scanf("%d", &n); 7 | 8 | for (h = 0; h < n; h++) for (x = 0; x <= h; x++) { 9 | if (x == 0 || x == h) { 10 | pascal[h][x] = 1; 11 | } else { 12 | pascal[h][x] = pascal[h - 1][x] + pascal[h - 1][x - 1]; 13 | } 14 | } 15 | 16 | for (h = 0; h < n; h++) { 17 | for (x = 0; x <= h; x++) { 18 | printf("%d ", pascal[h][x]); 19 | } 20 | printf("\n"); 21 | } 22 | 23 | return 0; 24 | } -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/36p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int n, x, y, h; 5 | printf("Input : "); 6 | scanf("%d", &n); 7 | 8 | if (n % 2 == 0) { 9 | for (x = 0; x < n; x++) { 10 | if (x < n / 2) { 11 | h = n / 2 - x; 12 | } else { 13 | h = x - n / 2 + 1; 14 | } 15 | 16 | 17 | for (y = 0; y < h; y++) printf("*"); 18 | for (y = 0; y < n - 2 * h; y++) printf(" "); 19 | for (y = 0; y < h; y++) printf("*"); 20 | printf("\n"); 21 | } 22 | } else { 23 | for (x = 0; x < n; x++) { 24 | if (x == 0 || x == n - 1) { 25 | for (y = 0; y < n; y++) printf("*"); 26 | } else { 27 | if (x < n / 2) { 28 | h = n / 2 - x + 1; 29 | } else { 30 | h = x - n / 2 + 1; 31 | } 32 | 33 | for (y = 0; y < h; y++) printf("*"); 34 | for (y = 0; y < n - 2 * h; y++) printf(" "); 35 | for (y = 0; y < h; y++) printf("*"); 36 | } 37 | printf("\n"); 38 | } 39 | } 40 | 41 | return 0; 42 | } -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/37p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void merge(int arrayA[], int arrayB[], int arrayMerge[]); 4 | 5 | int main() { 6 | int arrayA[5], arrayB[5], arrayMerge[10], i; 7 | 8 | printf("Input array A : "); 9 | for (i = 0; i < 5; i++) { 10 | scanf("%d", &arrayA[i]); 11 | } 12 | 13 | printf("Input array B : "); 14 | for (i = 0; i < 5; i++) { 15 | scanf("%d", &arrayB[i]); 16 | } 17 | 18 | merge(arrayA, arrayB, arrayMerge); 19 | 20 | printf("Merged array : "); 21 | for (i = 0; i < 10; i++) { 22 | printf("%d ", arrayMerge[i]); 23 | } 24 | printf("\n"); 25 | 26 | return 0; 27 | } 28 | 29 | void merge(int arrayA[], int arrayB[], int arrayMerge[]) { 30 | int aidx = 0, bidx = 0, i; 31 | 32 | for (i = 0; i < 10; i++) { 33 | if (aidx < 5 && bidx < 5) { 34 | if (arrayA[aidx] > arrayB[bidx]) { 35 | arrayMerge[i] = arrayB[bidx]; 36 | bidx++; 37 | } else { 38 | arrayMerge[i] = arrayA[aidx]; 39 | aidx++; 40 | } 41 | } else if (aidx < 5) { 42 | arrayMerge[i] = arrayA[aidx]; 43 | aidx++; 44 | } else { 45 | arrayMerge[i] = arrayB[bidx]; 46 | bidx++; 47 | } 48 | } 49 | 50 | return; 51 | } 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/39p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | FILE* in; 5 | int ref[26], i, c; 6 | 7 | for (i = 0; i < 26; i++) ref[i] = 0; 8 | 9 | in = fopen("input_39.txt", "r"); 10 | 11 | if (in == NULL) { 12 | printf("File open error.\n"); 13 | return 1; 14 | } 15 | 16 | while (1) { 17 | c = fgetc(in); 18 | if (c == EOF) break; 19 | 20 | if ('A' <= c && c <= 'Z') ref[c - 0x41]++; 21 | if ('a' <= c && c <= 'z') ref[c - 0x61]++; 22 | } 23 | 24 | fclose(in); 25 | 26 | for (i = 0; i < 26; i++) { 27 | printf("%c: %4d ", (char)(i + 0x41), ref[i]); 28 | } 29 | 30 | printf("\n"); 31 | 32 | return 0; 33 | } -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/3p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void) { 3 | int a, b; 4 | printf("Input two integer: "); 5 | scanf("%d %d", &a, &b); 6 | printf("%d / %d is %d with a remainder of %d\n", a, b, a / b, a % b); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/4-1p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int birthday; 5 | printf("Birthday : "); 6 | scanf("%d", &birthday); 7 | 8 | int year = birthday / 10000; 9 | int month = birthday % 10000 / 100; 10 | int date = birthday % 100; 11 | 12 | printf("Your birthday is %d / %d / %d\n", year, month, date); 13 | 14 | int preferredYear; 15 | printf("Year : "); 16 | scanf("%d", &preferredYear); 17 | 18 | printf("In %d, your age is %d\n", preferredYear, preferredYear - year + 1); 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/42p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int check (char binary[]); 4 | int biToDec (char binary[]); 5 | 6 | int main() { 7 | char binary[100]; 8 | 9 | printf("Enter binary number : "); 10 | scanf("%s", binary); 11 | 12 | if (check(binary)) { 13 | printf("Decimal number : %d\n", biToDec(binary)); 14 | } else { 15 | printf("Invalid binary number.\n"); 16 | return 1; 17 | } 18 | return 0; 19 | } 20 | 21 | int check (char binary[]) { 22 | int i, flag = 1; 23 | 24 | for (i = 0; i < 100; i++) { 25 | if (binary[i] != '1' && 26 | binary[i] != '0' && 27 | binary[i] != '\0') { 28 | flag = 0; 29 | break; 30 | } 31 | if (binary[i] == '\0') { 32 | break; 33 | } 34 | } 35 | 36 | return flag; 37 | } 38 | 39 | int biToDec (char binary[]) { 40 | int res = 0; 41 | int i; 42 | for (i = 0; i < 100; i++) { 43 | if (binary[i] == '1') { 44 | res *= 2; 45 | res += 1; 46 | } else if (binary[i] == '0') { 47 | res *= 2; 48 | } else if (binary[i] == '\0') { 49 | break; 50 | } 51 | } 52 | 53 | return res; 54 | } -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/4p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int price; 5 | printf("Price : "); 6 | scanf("%d", &price); 7 | 8 | int payment3 = price; 9 | int payment6 = price; 10 | 11 | for (int i = 0; i < 3; i++) { 12 | payment6 *= 1.05; 13 | } 14 | 15 | printf("----------------output----------------\n"); 16 | printf("Month\t\t: %10d%10d\n", 3, 6); 17 | printf("Payment\t\t: %10d%10d\n", payment3, payment6); 18 | printf("Pay/month\t: %10.2f%10.2f\n", (float) payment3 / 3, (float) payment6 / 6); 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/5p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void sqr(int n); 4 | void printOne(int n); 5 | 6 | int main(void) { 7 | int n; 8 | printf("Input: "); 9 | scanf("%d", &n); 10 | sqr(n); 11 | 12 | return 0; 13 | } 14 | 15 | void sqr(int n) { 16 | printOne(n * n); 17 | return; 18 | } 19 | 20 | void printOne(int n) { 21 | printf("Result: %d\n", n); 22 | return; 23 | } 24 | -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/6p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int add(int a, int b); 4 | 5 | int main(void) { 6 | int a, b; 7 | printf("Input first number: "); 8 | scanf("%d", &a); 9 | printf("Input second number: "); 10 | scanf("%d", &b); 11 | 12 | printf("%d + %d = %d\n", a, b, add(a, b)); 13 | return 0; 14 | } 15 | 16 | int add(int a, int b) { 17 | return a + b; 18 | } 19 | -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/7p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | float x(int a, int b, int c, int d); 4 | float y(int a, int b, int c, int d); 5 | 6 | int main(void) { 7 | int a, b, c, d; 8 | 9 | printf("1st equation: y = ax - b, input 'a' and 'b'\n"); 10 | printf("a = "); 11 | scanf("%d", &a); 12 | printf("b = "); 13 | scanf("%d", &b); 14 | 15 | printf("2nd equation: y = cx - d, input 'c' and 'd'\n"); 16 | printf("c = "); 17 | scanf("%d", &c); 18 | printf("d = "); 19 | scanf("%d", &d); 20 | 21 | printf("Result:\n"); 22 | printf("x is %f\n", x(a, b, c, d)); 23 | printf("y is %f\n", y(a, b, c, d)); 24 | 25 | return 0; 26 | } 27 | 28 | float x(int a, int b, int c, int d) { 29 | /** 30 | * ax - b = cx - d 31 | * (a - c)x = b - d 32 | * x = (b - d) / (a - c) 33 | */ 34 | 35 | return (float)(b - d) / (float)(a - c); 36 | } 37 | 38 | float y(int a, int b, int c, int d) { 39 | /** 40 | * (y + b) / a = (y + d) / c 41 | * cy + bc = ay + ad 42 | * (c - a)y = ad - bc 43 | * y = (ad - bc) / (c - a) 44 | */ 45 | 46 | return (float)(a * d - b * c) / (float)(c - a); 47 | } 48 | 49 | -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/8p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | float average(int a, int b, int c, int d); 4 | float deviation(int n, float average); 5 | 6 | int main(void) { 7 | int a, b, c, d; 8 | 9 | printf("Enter the first number : "); 10 | scanf("%d", &a); 11 | printf("Enter the second number : "); 12 | scanf("%d", &b); 13 | printf("Enter the third number : "); 14 | scanf("%d", &c); 15 | printf("Enter the fourth number : "); 16 | scanf("%d", &d); 17 | 18 | float av = average(a, b, c, d); 19 | 20 | printf("******** Average is %6.2f ********\n", av); 21 | printf("First number : %10d -- Deviation: %10.2f\n", a, deviation(a, av)); 22 | printf("Second number : %10d -- Deviation: %10.2f\n", b, deviation(b, av)); 23 | printf("Third number : %10d -- Deviation: %10.2f\n", c, deviation(c, av)); 24 | printf("Fourth number : %10d -- Deviation: %10.2f\n", d, deviation(d, av)); 25 | 26 | return 0; 27 | } 28 | 29 | float average(int a, int b, int c, int d) { 30 | return (float)(a + b + c + d) / 4; 31 | } 32 | 33 | float deviation(int n, float average) { 34 | return n - average; 35 | } 36 | -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/9p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void divideRemainder (int *a, int *b); 4 | 5 | int main(void) { 6 | int a, b; 7 | 8 | printf("Input two number : "); 9 | scanf("%d %d", &a, &b); 10 | 11 | divideRemainder(&a, &b); 12 | 13 | printf("OUTPUT : divide [%d] remainder [%d]\n", a, b); 14 | return 0; 15 | } 16 | 17 | void divideRemainder (int *a, int *b) { 18 | int quotient = *a / *b; 19 | int remainder = *a % *b; 20 | 21 | *a = quotient; 22 | *b = remainder; 23 | return; 24 | } 25 | -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/README.md: -------------------------------------------------------------------------------- 1 | # Exercise List 2 | 3 | ## Introduction to C / Basic Input and Output 4 | 5 | - [#1](1p1.c): `Hello World!` 출력 6 | - [#1-1](1-1p1.c): `char` 하나, `int` 하나, `float` 하나를 입력받아 그대로 출력 7 | - [#2-1](2p1.c), [#2-2](2p1.c): Unary operation(`++`, `--`)을 사용한 계산 8 | - [#3](3p1.c): Modulo 연산자(`%`)를 사용한 몫과 나머지 계산 9 | - [#4](4p1.c): `float`를 이용한 연산 (할부 납입금 비교하기) 10 | - [#4-1](4-1p1.c): 8자리 생일과 연도를 입력받아 몇 살이 되는지 계산 11 | - [#a-1](ap1.c): 섭씨 온도를 화씨로 변환 12 | - [#a-2](ap2.c): 글자의 ASCII 코드를 출력 13 | 14 | ## Functions 15 | 16 | - [#5](5p1.c): 함수를 이용한 제곱 계산과 출력 17 | - [#6](6p1.c): 함수를 이용한 덧셈 18 | - [#7](7p1.c): 함수를 이용한 이원 연립일차방정식 계산 19 | - [#8](8p1.c): 함수를 이용한 평균 및 분산 계산 20 | - [#9](9p1.c): Call by reference로 몫과 나머지 계산 21 | - [#10](10p1.c): Call by reference로 두 변수 swap 22 | - [#11](11p1.c): Call by reference로 사칙연산 수행 (요구조건 더러움) 23 | - [#12](12p1.c): Call by reference로 피보나치 수열 생성 24 | 25 | ## Conditional Statements 26 | 27 | - [#13](13p1.c): `>`, `>=`, `==`, `!=`을 반드시 사용해 어떤 실수가 반올림했을 때 올라간다면 1, 내려간다면 0을 판정 28 | - [#14](14p1.c): 윤년 판정 29 | - [#15](15p1.c): 삼항 연산자(`?:`)를 사용한 홀수 짝수 판정 30 | - [#16](16p1.c): 세 자리수 정수의 덧셈에서 받아올림 횟수 계산 31 | - [#a-3](ap3.c): 10진수를 입력받아 8진수, 16진수로 각각 변환 후 출력 32 | - [#17](17p1.c): 점수 3개를 입력받아 평균을 구하고 학점 판정 33 | - [#18](18p1.c): 일정한 형태의 식을 입력받아 계산 34 | - [#19](19p1.c): 수 3개의 절대값을 내림차순 정렬 35 | 36 | ## Loops 37 | 38 | - [#20](20p1.c): `while` 루프를 이용해 학생 수와 커트라인을 입력받아 평균 점수와 낙제 학생 계산 39 | - [#21](21p1.c): `while` 루프를 이용해 원하는 크기의 크리스마스 트리 출력 40 | - [#22](22p1.c): `for` 루프를 이용해 주어진 범위 안의 2의 배수 혹은 3의 배수를 전부 더해 출력 41 | - [#23](23p1.c): 최대공약수 계산 42 | - [#24](24p1.c): `do`-`while` 루프를 이용해 n번째 피보나치 수열 계산 43 | - [#25](25p1.c): n의 소인수 전부 출력 44 | 45 | ## Recursive Functions 46 | 47 | - [#26](26p1.c): 어떤 정수가 팰린드롬인지 재귀함수를 이용해 판정 48 | 49 | ## Arrays and Strings 50 | 51 | - [#27](27p1.c): 크기가 10인 배열을 입력받아 최댓값과 최솟값 출력 52 | - [#27-1](27-1p1.c): 크기가 5인 배열 2개를 입력받아 배열 두 개가 완전히 같은지 판정 53 | - [#27-2](27-2p1.c): 1부터 n까지를 각각 제곱, 세제곱한 수의 배열을 만들어 출력 54 | - [#28](28p1.c): 문자열을 입력받아 함수를 이용해 소문자를 모두 대문자화시킨 후 출력 55 | - [#29](29p1.c): n*n 행렬을 입력받아 함수를 이용해 각 행의 평균 계산 56 | - [#29E](29Ep1.c): 4*4 행렬 두 개의 곱을 3차원 배열을 이용해 계산 57 | - [#30](30p1.c): 문자열을 실수로 변환 58 | - [#35](35p1.c): 2차원 배열에 파스칼의 삼각형 생성 59 | - [#36](36p1.c): 사각형 안에 다이아몬드 모양 그려 출력 60 | - [#37](37p1.c): 오름차순으로 정렬되어 있는 두 배열을 정렬 상태를 유지하면서 병합 61 | - [#42](42p1.c): 2진수를 10진수로 변환 62 | 63 | ## File Input and Output 64 | 65 | - [#31](31p1.c): 텍스트 파일 두 개의 유사성을 판정 66 | - [#32](32p1.c): 어떤 텍스트 파일에 적혀 있는 식들을 검증하고, 그 결과를 다른 파일에 저장 67 | - [#33](33p1.c): 학생들의 성적 자료 파일을 입력받아 평균과 학점을 계산해 다른 파일에 저장 68 | - [#39](39p1.c): 어떤 텍스트 파일에 알파벳의 각 글자가 몇 번 등장하는지 출력 69 | -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/ap1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | float toFarenheit(float c); 4 | 5 | int main(void) { 6 | float c; 7 | 8 | printf("Enter Temperature in Celcius : "); 9 | scanf("%f", &c); 10 | 11 | printf("Temperature in Farenheit : %.2f\n", toFarenheit(c)); 12 | 13 | return 0; 14 | } 15 | 16 | float toFarenheit(float c) { 17 | return c / 5 * 9 + 32; 18 | } -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/ap2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int asciiCode(char c); 4 | 5 | int main(void) { 6 | char c; 7 | 8 | printf("Enter 1 Character : "); 9 | scanf("%c", &c); 10 | 11 | printf("ASCII code of [%c] is [%d]\n", c, asciiCode(c)); 12 | 13 | return 0; 14 | } 15 | 16 | int asciiCode(char c) { 17 | return (int) c; 18 | } -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/exercise/ap3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void dec2oct(int* a, int* b, int* c, int* d, int n); 4 | void dec2hex(char* x, char* y, char* z, int n); 5 | char digit2hex(int l); 6 | 7 | int main(void) { 8 | int n; 9 | 10 | int a, b, c, d; 11 | char x, y, z; 12 | 13 | scanf("%d", &n); 14 | 15 | dec2oct(&a, &b, &c, &d, n); 16 | dec2hex(&x, &y, &z, n); 17 | 18 | printf("%%o : %04o\n", n); 19 | printf("dec2oct : %d%d%d%d\n", a, b, c, d); 20 | printf("%%X : %03X\n", n); 21 | printf("dec2hex : %c%c%c\n", x, y, z); 22 | 23 | return 0; 24 | } 25 | 26 | void dec2oct(int* a, int* b, int* c, int* d, int n) { 27 | *a = n / 8 / 8 / 8 ; 28 | *b = n / 8 / 8 % 8; 29 | *c = n / 8 % 8; 30 | *d = n % 8; 31 | 32 | return; 33 | } 34 | 35 | void dec2hex(char* x, char* y, char* z, int n) { 36 | *x = digit2hex(n / 16 / 16 ); 37 | *y = digit2hex(n / 16 % 16); 38 | *z = digit2hex(n % 16); 39 | 40 | return; 41 | } 42 | 43 | char digit2hex(int l) { 44 | if (l < 9) return (char) (l + 0x30); 45 | else return (char) (l - 10 + 0x41); 46 | } -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/final/final-1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void Josephus(int n, int k); 4 | 5 | int main() { 6 | int n, k; 7 | scanf("%d%d", &n, &k); 8 | Josephus(n, k); 9 | 10 | return 0; 11 | } 12 | 13 | void Josephus(int n, int k) { 14 | int i, count = 0, x = 0; 15 | int r = n - k; 16 | int flag[100000]; 17 | for (i = 0; i < 100000; i++) flag[i] = 0; 18 | 19 | i = k % n - 1; 20 | while (count < r) { 21 | if (count == r - 1) { 22 | printf("%d", i + 1); 23 | } else { 24 | printf("%d ", i + 1); 25 | } 26 | flag[i] = 1; 27 | x = 0; 28 | while (x < k) { 29 | x++; 30 | i++; 31 | while (flag[i] == 1) i++; 32 | i %= n; 33 | } 34 | count++; 35 | } 36 | 37 | printf("\n"); 38 | return; 39 | } -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/final/final-2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | char* LGame(char word[]); 4 | int isVowel(char c); 5 | char toLowerCase(char c); 6 | 7 | int main() { 8 | char c[500], x, *res; 9 | int i = 0; 10 | 11 | while (1) { 12 | scanf("%c", &x); 13 | if (x == '\n') { 14 | c[i] = '\0'; 15 | break; 16 | } 17 | c[i] = x; 18 | i++; 19 | } 20 | 21 | LGame(c); 22 | 23 | return 0; 24 | } 25 | 26 | char* LGame(char word[]) { 27 | char res[1000]; 28 | char w[22]; 29 | char a[22], b[22]; 30 | int widx = 0, ridx = 0, i, j, strlen = 0, alen, blen, flag; 31 | char terminator = ' '; 32 | int process = 1; 33 | 34 | for (i = 0; i < 1000; i++) res[i] = ' '; 35 | for (i = 0; i < 500; i++) if (word[i] == '\0') { 36 | strlen = i; 37 | break; 38 | } 39 | 40 | while (widx < strlen) { 41 | for (i = 0; i < 22; i++) { 42 | w[i] = ' '; 43 | a[i] = ' '; 44 | b[i] = ' '; 45 | } 46 | 47 | i = 0; 48 | flag = 0; 49 | alen = 0; 50 | blen = 0; 51 | 52 | while (1) { 53 | w[i] = word[widx]; 54 | 55 | if (w[i] == ' ' || w[i] == '.' || w[i] == '!' || w[i] == '?' || w[i] == '\0') { 56 | terminator = w[i]; 57 | break; 58 | } 59 | 60 | i++; 61 | widx++; 62 | } 63 | 64 | if (terminator == '\0') { 65 | process = 0; 66 | } 67 | 68 | for (j = 0; j < i; j++) { 69 | if (isVowel(w[j]) || flag) { 70 | flag = 1; 71 | b[blen] = w[j]; 72 | blen++; 73 | } else { 74 | a[alen] = w[j]; 75 | alen++; 76 | } 77 | } 78 | 79 | for (j = 0; j < blen; j++) { 80 | res[ridx] = b[j]; 81 | ridx++; 82 | } 83 | 84 | for (j = 0; j < alen; j++) { 85 | res[ridx] = toLowerCase(a[j]); 86 | ridx++; 87 | } 88 | 89 | if (res[ridx - 1] != 'A' && res[ridx - 1] != 'a' && alen + blen != 0) { 90 | res[ridx] = 'a'; 91 | ridx++; 92 | } 93 | 94 | if (alen + blen != 0) { 95 | res[ridx] = 'y'; 96 | ridx++; 97 | } 98 | 99 | res[ridx] = terminator; 100 | ridx++; 101 | 102 | widx++; 103 | } 104 | 105 | res[ridx] = '\0'; 106 | 107 | printf("%s", res); 108 | 109 | return res; 110 | } 111 | 112 | int isVowel(char c) { 113 | return c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U' || 114 | c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; 115 | } 116 | 117 | char toLowerCase(char c) { 118 | if ('A' < c && c < 'Z') return c + 0x20; 119 | else return c; 120 | } -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/final/final-3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | float returnScore(int scores[]); 4 | char grade(float score); 5 | 6 | int main() { 7 | FILE *in; 8 | int scores[3], i; 9 | float score; 10 | 11 | in = fopen("input.txt", "r"); 12 | if (in == NULL) { 13 | printf("Input error.\n"); 14 | return 1; 15 | } 16 | 17 | for (i = 0; i < 3; i++) { 18 | fscanf(in, "%d", &scores[i]); 19 | } 20 | 21 | score = returnScore(scores); 22 | 23 | printf("%.2f %c\n", score, grade(score)); 24 | 25 | fclose(in); 26 | 27 | return 0; 28 | } 29 | 30 | float returnScore(int scores[]) { 31 | float sum = 0.25 * scores[0] + 0.25 * scores[1] + 0.5 * scores[2]; 32 | 33 | return sum; 34 | } 35 | 36 | char grade(float score) { 37 | if (score >= 95) return 'A'; 38 | else if (score >= 80) return 'B'; 39 | else if (score >= 50) return 'C'; 40 | else return 'D'; 41 | } -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/final/final-4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void solve(FILE* in); 4 | 5 | int main() { 6 | FILE *in; 7 | char filename[100]; 8 | 9 | scanf("%s", &filename); 10 | 11 | in = fopen(filename, "r"); 12 | if (in == NULL) { 13 | printf("Input error.\n"); 14 | return 1; 15 | } 16 | 17 | solve(in); 18 | 19 | fclose(in); 20 | 21 | return 0; 22 | } 23 | 24 | void solve(FILE* in) { 25 | int arr[100][100]; 26 | int n, m, i, j, x, y, change; 27 | int max = 0, total = 0; 28 | 29 | fscanf(in, "%d%d", &n, &m); 30 | 31 | for (i = 0; i < n; i++) for (j = 0; j < m; j++) { 32 | fscanf(in, "%d", &arr[i][j]); 33 | if (arr[i][j]) total++; 34 | } 35 | 36 | for (i = 0; i < n; i++) for (j = 0; j < m; j++) { 37 | change = 0; 38 | 39 | for (x = 0; x < n; x++) { 40 | if (arr[x][j] == 0) change++; else change--; 41 | } 42 | for (y = 0; y < m; y++) { 43 | if (arr[i][y] == 0) change++; else change--; 44 | } 45 | 46 | if (arr[i][j] == 0) change -= 2; else change += 2; 47 | 48 | if (max < change) max = change; 49 | } 50 | 51 | 52 | printf("%d\n", total + max); 53 | return; 54 | } -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/midterm/midterm-1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * April 19th, 2018 5 | * CSE2003 Midterm Exam 6 | * Problem #1 7 | */ 8 | 9 | float weightSum(int* five_ago, int* four_ago, int* three_ago, int* two_ago, int* one_ago); 10 | void printBySelection(float weight_sum); 11 | 12 | int main(void) { 13 | int five_ago, four_ago, three_ago, two_ago, one_ago; 14 | 15 | scanf("%d%d%d%d%d", &five_ago, &four_ago, &three_ago, &two_ago, &one_ago); 16 | printBySelection(weightSum(&five_ago, &four_ago, &three_ago, &two_ago, &one_ago)); 17 | 18 | return 0; 19 | } 20 | 21 | float weightSum(int* five_ago, int* four_ago, int* three_ago, int* two_ago, int* one_ago) { 22 | float sum = 0.0f; 23 | 24 | sum += *five_ago * 0.04f; 25 | sum += *four_ago * 0.06f; 26 | sum += *three_ago * 0.15f; 27 | sum += *two_ago * 0.25f; 28 | sum += *one_ago * 0.5f; 29 | 30 | return sum; 31 | } 32 | 33 | void printBySelection(float weight_sum) { 34 | if (weight_sum >= 150) { 35 | printf("%.2f very bad\n", weight_sum); 36 | } 37 | if (weight_sum >= 80 && weight_sum < 150) { 38 | printf("%.2f bad\n", weight_sum); 39 | } 40 | if (weight_sum >= 30 && weight_sum < 80) { 41 | printf("%.2f normal\n", weight_sum); 42 | } 43 | if ( weight_sum < 30) { 44 | printf("%.2f good\n", weight_sum); 45 | } 46 | } -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/midterm/midterm-2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * April 19th, 2018 5 | * CSE2003 Midterm Exam 6 | * Problem #2 7 | */ 8 | 9 | void checkWB(char col, int row, char *answer); 10 | 11 | int main(void) { 12 | char col, answer; 13 | int row; 14 | 15 | scanf("%c%d", &col, &row); 16 | checkWB(col, row, &answer); 17 | printf("%c\n", answer); 18 | 19 | return 0; 20 | } 21 | 22 | void checkWB(char col, int row, char *answer) { 23 | int _col = col - 0x60; // 'a' = 0x61 24 | 25 | if ((_col + row) % 2 == 1) { 26 | *answer = 'W'; 27 | } else { 28 | *answer = 'B'; 29 | } 30 | 31 | return; 32 | } -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/midterm/midterm-3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * April 19th, 2018 5 | * CSE2003 Midterm Exam 6 | * Problem #3 7 | */ 8 | 9 | void caesar(char* alphabet, int key); 10 | 11 | int main(void) { 12 | int key; 13 | char a, b, c; 14 | 15 | scanf("%d %c%c%c", &key, &a, &b, &c); 16 | 17 | caesar(&a, key); 18 | caesar(&b, key); 19 | caesar(&c, key); 20 | 21 | printf("%c%c%c\n", a, b, c); 22 | 23 | return 0; 24 | } 25 | 26 | void caesar(char* alphabet, int key) { 27 | /** 28 | * Problem never stated that key falls 29 | * in -26 .. 26 30 | * 31 | * 'A' = 0x41; 'a' = 0x61 32 | */ 33 | 34 | int alphabetIndex = *alphabet - 0x61; 35 | 36 | alphabetIndex += key; 37 | alphabetIndex %= 26; 38 | 39 | if (alphabetIndex < 0) alphabetIndex += 26; 40 | 41 | *alphabet = alphabetIndex + 0x61; 42 | 43 | return; 44 | } -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/midterm/midterm-4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * April 19th, 2018 5 | * CSE2003 Midterm Exam 6 | * Problem #4 7 | */ 8 | 9 | float check(float c1, float c2, float c3, float c4); 10 | 11 | int main(void) { 12 | float c1, c2, c3, c4; 13 | 14 | scanf("%f%f%f%f", &c1, &c2, &c3, &c4); 15 | 16 | printf("%.1f\n", check(c1, c2, c3, c4)); 17 | 18 | return 0; 19 | } 20 | 21 | float check(float c1, float c2, float c3, float c4) { 22 | float a = c2 - c1; 23 | float b = c3 - c2; 24 | float c = c4 - c3; 25 | 26 | float sum = a + b + c; 27 | if (a > b && a > c) sum -= a; 28 | if (b > a && b > c) sum -= b; 29 | if (c > a && c > b) sum -= c; 30 | 31 | return sum; 32 | } -------------------------------------------------------------------------------- /CSE2003 Introduction to Engineering Design/project/paper.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shiftpsh/sgcs/9456210b675ee514384d2b7867036bea5f525211/CSE2003 Introduction to Engineering Design/project/paper.pdf -------------------------------------------------------------------------------- /CSE2035 C Programming/README.md: -------------------------------------------------------------------------------- 1 | # C프로그래밍 2 | 3 | CSE2035 - C Programming 4 | 5 | 2018년 2학기 / 김지환 교수님 6 | 7 | - 조금 더 깊이 있는 C언어 프로그래밍 기초를 배웁니다. Pointer와 메모리 할당, 문자열 처리, Struct를 주로 배우고, 약간의 알고리즘도 배웁니다. 8 | 9 | - [실습 코드](exercise) 10 | - [프로젝트 리포트, 코드](project) 11 | - [중간고사 코드](midterm) (문제 미포함) 12 | - [기말고사 코드](final) (문제 미포함) 13 | 14 | ## Practice 15 | 16 | - 매 수업마다 과제를 주고 실습을 합니다. 제시문에서 구현하라는 대로 구현하면 됩니다. 17 | - 실습 문제를 매년 새로 만드는지는 잘 모르겠습니다. 18 | 19 | ## Exam 20 | 21 | - 시험 문제는 아마도 매년 새로 나옵니다. 족보는 별로 도움이 되지 않고 직접 코드를 짜보는 게 제일 도움이 되는 것 같습니다. 22 | 23 | ## Algorithms and Structures 24 | 25 | - 버블 정렬 (Bubble sort) 26 | - 순차 검색 (Linear search) 27 | - 이분 탐색 (Binary search) 28 | - 연결 리스트 (Linked list) 29 | - 큐 (Queue) 30 | - 스택 (Stack) 31 | - 덱 (Deque) 32 | 33 | ## Source Code 34 | 35 | - 첨부된 중간고사 소스코드의 점수는 94점입니다(3번 문제에서 테스트케이스 5개 중 1개 실패로 6점 감점). 60번째 줄 `qx = x + 7, qy = y + 13;`에서 7과 13을 2002와 2003으로 수정하면 정해입니다. 36 | 37 | - 원본 기말고사 소스코드의 점수는 3번 문제가 정해에서 마지막 줄을 출력하지 않아 87.5점이었으나, 첨부된 소스코드는 마지막 줄을 출력하도록 수정되었습니다. 38 | 39 | ## Tips 40 | 41 | - Algorithms and Structures에 소개된 알고리즘과 자료구조들, 그리고 이들이 쓰이는 상황들을 미리 알아 두면 좋습니다. 42 | 43 | - Null-terminated strings (C strings)의 개념을 알아 두면 좋습니다. -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/10p1.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | int main() { 8 | int i, words = 0, flag = 0; 9 | char str[10000], last = '\0'; 10 | gets(&str); 11 | 12 | for (i = 0; i < strlen(str); i++) { 13 | if (!flag && str[i] != ' ') { 14 | flag = 1; 15 | } 16 | if (flag) { 17 | if (last != ' ' && str[i] == ' ') { 18 | words++; 19 | } 20 | } 21 | 22 | last = str[i]; 23 | } 24 | 25 | if (last != ' ') words++; 26 | 27 | printf("%d\n", words); 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/10p2.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | int main() { 8 | char unit[] = {'K', 'M', 'G', 'T', 'P'}, str[17]; 9 | int l, i, basel, sigl, sig = 0, flag = 1; 10 | 11 | scanf("%16s", &str); 12 | l = strlen(str); 13 | basel = (l - 1) / 3; 14 | sigl = (l - 1) % 3 + 1; 15 | 16 | for (i = 0; i < l; i++) { 17 | if (!('0' <= str[i] && str[i] <= '9')) { 18 | flag = 0; 19 | break; 20 | } 21 | } 22 | 23 | if (!flag) { 24 | printf("%s\n", str); 25 | } 26 | else { 27 | if (basel == 0) { 28 | printf("%s\n", str); 29 | } 30 | else { 31 | for (i = 0; i < sigl; i++) { 32 | sig *= 10; 33 | sig += str[i] - '0'; 34 | } 35 | 36 | printf("%d%c\n", sig, unit[basel - 1]); 37 | } 38 | } 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/11p1.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | void split_string(char*, char*, char*); 8 | 9 | int main() { 10 | char str[51], string_1[51], string_2[51]; 11 | gets(str); 12 | 13 | split_string(str, string_1, string_2); 14 | 15 | printf("[%s] -> [%s] [%s]\n", str, string_1, string_2); 16 | 17 | return 0; 18 | } 19 | 20 | void split_string(char* str, char* string_1, char* string_2) { 21 | int i; 22 | int l = strlen(str); 23 | int m = l / 2; 24 | 25 | strcpy(string_1, str); 26 | strcpy(string_2, str); 27 | 28 | string_1[m] = '\0'; 29 | 30 | for (i = 0; i <= m; i++) { 31 | string_2[i] = string_2[i + m]; 32 | } 33 | 34 | string_2[m] = '\0'; 35 | } -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/11p2.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | char *my_strncpy(char*, char*, int); 8 | void initialization(char*, int); 9 | 10 | int main() { 11 | int i; 12 | char* str1; 13 | char str[20]; 14 | 15 | gets(str); 16 | 17 | str1 = (char*)malloc((strlen(str) + 1) * sizeof(char)); 18 | strcpy(str1, str); 19 | 20 | for (i = 0; i < strlen(str1) + 1; i++) { 21 | initialization(str, strlen(str)); 22 | my_strncpy(str, str1, i); 23 | printf("%s\n", str); 24 | } 25 | 26 | free(str1); 27 | 28 | return 0; 29 | } 30 | 31 | char *my_strncpy(char* a, char* b, int n) { 32 | int i; 33 | for (i = 0; i < n; i++) { 34 | a[i] = b[i]; 35 | } 36 | } 37 | 38 | void initialization(char* a, int n) { 39 | int i; 40 | for (i = 0; i < n; i++) { 41 | a[i] = '*'; 42 | } 43 | } -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/12p1.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | int strCmpPk(char* s1, char* s2); 8 | void strPk(char* s1, char* s2); 9 | 10 | int main() { 11 | char s1[101], s2[101]; 12 | 13 | printf("first string : "); 14 | scanf("%s", s1); 15 | printf("second string : "); 16 | scanf("%s", s2); 17 | 18 | int x = strCmpPk(s1, s2); 19 | if (x == 0) { 20 | printf("string1 == string2\n"); 21 | } 22 | else if (x < 0) { 23 | printf("string1 < string2\n"); 24 | } 25 | else { 26 | printf("string1 > string2\n"); 27 | } 28 | 29 | return 0; 30 | } 31 | 32 | int strCmpPk(char* s1, char* s2) { 33 | strPk(s1, s2); 34 | return strcmp(s1, s2); 35 | } 36 | 37 | void strPk(char* s1, char* s2) { 38 | int l1 = strlen(s1), l2 = strlen(s2); 39 | int i1 = 0, i2 = 0, j1 = 0, j2 = 0; 40 | 41 | char* t1 = (char*)malloc(sizeof(char) * (l1 + 1)); 42 | char* t2 = (char*)malloc(sizeof(char) * (l2 + 1)); 43 | 44 | for (i1 = 0; i1 < l1; i1++) { 45 | if ('a' <= s1[i1] && s1[i1] <= 'z' 46 | || 'A' <= s1[i1] && s1[i1] <= 'Z') { 47 | t1[j1] = s1[i1]; 48 | j1++; 49 | } 50 | } 51 | t1[j1] = '\0'; 52 | 53 | for (i2 = 0; i2 < l2; i2++) { 54 | if ('a' <= s2[i2] && s2[i2] <= 'z' 55 | || 'A' <= s2[i2] && s2[i2] <= 'Z') { 56 | t2[j2] = s2[i2]; 57 | j2++; 58 | } 59 | } 60 | t2[j2] = '\0'; 61 | 62 | strcpy(s1, t1); 63 | strcpy(s2, t2); 64 | free(t1); 65 | free(t2); 66 | } -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/13p1.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | 3 | #include 4 | #include 5 | 6 | enum Date { 7 | Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday 8 | }; 9 | 10 | struct Calendar { 11 | int year, month, day; 12 | enum Date date; 13 | char* dateString; 14 | }; 15 | 16 | enum Date getDate(int, int, int); 17 | char* getDay(enum Date); 18 | 19 | int main() { 20 | int y, m, d; 21 | scanf("%d%d%d", &y, &m, &d); 22 | 23 | struct Calendar date = { y, m, d }; 24 | date.date = getDate(date.year, date.month, date.day); 25 | date.dateString = getDay(date.date); 26 | 27 | printf("%s\n", date.dateString); 28 | 29 | return 0; 30 | } 31 | 32 | enum Date getDate(int y, int m, int d) { 33 | return (y + y / 4 - y / 100 + y / 400 + (13 * m + 8) / 5 + d) % 7; 34 | } 35 | 36 | char* getDay(enum Date currDate) { 37 | switch (currDate) { 38 | case Sunday: 39 | return "Sunday"; 40 | case Monday: 41 | return "Monday"; 42 | case Tuesday: 43 | return "Tuesday"; 44 | case Wednesday: 45 | return "Wednesday"; 46 | case Thursday: 47 | return "Thursday"; 48 | case Friday: 49 | return "Friday"; 50 | case Saturday: 51 | return "Saturday"; 52 | default: 53 | break; 54 | } 55 | } -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/13p2.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | 3 | #include 4 | #include 5 | 6 | struct CoinBox { 7 | int n500, n100, n50, n10; 8 | }; 9 | 10 | int main() { 11 | int m; 12 | scanf("%d", &m); 13 | 14 | struct CoinBox box = { 15 | m / 500, (m % 500) / 100, (m % 100) / 50, (m % 50) / 10 16 | }; 17 | 18 | printf("%d\n%d\n%d\n%d\n", box.n500, box.n100, box.n50, box.n10); 19 | 20 | return 0; 21 | } -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/14p1.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | typedef float* Vector_Comp; 8 | typedef float Comp; 9 | typedef float VectorSize; 10 | typedef float Distance; 11 | typedef float Scalar; 12 | 13 | typedef struct { 14 | Vector_Comp comps; int vec_size; 15 | } Vector; 16 | 17 | VectorSize vSize(Vector); 18 | Distance DistVector(Vector, Vector); 19 | Scalar innerProduct(Vector, Vector); 20 | 21 | int main() { 22 | int n1, n2, i; 23 | 24 | scanf("%d", &n1); 25 | 26 | Vector v1; 27 | v1.vec_size = n1; 28 | v1.comps = (Vector_Comp)malloc(sizeof(Comp) * v1.vec_size); 29 | 30 | for (i = 0; i < n1; i++) { 31 | scanf("%f", &v1.comps[i]); 32 | } 33 | 34 | scanf("%d", &n2); 35 | 36 | Vector v2; 37 | v2.vec_size = n2; 38 | v2.comps = (Vector_Comp)malloc(sizeof(Comp) * v2.vec_size); 39 | 40 | for (i = 0; i < n2; i++) { 41 | scanf("%f", &v2.comps[i]); 42 | } 43 | 44 | printf("%f\n", vSize(v1)); 45 | printf("%f\n", vSize(v2)); 46 | 47 | if (v1.vec_size == v2.vec_size) { 48 | printf("%f\n", DistVector(v1, v2)); 49 | printf("%f\n", innerProduct(v1, v2)); 50 | } 51 | 52 | free(v1.comps); 53 | free(v2.comps); 54 | 55 | return 0; 56 | } 57 | 58 | VectorSize vSize(Vector v) { 59 | int i; 60 | VectorSize size = 0; 61 | 62 | for (i = 0; i < v.vec_size; i++) { 63 | size += v.comps[i] * v.comps[i]; 64 | } 65 | 66 | return sqrt(size); 67 | } 68 | 69 | Distance DistVector(Vector v1, Vector v2) { 70 | if (v1.vec_size != v2.vec_size) return -1; 71 | int i; 72 | Distance d = 0; 73 | 74 | for (i = 0; i < v1.vec_size; i++) { 75 | d += (v1.comps[i] - v2.comps[i]) * (v1.comps[i] - v2.comps[i]); 76 | } 77 | 78 | return sqrt(d); 79 | } 80 | 81 | Scalar innerProduct(Vector v1, Vector v2) { 82 | if (v1.vec_size != v2.vec_size) return -1; 83 | int i; 84 | Scalar s = 0; 85 | 86 | for (i = 0; i < v1.vec_size; i++) { 87 | s += v1.comps[i] * v2.comps[i]; 88 | } 89 | 90 | return s; 91 | } -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/15p1.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | typedef struct { 8 | int numerator; 9 | int denominator; 10 | } FRACTION; 11 | 12 | void multiFr(FRACTION*, FRACTION*, FRACTION*); 13 | 14 | int main() { 15 | int a, b, c, d; 16 | FRACTION p, q, r; 17 | 18 | printf("x/y : "); 19 | scanf("%d/%d", &a, &b); 20 | printf("x/y : "); 21 | scanf("%d/%d", &c, &d); 22 | 23 | p.numerator = a; 24 | p.denominator = b; 25 | q.numerator = c; 26 | q.denominator = d; 27 | 28 | multiFr(&p, &q, &r); 29 | 30 | printf( 31 | "%d/%d * %d/%d = %d/%d\n", 32 | p.numerator, p.denominator, 33 | q.numerator, q.denominator, 34 | r.numerator, r.denominator 35 | ); 36 | 37 | return 0; 38 | } 39 | 40 | void multiFr(FRACTION* pFr1, FRACTION* pFr2, FRACTION* pRes) { 41 | pRes->numerator = pFr1->numerator * pFr2->numerator; 42 | pRes->denominator = pFr1->denominator * pFr2->denominator; 43 | } -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/15p2.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | typedef struct { 8 | float price; 9 | float number; 10 | } Buying; 11 | 12 | Buying average(Buying* arr, int n); 13 | 14 | int main() { 15 | int n, i; 16 | scanf("%d", &n); 17 | 18 | Buying* arr = (Buying*)malloc(sizeof(Buying) * n); 19 | for (i = 0; i < n; i++) { 20 | scanf("%f%f", &(arr[i].price), &(arr[i].number)); 21 | } 22 | 23 | Buying avg = average(arr, n); 24 | printf("%.2f %.2f\n", avg.price, avg.number); 25 | 26 | free(arr); 27 | 28 | return 0; 29 | } 30 | 31 | Buying average(Buying* arr, int n) { 32 | int i; 33 | Buying sum = {0, 0}; 34 | 35 | for (i = 0; i < n; i++) { 36 | sum.number += arr[i].number; 37 | sum.price += arr[i].price; 38 | } 39 | 40 | sum.price /= n; 41 | sum.number /= n; 42 | 43 | return sum; 44 | } -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/16p1.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | typedef struct __CUSTOMER__ { 8 | int number; 9 | char id[50]; 10 | char pw[50]; 11 | char name[50]; 12 | char address[200]; 13 | } customer; 14 | 15 | int main() { 16 | FILE* customers = fopen("customer.csv", "r"); 17 | char buffer[500], *pch; 18 | customer* arr = (customer*)malloc(0); 19 | 20 | int i = 0, j = 0, size = 0; 21 | char reqid[50], reqpw[50]; 22 | 23 | while (fgets(buffer, 500, customers)) { 24 | if (i == 0) { 25 | i++; 26 | continue; 27 | } 28 | 29 | size++; 30 | arr = (customer*)realloc(arr, size * sizeof(customer)); 31 | j = 0; 32 | 33 | pch = strtok(buffer, ","); 34 | while (pch != NULL) { 35 | switch (j) { 36 | case 0: 37 | arr[size - 1].number = atoi(pch); 38 | break; 39 | case 1: 40 | strcpy(arr[size - 1].id, pch); 41 | break; 42 | case 2: 43 | strcpy(arr[size - 1].pw, pch); 44 | break; 45 | case 3: 46 | strcpy(arr[size - 1].name, pch); 47 | break; 48 | case 4: 49 | strcpy(arr[size - 1].address, pch); 50 | break; 51 | } 52 | pch = strtok(NULL, ","); 53 | j++; 54 | } 55 | 56 | i++; 57 | } 58 | 59 | fclose(customers); 60 | 61 | /* Login page */ 62 | int user_id = -1; 63 | int login_try = 0; 64 | 65 | printf("로그인 프로그램\n"); 66 | 67 | while (1) { 68 | printf("아이디: "); 69 | scanf("%s", &reqid); 70 | printf("비밀번호: "); 71 | scanf("%s", &reqpw); 72 | 73 | for (i = 0; i < size; i++) { 74 | if (strcmp(arr[i].id, reqid) == 0 && strcmp(arr[i].pw, reqpw) == 0) { 75 | user_id = i; 76 | break; 77 | } 78 | } 79 | 80 | if (user_id == -1) { 81 | login_try++; 82 | printf("일치하는 회원이 없습니다.\n"); 83 | if (login_try == 3) break; 84 | } 85 | else { 86 | customer c = arr[user_id]; 87 | printf("===================\n"); 88 | printf("회원번호: %d\n", c.number); 89 | printf("아이디: %s\n", c.id); 90 | printf("비밀번호: %s\n", c.pw); 91 | printf("이름: %s\n", c.name); 92 | printf("주소: %s\n", c.address); 93 | break; 94 | } 95 | } 96 | 97 | free(arr); 98 | 99 | return 0; 100 | } -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/17p2.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | typedef struct { 9 | double x; 10 | double y; 11 | double z; 12 | double r; 13 | } SPHERE; 14 | 15 | double DISTANCE(SPHERE, SPHERE); 16 | double CrossSectionArea(SPHERE, SPHERE); 17 | double SurfaceArea(SPHERE, SPHERE); 18 | double Volume(SPHERE, SPHERE); 19 | 20 | int main() { 21 | SPHERE p, q; 22 | double d; 23 | 24 | printf("1st sphere coordinates : "); 25 | scanf("%lf%lf%lf", &(p.x), &(p.y), &(p.z)); 26 | printf("1st sphere radius : "); 27 | scanf("%lf", &(p.r)); 28 | 29 | printf("2nd sphere coordinates : "); 30 | scanf("%lf%lf%lf", &(q.x), &(q.y), &(q.z)); 31 | printf("2nd sphere radius : "); 32 | scanf("%lf", &(q.r)); 33 | 34 | d = DISTANCE(p, q); 35 | 36 | if (d == (p.r + q.r)) { 37 | printf("Sum of Surface Area : %.2lf\n", SurfaceArea(p, q)); 38 | } 39 | else if (d < (p.r + q.r)) { 40 | printf("Sum of Volume : %.2lf\n", Volume(p, q)); 41 | } 42 | else { 43 | printf("Sum of Cross Section Area : %.2lf\n", CrossSectionArea(p, q)); 44 | } 45 | 46 | return 0; 47 | } 48 | 49 | double DISTANCE(SPHERE p, SPHERE q) { 50 | return sqrt( 51 | (p.x - q.x) * (p.x - q.x) + 52 | (p.y - q.y) * (p.y - q.y) + 53 | (p.z - q.z) * (p.z - q.z) 54 | ); 55 | } 56 | 57 | double CrossSectionArea(SPHERE p, SPHERE q) { 58 | double PI = 3.14; 59 | 60 | return PI * (p.r * p.r + q.r * q.r); 61 | } 62 | 63 | double SurfaceArea(SPHERE p, SPHERE q) { 64 | double PI = 3.14; 65 | 66 | return 4 * PI * (p.r * p.r + q.r * q.r); 67 | } 68 | 69 | double Volume(SPHERE p, SPHERE q) { 70 | double PI = 3.14; 71 | 72 | return 4 * PI * (p.r * p.r * p.r + q.r * q.r * q.r) / 3; 73 | } -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/17p3.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | typedef struct __SUBJECT__ { 9 | char number[8]; 10 | int credit; 11 | double grade; 12 | }subject; 13 | 14 | typedef struct __STUDENT__ { 15 | int id; 16 | int number_of_course; 17 | subject *courses; 18 | }student; 19 | 20 | double calcCGPA(student); 21 | 22 | int main() { 23 | int n, i, j, id, m; 24 | 25 | scanf("%d", &n); 26 | student* arr = (student*)malloc(sizeof(student) * n); 27 | 28 | for (i = 0; i < n; i++) { 29 | scanf("%d%d", &id, &m); 30 | 31 | arr[i].id = id; 32 | arr[i].number_of_course = m; 33 | arr[i].courses = (subject*)malloc(sizeof(subject) * m); 34 | 35 | for (j = 0; j < m; j++) { 36 | scanf("%s%d%lf", 37 | &(arr[i].courses[j].number), 38 | &(arr[i].courses[j].credit), 39 | &(arr[i].courses[j].grade) 40 | ); 41 | } 42 | } 43 | 44 | printf("==========\n"); 45 | 46 | for (i = 0; i < n; i++) { 47 | printf("%d / %lf\n", arr[i].id, calcCGPA(arr[i])); 48 | } 49 | 50 | for (i = 0; i < n; i++) { 51 | free(arr[i].courses); 52 | } 53 | free(arr); 54 | 55 | return 0; 56 | } 57 | 58 | double calcCGPA(student s) { 59 | int csum = 0, i; 60 | double gsum = 0; 61 | 62 | for (i = 0; i < s.number_of_course; i++) { 63 | csum += s.courses[i].credit; 64 | gsum += s.courses[i].grade * s.courses[i].credit; 65 | } 66 | 67 | if (csum == 0) return 0.0; 68 | else return gsum / csum; 69 | } -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/18p1.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shiftpsh/sgcs/9456210b675ee514384d2b7867036bea5f525211/CSE2035 C Programming/exercise/18p1.c -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/18p2.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shiftpsh/sgcs/9456210b675ee514384d2b7867036bea5f525211/CSE2035 C Programming/exercise/18p2.c -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/19p1.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | typedef struct node* nptr; 9 | 10 | typedef struct node { 11 | int data; 12 | nptr *link; 13 | } NODE; 14 | 15 | void Insert(nptr*, int); 16 | void PrintAll(nptr); 17 | 18 | int main() { 19 | int A[10] = { 3, 9, 8, 2, 5, 10, 7, 1, 4, 6 }; 20 | 21 | nptr head = (NODE*)malloc(sizeof(NODE)); 22 | head->link = NULL; 23 | 24 | nptr curr = head, prev; 25 | 26 | for (int i = 0; i < 10; i++) { 27 | Insert(&curr, A[i]); 28 | curr = curr->link; 29 | } 30 | 31 | curr->link = NULL; 32 | 33 | PrintAll(head); 34 | 35 | prev = head; 36 | curr = head->link; 37 | for (int i = 0; i < 10; i++) { 38 | free(prev); 39 | prev = curr; 40 | curr = curr->link; 41 | } 42 | free(curr); 43 | 44 | return 0; 45 | } 46 | 47 | 48 | void Insert(nptr* ptr, int value) { 49 | (*ptr)->link = (NODE*)malloc(sizeof(NODE)); 50 | (*ptr)->data = value; 51 | } 52 | 53 | void PrintAll(nptr ptr) { 54 | nptr curr = ptr; 55 | 56 | while (1) { 57 | if (curr->link == NULL) break; 58 | printf("%d ", curr->data); 59 | curr = curr->link; 60 | } 61 | } -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/19p2.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | typedef struct node* nptr; 9 | 10 | typedef struct node { 11 | int data; 12 | nptr *link; 13 | } NODE; 14 | 15 | void Insert(nptr*, int); 16 | void PrintAll(nptr); 17 | 18 | int main() { 19 | int A[10] = { 3, 9, 8, 2, 5, 10, 7, 1, 4, 6 }; 20 | 21 | nptr head = (NODE*)malloc(sizeof(NODE)); 22 | head->link = NULL; 23 | 24 | nptr curr = head, prev; 25 | 26 | for (int i = 0; i < 10; i++) { 27 | Insert(&head, A[i]); 28 | } 29 | 30 | PrintAll(head); 31 | 32 | prev = head; 33 | curr = head->link; 34 | for (int i = 0; i < 10; i++) { 35 | free(prev); 36 | prev = curr; 37 | curr = curr->link; 38 | } 39 | free(curr); 40 | 41 | return 0; 42 | } 43 | 44 | 45 | void Insert(nptr* ptr, int value) { 46 | nptr newNode = (nptr) malloc(sizeof(NODE)); 47 | newNode->data = value; 48 | newNode->link = *ptr; 49 | *ptr = newNode; 50 | } 51 | 52 | void PrintAll(nptr ptr) { 53 | nptr curr = ptr; 54 | 55 | while (1) { 56 | if (curr->link == NULL) break; 57 | printf("%d ", curr->data); 58 | curr = curr->link; 59 | } 60 | } -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/19p3.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | typedef struct _Node { 9 | int st_id; 10 | char st_name[25]; 11 | struct _Node *link; 12 | } Node; 13 | 14 | Node *head = NULL, *curr = NULL; 15 | 16 | void create_Node(int, char*); 17 | void print_list(Node*); 18 | void free_list(Node*); 19 | 20 | int main() { 21 | FILE* input = fopen("input.csv", "r"); 22 | int id; 23 | char name[30]; 24 | 25 | char buffer[1000], *token; 26 | int n; 27 | 28 | fgets(buffer, 1000, input); 29 | n = atoi(buffer); 30 | 31 | while (fgets(buffer, 1000, input)) { 32 | token = strtok(buffer, ","); 33 | id = atoi(token); 34 | token = strtok(NULL, ","); 35 | token[strlen(token) - 1] = '\0'; 36 | strcpy(name, token); 37 | 38 | create_Node(id, name); 39 | } 40 | 41 | fclose(input); 42 | 43 | print_list(head); 44 | free_list(head); 45 | 46 | return 0; 47 | } 48 | 49 | void create_Node(int id, char *name){ 50 | Node *newNode = (Node*) malloc(sizeof(Node)); 51 | strcpy(newNode->st_name, name); 52 | newNode->st_id = id; 53 | newNode->link = NULL; 54 | 55 | if (head == NULL) { 56 | head = newNode; 57 | curr = head; 58 | } 59 | else { 60 | curr->link = newNode; 61 | curr = curr->link; 62 | } 63 | } 64 | 65 | void print_list(Node* head) { 66 | Node* currloc = head; 67 | 68 | while (1) { 69 | printf("%d / %s\n", currloc->st_id, currloc->st_name); 70 | if (currloc->link == NULL) break; 71 | currloc = currloc->link; 72 | } 73 | } 74 | 75 | void free_list(Node* head) { 76 | Node* prev = head; 77 | Node* currloc = head->link; 78 | 79 | while (1) { 80 | free(prev); 81 | prev = currloc; 82 | if (currloc->link == NULL) break; 83 | currloc = currloc->link; 84 | } 85 | 86 | free(currloc); 87 | } -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/19p4.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | typedef struct _Node { 9 | char c; 10 | struct _Node *link; 11 | } Node; 12 | 13 | Node *head = NULL, *tail = NULL; 14 | 15 | void InsertNodeAtFront(char a); 16 | void InsertNodeAtBack(char b); 17 | void PrintList(); 18 | void FreeList(); 19 | 20 | int main() { 21 | InsertNodeAtFront('p'); 22 | InsertNodeAtFront('p'); 23 | InsertNodeAtFront('a'); 24 | InsertNodeAtBack('l'); 25 | InsertNodeAtBack('e'); 26 | PrintList(); 27 | 28 | FreeList(); 29 | 30 | return 0; 31 | } 32 | 33 | void InsertNodeAtFront(char a) { 34 | Node *newNode = (Node*)malloc(sizeof(Node)); 35 | newNode->c = a; 36 | newNode->link = NULL; 37 | 38 | if (head == NULL) { 39 | head = newNode; 40 | tail = head; 41 | } 42 | else { 43 | newNode->link = head; 44 | head = newNode; 45 | } 46 | } 47 | 48 | void InsertNodeAtBack(char b) { 49 | Node *newNode = (Node*)malloc(sizeof(Node)); 50 | newNode->c = b; 51 | newNode->link = NULL; 52 | 53 | if (head == NULL) { 54 | head = newNode; 55 | tail = head; 56 | } 57 | else { 58 | tail->link = newNode; 59 | tail = tail->link; 60 | } 61 | } 62 | 63 | void PrintList() { 64 | Node *curr = head; 65 | 66 | while (1) { 67 | printf("%c", curr->c); 68 | if (curr->link == NULL) break; 69 | curr = curr->link; 70 | } 71 | 72 | printf("\n"); 73 | } 74 | 75 | void FreeList() { 76 | Node *prev = head; 77 | Node *curr = prev->link; 78 | 79 | while (1) { 80 | free(prev); 81 | prev = curr; 82 | if (curr->link == NULL) break; 83 | curr = curr->link; 84 | } 85 | 86 | free(curr); 87 | } -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/1p1.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | int main() { 5 | int a, b, c; 6 | int *pa, *pb, *pc; 7 | 8 | scanf("%d%d%d", &a, &b, &c); 9 | 10 | pa = &a; pb = &b; pc = &c; 11 | 12 | if (*pa >= *pb && *pb >= *pc) { // a >= b >= c 13 | printf("%d %d\n", *pc, *pa); 14 | printf("%p %p\n", pc, pa); 15 | } else if (*pa >= *pc && *pc >= *pb) { // a >= c >= b 16 | printf("%d %d\n", *pb, *pa); 17 | printf("%p %p\n", pb, pa); 18 | } else if (*pb >= *pa && *pa >= *pc) { // b >= a >= c 19 | printf("%d %d\n", *pc, *pb); 20 | printf("%p %p\n", pc, pb); 21 | } else if (*pb >= *pc && *pc >= *pa) { // b >= c >= a 22 | printf("%d %d\n", *pa, *pb); 23 | printf("%p %p\n", pa, pb); 24 | } else if (*pc >= *pa && *pa >= *pb) { // c >= a >= b 25 | printf("%d %d\n", *pb, *pc); 26 | printf("%p %p\n", pb, pc); 27 | } else if (*pc >= *pb && *pb >= *pa) { // c >= b >= a 28 | printf("%d %d\n", *pa, *pc); 29 | printf("%p %p\n", pa, pc); 30 | } 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/1p2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | double a, b, c, d; 5 | double *pa, *pb, *pc, *pd; 6 | 7 | scanf("%lf%lf%lf%lf", &a, &b, &c, &d); 8 | pa = &a; pb = &b; pc = &c; pd = &d; 9 | 10 | if (*pa == *pc && *pb == *pd) { 11 | printf("oo\n"); 12 | } else if (*pa == *pc) { 13 | printf("0\n"); 14 | } else { 15 | printf("1\n"); 16 | } 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/1p3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | double x1, y1, z1, x2, y2, z2; 5 | double *px1, *py1, *pz1, *px2, *py2, *pz2; 6 | 7 | scanf("%lf%lf%lf", &x1, &y1, &z1); 8 | scanf("%lf%lf%lf", &x2, &y2, &z2); 9 | 10 | px1 = &x1; py1 = &y1; pz1 = &z1; 11 | px2 = &x2; py2 = &y2; pz2 = &z2; 12 | 13 | // x1 = abs(x1 - x2), 14 | // y1 = abs(y1 - y2), 15 | // z1 = abs(z1 - z2) 16 | *px1 -= *px2; *py1 -= *py2; *pz1 -= *pz2; 17 | if (*px1 < 0) *px1 *= -1; 18 | if (*py1 < 0) *py1 *= -1; 19 | if (*pz1 < 0) *pz1 *= -1; 20 | 21 | // x1 = max(x1, y1, z1) 22 | if (*px1 < *py1) *px1 = *py1; 23 | if (*px1 < *pz1) *px1 = *pz1; 24 | 25 | printf("%lf\n", *px1); 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/20p1.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | typedef struct node* nptr; 9 | 10 | typedef struct node { 11 | int data; 12 | nptr *link; 13 | } NODE; 14 | 15 | void Insert(nptr*, int, int); 16 | void PrintAll(nptr); 17 | 18 | int main() { 19 | int A[10] = { 3, 9, 8, 2, 5, 10, 7, 1, 4, 6 }; 20 | 21 | nptr head = NULL; 22 | 23 | Insert(&head, A[0], 0); 24 | PrintAll(head); 25 | 26 | Insert(&head, A[1], 0); 27 | PrintAll(head); 28 | 29 | Insert(&head, A[2], 1); 30 | PrintAll(head); 31 | 32 | Insert(&head, A[3], 2); 33 | PrintAll(head); 34 | 35 | Insert(&head, A[4], 0); 36 | PrintAll(head); 37 | 38 | Insert(&head, A[5], 1); 39 | PrintAll(head); 40 | 41 | Insert(&head, A[6], 10); 42 | PrintAll(head); 43 | 44 | Insert(&head, A[7], 4); 45 | PrintAll(head); 46 | 47 | Insert(&head, A[8], -1); 48 | PrintAll(head); 49 | 50 | Insert(&head, A[9], 3); 51 | PrintAll(head); 52 | 53 | nptr curr = head->link, prev = head; 54 | while (1) { 55 | if (prev == NULL) break; 56 | free(prev); 57 | prev = curr; 58 | if (curr == NULL) break; 59 | curr = curr->link; 60 | } 61 | free(curr); 62 | 63 | return 0; 64 | } 65 | 66 | 67 | void Insert(nptr* head, int value, int index) { 68 | nptr curr = *head; 69 | int i; 70 | 71 | if (index == -1) index = 0x7fffffff; 72 | 73 | nptr newPtr = (nptr)malloc(sizeof(NODE)); 74 | newPtr->data = value; 75 | newPtr->link = NULL; 76 | 77 | if (curr == NULL) { 78 | *head = newPtr; 79 | return; 80 | } 81 | 82 | if (index == 0) { 83 | newPtr->link = *head; 84 | *head = newPtr; 85 | } 86 | else { 87 | for (i = 0; i < index - 1; i++) { 88 | if (curr->link == NULL) break; 89 | curr = curr->link; 90 | } 91 | 92 | newPtr->link = curr->link; 93 | curr->link = newPtr; 94 | } 95 | } 96 | 97 | void PrintAll(nptr ptr) { 98 | nptr curr = ptr; 99 | 100 | while (1) { 101 | if (curr == NULL) break; 102 | printf("%d ", curr->data); 103 | curr = curr->link; 104 | } 105 | 106 | printf("\n"); 107 | } -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/20p2.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | typedef struct node* nptr; 9 | 10 | typedef struct node { 11 | int data; 12 | nptr *link; 13 | } NODE; 14 | 15 | void Insert(nptr*, int); 16 | void Sort(nptr*); 17 | void PrintAll(nptr); 18 | 19 | int main() { 20 | int A[10] = { 3, 9, 8, 2, 5, 10, 7, 1, 4, 6 }; 21 | 22 | nptr head = NULL; 23 | 24 | for (int i = 0; i < 10; i++) { 25 | Insert(&head, A[i]); 26 | } 27 | 28 | PrintAll(head); 29 | 30 | Sort(&head); 31 | PrintAll(head); 32 | 33 | nptr curr = head->link, prev = head; 34 | while (1) { 35 | if (prev == NULL) break; 36 | free(prev); 37 | prev = curr; 38 | if (curr == NULL) break; 39 | curr = curr->link; 40 | } 41 | free(curr); 42 | 43 | return 0; 44 | } 45 | 46 | 47 | void Insert(nptr* head, int value) { 48 | nptr curr = *head; 49 | int i; 50 | 51 | nptr newPtr = (nptr)malloc(sizeof(NODE)); 52 | newPtr->data = value; 53 | newPtr->link = NULL; 54 | 55 | if (curr == NULL) { 56 | *head = newPtr; 57 | return; 58 | } 59 | 60 | while (1) { 61 | if (curr->link == NULL) break; 62 | curr = curr->link; 63 | } 64 | 65 | curr->link = newPtr; 66 | } 67 | 68 | void Sort(nptr* head) { 69 | nptr curr = *head; 70 | nptr ref = curr; 71 | int i, j, size = 0; 72 | 73 | while (1) { 74 | if (curr == NULL) break; 75 | size++; 76 | curr = curr->link; 77 | } 78 | 79 | curr = *head; 80 | 81 | for (i = size - 1; i >= 0; i--) { 82 | for (j = 0; j <= i; j++) { 83 | if (ref->link == NULL) break; 84 | 85 | if ((ref->data) > ((nptr) ref->link)->data) { 86 | int temp = ref->data; 87 | ref->data = ((nptr)ref->link)->data; 88 | ((nptr)ref->link)->data = temp; 89 | } 90 | ref = ref->link; 91 | } 92 | ref = curr; 93 | } 94 | } 95 | 96 | void PrintAll(nptr ptr) { 97 | nptr curr = ptr; 98 | 99 | while (1) { 100 | if (curr == NULL) break; 101 | printf("%d ", curr->data); 102 | curr = curr->link; 103 | } 104 | 105 | printf("\n"); 106 | } -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/21p1.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | typedef struct node* nptr; 9 | 10 | typedef struct node { 11 | int data; 12 | nptr *link; 13 | } NODE; 14 | 15 | void Insert(NODE**, int); 16 | 17 | void Delete(NODE**); 18 | 19 | void PrintAll(NODE*); 20 | 21 | 22 | int main() { 23 | int A[10] = { 3, 9, 8, 2, 5, 10, 7, 1, 4, 6 }; 24 | 25 | nptr head = NULL; 26 | 27 | for (int i = 0; i < 10; i++) { 28 | Insert(&head, A[i]); 29 | } 30 | PrintAll(head); 31 | 32 | for (int i = 0; i < 10; i++) { 33 | Delete(&head); 34 | PrintAll(head); 35 | } 36 | 37 | return 0; 38 | } 39 | 40 | 41 | void Insert(nptr* head, int value) { 42 | nptr curr = *head; 43 | int i; 44 | 45 | nptr newPtr = (nptr)malloc(sizeof(NODE)); 46 | newPtr->data = value; 47 | newPtr->link = NULL; 48 | 49 | if (curr == NULL) { 50 | *head = newPtr; 51 | return; 52 | } 53 | 54 | while (1) { 55 | if (curr->link == NULL) break; 56 | curr = curr->link; 57 | } 58 | 59 | curr->link = newPtr; 60 | } 61 | 62 | void Delete(nptr* head) { 63 | nptr temp = *head; 64 | *head = (*head)->link; 65 | free(temp); 66 | } 67 | 68 | void PrintAll(nptr ptr) { 69 | nptr curr = ptr; 70 | int count = 0; 71 | 72 | while (1) { 73 | if (curr == NULL) break; 74 | printf("%d ", curr->data); 75 | curr = curr->link; 76 | count++; 77 | } 78 | 79 | if (!count) printf("Empty"); 80 | 81 | printf("\n"); 82 | } -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/21p2.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | typedef struct _Node { 9 | char c; 10 | struct _Node *link; 11 | } Node; 12 | 13 | Node *head = NULL, *tail = NULL; 14 | 15 | void InsertNodeAtFront(char a); 16 | void InsertNodeAtBack(char b); 17 | void DeleteNodeAtFront(); 18 | void DeleteNodeAtBack(); 19 | void PrintList(); 20 | void FreeList(); 21 | 22 | int main() { 23 | InsertNodeAtFront('n'); 24 | InsertNodeAtFront('e'); 25 | InsertNodeAtFront('p'); 26 | InsertNodeAtBack('l'); 27 | InsertNodeAtFront('o'); 28 | InsertNodeAtBack('a'); 29 | InsertNodeAtBack('b'); 30 | PrintList(); 31 | 32 | DeleteNodeAtFront(); 33 | DeleteNodeAtBack(); 34 | DeleteNodeAtBack(); 35 | DeleteNodeAtBack(); 36 | PrintList(); 37 | 38 | FreeList(); 39 | 40 | return 0; 41 | } 42 | 43 | void InsertNodeAtFront(char a) { 44 | Node *newNode = (Node*)malloc(sizeof(Node)); 45 | newNode->c = a; 46 | newNode->link = NULL; 47 | 48 | if (head == NULL) { 49 | head = newNode; 50 | tail = head; 51 | } 52 | else { 53 | newNode->link = head; 54 | head = newNode; 55 | } 56 | } 57 | 58 | void InsertNodeAtBack(char b) { 59 | Node *newNode = (Node*)malloc(sizeof(Node)); 60 | newNode->c = b; 61 | newNode->link = NULL; 62 | 63 | if (head == NULL) { 64 | head = newNode; 65 | tail = head; 66 | } 67 | else { 68 | tail->link = newNode; 69 | tail = tail->link; 70 | } 71 | } 72 | 73 | void DeleteNodeAtFront() { 74 | Node* temp = head; 75 | head = head->link; 76 | free(temp); 77 | } 78 | 79 | void DeleteNodeAtBack() { 80 | Node *curr = head; 81 | 82 | while (1) { 83 | if (curr->link == NULL) break; 84 | if ((curr->link)->link == NULL) break; 85 | curr = curr->link; 86 | } 87 | 88 | free(curr->link); 89 | curr->link = NULL; 90 | } 91 | 92 | void PrintList() { 93 | Node *curr = head; 94 | 95 | while (1) { 96 | printf("%c", curr->c); 97 | if (curr->link == NULL) break; 98 | curr = curr->link; 99 | } 100 | 101 | printf("\n"); 102 | } 103 | 104 | void FreeList() { 105 | Node *prev = head; 106 | Node *curr = prev->link; 107 | 108 | while (1) { 109 | free(prev); 110 | prev = curr; 111 | if (curr->link == NULL) break; 112 | curr = curr->link; 113 | } 114 | 115 | free(curr); 116 | } -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/23p1.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | typedef struct node { 8 | int data; 9 | struct node* next; 10 | } QUEUE_NODE; 11 | 12 | typedef struct { 13 | QUEUE_NODE* front; 14 | int count; 15 | QUEUE_NODE* rear; 16 | } QUEUE; 17 | 18 | void Exit(QUEUE*); 19 | void Enqueue(QUEUE*); 20 | void PrintAll(QUEUE); 21 | 22 | int main() { 23 | QUEUE* q = (QUEUE*) malloc(sizeof(QUEUE)); 24 | q->count = 0; 25 | q->front = NULL; 26 | q->rear = NULL; 27 | 28 | int op; 29 | 30 | printf("****** Menu ******\n"); 31 | printf("* 0 : Exit *\n"); 32 | printf("* 1 : Enqueue *\n"); 33 | printf("* 2 : ------- *\n"); 34 | printf("* 3 : Print All *\n"); 35 | printf("******************\n"); 36 | 37 | int exitFlag = 0; 38 | 39 | while (!exitFlag) { 40 | printf("Select > "); 41 | scanf("%d", &op); 42 | 43 | switch (op) { 44 | case 0: 45 | exitFlag = 1; 46 | break; 47 | case 1: 48 | Enqueue(q); 49 | break; 50 | case 3: 51 | PrintAll(*q); 52 | break; 53 | default: 54 | break; 55 | } 56 | } 57 | 58 | Exit(q); 59 | 60 | return 0; 61 | } 62 | 63 | void Exit(QUEUE* queue) { 64 | if (queue->count != 0) { 65 | QUEUE_NODE* prev, *curr = queue->front; 66 | 67 | while (curr != NULL) { 68 | prev = curr; 69 | curr = curr->next; 70 | free(prev); 71 | } 72 | } 73 | 74 | free(queue); 75 | } 76 | 77 | void Enqueue(QUEUE * queue) { 78 | int data; 79 | printf("Input Data > "); 80 | scanf("%d", &data); 81 | 82 | QUEUE_NODE* newNode = (QUEUE_NODE*) malloc(sizeof(QUEUE_NODE)); 83 | newNode->data = data; 84 | newNode->next = NULL; 85 | 86 | if (queue->count == 0) { 87 | queue->front = newNode; 88 | queue->rear = newNode; 89 | } 90 | else { 91 | queue->rear->next = newNode; 92 | queue->rear = newNode; 93 | } 94 | 95 | queue->count++; 96 | } 97 | 98 | void PrintAll(QUEUE queue) { 99 | if (queue.count == 0) { 100 | printf("Queue is empty!\n"); 101 | return; 102 | } 103 | 104 | QUEUE_NODE* curr = queue.front; 105 | 106 | while (curr != NULL) { 107 | printf("%d ", curr->data); 108 | curr = curr->next; 109 | } 110 | 111 | printf("\n"); 112 | } -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/23p2.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | typedef struct node { 8 | char command[8]; 9 | struct node* next; 10 | } QUEUE_NODE; 11 | 12 | typedef struct { 13 | QUEUE_NODE* front; 14 | int count; 15 | QUEUE_NODE* rear; 16 | } QUEUE; 17 | 18 | void FreeQueue(QUEUE*); 19 | int CheckCommand(char* command); 20 | void EnqueueCommand(QUEUE*, char*); 21 | 22 | int main() { 23 | QUEUE* q = (QUEUE*) malloc(sizeof(QUEUE)); 24 | q->count = 0; 25 | q->front = NULL; 26 | q->rear = NULL; 27 | 28 | char op[1000]; 29 | 30 | while (1) { 31 | printf(">>"); 32 | scanf("%s", op); 33 | 34 | if (CheckCommand(op)) { 35 | if (!strcmp("h", op) || !strcmp("history", op)) { 36 | printf("queue count = %d\n", q->count); 37 | } 38 | else if (!strcmp("q", op) || !strcmp("quit", op)) { 39 | break; 40 | } else { 41 | printf("[Valid] %s\n", op); 42 | EnqueueCommand(q, op); 43 | } 44 | } 45 | else { 46 | printf("[Invalid]\n"); 47 | } 48 | } 49 | 50 | FreeQueue(q); 51 | 52 | return 0; 53 | } 54 | 55 | int CheckCommand(char* command) { 56 | char valid_op[][8] = { 57 | "help", "dir", "mkdir", "cd", "history", "h", "quit", "q" 58 | }; 59 | 60 | for (int i = 0; i < 8; i++) { 61 | if (!strcmp(command, valid_op[i])) return 1; 62 | } 63 | 64 | return 0; 65 | } 66 | 67 | void FreeQueue(QUEUE* queue) { 68 | if (queue->count != 0) { 69 | QUEUE_NODE* prev, *curr = queue->front; 70 | 71 | while (curr != NULL) { 72 | prev = curr; 73 | curr = curr->next; 74 | free(prev); 75 | } 76 | } 77 | 78 | free(queue); 79 | } 80 | 81 | void EnqueueCommand(QUEUE * pQueue, char* dataIn) { 82 | QUEUE_NODE* newNode = (QUEUE_NODE*) malloc(sizeof(QUEUE_NODE)); 83 | strcpy(newNode->command, dataIn); 84 | newNode->next = NULL; 85 | 86 | if (pQueue->count == 0) { 87 | pQueue->front = newNode; 88 | pQueue->rear = newNode; 89 | } 90 | else { 91 | pQueue->rear->next = newNode; 92 | pQueue->rear = newNode; 93 | } 94 | 95 | pQueue->count++; 96 | } -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/2p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void swap(int*, int*); 4 | 5 | int main() { 6 | int a, b; 7 | scanf("%d%d", &a, &b); 8 | swap(&a, &b); 9 | printf("%d %d\n", a, b); 10 | 11 | return 0; 12 | } 13 | 14 | void swap(int* a, int* b) { 15 | int temp; 16 | temp = *a; 17 | *a = *b; 18 | *b = temp; 19 | } 20 | -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/2p2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void swap(int*, int*); 4 | void sort4(int*, int*, int*, int*); 5 | 6 | int main() { 7 | int a, b, c, d; 8 | scanf("%d%d%d%d", &a, &b, &c, &d); 9 | sort4(&a, &b, &c, &d); 10 | printf("%d %d %d %d\n", a, b, c, d); 11 | 12 | return 0; 13 | } 14 | 15 | void swap(int* a, int* b) { 16 | int temp; 17 | temp = *a; 18 | *a = *b; 19 | *b = temp; 20 | } 21 | 22 | // a < b < c < d 23 | void sort4(int* a, int* b, int* c, int* d) { 24 | if (*a > *b) swap(a, b); 25 | if (*b > *c) swap(b, c); 26 | if (*c > *d) swap(c, d); 27 | if (*a > *b) swap(a, b); 28 | if (*b > *c) swap(b, c); 29 | if (*a > *b) swap(a, b); 30 | } 31 | -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/3p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void f(int*, int*, int*, int); 4 | 5 | int main() { 6 | int h, m, s, t; 7 | scanf("%d%d%d%d", &h, &m, &s, &t); 8 | 9 | f(&h, &m, &s, t); 10 | printf("%02d:%02d:%02d\n", h, m, s); 11 | return 0; 12 | } 13 | 14 | void f(int* h, int* m, int* s, int t) { 15 | *s += t; 16 | *m += *s / 60; 17 | *s %= 60; 18 | *h += *m / 60; 19 | *m %= 60; 20 | *h %= 24; 21 | } 22 | -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/3p2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void is_prime(int***); 4 | 5 | int main() { 6 | int a; 7 | scanf("%d", &a); 8 | 9 | int *pa; 10 | pa = &a; 11 | int **ppa; 12 | ppa = &pa; 13 | 14 | is_prime(&ppa); 15 | 16 | printf("%d\n", **ppa); 17 | 18 | return 0; 19 | } 20 | 21 | void is_prime(int*** a) { 22 | if (***a <= 1) { 23 | ***a = 0; 24 | } 25 | else { 26 | int p[201]; 27 | for (int i = 0; i <= 200; i++) p[i] = 1; 28 | 29 | for (int i = 2; i <= 200; i++) { 30 | if (p[i]) for (int j = i * 2; j <= 200; j += i) { 31 | p[j] = 0; 32 | } 33 | } 34 | 35 | ***a = p[***a]; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/3p3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void* f(void*, void*, int); 4 | 5 | int main() { 6 | int option; 7 | int a, b; 8 | float fa, fb; 9 | double da, db; 10 | void *pa, *pb, *ans; 11 | 12 | scanf("%d", &option); 13 | 14 | switch (option) 15 | { 16 | case 0: 17 | scanf("%d%d", &a, &b); 18 | pa = &a; 19 | pb = &b; 20 | 21 | ans = f(pa, pb, 0); 22 | printf("%d\n", *((int*)ans)); 23 | break; 24 | case 1: 25 | scanf("%f%f", &fa, &fb); 26 | pa = &fa; 27 | pb = &fb; 28 | 29 | ans = f(pa, pb, 1); 30 | printf("%f\n", *((float*)ans)); 31 | break; 32 | case 2: 33 | scanf("%lf%lf", &da, &db); 34 | pa = &da; 35 | pb = &db; 36 | 37 | ans = f(pa, pb, 2); 38 | printf("%lf\n", *((double*)ans)); 39 | break; 40 | default: 41 | break; 42 | } 43 | 44 | return 0; 45 | } 46 | 47 | void* f(void* pa, void* pb, int option) { 48 | int *a0, *b0; 49 | float *a1, *b1; 50 | double *a2, *b2; 51 | 52 | switch (option) 53 | { 54 | case 0: 55 | a0 = (int*) pa; 56 | b0 = (int*) pb; 57 | 58 | if (*a0 > *b0) { 59 | return pb; 60 | } 61 | else { 62 | return pa; 63 | } 64 | break; 65 | case 1: 66 | a1 = (float*)pa; 67 | b1 = (float*)pb; 68 | 69 | if (*a1 > *b1) { 70 | return pb; 71 | } 72 | else { 73 | return pa; 74 | } 75 | break; 76 | case 2: 77 | a2 = (double*)pa; 78 | b2 = (double*)pb; 79 | 80 | if (*a2 > *b2) { 81 | return pb; 82 | } 83 | else { 84 | return pa; 85 | } 86 | break; 87 | default: 88 | break; 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/3p4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void swap(int*, int*); 4 | void sort(int*, int); 5 | void print_array(int*, int); 6 | 7 | int main() { 8 | int int_size = sizeof(int); 9 | int n, a[1000]; 10 | scanf("%d", &n); 11 | 12 | for (int i = 0; i < n; i++) { 13 | scanf("%d", a + i * int_size); 14 | } 15 | 16 | sort(a, n); 17 | print_array(a, n); 18 | 19 | return 0; 20 | } 21 | 22 | void swap(int* a, int* b) { 23 | int temp = *a; 24 | *a = *b; 25 | *b = temp; 26 | } 27 | 28 | void sort(int* a, int n) { 29 | int int_size = sizeof(int); 30 | 31 | for (int i = n - 1; i >= 0; i--) for (int j = 0; j < i; j++) { 32 | if (*(a + j * int_size) > *(a + (j + 1) * int_size)) { 33 | swap(a + j * int_size, a + (j + 1) * int_size); 34 | } 35 | } 36 | } 37 | 38 | void print_array(int* a, int n) { 39 | int int_size = sizeof(int); 40 | 41 | for (int i = 0; i < n; i++) { 42 | printf("%d ", *(a + i * int_size)); 43 | } 44 | 45 | printf("\n"); 46 | } 47 | -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/4p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void shift_right(int*); 4 | void shift_n_right(int*, int); 5 | void print_array(int*); 6 | 7 | int main() { 8 | int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; 9 | int n; 10 | scanf("%d", &n); 11 | 12 | shift_n_right(a, n); 13 | print_array(a); 14 | 15 | return 0; 16 | } 17 | 18 | void shift_right(int* a) { 19 | int temp = *(a + 9); 20 | for (int i = 9; i >= 1; i--) { 21 | *(a + i) = *(a + i - 1); 22 | } 23 | *(a + 0) = temp; 24 | } 25 | 26 | void shift_n_right(int* a, int n) { 27 | while (n--) shift_right(a); 28 | } 29 | 30 | void print_array(int* a) { 31 | for (int i = 0; i < 10; i++) { 32 | printf("%d ", *(a + i)); 33 | } 34 | printf("\n"); 35 | } 36 | -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/4p2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void mat_mul(int[100][100], int[100][100], int[100][100], int); 4 | 5 | int main() { 6 | int n, a[100][100], b[100][100], c[100][100], i, j; 7 | 8 | scanf("%d", &n); 9 | 10 | for (i = 0; i < n; i++) for (j = 0; j < n; j++) { 11 | scanf("%d", *(a + i) + j); // &(*(*(a + i) + j)) == *(a + i) + j 12 | } 13 | for (i = 0; i < n; i++) for (j = 0; j < n; j++) { 14 | scanf("%d", *(b + i) + j); // &(*(*(a + i) + j)) == *(a + i) + j 15 | } 16 | 17 | mat_mul(a, b, c, n); 18 | 19 | for (i = 0; i < n; i++) { 20 | for (j = 0; j < n; j++) { 21 | printf("%d ", *(*(c + i) + j)); 22 | } 23 | printf("\n"); 24 | } 25 | 26 | return 0; 27 | } 28 | 29 | void mat_mul(int a[100][100], int b[100][100], int c[100][100], int n) { 30 | int i, j, k, sum; 31 | 32 | for (i = 0; i < n; i++) for (j = 0; j < n; j++) { 33 | sum = 0; 34 | 35 | for (k = 0; k < n; k++) { 36 | sum += (*(*(a + i) + k)) * (*(*(b + k) + j)); 37 | } 38 | 39 | *(*(c + i) + j) = sum; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/4p3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int h, w, n, a[9][99], i, j; 5 | scanf("%d%d%d", &h, &w, &n); 6 | 7 | for (i = 0; i < h; i++) for (j = 0; j < w; j++) { 8 | *(*(a + i) + j) = (i + 1) * 100 + (j + 1); 9 | } 10 | 11 | for (j = 0; j < w; j++) { 12 | for (i = 0; i < h; i++) { 13 | n--; 14 | if (n == 0) break; 15 | } 16 | if (n == 0) { 17 | printf("%d", *(*(a + i) + j)); 18 | break; 19 | } 20 | } 21 | 22 | printf("\n"); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/5p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void swap(int*, int*); 5 | void sort(int*, int); 6 | void print_array(int*, int); 7 | void all_sort(int[][100], int, int); 8 | void print_all_array(int[][100], int, int); 9 | 10 | int main() { 11 | int i, j, n, m; 12 | int a[100][100]; 13 | 14 | scanf("%d%d", &n, &m); 15 | 16 | for (i = 0; i < n; i++) { 17 | for (j = 0; j < m; j++) { 18 | scanf("%d", (*(a + i) + j)); 19 | } 20 | } 21 | 22 | all_sort(a, n, m); 23 | print_all_array(a, n, m); 24 | 25 | return 0; 26 | } 27 | 28 | void swap(int* a, int* b) { 29 | int temp = *a; 30 | *a = *b; 31 | *b = temp; 32 | } 33 | 34 | void sort(int* a, int m) { 35 | int i, j; 36 | 37 | for (i = m - 1; i >= 0; i--) { 38 | for (j = 1; j <= i; j++) { 39 | if (*(a + j - 1) > *(a + j)) { 40 | swap((a + j - 1), (a + j)); 41 | } 42 | } 43 | } 44 | } 45 | 46 | void print_array(int* a, int m) { 47 | int i; 48 | 49 | for (i = 0; i < m; i++) { 50 | printf("%d ", *(a + i)); 51 | } 52 | } 53 | 54 | void all_sort(int a[][100], int n, int m) { 55 | int i; 56 | 57 | for (i = 0; i < n; i++) { 58 | sort(a[i], m); 59 | } 60 | } 61 | 62 | void print_all_array(int a[][100], int n, int m) { 63 | int i; 64 | 65 | for (i = 0; i < n; i++) { 66 | print_array(a[i], m); 67 | printf("\n"); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/5p2.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | void swap(int*, int*); 6 | void sort(int*, int); 7 | void all_sort(int[][100], int, int); 8 | int binary_search(int*, int*, int, int**); 9 | int all_binary_search(int[][100], int, int, int, int**); 10 | 11 | int main() { 12 | int i, j, n, m, k, result, *findPtr; 13 | int a[100][100]; 14 | 15 | scanf("%d%d%d", &n, &m, &k); 16 | 17 | for (i = 0; i < n; i++) { 18 | for (j = 0; j < m; j++) { 19 | scanf("%d", (*(a + i) + j)); 20 | } 21 | } 22 | 23 | all_sort(a, n, m); 24 | result = all_binary_search(a, n, m, k, &findPtr); 25 | 26 | printf("%d\n%p\n", result, findPtr); 27 | 28 | return 0; 29 | } 30 | 31 | void swap(int* a, int* b) { 32 | int temp = *a; 33 | *a = *b; 34 | *b = temp; 35 | } 36 | 37 | void sort(int* a, int m) { 38 | int i, j; 39 | 40 | for (i = m - 1; i >= 0; i--) { 41 | for (j = 1; j <= i; j++) { 42 | if (*(a + j - 1) > *(a + j)) { 43 | swap((a + j - 1), (a + j)); 44 | } 45 | } 46 | } 47 | } 48 | 49 | void all_sort(int a[][100], int n, int m) { 50 | int i; 51 | 52 | for (i = 0; i < n; i++) { 53 | sort(a[i], m); 54 | } 55 | } 56 | 57 | int binary_search(int* a, int* endPtr, int key, int** findPtr) { 58 | int *l, *m, *r; 59 | 60 | l = a; 61 | r = endPtr; 62 | 63 | while (l <= r) { 64 | m = l + (r - l) / 2; 65 | 66 | if (key > *m) { 67 | l = m + 1; 68 | } else if (key < *m) { 69 | r = m - 1; 70 | } else { 71 | l = r + 1; 72 | } 73 | } 74 | *findPtr = m; 75 | return (key == *m); 76 | } 77 | 78 | int all_binary_search(int a[][100], int n, int m, int key, int **findPtr) { 79 | int i; 80 | int flag = 0; 81 | 82 | for (i = 0; i < n; i++) { 83 | if (binary_search(a[i], a[i] + m - 1, key, findPtr)) { 84 | flag = 1; 85 | break; 86 | } 87 | } 88 | 89 | return flag; 90 | } 91 | -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/6p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void show_memory(void) { 6 | FILE* status = fopen("/proc/self/status", "r"); 7 | char buffer[256]; 8 | while(fgets(buffer, sizeof(buffer) - 1, status)) { 9 | buffer[sizeof(buffer) -1] = '\0'; 10 | if (strncmp(buffer, "VmSize:", 7) == 0) 11 | printf("memory:\t%d kb\n", atoi(buffer + 8)); 12 | } 13 | fclose(status); 14 | } 15 | 16 | 17 | int main() { 18 | int* test; 19 | 20 | printf("*** Initial ***\n"); 21 | printf("test:\t%p\n", test); 22 | printf("&test:\t%p\n", &test); 23 | show_memory(); 24 | 25 | test = (int*) malloc(sizeof(int) * 100000); 26 | 27 | printf("*** malloc ***\n"); 28 | printf("test:\t%p\n", test); 29 | printf("&test:\t%p\n", &test); 30 | printf("&test[0]:\t%p\n", &test[0]); 31 | show_memory(); 32 | 33 | free(test); 34 | 35 | printf("*** free ***\n"); 36 | printf("test:\t%p\n", test); 37 | printf("&test:\t%p\n", &test); 38 | printf("&test[0]:\t%p\n", &test[0]); 39 | show_memory(); 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/6p2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int* get_new_array(int); 5 | void get_new_array_call_by_value(int*, int); 6 | void get_new_array_call_by_reference(int**, int); 7 | void print_state(int*); 8 | 9 | int main() { 10 | int *a = NULL; 11 | print_state(a); 12 | 13 | get_new_array_call_by_value(a, 10); 14 | print_state(a); 15 | free(a); 16 | 17 | get_new_array_call_by_reference(&a, 10); 18 | print_state(a); 19 | free(a); 20 | 21 | a = get_new_array(10); 22 | print_state(a); 23 | free(a); 24 | 25 | return 0; 26 | } 27 | 28 | int* get_new_array(int size) { 29 | return (int*) malloc(size * sizeof(int)); 30 | } 31 | 32 | void get_new_array_call_by_value(int* a, int size) { 33 | a = (int*) malloc(size * sizeof(int)); 34 | 35 | /* 36 | * This code does nothing to the pointer in main() scope 37 | * because it sets the 'value' of 'local' variable a in 38 | * this function scope, which has type of integer pointer, 39 | * to the newly allocated array. 40 | */ 41 | } 42 | 43 | void get_new_array_call_by_reference(int** a, int size) { 44 | *a = (int*) malloc(size * sizeof(int)); 45 | } 46 | 47 | void print_state(int* a) { 48 | if (a == NULL) { 49 | printf("not allocated\n"); 50 | } else { 51 | printf("allocated\n"); 52 | } 53 | } 54 | 55 | -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/6p3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void swap(int*, int*); 5 | void sort(int*, int); 6 | int* get_next_process(int*, int); 7 | void print_array(int*, int); 8 | 9 | int main() { 10 | int n, i; 11 | int* arr; 12 | scanf("%d", &n); 13 | 14 | arr = (int*) malloc(n * sizeof(int)); 15 | 16 | for (i = 0; i < n; i++) { 17 | scanf("%d", arr + i); // &(*(arr + i)) == arr + i 18 | } 19 | 20 | do { 21 | arr = get_next_process(arr, n); 22 | n = (n + 1) / 2; 23 | } while (n > 1); 24 | 25 | return 0; 26 | } 27 | 28 | void swap(int* a, int* b) { 29 | int temp = *a; 30 | *a = *b; 31 | *b = temp; 32 | } 33 | 34 | void sort(int* a, int m) { 35 | int i, j; 36 | 37 | for (i = m - 1; i >= 0; i--) { 38 | for (j = 1; j <= i; j++) { 39 | if (*(a + j - 1) < *(a + j)) { 40 | swap((a + j - 1), (a + j)); 41 | } 42 | } 43 | } 44 | } 45 | 46 | int* get_next_process(int* prev_pointer, int size) { 47 | int next_size, i; 48 | int *next_pointer; 49 | 50 | sort(prev_pointer, size); 51 | 52 | next_size = (size + 1) / 2; 53 | next_pointer = (int*) calloc(next_size * sizeof(int), 0); 54 | 55 | for (i = next_size - 1; i >= 0; i--) { 56 | if (i == next_size - 1) { 57 | if (size % 2 == 0) { 58 | *(next_pointer + i) = *(prev_pointer + 2 * i) + *(prev_pointer + 2 * i + 1); 59 | } else { 60 | *(next_pointer + i) = *(prev_pointer + 2 * i); 61 | } 62 | } else { 63 | *(next_pointer + i) = *(prev_pointer + 2 * i) + *(prev_pointer + 2 * i + 1); 64 | } 65 | } 66 | 67 | print_array(next_pointer, next_size); 68 | 69 | return next_pointer; 70 | } 71 | 72 | void print_array(int* arr, int size) { 73 | int i; 74 | 75 | for (i = 0; i < size; i++) { 76 | printf("%d ", *(arr + i)); 77 | } 78 | 79 | printf("\n"); 80 | } 81 | -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/7p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void swap(int*, int*); 5 | void sort(int*, int); 6 | void double_double(int**, int*); 7 | void print_array(int*, int); 8 | 9 | int main() { 10 | int n, i; 11 | scanf("%d", &n); 12 | 13 | int* arr = (int*) malloc(n * sizeof(int)); 14 | 15 | for (i = 0; i < n; i++) { 16 | scanf("%d", arr + i); 17 | } 18 | 19 | for (i = 0; i < 3; i++) { 20 | double_double(&arr, &n); 21 | print_array(arr, n); 22 | } 23 | 24 | free(arr); 25 | 26 | return 0; 27 | } 28 | 29 | void swap(int* a, int* b) { 30 | int temp = *a; 31 | *a = *b; 32 | *b = temp; 33 | } 34 | 35 | void sort(int* a, int size) { 36 | int i, j; 37 | 38 | for (i = 0; i < size; i++) for (j = size - 1; j > i; j--) { 39 | if (*(a + j) < *(a + j - 1)) { 40 | swap(a + j, a + j - 1); 41 | } 42 | } 43 | } 44 | 45 | void double_double(int** a, int* size) { 46 | int i; 47 | 48 | *a = (int*) realloc(*a, *size * 2 * sizeof(int)); 49 | 50 | for (i = *size - 1; i >= 0; i--) { 51 | *(*a + i + *size) = (*(*a + i)) * 2; 52 | } 53 | 54 | sort(*a, *size * 2); 55 | 56 | *size *= 2; 57 | } 58 | 59 | void print_array(int* a, int size) { 60 | int i; 61 | for (i = 0; i < size; i++) { 62 | printf("%d ", *(a + i)); 63 | } 64 | printf("\n"); 65 | } 66 | -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/7p2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void print_array(int*, int); 5 | void push_back(int**, int*, int); 6 | void pop_back(int**, int*); 7 | double average(int*, int); 8 | double variance(int*, int); 9 | 10 | int main() { 11 | int* a = (int*) malloc(0); 12 | int n, i, size = 0, op, x; 13 | scanf("%d", &n); 14 | 15 | for (i = 0; i < n; i++) { 16 | scanf("%d", &op); 17 | 18 | if (op == 1) { // push_back 19 | scanf("%d", &x); 20 | push_back(&a, &size, x); 21 | } else if (op == 2) { // pop_back 22 | pop_back(&a, &size); 23 | } 24 | } 25 | 26 | free(a); 27 | 28 | return 0; 29 | } 30 | 31 | void print_array(int* a, int size) { 32 | int i; 33 | 34 | for (int i = 0; i < size; i++) { 35 | printf("%d ", *(a + i)); 36 | } 37 | printf("\n"); 38 | } 39 | 40 | void push_back(int** a, int* size, int value) { 41 | (*size)++; 42 | *a = (int*) realloc(*a, *size * sizeof(int)); 43 | *(*a + *size - 1) = value; 44 | 45 | print_array(*a, *size); 46 | printf("%lf %lf\n", average(*a, *size), variance(*a, *size)); 47 | } 48 | 49 | void pop_back(int** a, int* size) { 50 | if (*size == 0) return; 51 | 52 | (*size)--; 53 | *a = (int*) realloc(*a, *size * sizeof(int)); 54 | 55 | print_array(*a, *size); 56 | printf("%lf %lf\n", average(*a, *size), variance(*a, *size)); 57 | } 58 | 59 | double average(int* a, int size) { 60 | int i; 61 | double sum = 0.0; 62 | 63 | if (size == 0) return 0.0; 64 | 65 | for (i = 0; i < size; i++) { 66 | sum += *(a + i); 67 | } 68 | 69 | return sum / size; 70 | } 71 | 72 | double variance(int* a, int size) { 73 | int i; 74 | double sum = 0.0, avg = average(a, size); 75 | 76 | if (size == 0) return 0.0; 77 | 78 | for (i = 0; i < size; i++) { 79 | sum += (*(a + i) - avg) * (*(a + i) - avg); 80 | } 81 | 82 | return sum / size; 83 | } 84 | 85 | -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/8p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void multiply_matrix(int**, int**, int**, int); 5 | void print_matrix(int**, int); 6 | 7 | int main() { 8 | int n, i, j, **a, **b, **c; 9 | scanf("%d", &n); 10 | 11 | a = (int**) malloc(n * sizeof(int*)); 12 | b = (int**) malloc(n * sizeof(int*)); 13 | c = (int**) malloc(n * sizeof(int*)); 14 | 15 | for (i = 0; i < n; i++) { 16 | a[i] = (int*) malloc(n * sizeof(int)); 17 | b[i] = (int*) malloc(n * sizeof(int)); 18 | c[i] = (int*) malloc(n * sizeof(int)); 19 | } 20 | 21 | for (i = 0; i < n; i++) for (j = 0; j < n; j++) { 22 | scanf("%d", &a[i][j]); 23 | } 24 | 25 | for (i = 0; i < n; i++) for (j = 0; j < n; j++) { 26 | scanf("%d", &b[i][j]); 27 | } 28 | 29 | multiply_matrix(a, b, c, n); 30 | print_matrix(c, n); 31 | 32 | for (i = 0; i < n; i++) { 33 | free(a[i]); 34 | free(b[i]); 35 | free(c[i]); 36 | } 37 | 38 | free(a); 39 | free(b); 40 | free(c); 41 | 42 | return 0; 43 | } 44 | 45 | void multiply_matrix(int** a, int** b, int** c, int n) { 46 | int i, j, k; 47 | 48 | for (i = 0; i < n; i++) for (j = 0; j < n; j++) { 49 | c[i][j] = 0; 50 | 51 | for (k = 0; k < n; k++) { 52 | c[i][j] += a[i][k] * b[k][j]; 53 | } 54 | } 55 | } 56 | 57 | void print_matrix(int** a, int n) { 58 | int i, j; 59 | 60 | for (i = 0; i < n; i++) { 61 | for (j = 0; j < n; j++) { 62 | printf("%d ", a[i][j]); 63 | } 64 | printf("\n"); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/8p2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void read_box(char*, int***, int*, int*); 5 | void print_box(int**, int, int); 6 | int drop_box(int**, int, int); 7 | void free_box(int***, int); 8 | 9 | int main() { 10 | int row, col, **box; 11 | char filename[20]; 12 | scanf("%s", filename); 13 | 14 | read_box(filename, &box, &row, &col); 15 | print_box(box, row, col); 16 | printf("%d\n", drop_box(box, row, col)); 17 | 18 | free_box(&box, row); 19 | 20 | return 0; 21 | } 22 | 23 | void read_box(char* filename, int*** box, int* row, int* col) { 24 | int i, j; 25 | 26 | FILE* file = fopen(filename, "r"); 27 | if (file == NULL) return; 28 | 29 | fscanf(file, "%d%d", row, col); 30 | 31 | *box = (int**) malloc(*row * sizeof(int*)); 32 | for (i = 0; i < *row; i++) { 33 | (*box)[i] = (int*) malloc(*col * sizeof(int)); 34 | } 35 | for (i = 0; i < *row; i++) for (j = 0; j < *col; j++) { 36 | fscanf(file, "%d", &((*box)[i][j])); 37 | } 38 | 39 | fclose(file); 40 | } 41 | 42 | void print_box(int **box, int row, int col) { 43 | int i, j; 44 | 45 | printf("%d %d\n", row, col); 46 | 47 | for (i = 0; i < row; i++) { 48 | for (j = 0; j < col; j++) { 49 | printf("%d ", box[i][j]); 50 | } 51 | printf("\n"); 52 | } 53 | } 54 | 55 | int drop_box(int **box, int row, int col) { 56 | int i = -1, j = 0, flag; 57 | 58 | while (i < row - 1) { 59 | flag = 0; 60 | 61 | while (j < col) { 62 | if (box[i + 1][j] == 0) { 63 | i++; 64 | flag = 1; 65 | break; 66 | } else { 67 | j++; 68 | } 69 | 70 | // printf("> %d %d\n", i, j); 71 | } 72 | 73 | if (!flag) { 74 | break; 75 | } 76 | } 77 | 78 | return j + 1; 79 | } 80 | 81 | void free_box(int ***box, int row) { 82 | int i; 83 | 84 | for (i = 0; i < row; i++) { 85 | free((*box)[i]); 86 | } 87 | 88 | free(*box); 89 | } 90 | 91 | -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/8p3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | int n, i, j, **p; 6 | scanf("%d", &n); 7 | 8 | p = (int**) malloc(n * sizeof(int*)); 9 | for (i = 0; i < n; i++) { 10 | p[i] = (int*) malloc((i + 1) * sizeof(int)); 11 | } 12 | 13 | p[0][0] = 1; 14 | 15 | for (i = 0; i < n; i++) { 16 | p[i][0] = 1; 17 | p[i][i] = 1; 18 | } 19 | 20 | for (i = 1; i < n; i++) for (j = 1; j < i; j++) { 21 | p[i][j] = p[i - 1][j - 1] + p[i - 1][j]; 22 | } 23 | 24 | for (i = 0; i < n; i++) { 25 | for (j = 0; j <= i; j++) { 26 | printf("%d ", p[i][j]); 27 | } 28 | 29 | printf("\n"); 30 | } 31 | 32 | for (i = 0; i < n; i++) { 33 | free(p[i]); 34 | } 35 | 36 | free(p); 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/9p1.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shiftpsh/sgcs/9456210b675ee514384d2b7867036bea5f525211/CSE2035 C Programming/exercise/9p1.c -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/9p2.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shiftpsh/sgcs/9456210b675ee514384d2b7867036bea5f525211/CSE2035 C Programming/exercise/9p2.c -------------------------------------------------------------------------------- /CSE2035 C Programming/exercise/9p3.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | int strlen(char[]); 8 | char* crypt(int, char[]); 9 | 10 | int main() { 11 | int i, n; 12 | char str[50]; 13 | scanf("%s", &str); 14 | 15 | srand(time(NULL)); 16 | 17 | for (i = 0; i < 5; i++) { 18 | n = rand() % 25 + 1; 19 | printf("%2d %s\n", n, crypt(n, str)); 20 | } 21 | 22 | return 0; 23 | } 24 | 25 | int strlen(char str[]) { 26 | int i = 0; 27 | 28 | while (str[i] != '\0') i++; 29 | 30 | return i; 31 | } 32 | 33 | char* crypt(int key, char str[]) { 34 | int l = strlen(str), i, x; 35 | char* res = (char*)malloc((l + 1) * sizeof(char)); 36 | res[l] = '\0'; 37 | 38 | for (i = 0; i < l; i++) { 39 | x = str[i] - 'a'; 40 | x += key; 41 | x %= 26; 42 | res[i] = x + 'A'; 43 | } 44 | 45 | return res; 46 | } -------------------------------------------------------------------------------- /CSE2035 C Programming/final/final-1.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shiftpsh/sgcs/9456210b675ee514384d2b7867036bea5f525211/CSE2035 C Programming/final/final-1.c -------------------------------------------------------------------------------- /CSE2035 C Programming/final/final-2.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shiftpsh/sgcs/9456210b675ee514384d2b7867036bea5f525211/CSE2035 C Programming/final/final-2.c -------------------------------------------------------------------------------- /CSE2035 C Programming/final/final-3.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shiftpsh/sgcs/9456210b675ee514384d2b7867036bea5f525211/CSE2035 C Programming/final/final-3.c -------------------------------------------------------------------------------- /CSE2035 C Programming/final/final-4.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shiftpsh/sgcs/9456210b675ee514384d2b7867036bea5f525211/CSE2035 C Programming/final/final-4.c -------------------------------------------------------------------------------- /CSE2035 C Programming/midterm/midterm-1.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | 3 | #include 4 | #include 5 | 6 | void pointerSort(int**, int, int); 7 | void swap(int**, int**); 8 | void printArray(int**, int); 9 | void printPointerArray(int**, int); 10 | 11 | int main() { 12 | int ASC = 0, DESC = 1; 13 | int n, i, *ref, **arr; 14 | scanf("%d", &n); 15 | 16 | ref = (int*)malloc(n * sizeof(int)); 17 | arr = (int**)malloc(n * sizeof(int*)); 18 | 19 | for (i = 0; i < n; i++) { 20 | scanf("%d", &ref[i]); 21 | arr[i] = &(ref[i]); 22 | } 23 | 24 | pointerSort(arr, n, ASC); 25 | printPointerArray(arr, n); 26 | 27 | pointerSort(arr, n, DESC); 28 | printPointerArray(arr, n); 29 | 30 | printArray(ref, n); 31 | 32 | free(ref); 33 | free(arr); 34 | 35 | return 0; 36 | } 37 | 38 | void pointerSort(int** arr, int size, int flag) { 39 | int ASC = 0, DESC = 1; 40 | int i, j; 41 | 42 | for (i = 0; i < size; i++) for (j = i; j < size; j++) { 43 | if (*(arr[i]) < *(arr[j])) { 44 | if (flag == DESC) swap(&(arr[i]), &(arr[j])); 45 | } 46 | else { 47 | if (flag == ASC) swap(&(arr[i]), &(arr[j])); 48 | } 49 | } 50 | } 51 | 52 | void swap(int** a, int** b) { 53 | int* c = *a; 54 | *a = *b; 55 | *b = c; 56 | } 57 | 58 | void printArray(int* arr, int n) { 59 | int i; 60 | 61 | for (i = 0; i < n; i++) { 62 | printf("%d ", arr[i]); 63 | } 64 | 65 | printf("\n"); 66 | } 67 | 68 | void printPointerArray(int** arr, int n) { 69 | int i; 70 | 71 | for (i = 0; i < n; i++) { 72 | printf("%d ", *(arr[i])); 73 | } 74 | 75 | printf("\n"); 76 | } -------------------------------------------------------------------------------- /CSE2035 C Programming/midterm/midterm-2.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | int findNumberAndSum(char*, int); 8 | 9 | int main() { 10 | char str[1001]; 11 | scanf("%s", &str); 12 | 13 | printf("%d", findNumberAndSum(str, strlen(str))); 14 | 15 | return 0; 16 | } 17 | 18 | int findNumberAndSum(char* text, int length) { 19 | int sum = 0, temp = 0, i; 20 | char last = '\0'; 21 | 22 | for (i = 0; i < length; i++) { 23 | if ('0' <= text[i] && text[i] <= '9') { 24 | temp *= 10; 25 | temp += text[i] - '0'; 26 | } 27 | else { 28 | sum += temp; 29 | temp = 0; 30 | } 31 | } 32 | 33 | sum += temp; 34 | return sum; 35 | } -------------------------------------------------------------------------------- /CSE2035 C Programming/project/paper.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shiftpsh/sgcs/9456210b675ee514384d2b7867036bea5f525211/CSE2035 C Programming/project/paper.pdf -------------------------------------------------------------------------------- /CSE2035 C Programming/project/project_sources/flow/admin/admin_login_page.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Suhyun Park on 2018-12-04. 3 | // 4 | 5 | #include 6 | #include 7 | #include "../../language.h" 8 | #include "../main_menu.h" 9 | #include "admin_login_page.h" 10 | #include "admin_page.h" 11 | 12 | void admin_login_page() { 13 | Language lang = lang_korean(); 14 | 15 | char try_id[100], try_password[100]; 16 | int flag = 0; 17 | 18 | printf(lang.admin_login_title); 19 | 20 | for (int i = 3; i >= 0; i--) { 21 | if (i) { 22 | printf(lang.login_id); 23 | scanf("%s", try_id); 24 | printf(lang.login_password); 25 | scanf("%s", try_password); 26 | 27 | // Because who cares about security? 28 | if (strcmp(try_id, "admin") || strcmp(try_password, "password")) { 29 | printf(lang.error_credentials_mismatch); 30 | } else { 31 | flag = 1; 32 | break; 33 | } 34 | } else { 35 | printf(lang.error_login_failed); 36 | } 37 | } 38 | 39 | if (flag) { 40 | admin_page(); 41 | } else { 42 | main_menu(); 43 | } 44 | } -------------------------------------------------------------------------------- /CSE2035 C Programming/project/project_sources/flow/admin/admin_login_page.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Suhyun Park on 2018-12-04. 3 | // 4 | 5 | #ifndef CP_PROJECT_ADMIN_LOGIN_PAGE_H 6 | #define CP_PROJECT_ADMIN_LOGIN_PAGE_H 7 | 8 | void admin_login_page(); 9 | 10 | #endif //CP_PROJECT_ADMIN_LOGIN_PAGE_H 11 | -------------------------------------------------------------------------------- /CSE2035 C Programming/project/project_sources/flow/admin/admin_page.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Suhyun Park on 2018-12-04. 3 | // 4 | 5 | #ifndef CP_PROJECT_ADMIN_PAGE_H 6 | #define CP_PROJECT_ADMIN_PAGE_H 7 | 8 | #include "../../types/product.h" 9 | #include "../../types/order.h" 10 | 11 | void admin_page(); 12 | 13 | void admin_lookup_customers(); 14 | 15 | void admin_add_or_delete_products(); 16 | 17 | void admin_add_product(); 18 | 19 | void admin_remove_product(); 20 | 21 | void admin_edit_products(); 22 | 23 | void admin_edit_product(Product product); 24 | 25 | void admin_search_product(); 26 | 27 | void admin_product_list(ProductList *products); 28 | 29 | void admin_lookup_product(Product product); 30 | 31 | void admin_lookup_orders(); 32 | 33 | void admin_order_list(OrderList* orders); 34 | 35 | void admin_lookup_parcel_status(); 36 | 37 | void admin_order_list_select_to_edit(OrderList* orders); 38 | 39 | void admin_edit_parcel_status(OrderHistory order); 40 | 41 | #endif //CP_PROJECT_ADMIN_PAGE_H 42 | -------------------------------------------------------------------------------- /CSE2035 C Programming/project/project_sources/flow/main_menu.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Suhyun Park on 02/12/2018. 3 | // 4 | 5 | #include 6 | #include "../language.h" 7 | #include "../util/util_io.h" 8 | #include "admin/admin_login_page.h" 9 | #include "user/register_page.h" 10 | #include "user/login_page.h" 11 | #include "main_menu.h" 12 | #include "search_product_page.h" 13 | 14 | /// Main menu flow. 15 | void main_menu() { 16 | Language lang = lang_korean(); 17 | 18 | printf(lang.main_menu_title); 19 | printf(lang.main_menu_1_login); 20 | printf(lang.main_menu_2_signup); 21 | printf(lang.main_menu_3_search); 22 | printf(lang.main_menu_4_admin_login); 23 | printf(lang.main_menu_5_quit); 24 | printf(lang.message_select); 25 | 26 | int menu = scan_valid_int(1, 5, lang.error_out_of_range, lang.message_select); 27 | 28 | switch (menu) { 29 | case 1: 30 | login_page(); 31 | break; 32 | case 2: 33 | register_page(); 34 | break; 35 | case 3: 36 | search_product_page(); 37 | break; 38 | case 4: 39 | admin_login_page(); 40 | break; 41 | case 5: 42 | return; 43 | } 44 | } -------------------------------------------------------------------------------- /CSE2035 C Programming/project/project_sources/flow/main_menu.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Suhyun Park on 02/12/2018. 3 | // 4 | 5 | #ifndef CP_PROJECT_MAIN_MENU_H 6 | #define CP_PROJECT_MAIN_MENU_H 7 | 8 | void main_menu(); 9 | 10 | #endif //CP_PROJECT_MAIN_MENU_H 11 | -------------------------------------------------------------------------------- /CSE2035 C Programming/project/project_sources/flow/search_product_page.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Suhyun Park on 2018-12-08. 3 | // 4 | 5 | #include 6 | #include "../util/util_io.h" 7 | #include "../util/util_string.h" 8 | #include "../types/product.h" 9 | #include "main_menu.h" 10 | #include "search_product_page.h" 11 | 12 | /// Search products. Does not require login. 13 | void search_product_page() { 14 | ProductList *products = product_get_all(), *queried = NULL; 15 | char query[101]; 16 | int min, max; 17 | 18 | int exit_flag = 0; 19 | Language lang = lang_korean(); 20 | 21 | while (!exit_flag) { 22 | printf(lang.product_search_title); 23 | printf(lang.product_search_1_by_name); 24 | printf(lang.product_search_2_by_category); 25 | printf(lang.product_search_3_by_price); 26 | printf(lang.product_search_4_all); 27 | printf(lang.product_search_5_exit); 28 | 29 | printf(lang.message_select); 30 | 31 | int menu = scan_valid_int(1, 5, lang.error_out_of_range, lang.message_select); 32 | 33 | CLEAR_INPUT_BUFFER 34 | 35 | switch (menu) { 36 | case 1: 37 | printf(lang.message_search); 38 | fgets(query, 100, stdin); 39 | queried = product_query_by_name(products, string_trim(query), 0); 40 | break; 41 | case 2: 42 | printf(lang.message_search); 43 | fgets(query, 100, stdin); 44 | queried = product_query_by_category(products, string_trim(query), 0); 45 | break; 46 | case 3: 47 | printf(lang.message_minimum_price); 48 | min = scan_valid_int(0, 2000000000, lang.error_out_of_range, lang.message_minimum_price); 49 | printf(lang.message_maximum_price); 50 | max = scan_valid_int(min, 2000000000, lang.error_out_of_range, lang.message_minimum_price); 51 | queried = product_query_by_price(products, min, max, 0); 52 | break; 53 | case 4: 54 | queried = product_query_available(products, 0); 55 | break; 56 | case 5: 57 | exit_flag = 1; 58 | break; 59 | } 60 | 61 | if (menu != 5) { 62 | product_print_all(products, lang); 63 | list_free(queried, ProductType); 64 | } 65 | } 66 | 67 | list_free(products, ProductType); 68 | 69 | main_menu(); 70 | } -------------------------------------------------------------------------------- /CSE2035 C Programming/project/project_sources/flow/search_product_page.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Suhyun Park on 2018-12-08. 3 | // 4 | 5 | #ifndef CP_PROJECT_SEARCH_PRODUCT_PAGE_H 6 | #define CP_PROJECT_SEARCH_PRODUCT_PAGE_H 7 | 8 | #import "../types/product.h" 9 | 10 | void search_product_page(); 11 | 12 | #endif //CP_PROJECT_SEARCH_PRODUCT_PAGE_H 13 | -------------------------------------------------------------------------------- /CSE2035 C Programming/project/project_sources/flow/user/login_page.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Suhyun Park on 02/12/2018. 3 | // 4 | 5 | #include 6 | #include 7 | #include "../../language.h" 8 | #include "../../types/member.h" 9 | #include "../main_menu.h" 10 | #include "login_page.h" 11 | #include "user_page.h" 12 | 13 | /// Login page flow. 14 | void login_page() { 15 | MemberList *members = member_get_all(); 16 | Language lang = lang_korean(); 17 | 18 | char try_id[100], try_password[100]; 19 | Member session; 20 | int flag = 0; 21 | 22 | printf(lang.login_title); 23 | 24 | for (int i = 3; i >= 0; i--) { 25 | if (i) { 26 | printf(lang.login_id); 27 | scanf("%s", try_id); 28 | printf(lang.login_password); 29 | scanf("%s", try_password); 30 | 31 | session = member_with_id(members, try_id); 32 | if (session._idx != -1) { 33 | if (strcmp(session.password, try_password) == 0 34 | && session.state != Unregistered) { 35 | flag = 1; 36 | break; 37 | } else { 38 | printf(lang.error_credentials_mismatch); 39 | } 40 | } else { 41 | printf(lang.error_credentials_mismatch); 42 | } 43 | } else { 44 | printf(lang.error_login_failed); 45 | } 46 | } 47 | 48 | list_free(members, MemberType); 49 | 50 | if (flag) { 51 | user_page(&session); 52 | } else { 53 | main_menu(); 54 | } 55 | } -------------------------------------------------------------------------------- /CSE2035 C Programming/project/project_sources/flow/user/login_page.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Suhyun Park on 02/12/2018. 3 | // 4 | 5 | #ifndef CP_PROJECT_LOGIN_PAGE_H 6 | #define CP_PROJECT_LOGIN_PAGE_H 7 | 8 | void login_page(); 9 | 10 | #endif //CP_PROJECT_LOGIN_PAGE_H 11 | -------------------------------------------------------------------------------- /CSE2035 C Programming/project/project_sources/flow/user/register_page.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Suhyun Park on 02/12/2018. 3 | // 4 | 5 | #include 6 | #include 7 | #include "../../util/util_string.h" 8 | #include "../../util/util_io.h" 9 | #include "../../types/member.h" 10 | #include "register_page.h" 11 | #include "user_page.h" 12 | 13 | /// Register page flow. 14 | void register_page() { 15 | MemberList *members = member_get_all(); 16 | Member m; 17 | Language lang = lang_korean(); 18 | char temp[1001]; 19 | temp[0] = '\0'; 20 | 21 | printf(lang.signup_title); 22 | 23 | // for some reason strcpy acts weird initially 24 | strcpy(m.name, ""); 25 | 26 | CLEAR_INPUT_BUFFER; 27 | 28 | printf(lang.signup_name); 29 | scan_valid_string(m.name, lang.error_invalid_string, lang.signup_name); 30 | 31 | printf(lang.signup_id); 32 | do { // ID conflict check 33 | scan_valid_id(m.id, lang.error_invalid_string, lang.signup_id); 34 | if (member_id_exists(members, m.id)) { 35 | printf(lang.error_id_confilct); 36 | printf(lang.signup_id); 37 | } 38 | } while (member_id_exists(members, m.id)); 39 | 40 | printf(lang.signup_password); 41 | scan_valid_string(m.password, lang.error_invalid_string, lang.signup_password); 42 | 43 | printf(lang.signup_re_password); 44 | do { // Password match check 45 | fgets(temp, 1000, stdin); 46 | temp[strlen(temp) - 1] = '\0'; 47 | if (strcmp(temp, m.password) != 0) { 48 | printf(lang.error_password_mismatch); 49 | printf(lang.signup_re_password); 50 | } 51 | } while (strcmp(temp, m.password) != 0); 52 | 53 | printf(lang.signup_address); 54 | scan_valid_string(m.address, lang.error_invalid_string, lang.signup_address); 55 | 56 | m._idx = members->size + 1; 57 | m.balance = 0; 58 | m.state = Registered; 59 | 60 | member_list_append(members, m); 61 | member_put_all(members); 62 | 63 | list_free(members, MemberType); 64 | 65 | user_page(&m); 66 | } 67 | -------------------------------------------------------------------------------- /CSE2035 C Programming/project/project_sources/flow/user/register_page.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Suhyun Park on 02/12/2018. 3 | // 4 | 5 | #ifndef CP_PROJECT_REGISTER_PAGE_H 6 | #define CP_PROJECT_REGISTER_PAGE_H 7 | 8 | void register_page(); 9 | 10 | #endif //CP_PROJECT_REGISTER_PAGE_H 11 | -------------------------------------------------------------------------------- /CSE2035 C Programming/project/project_sources/flow/user/user_page.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Suhyun Park on 02/12/2018. 3 | // 4 | 5 | #ifndef CP_PROJECT_USER_PAGE_H 6 | #define CP_PROJECT_USER_PAGE_H 7 | 8 | #include "../../types/member.h" 9 | #include "../../types/product.h" 10 | 11 | void user_page(Member *session); 12 | 13 | int user_edit_credentials(Member *session); 14 | 15 | void user_edit_balance(Member *session); 16 | 17 | void user_search_product(Member *session); 18 | 19 | void user_product_list(Member *session, ProductList *products); 20 | 21 | void user_lookup_product(Member *session, Product product); 22 | 23 | void user_purchase_product(Member *session, Product product); 24 | 25 | void user_search_orders(Member *session); 26 | 27 | void user_search_parcels(Member *session); 28 | 29 | #endif //CP_PROJECT_USER_PAGE_H 30 | -------------------------------------------------------------------------------- /CSE2035 C Programming/project/project_sources/makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | CFAGS = -W -Wall 3 | TARGET = proj1_20181634 4 | OBJECTS = language.o \ 5 | types/linked_list.o \ 6 | types/product.o \ 7 | types/member.o \ 8 | types/order.o \ 9 | util/util_io.o \ 10 | util/util_string.o \ 11 | util/util_file.o \ 12 | flow/admin/admin_login_page.o \ 13 | flow/admin/admin_page.o \ 14 | flow/user/login_page.o \ 15 | flow/user/register_page.o \ 16 | flow/user/user_page.o \ 17 | flow/search_product_page.o \ 18 | flow/main_menu.o \ 19 | proj1_20181634.o 20 | 21 | all: $(TARGET) 22 | 23 | $(TARGET): $(OBJECTS) 24 | $(CC) $(CFLAGS) -o $@ $^ 25 | 26 | clean : rm *.o cp_project_20181634 -------------------------------------------------------------------------------- /CSE2035 C Programming/project/project_sources/proj1_20181634.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | 3 | #include "util/util_file.h" 4 | #include "flow/main_menu.h" 5 | #include "proj1_20181634.h" 6 | 7 | /// Setup the program. 8 | int main() { 9 | initialize(); 10 | main_menu(); 11 | 12 | return 0; 13 | } 14 | 15 | /// Initializes the database needed to run the program. 16 | void initialize() { 17 | if (!file_exists("customer.csv")) { 18 | file_writeln("customer.csv", "회원번호,아이디,비밀번호,이름,주소,잔액,상태"); 19 | } 20 | if (!file_exists("product.csv")) { 21 | file_writeln("product.csv", "상품번호,상품명,카테고리,가격,상태"); 22 | } 23 | if (!file_exists("history.csv")) { 24 | file_writeln("history.csv", "주문번호,상품번호,회원번호,배송현황"); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /CSE2035 C Programming/project/project_sources/proj1_20181634.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Suhyun Park on 30/11/2018. 3 | // 4 | 5 | #ifndef PROJECT1_MAIN_H 6 | #define PROJECT1_MAIN_H 7 | 8 | #include "types/member.h" 9 | 10 | int main(); 11 | 12 | void initialize(); 13 | 14 | #endif //PROJECT1_MAIN_H 15 | -------------------------------------------------------------------------------- /CSE2035 C Programming/project/project_sources/types/linked_list.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Suhyun Park on 30/11/2018. 3 | // 4 | 5 | #ifndef PROJECT1_LINKED_LIST_H 6 | #define PROJECT1_LINKED_LIST_H 7 | 8 | typedef struct { 9 | void *head; 10 | void *tail; 11 | int size; 12 | } LinkedList; 13 | 14 | enum GenericType { 15 | MemberType, OrderType, ProductType 16 | }; 17 | 18 | LinkedList *list_create(); 19 | 20 | void list_append(LinkedList *list, enum GenericType type, void *node); 21 | 22 | void list_removelast(LinkedList *list, enum GenericType type); 23 | 24 | void *list_get(LinkedList *list, enum GenericType type, int idx); 25 | 26 | void list_set(LinkedList *list, enum GenericType type, void *value, int idx); 27 | 28 | void list_free(LinkedList *list, enum GenericType type); 29 | 30 | #endif //PROJECT1_LINKED_LIST_H 31 | -------------------------------------------------------------------------------- /CSE2035 C Programming/project/project_sources/types/member.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Suhyun Park on 13/11/2018. 3 | // 4 | 5 | #ifndef PROJECT1_MEMBER_H 6 | #define PROJECT1_MEMBER_H 7 | 8 | #include "../language.h" 9 | #include "linked_list.h" 10 | 11 | typedef enum { 12 | Unregistered = -1, Registered = 1 13 | } MemberState; 14 | 15 | typedef struct { 16 | int _idx; 17 | char id[101]; 18 | char password[101]; 19 | char name[101]; 20 | char address[301]; 21 | int balance; 22 | MemberState state; 23 | } Member; 24 | 25 | typedef struct __member_node { 26 | Member member; 27 | struct __member_node *next_ptr; 28 | } MemberNode; 29 | 30 | typedef LinkedList MemberList; 31 | 32 | #define null_member (Member) {-1} 33 | 34 | void member_serialize(char *str, Member member); 35 | 36 | Member parse_member(char *str); 37 | 38 | MemberList *member_list_create(); 39 | 40 | void member_list_append(MemberList *list, Member member); 41 | 42 | void member_list_removelast(MemberList *list); 43 | 44 | Member member_list_get(MemberList *list, int idx); 45 | 46 | void member_list_set(MemberList *list, Member member, int idx); 47 | 48 | int member_id_exists(MemberList *list, char *id); 49 | 50 | Member member_with_id(MemberList *list, char *id); 51 | 52 | MemberList *member_get_all(); 53 | 54 | void member_print_all(MemberList *list, Language lang); 55 | 56 | void member_put_all(MemberList *list); 57 | 58 | #endif //PROJECT1_MEMBER_H 59 | -------------------------------------------------------------------------------- /CSE2035 C Programming/project/project_sources/types/order.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Suhyun Park on 13/11/2018. 3 | // 4 | 5 | #ifndef PROJECT1_ORDER_H 6 | #define PROJECT1_ORDER_H 7 | 8 | #include "linked_list.h" 9 | 10 | typedef enum { 11 | DeliverReady = 0, Delivering = 1, Delivered = 2 12 | } ParcelState; 13 | 14 | typedef struct { 15 | int _idx; 16 | int product_idx; 17 | int member_idx; 18 | ParcelState state; 19 | } OrderHistory; 20 | 21 | typedef struct __order_node { 22 | OrderHistory order; 23 | struct __order_node *next_ptr; 24 | } OrderNode; 25 | 26 | typedef LinkedList OrderList; 27 | 28 | #define null_order (OrderHistory) {-1} 29 | 30 | void order_serialize(char *str, OrderHistory orderHistory); 31 | 32 | OrderHistory parse_order(char *str); 33 | 34 | OrderList *order_list_create(); 35 | 36 | void order_list_append(OrderList *list, OrderHistory order); 37 | 38 | void order_list_removelast(OrderList *list); 39 | 40 | OrderHistory order_list_get(OrderList *list, int idx); 41 | 42 | OrderHistory order_list_get_by_idx(OrderList *list, int idx); 43 | 44 | void order_list_set(OrderList *list, OrderHistory order, int idx); 45 | 46 | OrderList *order_query_by_member_id(OrderList *list, int member_id); 47 | 48 | OrderList *order_query_by_product_id(OrderList *list, int product_id); 49 | 50 | OrderList *order_query_undelivered(OrderList *list); 51 | 52 | OrderList *order_get_all(); 53 | 54 | void order_print_all(OrderList *list, Language lang); 55 | 56 | void order_print(OrderHistory order, Language lang); 57 | 58 | void order_put_all(OrderList *list); 59 | 60 | #endif //PROJECT1_ORDER_H 61 | -------------------------------------------------------------------------------- /CSE2035 C Programming/project/project_sources/types/product.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Suhyun Park on 13/11/2018. 3 | // 4 | 5 | #ifndef PROJECT1_PRODUCT_H 6 | #define PROJECT1_PRODUCT_H 7 | 8 | #include "../language.h" 9 | #include "linked_list.h" 10 | 11 | typedef enum { 12 | Unavailable = -1, Ready, Available 13 | } ProductState; 14 | 15 | typedef struct { 16 | int _idx; 17 | char name[101]; 18 | char category[101]; 19 | int price; 20 | ProductState state; 21 | } Product; 22 | 23 | typedef struct __product_node { 24 | Product product; 25 | struct __product_node *next_ptr; 26 | } ProductNode; 27 | 28 | typedef LinkedList ProductList; 29 | 30 | #define null_product (Product) {-1} 31 | 32 | void product_serialize(char *str, Product product); 33 | 34 | Product parse_product(char *str); 35 | 36 | ProductList *product_list_create(); 37 | 38 | void product_list_append(ProductList *list, Product product); 39 | 40 | void product_list_removelast(ProductList *list); 41 | 42 | Product product_list_get(ProductList *list, int idx); 43 | 44 | void product_list_set(ProductList *list, Product product, int idx); 45 | 46 | Product product_list_get_by_code(ProductList *list, int code); 47 | 48 | ProductList *product_query_by_name(ProductList *list, char *keyword, int query_ready); 49 | 50 | ProductList *product_query_by_category(ProductList *list, char *keyword, int query_ready); 51 | 52 | ProductList *product_query_by_price(ProductList *list, int min, int max, int query_ready); 53 | 54 | ProductList *product_query_available(ProductList *list, int query_ready); 55 | 56 | ProductList *product_get_all(); 57 | 58 | void product_print_all(ProductList *list, Language lang); 59 | 60 | void product_admin_print_all(ProductList *list, Language lang); 61 | 62 | void product_put_all(ProductList *list); 63 | 64 | int product_sales(Product product); 65 | 66 | #endif //PROJECT1_PRODUCT_H 67 | -------------------------------------------------------------------------------- /CSE2035 C Programming/project/project_sources/util/util_file.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | 3 | #include 4 | #include "util_file.h" 5 | 6 | /* 7 | ===== 8 | File - related helper functions 9 | ===== 10 | */ 11 | 12 | /// Check if specific file exists by filename. 13 | /// May behave strange when file permission changed manually; 14 | /// \param filename Name of the file 15 | /// \return 1 if file exists; 0 otherwise 16 | int file_exists(char *filename) { 17 | FILE *fptr = fopen(filename, "r"); 18 | if (fptr == NULL) return 0; 19 | fclose(fptr); 20 | return 1; 21 | } 22 | 23 | /// Initialize a file by writing some content to it. 24 | /// \param filename Name of the file 25 | /// \param contents Contents to write 26 | void file_write(char *filename, char *contents) { 27 | FILE *fptr = fopen(filename, "w"); 28 | fputs(contents, fptr); 29 | fclose(fptr); 30 | } 31 | 32 | /// Initialize a file by writing some content to it, and append newline(\n). 33 | /// \param filename Name of the file 34 | /// \param contents Contents to write 35 | void file_writeln(char *filename, char *contents) { 36 | FILE *fptr = fopen(filename, "w"); 37 | fputs(contents, fptr); 38 | fputs("\n", fptr); 39 | fclose(fptr); 40 | } -------------------------------------------------------------------------------- /CSE2035 C Programming/project/project_sources/util/util_file.h: -------------------------------------------------------------------------------- 1 | #ifndef UTIL_FILE_H 2 | #define UTIL_FILE_H 3 | 4 | int file_exists(char *filename); 5 | 6 | void file_write(char *filename, char *contents); 7 | 8 | void file_writeln(char *filename, char *contents); 9 | 10 | #endif -------------------------------------------------------------------------------- /CSE2035 C Programming/project/project_sources/util/util_io.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Suhyun Park on 2018-12-04. 3 | // 4 | 5 | #ifndef CP_PROJECT_UTIL_IO_H 6 | #define CP_PROJECT_UTIL_IO_H 7 | 8 | void scan_valid_id(char *temp, char *error_message, char *retry_message); 9 | 10 | void scan_valid_string(char *temp, char *error_message, char *retry_message); 11 | 12 | int scan_valid_boolean(char *error_message, char *retry_message); 13 | 14 | int scan_valid_int(int min, int max, char *error_message, char *retry_message); 15 | 16 | #endif //CP_PROJECT_UTIL_IO_H 17 | -------------------------------------------------------------------------------- /CSE2035 C Programming/project/project_sources/util/util_string.h: -------------------------------------------------------------------------------- 1 | #ifndef UTIL_STRING_H 2 | #define UTIL_STRING_H 3 | 4 | /// Macro to fix fgets getting newline character (\n) after using scanf 5 | #define CLEAR_INPUT_BUFFER int __x; \ 6 | while((__x = getchar()) != '\n' && __x != EOF); 7 | 8 | void string_itoa(char *str, int value); 9 | 10 | void string_to_lowercase(char *str); 11 | 12 | void string_to_uppercase(char *str); 13 | 14 | int string_invalid(char *str); 15 | 16 | int string_id_invalid(char *str); 17 | 18 | char *string_trim(char *str); 19 | 20 | void string_mask(char *str, char c); 21 | 22 | #endif -------------------------------------------------------------------------------- /CSE3080 Data Structures/README.md: -------------------------------------------------------------------------------- 1 | # 자료구조 2 | 3 | CSE3080 - Data Structures 4 | 5 | 2019년 1학기 / 정성원 교수님 6 | 7 | 컴퓨터의 1차원 메모리에 여러 유형의 데이터를 어떻게 저장하는지 배웁니다. 8 | 9 | - [오픈랩 코드](openlab) 10 | 11 | ## Open Lab 12 | 13 | - 매 주마다 수업에서 배운 자료구조를 구현하는 시간을 가집니다. -------------------------------------------------------------------------------- /CSE3080 Data Structures/openlab/README.md: -------------------------------------------------------------------------------- 1 | # Open Lab List 2 | 3 | ## [Week 1](week1/README.md) 4 | - Insertion Sort 구현 5 | - 주어진 자료를 Insertion Sort 6 | - 배열 크기에 따른 최악의 경우 데이터를 정렬하는 데 얼마만큼의 시간이 걸리는지 계산 7 | 8 | ## [Week 2](week2/README.md) 9 | - ADT Polynomial을 동적 할당된 array 혹은 linked list로 구현 10 | - Polynomial이 저장된 파일 세 개를 읽어서 세 polynomial을 더한 결과를 출력하는 프로그램 작성 11 | 12 | ## [Week 3](week3/README.md) 13 | - ADT Sparse Matrix 구현 14 | - Sparse Matrix를 transpose하는 알고리즘 구현 15 | 16 | ## [Week 4](week4/README.md) 17 | - KMP 알고리즘 구현 18 | - 부분 문자열을 naïve하게 찾는 알고리즘과 KMP 알고리즘에서 각각의 iteration 수 비교 19 | 20 | ## [Week 5](week5/README.md) 21 | - linked stack과 linked queue를 각각 구현 22 | 23 | ## [Week 6](week6/README.md) 24 | - 원형 링크드 리스트를 구현 25 | - 두 리스트를 합치고 두 리스트를 빼는 연산을 구현 26 | 27 | ## [Week 7](week7/README.md) 28 | - Threaded Binary Tree를 구현 29 | 30 | ## [Week 8](week8/README.md) 31 | - Binary Search Tree를 구현 32 | - inorder와 preorder로 탐색한 결과를 출력 -------------------------------------------------------------------------------- /CSE3080 Data Structures/openlab/week1/README.md: -------------------------------------------------------------------------------- 1 | # Week 1 2 | 3 | - 입력: [lab1.data](lab1.data) 4 | - [정답 코드](main.c) 5 | 6 | ## 문제 7 | 삽입정렬을 구현하고 입력 데이터의 개수에 따른 실행시간을 측정한 후 정렬 결과와 실행시간을 출력하는 프로그램을 작성하시오. 8 | 9 | - 동적 메모리 할당에 의한 배열을 사용한다. 10 | - 선택정렬과 유사하게 알고리즘 수행 시 정렬된 부분(S)과 안 된 부분(U)으로 나눈다. 11 | - 삽입정렬은 U의 첫 element를 반복적으로 S의 적합한 위치에 삽입한다. 12 | 13 | ### 예제 입력 14 | ``` 15 | 5 16 | 3 17 | 1 18 | 8 19 | 7 20 | 3 21 | 6 22 | 9 23 | 2 24 | 4 25 | ``` 26 | 27 | ### 예제 출력 28 | 29 | 1. 입력데이터와 정렬 결과를 출력 30 | 2. 프로그램 실행시간을 출력 최악의 경우(`lab1.data`는 최악의 경우가 아니다, 별도의 데이터 입력이 필요하다)에 대한 실행 시간을 출력한다. 이때 정확한 시간 측정을 위하여 입력의 크기에 반비례하여 실행 횟수를 결정할 것. 31 | 조건 - 입력의 최대 크기 = 100, 출력 간격 = 10, 반복 회수는 입력횟수에 반비례하면 된다. 반드시 아래의 횟수를 지켜야 하는 것은 아니다. 32 | 33 | ``` 34 | Input: 5 3 1 8 7 3 6 9 2 4 35 | Sorted: 1 2 3 3 4 5 6 7 8 9 36 | 37 | n repetitions time 38 | 0 109 0.000001 39 | 10 74 0.000001 40 | 20 40 0.000003 41 | 30 23 0.000005 42 | 40 15 0.000008 43 | 50 8 0.000014 44 | 60 7 0.000015 45 | 70 4 0.000025 46 | 80 3 0.000035 47 | 90 4 0.000032 48 | 100 2 0.000062 49 | ``` -------------------------------------------------------------------------------- /CSE3080 Data Structures/openlab/week1/lab1.data: -------------------------------------------------------------------------------- 1 | 5 2 | 3 3 | 1 4 | 8 5 | 7 6 | 3 7 | 6 8 | 9 9 | 2 10 | 4 11 | -------------------------------------------------------------------------------- /CSE3080 Data Structures/openlab/week1/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void insertion_sort(int *arr, int n) { 6 | for (int pivot = 1; pivot < n; pivot++) { 7 | int temp = arr[pivot]; 8 | int idx = pivot - 1; 9 | 10 | while ((idx >= 0) && (arr[idx] > temp)) { 11 | arr[idx + 1] = arr[idx]; 12 | idx--; 13 | } 14 | 15 | arr[idx + 1] = temp; 16 | } 17 | } 18 | 19 | void lab1() { 20 | char filename[20] = "lab1.data"; 21 | FILE *fp = fopen(filename, "r"); 22 | int *arr = (int *) malloc(sizeof(int) * 0); 23 | int x, n = 0; 24 | 25 | while (fscanf(fp, "%d", &x) == 1) { 26 | n++; 27 | realloc(arr, sizeof(int) * n); 28 | arr[n - 1] = x; 29 | } 30 | 31 | printf("Input: "); 32 | for (int i = 0; i < n; i++) { 33 | printf("%d ", arr[i]); 34 | } 35 | printf("\n"); 36 | 37 | insertion_sort(arr, n); 38 | 39 | printf("Output: "); 40 | for (int i = 0; i < n; i++) { 41 | printf("%d ", arr[i]); 42 | } 43 | printf("\n"); 44 | 45 | fclose(fp); 46 | free(arr); 47 | } 48 | 49 | void sort_test(int n) { 50 | int *arr = malloc(sizeof(int) * n); 51 | int rep = 100000 / (n + 1); 52 | double elapsed = 0; 53 | 54 | for (int t = 0; t < rep; t++) { 55 | for (int i = 0; i < n; i++) { 56 | arr[i] = (n - i); 57 | } 58 | 59 | clock_t start = clock(); 60 | insertion_sort(arr, n); 61 | elapsed += (double) (clock() - start); 62 | } 63 | 64 | free(arr); 65 | elapsed /= CLOCKS_PER_SEC; 66 | printf("%4d %12d %12.6lf\n", n, rep, elapsed); 67 | } 68 | 69 | void lab2() { 70 | printf(" n repetitions time\n"); 71 | 72 | for (int i = 0; i <= 100; i += 10) { 73 | sort_test(i); 74 | } 75 | } 76 | 77 | int main() { 78 | lab1(); 79 | lab2(); 80 | 81 | return 0; 82 | } -------------------------------------------------------------------------------- /CSE3080 Data Structures/openlab/week2/A.txt: -------------------------------------------------------------------------------- 1 | 2 1000 2 | 3 500 3 | 7 200 4 | 8 120 5 | 9 50 6 | 2 40 7 | 10 10 8 | 20 5 9 | -------------------------------------------------------------------------------- /CSE3080 Data Structures/openlab/week2/B.txt: -------------------------------------------------------------------------------- 1 | 7 25 2 | 8 23 3 | 10 20 4 | 2 18 5 | 6 14 6 | -2 10 7 | 1 9 8 | 7 6 9 | 8 5 10 | 2 3 11 | 9 0 12 | -------------------------------------------------------------------------------- /CSE3080 Data Structures/openlab/week2/C.txt: -------------------------------------------------------------------------------- 1 | 13 1000 2 | 15 500 3 | 4 200 4 | 12 120 5 | -4 50 6 | 1 40 7 | 3 10 8 | 3 5 -------------------------------------------------------------------------------- /CSE3080 Data Structures/openlab/week2/README.md: -------------------------------------------------------------------------------- 1 | # Week 2 2 | 3 | - 입력: [A.txt](A.txt), [B.txt](B.txt), [C.txt](C.txt) 4 | - [정답 코드](main.c) 5 | 6 | ## 문제 7 | 100*x*1000+1 과 같은 다항식을 담으려면 1000개짜리 배열이 필요하지만, 998개 칸의 메모리가 낭비된다. 이를 해결하기 위해 다항식을 저장하기 위한 ADT를 정의한다. 8 | 정의한 ADT를 사용해 세 개의 다항식을 입력받고, 세 다항식의 합을 출력하는 프로그램을 작성하시오. 다음을 구현하고, 사용하여 완성한다. (교과서 2.4절 참고) 9 | 10 | - ADT Poly을 정의한다. 11 | - `padd` 함수는 다항식 덧셈을 하는 함수이다. (Program 2.6) 12 | - `readPoly` 함수는 파일로부터 다항식을 읽어온다. (연습문제 2) 13 | - `printPoly` 함수는 다항식 덧셈 결과를 출력한다. (연습문제 2) 14 | 15 | ### 예제 입력 16 | 파일에 저장된 세 개의 다항식 (파일 `A.txt`, `B.txt`, `C.txt`에 계수와 지수 저장) 17 | - A(*x*) = 2*x*1000 + 3*x*500 + 7*x*200 + 8*x*120 + 9*x*50 + 2*x*40 + 10*x*10 + 20*x*5 18 | - B(*x*) = 7*x*25 + 8*x*23 + 10*x*20 + 2*x*18 + 6*x*14 - 2*x*10 + *x*9 + 7*x*6 + 8*x*5 + 2*x*3 + 9 19 | - C(*x*) = 13*x*1000 + 15*x*500 + 4*x*200 + 12*x*120 - 4*x*50 + *x*40 + 3*x*10 + 3*x*5 20 | 21 | ``` 22 | 2 1000 23 | 3 500 24 | 7 200 25 | 8 120 26 | 9 50 27 | 2 40 28 | 10 10 29 | 20 5 30 | ``` 31 | 32 | ``` 33 | 7 25 34 | 8 23 35 | 10 20 36 | 2 18 37 | 6 14 38 | -2 10 39 | 1 9 40 | 7 6 41 | 8 5 42 | 2 3 43 | 9 0 44 | ``` 45 | 46 | ``` 47 | 13 1000 48 | 15 500 49 | 4 200 50 | 12 120 51 | -4 50 52 | 1 40 53 | 3 10 54 | 3 5 55 | ``` 56 | 57 | ### 예제 출력 58 | 세 다항식 A, B, C의 합을 다음과 같이 (계수, 지수) 형태로 출력한다. 59 | 60 | ``` 61 | (15,1000), (18,500), (11,200), (20,120), (5,50), (3,40), (7,25), (8,23), (10,20), (2,18), (6,14), (11,10), (1,9), (7,6), (31,5), (2,3), (9,0) 62 | ``` -------------------------------------------------------------------------------- /CSE3080 Data Structures/openlab/week2/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define MAX_TERMS 100 6 | 7 | typedef struct __term__ { 8 | float coeff; 9 | int degree; 10 | } term; 11 | 12 | typedef struct __poly__ { 13 | term *terms; 14 | int term_count; 15 | } polynomial; 16 | 17 | void initPoly(polynomial *poly) { 18 | poly->terms = (term *) malloc(0); 19 | poly->term_count = 0; 20 | } 21 | 22 | void freePoly(polynomial *poly) { 23 | free(poly->terms); 24 | } 25 | 26 | void addTerm(polynomial *poly, float coeff, int degree) { 27 | poly->terms = (term *) realloc(poly->terms, sizeof(term) * ++(poly->term_count)); 28 | poly->terms[poly->term_count - 1].coeff = coeff; 29 | poly->terms[poly->term_count - 1].degree = degree; 30 | } 31 | 32 | void readPoly(polynomial *poly, char *filename) { 33 | FILE *fp = fopen(filename, "r"); 34 | 35 | float coeff; 36 | int degree; 37 | while (fscanf(fp, "%f%d", &coeff, °ree) != EOF) { 38 | addTerm(poly, coeff, degree); 39 | } 40 | 41 | fclose(fp); 42 | } 43 | 44 | void printPoly(polynomial poly) { 45 | for (int i = 0; i < poly.term_count; i++) { 46 | printf("(%.1f, %d)", poly.terms[i].coeff, poly.terms[i].degree); 47 | if (i != poly.term_count - 1) { 48 | printf(", "); 49 | } 50 | } 51 | } 52 | 53 | void padd(polynomial A, polynomial B, polynomial *result) { 54 | initPoly(result); 55 | 56 | int a = 0, b = 0; 57 | 58 | while (a < A.term_count && b < B.term_count) { 59 | if (A.terms[a].degree > B.terms[b].degree) { 60 | addTerm(result, A.terms[a].coeff, A.terms[a].degree); 61 | a++; 62 | } else if (A.terms[a].degree < B.terms[b].degree) { 63 | addTerm(result, B.terms[b].coeff, B.terms[b].degree); 64 | b++; 65 | } else { 66 | addTerm(result, A.terms[a].coeff + B.terms[b].coeff, A.terms[a].degree); 67 | a++, b++; 68 | } 69 | } 70 | 71 | while (a < A.term_count) { 72 | addTerm(result, A.terms[a].coeff, A.terms[a].degree); 73 | a++; 74 | } 75 | 76 | while (b < B.term_count) { 77 | addTerm(result, B.terms[b].coeff, B.terms[b].degree); 78 | b++; 79 | } 80 | } 81 | 82 | int main() { 83 | polynomial A, B, C, ApB, ApBpC; 84 | initPoly(&A), initPoly(&B), initPoly(&C); 85 | readPoly(&A, "A.txt"), readPoly(&B, "B.txt"), readPoly(&C, "C.txt"); 86 | 87 | padd(A, B, &ApB); 88 | padd(ApB, C, &ApBpC); 89 | 90 | printPoly(ApBpC); 91 | 92 | freePoly(&A), freePoly(&B), freePoly(&C), freePoly(&ApB), freePoly(&ApBpC); 93 | 94 | return 0; 95 | } -------------------------------------------------------------------------------- /CSE3080 Data Structures/openlab/week3/README.md: -------------------------------------------------------------------------------- 1 | # Week 3 2 | 3 | - 입력: [A.txt](a[3].txt) 4 | - [정답 코드](main.c) 5 | 6 | ## 문제 7 | `fastTranspose`는 수업 시간에 언급했듯이 `startingPos` 배열을 사용하지 않고 `rowTerms`만 사용해서 구현할 수 있다. `startingPos` 배열 없이 `fastTranspose`를 구현하시오. 8 | 참고 : 책 한글판 p89 2번 문제(= 책 영문판 p84 2번 문제) 9 | 10 | ### 예제 입력 11 | `A.txt`에 행렬에 대한 정보가 주어진다. 첫째 줄에 정수 *n*, *m*이 공백으로 구분되어 12 | 주어지고, 둘째 줄부터 *n* + 1번째 줄에 *m*개의 원소가 공백으로 구분되어 주어진다. 13 | 14 | ``` 15 | 7 8 16 | -10 78 37 0 0 11 48 0 17 | 0 0 -5 52 0 0 0 59 18 | 61 -45 0 0 28 0 0 74 19 | 0 0 0 14 8 0 0 44 20 | 0 1 0 9 9 7 0 1 21 | 4 9 6 0 0 0 0 99 22 | 0 0 0 0 12 13 14 15 23 | ``` 24 | 25 | ### 예제 출력 26 | 전치 행렬을 희소 행렬의 형태로 출력한다. 27 | 28 | ``` 29 | 8 7 28 30 | 0 0 -10 31 | 0 2 61 32 | 0 5 4 33 | 1 0 78 34 | 1 2 -45 35 | 1 4 1 36 | 1 5 9 37 | 2 0 37 38 | 2 1 -5 39 | 2 5 6 40 | 3 1 52 41 | 3 3 14 42 | 3 4 9 43 | 4 2 28 44 | 4 3 8 45 | 4 4 9 46 | 4 6 12 47 | 5 0 11 48 | 5 4 7 49 | 5 6 13 50 | 6 0 48 51 | 6 6 14 52 | 7 1 59 53 | 7 2 74 54 | 7 3 44 55 | 7 4 1 56 | 7 5 99 57 | 7 6 15 58 | ``` -------------------------------------------------------------------------------- /CSE3080 Data Structures/openlab/week3/a[3].txt: -------------------------------------------------------------------------------- 1 | 2 | 7 8 3 | -10 78 37 0 0 11 48 0 4 | 0 0 -5 52 0 0 0 59 5 | 61 -45 0 0 28 0 0 74 6 | 0 0 0 14 8 0 0 44 7 | 0 1 0 9 9 7 0 1 8 | 4 9 6 0 0 0 0 99 9 | 0 0 0 0 12 13 14 15 -------------------------------------------------------------------------------- /CSE3080 Data Structures/openlab/week3/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef struct __triple__ { 5 | int x, y, val; 6 | } triple; 7 | 8 | typedef struct __sparse_matrix__ { 9 | int rows, columns, entry_count; 10 | triple *values; 11 | } sparse_matrix; 12 | 13 | void appendEntry(sparse_matrix *matrix, int x, int y, int value) { 14 | int i = matrix->entry_count; 15 | matrix->values = (triple*) realloc(matrix->values, (i + 1) * sizeof(triple)); 16 | matrix->entry_count = i + 1; 17 | 18 | (matrix->values[i]).x = x; 19 | (matrix->values[i]).y = y; 20 | (matrix->values[i]).val = value; 21 | } 22 | 23 | void fastTranspose(sparse_matrix *matrix, sparse_matrix *result) { 24 | result->rows = matrix->columns; 25 | result->columns = matrix->rows; 26 | result->entry_count = 0; 27 | 28 | for (int y = 0; y < result->rows; y++) { 29 | for (int i = 0; i < matrix->entry_count; i++) { 30 | if (matrix->values[i].y == y) { 31 | appendEntry(result, 32 | matrix->values[i].y, 33 | matrix->values[i].x, 34 | matrix->values[i].val); 35 | } 36 | } 37 | } 38 | } 39 | 40 | void freeSparseMatrix(sparse_matrix* matrix) { 41 | free(matrix->values); 42 | } 43 | 44 | void initSparseMatrix(sparse_matrix *matrix) { 45 | matrix->entry_count = 0; 46 | matrix->columns = 0; 47 | matrix->rows = 0; 48 | matrix->values = (triple*) malloc(0); 49 | } 50 | 51 | void readSparseMatrix(sparse_matrix *matrix, char *filename) { 52 | FILE* fp = fopen(filename, "r"); 53 | 54 | int r, c, val; 55 | fscanf(fp, "%d%d", &r, &c); 56 | 57 | matrix->rows = r; 58 | matrix->columns = c; 59 | 60 | for (int x = 0; x < r; x++) { 61 | for (int y = 0; y < c; y++) { 62 | fscanf(fp, "%d", &val); 63 | if (val != 0) { 64 | appendEntry(matrix, x, y, val); 65 | } 66 | } 67 | } 68 | 69 | fclose(fp); 70 | } 71 | 72 | void printTriple(triple tri) { 73 | printf("%d %d %d\n", tri.x, tri.y, tri.val); 74 | } 75 | 76 | void printSparseMatrix(sparse_matrix matrix) { 77 | printf("%d %d %d\n", matrix.rows, matrix.columns, matrix.entry_count); 78 | for (int i = 0; i < matrix.entry_count; i++) { 79 | printTriple(matrix.values[i]); 80 | } 81 | } 82 | 83 | int main() { 84 | sparse_matrix matrix, transposed; 85 | 86 | initSparseMatrix(&matrix); 87 | initSparseMatrix(&transposed); 88 | 89 | readSparseMatrix(&matrix, "a[3].txt"); 90 | fastTranspose(&matrix, &transposed); 91 | 92 | printSparseMatrix(transposed); 93 | 94 | freeSparseMatrix(&matrix); 95 | freeSparseMatrix(&transposed); 96 | 97 | return 0; 98 | } -------------------------------------------------------------------------------- /CSE3080 Data Structures/openlab/week4/README.md: -------------------------------------------------------------------------------- 1 | # Week 4 2 | 3 | - 입력: [stdin](input[25].txt) 4 | - [정답 코드](main.c) 5 | 6 | ## 문제 7 | 문자열과 패턴을 입력받아서 문자열에 패턴이 존재하는지 판단하는 `nfind` 탐색 함수와 KMP 알고리즘을 이용한 함수를 작성하여 탐색 결과와 반복문(for, while) 횟수를 출력하는 프로그램을 작성한다. 8 | KMP 알고리즘의 실패함수를 구할 때 반복하는 횟수도 반복횟수에 포함하여 출력한다. 9 | 10 | ### 예제 입력 1 11 | 입력은 표준 입력(scanf)으로 주어진다. 첫째줄에 알파벳 소문자로 이루어진 문자열이 주어지고, 둘째 줄에 알파벳 소문자로 이루어진 문자열이 주어진다. (문자열과 패턴의 최대 길이는 100을 넘지 않는다.) 12 | 13 | ``` 14 | sogangdataengineeringlabfighting 15 | gineeringlabf 16 | ``` 17 | 18 | ### 예제 출력 1 19 | 출력은 표준 출력으로 출력한다. 첫째 줄에 `nfind` 탐색 함수의 탐색 결과와 반복문 횟수를 출력하고, 둘째 줄에 KMP 알고리즘을 이용한 함수의 탐색 결과와 반복문 횟수를 출력한다. 20 | 문자열에 패턴이 존재한다면 `YES`, 존재하지 않는다면 `NO`를 출력한다. 반복문 횟수는 코드에 따라 약간의 차이가 있을 수 있다. 21 | 22 | ``` 23 | nfind: YES 25 24 | kmp: YES 40 25 | ``` 26 | 27 | ### 예제 입력 2 28 | ``` 29 | aaaaaaaaaaaabaaaaaabaaaaaabaaaaaaaa 30 | aaabaaa 31 | ``` 32 | 33 | ### 예제 출력 2 34 | ``` 35 | nfind: YES 40 36 | kmp: YES 33 37 | ``` -------------------------------------------------------------------------------- /CSE3080 Data Structures/openlab/week4/input[15].txt: -------------------------------------------------------------------------------- 1 | sogangdataengineeringlabfighting 2 | gineeringlabf 3 | aaaaaaaaaaaabaaaaaabaaaaaabaaaaaaaa 4 | aaabaaa -------------------------------------------------------------------------------- /CSE3080 Data Structures/openlab/week4/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define MAX_LENGTH 101 6 | 7 | int pi[MAX_LENGTH]; 8 | 9 | int construct_pi(char *pat) { 10 | int j = 0; 11 | int counter = 0; 12 | int pat_length = strlen(pat); 13 | 14 | for (int i = 1; i < pat_length; i++) { 15 | counter++; 16 | while (j > 0 && pat[i] != pat[j]) { 17 | j = pi[j - 1]; 18 | counter++; 19 | } 20 | if (pat[i] == pat[j]) { 21 | pi[i] = ++j; 22 | } 23 | } 24 | 25 | return counter; 26 | } 27 | 28 | int kmp(char *str, char *pat) { 29 | int counter = construct_pi(pat); 30 | int str_length = strlen(str); 31 | int pat_length = strlen(pat); 32 | 33 | int j = 0; 34 | 35 | for (int i = 0; i < str_length; i++) { 36 | counter++; 37 | while (j > 0 && str[i] != pat[j]) { 38 | counter++; 39 | j = pi[j - 1]; 40 | } 41 | if (str[i] == pat[j]) { 42 | if (j == pat_length - 1) { 43 | return counter; 44 | } else { 45 | j++; 46 | } 47 | } 48 | } 49 | 50 | return -counter; 51 | } 52 | 53 | int nfind(char *str, char *pat) { 54 | int counter = 0; 55 | int str_length = strlen(str); 56 | int pat_length = strlen(pat); 57 | 58 | int i, j, start = 0; 59 | 60 | int endmatch = pat_length - 1; 61 | 62 | for (i = 0; endmatch < str_length; endmatch++, start++) { 63 | counter++; 64 | if (str[endmatch] == pat[pat_length - 1]) { 65 | j = 0; 66 | i = start; 67 | while (j < pat_length - 1 && str[i] == pat[j]) { 68 | counter++; 69 | i++; 70 | j++; 71 | } 72 | } 73 | if (j == pat_length - 1) { 74 | return counter; 75 | } 76 | } 77 | 78 | return -counter; 79 | } 80 | 81 | int main() { 82 | char str[MAX_LENGTH], pat[MAX_LENGTH]; 83 | 84 | fgets(str, MAX_LENGTH, stdin); 85 | str[strlen(str) - 1] = '\0'; 86 | 87 | fgets(pat, MAX_LENGTH, stdin); 88 | pat[strlen(pat) - 1] = '\0'; 89 | 90 | int nfind_result = nfind(str, pat); 91 | int kmp_result = kmp(str, pat); 92 | 93 | if (nfind_result > 0) { 94 | printf("nfind: YES %5d\n", nfind_result); 95 | } else { 96 | printf("nfind: NO %5d\n", -nfind_result); 97 | } 98 | 99 | if (kmp_result > 0) { 100 | printf("kmp: YES %5d\n", kmp_result); 101 | } else { 102 | printf("kmp: NO %5d\n", -kmp_result); 103 | } 104 | 105 | return 0; 106 | } -------------------------------------------------------------------------------- /CSE3080 Data Structures/openlab/week5/README.md: -------------------------------------------------------------------------------- 1 | # Week 5 2 | 3 | - 입력: [input.txt](input.txt) 4 | - [정답 코드](main.c) 5 | 6 | ## 문제 7 | linked stack과 linked queue를 각각 구현하고 테스트하는 프로그램을 작성하시오. 8 | 9 | ### 예제 입력 10 | 각 행에는 하나 혹은 두 개의 정수로 이루어져 있다. 첫번째 숫자가 0 일 때에는 두번째 숫자를 삽입하고 첫번째 숫자가 1 일 때에는 삭제하는 연산을 의미한다. 11 | 최종 출력은 스택 혹은 큐에서 차례대로 pop 한 결과이다. 12 | 13 | 예를 들어 `input.txt`의 경우에는 `push(10)`, `push(40)`, `pop()`, `push(80)`, `push(70)`, `push(60)`, `pop()`, `push(50)`을 나타내고, 14 | 최종 출력은 스택의 경우 `50 70 80 10`, 큐의 경우 `80 70 60 50` 을 출력한다. 15 | 16 | ``` 17 | 0 10 18 | 0 40 19 | 1 20 | 0 80 21 | 0 70 22 | 0 60 23 | 1 24 | 0 50 25 | ``` 26 | 27 | ### 예제 출력 28 | 출력은 표준 출력으로 출력한다. 첫째줄에 스택의 pop 결과를 출력하고 둘째줄에 큐의 pop 결과를 출력한다. 29 | 30 | ``` 31 | 50 70 80 10 32 | 80 70 60 50 33 | ``` 34 | 35 | ### 템플릿 36 | ```c 37 | #include 38 | #include 39 | typedef struct { 40 | int key; 41 | } element; 42 | typedef struct stack* stackPointer; 43 | typedef struct stack { 44 | element data; 45 | stackPointer link; 46 | } stack; 47 | typedef struct queue* queuePointer; 48 | typedef struct queue { 49 | element data; 50 | queuePointer link; 51 | } queue; 52 | stackPointer top; 53 | queuePointer front, rear; 54 | ``` -------------------------------------------------------------------------------- /CSE3080 Data Structures/openlab/week5/input.txt: -------------------------------------------------------------------------------- 1 | 0 10 2 | 0 40 3 | 1 4 | 0 80 5 | 0 70 6 | 0 60 7 | 1 8 | 0 50 9 | -------------------------------------------------------------------------------- /CSE3080 Data Structures/openlab/week5/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef struct { 5 | int key; 6 | } element; 7 | 8 | typedef struct stack *stackPointer; 9 | 10 | typedef struct stack { 11 | element data; 12 | stackPointer link; 13 | } stack; 14 | 15 | typedef struct queue *queuePointer; 16 | 17 | typedef struct queue { 18 | element data; 19 | queuePointer link; 20 | } queue; 21 | 22 | stackPointer top; 23 | 24 | queuePointer front, rear; 25 | 26 | void stack_push(int x) { 27 | stackPointer temp = top; 28 | stackPointer new = (stackPointer) malloc(sizeof(stackPointer)); 29 | new->data.key = x; 30 | new->link = temp; 31 | 32 | top = new; 33 | } 34 | 35 | int stack_pop(int *x) { 36 | if (top == NULL) return 0; 37 | *x = top->data.key; 38 | stackPointer temp = top; 39 | top = top->link; 40 | free(temp); 41 | return 1; 42 | } 43 | 44 | void queue_push(int x) { 45 | queuePointer new = (queuePointer) malloc(sizeof(queuePointer)); 46 | new->link = NULL; 47 | new->data.key = x; 48 | 49 | if (front == NULL) { 50 | front = new; 51 | rear = new; 52 | } else { 53 | rear->link = new; 54 | rear = new; 55 | } 56 | } 57 | 58 | int queue_pop(int *x) { 59 | if (front == NULL) return 0; 60 | *x = front->data.key; 61 | queuePointer temp = front; 62 | front = front->link; 63 | free(temp); 64 | return 1; 65 | } 66 | 67 | int main() { 68 | FILE* fp = fopen("input.txt", "r"); 69 | 70 | int op, x; 71 | while (~fscanf(fp, "%d", &op)) { 72 | if (op == 0) { 73 | fscanf(fp, "%d", &x); 74 | stack_push(x); 75 | queue_push(x); 76 | } else { 77 | stack_pop(&x); 78 | queue_pop(&x); 79 | } 80 | } 81 | 82 | while (stack_pop(&x)) { 83 | printf("%d ", x); 84 | } 85 | printf("\n"); 86 | while (queue_pop(&x)) { 87 | printf("%d ", x); 88 | } 89 | 90 | return 0; 91 | } -------------------------------------------------------------------------------- /CSE3080 Data Structures/openlab/week6/A.txt: -------------------------------------------------------------------------------- 1 | 1 2 | 3 3 | 5 4 | 7 5 | 9 6 | 11 7 | 13 8 | 15 9 | 17 10 | 19 11 | -------------------------------------------------------------------------------- /CSE3080 Data Structures/openlab/week6/B.txt: -------------------------------------------------------------------------------- 1 | 2 2 | 4 3 | 4 4 | 4 5 | 4 6 | 4 7 | 6 8 | 8 9 | 10 10 | 12 11 | 14 12 | 16 13 | 18 14 | 20 15 | 22 16 | 24 17 | 26 18 | -------------------------------------------------------------------------------- /CSE3080 Data Structures/openlab/week6/C.txt: -------------------------------------------------------------------------------- 1 | 26 2 | 19 3 | 1 4 | 2 5 | 123 6 | 456 7 | 789 8 | 3 9 | 4 10 | 5 11 | 17 12 | -------------------------------------------------------------------------------- /CSE3080 Data Structures/openlab/week6/README.md: -------------------------------------------------------------------------------- 1 | # Week 6 2 | 3 | - 입력: [A.txt](A.txt), [B.txt](B.txt), [C.txt](C.txt) 4 | - [정답 코드](main.c) 5 | 6 | ## 문제 7 | `A.txt`와 `B.txt`에서 정수를 입력받아서 각각의 원형 링크드 리스트를 구성하고, 두 원형 링크드 리스트를 합쳐서 하나의 원형 링크드 리스트로 만든다. 8 | 합쳐진 원형 링크드 리스트에서 `C.txt`에 저장된 정수를 가진 노드를 삭제하는 프로그램을 작성하시오. 9 | 10 | ### 예제 입력 11 | 입력은 파일(`A.txt`, `B.txt`, `C.txt`) 입력으로 주어진다. 첫째줄부터 EOF 까지 각 줄에 하나의 정수로 이루어져 있다. 12 | 13 | ``` 14 | 1 15 | 3 16 | 5 17 | 7 18 | 9 19 | 11 20 | 13 21 | 15 22 | 17 23 | 19 24 | ``` 25 | 26 | ``` 27 | 2 28 | 4 29 | 4 30 | 4 31 | 4 32 | 4 33 | 6 34 | 8 35 | 10 36 | 12 37 | 14 38 | 16 39 | 18 40 | 20 41 | 22 42 | 24 43 | 26 44 | ``` 45 | 46 | ``` 47 | 26 48 | 19 49 | 1 50 | 2 51 | 123 52 | 456 53 | 789 54 | 3 55 | 4 56 | 5 57 | 17 58 | ``` 59 | 60 | ### 예제 출력 61 | 출력은 표준 출력으로 출력한다. 첫째 줄에 합쳐진 원형 리스트를 출력하고, 둘째 줄에 삭제된 후의 원형 리스트를 출력한다. 62 | 63 | ``` 64 | 1 3 5 7 9 11 13 15 17 19 2 4 4 4 4 4 6 8 10 12 14 16 18 20 22 24 26 65 | 7 9 11 13 15 6 8 10 12 14 16 18 20 22 24 66 | ``` -------------------------------------------------------------------------------- /CSE3080 Data Structures/openlab/week7/README.md: -------------------------------------------------------------------------------- 1 | # Week 7 2 | 3 | - 입력: 없음 4 | - [정답 코드](main.c) 5 | 6 | ## 문제 7 | 책 [그림 5.23]에 있는 Threaded Binary Tree를 구성하고 inorder의 결과를 출력한다. 8 | `main` 함수와 함수의 원형, 자료구조는 아래와 같고 수정할 수 없다. 9 | 노드를 왼쪽에 삽입하는 함수인 `insertLeft` 함수와 빈 노드를 반환하는 함수인 `getNewNode` 함수는 직접 구현해야 한다. 10 | 11 | ### 예제 입력 12 | 입력은 없다. 13 | 14 | ### 예제 출력 15 | 출력은 표준 출력으로 출력한다. 첫째줄에 inorder로 탐색한 결과를 출력한다. 16 | 17 | ``` 18 | H D I B E A F C G 19 | ``` 20 | 21 | ### 템플릿 22 | ```c 23 | typedef struct threadedTree *threadedPointer; 24 | typedef struct threadedTree { 25 | short int leftThread; 26 | threadedPointer leftChild; 27 | char data; 28 | threadedPointer rightChild; 29 | short int rightThread; 30 | } threadedTree; 31 | threadedPointer insucc(threadedPointer tree); 32 | void tinorder(threadedPointer tree); 33 | void insertRight(threadedPointer s, threadedPointer r); 34 | void insertLeft(threadedPointer s, threadedPointer l); 35 | threadedPointer getNewNode(char data) ; 36 | int main() { 37 | /* image 5.23 */ 38 | threadedPointer root = getNewNode('-'); 39 | insertLeft(root, getNewNode('A')); 40 | insertLeft(root->leftChild, getNewNode('B')); 41 | insertLeft(root->leftChild->leftChild, getNewNode('D')); 42 | insertLeft(root->leftChild->leftChild->leftChild, getNewNode('H')); 43 | insertRight(root->leftChild->leftChild->leftChild, getNewNode('I')); 44 | insertRight(root->leftChild->leftChild, getNewNode('E')); 45 | insertRight(root->leftChild, getNewNode('C')); 46 | insertLeft(root->leftChild->rightChild, getNewNode('F')); 47 | insertRight(root->leftChild->rightChild, getNewNode('G')); 48 | tinorder(root); 49 | return 0; 50 | } 51 | ``` -------------------------------------------------------------------------------- /CSE3080 Data Structures/openlab/week8/README.md: -------------------------------------------------------------------------------- 1 | # Week 8 2 | 3 | - 입력: [input.txt](input.txt) 4 | - [정답 코드](main.c) 5 | 6 | ## 문제 7 | 입력파일로 주어진 `n`개의 양의 정수들을 읽어 binary search tree를 구성하라. 8 | 9 | ### 예제 입력 10 | 입력 파일의 첫째 줄에는 읽어야 할 정수들의 개수 `n`이 주어지고 그 다음 `n`줄에 양수인 정수가 한 줄에 하나씩 주어진다. 11 | 12 | ``` 13 | 10 14 | 3 15 | 9 16 | 8 17 | 2 18 | 5 19 | 10 20 | 7 21 | 1 22 | 4 23 | 6 24 | ``` 25 | 26 | ### 템플릿 27 | ```c 28 | #define MAX_TERMS 100 29 | typedef struct treeNode* treePtr; 30 | typedef struct treeNode { 31 | treePtr lchild; 32 | int data; 33 | treePtr rchild; 34 | }; 35 | void inorder (treePtr); 36 | void preorder(treePtr); 37 | void insert(treePtr*, int); 38 | treePtr modifiedSearch(treePtr, int); 39 | void main() { 40 | int i, n, A[MAX_TERMS]; 41 | treePtr tree = NULL; 42 | // 파일 오픈한다. 43 | // 파일을 읽어 정수의 개수는 n 에, 삽입할 정수들은 A 에 저장한다. 44 | // 순서대로 binary search tree 에 삽입한다. 45 | for (i=0; i 2 | #include 3 | #include 4 | 5 | #define MAX_TERMS 100 6 | typedef struct treeNode* treePtr; 7 | struct treeNode { 8 | treePtr lchild; 9 | int data; 10 | treePtr rchild; 11 | }; 12 | 13 | void inorder(treePtr); 14 | void preorder(treePtr); 15 | void insert(treePtr*, int); 16 | treePtr modifiedSearch(treePtr, int); 17 | 18 | void inorder(treePtr tree) { 19 | if (tree->lchild != NULL) { 20 | inorder(tree->lchild); 21 | } 22 | printf("%d ", tree->data); 23 | if (tree->rchild != NULL) { 24 | inorder(tree->rchild); 25 | } 26 | }; 27 | 28 | void preorder(treePtr tree) { 29 | printf("%d ", tree->data); 30 | if (tree->lchild != NULL) { 31 | preorder(tree->lchild); 32 | } 33 | if (tree->rchild != NULL) { 34 | preorder(tree->rchild); 35 | } 36 | }; 37 | 38 | treePtr modifiedSearch(treePtr tree, int key) { 39 | treePtr last = NULL; 40 | while (tree) { 41 | if (key == tree->data) return NULL; 42 | if (key < tree->data) { 43 | last = tree; 44 | tree = tree->lchild; 45 | } else { 46 | last = tree; 47 | tree = tree->rchild; 48 | } 49 | } 50 | return last; 51 | } 52 | 53 | void insert(treePtr *node, int num) { 54 | treePtr ptr, temp = modifiedSearch(*node, num); 55 | if (temp || !(*node)) { 56 | ptr = (treePtr) malloc(sizeof(struct treeNode)); 57 | if (ptr == NULL) { 58 | fprintf(stderr, "The memory is full"); 59 | exit(1); 60 | } 61 | ptr->data = num; 62 | ptr->lchild = ptr->rchild = NULL; 63 | if (*node) { 64 | if (num < temp->data) { 65 | temp->lchild = ptr; 66 | } else { 67 | temp->rchild = ptr; 68 | } 69 | } 70 | else *node = ptr; 71 | } 72 | } 73 | 74 | int main() { 75 | int n = 0, a[MAX_TERMS]; 76 | FILE* fp = fopen("input.txt", "r"); 77 | 78 | treePtr tree = NULL; 79 | fscanf(fp, "%d", &n); 80 | for (int i = 0; i < n; i++) { 81 | fscanf(fp, "%d", &a[i]); 82 | insert(&tree, a[i]); 83 | } 84 | 85 | inorder(tree); 86 | printf("\n"); 87 | preorder(tree); 88 | printf("\n"); 89 | 90 | fclose(fp); 91 | return 0; 92 | } -------------------------------------------------------------------------------- /MAT2410 Applied Mathematics I/README.md: -------------------------------------------------------------------------------- 1 | # 응용수학I 2 | 3 | MAT2410 - Applied Mathematics I 4 | 5 | 2019년 1학기 / 유지현 강사님 6 | 7 | - 통계학의 기초를 배웁니다. 8 | 9 | - [과제 유형 1](homework-1) 10 | - [중간고사 문제 및 해설](midterm/midterm.pdf) 11 | - [강의 노트](note.pdf) 12 | 13 | 개인적인 답안이기 때문에 정답이 아닐 수도 있습니다. 틀린 경우 Issues에 제보해주세요! 14 | 15 | ## Class 16 | 17 | - 매주 과제가 있습니다. 3문제 정도입니다. -------------------------------------------------------------------------------- /MAT2410 Applied Mathematics I/homework-1/README.md: -------------------------------------------------------------------------------- 1 | # 과제 유형 1 2 | 3 | * [과제 #1](hw1-1.pdf): 교재 1~3장 연습문제 51제 4 | * [과제 #2](hw1-2.pdf): 교재 4~5장 연습문제 40제 5 | * [과제 #3](hw1-3.pdf): 교재 7~9장 연습문제 42제 6 | 7 | # 과제 유형 2 8 | 9 | * 과제 #1 10 | * 과제 #2: 확률변수 11 | * 과제 #3: 결합확률분포 - 주변확률밀도함수, 조건부확률밀도함수 12 | * 과제 #4: 결합확률분포 - 조건부확률밀도함수, 독립, 상관관계 13 | * 과제 #5-6: 연속확률분포 - 감마분포, 지수분포, 포아송분포 14 | * 과제 #7: 확률변수, 결합확률분포 15 | * 과제 #8: 중심극한정리, 이항분포의 정규분포 근사 16 | * 과제 #9: 표본분포 - 불편추정량, 신뢰구간 17 | * 과제 #10: 추정 18 | * 과제 #11-12: 신뢰구간, 가설검정 -------------------------------------------------------------------------------- /MAT2410 Applied Mathematics I/homework-1/header.tex: -------------------------------------------------------------------------------- 1 | \documentclass[runningheads]{llncs} 2 | \usepackage[paperheight=295mm,paperwidth=210mm]{geometry} 3 | \usepackage{graphicx} 4 | \usepackage{import} 5 | \usepackage{kotex} 6 | \usepackage{amsmath} 7 | \usepackage{amssymb} 8 | \usepackage[dvipsnames]{xcolor} 9 | \usepackage{fancyvrb} 10 | \usepackage{listings} 11 | \usepackage{indentfirst} 12 | \usepackage{tabularx} 13 | \usepackage{underscore} 14 | \usepackage{multicol} 15 | \usepackage{tikz} 16 | \usepackage[square,sort,comma,super]{natbib} 17 | \usepackage{inconsolata} % Inconsolata 18 | \usepackage{mathptmx} % Times New Roman 19 | \usepackage[cache=false]{minted} 20 | \graphicspath{ {./images/} } 21 | \lstset{basicstyle=\footnotesize\ttfamily,breaklines=true} 22 | \renewcommand{\bibname}{참고문헌} 23 | \setlength{\parindent}{1em} 24 | \setlength{\parskip}{1em} 25 | \linespread{1.2} 26 | {\renewcommand{\arraystretch}{1.5}% 27 | \setlength{\tabcolsep}{0.5em}% 28 | \newenvironment{Figure} 29 | {\par\medskip\noindent\minipage{\linewidth}} 30 | {\endminipage\par\medskip} 31 | \newcommand{\translation}[1]{\textsuperscript{#1}} -------------------------------------------------------------------------------- /MAT2410 Applied Mathematics I/homework-1/hw/1-1.tex: -------------------------------------------------------------------------------- 1 | % Problem 2 2 | \subsubsection{2.} 다음 상황에 맞는 표본공간을 구하라. 3 | \begin{itemize} 4 | \item[(1)] ``1"의 눈이 나올 때까지 공정한 주사위를 반복하여 던진 횟수 5 | \item[(2)] 영하 5도에서 영상 7.5도까지 24시간 동안 연속적으로 기록된 온도계의 눈금의 위치 6 | \item[(3)] 형광등을 교체한 후로부터 형광등이 나갈 때까지 걸리는 시간 7 | \end{itemize} 8 | 9 | \paragraph{Solution.} 10 | \begin{itemize} 11 | \item[(1)] $S = \left\{ x \middle| x \geq 1 \right\}$ 12 | \item[(2)] $S = \left\{ x \middle| -5 \leq x \leq 7.5 \right\}$ 13 | \item[(3)] $S = \left\{ x \middle| x \geq 0 \right\}$ 14 | \end{itemize} 15 | 16 | % Problem 5 17 | \subsubsection{5.} 주머니 안에 빨간색과 파란색의 공깃돌이 두 개씩 들어 있는 주머니에서 공깃돌 두 개를 차례로 꺼낸다. 18 | \begin{itemize} 19 | \item[(1)] 나올 수 있는 공깃돌의 색에 대한 표본공간을 구하라. 20 | \item[(2)] 공깃돌 두 개가 서로 다른 사건을 구하라. 21 | \item[(3)] 파란색이 많아야 한 개인 사건을 구하라. 22 | \item[(4)] 첫 번째 공깃돌이 빨간색이고, 두 번째 공깃돌이 파란색인 사건을 구하라. 23 | \end{itemize} 24 | 25 | \paragraph{Solution.} 빨간색 공깃돌이 나오는 사건을 $R$, 파란색 공깃돌이 나오는 사건을 $B$라 하자. 26 | \begin{itemize} 27 | \item[(1)] $S = \left\{ RR, RB, BR, BB \right\}$ 28 | \item[(2)] $S = \left\{ RB, BR \right\}$ 29 | \item[(3)] $S = \left\{ RR, RB, BR \right\}$ 30 | \item[(4)] $S = \left\{ RB \right\}$ 31 | \end{itemize} 32 | -------------------------------------------------------------------------------- /MAT2410 Applied Mathematics I/homework-1/hw/1-3-diagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shiftpsh/sgcs/9456210b675ee514384d2b7867036bea5f525211/MAT2410 Applied Mathematics I/homework-1/hw/1-3-diagram.png -------------------------------------------------------------------------------- /MAT2410 Applied Mathematics I/homework-1/hw/4-1.tex: -------------------------------------------------------------------------------- 1 | % Problem 3 2 | \subsubsection{3.} $X$는 2와 11 사이의 정수들에 대하여 이산균등분포를 이룬다고 한다. 3 | 4 | \begin{itemize} 5 | \item [(1)] $X$의 확률질량함수를 구하라. 6 | \item [(2)] $Y=X-1$의 확률질량함수를 구하라. 7 | \item [(3)] $Y=X-1$의 평균과 분산을 구하라. 8 | \item [(4)] $X$의 평균과 분산을 구하라. 9 | \end{itemize} 10 | 11 | \paragraph{Solution.} 12 | \begin{itemize} 13 | \item [(1)] { 14 | \[f\left(x\right) = \frac{1}{11 - 2 + 1} = \frac{1}{10} \qquad 2 \leq x \leq 11,\, x \in \mathbb{Z}\] 15 | } 16 | \item [(2)] { 17 | \[f\left(y\right) = \frac{1}{10} \qquad 1 \leq y \leq 10,\, y \in \mathbb{Z}\] 18 | } 19 | \item [(3)] { 20 | $Y \sim \mathrm{DU}\left(10\right)$이므로 기댓값은 \[\frac{1 + 10}{2} = \frac{11}{2}\]이고, 분산은 \[\frac{10^2-1}{12}=\frac{33}{4}\]이다. 21 | } 22 | \item [(4)] { 23 | \begin{align*} 24 | \mathrm{E}\left(X\right) &= \mathrm{E}\left(Y + 1\right) = \frac{13}{2} \\ 25 | \mathrm{Var}\left(X\right) &= \mathrm{Var}\left(Y + 1\right) = \frac{33}{4} 26 | \end{align*} 27 | } 28 | \end{itemize} 29 | -------------------------------------------------------------------------------- /MAT2410 Applied Mathematics I/homework-1/hw/5-1.tex: -------------------------------------------------------------------------------- 1 | % Problem 1 2 | \subsubsection{1.} $X\sim\mathrm{U}\left(-1,\,1\right)$에 대하여 다음을 구하라. 3 | 4 | \begin{itemize} 5 | \item [(1)] 확률밀도함수와 분포함수 6 | \item [(2)] 평균과 분산 7 | \item [(3)] $P\left(\mu - \sigma < X < \mu + \sigma\right)$ 8 | \item [(4)] 사분위수 9 | \end{itemize} 10 | 11 | \paragraph{Solution.} 12 | 13 | \begin{itemize} 14 | \item [(1)] { 15 | 확률밀도함수 $f$는 다음과 같다. 16 | \[f\left(x\right) = \frac{1}{2} \qquad -1 \leq x \leq 1\] 17 | 분포함수 $F$는 다음과 같다. 18 | \[F\left(x\right) = \frac{x+1}{2} \qquad -1 \leq x \leq 1\] 19 | } 20 | \item [(2)] { 21 | \begin{align*} 22 | \mu &= \frac{1 + \left(-1\right)}{2} = 0 \\ 23 | \sigma^2 &= \frac{\left(1-\left(-1\right)\right)^2}{12} = \frac{1}{3} 24 | \end{align*} 25 | } 26 | \item [(3)] { 27 | \begin{align*} 28 | & P\left(\mu - \sigma < X < \mu + \sigma\right) \\\ 29 | &= P\left(-\sqrt{\frac{1}{3}} < X < \sqrt{\frac{1}{3}}\right) \\ 30 | &= \frac{1}{2} \times 2\sqrt{\frac{1}{3}} \\ 31 | &\approx 0.5773 32 | \end{align*} 33 | } 34 | \item [(4)] $Q_1 = -0.5$, $Q_2 = 0$, $Q_3 = 0.5$ 35 | \end{itemize} 36 | 37 | % Problem 3 38 | \subsubsection{3.} 특수직에 근무하는 사람들 중에서 임의로 선정한 사람의 출생에서 사망에 이르기까지 걸리는 시간을 $X$라 하자. 39 | 그러면 이 사람의 생존 시간 $X$는 사고나 질병에 의하지 않는다면 $\left[0,\,65.5\right]$에서 균등분포를 이룬다고 한다. 40 | 41 | \begin{itemize} 42 | \item [(1)] $X$의 밀도함수와 분포함수를 구하라. 43 | \item [(2)] $X$의 평균을 구하라. 44 | \item [(3)] 임의로 선정된 사람이 60세 이상 생존할 확률을 구하라. 45 | \item [(4)] 임의로 선정된 사람이 45세 이상 살았다는 조건 아래서, 60세 이상 생존할 확률을 구하라. 46 | \item [(5)] (4)의 조건 아래서, $x$ $\left(45 < x \leq 65.5\right)$세 이상 생존할 확률을 구하라. 47 | \end{itemize} 48 | 49 | \paragraph{Solution.} 50 | 51 | \begin{itemize} 52 | \item [(1)] { 53 | 확률밀도함수 $f$는 다음과 같다. 54 | \[f\left(x\right) = \frac{1}{65.5} \qquad 0 \leq x \leq 65.5\] 55 | 분포함수 $F$는 다음과 같다. 56 | \[F\left(x\right) = \frac{x}{65.5} \qquad 0 \leq x \leq 65.5\] 57 | } 58 | \item [(2)] { 59 | \[E\left(X\right) = \frac{0+65.5}{2} = 32.75\] 60 | } 61 | \item [(3)] { 62 | \begin{align*} 63 | P\left(X\geq 60\right) &= 1 - F\left(60\right) \\ 64 | &= 1 - \frac{60}{65.5} \approx 0.0840 65 | \end{align*} 66 | } 67 | \item [(4)] { 68 | \begin{align*} 69 | P\left(X\geq 60 \,\middle|\, X\geq 45\right) &= \frac{1-F\left(60\right)}{1-F\left(45\right)} \\ 70 | &= \frac{1 - \dfrac{60}{65.5}}{1 - \dfrac{45}{65.5}} \\ 71 | &\approx 0.2683 72 | \end{align*} 73 | } 74 | \item [(5)] { 75 | \begin{align*} 76 | P\left(X\geq x \,\middle|\, X\geq 45\right) &= \frac{1-F\left(x\right)}{1-F\left(45\right)} \\ 77 | &= \frac{1 - \dfrac{x}{65.5}}{1 - \dfrac{45}{65.5}} \\ 78 | &= \frac{65.5 - x}{20.5} = \frac{131 - 2x}{41} \qquad 45