Web view#include int main(void) {printf("%d\n", 2013); printf("%g\n", 3.14159); printf("%c\n",...

54
[103] 001 m#math practice by www.msharpmath.com [103] 001 revised on 2015.06.02 m#math The Simple is the Best 강강 1 main 강강 #include <stdio.h> int main(void) { // user return 0; } 강강 1.1 #include <stdio.h> int main(void) { printf("%d\n", 2013); printf("%g\n", 3.14159); printf("%c\n", 'y'); printf("%s\n", "interest rate is 4.5\% here"); return 0; } 강강 1.2 #include <stdio.h> 1

Transcript of Web view#include int main(void) {printf("%d\n", 2013); printf("%g\n", 3.14159); printf("%c\n",...

Page 1: Web view#include  int main(void) {printf("%d\n", 2013); printf("%g\n", 3.14159); printf("%c\n", 'y'); printf("%s\n", "interest rate is 4.5\% here");

[103] 001 m#math practice by www.msharpmath.com

[103] 001 revised on 2015.06.02 m#math

The Simple is the Best

강의 1

main 함수 #include <stdio.h>int main(void){ // user return 0;}

연습 1.1

#include <stdio.h>

int main(void){

printf("%d\n", 2013);printf("%g\n", 3.14159);printf("%c\n", 'y');printf("%s\n", "interest rate is 4.5\% here");

return 0;}

연습 1.2 #include <stdio.h> int main(void){

printf("%d\n", 's'-'a'+1 );printf("%d\n", 'x'-'c'-1 );

return 0;

1

Page 2: Web view#include  int main(void) {printf("%d\n", 2013); printf("%g\n", 3.14159); printf("%c\n", 'y'); printf("%s\n", "interest rate is 4.5\% here");

[103] 001 m#math practice by www.msharpmath.com

}

연습 1.3 #include <stdio.h> int main(void){

int n,sum = 0;

n = 100;while( n <= 200 ) {

sum += n;++n;

} printf("sum = %d\n", sum );

return 0;}

연습 1.4 #include <stdio.h> int main(void){

int n;

while(1) {printf("input : "); scanf("%d", &n);printf("%d\n", n);

}return 0;

}

연습 1.5 #include <stdio.h> int main(void){

int n,count = 0, sum = 0;

while(1) {printf("input : "); scanf("%d", &n);

if( n == 0 ) break;printf("%d-th element = %d\n", ++count,n);

2

Page 3: Web view#include  int main(void) {printf("%d\n", 2013); printf("%g\n", 3.14159); printf("%c\n", 'y'); printf("%s\n", "interest rate is 4.5\% here");

[103] 001 m#math practice by www.msharpmath.com

sum += n;}printf("count = %d\n", count);printf("mean = %g\n", sum/(count+0.));

return 0;}

연습 1.6 정수 n 을 하나 입력하고 소수(prime number) 인지 아닌지를 판단하라.

참고 : 소수가 아니라면 sqrt(n) 보다 작은 정수로 나누어진다.

const 한정자

#include <stdio.h>

int main(void){ const double pi = 3.14159; // mathematical constant

printf("%g %g \n", pi, 2*pi );

// pi = 3.; // error C2166: l-value specifies const object

return 0;}

#define 상수

#include <stdio.h>

int main(void){ #define PI 3.14159 // mathematical constant, no semi-colon

printf("%g %g \n", PI, 2*PI);

return 0;}

강의 2

3

Page 4: Web view#include  int main(void) {printf("%d\n", 2013); printf("%g\n", 3.14159); printf("%c\n", 'y'); printf("%s\n", "interest rate is 4.5\% here");

[103] 001 m#math practice by www.msharpmath.com

연습 2.1 #include <stdio.h> int main(void){

int i,n,count = 0, sum = 0;int nput[1000];

while(1) {printf("input : "); scanf("%d", &n);

if( n == 0 ) break;nput[count++] = n;

} for(i = 0; i < count; i++) sum += nput[i];printf("count = %d\n", count);printf("sum = %d\n", sum);printf("mean = %g\n", sum/(count+0.));

return 0;}

연습 2.2

#include <stdio.h> int medi(int a, int b, int c);

int main(void){

printf("%d\n", medi(12,9,31) );

return 0;}

int medi(int a, int b, int c){

int tmp;

if( a > b ) { tmp = a; a = b; b = tmp; // swap a and b

}

if( b < c ) return b;else {

tmp = b; b = c; c = tmp;

4

Page 5: Web view#include  int main(void) {printf("%d\n", 2013); printf("%g\n", 3.14159); printf("%c\n", 'y'); printf("%s\n", "interest rate is 4.5\% here");

[103] 001 m#math practice by www.msharpmath.com

if( a < b ) return b;else return a;

}}

연습 2.3

#include <stdio.h> int minfind( int nsize, int *iarray );

int main(void){

int nums[7] = { 3,5,2,9,-8,6,1 };

printf("min = %d\n", minfind(7,nums) );

return 0;}

int minfind( int nsize, int *iarray ){

int i,imin;

imin = iarray[0];

for(i = 1; i < nsize; i++) {if( imin > iarray[i] ) imin = iarray[i];

}return imin;

}

연습 2.4

#include <stdio.h> int fact( int n );

int main(void){

printf("%d\n", fact(5) );

return 0;}

int fact( int n ){

if( n == 1 ) return 1;else return n * fact(n-1);

}

5

Page 6: Web view#include  int main(void) {printf("%d\n", 2013); printf("%g\n", 3.14159); printf("%c\n", 'y'); printf("%s\n", "interest rate is 4.5\% here");

[103] 001 m#math practice by www.msharpmath.com

강의 3

강의 4 함수

함수의 선언

#include <stdio.h> double area(double a, double b) { return a*b; }

int main(void){

printf("%g \n", area(3.0, 4.0));return 0;

}

지역 변수

#include <stdio.h> double area(double a, double b) {

double s;

s = a*b;

return s; }

int main(void){

printf("%g \n", area(3.0, 4.0));return 0;

}

block 변수

#include <stdio.h>

6

Page 7: Web view#include  int main(void) {printf("%d\n", 2013); printf("%g\n", 3.14159); printf("%c\n", 'y'); printf("%s\n", "interest rate is 4.5\% here");

[103] 001 m#math practice by www.msharpmath.com

int main(void){

int i = 3; {

int i = 5; printf("inside i = %d \n", i);

} printf("outside i = %d \n", i); return 0;

}

외부 변수 (external variable)

#include <stdio.h>

int common = 5;

