1. C++ 实例 - 输出 "Hello, World!"
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. cout << "Hello, World!";
  8. return ;
  9. }
  10. C++ 实例 - 标准输入输出
  11. #include <iostream>
  12. using namespace std;
  13.  
  14. int main()
  15. {
  16. int number;
  17.  
  18. cout << "输入一个整数: ";
  19. cin >> number;
  20.  
  21. cout << "输入的数字为: " << number;
  22. return ;
  23. }
  24. C++ 实例 - 实现两个数相加
  25. #include <iostream>
  26. using namespace std;
  27.  
  28. int main()
  29. {
  30. int firstNumber, secondNumber, sumOfTwoNumbers;
  31.  
  32. cout << "输入两个整数: ";
  33. cin >> firstNumber >> secondNumber;
  34.  
  35. // 相加
  36. sumOfTwoNumbers = firstNumber + secondNumber;
  37.  
  38. // 输出
  39. cout << firstNumber << " + " << secondNumber << " = " << sumOfTwoNumbers;
  40.  
  41. return ;
  42. }
  43. C++ 实例 - 求商及余数
  44. #include <iostream>
  45. using namespace std;
  46.  
  47. int main()
  48. {
  49. int divisor, dividend, quotient, remainder;
  50.  
  51. cout << "输入被除数: ";
  52. cin >> dividend;
  53.  
  54. cout << "输入除数: ";
  55. cin >> divisor;
  56.  
  57. quotient = dividend / divisor;
  58. remainder = dividend % divisor;
  59.  
  60. cout << "商 = " << quotient << endl;
  61. cout << "余数 = " << remainder;
  62.  
  63. return ;
  64. }
  65. C++ 实例 - 查看 int, float, double char 变量大小
  66. #include <iostream>
  67. using namespace std;
  68.  
  69. int main()
  70. {
  71. cout << "char: " << sizeof(char) << " 字节" << endl;
  72. cout << "int: " << sizeof(int) << " 字节" << endl;
  73. cout << "float: " << sizeof(float) << " 字节" << endl;
  74. cout << "double: " << sizeof(double) << " 字节" << endl;
  75.  
  76. return ;
  77. }
  78. C++ 实例 - 交换两个数
  79. #include <iostream>
  80. using namespace std;
  81.  
  82. int main()
  83. {
  84. int a = , b = , temp;
  85.  
  86. cout << "交换之前:" << endl;
  87. cout << "a = " << a << ", b = " << b << endl;
  88.  
  89. temp = a;
  90. a = b;
  91. b = temp;
  92.  
  93. cout << "\n交换之后:" << endl;
  94. cout << "a = " << a << ", b = " << b << endl;
  95.  
  96. return ;
  97. }
  98. C++ 实例 - 判断一个数是奇数还是偶数
  99. #include <iostream>
  100. using namespace std;
  101.  
  102. int main()
  103. {
  104. int n;
  105.  
  106. cout << "输入一个整数: ";
  107. cin >> n;
  108.  
  109. if ( n % == )
  110. cout << n << " 为偶数。";
  111. else
  112. cout << n << " 为奇数。";
  113.  
  114. return ;
  115. }
  116. C++ 实例 - 判断元音/辅音
  117. #include <iostream>
  118. using namespace std;
  119.  
  120. int main()
  121. {
  122. char c;
  123. int isLowercaseVowel, isUppercaseVowel;
  124.  
  125. cout << "输入一个字母: ";
  126. cin >> c;
  127.  
  128. // 小写字母元音
  129. isLowercaseVowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
  130.  
  131. // 大写字母元音
  132. isUppercaseVowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
  133.  
  134. // if 语句判断
  135. if (isLowercaseVowel || isUppercaseVowel)
  136. cout << c << " 是元音";
  137. else
  138. cout << c << " 是辅音";
  139.  
  140. return ;
  141. }
  142. C++ 实例 - 判断三个数中的最大数
  143. #include <iostream>
  144. using namespace std;
  145.  
  146. int main()
  147. {
  148. float n1, n2, n3;
  149.  
  150. cout << "请输入三个数: ";
  151. cin >> n1 >> n2 >> n3;
  152.  
  153. if(n1 >= n2 && n1 >= n3)
  154. {
  155. cout << "最大数为: " << n1;
  156. }
  157.  
  158. if(n2 >= n1 && n2 >= n3)
  159. {
  160. cout << "最大数为: " << n2;
  161. }
  162.  
  163. if(n3 >= n1 && n3 >= n2) {
  164. cout << "最大数为: " << n3;
  165. }
  166.  
  167. return ;
  168. }
  169. C++ 实例 - 求一元二次方程的根
  170. #include <iostream>
  171. #include <cmath>
  172. using namespace std;
  173.  
  174. int main() {
  175.  
  176. float a, b, c, x1, x2, discriminant, realPart, imaginaryPart;
  177. cout << "输入 a, b 和 c: ";
  178. cin >> a >> b >> c;
  179. discriminant = b*b - *a*c;
  180.  
  181. if (discriminant > ) {
  182. x1 = (-b + sqrt(discriminant)) / (*a);
  183. x2 = (-b - sqrt(discriminant)) / (*a);
  184. cout << "Roots are real and different." << endl;
  185. cout << "x1 = " << x1 << endl;
  186. cout << "x2 = " << x2 << endl;
  187. }
  188.  
  189. else if (discriminant == ) {
  190. cout << "实根相同:" << endl;
  191. x1 = (-b + sqrt(discriminant)) / (*a);
  192. cout << "x1 = x2 =" << x1 << endl;
  193. }
  194.  
  195. else {
  196. realPart = -b/(*a);
  197. imaginaryPart =sqrt(-discriminant)/(*a);
  198. cout << "实根不同:" << endl;
  199. cout << "x1 = " << realPart << "+" << imaginaryPart << "i" << endl;
  200. cout << "x2 = " << realPart << "-" << imaginaryPart << "i" << endl;
  201. }
  202.  
  203. return ;
  204. }
  205. C++ 实例 - 计算自然数之和
  206. #include <iostream>
  207. using namespace std;
  208.  
  209. int main()
  210. {
  211. int n, sum = ;
  212.  
  213. cout << "输入一个正整数: ";
  214. cin >> n;
  215.  
  216. for (int i = ; i <= n; ++i) {
  217. sum += i;
  218. }
  219.  
  220. cout << "Sum = " << sum;
  221. return ;
  222. }
  223. C++ 实例 - 判断闰年
  224. #include <iostream>
  225. using namespace std;
  226.  
  227. int main()
  228. {
  229. int year;
  230.  
  231. cout << "输入年份: ";
  232. cin >> year;
  233.  
  234. if (year % == )
  235. {
  236. if (year % == )
  237. {
  238. // // 这里如果被 400 整数是闰年
  239. if (year % == )
  240. cout << year << " 是闰年";
  241. else
  242. cout << year << " 不是闰年";
  243. }
  244. else
  245. cout << year << " 是闰年";
  246. }
  247. else
  248. cout << year << " 不是闰年";
  249.  
  250. return ;
  251. }
  252. C++ 实例 - 求一个数的阶乘
  253. #include <iostream>
  254. using namespace std;
  255.  
  256. int main()
  257. {
  258. unsigned int n;
  259. unsigned long long factorial = ;
  260.  
  261. cout << "输入一个整数: ";
  262. cin >> n;
  263.  
  264. for(int i = ; i <=n; ++i)
  265. {
  266. factorial *= i;
  267. }
  268.  
  269. cout << n << " 的阶乘为:"<< " = " << factorial;
  270. return ;
  271. }
  272. C++ 实例 - 创建各类三角形图案
  273. #include <iostream>
  274. using namespace std;
  275.  
  276. int main()
  277. {
  278. int rows;
  279.  
  280. cout << "输入行数: ";
  281. cin >> rows;
  282.  
  283. for(int i = ; i <= rows; ++i)
  284. {
  285. for(int j = ; j <= i; ++j)
  286. {
  287. cout << "* ";
  288. }
  289. cout << "\n";
  290. }
  291. return ;
  292. }
  293. #include <iostream>
  294. using namespace std;
  295.  
  296. int main()
  297. {
  298. char input, alphabet = 'A';
  299.  
  300. cout << "输入最后一个大写字母: ";
  301. cin >> input;
  302.  
  303. for(int i = ; i <= (input-'A'+); ++i)
  304. {
  305. for(int j = ; j <= i; ++j)
  306. {
  307. cout << alphabet << " ";
  308. }
  309. ++alphabet;
  310.  
  311. cout << endl;
  312. }
  313. return ;
  314. }
  315. #include <iostream>
  316. using namespace std;
  317.  
  318. int main()
  319. {
  320. int rows;
  321.  
  322. cout << "输入行数: ";
  323. cin >> rows;
  324.  
  325. for(int i = rows; i >= ; --i)
  326. {
  327. for(int j = ; j <= i; ++j)
  328. {
  329. cout << "* ";
  330. }
  331. cout << endl;
  332. }
  333.  
  334. return ;
  335. }
  336. #include <iostream>
  337. using namespace std;
  338.  
  339. int main()
  340. {
  341. int rows;
  342.  
  343. cout << "输入行数: ";
  344. cin >> rows;
  345.  
  346. for(int i = rows; i >= ; --i)
  347. {
  348. for(int j = ; j <= i; ++j)
  349. {
  350. cout << j << " ";
  351. }
  352. cout << endl;
  353. }
  354.  
  355. return ;
  356. }
  357. #include <iostream>
  358. using namespace std;
  359.  
  360. int main()
  361. {
  362. int space, rows;
  363.  
  364. cout <<"输入行数: ";
  365. cin >> rows;
  366.  
  367. for(int i = , k = ; i <= rows; ++i, k = )
  368. {
  369. for(space = ; space <= rows-i; ++space)
  370. {
  371. cout <<" ";
  372. }
  373.  
  374. while(k != *i-)
  375. {
  376. cout << "* ";
  377. ++k;
  378. }
  379. cout << endl;
  380. }
  381. return ;
  382. }
  383. #include <iostream>
  384. using namespace std;
  385.  
  386. int main()
  387. {
  388. int rows, count = , count1 = , k = ;
  389.  
  390. cout << "输入行数: ";
  391. cin >> rows;
  392.  
  393. for(int i = ; i <= rows; ++i)
  394. {
  395. for(int space = ; space <= rows-i; ++space)
  396. {
  397. cout << " ";
  398. ++count;
  399. }
  400.  
  401. while(k != *i-)
  402. {
  403. if (count <= rows-)
  404. {
  405. cout << i+k << " ";
  406. ++count;
  407. }
  408. else
  409. {
  410. ++count1;
  411. cout << i+k-*count1 << " ";
  412. }
  413. ++k;
  414. }
  415. count1 = count = k = ;
  416.  
  417. cout << endl;
  418. }
  419. return ;
  420. }
  421. #include <iostream>
  422. using namespace std;
  423.  
  424. int main()
  425. {
  426. int rows;
  427.  
  428. cout << "输入行数: ";
  429. cin >> rows;
  430.  
  431. for(int i = rows; i >= ; --i)
  432. {
  433. for(int space = ; space < rows-i; ++space)
  434. cout << " ";
  435.  
  436. for(int j = i; j <= *i-; ++j)
  437. cout << "* ";
  438.  
  439. for(int j = ; j < i-; ++j)
  440. cout << "* ";
  441.  
  442. cout << endl;
  443. }
  444.  
  445. return ;
  446. }
  447. #include <iostream>
  448. using namespace std;
  449.  
  450. int main()
  451. {
  452. int rows, coef = ;
  453.  
  454. cout << "Enter number of rows: ";
  455. cin >> rows;
  456.  
  457. for(int i = ; i < rows; i++)
  458. {
  459. for(int space = ; space <= rows-i; space++)
  460. cout <<" ";
  461.  
  462. for(int j = ; j <= i; j++)
  463. {
  464. if (j == || i == )
  465. coef = ;
  466. else
  467. coef = coef*(i-j+)/j;
  468.  
  469. cout << coef << " ";
  470. }
  471. cout << endl;
  472. }
  473.  
  474. return ;
  475. }
  476. #include <iostream>
  477. using namespace std;
  478.  
  479. int main()
  480. {
  481. int rows, number = ;
  482.  
  483. cout << "输入行数: ";
  484. cin >> rows;
  485.  
  486. for(int i = ; i <= rows; i++)
  487. {
  488. for(int j = ; j <= i; ++j)
  489. {
  490. cout << number << " ";
  491. ++number;
  492. }
  493.  
  494. cout << endl;
  495. }
  496.  
  497. return ;
  498. }
  499. C++ 实例 - 求两数的最大公约数
  500. #include <iostream>
  501. using namespace std;
  502.  
  503. int main()
  504. {
  505. int n1, n2;
  506.  
  507. cout << "输入两个整数: ";
  508. cin >> n1 >> n2;
  509.  
  510. while(n1 != n2)
  511. {
  512. if(n1 > n2)
  513. n1 -= n2;
  514. else
  515. n2 -= n1;
  516. }
  517.  
  518. cout << "HCF = " << n1;
  519. return ;
  520. }
  521. C++ 实例 - 求两数最小公倍数
  522. #include <iostream>
  523. using namespace std;
  524.  
  525. int main()
  526. {
  527. int n1, n2, max;
  528.  
  529. cout << "输入两个数: ";
  530. cin >> n1 >> n2;
  531.  
  532. // 获取最大的数
  533. max = (n1 > n2) ? n1 : n2;
  534.  
  535. do
  536. {
  537. if (max % n1 == && max % n2 == )
  538. {
  539. cout << "LCM = " << max;
  540. break;
  541. }
  542. else
  543. ++max;
  544. } while (true);
  545.  
  546. return ;
  547. }
  548. C++ 实例 - 实现一个简单的计算器
  549. #include <iostream>
  550. using namespace std;
  551.  
  552. int main()
  553. {
  554. char op;
  555. float num1, num2;
  556.  
  557. cout << "输入运算符:+、-、*、/ : ";
  558. cin >> op;
  559.  
  560. cout << "输入两个数: ";
  561. cin >> num1 >> num2;
  562.  
  563. switch(op)
  564. {
  565. case '+':
  566. cout << num1+num2;
  567. break;
  568.  
  569. case '-':
  570. cout << num1-num2;
  571. break;
  572.  
  573. case '*':
  574. cout << num1*num2;
  575. break;
  576.  
  577. case '/':
  578. cout << num1/num2;
  579. break;
  580.  
  581. default:
  582. // 如果运算符不是 +, -, * 或 /, 提示错误信息
  583. cout << "Error! 请输入正确运算符。";
  584. break;
  585. }
  586.  
  587. return ;
  588. }
  589. 猴子吃桃问题
  590. 一只小猴子一天摘了许多桃子,第一天吃了一半,然后忍不住又吃了一个;第二天又吃了一半,再加上一个;后面每天都是这样吃。到第10天的时候,小猴子发现只有一个桃子了。问小猴子第一天共摘了多少个桃子。
  591. #include <iostream>
  592. using namespace std;
  593. int main()
  594. {
  595. int x, y, n;
  596. for (x=, n=; n<; y=(x+)*, x=y, n++);
  597. cout<<"第一天共摘的桃子数量为 "<<x<<endl;
  598. return ;
  599. }

