C Homework

Post on 26-Jan-2016

42 views 0 download

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

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)

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

Make

• make clean• make all• Makefile

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

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++

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

Pointer Addition

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

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)

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)

Structures

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

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

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

– LIST• first + rest

– NUMBER• int

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

Pname: Symbol StringIntern: String Symbol

Read, Eval, Print

Eval

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