int main(void){

extern int common; printf("common = %d \n", common); return 0;

}

정적 변수 (static variable)

#include <stdio.h>

int showstatic(int n){

static int show; if( n == 0 ) printf("\n show = %d", show);show = n*n;return n;

}

int main(void){

showstatic(5); // show = 25 nowshowstatic(0); // show in memory is printed return 0;

}

call by value 의 특성

7

Page 8: Web view#include  int main(void) {printf("%d\n", 2013); printf("%g\n", 3.14159); printf("%c\n", 'y'); printf("%s\n", "interest rate is 4.5\% here");

[103] 001 m#math practice by www.msharpmath.com

#include <stdio.h> void swap(double a, double b) { double tmp; tmp = a; a = b; b = tmp; }

int main(void){

double x,y; x = 3.; y = 4.0; printf("%g \t %g\n", x,y); swap(x,y); printf("%g \t %g\n", x,y);

return 0;}

초기화

#include <stdio.h> int main(void){

int n = 3;double x = 3.14, y = 2.718;char c = 'a';

int days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

char str[] = "special way to initialize";

return 0;}

prototype

#include <stdio.h>

double area(double, double);

int main(void){ printf("area = %g \n", area(3,4)); return 0;}

double area(double a,double b) { return a*b; }

8

Page 9: Web view#include  int main(void) {printf("%d\n", 2013); printf("%g\n", 3.14159); printf("%c\n", 'y'); printf("%s\n", "interest rate is 4.5\% here");

[103] 001 m#math practice by www.msharpmath.com

header

#include <stdio.h>#include <ctype.h>

int main(void){

char c = '3';

if( c >= '0' && c <= '9' ) printf("digit = %c \n", c);

if( isdigit(c) ) printf("digit = %c \n", c);

return 0;}

recursion

#include <stdio.h>

int factorial(int n) { if ( n == 1 ) return 1; return n * factorial(n-1); }

int main(void){

printf("%d \n", factorial(5));

return 0;}

macro 치환

#include <stdio.h>

#define square(x) ( (x)*(x) )#define square2(x) x*x

int main(void){

printf("%d \n", square(3+4)); printf("%d \n", square2(3+4));

return 0;}

macro 치환 (# 명령어)

9

Page 10: Web view#include  int main(void) {printf("%d\n", 2013); printf("%g\n", 3.14159); printf("%c\n", 'y'); printf("%s\n", "interest rate is 4.5\% here");

[103] 001 m#math practice by www.msharpmath.com

#include <stdio.h>

#define dprint(expr) printf(#expr " = %g \n", expr)

int main(void){

dprint(3./4.);

return 0;}

macro 치환 (## 명령어)

#include <stdio.h>

#define paste(front, back) front ## back

int main(void){

int name1 = 3; printf("%d \n", name1);paste(name,1) = 5; printf("%d \n", name1);

return 0;}

강의 5 포인터

포인터 #include <stdio.h> int main(void){

int moon = 29;int *pmoon;

pmoon = &moon; printf("%d \t %d \n", moon, *pmoon);*pmoon = 31; printf("%d \t %d \n", moon, *pmoon);

return 0;}

포인터에 의한 call by reference #include <stdio.h>

10

Page 11: Web view#include  int main(void) {printf("%d\n", 2013); printf("%g\n", 3.14159); printf("%c\n", 'y'); printf("%s\n", "interest rate is 4.5\% here");

[103] 001 m#math practice by www.msharpmath.com

void swap(int *pmoon, int *pstar) { int tmp = *pmoon; *pmoon = *pstar; *pstar = tmp;}

int main(void){

int moon = 29, star = 365;

printf("%d \t %d \n", moon, star);swap(&moon,&star); printf("%d \t %d \n", moon, star);

return 0;}

포인터 연산 #include <stdio.h>

int main(void){

int a = 3, b = 4;int *p, *q;

p = &a;q = &b;

printf("pointer p : %p\n", p);printf("pointer q : %p\n", q);

printf("p-q : %d\n", p-q);printf("p>q : %d\n", p>q);printf("p>=q : %d\n", p>=q);printf("p<q : %d\n", p<q);printf("p<=q : %d\n", p<=q);printf("p==q : %d\n", p==q);printf("p!=q : %d\n", p!=q);return 0;

}

add3 #include <stdio.h> int add3(int *ptr);

int main(void){

11

Page 12: Web view#include  int main(void) {printf("%d\n", 2013); printf("%g\n", 3.14159); printf("%c\n", 'y'); printf("%s\n", "interest rate is 4.5\% here");

[103] 001 m#math practice by www.msharpmath.com

int score[7] = { 3,2,6,7,5,4,2 };

printf("%d\n", add3(score+2));printf("%d\n", add3(score+3));printf("%d\n", add3(score+4));

return 0;}

int add3(int *ptr){

//return *ptr + *(ptr-1) + *(ptr+1);return ptr[0] + ptr[-1] + ptr[1];

}

강의 6 포인터와 문자열

문자열 변수, 문자열 상수

#include <stdio.h>

int main(void){

char msg[] = "Rewrite here" ; // 문자열 변수

const char book[] = "Frozen String" ; // 문자열 상수

printf("%d\n", strlen(msg)); printf("%d\n", strlen(book));

//book[0] = '1'; // error C2166: l-value specifies const object

return 0;}

호출 strlen #include <stdio.h> int main(void){

12

Page 13: Web view#include  int main(void) {printf("%d\n", 2013); printf("%g\n", 3.14159); printf("%c\n", 'y'); printf("%s\n", "interest rate is 4.5\% here");

[103] 001 m#math practice by www.msharpmath.com

printf("%d\n", strlen("hello"));

return 0;}

strlen #include <stdio.h> int strlen(char *chr) {

char *p;

p = chr;while( *p ) { ++p; }

return p-chr;}

int main(void){

printf("%d\n", strlen("hello"));

return 0;}

strcpy #include <stdio.h> void strcpy(char *s, char *t) {

while( *s++ = *t++ ) ;}

int main(void){

char to[100] = "";char from[] = "string copy";

printf("%s\n", from); strcpy(to,from);printf("%s\n", to);

return 0;}

strcmp #include <stdio.h>

13

Page 14: Web view#include  int main(void) {printf("%d\n", 2013); printf("%g\n", 3.14159); printf("%c\n", 'y'); printf("%s\n", "interest rate is 4.5\% here");

[103] 001 m#math practice by www.msharpmath.com

int strcmp(char *s, char *t) {

for( ; *s == *t; s++, t++) if( *s == '\0' ) return 0;

return *s - *t ;}

