II ( ) prog8-1.c s1542h017%./prog8-1 1 => 35 Hiroshi 2 => 23 Koji 3 => 67 Satoshi 4 => 87 Junko 5 => 64 Ichiro 6 => 89 Mari 7 => 73 D

Save this PDF as:
 WORD  PNG  TXT  JPG

Size: px
Start display at page:

Download "II ( ) prog8-1.c s1542h017%./prog8-1 1 => 35 Hiroshi 2 => 23 Koji 3 => 67 Satoshi 4 => 87 Junko 5 => 64 Ichiro 6 => 89 Mari 7 => 73 D"

Transcription

1 II ( ) prog8-1.c s1542h017%./prog8-1 1 => 35 Hiroshi 2 => 23 Koji 3 => 67 Satoshi 4 => 87 Junko 5 => 64 Ichiro 6 => 89 Mari 7 => 73 Daisuke 8 =>. 73 Daisuke 35 Hiroshi 64 Ichiro 87 Junko 23 Koji 89 Mari 67 Satoshi s1542h017% prog8-1.c C < <= 2 ( ) C 2 strcmp 1.1 C 1 strcmp int strcmp(const char *s1, const char *s2); strcpy strcmp #include <string.h> 1 2 ( ) s1 s2 strcmp 2 1 (1 ) int s1 s2 s1 s2 0 s1 s2 8 1

2 strcmp (char ) ASCII strcmp strcmp("abc", "abc") == 0 strcmp("abc", "xyz") < 0 strcmp("ab", "abc") < 0 strcmp("abc", "abd") < 0 strcmp("a", "ABC") > 0 "a" "ABC" ASCII a A B prog6-2.c quicksort ( ) ASCII prog8-1.c prog8-1.c #include <string.h> 1 quicksort ( ) 2 prog7-2.c ( ) ( ±1% ) prg7-2.c 1 (1) ( 3 ) 1 (C ) 2 A. 1 ASCII strcasecmp ( ) strcmp strcasecmp 8 2

3 1 clock time (2) ±1% ( ) 10 ( ) (3) (2) (4) (1) (3) 3 1 ( ) (1) (1) (3) checktime checktime 3 checktime (1-1) num (int ) sec (double ) 2 (1-2) Record data Record dp (1-3) data num name ( ) ( "Test") (1-4) clock reset 0 (1-5) clock on (1-6) (1-6-4) clock time sec 4 (1-6-1) data num score ( ) (rand() ) (1-6-2) dp num data (1-6-3) quicksort dp num score (1-6-4) clock time (1-7) clock off (1-6) 8 3

4 (1-8) clock reset 0 (1-9) clock on (1-10) (1-6) 2 (1-10-1) data num score ( ) (rand() ) (1-10-2) clock time 3 (1-11) clock off (1-10) (1-12) (ExpResult ) checktime typedef struct { int repeat; /* */ double t1; /* ( ) */ double t2; /* ( ) */ } ExpResult; repeat (1-6) t1 (1-10) t2 (1-6) (2) num ( ) 3.0 checktime ExpResult r (3) num ( ) 10.0*r.t2/(r.t2-r.t1) 1 checktime r 4 (4) r.repeat r.t2 t1 ( ) 1 (r.t2-r.t1)/r.repeat 3 main Unix (Linux) Windows OS C ( ) main main int main 2 (char * ) (char ** ) + 2 ( 0) ( ) ( ) 0 3 (1-6-4) clock time 4 ±1%

5 args.c 1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main(int argc, char *argv[]) 5 { 6 int i; 7 8 printf("argc = %d\n", argc); 9 for (i = 0; i < argc; i++) 10 printf("argv[%d] = %s\n", i, argv[i]); 11 return EXIT_SUCCESS; 12 } args.c s1542h017% cc -o args args.c s1542h017%./args argc = 1 argv[0] =./args s1542h017%./args -hello argc = 2 argv[0] =./args argv[1] = -hello s1542h017%./args This is a test. argc = 5 argv[0] =./args argv[1] = This argv[2] = is argv[3] = a argv[4] = test. s1542h017% args.c 1 main ( ) 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 int main(int argc, char *argv[]) 6 { 7 int a, b; 8 9 if (argc < 4) { 10 printf(" \n"); 11 return EXIT_FAILURE; 12 } 13 if (sscanf(argv[1], "%d", &a)!= 1) { 14 printf(" 1 \n"); 15 return EXIT_FAILURE; 16 } 17 if (sscanf(argv[3], "%d", &b)!= 1) { 18 printf(" 3 \n"); 19 return EXIT_FAILURE; calc.c 8 5

6 20 } if (strcmp(argv[2], " ") == 0) 23 printf("%d + %d = %d\n", a, b, a+b); 24 else if (strcmp(argv[2], " ") == 0) 25 printf("%d - %d = %d\n", a, b, a-b); 26 else if (strcmp(argv[2], " ") == 0) 27 printf("%d * %d = %d\n", a, b, a*b); 28 else if (strcmp(argv[2], " ") == 0) 29 printf("%d / %d = %d... %d\n", a, b, a/b, a%b); 30 else { 31 printf(" \n"); 32 return EXIT_FAILURE; 33 } return EXIT_SUCCESS; 36 } s1542h017%./calc = 28 s1542h017%./calc * 5 = 115 s1542h017%./calc / 5 = s1542h017%./calc = 18 s1542h017%./calc 23 5 s1542h017% calc.c 4 2 mprog2 TA 4.1 prog8-1.c 1.2 quicksort 1. prog8-1.c 4.2 prog8-2.c prog7-2.c prog8-2.c prog8-2.c ( ) ( ) 1 ( )

