打印练习

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. std::cout << "Hello World!\n";
  6. int i, j, k, f;
  7. for (size_t i = 0; i < 4; i++)
  8. {
  9. for (size_t j = 0; j < 30; j++)
  10. {
  11. cout << " ";
  12. }
  13. for (size_t k = 0; k < 8-2*i; k++)
  14. {
  15. cout << " ";
  16. }
  17. for (size_t f = 0; f < 2*i; f++)
  18. {
  19. cout << "*";
  20. }
  21. cout << endl;
  22. }
  23. for (size_t i = 0; i < 3; i++)
  24. {
  25. for (size_t j = 0; j < 30; j++)
  26. {
  27. cout << " ";
  28. }
  29. for (size_t f = 1; f < 7; f++)
  30. {
  31. cout << "*";
  32. }
  33. cout << endl;
  34. }
  35. }

测试闰年

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. std::cout << "Hello World!\n";
  6. int yearl;
  7. bool isLeapYear;
  8. cout << "Enter the year:";
  9. cin >> yearl;
  10. isLeapYear = (((yearl % 4 == 0) && (yearl % 100 != 0)) || (yearl % 400 == 0));
  11. if (isLeapYear)
  12. {
  13. cout << yearl << " is a leap year" << endl;
  14. }
  15. else
  16. {
  17. cout << yearl << " is not a lear year" << endl;
  18. }
  19. }

测试联合体

  1. std::cout << "Hello World!\n";
  2. aa.uu.x = 4;
  3. aa.uu.y = 5;
  4. aa.uu.z = 6;
  5. aa.k = 0; //覆盖掉第一个int空间值
  6. printf("d% d% d% d%\n", aa.uu.x, aa.uu.y, aa.uu.z, aa.k);

随机掷骰子

  1. #include <iostream>
  2. #include<stdio.h>
  3. #include<cstdlib>
  4. using namespace std;
  5. int rolldice();
  6. int main()
  7. {
  8. std::cout << "Hello World!\n";
  9. int flag;
  10. unsigned seed;
  11. cout << "请输入无符号整数:" << endl;
  12. cin >> seed;
  13. srand(seed);
  14. int sum = rolldice();
  15. int selfdim;
  16. switch (sum)
  17. {
  18. case 7:
  19. case 11:
  20. flag = 1;
  21. break;
  22. case 2:
  23. case 3:
  24. case 12:
  25. flag = 2;
  26. break;
  27. default:
  28. flag = 0;
  29. selfdim = sum;
  30. break;
  31. }
  32. while (flag == 0)
  33. {
  34. sum = rolldice();
  35. if (sum == selfdim)
  36. {
  37. flag = 1;
  38. }
  39. else if (sum = 7)
  40. {
  41. flag = 2;
  42. }
  43. }
  44. if (flag == 1)
  45. {
  46. cout << "player win\n";
  47. }
  48. else
  49. {
  50. cout << "player lose\n";
  51. }
  52. }
  53. int rolldice()
  54. {
  55. int sum = 0;
  56. int dim1 = rand() % 6 + 1;
  57. int dim2 = rand() % 6 + 1;
  58. sum = dim1 + dim2;
  59. cout << "sum=" << dim1 << "+" << dim2 << endl;
  60. return sum;
  61. }

递归

  1. #include <iostream>
  2. #include<stdio.h>
  3. #include<cstdlib>
  4. using namespace std;
  5. int f(int n);
  6. int main()
  7. {
  8. std::cout << "Hello World!\n";
  9. std::cout << "input x!\n";
  10. int x;
  11. cin >> x;
  12. cout << f(x) << endl;
  13. return 0;
  14. }
  15. int f(int n)
  16. {
  17. if (n==0)
  18. {
  19. return 1;
  20. }
  21. else
  22. {
  23. return n * f(n - 1);
  24. }
  25. }

内联函数

  1. #include <iostream>
  2. #include<stdio.h>
  3. #include<cstdlib>
  4. using namespace std;
  5. //函数声明
  6. inline double CalArea(double radius);
  7. int main()
  8. {
  9. std::cout << "Hello World!\n";
  10. double r(3.0);
  11. double area;
  12. area = CalArea(r);
  13. cout << area << endl;
  14. return 0;
  15. }
  16. //加关键字inline
  17. inline double CalArea(double radius)
  18. {
  19. return 3.14 * radius * radius;
  20. }

