1 /*#include<iostream>
2 using namespace std;
3 #define MaxSize 10
4
5 typedef struct {
6 int age;
7 double height;
8 }Person, *PPerson,Persons[MaxSize];
9
10 void printPerson(Person p)
11 {
12 cout << p.age << "," << p.height << endl;
13 }
14
15 int main() {
16 //普通变量
17 Person p;
18 p.age = 10;
19 p.height = 17.5;
20 printPerson(p);
21
22 //指针
23 PPerson pp = &p;//指向p的结构体
24 pp->age = 17;
25 pp->height = 18.6;
26 printPerson(p);
27 printPerson(*pp);
28
29 //数组
30 Persons ps;
31 cout << sizeof(ps) / sizeof(Person) << endl;
32 return 0;
33 }*/
34
35 //定义结构体的方式二
36 /*#include<iostream>
37 using namespace std;
38
39 typedef struct Person {
40 char *name;
41 int age;
42 double height;
43 //配偶
44 struct Person* mate;
45 }Person,*PPerson;
46
47 void printPerson(Person p)
48 {
49 cout << "姓名" << p.name << ",年龄" << p.age
50 <<"身高"<<p.height<< endl;
51 cout << "配偶姓名" << p.mate->name
52 << "配偶年龄:" << p.mate->age
53 << "配偶身高:" << p.mate->height << endl;
54 cout << endl;
55 }
56
57 int main()
58 {
59 Person lili, lihua;
60 //配偶
61 lili.mate = &lihua;
62 lihua.mate = &lili;
63 //信息
64 lili.age = 23;
65 lili.height = 1.65;
66 lili.name = "lili";
67
68 lihua.age = 25;
69 lihua.height = 1.75;
70 lihua.name ="lihua";
71
72 printPerson(lili);
73 printPerson(lihua);
74 return 0;
75 }*/
76
77 /*#include<iostream>
78 using namespace std;
79 //方式一:一次性定义出变量
80 struct {
81 int age;
82 double height;
83 }fq,zjl;
84
85 int main()
86 {
87 //方式一
88 //为结构体属性赋值
89 fq.age = 22;
90 fq.height = 1.75;
91 zjl.age = 46;
92 zjl.height = 1.78;
93
94 //访问结构体属性
95 cout << fq.age << "," << fq.height << endl;
96 cout << zjl.age << "," << zjl.height << endl;
97 return 0;
98 }*/
99
100 //方式二
101 /*#include<iostream>
102 using namespace std;
103 struct Person {
104 int age;
105 double height;
106 };
107
108 int main() {
109 //定义变量
110 struct Person p;
111 p.age = 10;
112 p.height = 1.75;
113 cout << p.age << "," << p.height << endl;
114 return 0;
115 }*/
116
117 //方式三
118 //typedef define形式
119 /*#include<iostream>
120 using namespace std;
121 #define MaxSize 10
122
123 typedef struct {
124 int age;
125 double height;
126 }Person,*PPerson,Persons[MaxSize];
127
128 void PrintPerson(Person p)
129 {
130 cout << p.age << "," << p.height << endl;
131 }
132
133 int main() {
134 //普通变量
135 Person p;
136 p.age = 10;
137 p.height = 175;
138 PrintPerson(p);
139
140 //指针
141 PPerson pp = &p;//修改属性
142 pp->age = 17;
143 pp->height = 18.9;
144 //PrintPerson(p);
145 PrintPerson(*pp);
146 return 0;
147 }*/
148 //第二种
149 /*#include<iostream>
150 using namespace std;
151 #define MaxSize 200
152
153 typedef struct Person {
154 char* name;
155 int age;
156 double height;
157 struct Person* mate;
158 }Person,*PPerson,Person[MaxSize];
159
160 void printPerson(Person p)
161 {
162 cout << p->age << "," << p->height << p->mate << endl;
163 }
164
165 int main()
166 {
167 Person lihua, lili;
168 lili->mate = &lihua;
169 lihua->mate = &lili;
170
171
172 return 0;
173 }*/
174
175
176 //#include<iostream>
177 //#include<stdlib.h>
178 //using namespace std;
179 //
180 //typedef struct {
181 // int age;
182 // double height;
183 //}Person,*PPerson;
184 //
185 //void printPerson(Person p)
186 //{
187 // cout << p.age << "," << p.height << endl;
188 //}
189 //
190 //int main()
191 //{
192 // //动态创建一个person
193 // PPerson pperson = (Person*)malloc(sizeof(Person));
194 // pperson->age = 10;
195 // pperson->height = 19.8;
196 // printPerson(*pperson);
197 // free(pperson);
198 // int* pa =(int*)malloc(sizeof(int));
199 // *pa = 10;
200 // cout <<&pa << "," << *pa << endl;
201 // free(pa);
202 // return 0;
203 //}
204 //#include<iostream>
205 //using namespace std;
206 //
207 //void swapValue(int* a, int* b) {
208 // int temp = *a;
209 // *a = *b;
210 // *b = temp;
211 //}
212 //
213 //int main()
214 //{
215 // int a = 10, b = 80;
216 // swapValue(&a, &b);
217 // cout << a << "," << b << endl;
218 // return 0;
219 //}
220
221 //++i返回的是i+1 i本身也加1
222 //i++返回的是i i本身增加1
223
224
225
226 //#include<iostream>
227 //using namespace std;
228 //
229 ////求n的阶乘
230 //int fac(int n)
231 //{
232 // if (n == 0 || n == 1) {
233 // cout << "汇聚fac(" << n << ")的阶乘结果" << endl;
234 // return 1;
235 // }
236 // else {
237 // cout << "将" << n << "分解为" << n << "*fac(" << n - 1 << ")" << endl;
238 // int result = fac(n - 1);
239 // cout << "汇聚" << n << "*fac" << n - 1
240 // << ")" << endl;
241 // return n * result;
242 // }
243 //}//递归就是一个分配再整合的过程
244 //
245 //
246 //int main()
247 //{
248 // printf("5!=%d\n", fac(5));
249 // return 0;
250 //}
251 //
252 ////递归的关键分解合并的模型以及结束的条件
253
254
255 //#include<iostream>
256 //using namespace std;
257 //
258 //int fact(int n)
259 //{
260 // if (n == 0 || n == 1) {
261 // return 1;
262 // }
263 // else {
264 // return n * fact(n - 1);
265 // }
266 //}
267 //int main()
268 //{
269 // cout << fact(4) << endl;
270 // return 0;
271 //}
272
273
274 //动态数组
275 #include<iostream>
276 using namespace std;
277
278 void testStaticArray() {
279 //初始化数组
280 int a[] = { 1,2,3,4,5 };
281 int sizeA = sizeof(a) / sizeof(int);
282 cout << "方式一访问静态数组:" << endl;
283 for (int i = 0; i <sizeA; i++) {
284 cout << a[i] << "\t";
285 }
286 cout << endl;
287 //方式二访问数组a
288 //数组名a:表示数组的首地址
289 cout << "方式二访问静态数组" << endl;
290 int* b = a;
291 int count = 0;
292 while (count < sizeA) {
293 //访问当前b指向位置上的值
294 cout << *b << "\t";
295 b++;
296 count++;
297 }
298 cout << endl;
299 }
300
301 void testDynamicArray() {
302 //初始化数组a 1 2 3 4 5
303 int sizeA = 5;
304 int* a = (int*)malloc(sizeA * sizeof(int));
305 cout << "方式一访问动态数组:赋值操作" << endl;
306 for (int i = 0; i < 5; ++i) {
307 a[i] = i + 1;
308 }
309
310 //方式二访问数组A
311 //数组名a表示这个数组的首地址
312 cout << "方式二访问动态数组" << endl;
313 int* b = a;
314 int count = 0;
315 while (count < sizeA)
316 {
317 //访问当前位置上的值
318 cout << *b << "\t";
319 //去数组的下一位
320 b++;
321 count++;
322 }
323 cout << endl;
324 free(a);
325 }
326
327 int main()
328 {
329 testStaticArray();
330 testDynamicArray();
331 return 0;
332 }