int main(void){

printf("%d\n", strcmp("abc", "abx") );printf("%d\n", strcmp("abc", "abc") );printf("%d\n", strcmp("abc", "aba") );

return 0;

}

strcat #include <stdio.h> void strcat(char *s, char *t) {

s += strlen(s); // end of s

while( *s++ = *t++ ) ;}

int main(void){

char s[100] = "abc";

strcat(s, "xyz");printf("%s\n", s );

return 0;}

atoi #include <stdio.h> int atoi(char *str) { char *p = str; int n = 0,sign;

while( *p == ' ' ) { ++p; } // skip space sign = (*p == '-') ? -1 : 1 ;

if( *p == '+' || *p == '-' ) ++p; // skip sign

14

Page 15: Web view#include  int main(void) {printf("%d\n", 2013); printf("%g\n", 3.14159); printf("%c\n", 'y'); printf("%s\n", "interest rate is 4.5\% here");

[103] 001 m#math practice by www.msharpmath.com

while( *p ) { n = 10*n + (*p - '0'); ++p; }

return sign*n;}

int main(void){

printf("%d \n", atoi(" +365") );

return 0;}

atof #include <stdio.h>

double atof(char *p) { double sign,val,power;

while( *p == ' ' ) ++p; // skip space sign = (*p == '-') ? -1. : 1. ; // find sign

if( *p == '+' || *p == '-' ) ++p; // skip sign

for(val = 0.; *p>='0' && *p<='9'; p++) val = 10.*val + (*p - '0');

if( *p == '.' ) p++;

for(power = 1.; *p>='0' && *p<='9'; p++) {val = 10.*val + (*p - '0'); power *= 10.;

} return sign*val/power;}

int main(void){

printf("%g \n", atof("-3.14159") );

return 0;}

strrev #include <stdio.h>

15

Page 16: Web view#include  int main(void) {printf("%d\n", 2013); printf("%g\n", 3.14159); printf("%c\n", 'y'); printf("%s\n", "interest rate is 4.5\% here");

[103] 001 m#math practice by www.msharpmath.com

void strrev(char *str) { char *p = str, *q, c;

q = p + strlen(p) -1;

while( p < q ) {c = *p; *p = *q; *q = c; // swap *p and *q++p; --q; // forward and

backward}

}

int main(void){

char msg[] = "abcde";

printf("%s \n", msg ); strrev(msg);printf("%s \n", msg );

return 0;}

itoa #include <stdio.h> void strrev(char *str) { char *p = str, *q, c;

q = p + strlen(p) -1;

while( p < q ) {c = *p; *p = *q; *q = c; // swap *p and *q++p; --q; // forward and

backward}

}

void itoa(int n,char *p) { int sign = n; char *str = p;

if( n < 0 ) n = -n; // make positive

do { *p = n % 10 + '0'; p++; // get next digit } while( (n/=10) > 0 ); // delete digit

16

Page 17: Web view#include  int main(void) {printf("%d\n", 2013); printf("%g\n", 3.14159); printf("%c\n", 'y'); printf("%s\n", "interest rate is 4.5\% here");

[103] 001 m#math practice by www.msharpmath.com

if( sign < 0 ) { *p = '-'; p++; } *p = '\0'; strrev(str);}

int main(void){

char s[100] = ""; itoa(-365, s); printf("%s : %d \n", s, strlen(s));

return 0;}

strtrim #include <stdio.h> int strtrim(char *str) { char *p;

for(p = str+strlen(str)-1; p >= str; p--) if( *p != ' ' && *p != '\t' && *p != '\n' ) break;

*(++p) = '\0'; return p-str; // string length }

int main(void){

int n; char str[] = "trim last ";

n = strtrim(str);printf(":%s: %d\n", str, n);

return 0;

}

강의 7

17

Page 18: Web view#include  int main(void) {printf("%d\n", 2013); printf("%g\n", 3.14159); printf("%c\n", 'y'); printf("%s\n", "interest rate is 4.5\% here");

[103] 001 m#math practice by www.msharpmath.com

문자열과 포인터의 차이

#include <stdio.h>

int main(void){ char str[] = "rose"; // array char *pstr = "rose"; // pointer

printf("%s\n", str); str[1] = 'u'; printf("%c\n", str[1]); printf("%s\n", str);

printf("%p %s\n", pstr, pstr);

pstr = "rose is rose";printf("%p %s\n", pstr, pstr); return 0;

}

문자열 배열

#include <stdio.h>

int main(void){

int i;char *pstr = "fruit";char *name[] = { "apple", "pear", "banana", "orange" };

printf("%s\n", pstr);for(i=0; i<4; i++) printf("%s\n", name[i]);

return 0;}

포인터 배열

#include <stdio.h>

18

Page 19: Web view#include  int main(void) {printf("%d\n", 2013); printf("%g\n", 3.14159); printf("%c\n", 'y'); printf("%s\n", "interest rate is 4.5\% here");

[103] 001 m#math practice by www.msharpmath.com

int main(void){

int i;char *pstr = "fruit";char *name[] = { "apple", "pear", "banana", "orange" };

printf("%s\n", pstr);for(i = 0; i < 4; i++) printf("%s\n", name[i]);

}

다차원 배열 #include <stdio.h>

int main(void){

int i;int M[3][3] = { {8,1,6}, {3,5,7}, {4,9,2} }; printf("%d %d %d\n", M[1][0],M[1][1],M[1][2]); printf("%d %d %d\n", *M[1],*(M[1]+1),*(M[1]+2));

printf("%d %d %d\n", *(*(M+1)), *(*(M+1)+1),*(*(M+1)+2) );

printf("%d ===== M\n", **M); printf("%p ===== M\n", M); printf("%p ===== M\n", *M); printf("%p\n%p\n%p\n", M[0], M[0]+1, M[0]+2);

printf("%p ===== M\n", M+1); printf("%p ===== M\n", *(M+1)); printf("%p\n%p\n%p\n", M[1], M[1]+1, M[1]+2);

printf("%p ===== M\n", M+2); printf("%p ===== M\n", *(M+2)); printf("%p\n%p\n%p\n", M[2], M[2]+1, M[2]+2); return 0;

}

1차원배열의 2차원 접근 #include <stdio.h>

int main(void){

int i,j;int A[] = { 0,1,2,3,4,5,6,7 };int *B[2][4];

19

Page 20: Web view#include  int main(void) {printf("%d\n", 2013); printf("%g\n", 3.14159); printf("%c\n", 'y'); printf("%s\n", "interest rate is 4.5\% here");

[103] 001 m#math practice by www.msharpmath.com

int *C[4][2];for(i = 0; i < 2; i++)for(j = 0; j < 4; j++) C[j][i] = B[i][j] = A+ 4*i+j;

for(i = 0; i < 2; i++) { printf("\n "); for(j = 0; j < 4; j++) printf("%d ", *(B[i][j]) );

} for(i = 0; i < 4; i++) {

printf("\n "); for(j = 0; j < 2; j++) printf("%d ", *(C[i][j]) );

}

return 0;}

함수 포인터 #include <stdio.h>#include <math.h>

const double pi = 3.1415926535;double sq(double (*f)(double x), double x) {

return f(x)*f(x);}

double my(double x) { return x*x*x; }

int main(void){

printf("%g \n", sq(cos, pi/6)); // cos(pi/6) = sqrt(3)/2printf("%g \n", sq(sin, pi/6)); // sin(pi/6) = 1/2printf("%g \n", sq(my, 2)); // my(2) = 8

return 0;}

강의 8

구조체의 선언 평면 위의 점에 대한 x좌표, y좌표를 가진 구조체를 선언하고, 멤버에 접근한다.

#include <stdio.h> struct point { // struct 은 C언어의 키워드(keyword)이다.

20

Page 21: Web view#include  int main(void) {printf("%d\n", 2013); printf("%g\n", 3.14159); printf("%c\n", 'y'); printf("%s\n", "interest rate is 4.5\% here");

[103] 001 m#math practice by www.msharpmath.com

double x; // x-coordinatedouble y; // y-coordinate

};

