1 / 24

2 주 강의

2 주 강의. C 에 대한 기본 ( 제 2 장 ). 1.3 Variables, Expressions and Assignment. int miles, yards; /* 정수로 선언* / float kilometers; /* 실수로 선언 * / kiIometers = 1.609 * (miles + yards / 1760.0);  조심 .. ‘ 1760.0 ’  Assignment, 정수연산 , 실수연산 , coercison, 상수 (constant): 1760 printf 와 ‘ f ’.

Download Presentation

2 주 강의

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 2주 강의 C에 대한 기본 (제 2장)

  2. 1.3 Variables, Expressions and Assignment • int miles, yards; /* 정수로 선언*/ • float kilometers; /* 실수로 선언 */ • kiIometers = 1.609 * (miles + yards / 1760.0);  조심 .. ‘1760.0’  Assignment,정수연산, 실수연산, coercison, 상수 (constant): 1760 • printf와 ‘f’

  3. 1.4 The use of #define and #include • #define LIMIT 100 • #define PI 3.14159 • #include “my_file.h” :: ‘.h’의 뜻, ‘_’ • 예문 참고 (책 15page) 설명 • 프로그램 (책 15page) 설명 • ‘%22.7e’의 의미, double의 의미, e-format

  4. The use of printf() and scanf() • ‘f’는 formatted를 뜻함 • 함수(function)임 • c(character), d(decimal), f(floating number), g(e-format or f-format), s(string) • printf(“%c%c%c”, ‘a’, ‘b’, ‘c’)

  5. scanf() • 입력함수 • scanf(“%d”, &x) :: &x :: x의 주소 • 왜 scanf는 주소를 사용하나??? • c(character), d(decimal integer), f(floating-point), lf 또는 LF (double), s(string) • 20page의 프로그램 참조

  6. 1.6 Flow of control • if (expr) statement • a = 1; if (b == 3) a = 5 ; printf(“%d”, a); • if (expr) statement1elsestatement2 • if (a > b) max = a; else max = b; • Swap 프로그램 설명, 23 page 프로그램

  7. while 문 • 23 page 프로그램 설명 • While (I <= 5) { sum += i; ++i; } • i++  i=i+1; • while (expr) statement

  8. for 문 • For (expr1; expr2; expr3) statement • expr1; while (expr2) {statement; expr3; } • for ( i=1; i <= 5; ++i) sum+=i ; • 프로그램 설명 (26page) • If (scanf(%lf”, &x) != 1) … exit(1) … • stdlib.h

  9. for문 • printf(“%5d%9.1f%9.1f%9.1f%12.3f%12.3f\n”, i, x, min, max, sum, avg); • 2번째부터 for문을 적용시킨 이유?? • Keyboard 입력과 file 입력 파일 입력 ::: a.out < data *** UNIX를 공부하자 !!! 그룹별

  10. 1.7 Functions • Decomposition of problems and programs • double pow(double x, double y) ; • function prototype double pow(double, double); double pow();  전통 C • called :: pow(2,3)  8

  11. 함수의 형식 • type function_name(parameter type list) • Page 30 예제 프로그램 (최고, 최저 값) • return x; return y;

  12. Call by value (값에 의한 부름) • 값에 의한 부름 • 주소에 의한 부름을 값에 의한 부름으로 처리하는 방법 • scanf()와 printf()

  13. Arrays • int a[3]; • 모든 형에 적용 가능 • 37page 예제 설명 • 값 바꾸기 (swap) tmp = score[j-1]; score[j-1] = score[j]; score[j] = tmp;

  14. 숙제 • 정수를 정렬할 수 있는 프로그램을 구현해와야 한다!!! 자료의 개수는 100개쯤으로 하거나 그 이하도 좋다. 수행되어야 하고, 조교에게 설명해야 한다. 기간은 1주일이다. 다른 사람의 도움을 받아도 되나 그 내용은 알아야 한다, 특별히 2명은 수업시간에 방법을 설명하게 한다.

  15. Strings • getchar(), putchar() :: 한 음절씩 입출력 [stdio.h, ctype.h] • C에는 진실한 의미의 string은 없다 • 문자배열을 이용한 string 처리 • ‘\0’를 이용하는 게 표준 • 책에서는 ‘\n’을 ‘\0’으로 변환

  16. String 2 • [c = getchar() != ‘\n’] 에서 우선순위 • isalpha(c)의 역할 :: macro ?? c값을 그대로 더했다… pp. 97 • 이 프로그램의 문제점 (page 39) • %s의 역할 ; --i ??? • name[i] = ‘\0’;

  17. Pointers • Hardware (컴퓨터 메모리)의 주소를 사용 • 많은 프로그램 오류가 pointer의 사용에서 발생

  18. Pointer 예제프로그램 • 43page • #include <string.h> ::: strcpy() 함수 • char c= ‘a’, *p, s[MAXSTRING]; • p = &c; • printf문에서 일어나는 더하기 • strcpy의 설명, pointer assign과 구별

  19. Pointer 예문 • p = s+ 14  p=&s[14] • 결과 설명 • C는 array와 pointer가 밀접한 연관 : char *p, s[100]; : s[1]  *(s+i)

  20. Files • int c; FILE *ifp; ifp = fopen(“my_file”, “r”) • “r(read)”, “w(write)”, “a(append)” • ‘null (0)’이 return되면 오류다!!! • fclose() • Open할 수 있는 file의 수

  21. File 예제프로그램 1 • Int main(int argc, char *argv[]) arg[0]는 자신, arg[i]는 i번째 argument을 가르키는 pointer • $ cnt_letters chapter1 data1 • argc는 3 • <stdlib.h>

  22. File 예제프로그램 2 • EOF :: #define EOF (-1) <- <stdio.h> • EOF ::: getc()에서 마지막에 돌려주는 값 (end of file) • (c = getc(ifp)) != EOF • (c >= ‘A’ && c <= ‘Z’) • ++letter[c-’A’] • i % 6 ::: modulus operators

  23. File 예제프로그램 3 • 출력 …‘A’ + ‘i’, letter[i]; <프로그램 변형> 이 예제 프로그램을 소문자를 대문자로 바꾸어 문서 내에 있는 알파벳의 수를 세는 프로그램으로 바꾸어라 !!!!

  24. Operating System • UNIX • cc, vi • a.out • mv(move), cp(copy), cat, pr, more • Redirection ::: shell programming, pipeline, ….

More Related