Initial Commit
This commit is contained in:
commit
ecdcb8dd2a
13
Makefile
Normal file
13
Makefile
Normal file
@ -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)
|
13
main.c
Normal file
13
main.c
Normal file
@ -0,0 +1,13 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#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;
|
||||
}
|
16
parse.rb
Normal file
16
parse.rb
Normal file
@ -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
|
37
person.c
Normal file
37
person.c
Normal file
@ -0,0 +1,37 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
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");
|
||||
}
|
||||
}
|
13
person.h
Normal file
13
person.h
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user