int main(void){

struct point P = {3,1}, Q = {1,2}, R; }

구조체의 멤버(Operations on structure) 평면 위의 점에 대한 x좌표, y좌표를 가진 구조체를 선언하고, 멤버에 접근한다.

#include <stdio.h> struct point { // struct 은 C언어의 키워드(keyword)이다.

double x; // x-coordinatedouble y; // y-coordinate

};

int main(void){

struct point P = {3,1}, Q = {1,2}, R;

P.x = 3; P.y = 1;Q.x = 1; Q.y = 2;

}

구조체의 포인터 (Operations on structure) #include <stdio.h> struct point { // struct 은 C언어의 키워드(keyword)이다.

double x; // x-coordinatedouble y; // y-coordinate

};

int main(void){

struct point P = {3,1}, Q = {1,2}, R;struct point *iP = &P, *iQ = &Q;

P.x = 3; P.y = 1;Q.x = 1; Q.y = 2;

printf("%g %g\n", iP->x, iP->y);printf("%g %g\n", (*iP).x, (*iP).y);printf("%g %g\n", P.x, P.y);

}

21

Page 22: Web view#include  int main(void) {printf("%d\n", 2013); printf("%g\n", 3.14159); printf("%c\n", 'y'); printf("%s\n", "interest rate is 4.5\% here");

[103] 001 m#math practice by www.msharpmath.com

구조체의 복사 (Operations on structure)

#include <stdio.h>

struct point { // struct 은 C언어의 키워드(keyword)이다.double x; // x-coordinatedouble y; // y-coordinate

};

int main(void){

struct point P, Q, R;

P.x = 3.; P.y = 1.; printf("%g %g\n", P.x, P.y);

R = P; printf("%g %g\n", R.x, R.y);

P.x = 7.; P.y = 9.; printf("%g %g\n", P.x, P.y);

printf("%g %g\n", R.x, R.y);

printf("%p \n%p \n%p \n", &P,&Q,&R);

return 0;}

구조체와 함수 1 #include <stdio.h> struct point { // struct 은 C언어의 키워드(keyword)이다.

double x; // x-coordinatedouble y; // y-coordinate

};

struct point makepoint(double x,double y) { struct point tmp; tmp.x = x; tmp.y = y; return tmp;}

int main(void){

22

Page 23: Web view#include  int main(void) {printf("%d\n", 2013); printf("%g\n", 3.14159); printf("%c\n", 'y'); printf("%s\n", "interest rate is 4.5\% here");

[103] 001 m#math practice by www.msharpmath.com

struct point P, Q, R;

P = makepoint(3,1);Q = makepoint(1,2); printf("%g %g\n", P.x, P.y);

}

구조체와 함수 2 #include <stdio.h> struct point { // struct 은 C언어의 키워드(keyword)이다.

double x; // x-coordinatedouble y; // y-coordinate

};

struct point makepoint(double x,double y) { struct point tmp; tmp.x = x; tmp.y = y; return tmp;}

struct point addpoint(struct point A, struct point B) { A.x += B.x; A.y += B.y; return A;}

int main(void){

struct point P, Q, R;

P = makepoint(3,1);Q = makepoint(1,2);R = addpoint(P,Q);

printf("%g %g\n", P.x, P.y);printf("%g %g\n", Q.x, Q.y);printf("%g %g\n", R.x, R.y);

return 0;}

형 정의 (typedef)

23

Page 24: Web view#include  int main(void) {printf("%d\n", 2013); printf("%g\n", 3.14159); printf("%c\n", 'y'); printf("%s\n", "interest rate is 4.5\% here");

[103] 001 m#math practice by www.msharpmath.com

#include <stdio.h> typedef struct point Point;

struct point { // struct 은 C언어의 키워드(keyword)이다.double x; // x-coordinatedouble y; // y-coordinate

};

Point makepoint(double x,double y) { Point tmp; tmp.x = x; tmp.y = y; return tmp;}

Point addpoint(Point A, Point B) { A.x += B.x; A.y += B.y; return A;}

int main(void){

Point P, Q, R;

P = makepoint(3,1);Q = makepoint(1,2);R = addpoint(P,Q);

printf("%g %g\n", P.x, P.y);printf("%g %g\n", Q.x, Q.y);printf("%g %g\n", R.x, R.y);

return 0;}

sizeof #include <stdio.h>

struct point { // struct 은 C언어의 키워드(keyword)이다.double x; // x-coordinatedouble y; // y-coordinate

};

int main(void){

double x;

24

Page 25: Web view#include  int main(void) {printf("%d\n", 2013); printf("%g\n", 3.14159); printf("%c\n", 'y'); printf("%s\n", "interest rate is 4.5\% here");

[103] 001 m#math practice by www.msharpmath.com

struct point A;

printf("%d \n", sizeof(char) ); printf("%d \n", sizeof(int) ); printf("%d \n", sizeof(x) ); printf("%d \n", sizeof(A) );

return 0;}

구조체의 구조체 #include <stdio.h>