综合练习(类等)

  1. #include <iostream>
  2. #include<stdio.h>
  3. #include<cstdlib>
  4. using namespace std;
  5. const float PI = 3.14159;
  6. const float FencePrice = 35;
  7. const float ConcretPrice = 20;
  8. class Circle
  9. {
  10. public:
  11. Circle(float r);
  12. ~Circle();
  13. float Circumference() const;
  14. float Area() const;
  15. private:
  16. float radious;
  17. };
  18. Circle::Circle(float r)
  19. {
  20. radious = r;
  21. }
  22. Circle::~Circle()
  23. {
  24. }
  25. float Circle::Circumference() const
  26. {
  27. return PI * 2 * radious;
  28. }
  29. float Circle::Area() const
  30. {
  31. return PI * radious * radious;
  32. }
  33. int main()
  34. {
  35. std::cout << "Hello World!\n";
  36. cout << "请输入游泳池半径\n";
  37. float radius;
  38. float FenceCost, ConcretCost;
  39. cin >> radius;
  40. Circle circle(radius);
  41. Circle circle2(radius+3);
  42. // 计算栅栏造价并输出
  43. FenceCost = circle.Circumference() * FencePrice;
  44. cout << "Fencing Cost is ¥" << FenceCost << endl;
  45. ConcretCost = (circle2.Area() - circle.Area()) * ConcretPrice;
  46. cout << "Concrete Cost is ¥" << ConcretCost << endl;
  47. return 0;
  48. }

常对象

  1. //main.cpp
  2. #include<iostream>
  3. using namespace std;
  4. class AA
  5. {
  6. public:
  7. AA();
  8. ~AA();
  9. AA(int r1, int r2) { R1 = r1; R2 = r2; }
  10. //const区分成员重载函数
  11. void print();
  12. void print() const;
  13. private:
  14. int R1, R2;
  15. };
  16. AA::AA()
  17. {
  18. }
  19. AA::~AA()
  20. {
  21. }
  22. void AA::print()
  23. {
  24. cout << "普通调用" << endl;
  25. cout << R1 << ":" << R2 << endl;
  26. }
  27. //实例化也需要带上
  28. void AA::print() const
  29. {
  30. cout << "常对象调用" << endl;
  31. cout << R1 << ";" << R2 << endl;
  32. }
  33. int main() {
  34. AA a(5, 4);
  35. a.print(); //调用void print()
  36. //通过常对象只能调用它的常成员函数
  37. const AA b(20, 52);
  38. b.print(); //调用void print() const
  39. return 0;
  40. }

重载运算符()

  1. #include <iostream>
  2. using namespace std;
  3. class Time
  4. {
  5. public:
  6. Time();
  7. ~Time();
  8. Time(int h = 0, int m = 0, int s = 0) :hh(h), mm(m), ss(s) {}
  9. void operator() (int h, int m, int s) {
  10. hh = h;
  11. mm = m;
  12. ss = s;
  13. }
  14. void ShowTime()
  15. {
  16. cout << hh << ":" << mm << ":" << ss << endl;
  17. }
  18. private:
  19. int hh, mm, ss;
  20. };
  21. Time::Time()
  22. {
  23. }
  24. Time::~Time()
  25. {
  26. }
  27. int main()
  28. {
  29. Time t1(12, 12, 33);
  30. t1.ShowTime();
  31. t1.operator()(33, 34, 56);
  32. t1.ShowTime();
  33. t1(23, 43, 56);
  34. t1.ShowTime();
  35. return 0;
  36. }

抽象类

  1. #include <iostream>
  2. using namespace std;
  3. class Figure
  4. {
  5. public:
  6. Figure();
  7. ~Figure();
  8. void set(double i, double j) {
  9. x = i;
  10. y = j;
  11. }
  12. virtual void area() = 0;
  13. private:
  14. protected:
  15. double x, y;
  16. };
  17. Figure::Figure()
  18. {
  19. }
  20. Figure::~Figure()
  21. {
  22. }
  23. class Triangle:public Figure
  24. {
  25. public:
  26. Triangle();
  27. ~Triangle();
  28. void area() {
  29. cout << "三角形面积:" << x * y * 0.5 << endl;
  30. }
  31. private:
  32. };
  33. Triangle::Triangle()
  34. {
  35. }
  36. Triangle::~Triangle()
  37. {
  38. }
  39. class Rectangle:public Figure
  40. {
  41. public:
  42. Rectangle();
  43. ~Rectangle();
  44. void area() {
  45. cout << "矩形面积:" << x * y * 1 << endl;
  46. }
  47. private:
  48. };
  49. Rectangle::Rectangle()
  50. {
  51. }
  52. Rectangle::~Rectangle()
  53. {
  54. }
  55. int main()
  56. {
  57. Figure* fg = nullptr;
  58. Rectangle r;
  59. Triangle t;
  60. t.set(2, 3);
  61. fg = &t;
  62. fg->area();
  63. r.set(3, 5);
  64. fg = &r;
  65. fg->area();
  66. Figure &rrf = t;
  67. rrf.set(4, 5);
  68. rrf.area();
  69. }