吴裕雄--天生自然C++语言学习笔记:C++ 实例的更多相关文章

  1. 吴裕雄--天生自然C++语言学习笔记:C++ 标准库

    C++ 标准库可以分为两部分: 标准函数库: 这个库是由通用的.独立的.不属于任何类的函数组成的.函数库继承自 C 语言. 面向对象类库: 这个库是类及其相关函数的集合. C++ 标准库包含了所有的 ...

  2. 吴裕雄--天生自然C++语言学习笔记:C++ 动态内存

    栈:在函数内部声明的所有变量都将占用栈内存. 堆:这是程序中未使用的内存,在程序运行时可用于动态分配内存. 可以使用特殊的运算符为给定类型的变量在运行时分配堆内的内存,这会返回所分配的空间地址.这种运 ...

  3. 吴裕雄--天生自然C++语言学习笔记:C++ 类 & 对象

    C++ 在 C 语言的基础上增加了面向对象编程,C++ 支持面向对象程序设计.类是 C++ 的核心特性,通常被称为用户定义的类型. 类用于指定对象的形式,它包含了数据表示法和用于处理数据的方法.类中的 ...

  4. 吴裕雄--天生自然C++语言学习笔记:C++ 日期 & 时间

    C++ 标准库没有提供所谓的日期类型.C++ 继承了 C 语言用于日期和时间操作的结构和函数.为了使用日期和时间相关的函数和结构,需要在 C++ 程序中引用 <ctime> 头文件. 有四 ...

  5. 吴裕雄--天生自然C++语言学习笔记:C++ 字符串

    C++ 提供了以下两种类型的字符串表示形式: C 风格字符串 C++ 引入的 string 类类型 C 风格的字符串起源于 C 语言,并在 C++ 中继续得到支持.字符串实际上是使用 null 字符 ...

  6. 吴裕雄--天生自然C++语言学习笔记:C++ 基本语法

    C++ 程序可以定义为对象的集合,这些对象通过调用彼此的方法进行交互. 对象 - 对象具有状态和行为.例如:一只狗的状态 - 颜色.名称.品种,行为 - 摇动.叫唤.吃.对象是类的实例. 类 - 类可 ...

  7. 吴裕雄--天生自然C++语言学习笔记:C++简介

    C++ 是一种中级语言,它是由 Bjarne Stroustrup 于 年在贝尔实验室开始设计开发的.C++ 进一步扩充和完善了 C 语言,是一种面向对象的程序设计语言.C++ 可运行于多种平台上,如 ...

  8. 吴裕雄--天生自然C++语言学习笔记:C++ STL 教程

    C++ STL(标准模板库)是一套功能强大的 C++ 模板类,提供了通用的模板类和函数,这些模板类和函数可以实现多种流行和常用的算法和数据结构,如向量.链表.队列.栈. C++ 标准模板库的核心包括以 ...

  9. 吴裕雄--天生自然C++语言学习笔记:C++ Web 编程

    什么是 CGI? 公共网关接口(CGI),是一套标准,定义了信息是如何在 Web 服务器和客户端脚本之间进行交换的. CGI 规范目前是由 NCSA 维护的,NCSA 定义 CGI 如下: 公共网关接 ...