struct point { // struct 은 C언어의 키워드(keyword)이다.double x; // x-coordinatedouble y; // y-coordinate

}; struct rect {

struct point pt1;struct point pt2;

};

int main(void){

struct rect box;

box.pt1.x = 2; box.pt1.y = 1; box.pt2.x = 5; box.pt2.y = 3;

printf("%p \n%p \n", &box.pt1.x, &box.pt1.y );

return 0;}

구조체의 배열 #include <stdio.h>

struct point { // struct 은 C언어의 키워드(keyword)이다.double x; // x-coordinatedouble y; // y-coordinate

};

int main(void){

struct point A[3] = { 0,0, 3,0, 1,2 };struct point B[3] = { {0}, {3,0}, {1,2} };

25

Page 26: Web view#include  int main(void) {printf("%d\n", 2013); printf("%g\n", 3.14159); printf("%c\n", 'y'); printf("%s\n", "interest rate is 4.5\% here");

[103] 001 m#math practice by www.msharpmath.com

printf("%g \n", A[2].x );printf("%g \n", A[2].y ); ;

printf("%g \n", B[0].x );printf("%g \n", B[0].y );

return 0;}

유니온

#include <stdio.h>

struct item {int utype;

union { int price; char maker[10]; }; };

int main(void){

struct item A;

A.utype = 1; A.price = 120;

if(A.utype == 1 ) printf("%d \n", A.price ); else if(A.utype == 2 ) printf("%s \n", A.maker );

A.utype = 2; strcpy(A.maker,"company");

if(A.utype == 1 ) printf("%d \n", A.price ); else if(A.utype == 2 ) printf("%s \n", A.maker ); printf("No way !!! %d \n", A.price ); // absord result

printf("%d \n", sizeof(struct item) );printf("%d \n", sizeof(A) );printf("%d \n", sizeof(A.price) );printf("%d \n", sizeof(A.maker) );

printf("%p address of utype \n", &A.utype );printf("%p address of price \n", &A.price );printf("%p address of maker \n", &A.maker );

return 0;}

26

Page 27: Web view#include  int main(void) {printf("%d\n", 2013); printf("%g\n", 3.14159); printf("%c\n", 'y'); printf("%s\n", "interest rate is 4.5\% here");

[103] 001 m#math practice by www.msharpmath.com

구조체의 응용예 #include <stdio.h> #include <math.h>

typedef struct Complex cplx;struct Complex { double x; double y;};cplx make(double x,double y) { cplx z = {x,y}; return z; } cplx mul(cplx u, cplx v) { return make(u.x*v.x-u.y*v.y, u.x*v.y+u.y*v.x ); }cplx div(cplx u, cplx v) { double d = 1./(v.x*v.x+v.y*v.y+1.e-60); return make( (u.x*v.x+u.y*v.y)*d, (-u.x*v.y+u.y*v.x)*d ); }

int main(void){

cplx u,v,z;

u = make(1,2); v = make(3,4);

z = mul(u,v); printf("%g %g\n", z.x, z.y);z = div(u,v); printf("%g %g\n", z.x, z.y);

return 0;}

구조체의 자기참조 #include <stdio.h> #include <ctype.h>

struct treenode { char *word; // point to the text int count; // number of occurrences struct treenode *left; // pointer for the left child struct treenode *right; // pointer for the right child};

struct treenode *treealloc(void) // make a treenode{ return (struct treenode *) malloc(sizeof(struct treenode)); // 다음 페이지 참조}

struct treenode *addtree (struct treenode *p, char *w) { int cond;

27

Page 28: Web view#include  int main(void) {printf("%d\n", 2013); printf("%g\n", 3.14159); printf("%c\n", 'y'); printf("%s\n", "interest rate is 4.5\% here");

[103] 001 m#math practice by www.msharpmath.com

if( p == NULL ) { // a new word has arrived p = treealloc(); // make a new node p->word = strdup(w); // string duplicate p->count = 1; p->left = p->right = NULL; } else if( (cond = strcmp(w, p->word)) == 0 ) p->count++; // repeat word else if( cond < 0 ) p->left = addtree(p->left,w); // less than into left subtree else p->right = addtree(p->right,w); // greater than into right subtree return p;}

void showtree(struct treenode *p){

if( p != NULL ) {showtree(p->left);printf("%4d %s\n", p->count, p->word);showtree(p->right);

}}

int main(void) {

int i;struct treenode *root;char *word[] = { "now", "is", "the", "time", "for", "all",

"good", "men", "to", "come", "to", "the", "aid", "of",

"their", "party" };

root = NULL;for(i = 0; i < 16; i++) {

root = addtree(root,word[i]);} showtree(root);

return 0;}

강의 9

행렬의 column-major 및 1차원처리

28

Page 29: Web view#include  int main(void) {printf("%d\n", 2013); printf("%g\n", 3.14159); printf("%c\n", 'y'); printf("%s\n", "interest rate is 4.5\% here");

[103] 001 m#math practice by www.msharpmath.com

#include <stdio.h> //-------------------------------------------------------------//void mulAB(double *C, double *A, double *B, double ***prow, double ***pcol, int n){ // C = A * B, n x n matrix

int i,j,k; double **a,**b;

b = pcol[0];for(j = 0; j < n; j++) {

a = prow[0];for(i = 0; i < n; i++) {

*C = 0; for(k = 0; k < n; k++) *C += *(a[k]+(A-

NULL)) * *(b[k]+(B-NULL)); ++C;a += n;

} b += n;

}}

//-------------------------------------------------------------//// **matrix@ m x n //-------------------------------------------------------------//double **matrix(int m,int n) { int i; double **A;

// pointers to rows A = (double **) malloc((size_t) (m*sizeof(double *))); if(A == NULL) { printf("allocation failure in matrix"); exit(1); } A[0] = (double *) malloc((size_t)(m*n*sizeof(double)));

for(i = 1; i < m; i++) A[i] = A[i-1]+n;

return A; // pointer to array of pointers to rows}//-------------------------------------------------------------//// ***rowmtrx@ m x n//-------------------------------------------------------------//double ***rowmtrx(int m,int n) {

int i,j,k; double ***A, *pnull = NULL;

// pointers to rowsA = (double ***) malloc((size_t) (m*sizeof(double **)));if(A == NULL) { printf("allocation failure in matrix");

exit(1); }

29

Page 30: Web view#include  int main(void) {printf("%d\n", 2013); printf("%g\n", 3.14159); printf("%c\n", 'y'); printf("%s\n", "interest rate is 4.5\% here");

[103] 001 m#math practice by www.msharpmath.com

A[0] = (double **) malloc((size_t)(m*n*sizeof(double*)));

for(i = 1; i < m; i++) A[i] = A[i-1]+n;

for(i = 0; i < m; i++) for(j = 0; j < n; j++) A[i][j] = pnull+i + m*j; // i,j

return A; // pointer to array of pointers to rows}//-------------------------------------------------------------//// ***colmtrx@ m x n//-------------------------------------------------------------//double ***colmtrx(int m,int n) {

int i,j,k; double ***A, *pnull = NULL;

// pointers to rowsA = (double ***) malloc((size_t) (n*sizeof(double **)));if(A == NULL) { printf("allocation failure in matrix");

exit(1); }