重载() []

  1. #include<iostream>
  2. using namespace std;
  3. class X
  4. {
  5. public:
  6. int operator() (int i = 0)
  7. {
  8. cout << "X::operator(" << i << ")" << endl; return i;
  9. };
  10. int operator() (int i, int j)
  11. {
  12. cout << "X::operator(" << i << "," << j << ")" << endl;
  13. return i;
  14. };
  15. int operator[] (int i)
  16. {
  17. cout << "X::operator[" << i << "]" << endl;
  18. return i;
  19. };
  20. int operator[] ( char* cp)
  21. {
  22. cout << "X::operator[" << cp << "]" << endl;
  23. return 0;
  24. };
  25. };
  26. int main(void)
  27. {
  28. X obj;
  29. char data[] = "ass";
  30. int i = obj(obj(1), 2);
  31. int a = obj[i];
  32. int b = obj[data];
  33. cout << "a=" << a << endl;
  34. cout << "b=" << b << endl;
  35. //system("pause");
  36. }

[] 实战

  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdio>
  4. using namespace std;
  5. struct Person {
  6. double salary;
  7. char *name;
  8. };
  9. class SalaryManager {
  10. public:
  11. SalaryManager();
  12. ~SalaryManager();
  13. SalaryManager(int Max = 0) {
  14. max = Max;
  15. n = 0;
  16. employ = new Person[max];
  17. }
  18. double &operator[](char *Name)//重载[],返回引用
  19. {
  20. Person *p;
  21. for (p = employ; p < employ + n; p++) {
  22. //如果存在则处理
  23. if (strcmp(p->name, Name) == 0) {
  24. return p->salary;
  25. }
  26. }
  27. p = employ + n++;
  28. p->name = new char[strlen(Name) + 1];
  29. strcpy(p->name, Name);
  30. p->salary = 0;
  31. return p->salary;
  32. }
  33. void display() {
  34. for (int i = 0; i < n; i++) {
  35. cout << employ[i].name << " " << employ[i].salary << endl;
  36. }
  37. }
  38. private:
  39. Person *employ;//存放职工信息的数组
  40. int max; //数组下标上界
  41. int n; //数组中的实际职工人数
  42. };
  43. SalaryManager::SalaryManager() {
  44. }
  45. SalaryManager::~SalaryManager() {
  46. }
  47. int main() {
  48. SalaryManager s(3);
  49. s["张三"] = 2323;
  50. s["李四"] = 34343;
  51. s["王五"] = 3432;
  52. cout << "zhangsan\n" << s["张三"] << endl;
  53. cout << "lisi\n" << s["李四"] << endl;
  54. s.display();
  55. }

线程1 (范围锁,等待,唤醒)

  1. #include <iostream> // std::cout
  2. #include <thread> // std::thread
  3. #include <mutex> // std::mutex, std::unique_lock
  4. #include <condition_variable> // std::condition_variable
  5. std::mutex mtx; // 全局互斥锁.
  6. std::condition_variable cv; // 全局条件变量.
  7. bool ready = false; // 全局标志位.
  8. void do_print_id(int id)
  9. {
  10. std::cout << "thread " << id << "start"<<'\n';
  11. std::unique_lock <std::mutex> lck(mtx);
  12. while (!ready) // 如果标志位不为 true, 则等待...
  13. cv.wait(lck); // 当前线程被阻塞, 当全局标志位变为 true 之后,
  14. // 线程被唤醒, 继续往下执行打印线程编号id.
  15. std::cout << "thread " << id << '\n';
  16. }
  17. void go()
  18. {
  19. std::unique_lock <std::mutex> lck(mtx);
  20. ready = true; // 设置全局标志位为 true.
  21. cv.notify_all(); // 唤醒所有线程.
  22. }
  23. int main()
  24. {
  25. std::thread threads[10];
  26. // spawn 10 threads:
  27. for (int i = 0; i < 10; ++i)
  28. threads[i] = std::thread(do_print_id, i);
  29. std::cout << "10 threads ready to race...\n";
  30. go(); // go!
  31. for (auto & th:threads)
  32. th.join();
  33. return 0;
  34. }

