c program design supplements 主講人:虞台文. content who takes the return value of main() ?

14
C Program Design Supplements 主主主 主主主

Upload: brent-atkinson

Post on 16-Dec-2015

225 views

Category:

Documents


0 download

TRANSCRIPT

C Program DesignSupplements

主講人:虞台文

Content Who takes the return value of main()?

C Program DesignSupplements

Who takes the return value of main()?

main()

int main( void ){ /* declaration of variable */

/* operations */

return result; /* who takes the result? */}

int main( void ){ /* declaration of variable */

/* operations */

return result; /* who takes the result? */}

Example: Unix grep

Ah Love! could you and I with Fate conspireTo grasp this sorry Scheme of Things entire,Would not we shatter it to bits -- and thenRe-mould it nearer to the Heart's Desire!

Ah Love! could you and I with Fate conspireTo grasp this sorry Scheme of Things entire,Would not we shatter it to bits -- and thenRe-mould it nearer to the Heart's Desire!

grepExample.txt

Ah Love! could you and I with Fate conspireTo grasp this sorry Scheme of Things entire,Would not we shatter it to bits -- and thenRe-mould it nearer to the Heart's Desire!

Ah Love! could you and I with Fate conspireTo grasp this sorry Scheme of Things entire,Would not we shatter it to bits -- and thenRe-mould it nearer to the Heart's Desire!

grep "ould" grepExample.txt

Ah Love! could you and I with Fate conspireWould not we shatter it to bits -- and thenRe-mould it nearer to the Heart's Desire!

Ah Love! could you and I with Fate conspireWould not we shatter it to bits -- and thenRe-mould it nearer to the Heart's Desire!

Example: Unix grep

Ah Love! could you and I with Fate conspireTo grasp this sorry Scheme of Things entire,Would not we shatter it to bits -- and thenRe-mould it nearer to the Heart's Desire!

Ah Love! could you and I with Fate conspireTo grasp this sorry Scheme of Things entire,Would not we shatter it to bits -- and thenRe-mould it nearer to the Heart's Desire!

grepExample.txt

Ah Love! could you and I with Fate conspireTo grasp this sorry Scheme of Things entire,Would not we shatter it to bits -- and thenRe-mould it nearer to the Heart's Desire!

Ah Love! could you and I with Fate conspireTo grasp this sorry Scheme of Things entire,Would not we shatter it to bits -- and thenRe-mould it nearer to the Heart's Desire!

grep "ould" grepExample.txt

Ah Love! could you and I with Fate conspireWould not we shatter it to bits -- and thenRe-mould it nearer to the Heart's Desire!

Ah Love! could you and I with Fate conspireWould not we shatter it to bits -- and thenRe-mould it nearer to the Heart's Desire!

Pseudo Code The Idea to Fulfill the Task

count = 0;

while (there's another line)

if (the line contains the pattern)

print it

count++;

return count;

Pseudo Code The Idea to Fulfill the Task

count = 0;

while (there's another line)

if (the line contains the pattern)

print it

count++;

return count;

How?Define functions to mind the

detail.

How?Define functions to mind the

detail.

Pseudo Code The Idea to Fulfill the Task

count = 0;

while (there's another line)

if (the line contains the pattern)

print it

count++;

return count;

Pseudo Code The Idea to Fulfill the Task

int getline(char line[], int max)

int strindex(char source[], char searchfor[])

Example: MyGrep#include <stdio.h>#define MAXLINE 1000 /* maximum input line length */

int getline(char line[], int max);int strindex(char source[], char searchfor[]);

char pattern[] = "ould"; /* pattern to search for */

/* find all lines matching pattern */int main( void ){ char line[MAXLINE]; int count = 0;

while (getline(line, MAXLINE) > 0) if (strindex(line, pattern) >= 0) { printf("%s", line); count++; } return count;}. . . . . . . . . . . . . . . . . .

#include <stdio.h>#define MAXLINE 1000 /* maximum input line length */

int getline(char line[], int max);int strindex(char source[], char searchfor[]);

char pattern[] = "ould"; /* pattern to search for */

/* find all lines matching pattern */int main( void ){ char line[MAXLINE]; int count = 0;

while (getline(line, MAXLINE) > 0) if (strindex(line, pattern) >= 0) { printf("%s", line); count++; } return count;}. . . . . . . . . . . . . . . . . .

count = 0;

while (there's another line)

if (the line contains the pattern)

print it

count++;

return count;

count = 0;

while (there's another line)

if (the line contains the pattern)

print it

count++;

return count;

return information to OS

Example: MyGrep#include <stdio.h>#define MAXLINE 1000 /* maximum input line length */

int getline(char line[], int max);int strindex(char source[], char searchfor[]);

char pattern[] = "ould"; /* pattern to search for */

/* find all lines matching pattern */main(){ char line[MAXLINE]; int count = 0;

while (getline(line, MAXLINE) > 0) if (strindex(line, pattern) >= 0) { printf("%s", line); found++; } return count;}. . . . . . . . . . . . . . . . . .

#include <stdio.h>#define MAXLINE 1000 /* maximum input line length */

int getline(char line[], int max);int strindex(char source[], char searchfor[]);

char pattern[] = "ould"; /* pattern to search for */

/* find all lines matching pattern */main(){ char line[MAXLINE]; int count = 0;

while (getline(line, MAXLINE) > 0) if (strindex(line, pattern) >= 0) { printf("%s", line); found++; } return count;}. . . . . . . . . . . . . . . . . .

while (there's another line)

if (the line contains the pattern)

print it

forward references

return information to OS

Batch Files

Example: MyGrep

Ah Love! could you and I with Fate conspireTo grasp this sorry Scheme of Things entire,Would not we shatter it to bits -- and thenRe-mould it nearer to the Heart's Desire!

Ah Love! could you and I with Fate conspireTo grasp this sorry Scheme of Things entire,Would not we shatter it to bits -- and thenRe-mould it nearer to the Heart's Desire!

練習:1. 完成以上程式,並測試之。2. 撰寫一程式讀取使用者從標準輸入裝置中輸入之字元直到發生 EOF止,該程式會將所讀取到的字元總數回傳給作業系統後結束執行。

3. 撰寫一批次檔驗證你能正確收到上題程式之回傳資料,並做適當顯示。