A[0] = (double **) malloc((size_t)(m*n*sizeof(double*)));

for(j = 1; j < n; j++) A[j] = A[j-1]+m;

for(i = 0; i < m; i++) for(j = 0; j < n; j++) A[j][i] = pnull+i + m*j; // j,i

return A; // pointer to array of pointers to rows}//-------------------------------------------------------------//// mulAtrB@ m x n//-------------------------------------------------------------//void mulAtrB(double*C, int m1,int n1, double*A, int m2,int n2, double*B){// m1 x n1 matrix A// m2 x n2 matrix B// C = A' * B, n1 x n2 matrix

int i,j,k; double *a,*b;

if( m1 != m2 ) { printf("mulAtrB : inner dimension mismatch\n"); exit(1); }

b = B; for(j = 0; j < n2; j++) {

a = A; for(i = 0; i < n1; i++) { *C = 0; for(k = 0; k < m1; k++) *C += a[k] * b[k];

a += m1;++C;

}

30

Page 31: Web view#include  int main(void) {printf("%d\n", 2013); printf("%g\n", 3.14159); printf("%c\n", 'y'); printf("%s\n", "interest rate is 4.5\% here");

[103] 001 m#math practice by www.msharpmath.com

b += m1; }}

//=======================================int main(void){

int i,j, m,n; double A[] = { 1,2,3,4, 5,6,7,8 }, B[] = { 2,3,4,5,

6,7,8,1 }; double X[] = { 1,2,3,4 }, Y[] = { 5,6,7,8 }, C[100]; double ***pcol,***prow;

m = 4; n = 2; prow = rowmtrx(m,n); // row-majorpcol = colmtrx(m,n); // column-major for(i=0;i<m;i++) for(j=0;j<n;j++) printf(" %g", *( (A-

NULL)+prow[i][j]) ); printf("\n");for(j=0;j<n;j++) for(i=0;i<m;i++) printf(" %g", *( (A-

NULL)+pcol[j][i]) ); printf("\n");

free(prow); prow = NULL;free(pcol); pcol = NULL;

m = n = 2;prow = rowmtrx(m,n); // row-majorpcol = colmtrx(m,n); // column-major

mulAB(C,X,Y, prow,pcol, 2);

for(i=0;i<m*n;i++) printf(" %g", C[i]); printf("\n");

for(i=0;i<m;i++) for(j=0;j<n;j++) printf(" %g", *( (C-NULL)+prow[i][j]) ); printf("\n");

for(j=0;j<n;j++) for(i=0;i<m;i++) printf(" %g", *( (C-NULL)+pcol[j][i]) ); printf("\n");

free(prow); prow = NULL;free(pcol); pcol = NULL;

mulAtrB(C,4,2,A, 4,2,B);for(i=0;i<4;i++) printf(" %g", C[i]); printf("\n");return 0;

}

강의 10

31

Page 32: Web view#include  int main(void) {printf("%d\n", 2013); printf("%g\n", 3.14159); printf("%c\n", 'y'); printf("%s\n", "interest rate is 4.5\% here");

[103] 001 m#math practice by www.msharpmath.com

getchar, putchar 키보드의 Caps Lock 을 대문자만을 치도록 설정하고 다음을 실행한다. 화면에서 마음대로 입력하고 리턴을 치면 소문자 변환을 출력하고, 다음의 입력을 대기한다.

#include <stdio.h> #include <ctype.h>

int main(void) {

int c;

while(1) {c = getchar(); if( c == EOF ) break;putchar(tolower(c));

}

return 0;}

getch, ungetch 의 작성 #include <stdio.h> #include <string.h>#include <ctype.h>#include <math.h>

#define BUFFSIZE 100

char buf[BUFFSIZE]; // buffer for ungetchint ibuf = 0; // next free position in buf

int getch(void) // get a (possibly pushed back) character{

return (ibuf > 0) ? buf[--ibuf] : getchar();}

void ungetch(int c) // push character back on input{

if( ibuf >= BUFFSIZE ) printf("ungetch: too many characters");

else buf[ibuf++] = c;}

int main(void) {

return 0;}

32

Page 33: Web view#include  int main(void) {printf("%d\n", 2013); printf("%g\n", 3.14159); printf("%c\n", 'y'); printf("%s\n", "interest rate is 4.5\% here");

[103] 001 m#math practice by www.msharpmath.com

스트링의 변환효과 #include <stdio.h>

int main(void) {

char str[] = "hello, world";

printf(":%s:\n", str); // :hello, world:printf(":%10s:\n", str); // :hello, world:printf(":%.10s:\n", str); // :hello, wor:printf(":%-10s:\n", str); // :hello, world:printf(":%.15s:\n", str); // :hello, world:printf(":%-15s:\n", str); // :hello, world :printf(":%15.10s:\n", str); // : hello, wor:printf(":%-15.10s:\n", str); // :hello, wor :

return 0;}

%.*s #include <stdio.h>

int main(void) {

char str[] = "hello, world";

int i;

for(i = 1; i <= 12; i++) printf(":%.*s:\n", i, str);

return 0;}

sprintf #include <stdio.h>

int main(void) {

char s[20] = "", str[]="hello, world"; sprintf(s, ":%s:\n", str); printf("%s",s);

// :hello, world:sprintf(s, ":%10s:\n", str); printf("%s",s);

// :hello, world:sprintf(s, ":%.10s:\n", str); printf("%s",s);

// :hello, wor:sprintf(s, ":%-10s:\n", str); printf("%s",s);

// :hello, world:

33

Page 34: Web view#include  int main(void) {printf("%d\n", 2013); printf("%g\n", 3.14159); printf("%c\n", 'y'); printf("%s\n", "interest rate is 4.5\% here");

[103] 001 m#math practice by www.msharpmath.com

sprintf(s, ":%.15s:\n", str); printf("%s",s); // :hello, world:

sprintf(s, ":%-15s:\n", str); printf("%s",s); // :hello, world :

sprintf(s, ":%15.10s:\n", str); printf("%s",s); // : hello, wor:

sprintf(s, ":%-15.10s:\n", str); printf("%s",s); // :hello, wor :

return 0;}

