#include <stdlib.h> #include <stdio.h> int fac(int); int main(int argc, char *argv[]) { if (argc < 2) { fprintf(stderr,"Usage asdf: %s N\n",argv[0]); exit(1); } int i = atoi(argv[1]); printf("%d! = %d\n", i, fac(i)); return 0; } int fac(int n) { int prod = 1; if (n > 1) { int i; for (i = 2; i <= n; i++) prod *= n; } return prod; }
#include <stdlib.h> #include <stdio.h> int fac(int); int main(int argc, char *argv[]) { if (argc < 2) { fprintf(stderr,"Usage: %s N\n",argv[0]); exit(1); } int i = atoi(argv[1]); printf("%d! = %d\n", i, fac(i)); printf("asdfasdf"); return 0; } int fac(int n) { if (n < 2){ return 1; }else return n * fac(n-1); }
// Item.h ... definition for items in Queues // Written by John Shepherd, March 2013 #ifndef ITEM_H #define ITEM_H typedef int Item; #define ItemCopy(i) (i) #define ItemEQ(i1,i2) ((i1) == (i2)) #define ItemLT(i1,i2) ((i1) < (i2)) #define ItemGT(i1,i2) ((i1) > (i2)) #define ItemRead(i) scanf("%d",(i)) #define ItemShow(i) printf("%d",(i)) #define ItemDrop(i) #endif
// List.h ... interface to List GADT // Written by John Shepherd, March 2013 #ifndef LIST_H #define LIST_H #include <stdlib.h> #include "Item.h" typedef struct ListRep *List; List newList(); // create new empty list void dropList(List); // free memory used by list void showList(List); // display as [1,2,3...] List readList(); // read+insert list values List ListCopy(List); // make a copy of a list void ListInsert(List,Item); // add item into list void ListDelete(List,Item); // remove item(s) Item ListItem(List,int); // get i'th item int ListFindPos(List,Item); // position of item int ListLength(List); // # items in list void ListStartScan(List); // begin scan Item ListScanNext(List); // fetch next element int ListScanDone(List); // reached end of scan? #endif
#ifndef QUEUE_H #define QUEUE_H #include <stdlib.h> #include <stdio.h> #include <assert.h> #include "Item.h" #include "List.h" typedef struct Node *Link; typedef struct Node { Item value; Link next; } Node; typedef struct ListRep { Node *first; // ptr to first node Node *last; // ptr to last node Node *curr; // current node in scan } ListRep; // create new empty list List newList() { List L; L = malloc(sizeof(ListRep)); assert(L != NULL); L->first = NULL; L->last = NULL; L->curr = NULL; L->first = NULL; return L; } // free memory used by list void dropList(List L) { free(L); } // display as [1,2,3,4...] void showList(List L) { Node *curr; printf("["); curr = L->first; while (curr->next != NULL) { ItemShow(curr->value); if (curr->next != NULL) printf(","); curr = curr->next; } printf("]"); } // read+insert list values List readList() { Item val; List new = newList(); while (ItemRead(&val)) { ListInsert(new, val); } return new; } // make a copy of a list List ListCopy(List L) { List new = newList(); Link cur = L->first; while (cur != NULL) { ListInsert(new, cur->value); cur = cur->next; } return new; } // add item into list void ListInsert(List L, Item it) { Link new; new = malloc(sizeof(Node)); assert(new != NULL); new->value = ItemCopy(it); new->next = NULL; if (L->last == NULL) { L->first = new; } else { L->last->next = new; } L->last = new; } // remove item(s) void ListDelete(List L, Item it) { return; } // get i'th item Item ListItem(List L, int i) { return 0; } // position of item int ListFindPos(List L, Item it) { return -1; } // # items in list int ListLength(List L) { int nnodes = 0; Link curr = L->first; while (curr != NULL) { nnodes++; curr = curr->next; } return nnodes; } // begin scan void ListStartScan(List L) { } // fetch next element Item ListScanNext(List L) { return 0; } // reached end of scan? int ListScanDone(List L) { return 1; } #endif
#ifndef QUEUE_H #define QUEUE_H #include <stdlib.h> #include <stdio.h> #include <assert.h> #include "Item.h" #include "List.h" typedef struct Node *Link; typedef struct Node { Item value; Link next; } Node; typedef struct ListRep { Node *first; // ptr to first node Node *last; // ptr to last node Node *curr; // current node in scan } ListRep; // create new empty list List newList() { List L; L = malloc(sizeof(ListRep)); assert(L != NULL); L->first = NULL; L->last = NULL; L->curr = NULL; L->first = NULL; return L; } // free memory used by list void dropList(List L) { free(L); } // display as [1,2,3,4...] void showList(List L) { Node *curr; printf("["); curr = L->first; while (curr != NULL) { ItemShow(curr->value); if (curr->next != NULL) printf(","); curr = curr->next; } printf("]"); } // read+insert list values List readList() { Item val; List new = newList(); while (ItemRead(&val)) { ListInsert(new, val); } return new; } // make a copy of a list List ListCopy(List L) { List new = newList(); Link cur = L->first; while (cur != NULL) { ListInsert(new, cur->value); cur = cur->next; } return new; } // add item into list void ListInsert(List L, Item it) { Link new; new = malloc(sizeof(Node)); assert(new != NULL); new->value = ItemCopy(it); new->next = NULL; if (L->last == NULL) { L->first = new; } else { L->last->next = new; } L->last = new; } // remove item(s) void ListDelete(List L, Item it) { return; } // get i'th item Item ListItem(List L, int i) { return 0; } // position of item int ListFindPos(List L, Item it) { return -1; } // # items in list int ListLength(List L) { int nnodes = 0; Link curr = L->first; while (curr != NULL) { nnodes++; curr = curr->next; } return nnodes; } // begin scan void ListStartScan(List L) { } // fetch next element Item ListScanNext(List L) { return 0; } // reached end of scan? int ListScanDone(List L) { return 1; } #endif
#include <stdlib.h> #include <stdio.h> #include <assert.h> #include "Item.h" #include "List.h" int main(int argc, char *argv[]) { int i; List L = newList(); printf("test list"); assert(ListLength(L) == 0); showList(L); for (i = 0; i < 20; i++) ListInsert(L,i); assert(ListLength(L) == 20); printf("L: "); showList(L); printf("\n"); dropList(L); return 0; }
CC=gcc CFLAGS=-Wall -Werror -g OBJS=testList.o List.o .PHONY: clean all : testList fac0 fac testList : testList.o List.o $(CC) -o testList $(OBJS) testList.o : testList.c List.h Item.h List.o : List.c List.h Item.h fac0 : fac0.c fac : fac.c clean : rm -f testList fac0 fac $(OBJS) core a.out
This is a buggy version of factorial