commit ecdcb8dd2a760d4ebba4d4668f37e12504d3bbaa Author: pjht Date: Mon Jun 4 09:06:12 2018 -0500 Initial Commit diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0b03512 --- /dev/null +++ b/Makefile @@ -0,0 +1,13 @@ +CC = gcc +CFLAGS = -Wall -g +SRCS = $(wildcard *.c */*.c */*/*.c) +OBJS = $(SRCS:.c=.o) +MAIN = oopc +.PHONY: clean +all: $(MAIN) +$(MAIN): $(OBJS) + $(CC) $(CFLAGS) -o $(MAIN) $(OBJS) +.c.o: + $(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@ +clean: + $(RM) *.o *~ $(MAIN) diff --git a/main.c b/main.c new file mode 100644 index 0000000..39fd33f --- /dev/null +++ b/main.c @@ -0,0 +1,13 @@ +#include +#include +#include +#include "person.h" +int main() { + Person *p=Person(0,"Peter") + p.print_age; + for (int i=0;i<13;i++) { + p.happy_birthday; + p.print_age; + } + return 0; +} diff --git a/parse.rb b/parse.rb new file mode 100644 index 0000000..398f13c --- /dev/null +++ b/parse.rb @@ -0,0 +1,16 @@ +inf=File.read("main.c") +updfile=File.read("main.c").split("\n") +i=0 +inf.each_line do |line| + if /\A(.+)\.(.+);/.match line.strip + obj=$1 + fname=$2 + updfile[i]="#{" "*line.index(/[^ ]/)}#{obj}->#{fname}(#{obj})" + end + i+=1 +end +f=File.open("main.c","a") +f.puts +f.puts +f.print updfile.join("\n") +f.close diff --git a/person.c b/person.c new file mode 100644 index 0000000..817bc04 --- /dev/null +++ b/person.c @@ -0,0 +1,37 @@ +#include +#include +#include +void init_person(Person *p,int age,char *name) { + p->age=age; + p->name=malloc(50*sizeof(char)); + strcpy(p->name,name); + p->happy_birthday=happy_birthday; + p->print_age=print_age; +} + +Person * new_person(int age,char *name) { + Person *tmp; + tmp=malloc(sizeof(Person)); + init_person(tmp,age,name); + return tmp; +} + +//private + +void happy_birthday(Person *self) { + printf("Happy birthday to you, happy birthday to you.\n"); + printf("Happy birthday dear %s, happy birthday to you!\n",self->name); + self->age=self->age+1; +} + +void print_age(Person *self) { + if (self->age>1) { + printf("%s is %d years old.\n",self->name,self->age); + } else if (self->age==1) { + printf("%s is 1 year old.\n",self->name); + } else if (self->age==0) { + printf("%s is a newborn.\n",self->name); + } else { + printf("You can't be under 0 years old!\n"); + } +} diff --git a/person.h b/person.h new file mode 100644 index 0000000..4145fba --- /dev/null +++ b/person.h @@ -0,0 +1,13 @@ +#ifndef PERSON_H +#define PERSON_H +typedef struct Person Person; +struct Person { + int age; + char *name; + void (*happy_birthday)(Person *); + void (*print_age)(Person *); +}; +//start +void init_person(Person *p,int age,char *name); +Person * new_person(int age,char *name); +#endif