myprintf, 가변길이의 매개변수 리스트 #include <stdio.h> #include <stdarg.h>#include <math.h>

void myprintf(char *fmt, ...);

typedef struct Complex cplx;struct Complex {

double x;double y;

};

int main(void) {

cplx z = {3,-4};myprintf("test %d %f %s %z", 36, 3.14, "pi=3.14", z);return 0;

}

// myprintfvoid myprintf(char *fmt, ...){

cplx z;char *p,*sval; int ival;double dval;

va_list ap; va_start(ap, fmt);

for( p = fmt; *p; p++) {if( *p != '%' ) {

putchar(*p);continue;

}switch(*++p) {case 'd':

ival = va_arg(ap,int);printf("%d", ival);

34

Page 35: Web view#include  int main(void) {printf("%d\n", 2013); printf("%g\n", 3.14159); printf("%c\n", 'y'); printf("%s\n", "interest rate is 4.5\% here");

[103] 001 m#math practice by www.msharpmath.com

break;

case 'f':dval = va_arg(ap, double);printf("%f", dval);break;

case 's':for(sval = va_arg(ap,char *); *sval; sval+

+) putchar(*sval);

break;

case 'z': // personalized by usersz = va_arg(ap,cplx); printf(" :%g%ci%g:", z.x, (z.y>0.) ?

'+':'-', fabs(z.y));break;

default:putchar(*p);break;

} }va_end(ap);

}

scanf #include <stdio.h> int main(void) {

int day,month,year; char monthname[20];

scanf("%d %s %d", &day, monthname, &year); printf("%d %s %d\n", day, monthname, year);

scanf("%d/%d/%d", &month, &day, &year); printf("%d/%d/%d\n", month, day, year); return 0;

}

sscanf #include <stdio.h> int getline(char s[], int lim){

int c,i;

35

Page 36: Web view#include  int main(void) {printf("%d\n", 2013); printf("%g\n", 3.14159); printf("%c\n", 'y'); printf("%s\n", "interest rate is 4.5\% here");

[103] 001 m#math practice by www.msharpmath.com

for(i = 0; i < lim-1 && (c=getchar()) != EOF && c != '\n'; ++i) s[i] = c;

if( c == '\n' ) { s[i] = c; ++i; }s[i] = '\0';return i;

}

int main(void) {

int day,month,year; char monthname[20];char line[80];

while( getline(line,sizeof(line)) > 0 ) {if( sscanf(line, "%d %s %d", &day, monthname,

&year) == 3 ) {printf("valid: %s", line);printf("%d %s %d\n\n", day, monthname,

year);}else if(sscanf(line, "%d/%d/%d",

&month,&day,&year) == 3 ) {printf("valid: %s", line);printf("%d/%d/%d\n\n", month, day, year);

}else

printf("invalid: %s \n\n", line); }

return 0;}

FILE #include <stdio.h>

int main(void) {

FILE *rfile; char rname[] = "usrin.txt";FILE *wfile; char wname[] = "usrout.txt";

int a,b,c;

if( (rfile = fopen(rname, "r")) == NULL ) printf("%s does not exist", rname);

else {fscanf(rfile, "%d %d %d", &a,&b,&c);

wfile = fopen(wname, "w");fprintf(wfile, "%d\n", a);fclose(wfile);

36

Page 37: Web view#include  int main(void) {printf("%d\n", 2013); printf("%g\n", 3.14159); printf("%c\n", 'y'); printf("%s\n", "interest rate is 4.5\% here");

[103] 001 m#math practice by www.msharpmath.com

wfile = fopen(wname, "a");fprintf(wfile, "%d\n", b);fclose(wfile);

printf("%s is created", wname);}

return 0;}

getc, putc #include <stdio.h>

int main(void) {

FILE *rfile; char rname[] = "usrin.txt";FILE *wfile; char wname[] = "usrout.txt";

int a,b,c;

if( (rfile = fopen(rname, "r")) == NULL ) printf("%s does not exist", rname);

else {fscanf(rfile, "%d %d %d", &a,&b,&c);

wfile = fopen(wname, "w");fprintf(wfile, "%d\n", a);fclose(wfile);

wfile = fopen(wname, "a");fprintf(wfile, "%d\n", b);fclose(wfile);

printf("%s is created", wname);}

// file copy

rfile = fopen(wname, "r");wfile = fopen("getcputc.txt","w");while( (c = getc(rfile)) != EOF ) {

putc(c, wfile);}

return 0;}

출력파일의 생성

37

Page 38: Web view#include  int main(void) {printf("%d\n", 2013); printf("%g\n", 3.14159); printf("%c\n", 'y'); printf("%s\n", "interest rate is 4.5\% here");

[103] 001 m#math practice by www.msharpmath.com

#include <stdio.h> #include <string.h>#include <ctype.h>#include <math.h>

void Strrev(char *str) { char *p = str, *q, c;

q = p + strlen(p) -1;

while( p < q ) {c = *p; *p = *q; *q = c; // swap *p and *q++p; --q; // forward and

backward}

}

void itoa(int n,char *p) { int sign = n; char *str = p;

if( n < 0 ) n = -n; // make positive

do { *p = n % 10 + '0'; p++; // get next digit } while( (n/=10) > 0 ); // delete digit

if( sign < 0 ) { *p = '-'; p++; } *p = '\0'; Strrev(str);}

int main(void) {

FILE *wfile; char wname[20] = "",anum[20] = ""; int i;

for(i = 1; i < 10; i++) { strcpy(wname,"output");itoa(i,anum);strcat(wname,anum);printf("%s\n", wname);wfile = fopen(wname,"w");fprintf(wfile, "%d\n", i);

}return 0;

}

38

Page 39: Web view#include  int main(void) {printf("%d\n", 2013); printf("%g\n", 3.14159); printf("%c\n", 'y'); printf("%s\n", "interest rate is 4.5\% here");

[103] 001 m#math practice by www.msharpmath.com

입력파일의 생성

#include <stdio.h> #include <string.h>#include <ctype.h>

int main(void) {

FILE *rfile; char rname[20] = "";

int i;

strcpy(rname,"myin"); rfile = fopen(rname,"r");if( rfile == NULL ) {

printf("\n %s - file does not exist", rname);exit(0);

}

while(1) {fscanf(rfile, "%d\n", &i);printf("%d\n", i);if( i < 0 ) break;

}

return 0;}

system(char *s) #include <stdio.h> int main(void) {

system("date");return 0;

}