线程2 生产者消费者问题

  1. #include<iostream>
  2. #include<deque>
  3. #include<thread>
  4. #include<mutex>
  5. #include<condition_variable>
  6. #include<windows.h>
  7. using namespace std;
  8. deque<int> q;
  9. mutex mu;//锁机制
  10. condition_variable cond;//通信用的条件变量
  11. int c=0;//缓冲区的产品个数
  12. void producer()
  13. {
  14. int data1;
  15. while (true) {
  16. if(c<3) //限流
  17. {
  18. data1=rand();
  19. unique_lock<mutex> lck(mu); //锁
  20. q.push_front(data1);
  21. cout<<"存了"<<data1<<endl;
  22. cond.notify_one(); //通知取
  23. ++c;
  24. }
  25. Sleep(500);
  26. }
  27. }
  28. void consumer()
  29. {
  30. int data2; //data用来覆盖存放取的数据
  31. while(true)
  32. {
  33. unique_lock<mutex> locker(mu);
  34. while (q.empty()) {
  35. cond.wait((locker)); //wait()阻塞前会先解锁,解锁后生产者才能获得锁来把产品放到缓冲区;生产者notify后,将不再阻塞,且自动又获得了锁.
  36. //线程在 condition_variable上等待时,会释放已持有的锁,直至被唤醒后(此时队列非空)重新获取锁。
  37. }
  38. data2=q.back(); //取的第二步
  39. cout<<"got "<<data2<<endl;
  40. --c;
  41. }
  42. Sleep(1500);
  43. }
  44. int main()
  45. {
  46. thread t1(producer);
  47. thread t2(consumer);
  48. t1.join();
  49. t2.join();
  50. return 0;
  51. }

随机数

  1. #include<iostream>
  2. #include<vector>
  3. #include<random>
  4. using namespace std;
  5. using my_engine= default_random_engine; //引擎类型
  6. using my_distribution= uniform_int_distribution<>; //分布类型
  7. class Rand_int
  8. {
  9. public:
  10. Rand_int(int low, int high):dist(low, high){}
  11. int operator()(){return dist(re);}
  12. private:
  13. default_random_engine re;
  14. uniform_int_distribution<> dist;
  15. };
  16. int main()
  17. {
  18. Rand_int rnd{0,4}; //创建一个随机数生成器
  19. vector<int> histogram(5); //构建一个相应尺寸的vector
  20. for(int i=0;i!=200;++i)
  21. {
  22. ++histogram[rnd()]; //用[0:4]之间的每个数字出现的次数填充histogram
  23. }
  24. for(int i=0;i!=histogram.size();++i)
  25. {
  26. cout<<i<<'\t';
  27. for(int j=0;j!=histogram[i];++j)
  28. cout<<'*';
  29. cout<<endl;
  30. }
  31. }

线程 future(有问题,乱码)

  1. #include <iostream>
  2. #include <thread>
  3. #include <mutex>
  4. #include<future>
  5. #include<Windows.h>
  6. using namespace std;
  7. double t1(const double a, const double b)
  8. {
  9. double c = a + b;
  10. Sleep(3000);//假设t1函数是个复杂的计算过程,需要消耗3秒
  11. return c;
  12. }
  13. int main()
  14. {
  15. double a = 2.3;
  16. double b = 6.7;
  17. future<double> fu = async(t1, a, b);//创建异步线程线程,并将线程的执行结果用fu占位;
  18. cout << "waiting..." << endl;
  19. cout << "wait a moment" << endl;
  20. cout<<a<<"+"<<b<<"="<<endl;
  21. cout << "result:" << fu.get() << endl;//阻塞主线程,直至异步线程return
  22. //cout << "计算结果:" << fu.get() << endl;//取消该语句注释后运行会报错,因为future对象的get()方法只能调用一次。
  23. return 0;
  24. }