c program design supplements 主講人:虞台文. content who takes the return value of main() ?
TRANSCRIPT
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
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!