스택(stack) 구조

#include <stdio.h>

#define MAX 100 struct stack {

int starray[MAX];int top;

39

Page 40: Web view#include  int main(void) {printf("%d\n", 2013); printf("%g\n", 3.14159); printf("%c\n", 'y'); printf("%s\n", "interest rate is 4.5\% here");

[103] 001 m#math practice by www.msharpmath.com

}; struct stack *create(void){

struct stack *st;

st = malloc(sizeof(struct stack));if (st == NULL) {

printf("malloc error\n");return NULL;

}st->top = 0;return st;

}

int is_empty(struct stack *st){

return st->top == 0;} void push(struct stack *st, int x){

if (st->top == MAX) {printf("stack is full\n");return;

}st->starray[st->top++] = x;

}

int pop(struct stack *st){

if (is_empty(st)) {printf("stack is empty\n");return 0;

}return st->starray[--st->top];

}

int main(void){

struct stack *st;int x;

st = create();while (1) {

printf("enter x (0 to terminate): ");scanf("%d", &x);if (x == 0) break;push(st, x);

}while (!is_empty(st))

printf("%d ", pop(st));

40

Page 41: Web view#include  int main(void) {printf("%d\n", 2013); printf("%g\n", 3.14159); printf("%c\n", 'y'); printf("%s\n", "interest rate is 4.5\% here");

[103] 001 m#math practice by www.msharpmath.com

printf("\n");return 0;

}

Parser(파서)

#include <stdio.h> #include <string.h> #include <ctype.h>

#define STRMAX 999#define SYMMAX 100

#define BSIZE 128#define NONE -1#define EOS '\0'

#define NUM 256#define DIV 257#define MOD 258#define ID 259#define DONE 260

int lookahead = 0; int tokenval = NONE; int lineno = 1;

int lastchar = -1;int lastentry = 0;

char lexbuf[BSIZE]; char lexemes[STRMAX];

struct entry {char *lexptr;

41

Page 42: Web view#include  int main(void) {printf("%d\n", 2013); printf("%g\n", 3.14159); printf("%c\n", 'y'); printf("%s\n", "interest rate is 4.5\% here");

[103] 001 m#math practice by www.msharpmath.com

int token;}; struct entry symtable[SYMMAX];

struct entry keywords[] = {"div", DIV,"mod", MOD,0, 0

};

int lookup(char s[]);int insert(char s[], int tok);void init();void parse();void expr();void term();void factor();void match(int t); void emit(int t, int tval);int lexan();void error(char *);

void main(){

init();parse();

// exit(0); }

// sample test // 2 + 3 * 5 ;// 12 div 5 mod 2 ;

void init(){

struct entry *p;for (p = keywords; p->token; p++)

insert(p->lexptr, p->token);}

int lookup(char s[]) {

int p;for (p = lastentry; p > 0; p = p - 1)

if (strcmp(symtable[p].lexptr, s) == 0)return p;

return 0;}

42

Page 43: Web view#include  int main(void) {printf("%d\n", 2013); printf("%g\n", 3.14159); printf("%c\n", 'y'); printf("%s\n", "interest rate is 4.5\% here");

[103] 001 m#math practice by www.msharpmath.com

int insert(char s[], int tok) {

int len;len = strlen(s);if (lastentry + 1 >= SYMMAX)

error("symbol table full");if (lastchar + len +1 >= STRMAX)

error("lexemes array full");lastentry = lastentry + 1;symtable[lastentry].token = tok;symtable[lastentry].lexptr = &lexemes[lastchar + 1];lastchar = lastchar + len + 1;strcpy(symtable[lastentry].lexptr, s);return lastentry;

}

void parse(){

lookahead = lexan();while (lookahead != DONE){

expr(); match(';');}

}

void expr(){

int t;term();while (1)

switch (lookahead){case '+': case'-':

t = lookahead;match(lookahead); term(); emit(t, NONE);continue;

default:return;

}}

void term(){

int t;factor();while (1)

switch (lookahead){case '*': case '/': case DIV: case MOD:

t = lookahead;match(lookahead); factor(); emit(t,

NONE);continue;

43

Page 44: Web view#include  int main(void) {printf("%d\n", 2013); printf("%g\n", 3.14159); printf("%c\n", 'y'); printf("%s\n", "interest rate is 4.5\% here");

[103] 001 m#math practice by www.msharpmath.com

default:return;

}}

void factor(){

switch (lookahead){case '(':

match('('); expr(); match(')'); break;case NUM:

emit(NUM, tokenval); match(NUM); break;case ID:

emit(ID, tokenval); match(ID); break;default:

error("syntax error");}

}

void match(int t) {

if (lookahead == t)lookahead = lexan();

else error("syntax error");

}

void emit(int t, int tval) {

switch (t) {case '+': case '-': case '*': case '/':

printf("%c\n", t); break;case DIV:

printf("DIV\n"); break;case MOD:

printf("MOD\n"); break;case NUM:

printf("%d\n", tval); break;case ID:

printf("%s\n", symtable[tval].lexptr); break;default:

printf("token %d, tokenval %d\n", t, tval);}

}

int lexan(){

int t;while (1){

44

Page 45: Web view#include  int main(void) {printf("%d\n", 2013); printf("%g\n", 3.14159); printf("%c\n", 'y'); printf("%s\n", "interest rate is 4.5\% here");

[103] 001 m#math practice by www.msharpmath.com

t = getchar();if (t == ' ' || t == '\t')

;else if (t == '\n')

lineno = lineno + 1;else if (isdigit(t)){

ungetc(t, stdin);scanf("%d", &tokenval);return NUM;

}else if (isalpha(t)){

int p, b = 0;while (isalnum(t)){

lexbuf[b] = t;t = getchar();b = b + 1;if (b >= BSIZE)

error("compiler error");}lexbuf[b] = EOS;if (t != EOF)

ungetc(t, stdin);p = lookup(lexbuf);if (p == 0)

p = insert(lexbuf, ID);tokenval = p;return symtable[p].token;

}else if (t == EOF)

return DONE;else{

tokenval = NONE;return t;

}}

}

void error(char *m) {

//fprintf(stderr, "line %d: %s\n", lineno, m);printf("line %d: %s\n", lineno, m);//exit(1);

}

//---------------------------------------------------------------// end of file//---------------------------------------------------------------

45