Programming Language B Takako Nemoto (JAIST) 3 December Takako Nemoto (JAIST) 3 December 1 / 18
Today s topics 1 Function-like macro 2 Sorting 3 Enumeration 4 Recursive definition of functions 5 Input/output and characters Takako Nemoto (JAIST) 3 December 2 / 18
Function-like macro (8-2) #define sqr(x) ((x)*(x)) int n; double x; printf("input an integer: "); scanf("%d", &n); printf("the square of it is %d.\n", sqr(n)); printf("input a real number: "); scanf("%lf", &x); printf("the square of it is %f\n", sqr(x)); return 0; Takako Nemoto (JAIST) 3 December 3 / 18
Function-like macro: Expansion #define sqr(x) ((x)*(x)) int n; double x; printf("input an integer: "); scanf("%d", &n); printf("the square of it is %d.\n", ((n)*(n))); printf("input a real number: "); scanf("%lf", &x); printf("the square of it is %f\n", ((x)*(x))); return 0; Takako Nemoto (JAIST) 3 December 4 / 18
Some points of function-like macro How the following will be expanded? sqr(a++); It will be expanded as follows: ((a++)*(a++)); and so is the same as ((a)*(a+1)); a = a+1; What happen if we write #define sqr (x) ((x)*(x))? Then sqr is replaced by (x) ((x)*(x)). Do not insert a white space between sqr and (x). Takako Nemoto (JAIST) 3 December 5 / 18
Some points of function-like macro Assume we have #define sum_of(x,y) x+y. Then how the following will be expanded? We will have which is same as z = sum of(a,b) * sum of(c,d); z = a+b * c+d; z = a + (b * c) + d; Put brackets to each variables and the total macro. Takako Nemoto (JAIST) 3 December 6 / 18
Comma operator (8-4) #define put_newline(str) ( putchar('\n'), puts(str) ) Input an integer: 3 int n; It is not 0. printf("input an integer: "); scanf("%d", &n); if (n) put_newline("it is not 0."); else put_newline("it is 0."); return 0; Takako Nemoto (JAIST) 3 December 7 / 18
Comma operator To use comma operator, use brackets ( ). int a=1, b=2, c=3, i;// commas as separators. i = (a, b); // comma as an operator. b is assigned into i. i = a, b; // a is assigned into i. Same as (i = a), b; i = (a += 2, a + b); // after evaluating a = a+2, a+b is assigned into i. i = a += 2, a + b; // after evaluating a = a+2, a is assigned into i. i = a, b, c; // a is assigned into i i = (a, b, c); // c is assigned into i. int j=0; j++, j++; // j = j+1, j=j+1; a = c++, j++; // c++ is assigned into a. printf("a = %d\n", a); printf("b = %d\n", b); printf("c = %d\n", c); printf("i = %d\n", i); printf("j = %d\n", j); Takako Nemoto (JAIST) 3 December 8 / 18
Sorting: Bubble sorting (8-5) #define NUMBER 5 void bsort(int a[], int n){ int i, j; for (i = 0; i < n - 1; i++){ for (j = n - 1; j > i; j--){ if (a[j - 1] > a[j]){ int temp = a[j]; a[j] = a[j - 1]; a[j - 1] = temp; int i; int height[number]; for (i = 0; i < NUMBER; i++){ printf("no.%2d: ", i + 1); scanf("%d", &height[i]); bsort(height, NUMBER); puts("sorted in the ascending order:"); for (i = 0; i < NUMBER; i++) printf("no. %2d: %d\n", i+1, height[i]); return 0; Takako Nemoto (JAIST) 3 December 9 / 18
Enumeration type (8-6) enum animal {Dog, Cat, Monkey, Invalid; void dog(void){puts("bow-wow"); void cat(void){puts("meow"); void monkey(void){puts("oh oh ah ah"); enum animal select(void){ int tmp; do{printf("0..dog, 1..Cat, 2..Monkey, 3..Quit: "); scanf("%d", &tmp); while (tmp < Dog tmp >Invalid); return tmp; enum animal selected; do{switch (selected = select()){ case Dog : dog(); break; case Cat : cat(); break; case Monkey : monkey(); break; while (selected!= Invalid); return 0; enum animal is a declaration of enumeration, with the identifier animal. Dog, Cat, Monkey, Invalid are enumeration constants, which are assigned 0, 1, 2, 3, respectively. enum animal is a enumerated type. enum animal selected is a declaration that the variable selected has the type enum animal. Takako Nemoto (JAIST) 3 December 10 / 18
Enumeration type (8-6) enum animal {Dog, Cat, Monkey, Invalid; void dog(void){puts("bow-wow"); void cat(void){puts("meow"); void monkey(void){puts("oh oh ah ah"); while(selected = select()) means selected = select(); while(selected) enum animal select(void){ int tmp; do{printf("0..dog, 1..Cat, 2..Monkey, 3..Quit: "); scanf("%d", &tmp); while (tmp < Dog tmp >Invalid); return tmp; enum animal selected; do{switch (selected = select()){ case Dog : dog(); break; case Cat : cat(); break; case Monkey : monkey(); break; while (selected!= Invalid); return 0; Takako Nemoto (JAIST) 3 December 11 / 18
Recursive definition of functions int factorial (int n){ if (n > 0) return n * factorial(n-1); else return 1; int main (void){ int num; printf("input an integer: "); scanf("%d", &num); printf("its factorial is %d.", factorial(num)); return 0; Takako Nemoto (JAIST) 3 December 12 / 18
Input/output and characters int ch; while ((ch = getchar())!= EOF) putchar(ch); return 0; EOF means the special character for the end of the file. In the command prompt in Windows, it can be inputed by Ctrl+z (+Entr). In Linux or OS X, it can be input by Ctrl+d. Takako Nemoto (JAIST) 3 December 13 / 18
// Example 8-9 int i, ch; int cnt[10] = {0; while((ch = getchar())!= EOF){ switch (ch){ case '0' : cnt[0]++; break; case '1' : cnt[1]++; break; case '2' : cnt[2]++; break; case '3' : cnt[3]++; break; case '4' : cnt[4]++; break; case '5' : cnt[5]++; break; case '6' : cnt[6]++; break; case '7' : cnt[7]++; break; case '8' : cnt[8]++; break; case '9' : cnt[9]++; break; puts("the number of the occurrences:"); for (i = 0; i < 10; i++) printf("'%d': %d\n", i, cnt[i]); return 0; while ((ch = getchar())!= EOF) means getchar() is assigned into ch and if it is not EOF, the loop body is executed. '1' means the number assigned into 1 in the system. So this program counts and shows the number of the occurrence of each number. Takako Nemoto (JAIST) 3 December 14 / 18
The code for each number Recall the program to answer the code for each characters: char char1[10]; printf("input a character: "); scanf("%s", char1); printf("%d",char1[0]); Let v[i] be the assigned for the alphabet i in your system. Verify that v[i]-v[j]=i-j for i, j {0,..., 9. Takako Nemoto (JAIST) 3 December 15 / 18
A simplification (8-10) int i, ch; int cnt[10] = {0; while((ch = getchar())!= EOF){ if (ch >= '0' && ch <='9') cnt[ch - '0']++; puts("the number of occurrences:"); for (i = 0; i < 10; i++) printf("'%d': %d\n", i, cnt[i]); return 0; Takako Nemoto (JAIST) 3 December 16 / 18
Input and output with files Assume that we get an executable file 8-10.exe. Then we can apply it to a file. 8-10.exe < inputfile > outputfile.txt Takako Nemoto (JAIST) 3 December 17 / 18
Today s homework 1 Do Exercise 8-4 ( 8-4) in p.222 (in the latest edition). 2 Do Exercise 8-7 ( 8-7) in p.227 (in the latest edition). 3 Make a program in C to count the number of lines of the input. Apply it to the file https://www.gutenberg.org/files/215/215-0.txt and report also the result as a text file. As usual, send the programs and the text file as attached files to me via e-mail, with the title Homework Lecture 9 by the next lecture. Please make sure to include your name and student ID number. Takako Nemoto (JAIST) 3 December 18 / 18