-
Notifications
You must be signed in to change notification settings - Fork 0
/
prove.c
176 lines (144 loc) · 2.56 KB
/
prove.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <stdio.h>
#include <stdlib.h>
// define the point type
typedef struct point {
int x;
int y;
char *name;
} t_point;
// define the rect type
typedef struct rect {
t_point p1;
t_point p2;
} t_rect;
// functions
void pointers(void);
void structures(void);
void printpoint(t_point *);
t_point *makepoint(int, int, char *);
t_rect *makerect(t_point, t_point);
int calcarea(t_rect *);
int calcarea2(t_rect rect);
void printrect(t_rect *);
void foo(void);
// main
int main(int argc, char *argv[])
{
//pointers();
foo();
structures();
return 0;
}
void foo()
{
char c;
// while((c = getchar()) != EOF)
// putchar(c);
while((c = getc(stdin)) != EOF)
putchar(c, stdout);
}
//
// structures
void structures()
{
t_point *pt = makepoint(2, 3, "alfa");
printpoint(pt);
t_rect *rect = makerect(*makepoint(10, 10, "p1"), *makepoint(20, 20, "p2"));
printrect(rect);
printf("Area: %d\n", calcarea(rect));
printf("Area: %d\n", calcarea2(*rect));
free(pt);
free(rect);
}
//
// calcarea
int calcarea(t_rect *rect)
{
int b = rect->p2.x - rect->p1.x;
int h = rect->p2.y - rect->p1.y;
return (b * h);
}
//
// calcarea2
int calcarea2(t_rect rect)
{
int b = rect.p2.x - rect.p1.x;
int h = rect.p2.y - rect.p1.y;
return (b * h);
}
//
// make a point
t_point *makepoint(int x, int y, char *name)
{
// memory allocation
t_point *p;
p = (t_point *)malloc(sizeof(t_point));
// set members
p->x = x;
p->y = y;
p->name = name;
// ret the point
return p;
}
//
// make a rect
t_rect *makerect(t_point p1, t_point p2)
{
t_rect *r;
r = (t_rect *)malloc(sizeof(t_rect));
r->p1 = p1;
r->p2 = p2;
return r;
}
//
// print the point
void printpoint(t_point *pt)
{
//printf("point: %s, %i, %i\n", pt->name, pt->x, pt->y);
// otherwise
printf("point: %s, %i, %i\n", (*pt).name, (*pt).x, (*pt).y);
}
//
// print the rect
void printrect(t_rect *rect)
{
printpoint(&(rect->p1));
printpoint(&(rect->p2));
}
void pointers()
{
int *p; // pointer to int
int a = 1;
p = &a; // p point to a
printf("address of p: %p \n", p);
printf("value of p: %i \n", *p);
char s[] = "Helllo World!";
char *pc; // pointer to char
pc = s;
printf("address of pc: %pc \n", p);
printf("value of pc: %s \n", pc);
while(*pc != '\0')
putchar(*pc++);
printf("\n");
char *months[] = {
"gennaio",
"febbrario",
"marzo",
"aprile",
"maggio",
"giugno",
"luglio",
"agosto",
"settembre",
"ottobre",
"novembre",
"dicembre"
};
char **mm = months;
int i;
for(i = 0; i < 12; i++)
{
//printf("%s\n", mm[i]);
printf("%s\n", *mm++);
}
}