随机推荐

  1. python中metaclass的工作原理

    class TMetaclass(type): def __new__(cls, name, bases, attrs): print(cls, name, bases, attrs) return ...

  2. [ERROR] error: error while loading <root>, error in opening zip file error: scala.reflect.internal.MissingRequirementError: object scala.runtime in compiler mirror not found.

    在家编译一个Apache的开源项目,在编译时遇到错误如下: error: error while loading <root>, error in opening zip file [ER ...

  3. 「PA2014」Kuglarz

    传送门 memset0好评 解题思路 其实这是一道图论题 不难发现,如果知道了 \(\sum1...i\) 和 \(\sum1...j\) 的奇偶性,那么就可以得知 \(\sum i+1...j\) ...

  4. Lesson 44 Patterns of culture

    What influences us from the moment of birth? Custom has not commonly been regarded as a subject of a ...

  5. k-近邻算法python代码实现(非常全)

    1.k近邻算法是学习机器学习算法最为经典和简单的算法,它是机器学习算法入门最好的算法之一,可以非常好并且快速地理解机器学习的算法的框架与应用.它是一种经典简单的分类算法,当然也可以用来解决回归问题.2 ...

  6. Vue项目中v-for无法渲染数据

    在Vue项目中,我们想要实现下面的布局效果 后端返回的数据格式如下,可以看出产品列表五张图的数据位于同一个数组中 而我的html结构如下: 我希望直接渲染左边一张大图,然后右边的四张小图通过v-for ...

  7. 全面理解Java中的String数据类型

    1. 首先String不属于8种基本数据类型,String是一个对象. 因为对象的默认值是null,所以String的默认值也是null:但它又是一种特殊的对象,有其它对象没有的一些特性. 2. ne ...

  8. Adapter之spinner

    前言: 在写代码当中有时候会用到下拉列表,下面我们讲一下spinner 正文: 因为比较简单,和之前的listView很像,所以直接上代码 <Spinner android:layout_wid ...

  9. vscode spring boot配置文件application.properties不提示解决方式

    背景 因实际的编程环境是jdk1.6,vscode安装了spring boot tools开发后,application.properties无法提示.spring boot tools的功能之一就是 ...

  10. 开发自己的 chart【转】

    Kubernetes 给我们提供了大量官方 chart,不过要部署微服务应用,还是需要开发自己的 chart,下面就来实践这个主题. 创建 chart 执行 helm create mychart 的 ...