C Homework

16
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)

description

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 - PowerPoint PPT Presentation

Transcript of C Homework

Page 1: C Homework

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)

Page 2: C Homework

hello.c

• #include <stdio.h>main(){ printf("hello, world\n");}

• New Concepts– C is a compiled

• Interpreted languages: python, javascript, R, lisp

– cc –o hello hello.c– ./hello

Page 3: C Homework

Make

• make clean• make all• Makefile

Page 4: C Homework

Input/Output: mycat.c (My Cat)

• #include <stdio.h>main(){ int c; while((c = getchar()) != EOF) putchar(c);}

• New Concepts– Stdio: getchar/putchar– Variable declarations

Page 5: C Homework

mywc.c (My Word Count)• #include <stdio.h>

#include <ctype.h>

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

IN OUT

words++words++

Page 6: C Homework

mywc2.c: ac & av• #include <stdio.h>

#include <ctype.h>

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]);}

Page 7: C Homework
Page 8: C Homework

Pointer Addition

• #include <stdio.h>main(){ char *str = "hello, world\n"; for( ; *str; str++) printf("%s", str);}

Page 9: C Homework

hello3• #include <stdio.h>

#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)

Page 10: C Homework

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)

Page 11: C Homework

Structures

• struct box { int ptr:30; unsigned int type:2;};

struct pair { struct box first, rest;} heap[HEAPSIZE];

Page 12: C Homework

Boxes: Pointer + Type• 3 Types– SYMBOL• nil, +, *, quote

– LIST• first + rest

– NUMBER• int

Page 13: C Homework

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

Page 14: C Homework

Pname: Symbol StringIntern: String Symbol

Page 15: C Homework

Read, Eval, Print

Page 16: C Homework

Eval

• Done: addition• To do: subtraction, multiplication, division