Presentation is loading. Please wait.

Presentation is loading. Please wait.

Cs288 Intensive Programming in Linux

Similar presentations


Presentation on theme: "Cs288 Intensive Programming in Linux"— Presentation transcript:

1 Cs288 Intensive Programming in Linux
Instructor: C F Yurkoski Section web site: Class 8

2 today homework revu scanf Pointers Bit operations FILE operations
Macros Typedefs malloc() and free() Linked lists C lib Recursion In class exercises cfy

3 homework Do exercise 1-19 from the text.
Redo problem 4 from class 2 as a C program Write a program which reads stdin and converts any local case characters to upper cfy

4 1-19 write a function called reverse(s) which reverses the character string s. Use it to write a program which reverses its input. solution at: cfy

5 cfy

6 cfy

7 cfy

8 1-21 write a program that replaces blanks with the minimum number of tabs and blanks. soluton at: cfy

9 cfy

10 Problem3 https://web.njit.edu/~yurkowsk/x/prob3A.c
cfy

11 toupper.c https://web.njit.edu/~yurkowsk/x/toupper.c
#include <stdio.h> main(){ char c; while((c=getchar())!=EOF){ if(c>='a' && c <= 'z') c+='A'-'a'; putchar(c); } cfy

12 scanf Format similar to printf %d, %s, %x etc.
int scanf(const char *restrict format, ... ); %d, %s, %x etc. cfy

13 example #include <stdio.h> main(){ char action[6]; int word; while(scanf("%s %d",action,&word)!=EOF){ fprintf(stderr,"%s %d\n",action,word); if(!strcmp(action,“DO")) fprintf(stderr, “do something%x\n",word); else “reverse action %x\n",word); } cfy

14 >: echo “DO 666" |. /a. out add 10 add a >: echo “UNDO 7" |. /a
>: echo “DO 666" | ./a.out add 10 add a >: echo “UNDO 7" | ./a.out delete 7 cfy

15 pointers char c='a', d='b', *cp; cp= &d; *cp=c; printf("%c %c\n",c,d);
cfy

16 Referencing structures
typedef struct string string; struct string{ struct string *forward; int data; }a[SZ]; a[i].forward=&a[i+1]; cfy

17 Using pointers w/ structures
struct list{ struct list *l; int payload; }array[SIZE]; struct list *lp; lp=array; lp->payload=data[10]; cfy

18 BIT OPERATIONS int x, y, z; x&=7; z=y|x; z=y<<2; if(y^x); x=~y;
cfy

19 FILE I/O fopen() and friends (fwrite(), fread(), fseek() etc.
cfy

20 FILE *f; f=fopen("/tmp/yurkoski","w"); fprintf(f,“msg");
cfy

21 Code macros e.g.: #define sum(a,b) (a+b)
#define macro-name(args) (expression) e.g.: #define sum(a,b) (a+b) cfy

22 #include <stdio.h> #include <stdlib.h>
#define cube(a) (a*a*a) /* no semicolon*/ main(int argc, char *argv[]) { int a; if(argc != 2){ fprintf(stderr, "usage: %s n\n",argv[0]); exit(-1); } a=atoi(argv[1]); printf("the cube of %d is %d\n", a, cube(a)); 6-9-15

23 Revu typedefs Used to define you own types
syntax: typedef {structure-def}typedef-name; Usage: typedef-name variable-name;

24 typedef struct { int fill; char name[8]; }frame; frame *p; p=(frame *)malloc(SIZE*sizeof(frame)); p++; p->fill=fill;

25 #include <stdlib.h> #define SIZE 10 typedef struct { int fill;
char name[8]; }frame; main() { frame *p, *q, fill; char cmd[10]; q=p=malloc(SIZE*sizeof(frame)); 6-9-15

26 Linked list Singly linked lists Doubly linked lists
Circularly linked lists Multiply linked lists cfy

27 Singly linked lists cfy

28 Doubly linked lists cfy

29 C library atoi() itoa() atof() cfy

30 Recursion cfy

31 In class problem 1 Write a C program which accepts as a command line argument an integer and reads another integer from standard input and bit wise or the two together and writes the result (converted to an ascii string) to a file in the in /tmp whose name is your student id number. Put your source code in moodle.

32 example if the file /tmp/x contains a 5, and your id is 666 cc yourprogm.c –o yourprogm cat /tmp/x | yourprogm 6 should write the ascii string 30 to the file 666 in the current directory.

33 In class problem 2 Write a program which reads a series of integers for standard input until an EOF is encountered. Store them in a linked list in sorted order. Upon encountering the EOF, output the items on the list.

34

35

36


Download ppt "Cs288 Intensive Programming in Linux"

Similar presentations


Ads by Google