├── chapter-1.md ├── chapter-2.md ├── chapter-3.md ├── chapter-4.md ├── chapter-5.md ├── clock ├── acllib.c ├── acllib.h ├── clock.c ├── clock.exe └── readme.md ├── clocks ├── Clock.cpp ├── Clock.h ├── EllipseClock.cpp ├── EllipseClock.h ├── RectangleClock.cpp ├── RectangleClock.h ├── Shape.cpp ├── Shape.h ├── acllib.c ├── acllib.h ├── clocks.cpp ├── clocks.exe └── readme.md ├── cube ├── acllib.c ├── acllib.h ├── cube.c ├── cube.exe └── readme.md ├── image └── 2021-09-02-16-15-55.png ├── library ├── MyClass.cpp ├── MyClass.h ├── MyStruct.cpp ├── MyStruct.h ├── readme.md └── test.cpp ├── misc ├── addone-1.cpp ├── addone-2.cpp ├── animal-1.cpp ├── animal-2.cpp ├── animal-3.cpp ├── animal-4.cpp ├── animal-5.cpp ├── const-member-1.cpp ├── const-member-2.cpp ├── const-pointer.cpp ├── constructor-1.cpp ├── constructor-2.cpp ├── delete-1.cpp ├── delete-2.cpp ├── delete-3.cpp ├── delete-4.cpp ├── delete-5.cpp ├── dog.cpp ├── exception.cpp ├── format.cpp ├── func.cpp ├── func.h ├── hello.c ├── hello.cpp ├── io.cpp ├── main.cpp ├── max.cpp ├── namespace.cpp ├── node.cpp ├── pi.cpp ├── setname-1.c ├── setname-2.c ├── setname-3.cpp ├── static-member.cpp ├── struct class.cpp ├── student-vip.cpp ├── student.cpp ├── swap.cpp ├── template.cpp ├── temporary-1.cpp ├── temporary-2.cpp ├── zoo-1.cpp ├── zoo-2.cpp ├── zoo-3.cpp └── zoo-4.cpp ├── readme.md ├── snake ├── acllib.c ├── acllib.h ├── readme.md ├── snake.c └── snake.exe ├── sprite ├── encapsulation │ ├── CautoSprite.cpp │ ├── CautoSprite.h │ ├── CusrSprite.cpp │ ├── CusrSprite.h │ ├── acllib.c │ ├── acllib.h │ ├── cat.jpg │ ├── dog.jpg │ ├── game.cpp │ ├── readme.md │ └── sprite.exe ├── inheritance │ ├── AutoSprite.cpp │ ├── AutoSprite.h │ ├── SpriteBase.cpp │ ├── SpriteBase.h │ ├── UsrSprite.cpp │ ├── UsrSprite.h │ ├── acllib.c │ ├── acllib.h │ ├── game.cpp │ ├── jerry.bmp │ ├── readme.md │ ├── sprite.exe │ └── tom.bmp ├── polymorphism │ ├── AutoSprite.cpp │ ├── AutoSprite.h │ ├── AvoidSprite.cpp │ ├── AvoidSprite.h │ ├── SpriteBase.cpp │ ├── SpriteBase.h │ ├── UsrSprite.cpp │ ├── UsrSprite.h │ ├── acllib.c │ ├── acllib.h │ ├── dog.bmp │ ├── duck.jpg │ ├── game.cpp │ ├── jerry.bmp │ ├── readme.md │ ├── sprite.exe │ └── tom.bmp └── struct │ ├── acllib.c │ ├── acllib.h │ ├── cat.jpg │ ├── dog.jpg │ ├── game.c │ ├── rabbit.jpg │ ├── readme.md │ └── sprite.exe └── student ├── student.cpp ├── studentarray.cpp ├── studentlist.cpp └── studentpointerarray.cpp /chapter-1.md: -------------------------------------------------------------------------------- 1 | --- 2 | marp: true 3 | theme: gaia 4 | --- 5 | 6 | 7 | 8 | # C++程序设计 9 | 10 | 电子科技大学 11 | 12 | 余盛季 13 | 14 | --- 15 | 16 | 17 | 18 | **主讲课程** 19 | 20 | C++程序设计、形式语言与自动机、程序设计语言与编译 21 | 22 | **研究方向** 23 | 24 | 程序设计语言、计算理论、云计算 25 | 26 | --- 27 | 28 | 29 | 30 | # 什么是C++ 31 | 32 | --- 33 | 34 | 35 | 36 | **C + 面向对象 + 其它** 37 | 38 | --- 39 | 40 | 41 | 42 | # 为什么要学习C++ 43 | 44 | --- 45 | 46 | 47 | 48 | https://www.tiobe.com/tiobe-index 49 | 50 | ![](image/2021-09-02-16-15-55.png) 51 | 52 | --- 53 | 54 | 55 | 56 | # 如何学习C++ 57 | 58 | --- 59 | 60 | 61 | 62 | **读代码 + 写代码 + 调代码** 63 | 64 | --- 65 | 66 | 67 | 68 | # 课程安排 69 | 70 | --- 71 | 72 | **学时分配** 73 | 总学时(32):课堂讲授(16)+ 上机实验(16) 74 | 75 | **成绩构成** 76 | 课后作业(码图作业,30%) 77 | 实验报告(精灵游戏,20%) 78 | 期末考试(码图考试,50%) 79 | 80 | --- 81 | 82 | **教材** 83 | C与C++程序设计(戴波) 84 | 85 | **参考书** 86 | The C++ Programming Language(Bjarne Stroustrup) 87 | C++ Primer(Stanley B. Lippman) 88 | 89 | **课程MOOC** 90 | www.icourse163.org/course/UESTC-1001774006?tid=1465243448 91 | 92 | --- 93 | 94 | 95 | 96 | # 实验环境 97 | 98 | 编辑器 + 编译器 + 调试器 99 | 100 | --- 101 | 102 | ## 编辑器 103 | 104 | **Visual Studio Code** 105 | 106 | 下载:https://code.visualstudio.com/ 107 | 108 | `Download > Windows > User Installer > 64bit` 109 | 110 | 文件:VSCodeUserSetup-x64-1.58.2.exe 111 | 112 | 插件:C/C++、Marp for VS Code 113 | 114 | --- 115 | 116 | ## 编译器 117 | 118 | **GCC (GNU Compiler Collection)** 119 | 120 | 下载:https://sourceforge.net/projects/mingw-w64/files/ 121 | 122 | `MinGW-W64 GCC-8.1.0 > x86_64-win32-seh` 123 | 124 | 文件:x86_64-8.1.0-release-win32-seh-rt_v6-rev0.7z 125 | 126 | 解压到任意文件夹,例如 C:\tools\mingw64。 127 | 128 | --- 129 | 130 | 将mingw64中的bin目录添加到环境变量Path中: 131 | 桌面 > 右击“此电脑” > 属性 > 高级系统设置 > 环境变量 > 用户变量 > Path > 编辑 > 新建 > ...\\...\mingw64\bin 132 | 133 | 打开命令提示符,执行命令 `g++ --version`,出现版本信息,表示编译器安装成功。 134 | 135 | ``` 136 | C:\>g++ --version 137 | g++ (x86_64-win32-seh-rev0, Built by MinGW-W64 project) 8.1.0 138 | Copyright (C) 2018 Free Software Foundation, Inc. 139 | This is free software; see the source for copying conditions. There is NO 140 | warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 141 | 142 | C:\> 143 | ``` 144 | 145 | --- 146 | 147 | ## 调试器 148 | 149 | **GDB (The GNU Project Debugger)** 150 | 151 | 打开命令提示符,执行命令`gdb --version`,出现版本信息,表示调试器安装成功。 152 | 153 | ``` 154 | C:\>gdb --version 155 | GNU gdb (GDB) 8.1 156 | Copyright (C) 2018 Free Software Foundation, Inc. 157 | License GPLv3+: GNU GPL version 3 or later 158 | This is free software: you are free to change and redistribute it. 159 | 160 | C:\> 161 | ``` 162 | 163 | --- 164 | 165 | **参考资料** 166 | [C/C++ for Visual Studio Code](https://code.visualstudio.com/docs/languages/cpp) 167 | [Using GCC with MinGW](https://code.visualstudio.com/docs/cpp/config-mingw) 168 | [GCC参数详解](https://www.runoob.com/w3cnote/gcc-parameter-detail.html) 169 | [C++ GDB调试大全](https://blog.csdn.net/bigheadyushan/article/details/77828949) 170 | 171 | --- 172 | 173 | 174 | 175 | # C++程序实例 176 | 177 | --- 178 | 179 | # 第一个C++程序 180 | 181 | ```c++ 182 | int main() 183 | { 184 | return 0; /* 1、2、3…… */ 185 | } 186 | ``` 187 | 188 | 编译:g++ test.cpp -o test.exe 189 | 执行:test.exe 190 | 查看结果(返回值): 191 | 192 | ``` 193 | echo %errorlevel% 194 | ``` 195 | 196 | --- 197 | 198 | # 第二个C++程序 199 | 200 | ```c++ 201 | #include 202 | int main() 203 | { 204 | printf("Hello, I'm a C++ program.\n"); 205 | return 0; 206 | } 207 | ``` 208 | 209 | 库函数:printf 210 | 头文件:stdio.h 211 | 全路径:C:\tools\mingw64\x86_64-w64-mingw32\include\stdio.h 212 | 213 | --- 214 | 215 | # 第三个C++程序 216 | 217 | ```c++ 218 | #include 219 | using namespace std; 220 | int main() 221 | { 222 | cout << "Hello, I'm really a C++ program." << endl; 223 | return 0; 224 | } 225 | ``` 226 | 227 | 输出流对象:cout 228 | 头文件:iostream 229 | 全路径:C:\tools\mingw64\lib\gcc\x86_64-w64-mingw32\8.1.0\include\c++\iostream 230 | 231 | --- 232 | 233 | # 输入与输出 234 | 235 | 输出流对象:cout 236 | 左移运算符:<< 237 | 238 | ``` 239 | int a = 1, b = 2; 240 | printf("a + b = %d \n", a + b); 241 | 242 | int c = 3, d = 4; 243 | cout << "c + d = " << c + d << endl; 244 | ``` 245 | 246 | --- 247 | 248 | 输入流对象:cin 249 | 右移运算符:>> 250 | 251 | ``` 252 | int a, b; 253 | scanf("%d %d", &a, &b); 254 | printf("a + b = %d \n", a + b); 255 | 256 | int c, d; 257 | cin >> c >> d; 258 | cout << "c + d = " << c + d << endl; 259 | ``` 260 | 261 | --- 262 | 263 | **参考资料** 264 | [cout格式化输出](http://c.biancheng.net/view/7578.html) 265 | [cin深入分析](https://blog.csdn.net/dongtingzhizi/article/details/2299358) 266 | 267 | --- 268 | 269 | 270 | 271 | # 数据类型 272 | 273 | --- 274 | 275 | 276 | 277 | **逻辑类型 + 引用类型 + 类类型** 278 | 279 | --- 280 | 281 | 282 | 283 | ## 逻辑类型 284 | 285 | --- 286 | 287 | ``` 288 | #include 289 | using namespace std; 290 | int main() 291 | { 292 | int i,j; 293 | bool a = true; 294 | bool b = false; 295 | cout << "a = " << a << ", " << "b = " << b << endl; 296 | 297 | i=a; 298 | j=b; 299 | cout << "i = " << i << ", " << "j = " << j << endl; 300 | 301 | a = !i; 302 | b = !j; 303 | cout << "a = " << a << ", " << "b = " << b << endl; 304 | return 0; 305 | } 306 | ``` 307 | 308 | --- 309 | 310 | 311 | 312 | ## 引用类型 313 | 314 | --- 315 | 316 | ### 独立引用 317 | 318 | 引用就是别名。 319 | 320 | ``` 321 | int number = 10; 322 | int & n = number; 323 | n = 20; 324 | cout << number << " " << n << endl; 325 | ``` 326 | 327 | --- 328 | 329 | **引用的初始化** 330 | 331 | 必须在定义的时候初始化,之后一直作为该变量的别名,不能再作为其它的变量的别名。 332 | 333 | ``` 334 | int a=1, b=2; 335 | int &n = a; 336 | n = b; 337 | n = 3; 338 | cout << a << " " << b << " " << endl; 339 | ``` 340 | 341 | 如何验证引用是另一个变量的别名? 342 | 343 | --- 344 | 345 | **程序调试** 346 | 347 | 设置编译选项:g++ -g test.cpp -o test.exe 348 | 349 | 启动调试器:gdb test.exe 350 | 351 | 执行与退出:run, quit …… 352 | 353 | 设置断点:break, info, del, enable, disable, watch …… 354 | 355 | 控制程序执行:next, step, continue, finish, set, call …… 356 | 357 | 查看程序状态:list, print, display, info, watch, backtrace …… 358 | 359 | --- 360 | 361 | 362 | 363 | ### 常引用 364 | 365 | --- 366 | 367 | 普通引用不能作为常量的别名。 368 | 369 | 常引用可以作为常量的别名。 370 | 371 | ``` 372 | const int & n = 1; 373 | ``` 374 | 或 375 | ``` 376 | int const & n = 1; 377 | ``` 378 | 379 | --- 380 | 381 | 常引用也可以作为变量的别名。 382 | 383 | ``` 384 | int a=1; 385 | const int & n = a; 386 | ``` 387 | 或 388 | ``` 389 | int a=1; 390 | int const & n = 1; 391 | ``` 392 | 393 | 无论怎样,常引用的值都不能修改。 394 | 395 | ``` 396 | n = 3; /* 错误 */ 397 | ``` 398 | 399 | --- 400 | 401 | **常量与宏** 402 | 403 | ``` 404 | #define PI 3.14 405 | 406 | int main() 407 | { 408 | float x, y; 409 | float const pi=3.14; 410 | 411 | x=PI; 412 | y=pi; 413 | cout << x << " " << y << endl; 414 | 415 | return 0; 416 | } 417 | 418 | ``` 419 | 420 | --- 421 | 422 | **常量与指针** 423 | 424 | ``` 425 | #define PI 3.14 426 | 427 | int main() 428 | { 429 | float x, y; 430 | float const pi=3.14; 431 | 432 | x=PI; 433 | y=pi; 434 | cout << x << " " << y << endl; 435 | 436 | ``` 437 | --- 438 | ``` 439 | 440 | float * const p1=&x; 441 | // p1=&y; 442 | // *p1=1.11; 443 | 444 | float const * p2=&x; 445 | // p2=&y; 446 | // *p2=1.11; 447 | 448 | float const * const p3=&x; 449 | // p3=&y; 450 | // *p3=1.11; 451 | 452 | cout << x << " " << y << endl; 453 | 454 | return 0; 455 | } 456 | ``` 457 | 458 | --- 459 | 460 | **几个容易混淆的概念** 461 | 462 | 指针常量:指针类型的常量(别乱指) 463 | 464 | 常量指针:指向常量的指针(别乱动) 465 | 466 | 指向常量的指针常量(别乱指 + 别乱动) 467 | 468 | --- 469 | 470 | 471 | 472 | ### 函数参数 473 | 474 | --- 475 | 476 | **普通变量作为参数** 477 | 478 | ``` 479 | void swap(int x, int y) 480 | { 481 | int tmp; 482 | tmp=x; 483 | x=y; 484 | y=tmp; 485 | } 486 | ``` 487 | 488 | --- 489 | 490 | **指针变量作为参数** 491 | 492 | ``` 493 | void swap(int * x, int * y) 494 | { 495 | int tmp; 496 | tmp=*x; 497 | *x=*y; 498 | *y=tmp; 499 | } 500 | ``` 501 | 502 | --- 503 | 504 | **引用作为参数** 505 | 506 | ``` 507 | void swap(int & x, int & y) 508 | { 509 | int tmp; 510 | tmp=x; 511 | x=y; 512 | y=tmp; 513 | } 514 | ``` 515 | 516 | 引用作为形参时,在函数调用时用实参对其进行初始化。 517 | 518 | --- 519 | 520 | 521 | 522 | ### 函数返回值 523 | 524 | --- 525 | 526 | **返回普通类型(右值)** 527 | 528 | ``` 529 | int x=1; 530 | int f() 531 | { 532 | x=2; 533 | return x; 534 | } 535 | int main() 536 | { 537 | int a=3; 538 | a = f(); 539 | cout << a << endl; 540 | } 541 | ``` 542 | 543 | 右值只能放在赋值符号的右边,因为它代表的仅仅是一个“值”。 544 | 545 | --- 546 | 547 | **返回引用(左值)** 548 | 549 | ``` 550 | int x=1; 551 | int & f() 552 | { 553 | x=2; 554 | return x; 555 | } 556 | int main() 557 | { 558 | int a=3; 559 | f() = a; 560 | cout << x << endl; 561 | } 562 | ``` 563 | 564 | 左值既可以放在赋值符号的左边,也可以放在赋值符号的右边,因为它代表的是一个“内存单元”。 565 | 566 | --- 567 | 568 | 569 | 570 | ## 内联函数 571 | 572 | --- 573 | 574 | ``` 575 | inline int add(int value) 576 | { 577 | int x; 578 | x = value + 1; 579 | x = x + 2; 580 | x = x + 3; 581 | return x; 582 | } 583 | 584 | int main() 585 | { 586 | int a,b; 587 | cin >> a; 588 | b=add(a); 589 | cout << b << endl; 590 | return 0; 591 | } 592 | ``` 593 | 594 | --- 595 | 596 | **内联与编译优化** 597 | 598 | inline仅仅是程序员对编译器提出的一个优化建议,是否采纳,还要看编译器自己。 599 | 600 | 即使不提这个建议,编译器也可能在适当的时候自动进行内联。 601 | 602 | 编译优化选项:`-O` 603 | 604 | ``` 605 | g++ -O test.cpp -o test.exe 606 | ``` 607 | 608 | --- 609 | 610 | 611 | 612 | ## 函数重载 613 | 614 | --- 615 | 616 | ``` 617 | int GetMax2(int x, int y) 618 | { 619 | if(x>y) 620 | return x; 621 | else 622 | return y; 623 | } 624 | 625 | int GetMax3(int x, int y, int z) 626 | { 627 | int tmp1,tmp2; 628 | tmp1=GetMax2(x,y); 629 | tmp2=GetMax2(tmp1,z); 630 | return tmp2; 631 | } 632 | 633 | int main() 634 | { 635 | return 0; 636 | } 637 | ``` 638 | 639 | --- 640 | 641 | 在编译的过程中,编译器会把函数名在原先的基础上做一些改变。 642 | 643 | C 编译器: 644 | 645 | ``` 646 | gcc test.c -o test.exe 647 | nm test.exe | find "GetMax" 648 | 004015c0 T _GetMax2 649 | 004015d5 T _GetMax3 650 | ``` 651 | 652 | C++ 编译器: 653 | 654 | ``` 655 | g++ test.cpp -o test.exe 656 | nm test.exe | find "GetMax" 657 | 004015c0 T __Z7GetMax2ii 658 | 004015d5 T __Z7GetMax3iii 659 | ``` 660 | --- 661 | 662 | C++ 编译器的修改方式,使得源代码中即使使用了两个完全相同的函数名,在编译的过程中也不会发生重名冲突,只要满足以下两个条件之一即可: 663 | 664 | 1. 参数个数不同 665 | 666 | 2. 参数类型不同 667 | 668 | --- 669 | 670 | 参数个数不同: 671 | 672 | ``` 673 | int GetMax(int x, int y) 674 | int GetMax(int x, int y, int z) 675 | ``` 676 | 677 | 修改后的函数名: 678 | 679 | ``` 680 | nm test.exe | find "GetMax" 681 | 004015c0 T __Z6GetMaxii 682 | 004015d5 T __Z6GetMaxiii 683 | ``` 684 | 685 | --- 686 | 687 | 参数类型不同: 688 | 689 | ``` 690 | int GetMax(int x, int y) 691 | int GetMax(float x, float y) 692 | ``` 693 | 694 | 修改后的函数名: 695 | 696 | ``` 697 | nm test.exe | find "GetMax" 698 | 004015d5 T __Z6GetMaxff 699 | 004015c0 T __Z6GetMaxii 700 | ``` 701 | 702 | --- 703 | 704 | 当存在形参个数或类型不同的同名函数,在函数调用时,编译器会根据实参的类型及个数的最佳匹配来选择调用哪一个函数。 705 | 706 | 707 | 通过调试器观察函数何时被调用: 708 | 709 | ``` 710 | (gdb) b *0x004015c0 711 | Breakpoint 3 at 0x4015c0: file test.cpp, line 5. 712 | (gdb) b *0x004015d5 713 | Breakpoint 4 at 0x4015d5: file test.cpp, line 13. 714 | ``` 715 | 716 | --- 717 | 718 | 719 | 720 | ## 默认形参值 721 | 722 | --- 723 | 724 | 可以在定义函数时给出默认的形参值。调用函数时如果给出了实参值,则使用给出的实参值;如果未给出实参值,则使用默认的形参值。 725 | 726 | ``` 727 | int register(int number, char const * name, int age=18, char const * country="China") 728 | { 729 | cout << "Number: " << number << endl; 730 | cout << "Name: " << name << endl; 731 | cout << "Age: " << age << endl; 732 | cout << "Country: " << country << endl << endl; 733 | return 0; 734 | } 735 | int main() 736 | { 737 | register(1,"ZhangSan"); 738 | register(2,"LiSi"); 739 | register(3,"WangWu", 20); 740 | register(4,"Tom", 18, "England"); 741 | return 0; 742 | } 743 | ``` 744 | 745 | --- 746 | 747 | **形参的顺序** 748 | 无默认值的形参在前,有默认值的形参在后。 749 | 750 | **注意** 751 | 本身不是函数重载,但可能会和函数重载相互影响。 752 | 编译器处理函数调用时,如果不能确定该调用哪个函数,则会出现编译错误;如果能够确定该调用哪个函数,则不会出现编译错误。 753 | 754 | --- 755 | 756 | ``` 757 | int register(int number, char const * name, int age=18, char const * country="China") 758 | { 759 | cout << "Number: " << number << endl; 760 | cout << "Name: " << name << endl; 761 | cout << "Age: " << age << endl; 762 | cout << "Country: " << country << endl << endl; 763 | return 0; 764 | } 765 | 766 | int register(int number, char const * name) 767 | { 768 | cout << "Number: " << number << endl; 769 | cout << "Name: " << name << endl; 770 | cout << "Age: " << 18 << endl; 771 | cout << "Country: " << "China" << endl << endl; 772 | return 0; 773 | } 774 | 775 | ``` 776 | --- 777 | ``` 778 | 779 | int main() 780 | { 781 | register(1,"ZhangSan"); /* error */ 782 | register(2,"LiSi"); /* error */ 783 | register(3,"WangWu", 20); 784 | register(4,"Tom", 18, "England"); 785 | return 0; 786 | } 787 | ``` 788 | 789 | --- 790 | 791 | 792 | 793 | ## 动态内存管理 794 | 795 | --- 796 | 797 | C语言的动态内存分配和释放使用**函数**:malloc()、free()、…… 798 | 799 | 它们来自标准函数库。 800 | 801 | ``` 802 | #include 803 | 804 | int main() 805 | { 806 | int * a; 807 | a = malloc(sizeof(int)); 808 | free(a); 809 | return 0; 810 | } 811 | ``` 812 | 813 | --- 814 | 815 | C++语言的动态内存分配和释放使用**运算符**:new、delete 816 | 817 | 它们是语言的组成部分。 818 | 819 | ``` 820 | int main() 821 | { 822 | int * a; 823 | a = new int; 824 | delete a; 825 | return 0; 826 | } 827 | ``` 828 | 829 | --- 830 | 831 | **分配和释放一个变量的空间** 832 | 833 | type *p; p=new type; delete p; 834 | 835 | ``` 836 | int main() 837 | { 838 | int * a; 839 | a = new int; 840 | *a = 123; 841 | cout << *a; 842 | delete a; 843 | return 0; 844 | } 845 | ``` 846 | 847 | --- 848 | 849 | **分配和释放一个变量的空间(初始化)** 850 | 851 | type *p; p=new type(x); delete p; 852 | 853 | 854 | ``` 855 | int main() 856 | { 857 | int * a; 858 | a = new int(456); 859 | cout << *a; 860 | delete a; 861 | return 0; 862 | } 863 | ``` 864 | 865 | --- 866 | 867 | **分配和释放一个数组的空间** 868 | 869 | type *p; p=new type[x]; delete []p; 870 | 871 | ``` 872 | #include 873 | #include 874 | using namespace std; 875 | 876 | int main() 877 | { 878 | char * a; 879 | a = new char[10]; 880 | strcpy(a,"hello"); 881 | cout << a; 882 | delete []a; 883 | return 0; 884 | } 885 | ``` 886 | 887 | --- 888 | 889 | **分配和释放一个数组的空间(初始化)** 890 | 891 | type *p; p=new type[x]{i,j,k,...}; delete []p; 892 | 893 | ``` 894 | #include 895 | using namespace std; 896 | 897 | int main() 898 | { 899 | char * a; 900 | a = new char[10]{'h','e', 'l', 'l', 'o'}; 901 | cout << a; 902 | delete []a; 903 | return 0; 904 | } 905 | ``` 906 | 907 | --- 908 | 909 | **new与delete必须配合使用** 910 | 911 | 未delete或重复delete,都可能导致程序崩溃。 912 | 913 | 这与操作系统的内存管理方式紧密相关。 914 | 915 | ``` 916 | #include 917 | using namespace std; 918 | 919 | void func(int i) 920 | { 921 | char *p; 922 | p=new char[1000000]; 923 | cout << i << endl; 924 | delete []p; 925 | } 926 | 927 | ``` 928 | --- 929 | ``` 930 | 931 | int main() 932 | { 933 | for(int i=1; i<=4000; i++){ 934 | func(i); 935 | } 936 | 937 | cout << "OK" << endl; 938 | return 0; 939 | } 940 | ``` 941 | 942 | --- 943 | 944 | 945 | # 课后复习 946 | 947 | --- 948 | 949 | **课程MOOC** 950 | www.icourse163.org/course/UESTC-1001774006?tid=1465243448 951 | 第七章 C++基础 952 | 953 | **码图作业** 954 | matu.uestc.edu.cn 955 | 班级:C++程序设计(2021) 956 | 第7章 作业1 957 | 第7章 作业2 958 | -------------------------------------------------------------------------------- /chapter-3.md: -------------------------------------------------------------------------------- 1 | --- 2 | marp: true 3 | theme: gaia 4 | --- 5 | 6 | 7 | 8 | # 继承 9 | 10 | --- 11 | 12 | 13 | 14 | ## 独立的类 15 | 16 | **animal-1.cpp** 17 | 18 | --- 19 | 20 | ## 基类和派生类 21 | 22 | 重用:name、my_name() 23 | 改造:say_hello() 24 | 扩充:birth、my_birth、color、my_color() 25 | 26 | 基类:animal 27 | 派生类:dog、cat 28 | 29 | **animal-2.cpp** 30 | 31 | --- 32 | 33 | 查看对象的内存空间分配情况: 34 | 35 | ``` 36 | (gdb) p a 37 | (gdb) p sizeof(a) 38 | (gdb) p &a 39 | (gdb) p &a.name 40 | ... 41 | (gdb) p a.my_name 42 | (gdb) p b.my_name 43 | (gdb) p c.my_name 44 | ... 45 | (gdb) p a.say_hello 46 | (gdb) p b.say_hello 47 | (gdb) p c.say_hello 48 | ... 49 | ``` 50 | 51 | --- 52 | 53 | 访问被覆盖的成员变量、成员函数: 54 | 55 | ``` 56 | b.say_hello(); 57 | b.dog::say_hello(); 58 | b.animal::say_hello(); 59 | 60 | c.say_hello(); 61 | c.cat::say_hello(); 62 | c.animal::say_hello(); 63 | ``` 64 | 65 | --- 66 | 67 | 68 | 69 | **构造函数、析构函数的调用顺序** 70 | 71 | **animal-3.cpp** 72 | 73 | --- 74 | 75 | ## 访问权限控制 76 | 77 | 能够继承,不代表能够访问。 78 | 79 | 以基类中的成员变量name为例,有3个位置可能对它进行访问。 80 | 81 | ``` 82 | char name[8]; 83 | ``` 84 | 85 | 1. 基类内部 86 | 87 | ``` 88 | void say_hello() 89 | { 90 | cout << "Hello" << name << endl; 91 | } 92 | ``` 93 | 94 | --- 95 | 96 | 2. 基类外部、派生类内部 97 | 98 | ``` 99 | void say_hello() 100 | { 101 | cout << "Wang Wang ... " << name << endl; 102 | } 103 | ``` 104 | 105 | 3. 基类外部、派生类外部 106 | 107 | ``` 108 | dog b("WangCai", 2020); 109 | cout << b.name; 110 | ``` 111 | 112 | --- 113 | 114 | 是否能访问成功,取决于两点: 115 | 116 | 1. 基类成员(public、protected、private) 117 | 118 | ``` 119 | public: 120 | char name[8]; 121 | ``` 122 | 123 | ``` 124 | protected: 125 | char name[8]; 126 | ``` 127 | 128 | ``` 129 | private: 130 | char name[8]; 131 | ``` 132 | 133 | --- 134 | 135 | 2. 继承方式(public、protected、private) 136 | 137 | ``` 138 | class dog : public animal { ... } 139 | ``` 140 | ``` 141 | class dog : protected animal { ... } 142 | ``` 143 | ``` 144 | class dog : private animal { ... } 145 | ``` 146 | 147 | --- 148 | 149 | 150 | 151 | $3 \times 3=9$ 152 | 153 | | 基类 | public成员 | protected成员 | private成员 | 154 | | ---- | ---- | ---- | ---- | 155 | | public继承 | public成员 |protected成员|不可访问成员| 156 | | protected继承 | protected成员 |protected成员|不可访问成员| 157 | | private继承 | private成员 |private成员|不可访问成员| 158 | 159 | --- 160 | 161 | ## 多继承 162 | 163 | 一个派生类可以有多个基类。 164 | 165 | **animal-4.cpp** 166 | 167 | ``` 168 | class shenshou : public dog, public cat 169 | { 170 | ... 171 | }; 172 | ``` 173 | 174 | ``` 175 | shenshou s("ShenShou", 2022, "GOLD"); 176 | s.my_birth(); 177 | s.my_color(); 178 | ``` 179 | 180 | --- 181 | 182 | 以下代码是否正确? 183 | 184 | ``` 185 | cout << s.name << endl; 186 | s.my_name(); 187 | ``` 188 | 189 | --- 190 | 191 | 改成以下代码: 192 | 193 | ``` 194 | cout << s.dog::name << endl; 195 | s.dog::my_name(); 196 | 197 | cout << s.cat::name << endl; 198 | s.cat::my_name(); 199 | ``` 200 | 201 | --- 202 | 203 | 查看对象的内存空间分配情况(name长度改为100): 204 | 205 | ``` 206 | (gdb) p s 207 | (gdb) p sizeof(s) 208 | ``` 209 | 210 | animal > dog 211 | animal > cat 212 | dog、cat > shenshou 213 | 214 | 如何更加优美的解决问题? 215 | 216 | --- 217 | 218 | ## 虚基类 219 | 220 | **animal-4.cpp** 221 | 222 | ``` 223 | class dog : public virtual animal 224 | class cat : public virtual animal 225 | class shenshou : public dog, public cat 226 | { 227 | ... 228 | }; 229 | ``` 230 | 231 | --- 232 | 233 | 以下代码是否正确? 234 | 235 | ``` 236 | shenshou s("ShenShou", 2022, "GOLD"); 237 | cout << s.name << endl; 238 | s.my_name(); 239 | ``` 240 | 241 | 以下代码是否正确? 242 | 243 | ``` 244 | s.say_hello(); 245 | ``` 246 | 247 | --- 248 | 249 | 改成以下代码: 250 | 251 | ``` 252 | s.animal::say_hello(); 253 | s.dog::say_hello(); 254 | s.cat::say_hello(); 255 | ``` 256 | 257 | 查看对象的内存空间分配情况(name长度改为100): 258 | 259 | ``` 260 | (gdb) p s 261 | (gdb) p sizeof(s) 262 | ``` 263 | -------------------------------------------------------------------------------- /chapter-4.md: -------------------------------------------------------------------------------- 1 | --- 2 | marp: true 3 | theme: gaia 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 | 进动物园之前: **zoo-1.cpp** 33 | 34 | 进动物园之后: **zoo-2.cpp** 35 | 36 | 注意:赋值兼容规则(P270) 37 | 38 | 问题:dog、cat都当作animal看待,缺乏个性。 39 | 40 | --- 41 | 42 | **解决方法1:静态多态** 43 | 44 | 基类不变;修改派生类,增加不同的成员函数。 45 | 46 | ``` 47 | say_hello_dog_version() { ... } 48 | 49 | say_hello_cat_version() { ... } 50 | ``` 51 | 52 | 改动太大,调用也不方便。 53 | 54 | --- 55 | 56 | **解决方法2:动态多态** 57 | 58 | 派生类不变;修改基类,把成员函数申明为虚函数。 59 | 60 | ``` 61 | virtual void say_hello() { ... } 62 | ``` 63 | 64 | 只需要这一点改动,即可解决问题。 65 | 66 | --- 67 | 68 | **幕后黑手 —— vtable** 69 | 70 | 在调试器中观察: 71 | 72 | ``` 73 | (gdb) p sizeof(a) 74 | (gdb) p sizeof(b) 75 | (gdb) p sizeof(c) 76 | ``` 77 | 78 | 疑点:多出的4字节是什么? 79 | 80 | --- 81 | 82 | 定位多出的4字节空间的地址: 83 | 84 | ``` 85 | (gdb) p &a 86 | (gdb) p &a.name 87 | (gdb) p &b 88 | (gdb) p &b.name 89 | (gdb) p &c 90 | (gdb) p &c.name 91 | ``` 92 | 93 | 查看该空间的值: 94 | 95 | ``` 96 | (gdb) p &a 97 | (gdb) p/x *(int *)&a 98 | (gdb) p a 99 | ``` 100 | 101 | --- 102 | 103 | 这个值是一个地址,查看该地址指向的空间的值: 104 | 105 | ``` 106 | (gdb) p *(*(int *)&a) 107 | (gdb) p/x *(*(int *)&a) 108 | ``` 109 | 110 | 对比每一个成员函数: 111 | 112 | ``` 113 | (gdb) p a.say_hello 114 | (gdb) p a.my_name 115 | ``` 116 | 117 | 可知它是成员函数my_name的地址 118 | 119 | --- 120 | 121 | 同样的方法,查看另外两个对象: 122 | 123 | ``` 124 | (gdb) p/x *(*(int *)&b) 125 | (gdb) p b.say_hello 126 | (gdb) p/x *(*(int *)&c) 127 | (gdb) p c.say_hello 128 | ``` 129 | 130 | 结论:该地址记录了各对象自己的虚函数的地址。 131 | 132 | --- 133 | 134 | **思考:** 135 | 136 | 当存在第2个、第3个、第4个……虚函数时,如何记录? 137 | 138 | **zoo-3.cpp** 139 | 140 | --- 141 | 142 | ``` 143 | (gdb) p a.say_hello 144 | (gdb) p a.my_food 145 | (gdb) p/x *(*(int *)&a) 146 | (gdb) p/x *(*(int *)&a + 4) 147 | 148 | (gdb) x *(int *)&a 149 | (gdb) x/2 *(int *)&a 150 | (gdb) x/2xw *(int *)&a 151 | (gdb) help x 152 | (gdb) help p 153 | ``` 154 | 155 | 结论:每个对象指向一段内存空间,用于记录该对象的所有虚函数的地址,这些地址构成了一个地址表,这个地址表就称为vtable。 156 | 157 | --- 158 | 159 | **思考:** 160 | 161 | 下例中有几个vtable?是否每个对象都有一个独立的vtable? 162 | 163 | ``` 164 | animal a("Animal"); 165 | dog b1("Dog1", 2021); 166 | dog b2("Dog2", 2022); 167 | cat c1("Cat1", "BLACK"); 168 | cat c2("Cat2", "WHITE"); 169 | 170 | animal *zoo[5]={&a, &b1, &b2, &c1, &c2}; 171 | for(int i=0; i<5; i++) 172 | { 173 | zoo[i]->say_hello(); 174 | } 175 | ``` 176 | 177 | --- 178 | 179 | **思考:** 180 | 181 | 问题1:animal能say_hello吗? 182 | 183 | 问题2:存在animal类型的对象吗? 184 | 185 | 直接删除say_hello,有什么问题? 186 | 187 | 解决方法:纯虚函数 188 | 189 | --- 190 | 191 | 192 | ## 纯虚函数 193 | 194 | --- 195 | 196 | **纯虚函数的定义** 197 | 198 | ``` 199 | virtual void say_hello() = 0; 200 | ``` 201 | 202 | **zoo-4.cpp** 203 | 204 | --- 205 | 206 | **抽象类:** 拥有纯虚函数的类称为抽象类。抽象类不能被实例化,只能作为其它类的基类。 207 | 208 | **接口类:** 当一个抽象类的所有成员函数都是纯虚函数时,这个抽象类又称为接口类。 209 | 210 | **抽象类的派生类:** 一个抽象类的派生类需要实现这个抽象类的全部纯虚函数,才能被实例化。否则,这个派生类仍然是抽象类,仍然不能实例化。 211 | 212 | --- 213 | 214 | 215 | ## 模板 216 | 217 | --- 218 | 219 | 模板:一种更加抽象的静态多态。 220 | 221 | 场景:多个函数,功能相似,参数类型不同。 222 | 223 | 解决方法:函数重载 224 | 225 | **addone-1.cpp** 226 | 227 | --- 228 | 229 | 利用强制类型转换,可少写几个函数,减少代码量。 230 | 231 | 删除第1、2、3个函数…… 232 | 233 | 删除第2、3、4个函数…… 234 | 235 | 删除第2、3个函数…… 236 | 237 | 删除第3个函数…… 238 | 239 | 都不够简化,也不够优雅。 240 | 241 | 解决方法:函数模板 242 | 243 | --- 244 | 245 | ### 函数模板 246 | 247 | **addone-2.cpp** 248 | 249 | 函数模板最终会被“实例化”为模板函数。 250 | 251 | 一个函数模板,会被“实例化”为多个模板函数 —— 多态。 252 | 253 | 一个函数模板“实例化”为多个模板函数的过程在编译时完成的,运行时不会改变。 —— 静态多态。 254 | 255 | --- 256 | 257 | 用nm观察函数模板实例化的结果。 258 | ``` 259 | nm test.exe | find "addone" 260 | 004027e0 T __Z6addoneIcET_S0_ 261 | 0040280c T __Z6addoneIdET_S0_ 262 | 0040283c T __Z6addoneIfET_S0_ 263 | 00402860 T __Z6addoneIiET_S0_ 264 | ``` 265 | 266 | 结论:一个函数模板“实例化”为多个模板函数的过程在编译时完成的。 267 | 268 | --- 269 | 270 | **函数模板 vs 函数重载** 271 | 272 | **addone-1.cpp** 273 | 274 | 函数重载:一个函数有多个实现 —— 多态。 275 | 函数与实现之间的对应在编译时完成,运行不会改变。 —— 静态多态。 276 | 用nm观察函数重载的结果。 277 | 278 | ``` 279 | nm test.exe | find "addone" 280 | 004015c0 T __Z6addonec 281 | 0040162e T __Z6addoned 282 | 0040160b T __Z6addonef 283 | 004015e9 T __Z6addonei 284 | ``` 285 | 286 | --- 287 | 288 | **总结** 289 | 290 | 如果把函数重载,看作你“手动”实现多个功能相似的函数,那么,可以把函数模板,看作由编译器“自动”帮你实现多个功能相似的函数。 291 | 292 | 函数模板也可以和函数重载配合使用,适应更加灵活的应用场景。 293 | 294 | --- 295 | 296 | ### 类模板 297 | 298 | 场景:多个类,功能相似,参数类型不同。 299 | 300 | 解决方法1:手工实现多个类(不够简化、不够优雅) 301 | 302 | 解决方法2:使用类模板,让编译器“自动”帮你实现多个类 303 | 304 | **node.cpp** 305 | 306 | --- 307 | 308 | 用nm观察类模板实例化的结果。 309 | 310 | ``` 311 | nm test.exe | find "node" 312 | 004028a0 T __ZN4nodeIfE6appendEPS0_ 313 | 004028e4 T __ZN4nodeIfE8get_nextEv 314 | 004028f8 T __ZN4nodeIfE9get_valueEv 315 | 00402908 T __ZN4nodeIfE9set_valueEf 316 | 00402920 T __ZN4nodeIfEC1Ev 317 | 00402940 T __ZN4nodeIiE6appendEPS0_ 318 | 00402984 T __ZN4nodeIiE8get_nextEv 319 | 00402998 T __ZN4nodeIiE9get_valueEv 320 | 004029a8 T __ZN4nodeIiE9set_valueEi 321 | 004029c0 T __ZN4nodeIiEC1Ev 322 | ``` 323 | 324 | 每个类都拥有自己的构造函数和成员函数版本。 325 | 326 | --- 327 | 328 | **总结** 329 | 330 | 函数模板、类模板,都是由编译器“自动”帮你实现功能相似的代码。 331 | 332 | --- 333 | 334 | ### 标准模板库 335 | 336 | 泛型程序设计(Generic Programming)是一种程序设计风格, 337 | 338 | 泛型允许程序员在编写代码时使用一些以后才指定的数据类型。 339 | 340 | 《设计模式(Design Patterns)》一书将泛型称为: 341 | 342 | 参数化类型(Parameterized Type)。 343 | 344 | --- 345 | 346 | 很多程序设计语言和编译器都支持泛型, 347 | 348 | 泛型在不同的程序设计语言中有不同的叫法。 349 | 350 | Ada、Delphi、Eiffel、Java、C#、F#、Swift、Visual Basic .NET:泛型(Generics) 351 | 352 | ML、Scala、Haskell:参数多态(Parametric Polymorphism) 353 | 354 | C++、D:模板 355 | 356 | --- 357 | 358 | 使用泛型,可编写出不依赖于具体数据类型的程序, 359 | 360 | 从而将算法从特定的数据结构中抽象出来。 361 | 362 | 实现时:算法和数据结构分离,减少重复的代码量。 363 | 364 | 使用时:算法和数据结构组合,适应不同的应用场景。 365 | 366 | --- 367 | 368 | C++ 的STL(Standard Template Library) ,即标准模板库,是泛型程序设计最成功的应用案例。 369 | 370 | STL 是一个工业级的、高效的C++程序库。 371 | 372 | STL 它包含了诸多在计算机科学领域里所常用的基本数据结构(如数组、向量、链表、队列、栈、树)和算法(如排序、查找)。这些数据结构和算法组合在一起,为我们的软件开发提供了良好的支持。 373 | 374 | STL于1998年正式加入 C++ 标准。 375 | 376 | Bjarne Stroustrup的《The C++ Programming Language》一书中,有1/3以上的篇幅讲述STL。 377 | 378 | --- 379 | 380 | **进一步的学习** 381 | 382 | The C++ Programming Language(Bjarne Stroustrup) 383 | 384 | C++ Primer(Stanley B. Lippman) 385 | 386 | --- 387 | 388 | 389 | 390 | # 课后复习 391 | 392 | --- 393 | 394 | **课程MOOC** 395 | www.icourse163.org/course/UESTC-1001774006?tid=1465243448 396 | 第九章 继承、派生与多态 397 | 第十章 模板、命名空间和异常处理 398 | 399 | **码图作业** 400 | matu.uestc.edu.cn 401 | 班级:C++程序设计(2021) 402 | 第9章 作业1、作业2 403 | 第10章 作业1、作业2 404 | 405 | -------------------------------------------------------------------------------- /chapter-5.md: -------------------------------------------------------------------------------- 1 | # 实验 2 | 3 | ***实验题目*** 4 | 5 | 基于ACLLib库的精灵游戏 6 | 7 | ***ACLLib库*** 8 | 9 | https://github.com/wengkai/acllib 10 | 11 | ***示例代码*** 12 | 13 | clock、clocks、cube、snake、sprite 14 | 15 | ***实验要求*** 16 | 17 | 实验分为4个难度级别,任意选择其中一个级别,按要求完成实验。 18 | 19 | 1. 封装(满分:60分) 20 | 21 | 阅读、理解示例代码,了解示例代码的功能,发现示例代码中存在的问题,独立编写自己的程序代码,实现示例代码的功能,避免示例代码中出现的问题。重写的代码要体现出封装的思想。 22 | 23 | 示例代码: sprite\encapsulation 24 | 25 | 2. 封装+继承(满分:70分) 26 | 27 | 阅读、理解示例代码,了解示例代码的功能,发现示例代码中存在的问题,独立编写自己的程序代码,实现示例代码的功能,避免示例代码中出现的问题。重写的代码要体现出封装和继承的思想。 28 | 29 | 示例代码: sprite\inheritance 30 | 31 | 3. 封装+继承+多态(满分:80分) 32 | 33 | 阅读、理解示例代码,了解示例代码的功能,发现示例代码中存在的问题,独立编写自己的程序代码,实现示例代码的功能,避免示例代码中出现的问题。重写的代码要体现出封装、继承和多态的思想。 34 | 35 | 示例代码: sprite\polymorphism 36 | 37 | 4. 封装+继承+多态+个性化(满分:100分) 38 | 39 | 在第3级的基础上,设计并实现更多的个性化功能,提高游戏的可玩性。 40 | 41 | ***实验评分*** 42 | 43 | 提交内容:实验报告、程序代码 44 | 45 | 提交时间:第16周周日晚24:00前 46 | 47 | 提交方式:实验报告和全部程序代码一起打成一个zip包,文件名为:学号_姓名.zip(如:123456_张三.zip),用飞书客户端发送给助教。实验报告模板见《实验报告模板.docx》。程序代码包含全部源码文件和一个可执行文件。 48 | 49 | 评分标准:见《实验报告模板.docx》 50 | 51 | ***注意事项*** 52 | 53 | 鼓励阅读、理解、学习、借鉴好的代码的思路和方法,但自己的实验代码要自己独立编写。禁止拷贝、粘贴别人的代码。如发现拷贝、粘贴别人的代码的现象,一律扣除相关部分的得分。 54 | -------------------------------------------------------------------------------- /clock/acllib.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MisterFishing/cpp/1eb363fc03a1119d67808dfed624fb75bbc337ba/clock/acllib.c -------------------------------------------------------------------------------- /clock/acllib.h: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | //////////////////////////////////////////////////////////////// 17 | // ACLLib - Advanced C Lab Library 18 | // Ver 2014-07 19 | // For students' Lab at Zhejiang University 20 | // Created 2008 by Gao Yuan 21 | // Modified 2009 by Cui Liwei 22 | // 2010 by Lan Huidong 23 | // Revised 2012 by Li Rui 24 | // Modified 2014 by Weng Kai for MOOC 25 | //////////////////////////////////////////////////////////////// 26 | 27 | /* 28 | For Dev C++, these lib files need to be added into linker options. 29 | Be sure to change the leading folders as your installation. 30 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libwinmm.a" 31 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libmsimg32.a" 32 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libkernel32.a" 33 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libuser32.a" 34 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libgdi32.a" 35 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libole32.a" 36 | "C:/Program Files/Dev-Cpp/MinGW32/lib/liboleaut32.a" 37 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libuuid.a" 38 | */ 39 | 40 | #ifndef __ACLLIB_H__ 41 | #define __ACLLIB_H__ 42 | 43 | #ifdef _UNICODE 44 | #undef _UNICODE 45 | #endif 46 | #ifdef UNICODE 47 | #undef UNICODE 48 | #endif 49 | 50 | #define WINVER 0x0501 51 | #include 52 | 53 | #define BLACK RGB(0, 0, 0) 54 | #define RED RGB(255, 0, 0) 55 | #define GREEN RGB(0, 255, 0) 56 | #define BLUE RGB(0, 0, 255) 57 | #define CYAN RGB(0, 255, 255) 58 | #define MAGENTA RGB(255, 0, 255) 59 | #define YELLOW RGB(255, 255, 0) 60 | #define WHITE RGB(255, 255, 255) 61 | 62 | #define EMPTY 0xffffffff 63 | #define DEFAULT -1 64 | 65 | typedef enum 66 | { 67 | PEN_STYLE_SOLID, 68 | PEN_STYLE_DASH, /* ------- */ 69 | PEN_STYLE_DOT, /* ....... */ 70 | PEN_STYLE_DASHDOT, /* _._._._ */ 71 | PEN_STYLE_DASHDOTDOT, /* _.._.._ */ 72 | PEN_STYLE_NULL 73 | } ACL_Pen_Style; 74 | 75 | typedef enum 76 | { 77 | BRUSH_STYLE_SOLID = -1, 78 | BRUSH_STYLE_HORIZONTAL, /* ----- */ 79 | BRUSH_STYLE_VERTICAL, /* ||||| */ 80 | BRUSH_STYLE_FDIAGONAL, /* \\\\\ */ 81 | BRUSH_STYLE_BDIAGONAL, /* ///// */ 82 | BRUSH_STYLE_CROSS, /* +++++ */ 83 | BRUSH_STYLE_DIAGCROSS, /* xxxxx */ 84 | BRUSH_STYLE_NULL 85 | } ACL_Brush_Style; 86 | 87 | typedef enum 88 | { 89 | NO_BUTTON = 0, 90 | LEFT_BUTTON, 91 | MIDDLE_BUTTON, 92 | RIGHT_BUTTON 93 | } ACL_Mouse_Button; 94 | 95 | typedef enum 96 | { 97 | BUTTON_DOWN, 98 | BUTTON_DOUBLECLICK, 99 | BUTTON_UP, 100 | ROLL_UP, 101 | ROLL_DOWN, 102 | MOUSEMOVE 103 | } ACL_Mouse_Event; 104 | 105 | typedef enum 106 | { 107 | KEY_DOWN, 108 | KEY_UP 109 | } ACL_Keyboard_Event; 110 | 111 | typedef struct 112 | { 113 | HBITMAP hbitmap; 114 | int width; 115 | int height; 116 | } ACL_Image; 117 | 118 | //typedef enum 119 | //{ 120 | // TM_NO = 0x00, 121 | // TM_COLOR = 0x01, 122 | // TM_ALPHA = 0x02 123 | //} ACL_TransparentMode; 124 | 125 | typedef COLORREF ACL_Color; 126 | typedef int ACL_Sound; 127 | 128 | typedef void(*KeyboardEventCallback) (int key, int event); 129 | typedef void(*CharEventCallback) (char c); 130 | typedef void(*MouseEventCallback) (int x, int y, int button, int event); 131 | typedef void(*TimerEventCallback) (int timerID); 132 | 133 | #ifdef __cplusplus 134 | extern "C" { 135 | #endif 136 | 137 | int Setup(void); 138 | 139 | // 140 | void initWindow(const char title[], int left, int top, int width, int height); 141 | void msgBox(const char title[], const char text[], int flag); 142 | 143 | void registerKeyboardEvent(KeyboardEventCallback callback); 144 | void registerCharEvent(CharEventCallback callback); 145 | void registerMouseEvent(MouseEventCallback callback); 146 | void registerTimerEvent(TimerEventCallback callback); 147 | 148 | void startTimer(int timerID, int timeinterval); 149 | void cancelTimer(int timerID); 150 | 151 | // Sound 152 | void loadSound(const char *fileName, ACL_Sound *pSound); 153 | void playSound(ACL_Sound soundID, int repeat); 154 | void stopSound(ACL_Sound soundID); 155 | 156 | // Paint 157 | void beginPaint(); 158 | void endPaint(); 159 | void clearDevice(void); 160 | int getWidth(); 161 | int getHeight(); 162 | 163 | // Pen 164 | void setPenColor(ACL_Color color); 165 | void setPenWidth(int width); 166 | void setPenStyle(ACL_Pen_Style style); 167 | 168 | // Brush 169 | void setBrushColor(ACL_Color color); 170 | void setBrushStyle(ACL_Brush_Style style); 171 | 172 | // Text 173 | void setTextColor(ACL_Color color); 174 | void setTextBkColor(ACL_Color color); 175 | void setTextSize(int size); 176 | void setTextFont(const char *pFontName); 177 | 178 | void paintText(int x, int y, const char *pStr); 179 | 180 | void setCaretSize(int w, int h); 181 | void setCaretPos(int x, int y); 182 | void showCaret(); 183 | void hideCaret(); 184 | 185 | // Pixel 186 | void putPixel(int x, int y, ACL_Color color); 187 | ACL_Color getPixel(int x, int y); 188 | 189 | // the Point 190 | int getX(void); 191 | int getY(void); 192 | void moveTo(int x, int y); 193 | void moveRel(int dx, int dy); 194 | 195 | // Lines and Curves 196 | void arc(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, \ 197 | int nXStartArc, int nYStartArc, int nXEndArc, int nYEndArc); 198 | void line(int x0, int y0, int x1, int y1); 199 | void lineTo(int nXEnd, int nYEnd); 200 | void lineRel(int dx, int dy); 201 | void polyBezier(const POINT *lppt, int cPoints); 202 | void polyLine(const POINT *lppt, int cPoints); 203 | 204 | // Filled Shapes 205 | void chrod(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, \ 206 | int nXRadial1, int nYRadial1, int nXRadial2, int nYRadial2); 207 | void ellipse(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect); 208 | void pie(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, \ 209 | int nXRadial1, int nYRadial1, int nXRadial2, int nYRadial2); 210 | void polygon(const POINT *lpPoints, int nCount); 211 | void rectangle(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect); 212 | void roundrect(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, \ 213 | int nWidth, int nHeight); 214 | 215 | // Image 216 | void loadImage(const char *pImageFileName, ACL_Image *pImage); 217 | void freeImage(ACL_Image *pImage); 218 | 219 | void putImage(ACL_Image *pImage, int x, int y); 220 | void putImageScale(ACL_Image *pImage, int x, int y, int width, int height); 221 | void putImageTransparent(ACL_Image *pImage, int x, int y, int width, int height, ACL_Color bkColor); 222 | 223 | //void putImageEx(ACL_Image *pImage,int dx,int dy,int dw,int dh, 224 | // int sx,int sy,int sw,int sh); 225 | 226 | //void setTransparentMode(ACL_TransparenetMode); 227 | //void setTransparentColor(ACL_Color); 228 | //void setTransparetnAlpha(int alpha); 229 | 230 | void initConsole(void); 231 | 232 | #ifdef __cplusplus 233 | } 234 | #endif 235 | 236 | #endif 237 | -------------------------------------------------------------------------------- /clock/clock.c: -------------------------------------------------------------------------------- 1 | #include "acllib.h" 2 | 3 | #include 4 | #include 5 | 6 | #define RAD(x) ((x)/360.0*2*3.1415926535) 7 | 8 | int h = 0; 9 | int m = 0; 10 | int s = 0; 11 | 12 | void paint() 13 | { 14 | static const int ox = 150; 15 | static const int oy = 150; 16 | 17 | static const int hl = 46; 18 | static const int ml = 74; 19 | static const int sl = 120; 20 | 21 | int i; 22 | 23 | beginPaint(); 24 | 25 | clearDevice(); 26 | 27 | // circle 28 | setPenWidth(2); 29 | setPenColor(BLACK); 30 | setBrushColor(WHITE); 31 | ellipse(25,25,275,275); 32 | 33 | // label 34 | setPenWidth(1); 35 | setPenColor(BLACK); 36 | for(i=0;i<12;++i) 37 | { 38 | moveTo(ox+115*sin(RAD(180-i*30)),oy+115*cos(RAD(180-i*30))); 39 | lineTo(ox+125*sin(RAD(180-i*30)),oy+125*cos(RAD(180-i*30))); 40 | } 41 | 42 | // hour 43 | setPenWidth(8); 44 | setPenColor(BLACK); 45 | moveTo(ox,oy); 46 | lineTo(ox+hl*sin(RAD(180-h*30)),oy+hl*cos(RAD(180-h*30))); 47 | 48 | // minute 49 | setPenWidth(4); 50 | setPenColor(GREEN); 51 | moveTo(ox,oy); 52 | lineTo(ox+ml*sin(RAD(180-m*6)),oy+ml*cos(RAD(180-m*6))); 53 | 54 | // second 55 | setPenWidth(2); 56 | setPenColor(RED); 57 | moveTo(ox,oy); 58 | lineTo(ox+sl*sin(RAD(180-s*6)),oy+sl*cos(RAD(180-s*6))); 59 | 60 | endPaint(); 61 | } 62 | 63 | void timerEvent(int tid) 64 | { 65 | ++s; 66 | if(s==60) 67 | { 68 | s = 0; 69 | ++m; 70 | } 71 | if(m==60) 72 | { 73 | m = 0; 74 | ++h; 75 | } 76 | if(h==12) 77 | h = 0; 78 | paint(); 79 | } 80 | 81 | int Setup() 82 | { 83 | initWindow("Clock",DEFAULT,DEFAULT,300,300); 84 | 85 | registerTimerEvent(timerEvent); 86 | 87 | startTimer(0,1000); 88 | //startTimer(0,100); 89 | 90 | 91 | return 0; 92 | } 93 | -------------------------------------------------------------------------------- /clock/clock.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MisterFishing/cpp/1eb363fc03a1119d67808dfed624fb75bbc337ba/clock/clock.exe -------------------------------------------------------------------------------- /clock/readme.md: -------------------------------------------------------------------------------- 1 | gcc *.c -lgdi32 -lole32 -loleaut32 -luuid -lwinmm -lmsimg32 -o clock.exe -------------------------------------------------------------------------------- /clocks/Clock.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include"Clock.h" 3 | #include"acllib.h" 4 | #include"time.h" 5 | #include"math.h" 6 | using namespace std; 7 | 8 | //角度转换为弧度 9 | double RAD(double x) 10 | { 11 | return ((x)/360.0*2*3.1415926535); 12 | } 13 | 14 | Clock::Clock(void) 15 | { 16 | time_t t; 17 | struct tm *tmm; 18 | t = time(NULL); 19 | tmm=localtime(&t); 20 | h=tmm->tm_hour; 21 | m=tmm->tm_min; 22 | s=tmm->tm_sec; 23 | } 24 | 25 | Clock::~Clock(void) 26 | { 27 | } 28 | 29 | //根据指定时间初始化时钟 30 | Clock::Clock(int h,int m,int s) 31 | { 32 | this->h=h; 33 | this->m=m; 34 | this->s=s; 35 | } 36 | 37 | //根据已经存在的时钟初始化该时钟 38 | Clock::Clock(Clock &c) 39 | { 40 | h=c.h; 41 | m=c.m; 42 | s=c.s; 43 | } 44 | 45 | //设置时间 46 | void Clock::SetTime(int h,int m,int s) 47 | { 48 | this->h=h; 49 | this->m=m; 50 | this->s=s; 51 | } 52 | 53 | //获取时间 54 | void Clock::GetTime(int &h,int &m,int &s) 55 | { 56 | h=this->h; 57 | m=this->m; 58 | s=this->s; 59 | } 60 | 61 | void Clock::AddOneS() 62 | { 63 | s++; 64 | if(s>=60) 65 | { 66 | m++; 67 | s=0; 68 | if(m>=60) 69 | { 70 | m=0; 71 | h++; 72 | if(h>=12) 73 | { 74 | h=0; 75 | } 76 | } 77 | } 78 | } 79 | 80 | -------------------------------------------------------------------------------- /clocks/Clock.h: -------------------------------------------------------------------------------- 1 | #ifndef __CLOCK_H__ 2 | #define __CLOCK_H__ 3 | 4 | double RAD(double x); 5 | 6 | class Clock 7 | { 8 | private: 9 | int h,m,s,z; 10 | 11 | public: 12 | Clock(void); 13 | //根据指定时间初始化时钟 14 | Clock(int h,int m,int s); 15 | //根据已经存在的时钟初始化该时钟 16 | Clock(Clock &); 17 | ~Clock(void); 18 | //设置时间 19 | void SetTime(int h,int m,int s); 20 | //获取时间 21 | void GetTime(int &h,int &m,int &s); 22 | //时间增加1秒 23 | void AddOneS(); 24 | }; 25 | 26 | #endif 27 | 28 | -------------------------------------------------------------------------------- /clocks/EllipseClock.cpp: -------------------------------------------------------------------------------- 1 | #include "EllipseClock.h" 2 | #include "acllib.h" 3 | #include "math.h" 4 | 5 | EllipseClock::EllipseClock(int x,int y,int width,int height):Clock(),CShape(x,y,width,height) 6 | { 7 | 8 | } 9 | EllipseClock::EllipseClock(int x,int y,int width,int height,int h,int m,int s):Clock(h,m,s),CShape(x,y,width,height) 10 | { 11 | } 12 | 13 | EllipseClock::~EllipseClock(void) 14 | { 15 | } 16 | void EllipseClock::Draw() 17 | { 18 | int h,m,s; 19 | int x,y,width,height; 20 | GetShapeData(x,y,width,height); 21 | int ox =x+ width/2;//150; 22 | int oy =y+ height/2;//150; 23 | 24 | int min=width=0 && jiaodu<45)||(jiaodu>=315 && jiaodu<360)){ 18 | x=x0+r*tan(RAD(jiaodu)); 19 | y=y0-r; 20 | } 21 | else if(jiaodu>=45 && jiaodu<135){ 22 | x=x0+r; 23 | y=y0+r/tan(RAD(jiaodu)); 24 | } 25 | else if(jiaodu>=135 && jiaodu<225){ 26 | x=x0+r*tan(RAD(jiaodu)); 27 | y=y0+r; 28 | } 29 | else if(jiaodu>=225 && jiaodu<315){ 30 | x=x0-r; 31 | y=y0+r/tan(RAD(jiaodu)); 32 | } 33 | } 34 | 35 | 36 | void RectangleClock::Draw() 37 | { 38 | int h,m,s; 39 | int x,y,width,height; 40 | GetShapeData(x,y,width,height); 41 | int ox =x+ width/2;//150; 42 | int oy =y+ height/2;//150; 43 | 44 | int min=widthx=x; 6 | this->y=y; 7 | this->width=w; 8 | this->height=h; 9 | } 10 | 11 | CShape::~CShape(void) 12 | { 13 | } 14 | void CShape::GetShapeData(int &x,int &y,int &width,int &height) 15 | { 16 | x=this->x; 17 | y=this->y; 18 | width=this->width; 19 | height=this->height; 20 | } 21 | -------------------------------------------------------------------------------- /clocks/Shape.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class CShape 4 | { 5 | int x,y,width,height; 6 | public: 7 | CShape(int x,int y,int w,int h); 8 | ~CShape(void); 9 | void GetShapeData(int &x,int &y,int &width,int &height); 10 | void Draw(){}; 11 | }; 12 | -------------------------------------------------------------------------------- /clocks/acllib.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MisterFishing/cpp/1eb363fc03a1119d67808dfed624fb75bbc337ba/clocks/acllib.c -------------------------------------------------------------------------------- /clocks/acllib.h: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | //////////////////////////////////////////////////////////////// 17 | // ACLLib - Advanced C Lab Library 18 | // Ver 2014-07 19 | // For students' Lab at Zhejiang University 20 | // Created 2008 by Gao Yuan 21 | // Modified 2009 by Cui Liwei 22 | // 2010 by Lan Huidong 23 | // Revised 2012 by Li Rui 24 | // Modified 2014 by Weng Kai for MOOC 25 | //////////////////////////////////////////////////////////////// 26 | 27 | /* 28 | For Dev C++, these lib files need to be added into linker options. 29 | Be sure to change the leading folders as your installation. 30 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libwinmm.a" 31 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libmsimg32.a" 32 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libkernel32.a" 33 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libuser32.a" 34 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libgdi32.a" 35 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libole32.a" 36 | "C:/Program Files/Dev-Cpp/MinGW32/lib/liboleaut32.a" 37 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libuuid.a" 38 | */ 39 | 40 | #ifndef __ACLLIB_H__ 41 | #define __ACLLIB_H__ 42 | 43 | #ifdef _UNICODE 44 | #undef _UNICODE 45 | #endif 46 | #ifdef UNICODE 47 | #undef UNICODE 48 | #endif 49 | 50 | #include 51 | 52 | #define BLACK RGB(0, 0, 0) 53 | #define RED RGB(255, 0, 0) 54 | #define GREEN RGB(0, 255, 0) 55 | #define BLUE RGB(0, 0, 255) 56 | #define CYAN RGB(0, 255, 255) 57 | #define MAGENTA RGB(255, 0, 255) 58 | #define YELLOW RGB(255, 255, 0) 59 | #define WHITE RGB(255, 255, 255) 60 | 61 | #define EMPTY 0xffffffff 62 | #define DEFAULT -1 63 | 64 | typedef enum 65 | { 66 | PEN_STYLE_SOLID, 67 | PEN_STYLE_DASH, /* ------- */ 68 | PEN_STYLE_DOT, /* ....... */ 69 | PEN_STYLE_DASHDOT, /* _._._._ */ 70 | PEN_STYLE_DASHDOTDOT, /* _.._.._ */ 71 | PEN_STYLE_NULL 72 | } ACL_Pen_Style; 73 | 74 | typedef enum 75 | { 76 | BRUSH_STYLE_SOLID = -1, 77 | BRUSH_STYLE_HORIZONTAL, /* ----- */ 78 | BRUSH_STYLE_VERTICAL, /* ||||| */ 79 | BRUSH_STYLE_FDIAGONAL, /* \\\\\ */ 80 | BRUSH_STYLE_BDIAGONAL, /* ///// */ 81 | BRUSH_STYLE_CROSS, /* +++++ */ 82 | BRUSH_STYLE_DIAGCROSS, /* xxxxx */ 83 | BRUSH_STYLE_NULL 84 | } ACL_Brush_Style; 85 | 86 | typedef enum 87 | { 88 | NO_BUTTON = 0, 89 | LEFT_BUTTON, 90 | MIDDLE_BUTTON, 91 | RIGHT_BUTTON 92 | } ACL_Mouse_Button; 93 | 94 | typedef enum 95 | { 96 | BUTTON_DOWN, 97 | BUTTON_DOUBLECLICK, 98 | BUTTON_UP, 99 | ROLL_UP, 100 | ROLL_DOWN, 101 | MOUSEMOVE 102 | } ACL_Mouse_Event; 103 | 104 | typedef enum 105 | { 106 | KEY_DOWN, 107 | KEY_UP 108 | } ACL_Keyboard_Event; 109 | 110 | typedef struct 111 | { 112 | HBITMAP hbitmap; 113 | int width; 114 | int height; 115 | } ACL_Image; 116 | 117 | //typedef enum 118 | //{ 119 | // TM_NO = 0x00, 120 | // TM_COLOR = 0x01, 121 | // TM_ALPHA = 0x02 122 | //} ACL_TransparentMode; 123 | 124 | typedef COLORREF ACL_Color; 125 | typedef int ACL_Sound; 126 | 127 | typedef void(*KeyboardEventCallback) (int key, int event); 128 | typedef void(*CharEventCallback) (char c); 129 | typedef void(*MouseEventCallback) (int x, int y, int button, int event); 130 | typedef void(*TimerEventCallback) (int timerID); 131 | 132 | #ifdef __cplusplus 133 | extern "C" { 134 | #endif 135 | 136 | int Setup(void); 137 | 138 | // 139 | void initWindow(const char title[], int left, int top, int width, int height); 140 | void msgBox(const char title[], const char text[], int flag); 141 | 142 | void registerKeyboardEvent(KeyboardEventCallback callback); 143 | void registerCharEvent(CharEventCallback callback); 144 | void registerMouseEvent(MouseEventCallback callback); 145 | void registerTimerEvent(TimerEventCallback callback); 146 | 147 | void startTimer(int timerID, int timeinterval); 148 | void cancelTimer(int timerID); 149 | 150 | // Sound 151 | void loadSound(const char *fileName, ACL_Sound *pSound); 152 | void playSound(ACL_Sound soundID, int repeat); 153 | void stopSound(ACL_Sound soundID); 154 | 155 | // Paint 156 | void beginPaint(); 157 | void endPaint(); 158 | void clearDevice(void); 159 | int getWidth(); 160 | int getHeight(); 161 | 162 | // Pen 163 | void setPenColor(ACL_Color color); 164 | void setPenWidth(int width); 165 | void setPenStyle(ACL_Pen_Style style); 166 | 167 | // Brush 168 | void setBrushColor(ACL_Color color); 169 | void setBrushStyle(ACL_Brush_Style style); 170 | 171 | // Text 172 | void setTextColor(ACL_Color color); 173 | void setTextBkColor(ACL_Color color); 174 | void setTextSize(int size); 175 | void setTextFont(const char *pFontName); 176 | 177 | void paintText(int x, int y, const char *pStr); 178 | 179 | void setCaretSize(int w, int h); 180 | void setCaretPos(int x, int y); 181 | void showCaret(); 182 | void hideCaret(); 183 | 184 | // Pixel 185 | void putPixel(int x, int y, ACL_Color color); 186 | ACL_Color getPixel(int x, int y); 187 | 188 | // the Point 189 | int getX(void); 190 | int getY(void); 191 | void moveTo(int x, int y); 192 | void moveRel(int dx, int dy); 193 | 194 | // Lines and Curves 195 | void arc(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, \ 196 | int nXStartArc, int nYStartArc, int nXEndArc, int nYEndArc); 197 | void line(int x0, int y0, int x1, int y1); 198 | void lineTo(int nXEnd, int nYEnd); 199 | void lineRel(int dx, int dy); 200 | void polyBezier(const POINT *lppt, int cPoints); 201 | void polyLine(const POINT *lppt, int cPoints); 202 | 203 | // Filled Shapes 204 | void chrod(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, \ 205 | int nXRadial1, int nYRadial1, int nXRadial2, int nYRadial2); 206 | void ellipse(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect); 207 | void pie(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, \ 208 | int nXRadial1, int nYRadial1, int nXRadial2, int nYRadial2); 209 | void polygon(const POINT *lpPoints, int nCount); 210 | void rectangle(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect); 211 | void roundrect(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, \ 212 | int nWidth, int nHeight); 213 | 214 | // Image 215 | void loadImage(const char *pImageFileName, ACL_Image *pImage); 216 | void freeImage(ACL_Image *pImage); 217 | 218 | void putImage(ACL_Image *pImage, int x, int y); 219 | void putImageScale(ACL_Image *pImage, int x, int y, int width, int height); 220 | void putImageTransparent(ACL_Image *pImage, int x, int y, int width, int height, ACL_Color bkColor); 221 | 222 | //void putImageEx(ACL_Image *pImage,int dx,int dy,int dw,int dh, 223 | // int sx,int sy,int sw,int sh); 224 | 225 | //void setTransparentMode(ACL_TransparenetMode); 226 | //void setTransparentColor(ACL_Color); 227 | //void setTransparetnAlpha(int alpha); 228 | 229 | void initConsole(void); 230 | 231 | #ifdef __cplusplus 232 | } 233 | #endif 234 | 235 | #endif 236 | -------------------------------------------------------------------------------- /clocks/clocks.cpp: -------------------------------------------------------------------------------- 1 | #include "acllib.h" 2 | #include "EllipseClock.h" 3 | #include "RectangleClock.h" 4 | #include 5 | #include 6 | 7 | EllipseClock c1(430,30,250,250); 8 | RectangleClock c2(30,30,250,250); 9 | 10 | void timerEvent(int tid) 11 | { 12 | beginPaint(); 13 | clearDevice(); 14 | c2.AddOneS(); 15 | c2.Draw(); 16 | c1.AddOneS(); 17 | c1.Draw(); 18 | endPaint(); 19 | } 20 | 21 | int Setup() 22 | { 23 | initWindow("Clock",DEFAULT,DEFAULT,800,300); 24 | registerTimerEvent(timerEvent); 25 | startTimer(0,1000); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /clocks/clocks.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MisterFishing/cpp/1eb363fc03a1119d67808dfed624fb75bbc337ba/clocks/clocks.exe -------------------------------------------------------------------------------- /clocks/readme.md: -------------------------------------------------------------------------------- 1 | gcc -c *.c 2 | 3 | g++ -c *.cpp 4 | 5 | g++ *.o -lgdi32 -lole32 -loleaut32 -luuid -lwinmm -lmsimg32 -o clocks.exe 6 | -------------------------------------------------------------------------------- /cube/acllib.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MisterFishing/cpp/1eb363fc03a1119d67808dfed624fb75bbc337ba/cube/acllib.c -------------------------------------------------------------------------------- /cube/acllib.h: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | //////////////////////////////////////////////////////////////// 17 | // ACLLib - Advanced C Lab Library 18 | // Ver 2014-07 19 | // For students' Lab at Zhejiang University 20 | // Created 2008 by Gao Yuan 21 | // Modified 2009 by Cui Liwei 22 | // 2010 by Lan Huidong 23 | // Revised 2012 by Li Rui 24 | // Modified 2014 by Weng Kai for MOOC 25 | //////////////////////////////////////////////////////////////// 26 | 27 | /* 28 | For Dev C++, these lib files need to be added into linker options. 29 | Be sure to change the leading folders as your installation. 30 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libwinmm.a" 31 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libmsimg32.a" 32 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libkernel32.a" 33 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libuser32.a" 34 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libgdi32.a" 35 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libole32.a" 36 | "C:/Program Files/Dev-Cpp/MinGW32/lib/liboleaut32.a" 37 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libuuid.a" 38 | */ 39 | 40 | #ifndef __ACLLIB_H__ 41 | #define __ACLLIB_H__ 42 | 43 | #ifdef _UNICODE 44 | #undef _UNICODE 45 | #endif 46 | #ifdef UNICODE 47 | #undef UNICODE 48 | #endif 49 | 50 | #include 51 | 52 | #define BLACK RGB(0, 0, 0) 53 | #define RED RGB(255, 0, 0) 54 | #define GREEN RGB(0, 255, 0) 55 | #define BLUE RGB(0, 0, 255) 56 | #define CYAN RGB(0, 255, 255) 57 | #define MAGENTA RGB(255, 0, 255) 58 | #define YELLOW RGB(255, 255, 0) 59 | #define WHITE RGB(255, 255, 255) 60 | 61 | #define EMPTY 0xffffffff 62 | #define DEFAULT -1 63 | 64 | typedef enum 65 | { 66 | PEN_STYLE_SOLID, 67 | PEN_STYLE_DASH, /* ------- */ 68 | PEN_STYLE_DOT, /* ....... */ 69 | PEN_STYLE_DASHDOT, /* _._._._ */ 70 | PEN_STYLE_DASHDOTDOT, /* _.._.._ */ 71 | PEN_STYLE_NULL 72 | } ACL_Pen_Style; 73 | 74 | typedef enum 75 | { 76 | BRUSH_STYLE_SOLID = -1, 77 | BRUSH_STYLE_HORIZONTAL, /* ----- */ 78 | BRUSH_STYLE_VERTICAL, /* ||||| */ 79 | BRUSH_STYLE_FDIAGONAL, /* \\\\\ */ 80 | BRUSH_STYLE_BDIAGONAL, /* ///// */ 81 | BRUSH_STYLE_CROSS, /* +++++ */ 82 | BRUSH_STYLE_DIAGCROSS, /* xxxxx */ 83 | BRUSH_STYLE_NULL 84 | } ACL_Brush_Style; 85 | 86 | typedef enum 87 | { 88 | NO_BUTTON = 0, 89 | LEFT_BUTTON, 90 | MIDDLE_BUTTON, 91 | RIGHT_BUTTON 92 | } ACL_Mouse_Button; 93 | 94 | typedef enum 95 | { 96 | BUTTON_DOWN, 97 | BUTTON_DOUBLECLICK, 98 | BUTTON_UP, 99 | ROLL_UP, 100 | ROLL_DOWN, 101 | MOUSEMOVE 102 | } ACL_Mouse_Event; 103 | 104 | typedef enum 105 | { 106 | KEY_DOWN, 107 | KEY_UP 108 | } ACL_Keyboard_Event; 109 | 110 | typedef struct 111 | { 112 | HBITMAP hbitmap; 113 | int width; 114 | int height; 115 | } ACL_Image; 116 | 117 | //typedef enum 118 | //{ 119 | // TM_NO = 0x00, 120 | // TM_COLOR = 0x01, 121 | // TM_ALPHA = 0x02 122 | //} ACL_TransparentMode; 123 | 124 | typedef COLORREF ACL_Color; 125 | typedef int ACL_Sound; 126 | 127 | typedef void(*KeyboardEventCallback) (int key, int event); 128 | typedef void(*CharEventCallback) (char c); 129 | typedef void(*MouseEventCallback) (int x, int y, int button, int event); 130 | typedef void(*TimerEventCallback) (int timerID); 131 | 132 | #ifdef __cplusplus 133 | extern "C" { 134 | #endif 135 | 136 | int Setup(void); 137 | 138 | // 139 | void initWindow(const char title[], int left, int top, int width, int height); 140 | void msgBox(const char title[], const char text[], int flag); 141 | 142 | void registerKeyboardEvent(KeyboardEventCallback callback); 143 | void registerCharEvent(CharEventCallback callback); 144 | void registerMouseEvent(MouseEventCallback callback); 145 | void registerTimerEvent(TimerEventCallback callback); 146 | 147 | void startTimer(int timerID, int timeinterval); 148 | void cancelTimer(int timerID); 149 | 150 | // Sound 151 | void loadSound(const char *fileName, ACL_Sound *pSound); 152 | void playSound(ACL_Sound soundID, int repeat); 153 | void stopSound(ACL_Sound soundID); 154 | 155 | // Paint 156 | void beginPaint(); 157 | void endPaint(); 158 | void clearDevice(void); 159 | int getWidth(); 160 | int getHeight(); 161 | 162 | // Pen 163 | void setPenColor(ACL_Color color); 164 | void setPenWidth(int width); 165 | void setPenStyle(ACL_Pen_Style style); 166 | 167 | // Brush 168 | void setBrushColor(ACL_Color color); 169 | void setBrushStyle(ACL_Brush_Style style); 170 | 171 | // Text 172 | void setTextColor(ACL_Color color); 173 | void setTextBkColor(ACL_Color color); 174 | void setTextSize(int size); 175 | void setTextFont(const char *pFontName); 176 | 177 | void paintText(int x, int y, const char *pStr); 178 | 179 | void setCaretSize(int w, int h); 180 | void setCaretPos(int x, int y); 181 | void showCaret(); 182 | void hideCaret(); 183 | 184 | // Pixel 185 | void putPixel(int x, int y, ACL_Color color); 186 | ACL_Color getPixel(int x, int y); 187 | 188 | // the Point 189 | int getX(void); 190 | int getY(void); 191 | void moveTo(int x, int y); 192 | void moveRel(int dx, int dy); 193 | 194 | // Lines and Curves 195 | void arc(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, \ 196 | int nXStartArc, int nYStartArc, int nXEndArc, int nYEndArc); 197 | void line(int x0, int y0, int x1, int y1); 198 | void lineTo(int nXEnd, int nYEnd); 199 | void lineRel(int dx, int dy); 200 | void polyBezier(const POINT *lppt, int cPoints); 201 | void polyLine(const POINT *lppt, int cPoints); 202 | 203 | // Filled Shapes 204 | void chrod(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, \ 205 | int nXRadial1, int nYRadial1, int nXRadial2, int nYRadial2); 206 | void ellipse(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect); 207 | void pie(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, \ 208 | int nXRadial1, int nYRadial1, int nXRadial2, int nYRadial2); 209 | void polygon(const POINT *lpPoints, int nCount); 210 | void rectangle(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect); 211 | void roundrect(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, \ 212 | int nWidth, int nHeight); 213 | 214 | // Image 215 | void loadImage(const char *pImageFileName, ACL_Image *pImage); 216 | void freeImage(ACL_Image *pImage); 217 | 218 | void putImage(ACL_Image *pImage, int x, int y); 219 | void putImageScale(ACL_Image *pImage, int x, int y, int width, int height); 220 | void putImageTransparent(ACL_Image *pImage, int x, int y, int width, int height, ACL_Color bkColor); 221 | 222 | //void putImageEx(ACL_Image *pImage,int dx,int dy,int dw,int dh, 223 | // int sx,int sy,int sw,int sh); 224 | 225 | //void setTransparentMode(ACL_TransparenetMode); 226 | //void setTransparentColor(ACL_Color); 227 | //void setTransparetnAlpha(int alpha); 228 | 229 | void initConsole(void); 230 | 231 | #ifdef __cplusplus 232 | } 233 | #endif 234 | 235 | #endif 236 | -------------------------------------------------------------------------------- /cube/cube.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "acllib.h" 5 | 6 | #define bool int 7 | #define true 1 8 | #define false 0 9 | 10 | //定义单位像素的边长 11 | #define step 20 12 | //游戏窗体高度 13 | #define boundaryH 28 14 | //游戏窗体宽体 15 | #define boundaryW 14 16 | //俄罗斯方块形状的总数目(包括旋转后的) 17 | #define Max_Shape 19 18 | 19 | //窗体内已经存在的内容(重要,用于存放已经落下的俄罗斯方块的信息) 20 | int WINDOW[boundaryH][boundaryW] = { 0 }; 21 | 22 | //旋转,向左/右移动的标志 23 | bool isTransform = false; 24 | bool isToLeft = false; 25 | bool isToRight = false; 26 | 27 | //俄罗斯方块形状编号num(重要全局变量,用于控制记录移动,形变以及边界是否碰撞) , 用于记录定时器次数cnt 28 | int num = 0,cnt=0; 29 | 30 | //位置信息结构体 31 | struct Pstruct { 32 | int y,x; 33 | }; 34 | 35 | //形状信息结构体 36 | struct Sstruct { 37 | int shape[4][4]; 38 | int next; 39 | }; 40 | 41 | //每个形状的标准方向的编号,用于初始化生成随机俄罗斯方块使用 42 | int seq[7] = { 0,2,6,10,12,14,15 }; 43 | 44 | //每个俄罗斯方块形状的图形数据(包括旋转后的),用4*4二维数组表示,心中有数即可,请直接略过 45 | struct Sstruct SHAPE[19] = { 46 | //l 47 | { 48 | 1,0,0,0, 49 | 1,0,0,0, 50 | 1,0,0,0, 51 | 1,0,0,0,1 52 | } 53 | , 54 | //一 55 | { 56 | 0,0,0,0, 57 | 0,0,0,0, 58 | 1,1,1,1, 59 | 0,0,0,0,0 60 | } 61 | //L 62 | ,{ 63 | 0,0,0,0, 64 | 1,0,0,0, 65 | 1,0,0,0, 66 | 1,1,0,0,3 67 | 68 | } 69 | , 70 | { 71 | 0,0,0,0, 72 | 1,1,1,0, 73 | 1,0,0,0, 74 | 0,0,0,0,4 75 | } 76 | 77 | ,{ 78 | 0,0,0,0, 79 | 1,1,0,0, 80 | 0,1,0,0, 81 | 0,1,0,0,5 82 | 83 | } 84 | , 85 | { 86 | 0,0,0,0, 87 | 0,0,1,0, 88 | 1,1,1,0, 89 | 0,0,0,0,2 90 | } 91 | //rL 92 | ,{ 93 | 0,0,0,0, 94 | 0,1,0,0, 95 | 0,1,0,0, 96 | 1,1,0,0,7 97 | 98 | } 99 | , 100 | { 101 | 0,0,0,0, 102 | 1,0,0,0, 103 | 1,1,1,0, 104 | 0,0,0,0,8 105 | 106 | } 107 | 108 | ,{ 109 | 0,0,0,0, 110 | 1,1,0,0, 111 | 1,0,0,0, 112 | 1,0,0,0,9 113 | 114 | } 115 | 116 | , 117 | { 118 | 0,0,0,0, 119 | 1,1,1,0, 120 | 0,0,1,0, 121 | 0,0,0,0,6 122 | } 123 | //Z 124 | , 125 | { 126 | 0,0,0,0, 127 | 0,1,0,0, 128 | 1,1,0,0, 129 | 1,0,0,0,11 130 | } 131 | , 132 | { 133 | 0,0,0,0, 134 | 1,1,0,0, 135 | 0,1,1,0, 136 | 0,0,0,0,10 137 | } 138 | , 139 | //rZ 140 | { 141 | 0,0,0,0, 142 | 0,1,1,0, 143 | 1,1,0,0, 144 | 0,0,0,0,13 145 | } 146 | , 147 | { 148 | 0,0,0,0, 149 | 1,0,0,0, 150 | 1,1,0,0, 151 | 0,1,0,0,12 152 | } 153 | , 154 | //田 155 | { 156 | 0, 0, 0, 0, 157 | 1, 1, 0, 0, 158 | 1, 1, 0, 0, 159 | 0, 0, 0, 0,14 160 | } 161 | , 162 | //T 163 | { 164 | 0, 0, 0, 0, 165 | 0, 1, 0, 0, 166 | 1, 1, 1, 0, 167 | 0, 0, 0, 0,16 168 | } 169 | , 170 | { 171 | 0,0,0,0, 172 | 1,0,0,0, 173 | 1,1,0,0, 174 | 1,0,0,0,17 175 | } 176 | , 177 | { 178 | 0, 0, 0, 0, 179 | 1, 1, 1, 0, 180 | 0, 1, 0, 0, 181 | 0, 0, 0, 0,18 182 | } 183 | , 184 | { 185 | 0,0, 0, 0, 186 | 0,1, 0, 0, 187 | 1,1, 0, 0, 188 | 0,1, 0, 0,15 189 | } 190 | }; 191 | //当前正在运动的俄罗斯方块的位置变量posi,(重要全局变量,用于控制记录方块移动,是否碰撞,4*4数组最左上角的位置) 192 | struct Pstruct posi; 193 | 194 | //俄罗斯方块生成函数 195 | void initCube(); 196 | //定时器中断 197 | void timeEvent(int tid); 198 | //键盘中断 199 | void keyEvent(int key, int event); 200 | /* 201 | //获取俄罗斯方块的右边源 202 | int getRightEdge(int num); 203 | //获取俄罗斯方块的底边源 204 | int getBottomEdge(int num); 205 | */ 206 | 207 | 208 | int Setup() 209 | { 210 | //生成窗口 211 | initWindow("Test", DEFAULT, DEFAULT, boundaryW * step, boundaryH * step); 212 | 213 | initCube(); 214 | //注册定时器中断 215 | registerTimerEvent(timeEvent); 216 | //注册键盘中断 217 | registerKeyboardEvent(keyEvent); 218 | //开启定时器,50ms 219 | startTimer(0, 50); 220 | return 0; 221 | } 222 | 223 | void initCube() { 224 | //方块初始位置,游戏窗体的中点处 225 | // posi = { 0,(boundaryW+5)/2 }; 226 | posi.y = 0; 227 | posi.x = boundaryW/2-1; 228 | //srand生成随机因子,rand生成随机数 229 | srand((unsigned)time(NULL)); 230 | num = seq[rand() % 7]; 231 | } 232 | 233 | //定时器每中断一次重画一次游戏窗体,即每50ms重画一次 234 | void picture_a_Cube() { 235 | beginPaint(); 236 | //清屏 237 | clearDevice(); 238 | //定义画笔画刷为黑色 239 | setPenColor(BLACK); 240 | setBrushColor(BLACK); 241 | 242 | //将WINDOW内存在的并且还未消除的俄罗斯方块画出 243 | for (int i = 0; i < boundaryH; i++) { 244 | for (int j = 0; j < boundaryW; j++) { 245 | if (WINDOW[i][j])rectangle(j * step, i * step, (j + 1) * step, (i + 1) * step); 246 | } 247 | } 248 | //旋转 249 | if (isTransform) { 250 | isTransform = false; 251 | //temp用于存储原来的形状 252 | int temp = num; 253 | num = SHAPE[num].next; 254 | //判断是否可以进行旋转,如果旋转后的与原WINDOW内存在内容冲突则撤销 255 | bool isReach = false; 256 | for (int i = 0; i < 4; i++) 257 | for (int j = 0; j < 4; j++) { 258 | if (SHAPE[num].shape[i][j]) { 259 | if (posi.y + i + 1 >= boundaryH || WINDOW[posi.y + i + 1][posi.x + j]) { 260 | isReach = true; 261 | break; 262 | } 263 | } 264 | } 265 | if (isReach) num = temp; //撤销 266 | } 267 | //左移 268 | if (isToLeft) { 269 | isToLeft = false; 270 | bool isReach = false; 271 | //判断左移后是否冲突 272 | for (int i = 0; i < 4; i++) 273 | for (int j = 0; j < 4; j++) { 274 | if (SHAPE[num].shape[i][j]) { 275 | if (posi.x + j - 1 < 0 || WINDOW[posi.y + i][posi.x + j - 1]) { 276 | isReach = true; 277 | break; 278 | } 279 | } 280 | } 281 | if(!isReach) posi.x--; 282 | 283 | } 284 | //右移 285 | if (isToRight) { 286 | isToRight = false; 287 | bool isReach = false; 288 | //判断是否冲突 289 | for (int i = 0; i < 4; i++) 290 | for (int j = 0; j < 4; j++) { 291 | if (SHAPE[num].shape[i][j]) { 292 | if (posi.x + j + 1 >= boundaryW || WINDOW[posi.y + i][posi.x + j + 1]) { 293 | isReach = true; 294 | break; 295 | } 296 | } 297 | } 298 | if (!isReach) posi.x++; 299 | } 300 | //画出俄罗斯方块图形 301 | for (int i = 0; i < 4; i++) { 302 | for (int j = 0; j < 4; j++) { 303 | if (SHAPE[num].shape[i][j]) { 304 | rectangle((posi.x + j) * step, (posi.y + i) * step, (posi.x + j + 1) * step, (posi.y + i + 1) * step); 305 | } 306 | } 307 | } 308 | 309 | //判断WINDOW数组是否存在某一行都存在方块,如果是则消除该行,并将之上的数据下移一行 310 | for(int i=0;i= 0; k--) { 319 | for (int j = 0; j < boundaryW; j++) { 320 | WINDOW[k+1][j] = WINDOW[k][j]; 321 | } 322 | } 323 | for (int j = 0; j < boundaryW; j++) WINDOW[0][j] = 0; 324 | } 325 | } 326 | endPaint(); 327 | 328 | } 329 | 330 | void timeEvent(int tid) { 331 | cnt++; 332 | //每隔50*5ms下降一个单位 333 | if (cnt >= 10 ) { 334 | cnt = 0; 335 | bool isReach = false; 336 | //判断是否可以下降 337 | for (int i = 0; i < 4; i++) 338 | for (int j = 0; j < 4; j++) { 339 | if (SHAPE[num].shape[i][j]) { 340 | if (posi.y + i + 1>=boundaryH || WINDOW[posi.y + i + 1][posi.x + j]) { 341 | isReach = true; 342 | break; 343 | } 344 | } 345 | } 346 | if(!isReach) posi.y++; 347 | else { //方块落地/障碍。 348 | for (int i = 0; i < 4; i++) 349 | for (int j = 0; j < 4; j++) { 350 | if (SHAPE[num].shape[i][j]) { 351 | WINDOW[posi.y + i][posi.x + j] = 1; 352 | } 353 | } 354 | initCube(); 355 | } 356 | } 357 | //重绘游戏窗体 358 | picture_a_Cube(); 359 | } 360 | void keyEvent(int key, int event) { 361 | //形变 362 | if (key == VK_SPACE) { 363 | if (event == KEY_UP) { 364 | isTransform = true; 365 | } 366 | } 367 | //左移 368 | if (key == VK_LEFT) { 369 | if (event == KEY_UP) { 370 | isToLeft = true; 371 | } 372 | } 373 | //右移 374 | if (key == VK_RIGHT) { 375 | if (event == KEY_UP) { 376 | isToRight = true; 377 | } 378 | } 379 | //加速下降,50ms改为10ms 380 | if (key == VK_DOWN) { 381 | if (event == KEY_DOWN) { 382 | startTimer(0, 10); 383 | } 384 | else if (event == KEY_UP) { 385 | startTimer(0, 50); 386 | } 387 | } 388 | } 389 | -------------------------------------------------------------------------------- /cube/cube.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MisterFishing/cpp/1eb363fc03a1119d67808dfed624fb75bbc337ba/cube/cube.exe -------------------------------------------------------------------------------- /cube/readme.md: -------------------------------------------------------------------------------- 1 | gcc *.c -lgdi32 -lole32 -loleaut32 -luuid -lwinmm -lmsimg32 -o cube.exe -------------------------------------------------------------------------------- /image/2021-09-02-16-15-55.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MisterFishing/cpp/1eb363fc03a1119d67808dfed624fb75bbc337ba/image/2021-09-02-16-15-55.png -------------------------------------------------------------------------------- /library/MyClass.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "MyClass.h" 5 | using namespace std; 6 | 7 | char* Student2::GetName() 8 | { 9 | return name; 10 | } 11 | 12 | void Student2::SetName(char const *text) 13 | { 14 | if(strlen(text)>10) 15 | { 16 | cout << "Name no more than 10!" << endl; 17 | exit(1); 18 | } 19 | strcpy(name,text); 20 | } 21 | -------------------------------------------------------------------------------- /library/MyClass.h: -------------------------------------------------------------------------------- 1 | class Student2 2 | { 3 | private: 4 | char name[10]; 5 | 6 | public: 7 | int number; 8 | int score; 9 | 10 | char* GetName(); 11 | void SetName(char const *text); 12 | }; 13 | 14 | -------------------------------------------------------------------------------- /library/MyStruct.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "MyStruct.h" 5 | using namespace std; 6 | 7 | char* GetName(struct Student1 * x) 8 | { 9 | return x->name; 10 | } 11 | 12 | void SetName(struct Student1 * x, char const* text) 13 | { 14 | if(strlen(text)>10) 15 | { 16 | cout << "Name no more than 10!" << endl; 17 | exit(1); 18 | } 19 | strcpy(x->name,text); 20 | } 21 | -------------------------------------------------------------------------------- /library/MyStruct.h: -------------------------------------------------------------------------------- 1 | struct Student1 2 | { 3 | char name[10]; 4 | int number; 5 | int score; 6 | }; 7 | 8 | char* GetName(struct Student1 * x); 9 | void SetName(struct Student1 * x, char const* text); 10 | -------------------------------------------------------------------------------- /library/readme.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MisterFishing/cpp/1eb363fc03a1119d67808dfed624fb75bbc337ba/library/readme.md -------------------------------------------------------------------------------- /library/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "MyStruct.h" 5 | #include "MyClass.h" 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | struct Student1 a; 11 | Student2 b; 12 | 13 | SetName(&a,"ZhangSan"); 14 | a.number = 1; 15 | a.score = 100; 16 | 17 | b.SetName("LiSi"); 18 | b.number = 2; 19 | b.score = 90; 20 | 21 | cout << GetName(&a) << " " << a.number << " " << a.score << endl; 22 | cout << b.GetName() << " " << b.number << " " << b.score << endl; 23 | 24 | return 0; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /misc/addone-1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | char addone(char x) 6 | { 7 | cout << "char: "; 8 | return (x+1); 9 | } 10 | 11 | int addone(int x) 12 | { 13 | cout << "int: "; 14 | return (x+1); 15 | } 16 | 17 | float addone(float x) 18 | { 19 | cout << "float: "; 20 | return (x+1); 21 | } 22 | 23 | double addone(double x) 24 | { 25 | cout << "double: "; 26 | return (x+1); 27 | } 28 | 29 | int main() 30 | { 31 | char x1='a'; 32 | int x2=1; 33 | float x3=1.1; 34 | double x4=1.11; 35 | 36 | cout << addone(x1) << endl; 37 | cout << addone(x2) << endl; 38 | cout << addone(x3) << endl; 39 | cout << addone(x4) << endl; 40 | 41 | return 0; 42 | } -------------------------------------------------------------------------------- /misc/addone-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | template 6 | T addone(T x) 7 | { 8 | cout << "T: "; 9 | return (x+1); 10 | } 11 | 12 | int main() 13 | { 14 | char x1='a'; 15 | int x2=1; 16 | float x3=1.1; 17 | double x4=1.11; 18 | 19 | cout << addone(x1) << endl; 20 | cout << addone(x2) << endl; 21 | cout << addone(x3) << endl; 22 | cout << addone(x4) << endl; 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /misc/animal-1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class animal 7 | { 8 | public: 9 | 10 | char name[8]; 11 | 12 | animal(const char * name) 13 | { 14 | strncpy(this->name, name, 7); 15 | this->name[7]='\0'; 16 | } 17 | 18 | void say_hello() 19 | { 20 | cout << "Hello" << endl; 21 | } 22 | 23 | void my_name() 24 | { 25 | cout << "My name is "<< name << endl; 26 | } 27 | 28 | }; 29 | 30 | class dog 31 | { 32 | public: 33 | 34 | char name[8]; 35 | int birth; 36 | 37 | dog(const char * name, int birth) 38 | { 39 | strncpy(this->name, name, 7); 40 | this->name[7]='\0'; 41 | this->birth=birth; 42 | } 43 | 44 | void say_hello() 45 | { 46 | cout << "Wang Wang" << endl; 47 | } 48 | 49 | void my_name() 50 | { 51 | cout << "My name is "<< name << endl; 52 | } 53 | 54 | void my_birth() 55 | { 56 | cout << "I born in " << birth << endl; 57 | } 58 | }; 59 | 60 | class cat 61 | { 62 | public: 63 | 64 | char name[8]; 65 | char color[8]; 66 | 67 | cat(const char * name, const char * color) 68 | { 69 | strncpy(this->name, name, 7); 70 | this->name[7]='\0'; 71 | 72 | strncpy(this->color, color, 7); 73 | this->color[7]='\0'; 74 | } 75 | 76 | void say_hello() 77 | { 78 | cout << "Miao Miao" << endl; 79 | } 80 | 81 | void my_name() 82 | { 83 | cout << "My name is "<< name << endl; 84 | } 85 | 86 | void my_color() 87 | { 88 | cout << "I am " << color << endl; 89 | } 90 | }; 91 | 92 | int main() 93 | { 94 | animal a("Animal"); 95 | a.say_hello(); 96 | a.my_name(); 97 | 98 | dog b("WangCai", 2020); 99 | b.say_hello(); 100 | b.my_name(); 101 | b.my_birth(); 102 | 103 | 104 | cat c("JiaFei", "BLACK"); 105 | c.say_hello(); 106 | c.my_name(); 107 | c.my_color(); 108 | 109 | return 0; 110 | } -------------------------------------------------------------------------------- /misc/animal-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class animal 7 | { 8 | public: 9 | 10 | char name[8]; 11 | 12 | animal(const char * name) 13 | { 14 | strncpy(this->name, name, 7); 15 | this->name[7]='\0'; 16 | } 17 | 18 | void say_hello() 19 | { 20 | cout << "Hello" << endl; 21 | } 22 | 23 | void my_name() 24 | { 25 | cout << "My name is "<< name << endl; 26 | } 27 | 28 | }; 29 | 30 | class dog : public animal 31 | { 32 | public: 33 | 34 | int birth; 35 | 36 | dog(const char * name, int birth) : animal(name) 37 | { 38 | this->birth=birth; 39 | } 40 | 41 | void say_hello() 42 | { 43 | cout << "Wang Wang" << endl; 44 | } 45 | 46 | void my_birth() 47 | { 48 | cout << "I born in " << birth << endl; 49 | } 50 | }; 51 | 52 | class cat : public animal 53 | { 54 | public: 55 | 56 | char color[8]; 57 | 58 | cat(const char * name, const char * color) : animal(name) 59 | { 60 | strncpy(this->color, color, 7); 61 | this->color[7]='\0'; 62 | } 63 | 64 | void say_hello() 65 | { 66 | cout << "Miao Miao" << endl; 67 | } 68 | 69 | void my_color() 70 | { 71 | cout << "I am " << color << endl; 72 | } 73 | }; 74 | 75 | int main() 76 | { 77 | animal a("Animal"); 78 | a.say_hello(); 79 | a.my_name(); 80 | 81 | dog b("WangCai", 2020); 82 | b.say_hello(); 83 | b.my_name(); 84 | b.my_birth(); 85 | 86 | cat c("JiaFei", "BLACK"); 87 | c.say_hello(); 88 | c.my_name(); 89 | c.my_color(); 90 | 91 | return 0; 92 | } -------------------------------------------------------------------------------- /misc/animal-3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class animal 7 | { 8 | public: 9 | 10 | char name[8]; 11 | 12 | animal(const char * name) 13 | { 14 | cout << "animal(): " << name << endl; 15 | strncpy(this->name, name, 7); 16 | this->name[7]='\0'; 17 | } 18 | 19 | ~animal() 20 | { 21 | cout << "~animal(): " << name << endl; 22 | } 23 | 24 | void say_hello() 25 | { 26 | cout << "Hello" << endl; 27 | } 28 | 29 | void my_name() 30 | { 31 | cout << "My name is "<< name << endl; 32 | } 33 | 34 | }; 35 | 36 | class dog : public animal 37 | { 38 | public: 39 | 40 | int birth; 41 | 42 | dog(const char * name, int birth) : animal(name) 43 | { 44 | cout << "dog():" << name << endl; 45 | this->birth=birth; 46 | } 47 | 48 | ~dog() 49 | { 50 | cout << "~dog():" << name << endl; 51 | } 52 | 53 | void say_hello() 54 | { 55 | cout << "Wang Wang" << endl; 56 | } 57 | 58 | void my_birth() 59 | { 60 | cout << "I born in " << birth << endl; 61 | } 62 | }; 63 | 64 | class cat : public animal 65 | { 66 | public: 67 | 68 | char color[8]; 69 | 70 | cat(const char * name, const char * color) : animal(name) 71 | { 72 | cout << "cat():" << name << endl; 73 | strncpy(this->color, color, 7); 74 | this->color[7]='\0'; 75 | } 76 | 77 | ~cat() 78 | { 79 | cout << "~cat():" << name << endl; 80 | } 81 | 82 | void say_hello() 83 | { 84 | cout << "Miao Miao" << endl; 85 | } 86 | 87 | void my_color() 88 | { 89 | cout << "I am " << color << endl; 90 | } 91 | }; 92 | 93 | int main() 94 | { 95 | animal a("Animal"); 96 | a.say_hello(); 97 | a.my_name(); 98 | 99 | dog b("WangCai", 2020); 100 | b.say_hello(); 101 | b.my_name(); 102 | b.my_birth(); 103 | 104 | cat c("JiaFei", "BLACK"); 105 | c.say_hello(); 106 | c.my_name(); 107 | c.my_color(); 108 | 109 | return 0; 110 | } -------------------------------------------------------------------------------- /misc/animal-4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class animal 7 | { 8 | public: 9 | 10 | char name[8]; 11 | 12 | animal(const char * name) 13 | { 14 | cout << "animal(): " << name << endl; 15 | strncpy(this->name, name, 7); 16 | this->name[7]='\0'; 17 | } 18 | 19 | ~animal() 20 | { 21 | cout << "~animal(): " << name << endl; 22 | } 23 | 24 | void say_hello() 25 | { 26 | cout << "Hello" << endl; 27 | } 28 | 29 | void my_name() 30 | { 31 | cout << "My name is "<< name << endl; 32 | } 33 | 34 | }; 35 | 36 | class dog : public animal 37 | { 38 | public: 39 | 40 | int birth; 41 | 42 | dog(const char * name, int birth) : animal(name) 43 | { 44 | cout << "dog():" << name << endl; 45 | this->birth=birth; 46 | } 47 | 48 | ~dog() 49 | { 50 | cout << "~dog():" << name << endl; 51 | } 52 | 53 | void say_hello() 54 | { 55 | cout << "Wang Wang" << endl; 56 | } 57 | 58 | void my_birth() 59 | { 60 | cout << "I born in " << birth << endl; 61 | } 62 | }; 63 | 64 | class cat : public animal 65 | { 66 | public: 67 | 68 | char color[8]; 69 | 70 | cat(const char * name, const char * color) : animal(name) 71 | { 72 | cout << "cat():" << name << endl; 73 | strncpy(this->color, color, 7); 74 | this->color[7]='\0'; 75 | } 76 | 77 | ~cat() 78 | { 79 | cout << "~cat():" << name << endl; 80 | } 81 | 82 | void say_hello() 83 | { 84 | cout << "Miao Miao" << endl; 85 | } 86 | 87 | void my_color() 88 | { 89 | cout << "I am " << color << endl; 90 | } 91 | }; 92 | 93 | class shenshou : public dog, public cat 94 | { 95 | public: 96 | shenshou(const char * name, int birth, const char * color) : dog(name,birth) , cat(name, color) 97 | { 98 | } 99 | }; 100 | 101 | int main() 102 | { 103 | shenshou s("ShenShou", 2022, "GOLD"); 104 | s.my_birth(); 105 | s.my_color(); 106 | 107 | return 0; 108 | } -------------------------------------------------------------------------------- /misc/animal-5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class animal 7 | { 8 | public: 9 | 10 | char name[8]; 11 | 12 | animal(const char * name) 13 | { 14 | cout << "animal(): " << name << endl; 15 | strncpy(this->name, name, 7); 16 | this->name[7]='\0'; 17 | } 18 | 19 | ~animal() 20 | { 21 | cout << "~animal(): " << name << endl; 22 | } 23 | 24 | void say_hello() 25 | { 26 | cout << "Hello" << endl; 27 | } 28 | 29 | void my_name() 30 | { 31 | cout << "My name is "<< name << endl; 32 | } 33 | 34 | }; 35 | 36 | class dog : public virtual animal 37 | { 38 | public: 39 | 40 | int birth; 41 | 42 | dog(const char * name, int birth) : animal(name) 43 | { 44 | cout << "dog():" << name << endl; 45 | this->birth=birth; 46 | } 47 | 48 | ~dog() 49 | { 50 | cout << "~dog():" << name << endl; 51 | } 52 | 53 | void say_hello() 54 | { 55 | cout << "Wang Wang" << endl; 56 | } 57 | 58 | void my_birth() 59 | { 60 | cout << "I born in " << birth << endl; 61 | } 62 | }; 63 | 64 | class cat : public virtual animal 65 | { 66 | public: 67 | 68 | char color[8]; 69 | 70 | cat(const char * name, const char * color) : animal(name) 71 | { 72 | cout << "cat():" << name << endl; 73 | strncpy(this->color, color, 7); 74 | this->color[7]='\0'; 75 | } 76 | 77 | ~cat() 78 | { 79 | cout << "~cat():" << name << endl; 80 | } 81 | 82 | void say_hello() 83 | { 84 | cout << "Miao Miao" << endl; 85 | } 86 | 87 | void my_color() 88 | { 89 | cout << "I am " << color << endl; 90 | } 91 | }; 92 | 93 | class shenshou : public dog, public cat 94 | { 95 | public: 96 | shenshou(const char * name, int birth, const char * color) : dog(name,birth) , cat(name, color), animal(name) 97 | { 98 | } 99 | }; 100 | 101 | int main() 102 | { 103 | shenshou s("ShenShou", 2022, "GOLD"); 104 | cout << s.name << endl; 105 | s.my_name(); 106 | 107 | return 0; 108 | } -------------------------------------------------------------------------------- /misc/const-member-1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class student 6 | { 7 | private: 8 | char name[10]; 9 | 10 | public: 11 | int const number; 12 | int score; 13 | 14 | student() 15 | { 16 | strcpy(name,"none"); 17 | number=0; 18 | score=0; 19 | } 20 | 21 | student(char const * text) 22 | { 23 | strcpy(name,text); 24 | number=0; 25 | score=0; 26 | } 27 | 28 | student(char const * text, int n, int s) 29 | { 30 | strcpy(name,text); 31 | number=n; 32 | score=s; 33 | } 34 | 35 | student(student const & s) 36 | { 37 | strcpy(name,s.name); 38 | number=s.number; 39 | score=s.score; 40 | } 41 | 42 | ~student() 43 | { 44 | } 45 | 46 | void set_name(char const * text) 47 | { 48 | if(strlen(text)>=10) 49 | { 50 | cout << "length of " << text << " >= 10 " << endl; 51 | exit(0); 52 | } 53 | strcpy(name,text); 54 | } 55 | 56 | void display() 57 | { 58 | cout << name << " " << number << " " << score << endl; 59 | } 60 | 61 | }; 62 | 63 | int main() 64 | { 65 | student zs("zhangsan"); 66 | zs.display(); 67 | 68 | student const ls("lisi",2,80); 69 | ls.display(); 70 | 71 | return 0; 72 | } -------------------------------------------------------------------------------- /misc/const-member-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class student 6 | { 7 | private: 8 | char name[10]; 9 | 10 | public: 11 | int const number; 12 | int score; 13 | 14 | student():number(0) 15 | { 16 | strcpy(name,"none"); 17 | score=0; 18 | } 19 | 20 | student(char const * text):number(0) 21 | { 22 | strcpy(name,text); 23 | score=0; 24 | } 25 | 26 | student(char const * text, int n, int s):number(n) 27 | { 28 | strcpy(name,text); 29 | score=s; 30 | } 31 | 32 | student(student const & s):number(s.number) 33 | { 34 | strcpy(name,s.name); 35 | score=s.score; 36 | } 37 | 38 | ~student() 39 | { 40 | } 41 | 42 | void set_name(char const * text) 43 | { 44 | if(strlen(text)>=10) 45 | { 46 | cout << "length of " << text << " >= 10 " << endl; 47 | exit(0); 48 | } 49 | strcpy(name,text); 50 | } 51 | 52 | void display() const 53 | { 54 | cout << name << " " << number << " " << score << endl; 55 | } 56 | 57 | }; 58 | 59 | int main() 60 | { 61 | student zs("zhangsan"); 62 | zs.display(); 63 | 64 | student const ls("lisi",2,80); 65 | ls.display(); 66 | 67 | return 0; 68 | } -------------------------------------------------------------------------------- /misc/const-pointer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define PI 3.14 5 | 6 | int main() 7 | { 8 | float x, y; 9 | const float pi=3.14; 10 | 11 | x=PI; 12 | y=pi; 13 | cout << "x: " << x << endl << "y: " << y << endl; 14 | 15 | 16 | float * const p1=&x; 17 | // p1=&y; 18 | // *p1=1.11; 19 | 20 | float const * p2=&x; 21 | // p2=&y; 22 | // *p2=1.11; 23 | 24 | float const * const p3=&x; 25 | // p3=&y; 26 | // *p3=1.11; 27 | 28 | cout << "x: " << x << endl << "y: " << y << endl; 29 | 30 | return 0; 31 | } 32 | 33 | -------------------------------------------------------------------------------- /misc/constructor-1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class student 6 | { 7 | public: 8 | 9 | char name[10]; 10 | int number; 11 | int score; 12 | 13 | student(char const * text) 14 | { 15 | strcpy(name,text); 16 | } 17 | 18 | student(char const * text, int n, int s) 19 | { 20 | strcpy(name,text); 21 | number=n; 22 | score=s; 23 | } 24 | 25 | void display() 26 | { 27 | cout << name << " " << number << " " << score << endl; 28 | } 29 | 30 | }; 31 | 32 | int main() 33 | { 34 | student zs; 35 | zs.display(); 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /misc/constructor-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class student 6 | { 7 | public: 8 | 9 | char name[10]; 10 | int number; 11 | int score; 12 | 13 | student() 14 | { 15 | strcpy(name,"none"); 16 | number=0; 17 | score=0; 18 | } 19 | 20 | student(char const * text) 21 | { 22 | strcpy(name,text); 23 | number=0; 24 | score=0; 25 | } 26 | 27 | student(char const * text, int n, int s) 28 | { 29 | strcpy(name,text); 30 | number=n; 31 | score=s; 32 | } 33 | 34 | void display() 35 | { 36 | cout << name << " " << number << " " << score << endl; 37 | } 38 | 39 | }; 40 | 41 | int main() 42 | { 43 | student zs; 44 | zs.display(); 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /misc/delete-1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int num=1000000; 5 | 6 | void func(int i) 7 | { 8 | char *p; 9 | p=new char[num](); 10 | cout << i << endl; 11 | // delete []p; 12 | } 13 | 14 | int main() 15 | { 16 | char x; 17 | for(int i=1; i<=4000; i++) 18 | { 19 | func(i); 20 | if( (i%500)==0 ) 21 | cin >> x; 22 | } 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /misc/delete-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | const int num=1000000; 6 | 7 | class student 8 | { 9 | public: 10 | 11 | char name[10]; 12 | int number; 13 | int score; 14 | char * resume; 15 | 16 | student() 17 | { 18 | resume=new char[num](); 19 | } 20 | }; 21 | 22 | void func(int i) 23 | { 24 | student zs; 25 | cout << i << endl; 26 | } 27 | 28 | int main() 29 | { 30 | char x; 31 | for(int i=1; i<=4000; i++) 32 | { 33 | func(i); 34 | if( (i%500)==0 ) 35 | cin >> x; 36 | } 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /misc/delete-3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | const int num=1000000; 6 | 7 | class student 8 | { 9 | public: 10 | 11 | char name[10]; 12 | int number; 13 | int score; 14 | char * resume; 15 | 16 | student() 17 | { 18 | resume=new char[num](); 19 | } 20 | 21 | ~student() 22 | { 23 | delete[] resume; 24 | } 25 | 26 | }; 27 | 28 | void func(int i) 29 | { 30 | student zs; 31 | cout << i << endl; 32 | } 33 | 34 | int main() 35 | { 36 | char x; 37 | for(int i=1; i<=4000; i++) 38 | { 39 | func(i); 40 | if( (i%500)==0 ) 41 | cin >> x; 42 | } 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /misc/delete-4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | const int num=1000000; 6 | 7 | class student 8 | { 9 | public: 10 | 11 | char name[10]; 12 | int number; 13 | int score; 14 | char * resume; 15 | 16 | student() 17 | { 18 | resume=new char[num](); 19 | } 20 | 21 | ~student() 22 | { 23 | delete[] resume; 24 | } 25 | }; 26 | 27 | void func(int i) 28 | { 29 | student zs; 30 | student ls(zs); 31 | cout << i << endl; 32 | } 33 | 34 | int main() 35 | { 36 | char x; 37 | for(int i=1; i<=4000; i++) 38 | { 39 | func(i); 40 | if( (i%500)==0 ) 41 | cin >> x; 42 | } 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /misc/delete-5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | const int num=1000000; 6 | 7 | class student 8 | { 9 | public: 10 | 11 | char name[10]; 12 | int number; 13 | int score; 14 | char * resume; 15 | 16 | student() 17 | { 18 | resume=new char[num](); 19 | } 20 | 21 | ~student() 22 | { 23 | delete[] resume; 24 | } 25 | 26 | student(student & s) 27 | { 28 | strcpy(name,s.name); 29 | number=s.number; 30 | score=s.score; 31 | resume=new char[num](); 32 | strcpy(resume,s.resume); 33 | } 34 | 35 | }; 36 | 37 | void func(int i) 38 | { 39 | student zs; 40 | student ls(zs); 41 | cout << i << endl; 42 | } 43 | 44 | int main() 45 | { 46 | char x; 47 | for(int i=1; i<=4000; i++) 48 | { 49 | func(i); 50 | if( (i%500)==0 ) 51 | cin >> x; 52 | } 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /misc/dog.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class dog { 5 | private: 6 | char tail[10]="\n~~~\n"; /* 私有成员,“别人”不能碰狗的尾巴 */ 7 | 8 | public: 9 | void wag() /* 公有成员,“别人”可以叫狗摇尾巴 */ 10 | { 11 | cout << tail; 12 | } 13 | }; 14 | 15 | 16 | 17 | int main() 18 | { 19 | dog wangcai; 20 | cout << wangcai.tail; /* 错误:访问私有成员 */ 21 | wangcai.wag(); /* 正确:访问公有成员 */ 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /misc/exception.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | const int num=10000000; 8 | 9 | void func(int i) 10 | { 11 | char *p; 12 | p=new char[num]; 13 | memset(p, 0, num); 14 | cout << i << endl; 15 | // throw 404; 16 | // throw runtime_error("runtime error"); 17 | delete []p; 18 | } 19 | 20 | int main() 21 | { 22 | try 23 | { 24 | for(int i=1; i<=300; i++){ 25 | func(i); 26 | //Sleep(5); 27 | } 28 | } 29 | catch(bad_alloc err) 30 | { 31 | cout << "Oh My God: " << err.what() << endl; 32 | } 33 | catch(int err) 34 | { 35 | cout << "Oh My God: " << err << endl; 36 | } 37 | catch(runtime_error err) 38 | { 39 | cout << "Oh My God: " << err.what() << endl; 40 | } 41 | 42 | system("pause"); 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /misc/format.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | float pi=3.1415926; 8 | cout << pi << endl; 9 | cout.setf(ios::fixed); 10 | cout << setprecision(0) << pi << endl; 11 | cout << setprecision(1) << pi << endl; 12 | cout << setprecision(2) << pi << endl; 13 | cout << setprecision(3) << pi << endl; 14 | cout.setf(ios::scientific, ios::floatfield); 15 | cout << setprecision(0) << pi << endl; 16 | cout << setprecision(1) << pi << endl; 17 | cout << setprecision(2) << pi << endl; 18 | cout << setprecision(3) << pi << endl; 19 | cout << true << endl; 20 | cout << false << endl; 21 | cout << ((true)?"true":"false") << endl; 22 | cout << ((false)?"true":"false") << endl; 23 | cout.setf(ios::boolalpha); 24 | cout << true << endl; 25 | cout << false << endl; 26 | cout << pi << endl; 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /misc/func.cpp: -------------------------------------------------------------------------------- 1 | void func(int &x, int &y) 2 | { 3 | x = x+y ; 4 | y = x-y ; 5 | x = x-y; 6 | } 7 | 8 | -------------------------------------------------------------------------------- /misc/func.h: -------------------------------------------------------------------------------- 1 | void func(int &x, int &y); 2 | -------------------------------------------------------------------------------- /misc/hello.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | printf("Hello World\n"); 6 | return 0; 7 | } 8 | 9 | -------------------------------------------------------------------------------- /misc/hello.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | cout << "Hello World\n"; 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /misc/io.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | char buffer[100]; 8 | char buffer_2[100]; 9 | 10 | printf("input_1: "); 11 | scanf("%s",&buffer); 12 | printf("output_1: %s\n", buffer); 13 | 14 | operator<<(cout,"input_2: "); 15 | operator>>(cin,buffer); 16 | operator<<(cout,"output_2: "); 17 | operator<<(cout,buffer); 18 | operator<<(cout,"\n"); 19 | 20 | cout << "input_3: "; 21 | cin >> buffer; 22 | cout << "output_3: "; 23 | cout << buffer; 24 | cout << "\n"; 25 | 26 | cout << "input_4: "; 27 | cin >> buffer; 28 | cout << "output_4: " << buffer << "\n"; 29 | 30 | cout << "input_5: "; 31 | cin >> buffer; 32 | cout << "output_5: " << buffer << endl; 33 | 34 | cout << "input_6 input_7: "; 35 | cin >> buffer >> buffer_2; 36 | cout << "output_6: " << buffer << endl << "output_7: " << buffer_2 << endl; 37 | 38 | return 0; 39 | } 40 | 41 | -------------------------------------------------------------------------------- /misc/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "func.h" 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int a=1, b=2; 9 | cout << a << " " << b << endl; 10 | func(a, b); 11 | cout << a << " " << b << endl; 12 | return 0; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /misc/max.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int GetMax2(int x, int y) 5 | { 6 | if(x>y) 7 | return x; 8 | else 9 | return y; 10 | } 11 | 12 | int GetMax3(int x, int y, int iz) 13 | { 14 | int tmp1,tmp2; 15 | tmp1=GetMax2(x,y); 16 | tmp2=GetMax2(tmp1,iz); 17 | return tmp2; 18 | } 19 | 20 | 21 | int main() 22 | { 23 | int a,b,c; 24 | int number; 25 | int max; 26 | 27 | cout << "Input number: "; 28 | cin >> number; 29 | 30 | if(number==2) 31 | { 32 | cout << "Input a b: "; 33 | cin >> a >> b; 34 | max=GetMax2(a, b); 35 | cout << "max: " << max << endl; 36 | } 37 | 38 | if(number==3) 39 | { 40 | cout << "Input a b c: "; 41 | cin >> a >> b >> c; 42 | max=GetMax3(a, b, c); 43 | cout << "max: " << max << endl; 44 | } 45 | 46 | return 0; 47 | } 48 | 49 | *** 50 | 51 | #include 52 | using namespace std; 53 | 54 | int GetMax(int x, int y) 55 | { 56 | if(x>y) 57 | return x; 58 | else 59 | return y; 60 | } 61 | 62 | int GetMax(int x, int y, int z) 63 | { 64 | int tmp1,tmp2; 65 | tmp1=GetMax(x,y); 66 | tmp2=GetMax(tmp1,z); 67 | return tmp2; 68 | } 69 | 70 | 71 | int main() 72 | { 73 | int a,b,c; 74 | int number; 75 | int max; 76 | 77 | cout << "input number: "; 78 | cin >> number; 79 | 80 | if(number==2) 81 | { 82 | cout << "input a b: "; 83 | cin >> a >> b; 84 | max=GetMax(a, b); 85 | cout << "max: " << max << endl; 86 | } 87 | 88 | if(number==3) 89 | { 90 | cout << "input a b c: "; 91 | cin >> a >> b >> c; 92 | max=GetMax(a, b, c); 93 | cout << "max: " << max << endl; 94 | } 95 | 96 | return 0; 97 | } 98 | 99 | *** 100 | 101 | #include 102 | using namespace std; 103 | 104 | int GetMax(int x, int y) 105 | { 106 | if(x>y) 107 | return x; 108 | else 109 | return y; 110 | } 111 | 112 | double GetMax(double dx, double dy) 113 | { 114 | if(dx>dy) 115 | return dy; 116 | else 117 | return dy; 118 | } 119 | 120 | int main() 121 | { 122 | int a,b; 123 | double da,db; 124 | 125 | cout << "Input a b: "; 126 | cin >> a >> b; 127 | cout << "Max: " << GetMax(a, b) << endl; 128 | 129 | cout << "Input da db: "; 130 | cin >> da >> db; 131 | cout << "Max: " << GetMax(da, db) << endl; 132 | 133 | return 0; 134 | } 135 | 136 | -------------------------------------------------------------------------------- /misc/namespace.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | namespace ns1 5 | { 6 | int x=1, y=2; 7 | } 8 | 9 | namespace ns2 10 | { 11 | int x=11, y=22; 12 | } 13 | 14 | int main() 15 | { 16 | cout << ns1::x << " " << ns1::y << endl; 17 | cout << ns2::x << " " << ns2::y << endl; 18 | return 0; 19 | } 20 | 21 | ---------- 22 | 23 | #include 24 | using namespace std; 25 | 26 | namespace ns1 27 | { 28 | int x=1, y=2; 29 | } 30 | 31 | namespace ns2 32 | { 33 | int x=11, y=22; 34 | } 35 | 36 | int main() 37 | { 38 | cout << ns1::x << " " << ns1::y << endl; 39 | cout << ns2::x << " " << ns2::y << endl; 40 | 41 | using namespace ns1; 42 | cout << x << " " << y << endl; 43 | 44 | return 0; 45 | } 46 | 47 | ---------- 48 | 49 | #include 50 | using namespace std; 51 | 52 | namespace ns1 53 | { 54 | int x=1, y=2; 55 | 56 | namespace ns2 57 | { 58 | int x=11, y=22; 59 | } 60 | 61 | } 62 | 63 | 64 | int main() 65 | { 66 | cout << ns1::x << " " << ns1::y << endl; 67 | cout << ns1::ns2::x << " " << ns1::ns2::y << endl; 68 | 69 | using namespace ns1; 70 | cout << x << " " << y << endl; 71 | 72 | return 0; 73 | } 74 | 75 | ---------- 76 | 77 | #include 78 | using namespace std; 79 | 80 | namespace ns1 81 | { 82 | int x=1, y=2; 83 | 84 | namespace ns2 85 | { 86 | int x=11, y=22; 87 | } 88 | 89 | } 90 | 91 | 92 | int main() 93 | { 94 | int x=11111, y=22222; 95 | cout << ns1::x << " " << ns1::y << endl; 96 | cout << ns1::ns2::x << " " << ns1::ns2::y << endl; 97 | 98 | using namespace ns1; 99 | using namespace ns1::ns2; 100 | cout << x << " " << y << endl; 101 | 102 | return 0; 103 | } 104 | 105 | ---------- 106 | 107 | #include 108 | using namespace std; 109 | 110 | namespace ns1 111 | { 112 | int x=1, y=2; 113 | 114 | namespace ns2 115 | { 116 | int x=11, y=22; 117 | } 118 | 119 | } 120 | 121 | namespace ns3 122 | { 123 | int x=111, y=222; 124 | 125 | void swap(int x, int y) 126 | { 127 | int tmp; 128 | tmp=x; 129 | x=y; 130 | y=tmp; 131 | } 132 | 133 | } 134 | 135 | int main() 136 | { 137 | int x=11111, y=22222; 138 | cout << ns1::x << " " << ns1::y << endl; 139 | cout << ns1::ns2::x << " " << ns1::ns2::y << endl; 140 | 141 | using namespace ns1; 142 | using namespace ns1::ns2; 143 | using namespace ns3; 144 | 145 | cout << x << " " << y << endl; 146 | swap(x,y); 147 | cout << x << " " << y << endl; 148 | 149 | return 0; 150 | } 151 | 152 | -------------------------------------------------------------------------------- /misc/node.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | template 6 | class node 7 | { 8 | T value; 9 | node *prev,*next; 10 | 11 | public: 12 | 13 | node() 14 | { 15 | prev=NULL; 16 | next=NULL; 17 | } 18 | 19 | void set_value(T value) 20 | { 21 | this->value=value; 22 | } 23 | 24 | T get_value() 25 | { 26 | return value; 27 | } 28 | 29 | node * get_prev() 30 | { 31 | return prev; 32 | } 33 | 34 | node * get_next() 35 | { 36 | return next; 37 | } 38 | 39 | void append(node *p) 40 | { 41 | p->prev=this; 42 | if(next!=NULL) next->prev=p; 43 | p->next=next; 44 | next=p; 45 | } 46 | 47 | }; 48 | 49 | int main( ) 50 | { 51 | node *ptr1; 52 | node n1,n2,n3; 53 | n1.set_value(97); 54 | n2.set_value(98); 55 | n3.set_value(99); 56 | n1.append(&n2); 57 | n2.append(&n3); 58 | 59 | for(ptr1=&n1 ; ; ptr1=ptr1->get_next()) 60 | { 61 | cout << ptr1->get_value() << endl; 62 | if(ptr1->get_next()==NULL) break; 63 | } 64 | 65 | node *ptr2; 66 | node n4,n5,n6; 67 | n4.set_value(97.5); 68 | n5.set_value(98.5); 69 | n6.set_value(99.5); 70 | n4.append(&n5); 71 | n5.append(&n6); 72 | 73 | for(ptr2=&n4 ; ; ptr2=ptr2->get_next()) 74 | { 75 | cout << ptr2->get_value() << endl; 76 | if(ptr2->get_next()==NULL) break; 77 | } 78 | 79 | return 0; 80 | } -------------------------------------------------------------------------------- /misc/pi.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define PI 3.14 5 | 6 | int main() 7 | { 8 | float x, y; 9 | const float pi=3.14; 10 | 11 | x=PI; 12 | y=pi; 13 | cout << "x: " << x << endl << "y: " << y << endl; 14 | 15 | return 0; 16 | } 17 | 18 | -------------------------------------------------------------------------------- /misc/setname-1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | struct student 6 | { 7 | char name[10]; 8 | int number; 9 | int score; 10 | }; 11 | 12 | int main() 13 | { 14 | struct student zs; 15 | 16 | strcpy(zs.name,"zhangsan"); 17 | zs.number = 1; 18 | zs.score = 90; 19 | 20 | printf("%s %d %d\n", zs.name, zs.number, zs.score); 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /misc/setname-2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | struct student 6 | { 7 | char name[10]; 8 | int number; 9 | int score; 10 | }; 11 | 12 | void set_name(struct student * stu, char const * text) 13 | { 14 | if(strlen(text)>=10) 15 | { 16 | printf("length of %s >= 10 \n", text); 17 | exit(0); 18 | } 19 | 20 | strcpy(stu->name,text); 21 | } 22 | 23 | void set_number(struct student * stu, int num) 24 | { 25 | if(num <= 0) 26 | { 27 | printf("number %d <= 0 \n", num); 28 | exit(0); 29 | } 30 | 31 | stu->number = num; 32 | } 33 | 34 | void set_score(struct student * stu, int score) 35 | { 36 | if(score < 0) 37 | { 38 | printf("score %d < 0 \n", score); 39 | exit(0); 40 | } 41 | 42 | if(score > 100) 43 | { 44 | printf("score %d > 100 \n", score); 45 | exit(0); 46 | } 47 | 48 | stu->score = score; 49 | } 50 | 51 | void display(struct student *stu) 52 | { 53 | printf("%s %d %d\n", stu->name, stu->number, stu->score); 54 | } 55 | 56 | int main() 57 | { 58 | struct student zs; 59 | 60 | set_name(&zs, "zhangsan"); 61 | set_number(&zs, 1); 62 | set_score(&zs, 90); 63 | display(&zs); 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /misc/setname-3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | class student 7 | { 8 | private: 9 | char name[10]; 10 | 11 | public: 12 | int number; 13 | int score; 14 | 15 | void set_name(char const * text) 16 | { 17 | if(strlen(text)>=10) 18 | { 19 | cout << "length of " << text << " >= 10 " << endl; 20 | exit(0); 21 | } 22 | strcpy(name,text); 23 | } 24 | 25 | void display() 26 | { 27 | cout << name << " " << number << " " << score << endl; 28 | } 29 | 30 | }; 31 | 32 | int main() 33 | { 34 | student zs; 35 | 36 | zs.set_name("zhangsan"); 37 | zs.number = 1; 38 | zs.score = 90; 39 | zs.display(); 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /misc/static-member.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class student 6 | { 7 | private: 8 | char name[10]; 9 | 10 | public: 11 | int const number; 12 | int score; 13 | static char school[10]; 14 | 15 | student():number(0) 16 | { 17 | strcpy(name,"none"); 18 | score=0; 19 | } 20 | 21 | student(char const * text):number(0) 22 | { 23 | strcpy(name,text); 24 | score=0; 25 | } 26 | 27 | student(char const * text, int n, int s):number(n) 28 | { 29 | strcpy(name,text); 30 | score=s; 31 | } 32 | 33 | student(student const & s):number(s.number) 34 | { 35 | strcpy(name,s.name); 36 | score=s.score; 37 | } 38 | 39 | ~student() 40 | { 41 | } 42 | 43 | void set_name(char const * text) 44 | { 45 | if(strlen(text)>=10) 46 | { 47 | cout << "length of " << text << " >= 10 " << endl; 48 | exit(0); 49 | } 50 | strcpy(name,text); 51 | } 52 | 53 | void display() const 54 | { 55 | cout << name << " " << number << " " << score << endl; 56 | } 57 | 58 | static void set_school(char const * text) 59 | { 60 | strcpy(school, text); 61 | // score = 100; 62 | } 63 | }; 64 | 65 | char student::school[10] = "UESTC"; 66 | 67 | int main() 68 | { 69 | student::set_school("uestc"); 70 | cout << student::school << endl; 71 | return 0; 72 | } -------------------------------------------------------------------------------- /misc/struct class.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct student 5 | { 6 | char name[10]; 7 | int number; 8 | int score; 9 | }; 10 | 11 | int main() 12 | { 13 | struct student zs; 14 | 15 | strcpy(zs.name,"zhangsan"); 16 | zs.number = 1; 17 | zs.score = 90; 18 | 19 | printf("%s %d %d\n", zs.name, zs.number, zs.score); 20 | return 0; 21 | } 22 | 23 | *** 24 | 25 | #include 26 | #include 27 | using namespace std; 28 | 29 | class student 30 | { 31 | public: 32 | char name[10]; 33 | int number; 34 | int score; 35 | }; 36 | 37 | int main() 38 | { 39 | student ls; 40 | 41 | strcpy(ls.name,"lisi"); 42 | ls.number = 2; 43 | ls.score = 80; 44 | 45 | cout << ls.name << " " << ls.number << " " << ls.score << endl; 46 | return 0; 47 | } 48 | 49 | *** 50 | 51 | #include 52 | #include 53 | #include 54 | 55 | struct student 56 | { 57 | char name[10]; 58 | int number; 59 | int score; 60 | }; 61 | 62 | void set_name(struct student *stu, char const *text) 63 | { 64 | if(strlen(text)>10) 65 | { 66 | printf("Your input seems have some problem. \n"); 67 | exit(0); 68 | } 69 | 70 | strcpy(stu->name,text); 71 | } 72 | 73 | int main() 74 | { 75 | struct student zs; 76 | char buffer[100]; 77 | 78 | scanf("%s", buffer); 79 | set_name(&zs, buffer); 80 | // strcpy(zs.name,buffer); 81 | 82 | zs.number = 1; 83 | zs.score = 90; 84 | 85 | printf("%s %d %d\n", zs.name, zs.number, zs.score); 86 | return 0; 87 | } 88 | 89 | *** 90 | 91 | #include 92 | #include 93 | using namespace std; 94 | 95 | class student 96 | { 97 | private: 98 | char name[10]; 99 | 100 | public: 101 | int number; 102 | int score; 103 | 104 | char* get_name() 105 | { 106 | return name; 107 | } 108 | 109 | void set_name(char const *text) 110 | { 111 | if(strlen(text)>10) 112 | { 113 | cout << "Your input seems have some problem." << endl; 114 | exit(0); 115 | } 116 | 117 | strcpy(name,text); 118 | } 119 | 120 | }; 121 | 122 | int main() 123 | { 124 | student ls; 125 | char buffer[100]; 126 | 127 | scanf("%s", buffer); 128 | ls.set_name(buffer); 129 | // strcpy(ls.name,buffer); 130 | ls.number = 2; 131 | ls.score = 80; 132 | 133 | cout << ls.get_name() << " " << ls.number << " " << ls.score << endl; 134 | return 0; 135 | } 136 | 137 | -------------------------------------------------------------------------------- /misc/student-vip.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class student 6 | { 7 | private: 8 | 9 | char name[10]; 10 | int number; 11 | int score; 12 | static student * vip; 13 | 14 | student(char const * name, int number, int score) 15 | { 16 | if(strlen(name)>=10) 17 | { 18 | cout << "length of " << name << " >= 10 " << endl; 19 | exit(0); 20 | } 21 | 22 | strcpy(this->name,name); 23 | this->number=number; 24 | this->score=score; 25 | } 26 | 27 | public: 28 | 29 | void display() 30 | { 31 | cout << name << " " << number << " " << score << endl; 32 | } 33 | 34 | static student * new_student(char const * name, int number, int score) 35 | { 36 | if(vip==NULL) 37 | vip=new student(name, number, score); 38 | else 39 | cout << "sorry, only one vip" << endl; 40 | return vip; 41 | } 42 | 43 | }; 44 | 45 | student * student::vip = NULL; 46 | 47 | int main() 48 | { 49 | student *zs = student::new_student("zhangsan", 1, 90); 50 | zs->display(); 51 | 52 | student *ls = student::new_student("lisi", 2, 80); 53 | ls->display(); 54 | 55 | student *ww = student::new_student("wangwu ", 3, 70); 56 | ww->display(); 57 | 58 | return 0; 59 | } -------------------------------------------------------------------------------- /misc/student.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class student 6 | { 7 | private: 8 | char name[10]; 9 | 10 | public: 11 | int number; 12 | int score; 13 | 14 | student() 15 | { 16 | strcpy(name,"none"); 17 | number=0; 18 | score=0; 19 | } 20 | 21 | student(char const * text) 22 | { 23 | strcpy(name,text); 24 | number=0; 25 | score=0; 26 | } 27 | 28 | student(char const * text, int n, int s) 29 | { 30 | strcpy(name,text); 31 | number=n; 32 | score=s; 33 | } 34 | 35 | student(student const & s) 36 | { 37 | strcpy(name,s.name); 38 | number=s.number; 39 | score=s.score; 40 | } 41 | 42 | ~student() 43 | { 44 | } 45 | 46 | void set_name(char const * text) 47 | { 48 | if(strlen(text)>=10) 49 | { 50 | cout << "length of " << text << " >= 10 " << endl; 51 | exit(0); 52 | } 53 | strcpy(name,text); 54 | } 55 | 56 | void display() 57 | { 58 | cout << name << " " << number << " " << score << endl; 59 | } 60 | 61 | }; 62 | 63 | int main() 64 | { 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /misc/swap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void swap(int x, int y) 5 | { 6 | int tmp; 7 | tmp=x; 8 | x=y; 9 | y=tmp; 10 | } 11 | 12 | int main() 13 | { 14 | int a,b; 15 | 16 | cout << "input a b: "; 17 | cin >> a >> b; 18 | cout << "----------" << endl; 19 | cout << "a=" << a << " " << "b=" << b < 47 | 48 | using namespace std; 49 | 50 | template 51 | class node 52 | { 53 | T value; 54 | node *prev,*next; 55 | 56 | public: 57 | 58 | node() 59 | { 60 | prev=NULL; 61 | next=NULL; 62 | } 63 | 64 | void set_value(T value) 65 | { 66 | this->value=value; 67 | } 68 | 69 | T get_value() 70 | { 71 | return value; 72 | } 73 | 74 | node * get_prev() 75 | { 76 | return prev; 77 | } 78 | 79 | node * get_next() 80 | { 81 | return next; 82 | } 83 | 84 | void append(node *p) 85 | { 86 | p->prev=this; 87 | if(next!=NULL) next->prev=p; 88 | p->next=next; 89 | next=p; 90 | } 91 | 92 | }; 93 | 94 | int main( ) 95 | { 96 | node *ptr; 97 | node node1,node2,node3; 98 | node1.set_value(97.5); 99 | node2.set_value(98.5); 100 | node3.set_value(99.5); 101 | node1.append(&node2); 102 | node2.append(&node3); 103 | 104 | for(ptr=&node1 ; ; ptr=ptr->get_next()) 105 | { 106 | cout << ptr->get_value() << endl; 107 | if(ptr->get_next()==NULL) break; 108 | } 109 | 110 | return 0; 111 | } 112 | 113 | -------------------------------------------------------------------------------- /misc/temporary-1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class student 6 | { 7 | private: 8 | char name[10]; 9 | 10 | public: 11 | int number; 12 | int score; 13 | 14 | student() 15 | { 16 | strcpy(name,"none"); 17 | number=0; 18 | score=0; 19 | } 20 | 21 | student(char const * text) 22 | { 23 | strcpy(name,text); 24 | number=0; 25 | score=0; 26 | } 27 | 28 | student(char const * text, int n, int s) 29 | { 30 | strcpy(name,text); 31 | number=n; 32 | score=s; 33 | } 34 | 35 | student(student & s) 36 | { 37 | strcpy(name,s.name); 38 | number=s.number; 39 | score=s.score; 40 | } 41 | 42 | ~student() 43 | { 44 | } 45 | 46 | void set_name(char const * text) 47 | { 48 | if(strlen(text)>=10) 49 | { 50 | cout << "length of " << text << " >= 10 " << endl; 51 | exit(0); 52 | } 53 | strcpy(name,text); 54 | } 55 | 56 | void display() 57 | { 58 | cout << name << " " << number << " " << score << endl; 59 | } 60 | 61 | }; 62 | 63 | int main() 64 | { 65 | student zs=student(); 66 | zs.display(); 67 | 68 | student ls=student("lisi"); 69 | ls.display(); 70 | 71 | student ww=student("wangwu,3,80"); 72 | ww.display(); 73 | 74 | return 0; 75 | } 76 | -------------------------------------------------------------------------------- /misc/temporary-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class student 6 | { 7 | private: 8 | char name[10]; 9 | 10 | public: 11 | int number; 12 | int score; 13 | 14 | student() 15 | { 16 | strcpy(name,"none"); 17 | number=0; 18 | score=0; 19 | } 20 | 21 | student(char const * text) 22 | { 23 | strcpy(name,text); 24 | number=0; 25 | score=0; 26 | } 27 | 28 | student(char const * text, int n, int s) 29 | { 30 | strcpy(name,text); 31 | number=n; 32 | score=s; 33 | } 34 | 35 | student(student const & s) 36 | { 37 | strcpy(name,s.name); 38 | number=s.number; 39 | score=s.score; 40 | } 41 | 42 | ~student() 43 | { 44 | } 45 | 46 | void set_name(char const * text) 47 | { 48 | if(strlen(text)>=10) 49 | { 50 | cout << "length of " << text << " >= 10 " << endl; 51 | exit(0); 52 | } 53 | strcpy(name,text); 54 | } 55 | 56 | void display() 57 | { 58 | cout << name << " " << number << " " << score << endl; 59 | } 60 | 61 | }; 62 | 63 | int main() 64 | { 65 | student zs=student(); 66 | zs.display(); 67 | 68 | student ls=student("lisi"); 69 | ls.display(); 70 | 71 | student ww=student("wangwu,3,80"); 72 | ww.display(); 73 | 74 | return 0; 75 | } 76 | -------------------------------------------------------------------------------- /misc/zoo-1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class animal 7 | { 8 | public: 9 | 10 | char name[8]; 11 | 12 | animal(const char * name) 13 | { 14 | strncpy(this->name, name, 7); 15 | this->name[7]='\0'; 16 | } 17 | 18 | void say_hello() 19 | { 20 | cout << "Hello" << endl; 21 | my_name(); 22 | cout << endl; 23 | } 24 | 25 | void my_name() 26 | { 27 | cout << "My name is "<< name << endl; 28 | } 29 | 30 | }; 31 | 32 | class dog : public animal 33 | { 34 | public: 35 | 36 | int birth; 37 | 38 | dog(const char * name, int birth) : animal(name) 39 | { 40 | this->birth=birth; 41 | } 42 | 43 | void say_hello() 44 | { 45 | cout << "Wang Wang" << endl; 46 | my_name(); 47 | my_birth(); 48 | cout << endl; 49 | } 50 | 51 | void my_birth() 52 | { 53 | cout << "I born in " << birth << endl; 54 | } 55 | }; 56 | 57 | class cat : public animal 58 | { 59 | public: 60 | 61 | char color[8]; 62 | 63 | cat(const char * name, const char * color) : animal(name) 64 | { 65 | strncpy(this->color, color, 7); 66 | this->color[7]='\0'; 67 | } 68 | 69 | void say_hello() 70 | { 71 | cout << "Miao Miao" << endl; 72 | my_name(); 73 | my_color(); 74 | cout << endl; 75 | } 76 | 77 | void my_color() 78 | { 79 | cout << "I am " << color << endl; 80 | } 81 | }; 82 | 83 | int main() 84 | { 85 | animal a("Animal"); 86 | dog b("WangCai", 2021); 87 | cat c("JiaFei", "BLACK"); 88 | 89 | cout << "--- At home ---" << endl; 90 | a.say_hello(); 91 | b.say_hello(); 92 | c.say_hello(); 93 | 94 | return 0; 95 | } -------------------------------------------------------------------------------- /misc/zoo-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class animal 7 | { 8 | public: 9 | 10 | char name[8]; 11 | 12 | animal(const char * name) 13 | { 14 | strncpy(this->name, name, 7); 15 | this->name[7]='\0'; 16 | } 17 | 18 | void say_hello() 19 | { 20 | cout << "Hello" << endl; 21 | my_name(); 22 | cout << endl; 23 | } 24 | 25 | void my_name() 26 | { 27 | cout << "My name is "<< name << endl; 28 | } 29 | 30 | }; 31 | 32 | class dog : public animal 33 | { 34 | public: 35 | 36 | int birth; 37 | 38 | dog(const char * name, int birth) : animal(name) 39 | { 40 | this->birth=birth; 41 | } 42 | 43 | void say_hello() 44 | { 45 | cout << "Wang Wang" << endl; 46 | my_name(); 47 | my_birth(); 48 | cout << endl; 49 | } 50 | 51 | void my_birth() 52 | { 53 | cout << "I born in " << birth << endl; 54 | } 55 | }; 56 | 57 | class cat : public animal 58 | { 59 | public: 60 | 61 | char color[8]; 62 | 63 | cat(const char * name, const char * color) : animal(name) 64 | { 65 | strncpy(this->color, color, 7); 66 | this->color[7]='\0'; 67 | } 68 | 69 | void say_hello() 70 | { 71 | cout << "Miao Miao" << endl; 72 | my_name(); 73 | my_color(); 74 | cout << endl; 75 | } 76 | 77 | void my_color() 78 | { 79 | cout << "I am " << color << endl; 80 | } 81 | }; 82 | 83 | int main() 84 | { 85 | animal a("Animal"); 86 | dog b("WangCai", 2021); 87 | cat c("JiaFei", "BLACK"); 88 | 89 | cout << "--- At home ---" << endl; 90 | a.say_hello(); 91 | b.say_hello(); 92 | c.say_hello(); 93 | 94 | cout << "--- At zoo ---" << endl; 95 | animal *zoo[3]={&a, &b, &c}; 96 | for(int i=0; i<3; i++) 97 | { 98 | zoo[i]->say_hello(); 99 | } 100 | 101 | return 0; 102 | } -------------------------------------------------------------------------------- /misc/zoo-3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class animal 7 | { 8 | public: 9 | 10 | char name[8]; 11 | 12 | animal(const char * name) 13 | { 14 | strncpy(this->name, name, 7); 15 | this->name[7]='\0'; 16 | } 17 | 18 | virtual void say_hello() 19 | { 20 | cout << "Hello" << endl; 21 | my_name(); 22 | cout << endl; 23 | } 24 | 25 | void my_name() 26 | { 27 | cout << "My name is "<< name << endl; 28 | } 29 | 30 | virtual void my_food() 31 | { 32 | cout << "I like food" << endl << endl; 33 | } 34 | }; 35 | 36 | class dog : public animal 37 | { 38 | public: 39 | 40 | int birth; 41 | 42 | dog(const char * name, int birth) : animal(name) 43 | { 44 | this->birth=birth; 45 | } 46 | 47 | void say_hello() 48 | { 49 | cout << "Wang Wang" << endl; 50 | my_name(); 51 | my_birth(); 52 | cout << endl; 53 | } 54 | 55 | void my_birth() 56 | { 57 | cout << "I born in " << birth << endl; 58 | } 59 | 60 | void my_food() 61 | { 62 | cout << "I like bone" << endl << endl; 63 | } 64 | }; 65 | 66 | class cat : public animal 67 | { 68 | public: 69 | 70 | char color[8]; 71 | 72 | cat(const char * name, const char * color) : animal(name) 73 | { 74 | strncpy(this->color, color, 7); 75 | this->color[7]='\0'; 76 | } 77 | 78 | void say_hello() 79 | { 80 | cout << "Miao Miao" << endl; 81 | my_name(); 82 | my_color(); 83 | cout << endl; 84 | } 85 | 86 | void my_color() 87 | { 88 | cout << "I am " << color << endl; 89 | } 90 | 91 | void my_food() 92 | { 93 | cout << "I like fish" << endl << endl; 94 | } 95 | }; 96 | 97 | int main() 98 | { 99 | animal a("Animal"); 100 | dog b("WangCai", 2021); 101 | cat c("JiaFei", "BLACK"); 102 | 103 | cout << "--- At zoo ---" << endl; 104 | animal *zoo[3]={&a, &b, &c}; 105 | for(int i=0; i<3; i++) 106 | { 107 | zoo[i]->say_hello(); 108 | zoo[i]->my_food(); 109 | } 110 | 111 | return 0; 112 | } -------------------------------------------------------------------------------- /misc/zoo-4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class animal 7 | { 8 | public: 9 | 10 | char name[8]; 11 | 12 | animal(const char * name) 13 | { 14 | strncpy(this->name, name, 7); 15 | this->name[7]='\0'; 16 | } 17 | 18 | virtual void say_hello() = 0; 19 | 20 | void my_name() 21 | { 22 | cout << "My name is "<< name << endl; 23 | } 24 | 25 | virtual void my_food() = 0; 26 | 27 | }; 28 | 29 | class dog : public animal 30 | { 31 | public: 32 | 33 | int birth; 34 | 35 | dog(const char * name, int birth) : animal(name) 36 | { 37 | this->birth=birth; 38 | } 39 | 40 | void say_hello() 41 | { 42 | cout << "Wang Wang" << endl; 43 | my_name(); 44 | my_birth(); 45 | cout << endl; 46 | } 47 | 48 | void my_birth() 49 | { 50 | cout << "I born in " << birth << endl; 51 | } 52 | 53 | void my_food() 54 | { 55 | cout << "I like bone" << endl << endl; 56 | } 57 | }; 58 | 59 | class cat : public animal 60 | { 61 | public: 62 | 63 | char color[8]; 64 | 65 | cat(const char * name, const char * color) : animal(name) 66 | { 67 | strncpy(this->color, color, 7); 68 | this->color[7]='\0'; 69 | } 70 | 71 | void say_hello() 72 | { 73 | cout << "Miao Miao" << endl; 74 | my_name(); 75 | my_color(); 76 | cout << endl; 77 | } 78 | 79 | void my_color() 80 | { 81 | cout << "I am " << color << endl; 82 | } 83 | 84 | void my_food() 85 | { 86 | cout << "I like fish" << endl << endl; 87 | } 88 | }; 89 | 90 | int main() 91 | { 92 | dog b1("Dog1", 2021); 93 | dog b2("Dog2", 2022); 94 | cat c1("Cat1", "BLACK"); 95 | cat c2("Cat2", "WHITE"); 96 | 97 | cout << "--- At zoo ---" << endl; 98 | 99 | animal *zoo[4]={&b1, &b2, &c1, &c2}; 100 | for(int i=0; i<4; i++) 101 | { 102 | zoo[i]->say_hello(); 103 | } 104 | 105 | return 0; 106 | } -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # 目录 2 | ## [基础](chapter-1.md) 3 | ## [封装](chapter-2.md) 4 | ## [继承](chapter-3.md) 5 | ## [多态](chapter-4.md) 6 | ## [实验](chapter-5.md) 7 | -------------------------------------------------------------------------------- /snake/acllib.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MisterFishing/cpp/1eb363fc03a1119d67808dfed624fb75bbc337ba/snake/acllib.c -------------------------------------------------------------------------------- /snake/acllib.h: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | //////////////////////////////////////////////////////////////// 17 | // ACLLib - Advanced C Lab Library 18 | // Ver 2014-07 19 | // For students' Lab at Zhejiang University 20 | // Created 2008 by Gao Yuan 21 | // Modified 2009 by Cui Liwei 22 | // 2010 by Lan Huidong 23 | // Revised 2012 by Li Rui 24 | // Modified 2014 by Weng Kai for MOOC 25 | //////////////////////////////////////////////////////////////// 26 | 27 | /* 28 | For Dev C++, these lib files need to be added into linker options. 29 | Be sure to change the leading folders as your installation. 30 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libwinmm.a" 31 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libmsimg32.a" 32 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libkernel32.a" 33 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libuser32.a" 34 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libgdi32.a" 35 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libole32.a" 36 | "C:/Program Files/Dev-Cpp/MinGW32/lib/liboleaut32.a" 37 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libuuid.a" 38 | */ 39 | 40 | #ifndef __ACLLIB_H__ 41 | #define __ACLLIB_H__ 42 | 43 | #ifdef _UNICODE 44 | #undef _UNICODE 45 | #endif 46 | #ifdef UNICODE 47 | #undef UNICODE 48 | #endif 49 | 50 | #include 51 | 52 | #define BLACK RGB(0, 0, 0) 53 | #define RED RGB(255, 0, 0) 54 | #define GREEN RGB(0, 255, 0) 55 | #define BLUE RGB(0, 0, 255) 56 | #define CYAN RGB(0, 255, 255) 57 | #define MAGENTA RGB(255, 0, 255) 58 | #define YELLOW RGB(255, 255, 0) 59 | #define WHITE RGB(255, 255, 255) 60 | 61 | #define EMPTY 0xffffffff 62 | #define DEFAULT -1 63 | 64 | typedef enum 65 | { 66 | PEN_STYLE_SOLID, 67 | PEN_STYLE_DASH, /* ------- */ 68 | PEN_STYLE_DOT, /* ....... */ 69 | PEN_STYLE_DASHDOT, /* _._._._ */ 70 | PEN_STYLE_DASHDOTDOT, /* _.._.._ */ 71 | PEN_STYLE_NULL 72 | } ACL_Pen_Style; 73 | 74 | typedef enum 75 | { 76 | BRUSH_STYLE_SOLID = -1, 77 | BRUSH_STYLE_HORIZONTAL, /* ----- */ 78 | BRUSH_STYLE_VERTICAL, /* ||||| */ 79 | BRUSH_STYLE_FDIAGONAL, /* \\\\\ */ 80 | BRUSH_STYLE_BDIAGONAL, /* ///// */ 81 | BRUSH_STYLE_CROSS, /* +++++ */ 82 | BRUSH_STYLE_DIAGCROSS, /* xxxxx */ 83 | BRUSH_STYLE_NULL 84 | } ACL_Brush_Style; 85 | 86 | typedef enum 87 | { 88 | NO_BUTTON = 0, 89 | LEFT_BUTTON, 90 | MIDDLE_BUTTON, 91 | RIGHT_BUTTON 92 | } ACL_Mouse_Button; 93 | 94 | typedef enum 95 | { 96 | BUTTON_DOWN, 97 | BUTTON_DOUBLECLICK, 98 | BUTTON_UP, 99 | ROLL_UP, 100 | ROLL_DOWN, 101 | MOUSEMOVE 102 | } ACL_Mouse_Event; 103 | 104 | typedef enum 105 | { 106 | KEY_DOWN, 107 | KEY_UP 108 | } ACL_Keyboard_Event; 109 | 110 | typedef struct 111 | { 112 | HBITMAP hbitmap; 113 | int width; 114 | int height; 115 | } ACL_Image; 116 | 117 | //typedef enum 118 | //{ 119 | // TM_NO = 0x00, 120 | // TM_COLOR = 0x01, 121 | // TM_ALPHA = 0x02 122 | //} ACL_TransparentMode; 123 | 124 | typedef COLORREF ACL_Color; 125 | typedef int ACL_Sound; 126 | 127 | typedef void(*KeyboardEventCallback) (int key, int event); 128 | typedef void(*CharEventCallback) (char c); 129 | typedef void(*MouseEventCallback) (int x, int y, int button, int event); 130 | typedef void(*TimerEventCallback) (int timerID); 131 | 132 | #ifdef __cplusplus 133 | extern "C" { 134 | #endif 135 | 136 | int Setup(void); 137 | 138 | // 139 | void initWindow(const char title[], int left, int top, int width, int height); 140 | void msgBox(const char title[], const char text[], int flag); 141 | 142 | void registerKeyboardEvent(KeyboardEventCallback callback); 143 | void registerCharEvent(CharEventCallback callback); 144 | void registerMouseEvent(MouseEventCallback callback); 145 | void registerTimerEvent(TimerEventCallback callback); 146 | 147 | void startTimer(int timerID, int timeinterval); 148 | void cancelTimer(int timerID); 149 | 150 | // Sound 151 | void loadSound(const char *fileName, ACL_Sound *pSound); 152 | void playSound(ACL_Sound soundID, int repeat); 153 | void stopSound(ACL_Sound soundID); 154 | 155 | // Paint 156 | void beginPaint(); 157 | void endPaint(); 158 | void clearDevice(void); 159 | int getWidth(); 160 | int getHeight(); 161 | 162 | // Pen 163 | void setPenColor(ACL_Color color); 164 | void setPenWidth(int width); 165 | void setPenStyle(ACL_Pen_Style style); 166 | 167 | // Brush 168 | void setBrushColor(ACL_Color color); 169 | void setBrushStyle(ACL_Brush_Style style); 170 | 171 | // Text 172 | void setTextColor(ACL_Color color); 173 | void setTextBkColor(ACL_Color color); 174 | void setTextSize(int size); 175 | void setTextFont(const char *pFontName); 176 | 177 | void paintText(int x, int y, const char *pStr); 178 | 179 | void setCaretSize(int w, int h); 180 | void setCaretPos(int x, int y); 181 | void showCaret(); 182 | void hideCaret(); 183 | 184 | // Pixel 185 | void putPixel(int x, int y, ACL_Color color); 186 | ACL_Color getPixel(int x, int y); 187 | 188 | // the Point 189 | int getX(void); 190 | int getY(void); 191 | void moveTo(int x, int y); 192 | void moveRel(int dx, int dy); 193 | 194 | // Lines and Curves 195 | void arc(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, \ 196 | int nXStartArc, int nYStartArc, int nXEndArc, int nYEndArc); 197 | void line(int x0, int y0, int x1, int y1); 198 | void lineTo(int nXEnd, int nYEnd); 199 | void lineRel(int dx, int dy); 200 | void polyBezier(const POINT *lppt, int cPoints); 201 | void polyLine(const POINT *lppt, int cPoints); 202 | 203 | // Filled Shapes 204 | void chrod(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, \ 205 | int nXRadial1, int nYRadial1, int nXRadial2, int nYRadial2); 206 | void ellipse(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect); 207 | void pie(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, \ 208 | int nXRadial1, int nYRadial1, int nXRadial2, int nYRadial2); 209 | void polygon(const POINT *lpPoints, int nCount); 210 | void rectangle(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect); 211 | void roundrect(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, \ 212 | int nWidth, int nHeight); 213 | 214 | // Image 215 | void loadImage(const char *pImageFileName, ACL_Image *pImage); 216 | void freeImage(ACL_Image *pImage); 217 | 218 | void putImage(ACL_Image *pImage, int x, int y); 219 | void putImageScale(ACL_Image *pImage, int x, int y, int width, int height); 220 | void putImageTransparent(ACL_Image *pImage, int x, int y, int width, int height, ACL_Color bkColor); 221 | 222 | //void putImageEx(ACL_Image *pImage,int dx,int dy,int dw,int dh, 223 | // int sx,int sy,int sw,int sh); 224 | 225 | //void setTransparentMode(ACL_TransparenetMode); 226 | //void setTransparentColor(ACL_Color); 227 | //void setTransparetnAlpha(int alpha); 228 | 229 | void initConsole(void); 230 | 231 | #ifdef __cplusplus 232 | } 233 | #endif 234 | 235 | #endif 236 | -------------------------------------------------------------------------------- /snake/readme.md: -------------------------------------------------------------------------------- 1 | gcc *.c -lgdi32 -lole32 -loleaut32 -luuid -lwinmm -lmsimg32 -o snake.exe -------------------------------------------------------------------------------- /snake/snake.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "acllib.h" 4 | #include 5 | #include //用到产生随机数 6 | #include 7 | 8 | #define WIDTH 640 9 | #define HEIGHT 480 10 | 11 | typedef struct FOOD { 12 | int x; //食物的横坐标; 13 | int y; //食物的纵坐标; 14 | int yes; //判断是否要出现食物; 15 | } food; //定义食物的结构体 16 | 17 | typedef struct SNAKE { 18 | int x[200]; 19 | int y[200]; 20 | int jieshu; //蛇的节数; 21 | int direction; //蛇的移动方向 22 | int life; //蛇的存活 23 | } snake; //定义蛇的结构体 24 | 25 | food Food; 26 | snake Snake1, Snake2; 27 | 28 | void over() 29 | { 30 | cancelTimer(0); 31 | 32 | beginPaint(); 33 | setTextBkColor(EMPTY); 34 | setTextSize(80); 35 | 36 | if(Snake1.life==0&&Snake2.life==0){ 37 | if (Snake1.jieshu>Snake2.jieshu) { 38 | setTextColor(RED); 39 | paintText(180,150,"RED WIN"); 40 | } else if (Snake1.jieshu0;i--) { //关键!! 76 | Snake1.x[i]=Snake1.x[i-1]; //蛇往前移动了,所以前一个坐标给到后一个坐标 77 | Snake1.y[i]=Snake1.y[i-1]; 78 | } 79 | 80 | for (i=Snake2.jieshu-1;i>0;i--) { //关键!! 81 | Snake2.x[i]=Snake2.x[i-1]; //蛇往前移动了,所以前一个坐标给到后一个坐标 82 | Snake2.y[i]=Snake2.y[i-1]; 83 | } 84 | 85 | switch (Snake1.direction) { //判断蛇头的移动方向,1234表示右左上下 86 | case 1:Snake1.x[0]+=10;break; //向右移动十格 87 | case 2:Snake1.x[0]-=10;break; 88 | case 3:Snake1.y[0]-=10;break; 89 | case 4:Snake1.y[0]+=10;break; 90 | } 91 | 92 | switch (Snake2.direction) { //判断蛇头的移动方向,1234表示右左上下 93 | case 1:Snake2.x[0]+=10;break; //向右移动十格 94 | case 2:Snake2.x[0]-=10;break; 95 | case 3:Snake2.y[0]-=10;break; 96 | case 4:Snake2.y[0]+=10;break; 97 | } 98 | 99 | //判断蛇是否撞到自己 100 | for (i=1;iWIDTH-10||Snake1.y[0]<0||Snake1.y[0]>HEIGHT-10) { 127 | Snake1.life=0; 128 | } 129 | 130 | if (Snake2.x[0]<0||Snake2.x[0]>WIDTH-10||Snake2.y[0]<0||Snake2.y[0]>HEIGHT-10) { 131 | Snake2.life=0; 132 | } 133 | 134 | if(Snake1.life==0 || Snake2.life==0){ 135 | over(); 136 | return; 137 | } 138 | 139 | if (Snake1.x[0]==Food.x&&Snake1.y[0]==Food.y) { 140 | Food.yes=0; //产生新的食物 141 | Snake1.jieshu++; //蛇的身体加长一节 142 | } 143 | 144 | if (Snake2.x[0]==Food.x&&Snake2.y[0]==Food.y) { 145 | Food.yes=0; //产生新的食物 146 | Snake2.jieshu++; //蛇的身体加长一节 147 | } 148 | } 149 | 150 | void paint() 151 | { 152 | int i; 153 | 154 | beginPaint(); 155 | 156 | setBrushColor(GREEN); // Food 157 | setBrushColor(GREEN); 158 | rectangle(Food.x,Food.y,Food.x+10,Food.y+10); 159 | 160 | setBrushColor(RED); // Snake1 161 | setPenColor(RED); 162 | for (i=0;ix=x; 24 | this->y=y; 25 | this->dx=dx; 26 | this->dy=dy; 27 | winWidth=wWidth; 28 | winHeight=wHeight; 29 | } 30 | struct rect CautoSprite::getRect() 31 | { 32 | struct rect r; 33 | r.x=x; 34 | r.y=y; 35 | r.width=width; 36 | r.height=height; 37 | return r; 38 | } 39 | void CautoSprite::move() 40 | { 41 | x+=dx; 42 | y+=dy; 43 | if(x<0 || x> winWidth-width) dx*=-1; 44 | if(y<0 || y>winHeight-height) dy*=-1; 45 | } 46 | void CautoSprite::drawScale(int w,int h) 47 | { 48 | putImageScale(&img,x,y,w,h); 49 | } -------------------------------------------------------------------------------- /sprite/encapsulation/CautoSprite.h: -------------------------------------------------------------------------------- 1 | #ifndef CautoSprite_h 2 | #define CautoSprite_h 3 | 4 | #include"acllib.h" 5 | 6 | struct rect 7 | { 8 | int x,y,width,height; 9 | }; 10 | class CautoSprite 11 | { 12 | ACL_Image img; 13 | int x,y; 14 | int dx,dy; 15 | int width,height; 16 | int winWidth,winHeight; 17 | public: 18 | //CautoSprite(void); 19 | CautoSprite(CautoSprite &); 20 | CautoSprite(char const *name,int x,int y,int dx,int dy,int w,int h,int wWidth,int wHeight); 21 | ~CautoSprite(void); 22 | void move(); 23 | void drawScale(int w,int h); 24 | struct rect getRect(); 25 | }; 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /sprite/encapsulation/CusrSprite.cpp: -------------------------------------------------------------------------------- 1 | #include "CusrSprite.h" 2 | 3 | CusrSprite::CusrSprite(CusrSprite & a) 4 | { 5 | img=a.img; 6 | x=a.x; 7 | y=a.y; 8 | dx=a.dx; 9 | dy=a.dy; 10 | width=a.width; 11 | height=a.height; 12 | winWidth=a.winWidth; 13 | winHeight=a.winHeight; 14 | } 15 | CusrSprite::CusrSprite(char const *name,int x,int y,int dx,int dy,int w,int h,int wWidth,int wHeight) 16 | { 17 | loadImage(name,&img); 18 | width=w; 19 | height=h; 20 | this->x=x; 21 | this->y=y; 22 | this->dx=dx; 23 | this->dy=dy; 24 | winWidth=wWidth; 25 | winHeight=wHeight; 26 | } 27 | 28 | 29 | CusrSprite::~CusrSprite(void) 30 | { 31 | } 32 | void CusrSprite::keyMove(int key) 33 | { 34 | switch(key) 35 | { 36 | case VK_UP:y-=dy; 37 | if(y<0)y=0; 38 | break; 39 | case VK_DOWN: 40 | y+=dy; 41 | if(y>=winHeight-height)y = winHeight-height; 42 | break; 43 | case VK_LEFT: 44 | x -=dx; 45 | if(x<=0)x=0; 46 | break; 47 | case VK_RIGHT: 48 | x+=dx; 49 | if(x>=winWidth-width)x = winWidth-width; 50 | break; 51 | } 52 | } 53 | int CusrSprite::collision(CautoSprite &a){ int c = 1; struct rect r2=a.getRect(); if (x < r2.x && x+width >r2.x ){ if (y > r2.y && yr2.y)return c; } else{ if (x > r2.x && r2.x + r2.width > x) { if (y > r2.y && yr2.y)return c; } } c = 0; return c; } void CusrSprite::drawScale(int w,int h) { putImageScale(&img,x,y,w,h); } 54 | -------------------------------------------------------------------------------- /sprite/encapsulation/CusrSprite.h: -------------------------------------------------------------------------------- 1 | #ifndef CusrSprite_h 2 | #define CusrSprite_h 3 | 4 | #include"acllib.h" 5 | #include"CautoSprite.h" 6 | 7 | class CusrSprite 8 | { 9 | ACL_Image img; 10 | int x,y; 11 | int dx,dy; 12 | int width,height; 13 | int winWidth,winHeight; 14 | public: 15 | CusrSprite(CusrSprite &); 16 | CusrSprite(char const *name,int x,int y,int dx,int dy,int w,int h,int wWidth,int wHeight); 17 | ~CusrSprite(void); 18 | 19 | void keyMove(int key); 20 | int collision(CautoSprite &a); 21 | void drawScale(int w,int h); 22 | }; 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /sprite/encapsulation/acllib.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MisterFishing/cpp/1eb363fc03a1119d67808dfed624fb75bbc337ba/sprite/encapsulation/acllib.c -------------------------------------------------------------------------------- /sprite/encapsulation/acllib.h: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | //////////////////////////////////////////////////////////////// 17 | // ACLLib - Advanced C Lab Library 18 | // Ver 2014-07 19 | // For students' Lab at Zhejiang University 20 | // Created 2008 by Gao Yuan 21 | // Modified 2009 by Cui Liwei 22 | // 2010 by Lan Huidong 23 | // Revised 2012 by Li Rui 24 | // Modified 2014 by Weng Kai for MOOC 25 | //////////////////////////////////////////////////////////////// 26 | 27 | /* 28 | For Dev C++, these lib files need to be added into linker options. 29 | Be sure to change the leading folders as your installation. 30 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libwinmm.a" 31 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libmsimg32.a" 32 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libkernel32.a" 33 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libuser32.a" 34 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libgdi32.a" 35 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libole32.a" 36 | "C:/Program Files/Dev-Cpp/MinGW32/lib/liboleaut32.a" 37 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libuuid.a" 38 | */ 39 | 40 | #ifndef __ACLLIB_H__ 41 | #define __ACLLIB_H__ 42 | 43 | #ifdef _UNICODE 44 | #undef _UNICODE 45 | #endif 46 | #ifdef UNICODE 47 | #undef UNICODE 48 | #endif 49 | 50 | #include 51 | 52 | #define BLACK RGB(0, 0, 0) 53 | #define RED RGB(255, 0, 0) 54 | #define GREEN RGB(0, 255, 0) 55 | #define BLUE RGB(0, 0, 255) 56 | #define CYAN RGB(0, 255, 255) 57 | #define MAGENTA RGB(255, 0, 255) 58 | #define YELLOW RGB(255, 255, 0) 59 | #define WHITE RGB(255, 255, 255) 60 | 61 | #define EMPTY 0xffffffff 62 | #define DEFAULT -1 63 | 64 | typedef enum 65 | { 66 | PEN_STYLE_SOLID, 67 | PEN_STYLE_DASH, /* ------- */ 68 | PEN_STYLE_DOT, /* ....... */ 69 | PEN_STYLE_DASHDOT, /* _._._._ */ 70 | PEN_STYLE_DASHDOTDOT, /* _.._.._ */ 71 | PEN_STYLE_NULL 72 | } ACL_Pen_Style; 73 | 74 | typedef enum 75 | { 76 | BRUSH_STYLE_SOLID = -1, 77 | BRUSH_STYLE_HORIZONTAL, /* ----- */ 78 | BRUSH_STYLE_VERTICAL, /* ||||| */ 79 | BRUSH_STYLE_FDIAGONAL, /* \\\\\ */ 80 | BRUSH_STYLE_BDIAGONAL, /* ///// */ 81 | BRUSH_STYLE_CROSS, /* +++++ */ 82 | BRUSH_STYLE_DIAGCROSS, /* xxxxx */ 83 | BRUSH_STYLE_NULL 84 | } ACL_Brush_Style; 85 | 86 | typedef enum 87 | { 88 | NO_BUTTON = 0, 89 | LEFT_BUTTON, 90 | MIDDLE_BUTTON, 91 | RIGHT_BUTTON 92 | } ACL_Mouse_Button; 93 | 94 | typedef enum 95 | { 96 | BUTTON_DOWN, 97 | BUTTON_DOUBLECLICK, 98 | BUTTON_UP, 99 | ROLL_UP, 100 | ROLL_DOWN, 101 | MOUSEMOVE 102 | } ACL_Mouse_Event; 103 | 104 | typedef enum 105 | { 106 | KEY_DOWN, 107 | KEY_UP 108 | } ACL_Keyboard_Event; 109 | 110 | typedef struct 111 | { 112 | HBITMAP hbitmap; 113 | int width; 114 | int height; 115 | } ACL_Image; 116 | 117 | //typedef enum 118 | //{ 119 | // TM_NO = 0x00, 120 | // TM_COLOR = 0x01, 121 | // TM_ALPHA = 0x02 122 | //} ACL_TransparentMode; 123 | 124 | typedef COLORREF ACL_Color; 125 | typedef int ACL_Sound; 126 | 127 | typedef void(*KeyboardEventCallback) (int key, int event); 128 | typedef void(*CharEventCallback) (char c); 129 | typedef void(*MouseEventCallback) (int x, int y, int button, int event); 130 | typedef void(*TimerEventCallback) (int timerID); 131 | 132 | #ifdef __cplusplus 133 | extern "C" { 134 | #endif 135 | 136 | int Setup(void); 137 | 138 | // 139 | void initWindow(const char title[], int left, int top, int width, int height); 140 | void msgBox(const char title[], const char text[], int flag); 141 | 142 | void registerKeyboardEvent(KeyboardEventCallback callback); 143 | void registerCharEvent(CharEventCallback callback); 144 | void registerMouseEvent(MouseEventCallback callback); 145 | void registerTimerEvent(TimerEventCallback callback); 146 | 147 | void startTimer(int timerID, int timeinterval); 148 | void cancelTimer(int timerID); 149 | 150 | // Sound 151 | void loadSound(const char *fileName, ACL_Sound *pSound); 152 | void playSound(ACL_Sound soundID, int repeat); 153 | void stopSound(ACL_Sound soundID); 154 | 155 | // Paint 156 | void beginPaint(); 157 | void endPaint(); 158 | void clearDevice(void); 159 | int getWidth(); 160 | int getHeight(); 161 | 162 | // Pen 163 | void setPenColor(ACL_Color color); 164 | void setPenWidth(int width); 165 | void setPenStyle(ACL_Pen_Style style); 166 | 167 | // Brush 168 | void setBrushColor(ACL_Color color); 169 | void setBrushStyle(ACL_Brush_Style style); 170 | 171 | // Text 172 | void setTextColor(ACL_Color color); 173 | void setTextBkColor(ACL_Color color); 174 | void setTextSize(int size); 175 | void setTextFont(const char *pFontName); 176 | 177 | void paintText(int x, int y, const char *pStr); 178 | 179 | void setCaretSize(int w, int h); 180 | void setCaretPos(int x, int y); 181 | void showCaret(); 182 | void hideCaret(); 183 | 184 | // Pixel 185 | void putPixel(int x, int y, ACL_Color color); 186 | ACL_Color getPixel(int x, int y); 187 | 188 | // the Point 189 | int getX(void); 190 | int getY(void); 191 | void moveTo(int x, int y); 192 | void moveRel(int dx, int dy); 193 | 194 | // Lines and Curves 195 | void arc(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, \ 196 | int nXStartArc, int nYStartArc, int nXEndArc, int nYEndArc); 197 | void line(int x0, int y0, int x1, int y1); 198 | void lineTo(int nXEnd, int nYEnd); 199 | void lineRel(int dx, int dy); 200 | void polyBezier(const POINT *lppt, int cPoints); 201 | void polyLine(const POINT *lppt, int cPoints); 202 | 203 | // Filled Shapes 204 | void chrod(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, \ 205 | int nXRadial1, int nYRadial1, int nXRadial2, int nYRadial2); 206 | void ellipse(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect); 207 | void pie(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, \ 208 | int nXRadial1, int nYRadial1, int nXRadial2, int nYRadial2); 209 | void polygon(const POINT *lpPoints, int nCount); 210 | void rectangle(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect); 211 | void roundrect(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, \ 212 | int nWidth, int nHeight); 213 | 214 | // Image 215 | void loadImage(const char *pImageFileName, ACL_Image *pImage); 216 | void freeImage(ACL_Image *pImage); 217 | 218 | void putImage(ACL_Image *pImage, int x, int y); 219 | void putImageScale(ACL_Image *pImage, int x, int y, int width, int height); 220 | void putImageTransparent(ACL_Image *pImage, int x, int y, int width, int height, ACL_Color bkColor); 221 | 222 | //void putImageEx(ACL_Image *pImage,int dx,int dy,int dw,int dh, 223 | // int sx,int sy,int sw,int sh); 224 | 225 | //void setTransparentMode(ACL_TransparenetMode); 226 | //void setTransparentColor(ACL_Color); 227 | //void setTransparetnAlpha(int alpha); 228 | 229 | void initConsole(void); 230 | 231 | #ifdef __cplusplus 232 | } 233 | #endif 234 | 235 | #endif 236 | -------------------------------------------------------------------------------- /sprite/encapsulation/cat.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MisterFishing/cpp/1eb363fc03a1119d67808dfed624fb75bbc337ba/sprite/encapsulation/cat.jpg -------------------------------------------------------------------------------- /sprite/encapsulation/dog.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MisterFishing/cpp/1eb363fc03a1119d67808dfed624fb75bbc337ba/sprite/encapsulation/dog.jpg -------------------------------------------------------------------------------- /sprite/encapsulation/game.cpp: -------------------------------------------------------------------------------- 1 | #include"acllib.h" 2 | #include"CautoSprite.h" 3 | #include"CusrSprite.h" 4 | #include 5 | #include 6 | 7 | const int winWidth = 880, winHeight = 600,width=100,height=100; 8 | const int MAXNUM=50; 9 | CautoSprite *autos[MAXNUM]={NULL}; 10 | CusrSprite *usr=NULL; 11 | void timerEvent(int id); 12 | void paintSprites(); 13 | void keyEvent(int key,int e); 14 | int d=0; 15 | int Setup() 16 | { 17 | initWindow("load Image", DEFAULT, DEFAULT, winWidth, winHeight); 18 | srand((unsigned)time(NULL)); 19 | int x,y,dx,dy; 20 | x=rand()%winWidth; 21 | y=rand()%winHeight; 22 | if(x>winWidth-width)x=winWidth-width; 23 | if(y>winHeight-height)y=winHeight-height; 24 | dx=dy=5; 25 | usr=new CusrSprite("dog.jpg",x,y,dx,dy,width,height,winWidth,winHeight); 26 | registerTimerEvent(timerEvent); 27 | startTimer(0,500); 28 | startTimer(1,50); 29 | registerKeyboardEvent(keyEvent); 30 | paintSprites(); 31 | return 0; 32 | } 33 | void timerEvent(int id) 34 | { 35 | switch(id) 36 | { 37 | case 0: 38 | if(d>=MAXNUM) return; 39 | int x,y,dx,dy; 40 | x=rand()%winWidth; 41 | y=rand()%winHeight; 42 | if(x>winWidth-width)x=winWidth-width; 43 | if(y>winHeight-height)y=winHeight-height; 44 | dx=rand()%5+1; 45 | dy=rand()%5+1; 46 | autos[d]=new CautoSprite("cat.jpg",x,y,dx,dy,width,height,winWidth,winHeight); 47 | if(autos[d]) 48 | d++; 49 | 50 | break; 51 | case 1: 52 | for(int i=0;imove(); 55 | 56 | paintSprites(); 57 | 58 | break; 59 | } 60 | } 61 | void paintSprites() 62 | { 63 | int i; 64 | beginPaint(); 65 | clearDevice(); 66 | for(i=0;idrawScale(width,height); 71 | } 72 | } 73 | usr->drawScale(width,height); 74 | endPaint(); 75 | } 76 | void keyEvent(int key,int e) 77 | { 78 | if(e!=KEY_DOWN)return; 79 | usr->keyMove(key); 80 | for(int i=0;icollision(*autos[i])) 85 | { 86 | delete autos[i]; 87 | autos[i]=NULL; 88 | } 89 | } 90 | } 91 | paintSprites(); 92 | } -------------------------------------------------------------------------------- /sprite/encapsulation/readme.md: -------------------------------------------------------------------------------- 1 | gcc -c *.c 2 | 3 | g++ -c *.cpp 4 | 5 | g++ *.o -lgdi32 -lole32 -loleaut32 -luuid -lwinmm -lmsimg32 -o sprite.exe 6 | 7 | -------------------------------------------------------------------------------- /sprite/encapsulation/sprite.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MisterFishing/cpp/1eb363fc03a1119d67808dfed624fb75bbc337ba/sprite/encapsulation/sprite.exe -------------------------------------------------------------------------------- /sprite/inheritance/AutoSprite.cpp: -------------------------------------------------------------------------------- 1 | #include "AutoSprite.h" 2 | 3 | CAutoSprite::CAutoSprite(int x, int y, int width, int height, int dx, int dy, ACL_Image *img, rect r1):SpriteBase(x,y,width,height,dx,dy,img,r1) 4 | { 5 | } 6 | CAutoSprite::CAutoSprite(CAutoSprite &sprite):SpriteBase(sprite) 7 | { 8 | 9 | } 10 | 11 | CAutoSprite::~CAutoSprite() 12 | { 13 | } 14 | void CAutoSprite::move() 15 | { 16 | x += dx; 17 | y += dy; 18 | if (x(r.x + r.width - width))dx *= -1; 19 | if (y(r.y + r.height - height))dy = -1; 20 | } 21 | -------------------------------------------------------------------------------- /sprite/inheritance/AutoSprite.h: -------------------------------------------------------------------------------- 1 | #ifndef AutoSprite_h 2 | #define AutoSprite_h 3 | 4 | #include "SpriteBase.h" 5 | 6 | class CAutoSprite : 7 | public SpriteBase 8 | { 9 | public: 10 | CAutoSprite(int x, int y, int width, int height, int dx, int dy, ACL_Image *img, rect r1); 11 | CAutoSprite(CAutoSprite &sprite); 12 | ~CAutoSprite(); 13 | 14 | void move(); 15 | }; 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /sprite/inheritance/SpriteBase.cpp: -------------------------------------------------------------------------------- 1 | #include "SpriteBase.h" 2 | 3 | SpriteBase::SpriteBase(int x, int y, int width, int height, int dx, int dy, ACL_Image *img, rect r1) 4 | { 5 | this->x = x; 6 | this->y = y; 7 | this->width = width; 8 | this->height = height; 9 | this->dx = dx; 10 | this->dy = dy; 11 | this->img = img; 12 | r = r1; 13 | } 14 | SpriteBase::SpriteBase(SpriteBase & sprite) 15 | { 16 | x = sprite.x; 17 | y = sprite.y; 18 | width = sprite.width; 19 | height = sprite.height; 20 | dx = sprite.dx; 21 | dy = sprite.dy; 22 | img = sprite.img; 23 | r = sprite.r; 24 | } 25 | SpriteBase::~SpriteBase() 26 | { 27 | 28 | } 29 | 30 | void SpriteBase::move() 31 | { 32 | 33 | } 34 | void SpriteBase::drawSprite(int width, int height) 35 | { 36 | putImageScale(img, x, y, width, height); 37 | } 38 | void SpriteBase::drawSprite() 39 | { 40 | putImageScale(img, x, y, width, height); 41 | } 42 | rect SpriteBase::getRect() 43 | { 44 | rect r = { x, y, width, height }; 45 | return r; 46 | } -------------------------------------------------------------------------------- /sprite/inheritance/SpriteBase.h: -------------------------------------------------------------------------------- 1 | #ifndef SpriteBase_h 2 | #define SpriteBase_h 3 | 4 | #include"acllib.h" 5 | 6 | class SpriteBase 7 | { 8 | protected: 9 | int x, y; 10 | int width, height; 11 | int dx, dy; 12 | ACL_Image *img; 13 | rect r; 14 | public: 15 | SpriteBase(int x, int y, int width, int height, int dx, int dy, ACL_Image *img, rect r1); 16 | SpriteBase(SpriteBase & base); 17 | ~SpriteBase(); 18 | 19 | void move(); 20 | void drawSprite(int width, int height); 21 | void drawSprite(); 22 | rect getRect(); 23 | }; 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /sprite/inheritance/UsrSprite.cpp: -------------------------------------------------------------------------------- 1 | #include "UsrSprite.h" 2 | 3 | CUsrSprite::CUsrSprite(int x, int y, int width, int height, int dx, int dy, ACL_Image *img, rect r1) 4 | :SpriteBase(x,y,width,height,dx,dy,img,r1) 5 | { 6 | 7 | } 8 | CUsrSprite::CUsrSprite(CUsrSprite &sprite):SpriteBase(sprite) 9 | { 10 | 11 | } 12 | CUsrSprite::~CUsrSprite(){} 13 | 14 | void CUsrSprite::move(int xx, int yy) 15 | { 16 | x = xx; 17 | y = yy; 18 | if (x < r.x)x = r.x; 19 | if (x >(r.x + r.width - width))x = (r.x + r.width - width); 20 | if (y < r.y)y = r.y; 21 | if (y >(r.y + r.height - height))y = (r.y + r.height - height); 22 | 23 | } 24 | void CUsrSprite::move(int key) 25 | { 26 | switch (key) 27 | { 28 | case VK_UP: 29 | y -= dx; 30 | if (y < r.y)y = r.y; 31 | break; 32 | case VK_DOWN: 33 | y += dy; 34 | if (y >(r.y + r.height - height))y = (r.y + r.height - height); 35 | 36 | break; 37 | case VK_LEFT: 38 | x -= dx; 39 | if (x < r.x)x = r.x; 40 | break; 41 | case VK_RIGHT: 42 | x += dx; 43 | if (x >(r.x + r.width - width))x = (r.x + r.width - width); 44 | 45 | break; 46 | } 47 | } 48 | 49 | int CUsrSprite::collision(rect r2) 50 | { 51 | rect r1 = { x,y, width, height }; 52 | int c = 1; 53 | if (r1.x < r2.x && r1.x + r1.width >r2.x) { 54 | if (r1.y > r2.y && r1.yr2.y)return c; 56 | } 57 | else { 58 | if (r1.x > r2.x && r2.x + r2.width > r1.x) 59 | { 60 | if (r1.y > r2.y && r1.yr2.y)return c; 62 | } 63 | } 64 | c = 0; 65 | return c; 66 | } 67 | int CUsrSprite::getScore() 68 | { 69 | return score; 70 | } 71 | void CUsrSprite::setScore(int s) 72 | { 73 | score = s; 74 | } 75 | void CUsrSprite::addScore(int m) 76 | { 77 | score += m; 78 | } 79 | -------------------------------------------------------------------------------- /sprite/inheritance/UsrSprite.h: -------------------------------------------------------------------------------- 1 | #ifndef UsrSprite_h 2 | #define UsrSprite_h 3 | 4 | #include "SpriteBase.h" 5 | 6 | class CUsrSprite : 7 | public SpriteBase 8 | { 9 | int score; 10 | public: 11 | CUsrSprite(int x, int y, int width, int height, int dx, int dy, ACL_Image *img, rect r1); 12 | CUsrSprite(CUsrSprite &sprite); 13 | ~CUsrSprite(); 14 | 15 | void move(int x,int y); 16 | void move(int key); 17 | 18 | int collision(rect r1); 19 | int getScore(); 20 | void setScore(int s); 21 | void addScore(int m); 22 | }; 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /sprite/inheritance/acllib.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MisterFishing/cpp/1eb363fc03a1119d67808dfed624fb75bbc337ba/sprite/inheritance/acllib.c -------------------------------------------------------------------------------- /sprite/inheritance/acllib.h: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | //////////////////////////////////////////////////////////////// 17 | // ACLLib - Advanced C Lab Library 18 | // Ver 2014-07 19 | // For students' Lab at Zhejiang University 20 | // Created 2008 by Gao Yuan 21 | // Modified 2009 by Cui Liwei 22 | // 2010 by Lan Huidong 23 | // Revised 2012 by Li Rui 24 | // Modified 2014 by Weng Kai for MOOC 25 | //////////////////////////////////////////////////////////////// 26 | 27 | /* 28 | For Dev C++, these lib files need to be added into linker options. 29 | Be sure to change the leading folders as your installation. 30 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libwinmm.a" 31 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libmsimg32.a" 32 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libkernel32.a" 33 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libuser32.a" 34 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libgdi32.a" 35 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libole32.a" 36 | "C:/Program Files/Dev-Cpp/MinGW32/lib/liboleaut32.a" 37 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libuuid.a" 38 | */ 39 | 40 | #ifndef __ACLLIB_H__ 41 | #define __ACLLIB_H__ 42 | 43 | #ifdef _UNICODE 44 | #undef _UNICODE 45 | #endif 46 | #ifdef UNICODE 47 | #undef UNICODE 48 | #endif 49 | 50 | #include 51 | 52 | #define BLACK RGB(0, 0, 0) 53 | #define RED RGB(255, 0, 0) 54 | #define GREEN RGB(0, 255, 0) 55 | #define BLUE RGB(0, 0, 255) 56 | #define CYAN RGB(0, 255, 255) 57 | #define MAGENTA RGB(255, 0, 255) 58 | #define YELLOW RGB(255, 255, 0) 59 | #define WHITE RGB(255, 255, 255) 60 | 61 | #define EMPTY 0xffffffff 62 | #define DEFAULT -1 63 | 64 | typedef enum 65 | { 66 | PEN_STYLE_SOLID, 67 | PEN_STYLE_DASH, /* ------- */ 68 | PEN_STYLE_DOT, /* ....... */ 69 | PEN_STYLE_DASHDOT, /* _._._._ */ 70 | PEN_STYLE_DASHDOTDOT, /* _.._.._ */ 71 | PEN_STYLE_NULL 72 | } ACL_Pen_Style; 73 | 74 | typedef enum 75 | { 76 | BRUSH_STYLE_SOLID = -1, 77 | BRUSH_STYLE_HORIZONTAL, /* ----- */ 78 | BRUSH_STYLE_VERTICAL, /* ||||| */ 79 | BRUSH_STYLE_FDIAGONAL, /* \\\\\ */ 80 | BRUSH_STYLE_BDIAGONAL, /* ///// */ 81 | BRUSH_STYLE_CROSS, /* +++++ */ 82 | BRUSH_STYLE_DIAGCROSS, /* xxxxx */ 83 | BRUSH_STYLE_NULL 84 | } ACL_Brush_Style; 85 | 86 | typedef enum 87 | { 88 | NO_BUTTON = 0, 89 | LEFT_BUTTON, 90 | MIDDLE_BUTTON, 91 | RIGHT_BUTTON 92 | } ACL_Mouse_Button; 93 | 94 | typedef enum 95 | { 96 | BUTTON_DOWN, 97 | BUTTON_DOUBLECLICK, 98 | BUTTON_UP, 99 | ROLL_UP, 100 | ROLL_DOWN, 101 | MOUSEMOVE 102 | } ACL_Mouse_Event; 103 | 104 | typedef enum 105 | { 106 | KEY_DOWN, 107 | KEY_UP 108 | } ACL_Keyboard_Event; 109 | 110 | typedef struct 111 | { 112 | HBITMAP hbitmap; 113 | int width; 114 | int height; 115 | } ACL_Image; 116 | 117 | //typedef enum 118 | //{ 119 | // TM_NO = 0x00, 120 | // TM_COLOR = 0x01, 121 | // TM_ALPHA = 0x02 122 | //} ACL_TransparentMode; 123 | 124 | typedef COLORREF ACL_Color; 125 | typedef int ACL_Sound; 126 | 127 | typedef void(*KeyboardEventCallback) (int key, int event); 128 | typedef void(*CharEventCallback) (char c); 129 | typedef void(*MouseEventCallback) (int x, int y, int button, int event); 130 | typedef void(*TimerEventCallback) (int timerID); 131 | typedef struct rect 132 | { 133 | int x, y, width, height; 134 | }rect; 135 | #ifdef __cplusplus 136 | extern "C" { 137 | #endif 138 | 139 | int Setup(void); 140 | 141 | // 142 | void initWindow(const char title[], int left, int top, int width, int height); 143 | void msgBox(const char title[], const char text[], int flag); 144 | 145 | void registerKeyboardEvent(KeyboardEventCallback callback); 146 | void registerCharEvent(CharEventCallback callback); 147 | void registerMouseEvent(MouseEventCallback callback); 148 | void registerTimerEvent(TimerEventCallback callback); 149 | 150 | void startTimer(int timerID, int timeinterval); 151 | void cancelTimer(int timerID); 152 | 153 | // Sound 154 | void loadSound(const char *fileName, ACL_Sound *pSound); 155 | void playSound(ACL_Sound soundID, int repeat); 156 | void stopSound(ACL_Sound soundID); 157 | 158 | // Paint 159 | void beginPaint(); 160 | void endPaint(); 161 | void clearDevice(void); 162 | int getWidth(); 163 | int getHeight(); 164 | 165 | // Pen 166 | void setPenColor(ACL_Color color); 167 | void setPenWidth(int width); 168 | void setPenStyle(ACL_Pen_Style style); 169 | 170 | // Brush 171 | void setBrushColor(ACL_Color color); 172 | void setBrushStyle(ACL_Brush_Style style); 173 | 174 | // Text 175 | void setTextColor(ACL_Color color); 176 | void setTextBkColor(ACL_Color color); 177 | void setTextSize(int size); 178 | void setTextFont(const char *pFontName); 179 | 180 | void paintText(int x, int y, const char *pStr); 181 | 182 | void setCaretSize(int w, int h); 183 | void setCaretPos(int x, int y); 184 | void showCaret(); 185 | void hideCaret(); 186 | 187 | // Pixel 188 | void putPixel(int x, int y, ACL_Color color); 189 | ACL_Color getPixel(int x, int y); 190 | 191 | // the Point 192 | int getX(void); 193 | int getY(void); 194 | void moveTo(int x, int y); 195 | void moveRel(int dx, int dy); 196 | 197 | // Lines and Curves 198 | void arc(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, \ 199 | int nXStartArc, int nYStartArc, int nXEndArc, int nYEndArc); 200 | void line(int x0, int y0, int x1, int y1); 201 | void lineTo(int nXEnd, int nYEnd); 202 | void lineRel(int dx, int dy); 203 | void polyBezier(const POINT *lppt, int cPoints); 204 | void polyLine(const POINT *lppt, int cPoints); 205 | 206 | // Filled Shapes 207 | void chrod(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, \ 208 | int nXRadial1, int nYRadial1, int nXRadial2, int nYRadial2); 209 | void ellipse(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect); 210 | void pie(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, \ 211 | int nXRadial1, int nYRadial1, int nXRadial2, int nYRadial2); 212 | void polygon(const POINT *lpPoints, int nCount); 213 | void rectangle(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect); 214 | void roundrect(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, \ 215 | int nWidth, int nHeight); 216 | 217 | // Image 218 | void loadImage(const char *pImageFileName, ACL_Image *pImage); 219 | void freeImage(ACL_Image *pImage); 220 | 221 | void putImage(ACL_Image *pImage, int x, int y); 222 | void putImageScale(ACL_Image *pImage, int x, int y, int width, int height); 223 | void putImageTransparent(ACL_Image *pImage, int x, int y, int width, int height, ACL_Color bkColor); 224 | 225 | //void putImageEx(ACL_Image *pImage,int dx,int dy,int dw,int dh, 226 | // int sx,int sy,int sw,int sh); 227 | 228 | //void setTransparentMode(ACL_TransparenetMode); 229 | //void setTransparentColor(ACL_Color); 230 | //void setTransparetnAlpha(int alpha); 231 | 232 | void initConsole(void); 233 | 234 | #ifdef __cplusplus 235 | } 236 | #endif 237 | 238 | #endif 239 | -------------------------------------------------------------------------------- /sprite/inheritance/game.cpp: -------------------------------------------------------------------------------- 1 | #include"acllib.h" 2 | #include"AutoSprite.h" 3 | #include"UsrSprite.h" 4 | #include 5 | #include 6 | const int maxNum = 50; 7 | const int winWidth = 800, winHeight = 600; 8 | CAutoSprite *autosprite[maxNum] = { 0 }; 9 | CUsrSprite *usr = NULL; 10 | int autoWidth = 100, autoHeight = 100; 11 | int usrWidth = 100, usrHeight = 100; 12 | ACL_Image img,imgUsr; 13 | rect winRect; 14 | void timerEvent(int id); 15 | void createData(CAutoSprite **autoSprite); 16 | void createData(CUsrSprite **usr); 17 | void keyEvent(int key, int event); 18 | void paint(); 19 | int nowNum = 0; 20 | 21 | int Setup() 22 | { 23 | winRect.x = DEFAULT; 24 | winRect.y = DEFAULT; 25 | winRect.width = winWidth; 26 | winRect.height = winHeight; 27 | initWindow("auto sprite", DEFAULT, DEFAULT, winWidth, winHeight); 28 | srand((unsigned)time(NULL)); 29 | 30 | loadImage("jerry.bmp", &img); 31 | loadImage("tom.bmp", &imgUsr); 32 | //autosprite[nowNum++] = new CAutoSprite(x, y, autoWidth, autoHeight, dx, dy, &img, winRect); 33 | createData(autosprite); 34 | createData(&usr); 35 | registerTimerEvent(timerEvent); 36 | registerKeyboardEvent(keyEvent); 37 | startTimer(0, 40); 38 | startTimer(1, 1000); 39 | return 0; 40 | } 41 | void timerEvent(int id) 42 | { 43 | int i = 0; 44 | switch (id) 45 | { 46 | case 0: 47 | for (i = 0; i < nowNum; ++i) 48 | if (autosprite[i]) 49 | { 50 | autosprite[i]->move(); 51 | 52 | } 53 | 54 | break; 55 | case 1: 56 | if (nowNum < maxNum) 57 | { 58 | createData(autosprite); 59 | } 60 | break; 61 | }//end switch 62 | paint(); 63 | } 64 | void createData(CAutoSprite **autoSprite) 65 | { 66 | int x = rand() % winWidth - autoWidth; 67 | if (x < 0)x = 0; 68 | int y = rand() % winHeight - autoHeight; 69 | if (y < 0)y = 0; 70 | int dx = rand() % 5 + 1; 71 | int dy = rand() % 5 + 1; 72 | autosprite[nowNum++] = new CAutoSprite(x, y, autoWidth, autoHeight, dx, dy, &img, winRect); 73 | 74 | } 75 | void createData(CUsrSprite **usr) 76 | { 77 | int x = rand() % winWidth - autoWidth; 78 | if (x < 0)x = 0; 79 | int y = rand() % winHeight - autoHeight; 80 | if (y < 0)y = 0; 81 | int dx = 5; 82 | int dy = 5; 83 | *usr = new CUsrSprite(x, y, usrWidth, usrHeight, dx, dy, &imgUsr, winRect); 84 | 85 | } 86 | void paint() 87 | { 88 | beginPaint(); 89 | clearDevice(); 90 | int i = 0; 91 | for (i = 0; i < nowNum; ++i) 92 | { 93 | if (autosprite[i]) 94 | { 95 | autosprite[i]->drawSprite(); 96 | } 97 | } 98 | if (usr) 99 | { 100 | usr->drawSprite(); 101 | 102 | char txt[10]; 103 | sprintf_s(txt, "%d", usr->getScore()); 104 | setTextSize(20); 105 | paintText(10, 10, txt); 106 | } 107 | endPaint(); 108 | } 109 | void keyEvent(int key, int event) 110 | { 111 | if (event != KEY_DOWN)return; 112 | if(usr)usr->move(key); 113 | for (int i = 0; i < nowNum; ++i) 114 | { 115 | if (autosprite[i]) 116 | { 117 | if (usr->collision(autosprite[i]->getRect())) 118 | { 119 | delete(autosprite[i]); 120 | autosprite[i] = NULL; 121 | if (usr)usr->addScore(1); 122 | } 123 | } 124 | } 125 | paint(); 126 | } -------------------------------------------------------------------------------- /sprite/inheritance/jerry.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MisterFishing/cpp/1eb363fc03a1119d67808dfed624fb75bbc337ba/sprite/inheritance/jerry.bmp -------------------------------------------------------------------------------- /sprite/inheritance/readme.md: -------------------------------------------------------------------------------- 1 | gcc -c *.c 2 | 3 | g++ -c *.cpp 4 | 5 | g++ *.o -lgdi32 -lole32 -loleaut32 -luuid -lwinmm -lmsimg32 -o sprite.exe 6 | 7 | -------------------------------------------------------------------------------- /sprite/inheritance/sprite.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MisterFishing/cpp/1eb363fc03a1119d67808dfed624fb75bbc337ba/sprite/inheritance/sprite.exe -------------------------------------------------------------------------------- /sprite/inheritance/tom.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MisterFishing/cpp/1eb363fc03a1119d67808dfed624fb75bbc337ba/sprite/inheritance/tom.bmp -------------------------------------------------------------------------------- /sprite/polymorphism/AutoSprite.cpp: -------------------------------------------------------------------------------- 1 | #include "AutoSprite.h" 2 | 3 | CAutoSprite::CAutoSprite(int x, int y, int width, int height, int dx, int dy, ACL_Image *img, rect r1,int score):SpriteBase(x,y,width,height,dx,dy,img,r1) 4 | { 5 | this->score = score; 6 | } 7 | CAutoSprite::CAutoSprite(CAutoSprite &sprite):SpriteBase(sprite) 8 | { 9 | score = sprite.score; 10 | } 11 | 12 | CAutoSprite::~CAutoSprite() 13 | { 14 | } 15 | void CAutoSprite::move(rect r1) 16 | { 17 | 18 | if (x<0 || x>(r.width - width)) dx *= -1; 19 | if (y<0 || y>(r.height - height)) dy *= -1; 20 | x += dx; 21 | y += dy; 22 | 23 | } 24 | int CAutoSprite::getScore() 25 | { 26 | return score; 27 | } 28 | -------------------------------------------------------------------------------- /sprite/polymorphism/AutoSprite.h: -------------------------------------------------------------------------------- 1 | #ifndef AutoSprite_h 2 | #define AutoSprite_h 3 | 4 | #include "SpriteBase.h" 5 | 6 | class CAutoSprite : 7 | public SpriteBase 8 | { 9 | int score; 10 | public: 11 | CAutoSprite(int x, int y, int width, int height, int dx, int dy, ACL_Image *img, rect r1,int score); 12 | CAutoSprite(CAutoSprite &sprite); 13 | ~CAutoSprite(); 14 | 15 | void move(rect r); 16 | int getScore(); 17 | }; 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /sprite/polymorphism/AvoidSprite.cpp: -------------------------------------------------------------------------------- 1 | #include "AvoidSprite.h" 2 | #include 3 | 4 | CAvoidSprite::~CAvoidSprite() 5 | { 6 | } 7 | CAvoidSprite::CAvoidSprite(int x, int y, int width, int height, int dx, int dy, ACL_Image *img, rect r1,int score) 8 | :CAutoSprite(x, y, width, height, dx, dy, img, r1,score) 9 | { 10 | oldx = dx; 11 | oldy = dy; 12 | bfirst = true; 13 | } 14 | CAvoidSprite::CAvoidSprite(CAvoidSprite &sprite) 15 | :CAutoSprite(sprite) 16 | { 17 | oldx = dx; 18 | oldy = dy; 19 | bfirst = true; 20 | } 21 | 22 | 23 | void CAvoidSprite::move(rect ur) 24 | { 25 | if (danger(ur)) 26 | { 27 | if (x < ur.x) dx = -abs(dx); 28 | else dx=abs(dx); 29 | 30 | if (y < ur.y) dy = -abs(dy); 31 | else dy=abs(dy); 32 | 33 | x += 1.5*dx; 34 | y += 1.5*dy; 35 | } 36 | else 37 | { 38 | x += dx; 39 | y += dy; 40 | } 41 | 42 | if ((dx<0)&&(x<-width/2)) x = r.width-width/2; 43 | if ((dx>0)&&(x>(r.width-width/2))) x = -width/2; 44 | if ((dy<0)&&(y<-height/2)) y = r.height-height/2; 45 | if ((dy>0)&&(y>(r.height-height/2))) y = -height/2; 46 | 47 | } 48 | bool CAvoidSprite::danger(rect ur) 49 | { 50 | int cx = x + width / 2; 51 | int cy = y + height / 2; 52 | int ux = ur.x + ur.width / 2; 53 | int uy = ur.y + ur.height / 2; 54 | float dist = sqrt((cx - ux)*(cx - ux) + (cy - uy)*(cy - uy)); 55 | if (dist < 150) return true; 56 | else return false; 57 | } 58 | -------------------------------------------------------------------------------- /sprite/polymorphism/AvoidSprite.h: -------------------------------------------------------------------------------- 1 | #ifndef AvoidSprite_h 2 | #define AvoidSprite_h 3 | 4 | #include"acllib.h" 5 | #include "AutoSprite.h" 6 | 7 | class CAvoidSprite : 8 | public CAutoSprite 9 | { 10 | int oldx, oldy; 11 | bool danger(rect ur); 12 | bool bfirst; 13 | public: 14 | CAvoidSprite(int x, int y, int width, int height, int dx, int dy, ACL_Image *img, rect r1,int score); 15 | CAvoidSprite(CAvoidSprite &sprite); 16 | virtual ~CAvoidSprite(); 17 | 18 | void move(rect ur); 19 | }; 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /sprite/polymorphism/SpriteBase.cpp: -------------------------------------------------------------------------------- 1 | #include "SpriteBase.h" 2 | 3 | SpriteBase::SpriteBase(int x, int y, int width, int height, int dx, int dy, ACL_Image *img, rect r1) 4 | { 5 | this->x = x; 6 | this->y = y; 7 | this->width = width; 8 | this->height = height; 9 | this->dx = dx; 10 | this->dy = dy; 11 | this->img = img; 12 | r = r1; 13 | } 14 | SpriteBase::SpriteBase(SpriteBase & sprite) 15 | { 16 | x = sprite.x; 17 | y = sprite.y; 18 | width = sprite.width; 19 | height = sprite.height; 20 | dx = sprite.dx; 21 | dy = sprite.dy; 22 | img = sprite.img; 23 | r = sprite.r; 24 | } 25 | SpriteBase::~SpriteBase() 26 | { 27 | 28 | } 29 | 30 | 31 | void SpriteBase::drawSprite(int width, int height) 32 | { 33 | putImageScale(img, x, y, width, height); 34 | } 35 | void SpriteBase::drawSprite() 36 | { 37 | putImageScale(img, x, y, width, height); 38 | } 39 | rect SpriteBase::getRect() 40 | { 41 | rect r = { x, y, width, height }; 42 | return r; 43 | } -------------------------------------------------------------------------------- /sprite/polymorphism/SpriteBase.h: -------------------------------------------------------------------------------- 1 | #ifndef SpriteBase_h 2 | #define SpriteBase_h 3 | 4 | #include"acllib.h" 5 | 6 | class SpriteBase 7 | { 8 | protected: 9 | int x, y; 10 | int width, height; 11 | int dx, dy; 12 | ACL_Image *img; 13 | rect r; 14 | public: 15 | SpriteBase(int x, int y, int width, int height, int dx, int dy, ACL_Image *img, rect r1); 16 | SpriteBase(SpriteBase & base); 17 | virtual ~SpriteBase(); 18 | 19 | virtual void move(rect r)=0; 20 | void drawSprite(int width, int height); 21 | void drawSprite(); 22 | rect getRect(); 23 | }; 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /sprite/polymorphism/UsrSprite.cpp: -------------------------------------------------------------------------------- 1 | #include "UsrSprite.h" 2 | 3 | CUsrSprite::CUsrSprite(int x, int y, int width, int height, int dx, int dy, ACL_Image *img, rect r1) 4 | :SpriteBase(x,y,width,height,dx,dy,img,r1) 5 | { 6 | 7 | } 8 | CUsrSprite::CUsrSprite(CUsrSprite &sprite):SpriteBase(sprite) 9 | { 10 | 11 | } 12 | CUsrSprite::~CUsrSprite(){} 13 | 14 | /* 15 | void CUsrSprite::move(int xx, int yy) 16 | { 17 | x = xx; 18 | y = yy; 19 | if (x < r.x)x = r.x; 20 | if (x >(r.x + r.width - width))x = (r.x + r.width - width); 21 | if (y < r.y)y = r.y; 22 | if (y >(r.y + r.height - height))y = (r.y + r.height - height); 23 | 24 | } 25 | */ 26 | 27 | void CUsrSprite::move(rect r1) 28 | { 29 | x = r1.x; 30 | y = r1.y; 31 | if (x < r.x)x = r.x; 32 | if (x >(r.x + r.width - width))x = (r.x + r.width - width); 33 | if (y < r.y)y = r.y; 34 | if (y >(r.y + r.height - height))y = (r.y + r.height - height); 35 | 36 | } 37 | 38 | void CUsrSprite::move(int key) 39 | { 40 | switch (key) 41 | { 42 | case VK_UP: 43 | y -= dx; 44 | if (y < 0) y = 0; 45 | break; 46 | case VK_DOWN: 47 | y += dy; 48 | if (y >(r.height - height)) y = (r.height - height); 49 | 50 | break; 51 | case VK_LEFT: 52 | x -= dx; 53 | if (x < r.x)x = r.x; 54 | break; 55 | case VK_RIGHT: 56 | x += dx; 57 | if (x >(r.width - width)) x = (r.width - width); 58 | 59 | break; 60 | } 61 | } 62 | 63 | int CUsrSprite::collision(rect r2) 64 | { 65 | rect r1 = { x,y, width, height }; 66 | int c = 1; 67 | if (r1.x < r2.x && r1.x + r1.width >r2.x) { 68 | if (r1.y > r2.y && r1.yr2.y)return c; 70 | } 71 | else { 72 | if (r1.x > r2.x && r2.x + r2.width > r1.x) 73 | { 74 | if (r1.y > r2.y && r1.yr2.y)return c; 76 | } 77 | } 78 | c = 0; 79 | return c; 80 | } 81 | int CUsrSprite::getScore() 82 | { 83 | return score; 84 | } 85 | void CUsrSprite::setScore(int s) 86 | { 87 | score = s; 88 | } 89 | void CUsrSprite::addScore(int m) 90 | { 91 | score += m; 92 | } 93 | -------------------------------------------------------------------------------- /sprite/polymorphism/UsrSprite.h: -------------------------------------------------------------------------------- 1 | #ifndef UsrSprite_h 2 | #define UsrSprite_h 3 | 4 | #include "SpriteBase.h" 5 | 6 | class CUsrSprite : 7 | public SpriteBase 8 | { 9 | int score; 10 | public: 11 | CUsrSprite(int x, int y, int width, int height, int dx, int dy, ACL_Image *img, rect r1); 12 | CUsrSprite(CUsrSprite &sprite); 13 | ~CUsrSprite(); 14 | 15 | // void move(int x,int y); 16 | void move(int key); 17 | void move(rect r); 18 | 19 | int collision(rect r1); 20 | int getScore(); 21 | void setScore(int s); 22 | void addScore(int m); 23 | }; 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /sprite/polymorphism/acllib.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MisterFishing/cpp/1eb363fc03a1119d67808dfed624fb75bbc337ba/sprite/polymorphism/acllib.c -------------------------------------------------------------------------------- /sprite/polymorphism/acllib.h: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | //////////////////////////////////////////////////////////////// 17 | // ACLLib - Advanced C Lab Library 18 | // Ver 2014-07 19 | // For students' Lab at Zhejiang University 20 | // Created 2008 by Gao Yuan 21 | // Modified 2009 by Cui Liwei 22 | // 2010 by Lan Huidong 23 | // Revised 2012 by Li Rui 24 | // Modified 2014 by Weng Kai for MOOC 25 | //////////////////////////////////////////////////////////////// 26 | 27 | /* 28 | For Dev C++, these lib files need to be added into linker options. 29 | Be sure to change the leading folders as your installation. 30 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libwinmm.a" 31 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libmsimg32.a" 32 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libkernel32.a" 33 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libuser32.a" 34 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libgdi32.a" 35 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libole32.a" 36 | "C:/Program Files/Dev-Cpp/MinGW32/lib/liboleaut32.a" 37 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libuuid.a" 38 | */ 39 | 40 | #ifndef __ACLLIB_H__ 41 | #define __ACLLIB_H__ 42 | 43 | #ifdef _UNICODE 44 | #undef _UNICODE 45 | #endif 46 | #ifdef UNICODE 47 | #undef UNICODE 48 | #endif 49 | 50 | #include 51 | 52 | #define BLACK RGB(0, 0, 0) 53 | #define RED RGB(255, 0, 0) 54 | #define GREEN RGB(0, 255, 0) 55 | #define BLUE RGB(0, 0, 255) 56 | #define CYAN RGB(0, 255, 255) 57 | #define MAGENTA RGB(255, 0, 255) 58 | #define YELLOW RGB(255, 255, 0) 59 | #define WHITE RGB(255, 255, 255) 60 | 61 | #define EMPTY 0xffffffff 62 | #define DEFAULT -1 63 | 64 | typedef enum 65 | { 66 | PEN_STYLE_SOLID, 67 | PEN_STYLE_DASH, /* ------- */ 68 | PEN_STYLE_DOT, /* ....... */ 69 | PEN_STYLE_DASHDOT, /* _._._._ */ 70 | PEN_STYLE_DASHDOTDOT, /* _.._.._ */ 71 | PEN_STYLE_NULL 72 | } ACL_Pen_Style; 73 | 74 | typedef enum 75 | { 76 | BRUSH_STYLE_SOLID = -1, 77 | BRUSH_STYLE_HORIZONTAL, /* ----- */ 78 | BRUSH_STYLE_VERTICAL, /* ||||| */ 79 | BRUSH_STYLE_FDIAGONAL, /* \\\\\ */ 80 | BRUSH_STYLE_BDIAGONAL, /* ///// */ 81 | BRUSH_STYLE_CROSS, /* +++++ */ 82 | BRUSH_STYLE_DIAGCROSS, /* xxxxx */ 83 | BRUSH_STYLE_NULL 84 | } ACL_Brush_Style; 85 | 86 | typedef enum 87 | { 88 | NO_BUTTON = 0, 89 | LEFT_BUTTON, 90 | MIDDLE_BUTTON, 91 | RIGHT_BUTTON 92 | } ACL_Mouse_Button; 93 | 94 | typedef enum 95 | { 96 | BUTTON_DOWN, 97 | BUTTON_DOUBLECLICK, 98 | BUTTON_UP, 99 | ROLL_UP, 100 | ROLL_DOWN, 101 | MOUSEMOVE 102 | } ACL_Mouse_Event; 103 | 104 | typedef enum 105 | { 106 | KEY_DOWN, 107 | KEY_UP 108 | } ACL_Keyboard_Event; 109 | 110 | typedef struct 111 | { 112 | HBITMAP hbitmap; 113 | int width; 114 | int height; 115 | } ACL_Image; 116 | 117 | //typedef enum 118 | //{ 119 | // TM_NO = 0x00, 120 | // TM_COLOR = 0x01, 121 | // TM_ALPHA = 0x02 122 | //} ACL_TransparentMode; 123 | 124 | typedef COLORREF ACL_Color; 125 | typedef int ACL_Sound; 126 | 127 | typedef void(*KeyboardEventCallback) (int key, int event); 128 | typedef void(*CharEventCallback) (char c); 129 | typedef void(*MouseEventCallback) (int x, int y, int button, int event); 130 | typedef void(*TimerEventCallback) (int timerID); 131 | typedef struct rect 132 | { 133 | int x, y, width, height; 134 | }rect; 135 | #ifdef __cplusplus 136 | extern "C" { 137 | #endif 138 | 139 | int Setup(void); 140 | 141 | // 142 | void initWindow(const char title[], int left, int top, int width, int height); 143 | void msgBox(const char title[], const char text[], int flag); 144 | 145 | void registerKeyboardEvent(KeyboardEventCallback callback); 146 | void registerCharEvent(CharEventCallback callback); 147 | void registerMouseEvent(MouseEventCallback callback); 148 | void registerTimerEvent(TimerEventCallback callback); 149 | 150 | void startTimer(int timerID, int timeinterval); 151 | void cancelTimer(int timerID); 152 | 153 | // Sound 154 | void loadSound(const char *fileName, ACL_Sound *pSound); 155 | void playSound(ACL_Sound soundID, int repeat); 156 | void stopSound(ACL_Sound soundID); 157 | 158 | // Paint 159 | void beginPaint(); 160 | void endPaint(); 161 | void clearDevice(void); 162 | int getWidth(); 163 | int getHeight(); 164 | 165 | // Pen 166 | void setPenColor(ACL_Color color); 167 | void setPenWidth(int width); 168 | void setPenStyle(ACL_Pen_Style style); 169 | 170 | // Brush 171 | void setBrushColor(ACL_Color color); 172 | void setBrushStyle(ACL_Brush_Style style); 173 | 174 | // Text 175 | void setTextColor(ACL_Color color); 176 | void setTextBkColor(ACL_Color color); 177 | void setTextSize(int size); 178 | void setTextFont(const char *pFontName); 179 | 180 | void paintText(int x, int y, const char *pStr); 181 | 182 | void setCaretSize(int w, int h); 183 | void setCaretPos(int x, int y); 184 | void showCaret(); 185 | void hideCaret(); 186 | 187 | // Pixel 188 | void putPixel(int x, int y, ACL_Color color); 189 | ACL_Color getPixel(int x, int y); 190 | 191 | // the Point 192 | int getX(void); 193 | int getY(void); 194 | void moveTo(int x, int y); 195 | void moveRel(int dx, int dy); 196 | 197 | // Lines and Curves 198 | void arc(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, \ 199 | int nXStartArc, int nYStartArc, int nXEndArc, int nYEndArc); 200 | void line(int x0, int y0, int x1, int y1); 201 | void lineTo(int nXEnd, int nYEnd); 202 | void lineRel(int dx, int dy); 203 | void polyBezier(const POINT *lppt, int cPoints); 204 | void polyLine(const POINT *lppt, int cPoints); 205 | 206 | // Filled Shapes 207 | void chrod(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, \ 208 | int nXRadial1, int nYRadial1, int nXRadial2, int nYRadial2); 209 | void ellipse(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect); 210 | void pie(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, \ 211 | int nXRadial1, int nYRadial1, int nXRadial2, int nYRadial2); 212 | void polygon(const POINT *lpPoints, int nCount); 213 | void rectangle(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect); 214 | void roundrect(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, \ 215 | int nWidth, int nHeight); 216 | 217 | // Image 218 | void loadImage(const char *pImageFileName, ACL_Image *pImage); 219 | void freeImage(ACL_Image *pImage); 220 | 221 | void putImage(ACL_Image *pImage, int x, int y); 222 | void putImageScale(ACL_Image *pImage, int x, int y, int width, int height); 223 | void putImageTransparent(ACL_Image *pImage, int x, int y, int width, int height, ACL_Color bkColor); 224 | 225 | //void putImageEx(ACL_Image *pImage,int dx,int dy,int dw,int dh, 226 | // int sx,int sy,int sw,int sh); 227 | 228 | //void setTransparentMode(ACL_TransparenetMode); 229 | //void setTransparentColor(ACL_Color); 230 | //void setTransparetnAlpha(int alpha); 231 | 232 | void initConsole(void); 233 | 234 | #ifdef __cplusplus 235 | } 236 | #endif 237 | 238 | #endif 239 | -------------------------------------------------------------------------------- /sprite/polymorphism/dog.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MisterFishing/cpp/1eb363fc03a1119d67808dfed624fb75bbc337ba/sprite/polymorphism/dog.bmp -------------------------------------------------------------------------------- /sprite/polymorphism/duck.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MisterFishing/cpp/1eb363fc03a1119d67808dfed624fb75bbc337ba/sprite/polymorphism/duck.jpg -------------------------------------------------------------------------------- /sprite/polymorphism/game.cpp: -------------------------------------------------------------------------------- 1 | #include"acllib.h" 2 | #include"AutoSprite.h" 3 | #include"UsrSprite.h" 4 | #include"AvoidSprite.h" 5 | #include 6 | #include 7 | 8 | const int maxNum = 50; 9 | const int winWidth = 800, winHeight = 600; 10 | CAutoSprite *autosprite[maxNum] = { 0 }; 11 | CUsrSprite *usr = NULL; 12 | int autoWidth = 100, autoHeight = 100; 13 | int usrWidth = 100, usrHeight = 100; 14 | ACL_Image img,imgUsr,imgHeart; 15 | rect winRect; 16 | void timerEvent(int id); 17 | void createData(CAutoSprite **autoSprite); 18 | void createData(CUsrSprite **usr); 19 | void keyEvent(int key, int event); 20 | void paint(); 21 | int nowNum = 0; 22 | 23 | int Setup() 24 | { 25 | winRect.x = DEFAULT; 26 | winRect.y = DEFAULT; 27 | winRect.width = winWidth; 28 | winRect.height = winHeight; 29 | initWindow("auto sprite", DEFAULT, DEFAULT, winWidth, winHeight); 30 | srand((unsigned)time(NULL)); 31 | 32 | loadImage("jerry.bmp", &img); 33 | loadImage("tom.bmp", &imgUsr); 34 | loadImage("duck.jpg", &imgHeart); 35 | //autosprite[nowNum++] = new CAutoSprite(x, y, autoWidth, autoHeight, dx, dy, &img, winRect); 36 | createData(autosprite); 37 | createData(&usr); 38 | registerTimerEvent(timerEvent); 39 | registerKeyboardEvent(keyEvent); 40 | startTimer(0, 40); 41 | startTimer(1, 1000); 42 | return 0; 43 | } 44 | void timerEvent(int id) 45 | { 46 | int i = 0; 47 | switch (id) 48 | { 49 | case 0: 50 | for (i = 0; i < nowNum; ++i) 51 | if (autosprite[i]) 52 | { 53 | rect ur = usr->getRect(); 54 | autosprite[i]->move(ur); 55 | 56 | } 57 | 58 | break; 59 | case 1: 60 | if (nowNum < maxNum) 61 | { 62 | createData(autosprite); 63 | } 64 | break; 65 | }//end switch 66 | paint(); 67 | } 68 | void createData(CAutoSprite **autoSprite) 69 | { 70 | int x = rand() % winWidth - autoWidth; 71 | if (x < 0)x = 0; 72 | int y = rand() % winHeight - autoHeight; 73 | if (y < 0)y = 0; 74 | int dx = rand() % 5 + 1; 75 | int dy = rand() % 5 + 1; 76 | int t = rand() % 100; 77 | if(t<80) 78 | autosprite[nowNum++] = new CAutoSprite(x, y, autoWidth, autoHeight, dx, dy, &img, winRect,1); 79 | else 80 | autosprite[nowNum++] = new CAvoidSprite(x, y, autoWidth, autoHeight, dx, dy, &imgHeart, winRect,5); 81 | } 82 | void createData(CUsrSprite **usr) 83 | { 84 | int x = rand() % winWidth - autoWidth; 85 | if (x < 0)x = 0; 86 | int y = rand() % winHeight - autoHeight; 87 | if (y < 0)y = 0; 88 | int dx = 5; 89 | int dy = 5; 90 | *usr = new CUsrSprite(x, y, usrWidth, usrHeight, dx, dy, &imgUsr, winRect); 91 | 92 | } 93 | void paint() 94 | { 95 | beginPaint(); 96 | clearDevice(); 97 | int i = 0; 98 | for (i = 0; i < nowNum; ++i) 99 | { 100 | if (autosprite[i]) 101 | { 102 | autosprite[i]->drawSprite(); 103 | } 104 | } 105 | if (usr) 106 | { 107 | usr->drawSprite(); 108 | 109 | char txt[10]; 110 | sprintf_s(txt, "%d", usr->getScore()); 111 | setTextSize(20); 112 | paintText(10, 10, txt); 113 | } 114 | endPaint(); 115 | } 116 | void keyEvent(int key, int event) 117 | { 118 | if (event != KEY_DOWN)return; 119 | if(usr)usr->move(key); 120 | for (int i = 0; i < nowNum; ++i) 121 | { 122 | if (autosprite[i]) 123 | { 124 | if (usr->collision(autosprite[i]->getRect())) 125 | { 126 | int s = autosprite[i]->getScore(); 127 | if (usr)usr->addScore(s); 128 | delete(autosprite[i]); 129 | autosprite[i] = NULL; 130 | } 131 | } 132 | } 133 | paint(); 134 | } -------------------------------------------------------------------------------- /sprite/polymorphism/jerry.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MisterFishing/cpp/1eb363fc03a1119d67808dfed624fb75bbc337ba/sprite/polymorphism/jerry.bmp -------------------------------------------------------------------------------- /sprite/polymorphism/readme.md: -------------------------------------------------------------------------------- 1 | gcc -c *.c 2 | 3 | g++ -c *.cpp 4 | 5 | g++ *.o -lgdi32 -lole32 -loleaut32 -luuid -lwinmm -lmsimg32 -o sprite.exe 6 | 7 | -------------------------------------------------------------------------------- /sprite/polymorphism/sprite.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MisterFishing/cpp/1eb363fc03a1119d67808dfed624fb75bbc337ba/sprite/polymorphism/sprite.exe -------------------------------------------------------------------------------- /sprite/polymorphism/tom.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MisterFishing/cpp/1eb363fc03a1119d67808dfed624fb75bbc337ba/sprite/polymorphism/tom.bmp -------------------------------------------------------------------------------- /sprite/struct/acllib.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MisterFishing/cpp/1eb363fc03a1119d67808dfed624fb75bbc337ba/sprite/struct/acllib.c -------------------------------------------------------------------------------- /sprite/struct/acllib.h: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | //////////////////////////////////////////////////////////////// 17 | // ACLLib - Advanced C Lab Library 18 | // Ver 2014-07 19 | // For students' Lab at Zhejiang University 20 | // Created 2008 by Gao Yuan 21 | // Modified 2009 by Cui Liwei 22 | // 2010 by Lan Huidong 23 | // Revised 2012 by Li Rui 24 | // Modified 2014 by Weng Kai for MOOC 25 | //////////////////////////////////////////////////////////////// 26 | 27 | /* 28 | For Dev C++, these lib files need to be added into linker options. 29 | Be sure to change the leading folders as your installation. 30 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libwinmm.a" 31 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libmsimg32.a" 32 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libkernel32.a" 33 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libuser32.a" 34 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libgdi32.a" 35 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libole32.a" 36 | "C:/Program Files/Dev-Cpp/MinGW32/lib/liboleaut32.a" 37 | "C:/Program Files/Dev-Cpp/MinGW32/lib/libuuid.a" 38 | */ 39 | 40 | #ifndef __ACLLIB_H__ 41 | #define __ACLLIB_H__ 42 | 43 | #ifdef _UNICODE 44 | #undef _UNICODE 45 | #endif 46 | #ifdef UNICODE 47 | #undef UNICODE 48 | #endif 49 | 50 | #include 51 | 52 | #define BLACK RGB(0, 0, 0) 53 | #define RED RGB(255, 0, 0) 54 | #define GREEN RGB(0, 255, 0) 55 | #define BLUE RGB(0, 0, 255) 56 | #define CYAN RGB(0, 255, 255) 57 | #define MAGENTA RGB(255, 0, 255) 58 | #define YELLOW RGB(255, 255, 0) 59 | #define WHITE RGB(255, 255, 255) 60 | 61 | #define EMPTY 0xffffffff 62 | #define DEFAULT -1 63 | 64 | typedef enum 65 | { 66 | PEN_STYLE_SOLID, 67 | PEN_STYLE_DASH, /* ------- */ 68 | PEN_STYLE_DOT, /* ....... */ 69 | PEN_STYLE_DASHDOT, /* _._._._ */ 70 | PEN_STYLE_DASHDOTDOT, /* _.._.._ */ 71 | PEN_STYLE_NULL 72 | } ACL_Pen_Style; 73 | 74 | typedef enum 75 | { 76 | BRUSH_STYLE_SOLID = -1, 77 | BRUSH_STYLE_HORIZONTAL, /* ----- */ 78 | BRUSH_STYLE_VERTICAL, /* ||||| */ 79 | BRUSH_STYLE_FDIAGONAL, /* \\\\\ */ 80 | BRUSH_STYLE_BDIAGONAL, /* ///// */ 81 | BRUSH_STYLE_CROSS, /* +++++ */ 82 | BRUSH_STYLE_DIAGCROSS, /* xxxxx */ 83 | BRUSH_STYLE_NULL 84 | } ACL_Brush_Style; 85 | 86 | typedef enum 87 | { 88 | NO_BUTTON = 0, 89 | LEFT_BUTTON, 90 | MIDDLE_BUTTON, 91 | RIGHT_BUTTON 92 | } ACL_Mouse_Button; 93 | 94 | typedef enum 95 | { 96 | BUTTON_DOWN, 97 | BUTTON_DOUBLECLICK, 98 | BUTTON_UP, 99 | ROLL_UP, 100 | ROLL_DOWN, 101 | MOUSEMOVE 102 | } ACL_Mouse_Event; 103 | 104 | typedef enum 105 | { 106 | KEY_DOWN, 107 | KEY_UP 108 | } ACL_Keyboard_Event; 109 | 110 | typedef struct 111 | { 112 | HBITMAP hbitmap; 113 | int width; 114 | int height; 115 | } ACL_Image; 116 | 117 | //typedef enum 118 | //{ 119 | // TM_NO = 0x00, 120 | // TM_COLOR = 0x01, 121 | // TM_ALPHA = 0x02 122 | //} ACL_TransparentMode; 123 | 124 | typedef COLORREF ACL_Color; 125 | typedef int ACL_Sound; 126 | 127 | typedef void(*KeyboardEventCallback) (int key, int event); 128 | typedef void(*CharEventCallback) (char c); 129 | typedef void(*MouseEventCallback) (int x, int y, int button, int event); 130 | typedef void(*TimerEventCallback) (int timerID); 131 | 132 | #ifdef __cplusplus 133 | extern "C" { 134 | #endif 135 | 136 | int Setup(void); 137 | 138 | // 139 | void initWindow(const char title[], int left, int top, int width, int height); 140 | void msgBox(const char title[], const char text[], int flag); 141 | 142 | void registerKeyboardEvent(KeyboardEventCallback callback); 143 | void registerCharEvent(CharEventCallback callback); 144 | void registerMouseEvent(MouseEventCallback callback); 145 | void registerTimerEvent(TimerEventCallback callback); 146 | 147 | void startTimer(int timerID, int timeinterval); 148 | void cancelTimer(int timerID); 149 | 150 | // Sound 151 | void loadSound(const char *fileName, ACL_Sound *pSound); 152 | void playSound(ACL_Sound soundID, int repeat); 153 | void stopSound(ACL_Sound soundID); 154 | 155 | // Paint 156 | void beginPaint(); 157 | void endPaint(); 158 | void clearDevice(void); 159 | int getWidth(); 160 | int getHeight(); 161 | 162 | // Pen 163 | void setPenColor(ACL_Color color); 164 | void setPenWidth(int width); 165 | void setPenStyle(ACL_Pen_Style style); 166 | 167 | // Brush 168 | void setBrushColor(ACL_Color color); 169 | void setBrushStyle(ACL_Brush_Style style); 170 | 171 | // Text 172 | void setTextColor(ACL_Color color); 173 | void setTextBkColor(ACL_Color color); 174 | void setTextSize(int size); 175 | void setTextFont(const char *pFontName); 176 | 177 | void paintText(int x, int y, const char *pStr); 178 | 179 | void setCaretSize(int w, int h); 180 | void setCaretPos(int x, int y); 181 | void showCaret(); 182 | void hideCaret(); 183 | 184 | // Pixel 185 | void putPixel(int x, int y, ACL_Color color); 186 | ACL_Color getPixel(int x, int y); 187 | 188 | // the Point 189 | int getX(void); 190 | int getY(void); 191 | void moveTo(int x, int y); 192 | void moveRel(int dx, int dy); 193 | 194 | // Lines and Curves 195 | void arc(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, \ 196 | int nXStartArc, int nYStartArc, int nXEndArc, int nYEndArc); 197 | void line(int x0, int y0, int x1, int y1); 198 | void lineTo(int nXEnd, int nYEnd); 199 | void lineRel(int dx, int dy); 200 | void polyBezier(const POINT *lppt, int cPoints); 201 | void polyLine(const POINT *lppt, int cPoints); 202 | 203 | // Filled Shapes 204 | void chrod(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, \ 205 | int nXRadial1, int nYRadial1, int nXRadial2, int nYRadial2); 206 | void ellipse(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect); 207 | void pie(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, \ 208 | int nXRadial1, int nYRadial1, int nXRadial2, int nYRadial2); 209 | void polygon(const POINT *lpPoints, int nCount); 210 | void rectangle(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect); 211 | void roundrect(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, \ 212 | int nWidth, int nHeight); 213 | 214 | // Image 215 | void loadImage(const char *pImageFileName, ACL_Image *pImage); 216 | void freeImage(ACL_Image *pImage); 217 | 218 | void putImage(ACL_Image *pImage, int x, int y); 219 | void putImageScale(ACL_Image *pImage, int x, int y, int width, int height); 220 | void putImageTransparent(ACL_Image *pImage, int x, int y, int width, int height, ACL_Color bkColor); 221 | 222 | //void putImageEx(ACL_Image *pImage,int dx,int dy,int dw,int dh, 223 | // int sx,int sy,int sw,int sh); 224 | 225 | //void setTransparentMode(ACL_TransparenetMode); 226 | //void setTransparentColor(ACL_Color); 227 | //void setTransparetnAlpha(int alpha); 228 | 229 | void initConsole(void); 230 | 231 | #ifdef __cplusplus 232 | } 233 | #endif 234 | 235 | #endif 236 | -------------------------------------------------------------------------------- /sprite/struct/cat.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MisterFishing/cpp/1eb363fc03a1119d67808dfed624fb75bbc337ba/sprite/struct/cat.jpg -------------------------------------------------------------------------------- /sprite/struct/dog.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MisterFishing/cpp/1eb363fc03a1119d67808dfed624fb75bbc337ba/sprite/struct/dog.jpg -------------------------------------------------------------------------------- /sprite/struct/game.c: -------------------------------------------------------------------------------- 1 | //#include 2 | #include 3 | #include 4 | #include"acllib.h" 5 | #define MAXNUM 10 6 | struct autoSprite 7 | { 8 | int x,y; 9 | int dx,dy; 10 | int width,height; 11 | int bDead; 12 | }sprites[MAXNUM]; 13 | const int winWidth = 880, winHeight = 600; 14 | ACL_Image img; 15 | struct usrSprite 16 | { 17 | int x,y; 18 | int dx,dy; 19 | ACL_Image img; 20 | int width,height; 21 | 22 | }usr; 23 | void initSprites(); 24 | void initUsr(); 25 | void timerEvent(int id); 26 | void paintSprites(); 27 | void keyEvent(int key,int e); 28 | int collision(struct usrSprite r1, struct autoSprite r2); 29 | void JudgeCollision(); 30 | int Setup() 31 | { 32 | initWindow("load Image", DEFAULT, DEFAULT, winWidth, winHeight); 33 | srand((unsigned)time(NULL)); 34 | loadImage("cat.jpg",&img); 35 | initUsr(); 36 | initSprites(); 37 | paintSprites(); 38 | registerTimerEvent(timerEvent); 39 | startTimer(0,50); 40 | registerKeyboardEvent(keyEvent); 41 | return 0; 42 | } 43 | void initSprites() 44 | { 45 | int i=0; 46 | for(i=0;i=winWidth-sprites[i].width) 52 | sprites[i].x = winWidth-sprites[i].width; 53 | if(sprites[i].y>=winHeight-sprites[i].height) 54 | { 55 | sprites[i].y = winHeight-sprites[i].height; 56 | } 57 | sprites[i].dx=rand()%5+1; 58 | sprites[i].dy=rand()%5+1; 59 | sprites[i].bDead=0; 60 | } 61 | } 62 | void timerEvent(int id) 63 | { 64 | int i; 65 | if(id==0) 66 | { 67 | for(i=0;i=winWidth-sprites[i].width || sprites[i].x<=0) 72 | sprites[i].dx *= -1; 73 | if(sprites[i].y>=winHeight-sprites[i].height|| sprites[i].y<=0) 74 | { 75 | sprites[i].dy *= -1; 76 | } 77 | sprites[i].x+=sprites[i].dx; 78 | sprites[i].y+=sprites[i].dy; 79 | 80 | } 81 | } 82 | JudgeCollision(); 83 | paintSprites(); 84 | } 85 | } 86 | void paintSprites() 87 | { 88 | int i; 89 | beginPaint(); 90 | clearDevice(); 91 | for(i=0;i=winWidth-usr.width)usr.x = winWidth-usr.width; 109 | if(usr.y>=winHeight-usr.height)usr.y = winHeight-usr.height; 110 | } 111 | void keyEvent(int key,int e) 112 | { 113 | if(e!=KEY_DOWN)return; 114 | switch(key) 115 | { 116 | case VK_UP:usr.y-=usr.dy; 117 | if(usr.y<0)usr.y=0; 118 | break; 119 | case VK_DOWN: 120 | usr.y+=usr.dy; 121 | if(usr.y>=winHeight-usr.height)usr.y = winHeight-usr.height; 122 | break; 123 | case VK_LEFT: 124 | usr.x -=usr.dx; 125 | if(usr.x<=0)usr.x=0; 126 | break; 127 | case VK_RIGHT: 128 | usr.x+=usr.dx; 129 | if(usr.x>=winWidth-usr.width)usr.x = winWidth-usr.width; 130 | break; 131 | } 132 | JudgeCollision(); 133 | paintSprites(); 134 | } 135 | int collision(struct usrSprite r1, struct autoSprite r2){ 136 | int c = 1; 137 | if (r1.x < r2.x && r1.x+r1.width >r2.x ){ 138 | if (r1.y > r2.y && r1.yr2.y)return c; 140 | } 141 | else{ 142 | if (r1.x > r2.x && r2.x + r2.width > r1.x) 143 | { 144 | if (r1.y > r2.y && r1.yr2.y)return c; 146 | } 147 | } 148 | c = 0; 149 | return c; 150 | } 151 | void JudgeCollision() 152 | { 153 | int i; 154 | for(i=0;i 2 | using namespace std; 3 | 4 | class student { 5 | int id; 6 | int score; 7 | 8 | public: 9 | student(int newid){ 10 | id=newid; 11 | } 12 | 13 | int getid(){ 14 | return id; 15 | } 16 | 17 | int getscore(){ 18 | return score; 19 | } 20 | 21 | void setscore(int newscore){ 22 | score=newscore; 23 | } 24 | }; 25 | int main() 26 | { 27 | student s1(1); 28 | s1.setscore(90); 29 | cout << "id:" << s1.getid() << endl; 30 | cout << "score:" << s1.getscore() << endl; 31 | 32 | cout << endl; 33 | 34 | student s2(2); 35 | s2.setscore(80); 36 | cout << "id:" << s2.getid() << endl; 37 | cout << "score:" << s2.getscore() << endl; 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /student/studentarray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class student { 5 | int id; 6 | int score; 7 | 8 | public: 9 | student(int newid){ 10 | id=newid; 11 | } 12 | 13 | int getid(){ 14 | return id; 15 | } 16 | 17 | int getscore(){ 18 | return score; 19 | } 20 | 21 | void setscore(int newscore){ 22 | score=newscore; 23 | } 24 | }; 25 | 26 | int main() 27 | { 28 | // student array 29 | student sa[3]={student(1),student(2),student(3)}; 30 | sa[0].setscore(90); 31 | sa[1].setscore(80); 32 | sa[2].setscore(70); 33 | 34 | for(int i=0;i<3;i++){ 35 | cout << "id:" << sa[i].getid() << endl; 36 | cout << "score:" << sa[i].getscore() << endl; 37 | cout << endl; 38 | } 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /student/studentlist.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class student { 5 | int id; 6 | int score; 7 | 8 | public: 9 | student(int newid){ 10 | id=newid; 11 | } 12 | 13 | int getid(){ 14 | return id; 15 | } 16 | 17 | int getscore(){ 18 | return score; 19 | } 20 | 21 | void setscore(int newscore){ 22 | score=newscore; 23 | } 24 | }; 25 | 26 | class studentnode { 27 | public: 28 | student s; 29 | studentnode *next; 30 | studentnode(int newid):s(newid){ 31 | next=NULL; 32 | } 33 | }; 34 | 35 | int main() 36 | { 37 | studentnode zero(0); 38 | 39 | // student list 40 | studentnode *sl=&zero; 41 | studentnode *tmp; 42 | 43 | for(int i=1;i<=3;i++){ 44 | tmp=new studentnode(i); 45 | tmp->s.setscore(100-10*i); 46 | tmp->next=sl; 47 | sl=tmp; 48 | } 49 | 50 | for(tmp=sl;tmp->next!=NULL;tmp=tmp->next){ 51 | cout << "id:" << tmp->s.getid() << endl; 52 | cout << "score:" << tmp->s.getscore() << endl; 53 | cout << endl; 54 | } 55 | 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /student/studentpointerarray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class student { 5 | int id; 6 | int score; 7 | 8 | public: 9 | student(int newid){ 10 | id=newid; 11 | } 12 | 13 | int getid(){ 14 | return id; 15 | } 16 | 17 | int getscore(){ 18 | return score; 19 | } 20 | 21 | void setscore(int newscore){ 22 | score=newscore; 23 | } 24 | }; 25 | 26 | int main() 27 | { 28 | // student pointer array 29 | student *spa[3]; 30 | for(int i=0;i<3;i++){ 31 | spa[i]=new student(i+1); 32 | spa[i]->setscore(90-10*i); 33 | } 34 | 35 | for(int i=0;i<3;i++){ 36 | cout << "id:" << spa[i]->getid() << endl; 37 | cout << "score:" << spa[i]->getscore() << endl; 38 | cout << endl; 39 | } 40 | 41 | return 0; 42 | } 43 | --------------------------------------------------------------------------------