21xrx.com
2024-09-19 09:55:52 Thursday
登录
文章检索 我的文章 写文章
C++必背100行代码
2023-07-01 01:16:59 深夜i     --     --
C++ 必背 100行代码 编程 基础知识

在学习编程语言的过程中,掌握一些基础的代码片段是非常重要的。这些代码片段可以帮助我们快速地解决一些问题,提高流程效率。今天,我们来学习C++语言必背的100行代码。

一、输入输出

1. 输出一个字符串


cout << "Hello world!" << endl;

2. 从键盘输入一个整数


int n;

cin >> n;

3. 输出一个整数


int n = 123;

cout << n << endl;

4. 输出一个带有占位符(类似于printf函数)


int n = 123;

printf("n=%d", n);

5. 输出一个浮点数


float f = 1.23;

cout << f << endl;

6. 输出一个双精度浮点数


double d = 1.23;

cout << d << endl;

二、字符串操作

7. 拷贝字符串


char str1[] = "Hello";

char str2[10];

strcpy(str2, str1);

cout << str2 << endl;

8. 连接字符串


char str1[] = "Hello";

char str2[] = "world";

char str3[15];

strcpy(str3, str1);

strcat(str3, str2);

cout << str3 << endl;

9. 字符串长度


char str[] = "Hello";

int len = strlen(str);

cout << len << endl;

10. 字符串比较


char str1[] = "Hello";

char str2[] = "hello";

int cmp = strcmp(str1, str2);

if (cmp == 0)

  cout << "Equal" << endl;

else

  cout << "Not equal" << endl;

三、数组

11. 初始化数组


int arr[] = 3;

12. 访问数组元素


int arr[] = 7;

for (int i = 0; i < 5; i++) {

  cout << arr[i] << " ";

}

cout << endl;

13. 冒泡排序


int arr[] = 5;

for (int i = 0; i < 5; i++) {

  for (int j = i + 1; j < 5; j++) {

    if (arr[i] > arr[j]) {

      int t = arr[i];

      arr[i] = arr[j];

      arr[j] = t;

    }

  }

}

for (int i = 0; i < 5; i++) {

  cout << arr[i] << " ";

}

cout << endl;

14. 数组求和


int arr[] = 3;

int sum = 0;

for (int i = 0; i < 5; i++) {

  sum += arr[i];

}

cout << sum << endl;

四、循环

15. while循环


int n = 1;

while (n <= 10) {

  cout << n << endl;

  n++;

}

16. do-while循环


int n = 20;

do {

  cout << n << endl;

  n++;

} while (n <= 10);

17. for循环


for (int i = 1; i <= 10; i++)

  cout << i << endl;

18. 打印九九乘法表


for (int i = 1; i <= 9; i++) {

  for (int j = 1; j <= i; j++) {

    cout << j << "*" << i << "=" << j * i << " ";

  }

  cout << endl;

}

五、条件语句

19. if语句


int n = 10;

if (n > 0)

  cout << "Positive" << endl;

else if (n == 0)

  cout << "Zero" << endl;

else

  cout << "Negative" << endl;

20. switch语句


char c = 'A';

switch (c)

  case 'A':

    cout << "Excellent" << endl;

    break;

  case 'B':

    cout << "Good" << endl;

    break;

  case 'C':

    cout << "OK" << endl;

    break;

  default:

    cout << "Fail" << endl;

    break;

六、函数

21. 定义函数


int add(int x, int y) {

  return x + y;

}

22. 调用函数


int a = 1, b = 2;

int c = add(a, b);

cout << c << endl;

七、指针

23. 定义指针变量


int n = 123;

int *p = &n;

24. 访问指针变量


int n = 123;

int *p = &n;

cout << *p << endl;

25. 指针运算,遍历数组


int arr[] = 1;

for (int *p = arr; p < arr + 5; p++) {

  cout << *p << " ";

}

cout << endl;

26. 动态分配内存


int *p = new int;

*p = 123;

cout << *p << endl;

delete p;

八、结构体

27. 定义结构体


struct Student {

  char name[20];

  int age;

  float score;

};

28. 初始化结构体


struct Student s1 = 85.5;

struct Student s2 = 21;

29. 访问结构体成员


struct Student s1 = 85.5;

cout << s1.name << " " << s1.age << " " << s1.score << endl;

九、类

30. 定义类


class Point

public:

  int x;

  int y;

;

31. 创建对象


Point p1;

p1.x = 1;

p1.y = 2;

32. 构造函数


class Point {

public:

  int x;

  int y;

  Point(int a, int b) : x(a), y(b) {}

};

33. 调用构造函数


Point p1(1, 2);

十、继承

34. 定义基类


class Shape {

public:

  double area;

  virtual void computeArea() = 0;

};

35. 定义派生类


class Circle : public Shape {

public:

  double radius;

  void computeArea() {

    area = 3.14 * radius * radius;

  }

};

36. 使用派生类


Circle c;

c.radius = 2.0;

c.computeArea();

cout << c.area << endl;

十一、异常处理

37. 抛出异常


int n = 0;

if (n == 0)

  throw "n cannot be zero";

38. 捕获异常


try

  // some code

catch (const char* msg)

  cout << "Exception: " << msg << endl;

总结:以上100行代码包含了使用C++语言编写程序中常用的代码片段,涵盖了输入输出、字符串操作、数组、循环、条件语句、函数、指针、结构体、类、继承、异常处理等方面。掌握这些代码,可以让我们在编写程序时更加得心应手,提高生产效率,快速定位代码问题并进行解决。希望大家在学习C++语言时,努力掌握这些必背代码,提高自己的编程水平。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复