Presentation is loading. Please wait.

Presentation is loading. Please wait.

C Homework Write mypaste.c and mycomm.c – mypaste is like paste & mycomm is like comm – Both take two files as arguments – Paste reads a line from each.

Similar presentations


Presentation on theme: "C Homework Write mypaste.c and mycomm.c – mypaste is like paste & mycomm is like comm – Both take two files as arguments – Paste reads a line from each."— Presentation transcript:

1 C Homework Write mypaste.c and mycomm.c – mypaste is like paste & mycomm is like comm – Both take two files as arguments – Paste reads a line from each file and outputs them to stdout on the same line – Comm compares the two lines with strcmp and outputs the one that comes first in alphabetical order (with the appropriate number of tabs)strcmp

2 hello.c #include main() { printf("hello, world\n"); } New Concepts – C is a compiled Interpreted languages: python, javascript, R, lisp – cc –o hello hello.c –./hello

3 Make make clean make all Makefile

4 Input/Output: mycat.c (My Cat) #include main() { int c; while((c = getchar()) != EOF) putchar(c); } New Concepts – Stdio: getchar/putchar – Variable declarations

5 mywc.c (My Word Count) #include #include main() { int c, bytes=0, words=0, lines=0; int prev_state=0; while((c = getchar()) != EOF) { bytes++; if(c == '\n') lines++; int current_state = isalnum(c) || ispunct(c); if(!prev_state && current_state) words++; prev_state = current_state; } printf("%d\t%d\t%d\n", lines, words, bytes); } New Concepts – ++ – ‘\n’ – printf – isalnum, ispunct – man isalnum INOUT words++

6 mywc2.c: ac & av #include #include void wc_fd(FILE *fd, char *filename) { int c, bytes=0, words=0, lines=0; int prev_state=0; while((c = getc(fd)) != EOF) { bytes++; if(c == '\n') lines++; int current_state = isalnum(c) || ispunct(c); if(!prev_state && current_state) words++; prev_state = current_state; } printf("%d\t%d\t%d\t%s\n", lines, words, bytes, filename); if(fd && (fd != stdin)) fclose(fd); } void wc(char *filename) { if(strcmp(filename, "-") == 0) { wc_fd(stdin, filename); return; } FILE *fd = fopen(filename, "r"); if(!fd) printf("NA\tNA\tNA\t%s\n", filename); else wc_fd(fd, filename); } int main(int ac, char **av) { int i; for(i=0;i<ac;i++) fprintf(stderr, "av[%d] = %s\n", i, av[i]); if(ac <= 1) wc("-"); else for(i = 1; i<ac; i++) wc(av[i]); }

7

8 Pointer Addition #include main() { char *str = "hello, world\n"; for( ; *str; str++) printf("%s", str); }

9 hello3 #include #define MAXLINE 1024 int main() { char line[MAXLINE]; while(fgets(line, MAXLINE, stdin) != NULL) { char *str = line; for( ; *str; str++) if(str == line || (isblank(str[-1]) && !isblank(str[0]))) printf("%s", str); } } Subscripting – x[i] – (x+i)[0] – *(x+i)

10 Concepts so far… Interpreted v. Compiled – cc, gcc, make stdio Variable declarations Strings: pointers to sequences of characters Pointer addition Subscripting by positive & negative numbers – x[i] – (x+i)[0] – *(x+i)

11 Structures struct box { int ptr:30; unsigned int type:2; }; struct pair { struct box first, rest; } heap[HEAPSIZE];

12 Boxes: Pointer + Type 3 Types – SYMBOL nil, +, *, quote – LIST first + rest – NUMBER int

13 first(cons(left, right))  left rest(cons(left, right))  right

14 Pname: Symbol  String Intern: String  Symbol

15 Read, Eval, Print

16 Eval Done: addition To do: subtraction, multiplication, division


Download ppt "C Homework Write mypaste.c and mycomm.c – mypaste is like paste & mycomm is like comm – Both take two files as arguments – Paste reads a line from each."

Similar presentations


Ads by Google