7 ±1% ( ) 10 prog8-2.c s1542h017%./prog8-2 s1542h017%./prog8-2 0 s1542h017%./prog ( ) / = s1542h017%./prog ( ) / = s1542h017%./prog ( ) / = s1542h017%./prog ( ) / 2458 = s1542h017%./prog ( ) / 119 = s1542h017%./prog s1542h017% 3. main prog7-3.c prog8-3.c prog8-2.c II 8 A. ASCII ( ) ( ) ASCII ( ) ASCII ASCII ASCII ( ) 8 7

8 0 NUL 16 DLE SP P p 1 SOH 17 DC1! 1 A Q a q STX 2 18 DC2 " 2 B R b r 3 ETX 19 DC3 # 3 C S c s 4 EOT 20 DC4 $ 4 D T d t 5 ENQ 21 NAK % 5 E U e u 6 ACK 22 SYN & 6 F V f v BEL 7 23 ETB 7 G W g w BS 8 24 CAN ( 8 H X h x HT EM ) 9 I Y i y 1 ASCII LF SUB * : J Z j z VT ESC + ; K [ k { FF CR SO SI FS GS RS US , -. / < = >? L M N O \ ] ^ _ l m n o } ~ DEL () 32 SP 5 (Space) 1 SP ( ) ISO 646-US ISO 646-US ISO 646-JP JIS X 0201 Roman ISO 646-US (ASCII ) ISO 646-JP (JIS X 0201 Roman) 2 ISO 646-JP (JIS X 0201 Roman) 92 ( ) 126 ( ) \ ( ) ~ ( ) C NUL Null C 6 7 \0 C BEL Bell \a BS Backspace \b ( )1 HT Horizontal Tabulation \t ( ) ( 8 ) LF Line feed 8 \n C 1 VT Vertical Tabulation \v ( ) FF Form Feed 9 \f CR Carriage Return \r

9 printf 1 byte ASCII 1 byte (= 8 bit) printf printf %c ( ) printf ascii.c #include <stdio.h> 2 #include <stdlib.h> 3 4 int main() 5 { 6 int c; 7 8 for (c = 32; c <= 126; c++) { 9 printf("%3d=%c", c, c); 10 if (c%8 == 7 c == 126) 11 printf("\n"); 12 else 13 printf(" "); 14 } return EXIT_SUCCESS; 17 } ascii.c 8 printf %3d %c c %3d c ( ) %c c 1 s1542h017% cc ascii.c -o ascii s1542h017%./ascii 32= 33=! 34=" 35=# 36=$ 37=% 38=& 39= 40=( 41=) 42=* 43=+ 44=, 45=- 46=. 47=/ 48=0 49=1 50=2 51=3 52=4 53=5 54=6 55=7 56=8 57=9 58=: 59=; 60=< 61== 62=> 63=? 65=A 66=B 67=C 68=D 69=E 70=F 71=G 72=H 73=I 74=J 75=K 76=L 77=M 78=N 79=O 80=P 81=Q 82=R 83=S 84=T 85=U 86=V 87=W 88=X 89=Y 90=Z 91=[ 92=\ 93=] 94=^ 95=_ 96= 97=a 98=b 99=c 100=d 101=e 102=f 103=g 104=h 105=i 106=j 107=k 108=l 109=m 110=n 111=o 112=p 113=q 114=r 115=s 116=t 117=u 118=v 119=w 120=x 121=y 122=z 123={ 124= 125=} 126=~ s1542h017% ascii.c SP ( ) 4 New Line 5 8 9

10 C ( ) (int ) C A A (ASCII 65) C \n ( ) \ \ ( ) \\ ASCII ascii.c ascii2.c 1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main() 5 { 6 int c; 7 8 for (c = ; c <= ~ ; c++) 9 printf("%3d=%c%c", c, c, (c%8 == 7 c == ~ )? \n : ); 10 return EXIT_SUCCESS; 11 } char ASCII C char char 1 byte (= 8 bit) char 11 char int char char int C char char ch; 1 char 1 char ( ) int ascii.c ascii2.c 5 int c; char c; c ( ) char 10 C 11 C

11 printf 2 char char int int printf scanf 1 byte scanf 1 byte %c ( ) (scanf ) %c char (char ) chars.c 9 scanf char ( ) 5 char c; int c; 1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main() 5 { 6 char c; 7 8 printf(" = "); 9 do { 10 scanf("%c", &c); 11 printf("%3d=%c\n", c, c); 12 } while (c!= \n ); 13 return EXIT_SUCCESS; 14 } chars.c s1542h017%./chars = This is a test. 84=T 104=h 105=i 115=s 32= 105=i 115=s 32= 97=a 32= 116=t 101=e 115=s 116=t 46=. 10= chars.c s1542h017% chars.c (Enter) 8 11

12 chars 7 printf = (Enter) 9 scanf This is a test. (Enter) T 16 scanf T c main scanf scanf %d %f %lf () ( ) %c 1 %c " %c" scanf ( ) B. prog7-1.c quicksort 1 int quicksort(record *d[], int h, int t) 2 { 3 Record *tmp; 4 int r; 5 int i, j, count = 0; 6 7 if (h >= t) 8 return count; 9 10 r = d[(h+t)/2]->score; 11 i = h; 12 j = t; 13 while (1) { 14 count++; 15 while (d[i]->score < r) { 16 i++; 17 count++; 18 } 19 count++; 20 while (r < d[j]->score) { 21 j--; 22 count++; 23 } 24 if (j <= i) 25 break; 26 tmp = d[i]; 27 d[i++] = d[j]; 28 d[j--] = tmp; 29 } 30 count += quicksort(d, h, i-1); 31 count += quicksort(d, j+1, t); 32 return count; 33 } 8 12