연습문제만-수정

Similar documents
歯9장.PDF

: 1 int arr[9]; int n, i; printf(" : "); scanf("%d", &n); : : for(i=1; i<10; i++) arr[i-1] = n * i; for(i=0; i<9; i++) if(i%2 == 1) print

chap7.key


Microsoft PowerPoint - chap13-입출력라이브러리.pptx

K&R2 Reference Manual 번역본

untitled

untitled


Microsoft PowerPoint - chap10-함수의활용.pptx


프로그램을 학교 등지에서 조금이라도 배운 사람들을 위한 프로그래밍 노트 입니다. 저 역시 그 사람들 중 하나 입니다. 중고등학교 시절 학교 도서관, 새로 생긴 시립 도서관 등을 다니며 책을 보 고 정리하며 어느정도 독학으르 공부하긴 했지만, 자주 안하다 보면 금방 잊어

untitled

chap8.PDF

금오공대 컴퓨터공학전공 강의자료

untitled

Microsoft PowerPoint - 제11강 파일 처리

13주-14주proc.PDF

À©µµ³×Æ®¿÷ÇÁ·Î±×·¡¹Ö4Àå_ÃÖÁ¾

歯7장.PDF

chap7.PDF

13 주차문자열의표현과입출력

0. 표지에이름과학번을적으시오. (6) 1. 변수 x, y 가 integer type 이라가정하고다음빈칸에 x 와 y 의계산결과값을적으시오. (5) x = (3 + 7) * 6; x = 60 x = (12 + 6) / 2 * 3; x = 27 x = 3 * (8 / 4

int main(void) int a; int b; a=3; b=a+5; printf("a : %d \n", a); printf("b : %d \n", b); a b 3 a a+5 b &a(12ff60) &b(12ff54) 3 a 8 b printf(" a : %x \

본 강의에 들어가기 전

Microsoft PowerPoint - chap4 [호환 모드]

<4D F736F F F696E74202D D20B9AEC0DABFAD2C20BDBAC6AEB8B2B0FA20C6C4C0CF20C0D4C3E2B7C2>

Microsoft PowerPoint - Chapter_07.pptx

<4D F736F F F696E74202D20C1A63132B0AD20B5BFC0FB20B8DEB8F0B8AEC7D2B4E7>

03장.스택.key

Microsoft PowerPoint - Chapter_09.pptx

<4D F736F F F696E74202D2034C5D8BDBAC6AEC6C4C0CFC0D4C3E2B7C2312E505054>

Microsoft PowerPoint - chap11-포인터의활용.pptx

Microsoft PowerPoint - Lesson13.pptx

C 언어 프로그래밊 과제 풀이

11장 포인터

Microsoft PowerPoint APUE(Intro).ppt

<443A5C4C C4B48555C B3E25C32C7D0B1E25CBCB3B0E8C7C1B7CEC1A7C6AE425CBED0C3E0C7C1B7CEB1D7B7A55C D616E2E637070>

Computer Programming (2008 Fall)

목차 포인터의개요 배열과포인터 포인터의구조 실무응용예제 C 2

Microsoft PowerPoint - 10장 문자열 pptx

11장 포인터

PowerPoint 프레젠테이션

C Programming

, ( ),, ( ), 3, int kor[5]; int eng[5]; int Microsoft Windows 4 (ANSI C2 ) int kor[5] 20 # define #define SIZE 20 int a[10]; char c[10]; float

; struct point p[10] = {{1, 2, {5, -3, {-3, 5, {-6, -2, {2, 2, {-3, -3, {-9, 2, {7, 8, {-6, 4, {8, -5; for (i = 0; i < 10; i++){ if (p[i].x > 0 && p[i

Microsoft PowerPoint - chap-12.pptx

PowerPoint 프레젠테이션

PowerPoint 프레젠테이션

이번장에서학습할내용 문자표현방법 문자열표현방법 문자열이란무엇인가? 문자열의입출력 문자처리라이브러리함수 표준입출력라이브러리함수 인간은문자를사용하여정보를표현하므로문자열은프로그램에서중요한위치를차지하고있다. 이번장에서는 C 에서의문자열처리방법에대하여자세히살펴볼것입니다. 2

(Asynchronous Mode) ( 1, 5~8, 1~2) & (Parity) 1 ; * S erial Port (BIOS INT 14H) - 1 -

Microsoft PowerPoint - 10_C_Language_Text_Files

Microsoft PowerPoint - 09_(C_Programming)_(Korean)_File_Processing

C 프로그래밊 개요

ABC 11장

C 프로그래밊 개요

이번장에서학습할내용 동적메모리란? malloc() 와 calloc() 연결리스트 파일을이용하면보다많은데이터를유용하고지속적으로사용및관리할수있습니다. 2

금오공대 컴퓨터공학전공 강의자료

PowerPoint 프레젠테이션

<4D F736F F F696E74202D20B8AEB4AABDBA20BFC0B7F920C3B3B8AEC7CFB1E22E BC8A3C8AF20B8F0B5E55D>

C++-¿Ïº®Çؼ³10Àå

chap10.PDF

중간고사

Microsoft PowerPoint - 7_배열_문자열

untitled

untitled

< E20C6DFBFFEBEEE20C0DBBCBAC0BB20C0A7C7D12043BEF0BEEE20492E707074>

Microsoft PowerPoint - Chap14_FileAccess.pptx

1 장 C 언어복습 표준입출력배열포인터배열과포인터함수 const와포인터구조체컴파일러사용방법 C++ 프로그래밍입문

<322EBCF8C8AF28BFACBDC0B9AEC1A6292E687770>

商用

C프로-3장c03逞풚

Microsoft PowerPoint - chap12-고급기능.pptx

윤성우의 열혈 TCP/IP 소켓 프로그래밍

Chapter_06

Microsoft PowerPoint - Chapter_05.pptx

T100MD+

문서의 제목 나눔명조R, 40pt

Microsoft PowerPoint - chap03-변수와데이터형.pptx

BMP 파일 처리

구조체정의 자료형 (data types) 기본자료형 (primitive data types) : char, int, float 등과같이 C 언어에서제공하는자료형. 사용자정의자료형 (user-defined data types) : 다양한자료형을묶어서목적에따라새로운자료형을

Microsoft PowerPoint - Chapter8.pptx

5.스택(강의자료).key

02장.배열과 클래스

기초컴퓨터프로그래밍

PowerPoint 프레젠테이션

제 14 장포인터활용 유준범 (JUNBEOM YOO) Ver 본강의자료는생능출판사의 PPT 강의자료 를기반으로제작되었습니다.

PowerPoint 프레젠테이션

PowerPoint 프레젠테이션

슬라이드 1

컴파일러

Microsoft PowerPoint - ch08 - 구조체 (structure) am0845

<4D F736F F F696E74202D D20B9AEC0DABFAD2C20BDBAC6AEB8B2B0FA20C6C4C0CF20C0D4C3E2B7C2>

C Language Programming_문제.hwp

PowerPoint 프레젠테이션

14 주차구조체와공용체

Microsoft PowerPoint - chap11-1.ppt [호환 모드]

11장 포인터

Microsoft PowerPoint - ch09 - 연결형리스트, Stack, Queue와 응용 pm0100

PowerPoint 프레젠테이션

Transcription:

10 Call by Value Call by Reference Call by Result Call by Name C call by value void int X int hap(int x, int y, int z, int w); printf("%d\n", hap(3, 4, 5, 6)); int hap(int x, int y, int z, int w) return x+y+z+w; main X 01 int expo(int a, int b) 02 03 int i,s=1; 04 for(i=1;i<=b;i++) 05 s=s*a; 06 return s; 07

for(i=1;i<=4;i++) s=s*3; 3 81 void gugudan(int k); int dan; printf(" >"); scanf("%d", &dan); gugudan(dan); void gugudan(int k) int i; for(i=1; i<=9; i++) printf("\n%d*%d=%2d", k, i, k*i); int num1, num2; printf(" Enter>"); scanf("%d", &num1); if (num1<0) num2=-num1; printf("\n%d %d", num1, num2); int absolute(int abs) if (abs<0) abs=-abs; return abs;

double factorial(int n); int n; printf("\n n!. "); printf("\n n Enter"); scanf("%d", &n); printf("\n n!=%.lf", factorial(n)); double factorial(int k) double product=1; int i; for(i=1; i<=k; i++) product=product*i; return product; int IsLeapYear (int y); int year; printf( : ); scanf( %d, &year); printf( \n %d ); if (IsLeapYear(year)) printf(. ); else printf(. ); int IsLeapYear(int y) if((y%4==0 && y%100!=0) y%400==0) return 1; else

01 02 #define square(x) x*x 03 04 05 int y=4; 06 printf("%d %d\n",square(y), square(y+1)); 07 08 square(y+1)=y+1*y+1 01 02 int number(int i); 03 04 05 number(6); 06 07 08 int number(int i) 09 10 if (i==0) 11 return i; 12 else 13 number(i/2); 14 printf("%d", i%2); 15 number() 2

11 X X X X RAM register register

01 02 03 04 int x=0; 05 06 int x=2; 07 printf("%d", x); 08 x+=3; 09 10 printf("%d", x); 11 12 line 10 x line 04 x 0

12 stdio.h string.h math.h dos.h scanf() scanf() gets() NULL 1 byte 2 byte fflush() fflush() #include <ctype.h> char ch='a'; printf("%d", isalnum(ch)); printf("%d", isalpha(ch)); printf("%d", isascii(ch)); printf("%d", isdigit(ch)); printf("%d", isupper(ch)); #include <string.h> char *a="visual c++", b[6]="turbo"; printf("%d \n", strlen(a)); printf("%d \n", strcmp(a,b)); printf("%s \n", strlwr(b)); printf("%s \n", strupr(a));

01 02 #include <time.h> 03 04 05 int i, x, y; 06 srand(time(null)); 07 for(i=1;i<=10;i+=1) 08 09 x=rand()%9+1; 10 y=rand()%9+1; 11 printf("%d*%d=?\n", x, y); 12 13 01 02 03 04 05 06 long num=9876543; 07 08 09 char s[25]; ltoa(num2, s, 10); printf( :%d, :%s\n, num, s); 10 printf( :%d\n, strlen(s); ); 11 12

01 02 #include <time.h> 03 #include <conio.h> 04 05 06 int i; 07 time_t t1. t2; 08 t1 = time(null); 09 printf( : \n%s, ctime(&t1)); 10 11 t2=t1+3*60l*60l; printf( : \n%s, ctime(&t2)); 12 13 01 02 #include <stdlib.h> 03 #include <time.h> 04 #include <conio.h> 05 06 07 int i; 08 time_t t; 09 srand(time(null)); 10 printf(". \n"); 11 printf(".\n"); 12 printf(".\n\n"); 13 printf(" \n"); 14 while(!kbhit()) 15 16 17 printf("%2d", rand()%9+1); 18 printf("%2d", rand()%10); 19 printf("%2d", rand()%10); 20 printf("%2d", rand()%10); 21 printf("\n."); 22 23

A Z 100 rand() 01 02 #include <stdlib.h> 03 #include <time.h> 04 05 06 int i, rnd; 07 char alpha[26]=0; 08 srand(time(null)); 09 printf(" \n"); 10 for(i=1;i<=100;i++) 11 12 rnd=rand()%26; 13 printf("%c", rnd+65); 14 if (i%20==0) 15 printf("\n"); 16 alpha[rnd]+=1; 17 18 printf("\n \n"); 19 for(i=0;i<26;i++) 20 21 printf("%c : %2d ", i+65, alpha[i]); 22 if ((i+1)%3==0) 23 printf("\n"); 24 25 26

01 02 #include <math.h> 03 #define PI 3.141592 04 05 06 int i, j ; 07 double sine; 08 for(i=0 ; i<=360 ; i+=15) 09 10 sine=sin((double)i*pi/180); 11 printf("sin(%3d)= %5.2f", i,sine); 12 for(j=0 ;j<=(int)(sine*10+10);j++) 13 printf(" "); 14 printf("* \n"); 15 16 17 #define PI 3.141592 sin((double)i*pi/180); for(j=0; j<=(int)(sine*10+10); j++) printf(" "); sine sine sine '*' sin(0 0 )=0 11 (for(j=0;j<=10;j++) '*' sin(45 0 )=0.71 18 (for(j=0;j<=17;j++) '*'

01 #include <time.h> 02 03 04 05 int birthy, birthm, birthd; 06 time_t curr; 07 struct tm *d; 08 curr=time(null); 09 d=localtime(&curr); 10 printf(" Enter.>"); 11 scanf("%d",birthy); 12 printf(" Enter.>"); 13 scanf("%d",birthm); 14 printf(" Enter.>"); 15 scanf("%d",birthd); 16 printf("\n"); 17 printf("%d %d %d \n",d->tm_year+1900,d->tm_mon+1, d->tm_mday); 18 printf(" \n); 19 printf("%d %d %d \n",birthy, birthm, birthd); 20 diffy=birthy-d->tm_year+1900; 21 diffm=birthm-d->tm_mon+1; 22 diffd=birthd-d->tm_mday; 23 if (diffm<=0 && diffd<=0) 24 diffy++; 25 printf(" : %d\n", diffy); 26 27

13 int korea[5]; int korea[ ]=2, 4, 9, 6; int korea[3]=2, 4, 9, 6; int korea[5]=2, 8; 0 char c[5]="test"; char c[2]="test"; char c[7]="test"; char c[ ]="test"; auto int num[8]; static int num[8]; register int num[8]; extern int num[8]; 2 3 cha int int arr[7]=11, 22, 33, 44, 55, 66, 77; int i; for(i=1; i<=6; i=i+2) printf("%d\n", arr[i]);

i 1 2 arr[1]arr[3]arr[5] 0 < int arr[3]=0; int i; for(i=0; i<3; i++) printf("%d", arr[i]); %c %d %d ASCII char ch[9]="cprogram"; int i; for(i=0; i<8; i++) printf("%c : %3d\n", ch[i], ch[i]); i 2 0 d[2-i] 0 2 double d[3]=0.1, 0.2, 0.3; int i; for(i=2;i>=0;i--) printf("%.1f\n", d[2-i]);

korea[9] japan[9] int korea[9]=0,0,0,0,1,0,0,2,0; int japan[9]=1,1,0,0,0,0,0,0,0; int i, sumk=0, sumj=0; printf(" \n\n"); printf(" \t 1 2 3 4 5 6 7 8 9 Run\n"); printf("korea\t"); for(i=0;i<9;i++) sumk+=korea[i]; printf("%2d", korea[i]); printf("%3d\n", sumk); printf("japan\t"); for(i=0;i<9;i++) sumj+=japan[i]; printf("%2d", japan[i]); printf("%3d\n", sumj); 6 char num[7];

atof() atol() ASCII char num[7]="871209"; 1987 (num[0]-48)*10+num[1]-48+1900; '8' ASCII 56 48 8 10 10 '7' 1 num[1]-48 1900 int birthy, birthm, birthd; char num[7]; printf(">"); scanf("%s", num); birthy=(num[0]-48)*10+num[1]-48+1900; birthm=(num[2]-48)*10+num[3]-48; birthd=(num[4]-48)*10+num[5]-48; printf("%d %d %d. \n", birthy, birthm, birthd); t(1,2)=3; t[1,2]=3; t(0)(1)=3; t[0][1]=3; C

int a[2][3]=, i, j; for(i=0; i<2;i++) for (j=0;j<3;j++); printf( %d, a[i][j]); printf( \n ); int a[2][2][2]=1,2,3,4,5,6,7,8 3 a a[0][0][0]=1, a[0][0][1]=2 a[0][1][0]=3, a[0][1][1]=4 a[1][0][0]=5, a[1][0][1]=6 a[1][1][0]=7, a[1][1][1]=8 transpose matrix matrix[i][j] matrix[j][i]

int matrix[3][3]=2, 4, 6, 8, 10, 12, 14, 16, 18; int i, j; printf("original matrix\n"); for(i=0;i<3;i++) for(j=0;j<3;j++) printf("%2d", matrix[i][j]); printf("\n"); printf("\ntranspose matrix\n"); for(i=0;i<3;i++) for(j=0;j<3;j++) printf("%2d", matrix[j][i]); printf("\n"); long money, i, num; long money_unit[]=10000, 5000, 1000, 500, 100, 50, 10; printf(" Enter>"); scanf("%ld", &money); printf("\n \n"); for(i=0; i<=6;i++) num=money/money_unit[i]; printf("%5ld : %3ld \n", money_unit[i], num); money=money-num*money_unit[i];

ASCII 'A' 10 65'a' 97 'A' 'A'-65 0 chr[0] 'a' 'a'-97=0 chr[0] char ch[100]=0; int chr[26]=0, i, nchr=0; printf(" count\n\n"); printf(" Enter\n"); printf(">"); gets(ch); for(i=0; ch[i]!=0 ; i++) if( ch[i]>=65 && 90>= ch[i]) chr[ch[i]-65]+=1; else if (ch[i]>=97 && 122>= ch[i]) chr[ch[i]-97]+=1; else nchr+=1; printf("\n\n"); printf(" count\n"); for(i=0; i<26 ; i++) if (chr[i]==0) continue; printf("%c \t %d\n", i+65, chr[i]); printf("\t %d", nchr);

strlen() ASCII #include <string.h> char str[11]; int i, len, sum; printf(" Enter>"); scanf("%s", str); len = strlen(str); sum = 0; for(i = 0; i < len; i++) sum += str[i]; printf(" : %d\n", sum);

14 int x; x &x x x x x *po+3 *(po+3) int a[4]=10,20,30,40; int *po=a; 13, 20 13, 40 23, 30 23, 40 13, 23 *po+3 a[0]+3 13*(po+3) a[3] 40 int a[5]=3,1,4,2,5; int *pt=a; *a+1 a[1] *(a+1) *++pt *(pt+1) a a[0] *a+1 a[0]+1 4 *(a+1) a[1] *(pt+1) *++pt ++pt; *pt; a[1] int i=15, j=20, *p; p=&i; j=*p; printf("%d",j); int *p, y[3]=2, 6, 9, a; p=y; a=*p++; printf("%d", a); printf("%d", *p);

int i=15, j=20, *p; p=&i; p i j=*p; p 15 j printf("%d",j); a=*p++ a=*p; p++ a=*p; a y[0] p p++ y[1] char *p="computer"; int i=0; while(*p!= '\0') i++; p++; printf("%d",i); char x[3], *p; x[1]='a'; p=&x[1]; *(p-1)='b'; *(p+1)='c'; printf("%c %c %c\n",*(p-1),*p,*(p+1)); char *p="computer"; p "computer" 'c' while p NULL p i while NULL i "computer" 8

int k[]=1,2,3,4,*p; p=&k[0]; printf("%d\n",*p); p++; printf("%d\n",*p); printf("%d\n", *p+1); printf("%d\n",*(p+1)); p=&k[0]; p k k[0] p++; p k[1] *p k[1] *p+1 k[1]+1 2+1 3 *(p+1) p &k[1] &k[2] k[2] 3 int a[3][3]=1,2,3,4,5,6,7,8,9; int b[ ][3]=7,8, 9,10,11; printf(" %d \n", a[1][1]); printf(" %d \n",*(a[2]+2)); printf(" %d \n",(*(b+1))[2]); a a[0][0]=1, a[0][1]=2, a[0][2]=3, a[1][0]=4, a[1][1]=5, a[1][2]=6, a[2][0]=7, a[2][1]=8, a[2][2]=9, b b[0][0]=7, b[0][1]=8, b[0][2]=0, b[1][0]=9, b[1][1]=10, b[1][2]=11, *(a[2]+2) a[2] a[2][0] a[2]+1 a[2][1]* a[2]+2 a[2][2] 9 (b+1)

b[1][0] (*(b+1))[2] b[1][2] 6 char num[7]; atof() atol() ASCII char num[7]="871209"; 1987 = (num[0]-48)*10+num[1]-48+1900; '8' ASCII 56 48 8 10 10 '7' 1 num[1]-48 1900 1900 1900 12 0 int birthy, birthm, birthd, day; char *animal[12]=" ", " ", " ", "", " ", " ", " ", " ", " ", " ", " ", ""; char num[7]; printf(">"); scanf("%s", num); birthy=(num[0]-48)*10+num[1]-48+1900; birthm=(num[2]-48)*10+num[3]-48; birthd=(num[4]-48)*10+num[5]-48; printf("%d %d %d, \n", birthy, birthm, birthd);

day=(birthy-1900)%12; printf("%s.\n",animal[day]); long long money; long money_unit[]=1000, 500, 100, 50, 10, 5, 1, i, num; char *unit[]=" ", "", " ", "", " ", "", " "; printf(" Enter>"); scanf("%ld", &money); printf("\n\n"); money=money/10l; for(i=0; i<=6; i++) num=money/money_unit[i]; printf("\n%8s %3d", unit[i], num); money=money-num*money_unit[i];

int i; long cost[5]=135000, 235000, 122000, 260000, 73500; long quantity[5], sum, total=0; char *good[5]="hard disk", "CPU(intel)", "main board", "LCD monitor", "DVD-RW(combo)"; printf(" \n\n"); printf(" Enter\n"); for(i=0; i<=4; i++) printf("%s: >", good[i]); scanf("%ld", &quantity[i]); printf("\n\n"); printf("\n\t\t\t\t"); for(i=0; i<=4; i++) sum=cost[i]*quantity[i]; printf("\n%s\t%7ld\t%3ld %10ld ", good[i], cost[i], quantity[i], sum); total+=sum; printf("\n \t\t\t %12ld ", total);

15 struct user d_1; user PERSON; struct user data[10]; struct user *point; struct user $amount; struct std int number; char *name; ; std st1, st2; struct st1, st2; std struct st1, st2; struct std st1, st2; typedef st1, st2; var name name char name[10]; "kim" name="kim"; *name="kim"; var.name="kim"; strcpy(var.name, "kim"); strcpy() var name name char *name; "kim" name="kim"; *name="kim"; var.name="kim"; strcpy(var.name, "kim");

person struct person char name[10]; int age; ; struct person d="park", 18; strcpy(d.name, "Park"); scanf("%s", d.name); d.name="park"; d1 byte struct data char name[12]; long income; double saving; d1; 20 22 24 30 byte 12 byte + 4 byte + 8 byte = 24 byte #include <string.h> struct student char name[20]; int jumsu; ; struct student st; printf(" Enter"); scanf("%s", st.name ); printf(" Enter"); scanf("%d", &st.jumsu ); printf(" : %s \n, st.name ); printf(" : %d \n, st.jumsu ); scanf() &

& & struct planet char name[10]; long diameter; float distance; float speed; float cycle; char satellite; bit field bit field bit

struct person char name[10]; int age; ; struct person d, *pt; pt=&d; strcpy((*pt).name, "Choi"); strcpy(pt->name, "Choi"); scanf("%s", pt.name); scanf("%s", pt->name); struct person char *name; int age; ; struct person d, *pt; pt=&d; scanf("%s", pt->name); strcpy("%s", pt->name); (*pt).name="lee"; pt->name="lee"; struct human char name01[10]; char *name02; data01="miss Kim", "Mr Park"; struct company char com_name[10]; struct human people; int salary; data02="choi", "Kim", "Park", 500; printf("%s\n", data02.com_name); printf("%s\n", data02.people.name01); printf("%c\n", data01.name01[1]); printf("%d\n ",data02.salary);

company data02.com_name "Choi" data02.people.name01 "Kim" data01.name01 "Miss Kim" data01.name01[1] 'i' data02.salary 500

16 struct union 01 02 #include <string.h> 03 union univar 04 05 char ch1; 06 char ch2[4]; 07 ; 08 09 10 union univar data; 11 strcpy(data.ch2, "ABC"); 12 data.ch1='d'; 13 printf("%s\n", data.ch2); 14 15 DBC data 4 byte (char ch2[4];) strcpy(data.ch2, "ABC"); 1 bit byte 'A' 01000001 'B' 01000010 'C' 01000011

data.ch2[0] data.ch2[1] data.ch2[2] A B C NULL data.ch1= D ; data.ch2[10] data.ch1 data.ch2[1] data.ch2[2] D B C NULL DBC enum card spade, diamond, heart, clover; enum card card01; card01=heart; printf("card01 : %d\n", card01); printf("card01 : %d\n", diamond); printf("card01 : %d\n", spade); card01 : 2 card01 : 1 card01 : 0 enum tag_name week card01 enum card spade, diamond, heart, clover; enum card card01; 0 1 space 0 clover 3

struct patient char age; unsigned sex : 1; unsigned drinking : 1; unsigned smoking : 1; unsigned marriage : 1; ; struct patient a=24,0,1,1,0; printf("age :%d\n", a.age); printf("sex :%d\n", a.sex); printf("drinking :%d\n", a.drinking); printf("smoking :%d\n", a.smoking); char 1 byte 1 bit 4 bit 1 byte + 4 bit byte a 2 byte sex 01 drinking 1smoking 1 marriage 1 24 struct patient char age; unsigned sex : 1; unsigned drinking : 1; unsigned smoking : 1; unsigned marriage : 1; ; struct patient a=24,0,1,1,0; printf(" :%d\n", a.age); if (a.sex==0) printf(" : \n"); else printf(" : \n");

if (a.drinking==0) printf(" : \n"); else printf(" : \n"); if (a.smoking==0) printf(" : \n"); else printf(" : \n"); if (a.marriage==0) printf(" : \n"); else printf(" : \n");

17 ASCII byte type stream open fopen() open open fgets(), fputs() X 's' string fprintf printf fscanf() X fscanf() scanf() fgets() fread() FILE X C high level I/O low level I/O

CD X NULL X fopen() NULL NULL exit() r w a fgets() fscanf() fgetc() fputs()

#include <stdlib.h> char *name[3]=" "," ", " "; char *phone[3]="015-2745-2233", "016-0023-9983", "014-740-4502"; int i; FILE *fp; fp=fopen("sample", "w"); if (fp==null) printf("file open "); exit(1); for(i=0;i<3;i++) printf("%s %s\n", name[i], phone[i]); fprintf(fp, "%s %d\n", name[i], phone[i]); fclose(fp); #include <stdlib.h> char c; FILE *fp; fp=fopen( chr.txt, r ) if (fp==null) printf( File open ) exit(1); while((c=fgetc(fp))!= EOF ) printf( %c, c ) fclose(fp); printf( \n );

int i; long cost[5]=135000, 235000, 122000, 260000, 73500; long quantity[5], sum, total=0; char *good[5]="hard disk", "CPU(intel)", "main board", "LCD monitor", "DVD-RW(combo)"; FILE *fp; printf(" \n\n"); printf(" Enter\n"); for(i=0; i<=4; i++) printf("%s: >", good[i]); scanf("%ld", &quantity[i]); fp=fopen("sale.txt", "w"); printf("\n\n"); fprintf(fp, "\n\n"); printf("\n\t\t\t\t"); fprintf(fp, "\n\t\t\t\t\n"); for(i=0; i<=4; i++) sum=cost[i]*quantity[i]; printf("\n%s\t%7ld\t%3ld %10ld ", good[i], cost[i], quantity[i], sum); fprintf(fp, "%s\t%7ld\t%3ld %10ld \n", good[i], cost[i], quantity[i], sum); total+=sum; printf("\n \t\t\t %12ld ", total); fprintf(fp, "\n \t\t\t %12ld ", total); fclose(fp);

wb ASCII ASCII byte type random access block block block block block block block fread()block fread() block fseek() fp=fopen("group.dat", "rb"); fseek(fp, 0L, SEEK_END); total_block= ftell(fp) / sizeof(stdata) ; ftell() byte byte block byte blockbyte SEEK_END offset 0 byte ftell() block fseek(fp, 0L, SEEK_END); total=ftell(fp)/sizeof(stdata); block block

10 byte 10 byte 10 byte 10 byte block block block block whence block (n=1) block (SEEK_SET) (SEEK_END) offset fseek(fp, -30L, SEEK_END); 10 byte 10 byte 10 byte block block block block fseek(fp, 20L, SEEK_SET); 10 byte block block block block #include <stdlib.h> struct sale char good[15]; long cost; long quantity; long total;

; int i; long total=0; struct sale sdata[5]="hard disk", 135000, "CPU(intel)", 235000, "main board", 122000, "LCD monitor",260000, "DVD-RW(combo)",73500; FILE *fp; printf(" \n\n"); printf(" Enter\n"); for(i=0; i<=4; i++) printf("%s: >", sdata[i].good); scanf("%ld", &sdata[i].quantity); fp=fopen("sale.bin", "wb"); printf("\n\n"); printf("\n\t\t\t\t"); for(i=0; i<=4; i++) sdata[i].total=sdata[i].cost*sdata[i].quantity; printf("\n%s\t%7ld\t%3ld %10ld ", sdata[i].good, sdata[i].cost, sdata[i].quantity, sdata[i].total); total+=sdata[i].total; printf("\n \t\t\t %12ld ", total); if(fwrite(sdata, sizeof(sale), 5, fp)!=5) printf("file Write Error!"); exit(1); fclose(fp);

#include <stdlib.h> struct sale char good[15]; long cost; long quantity; long total; ; int i; long total_block, block_number; struct sale d; FILE *fp; fp=fopen("sale.bin", "rb"); fseek(fp, 0L, SEEK_END); total_block=ftell(fp)/sizeof(sale); printf(" \n\n"); printf(" block %d.\n", total_block); printf("0 \n", total_block); while(1) printf("\n block (1-%d) Enter >", total_block); scanf("%ld", &block_number); if (block_number==0) break; fflush(stdin); if (block_number> total_block) break; fseek(fp, sizeof(sale)*(block_number-1), SEEK_SET); if(fread(&d, sizeof(sale), 1, fp)!=1) printf("file read Error!");

exit(1); printf("\n\t\t\t\t\n"); printf("%s\t%7ld\t%3ld %10ld \n", d.good, d.cost, d.quantity, d.total); fclose(fp);