吴裕雄--天生自然C++语言学习笔记:C++ 实例
- C++ 实例 - 输出 "Hello, World!"
- #include <iostream>
- using namespace std;
- int main()
- {
- cout << "Hello, World!";
- return ;
- }
- C++ 实例 - 标准输入输出
- #include <iostream>
- using namespace std;
- int main()
- {
- int number;
- cout << "输入一个整数: ";
- cin >> number;
- cout << "输入的数字为: " << number;
- return ;
- }
- C++ 实例 - 实现两个数相加
- #include <iostream>
- using namespace std;
- int main()
- {
- int firstNumber, secondNumber, sumOfTwoNumbers;
- cout << "输入两个整数: ";
- cin >> firstNumber >> secondNumber;
- // 相加
- sumOfTwoNumbers = firstNumber + secondNumber;
- // 输出
- cout << firstNumber << " + " << secondNumber << " = " << sumOfTwoNumbers;
- return ;
- }
- C++ 实例 - 求商及余数
- #include <iostream>
- using namespace std;
- int main()
- {
- int divisor, dividend, quotient, remainder;
- cout << "输入被除数: ";
- cin >> dividend;
- cout << "输入除数: ";
- cin >> divisor;
- quotient = dividend / divisor;
- remainder = dividend % divisor;
- cout << "商 = " << quotient << endl;
- cout << "余数 = " << remainder;
- return ;
- }
- C++ 实例 - 查看 int, float, double 和 char 变量大小
- #include <iostream>
- using namespace std;
- int main()
- {
- cout << "char: " << sizeof(char) << " 字节" << endl;
- cout << "int: " << sizeof(int) << " 字节" << endl;
- cout << "float: " << sizeof(float) << " 字节" << endl;
- cout << "double: " << sizeof(double) << " 字节" << endl;
- return ;
- }
- C++ 实例 - 交换两个数
- #include <iostream>
- using namespace std;
- int main()
- {
- int a = , b = , temp;
- cout << "交换之前:" << endl;
- cout << "a = " << a << ", b = " << b << endl;
- temp = a;
- a = b;
- b = temp;
- cout << "\n交换之后:" << endl;
- cout << "a = " << a << ", b = " << b << endl;
- return ;
- }
- C++ 实例 - 判断一个数是奇数还是偶数
- #include <iostream>
- using namespace std;
- int main()
- {
- int n;
- cout << "输入一个整数: ";
- cin >> n;
- if ( n % == )
- cout << n << " 为偶数。";
- else
- cout << n << " 为奇数。";
- return ;
- }
- C++ 实例 - 判断元音/辅音
- #include <iostream>
- using namespace std;
- int main()
- {
- char c;
- int isLowercaseVowel, isUppercaseVowel;
- cout << "输入一个字母: ";
- cin >> c;
- // 小写字母元音
- isLowercaseVowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
- // 大写字母元音
- isUppercaseVowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
- // if 语句判断
- if (isLowercaseVowel || isUppercaseVowel)
- cout << c << " 是元音";
- else
- cout << c << " 是辅音";
- return ;
- }
- C++ 实例 - 判断三个数中的最大数
- #include <iostream>
- using namespace std;
- int main()
- {
- float n1, n2, n3;
- cout << "请输入三个数: ";
- cin >> n1 >> n2 >> n3;
- if(n1 >= n2 && n1 >= n3)
- {
- cout << "最大数为: " << n1;
- }
- if(n2 >= n1 && n2 >= n3)
- {
- cout << "最大数为: " << n2;
- }
- if(n3 >= n1 && n3 >= n2) {
- cout << "最大数为: " << n3;
- }
- return ;
- }
- C++ 实例 - 求一元二次方程的根
- #include <iostream>
- #include <cmath>
- using namespace std;
- int main() {
- float a, b, c, x1, x2, discriminant, realPart, imaginaryPart;
- cout << "输入 a, b 和 c: ";
- cin >> a >> b >> c;
- discriminant = b*b - *a*c;
- if (discriminant > ) {
- x1 = (-b + sqrt(discriminant)) / (*a);
- x2 = (-b - sqrt(discriminant)) / (*a);
- cout << "Roots are real and different." << endl;
- cout << "x1 = " << x1 << endl;
- cout << "x2 = " << x2 << endl;
- }
- else if (discriminant == ) {
- cout << "实根相同:" << endl;
- x1 = (-b + sqrt(discriminant)) / (*a);
- cout << "x1 = x2 =" << x1 << endl;
- }
- else {
- realPart = -b/(*a);
- imaginaryPart =sqrt(-discriminant)/(*a);
- cout << "实根不同:" << endl;
- cout << "x1 = " << realPart << "+" << imaginaryPart << "i" << endl;
- cout << "x2 = " << realPart << "-" << imaginaryPart << "i" << endl;
- }
- return ;
- }
- C++ 实例 - 计算自然数之和
- #include <iostream>
- using namespace std;
- int main()
- {
- int n, sum = ;
- cout << "输入一个正整数: ";
- cin >> n;
- for (int i = ; i <= n; ++i) {
- sum += i;
- }
- cout << "Sum = " << sum;
- return ;
- }
- C++ 实例 - 判断闰年
- #include <iostream>
- using namespace std;
- int main()
- {
- int year;
- cout << "输入年份: ";
- cin >> year;
- if (year % == )
- {
- if (year % == )
- {
- // // 这里如果被 400 整数是闰年
- if (year % == )
- cout << year << " 是闰年";
- else
- cout << year << " 不是闰年";
- }
- else
- cout << year << " 是闰年";
- }
- else
- cout << year << " 不是闰年";
- return ;
- }
- C++ 实例 - 求一个数的阶乘
- #include <iostream>
- using namespace std;
- int main()
- {
- unsigned int n;
- unsigned long long factorial = ;
- cout << "输入一个整数: ";
- cin >> n;
- for(int i = ; i <=n; ++i)
- {
- factorial *= i;
- }
- cout << n << " 的阶乘为:"<< " = " << factorial;
- return ;
- }
- C++ 实例 - 创建各类三角形图案
- #include <iostream>
- using namespace std;
- int main()
- {
- int rows;
- cout << "输入行数: ";
- cin >> rows;
- for(int i = ; i <= rows; ++i)
- {
- for(int j = ; j <= i; ++j)
- {
- cout << "* ";
- }
- cout << "\n";
- }
- return ;
- }
- #include <iostream>
- using namespace std;
- int main()
- {
- char input, alphabet = 'A';
- cout << "输入最后一个大写字母: ";
- cin >> input;
- for(int i = ; i <= (input-'A'+); ++i)
- {
- for(int j = ; j <= i; ++j)
- {
- cout << alphabet << " ";
- }
- ++alphabet;
- cout << endl;
- }
- return ;
- }
- #include <iostream>
- using namespace std;
- int main()
- {
- int rows;
- cout << "输入行数: ";
- cin >> rows;
- for(int i = rows; i >= ; --i)
- {
- for(int j = ; j <= i; ++j)
- {
- cout << "* ";
- }
- cout << endl;
- }
- return ;
- }
- #include <iostream>
- using namespace std;
- int main()
- {
- int rows;
- cout << "输入行数: ";
- cin >> rows;
- for(int i = rows; i >= ; --i)
- {
- for(int j = ; j <= i; ++j)
- {
- cout << j << " ";
- }
- cout << endl;
- }
- return ;
- }
- #include <iostream>
- using namespace std;
- int main()
- {
- int space, rows;
- cout <<"输入行数: ";
- cin >> rows;
- for(int i = , k = ; i <= rows; ++i, k = )
- {
- for(space = ; space <= rows-i; ++space)
- {
- cout <<" ";
- }
- while(k != *i-)
- {
- cout << "* ";
- ++k;
- }
- cout << endl;
- }
- return ;
- }
- #include <iostream>
- using namespace std;
- int main()
- {
- int rows, count = , count1 = , k = ;
- cout << "输入行数: ";
- cin >> rows;
- for(int i = ; i <= rows; ++i)
- {
- for(int space = ; space <= rows-i; ++space)
- {
- cout << " ";
- ++count;
- }
- while(k != *i-)
- {
- if (count <= rows-)
- {
- cout << i+k << " ";
- ++count;
- }
- else
- {
- ++count1;
- cout << i+k-*count1 << " ";
- }
- ++k;
- }
- count1 = count = k = ;
- cout << endl;
- }
- return ;
- }
- #include <iostream>
- using namespace std;
- int main()
- {
- int rows;
- cout << "输入行数: ";
- cin >> rows;
- for(int i = rows; i >= ; --i)
- {
- for(int space = ; space < rows-i; ++space)
- cout << " ";
- for(int j = i; j <= *i-; ++j)
- cout << "* ";
- for(int j = ; j < i-; ++j)
- cout << "* ";
- cout << endl;
- }
- return ;
- }
- #include <iostream>
- using namespace std;
- int main()
- {
- int rows, coef = ;
- cout << "Enter number of rows: ";
- cin >> rows;
- for(int i = ; i < rows; i++)
- {
- for(int space = ; space <= rows-i; space++)
- cout <<" ";
- for(int j = ; j <= i; j++)
- {
- if (j == || i == )
- coef = ;
- else
- coef = coef*(i-j+)/j;
- cout << coef << " ";
- }
- cout << endl;
- }
- return ;
- }
- #include <iostream>
- using namespace std;
- int main()
- {
- int rows, number = ;
- cout << "输入行数: ";
- cin >> rows;
- for(int i = ; i <= rows; i++)
- {
- for(int j = ; j <= i; ++j)
- {
- cout << number << " ";
- ++number;
- }
- cout << endl;
- }
- return ;
- }
- C++ 实例 - 求两数的最大公约数
- #include <iostream>
- using namespace std;
- int main()
- {
- int n1, n2;
- cout << "输入两个整数: ";
- cin >> n1 >> n2;
- while(n1 != n2)
- {
- if(n1 > n2)
- n1 -= n2;
- else
- n2 -= n1;
- }
- cout << "HCF = " << n1;
- return ;
- }
- C++ 实例 - 求两数最小公倍数
- #include <iostream>
- using namespace std;
- int main()
- {
- int n1, n2, max;
- cout << "输入两个数: ";
- cin >> n1 >> n2;
- // 获取最大的数
- max = (n1 > n2) ? n1 : n2;
- do
- {
- if (max % n1 == && max % n2 == )
- {
- cout << "LCM = " << max;
- break;
- }
- else
- ++max;
- } while (true);
- return ;
- }
- C++ 实例 - 实现一个简单的计算器
- #include <iostream>
- using namespace std;
- int main()
- {
- char op;
- float num1, num2;
- cout << "输入运算符:+、-、*、/ : ";
- cin >> op;
- cout << "输入两个数: ";
- cin >> num1 >> num2;
- switch(op)
- {
- case '+':
- cout << num1+num2;
- break;
- case '-':
- cout << num1-num2;
- break;
- case '*':
- cout << num1*num2;
- break;
- case '/':
- cout << num1/num2;
- break;
- default:
- // 如果运算符不是 +, -, * 或 /, 提示错误信息
- cout << "Error! 请输入正确运算符。";
- break;
- }
- return ;
- }
- 猴子吃桃问题
- 一只小猴子一天摘了许多桃子,第一天吃了一半,然后忍不住又吃了一个;第二天又吃了一半,再加上一个;后面每天都是这样吃。到第10天的时候,小猴子发现只有一个桃子了。问小猴子第一天共摘了多少个桃子。
- #include <iostream>
- using namespace std;
- int main()
- {
- int x, y, n;
- for (x=, n=; n<; y=(x+)*, x=y, n++);
- cout<<"第一天共摘的桃子数量为 "<<x<<endl;
- return ;
- }
吴裕雄--天生自然C++语言学习笔记:C++ 实例的更多相关文章
- 吴裕雄--天生自然C++语言学习笔记:C++ 标准库
C++ 标准库可以分为两部分: 标准函数库: 这个库是由通用的.独立的.不属于任何类的函数组成的.函数库继承自 C 语言. 面向对象类库: 这个库是类及其相关函数的集合. C++ 标准库包含了所有的 ...
- 吴裕雄--天生自然C++语言学习笔记:C++ 动态内存
栈:在函数内部声明的所有变量都将占用栈内存. 堆:这是程序中未使用的内存,在程序运行时可用于动态分配内存. 可以使用特殊的运算符为给定类型的变量在运行时分配堆内的内存,这会返回所分配的空间地址.这种运 ...
- 吴裕雄--天生自然C++语言学习笔记:C++ 类 & 对象
C++ 在 C 语言的基础上增加了面向对象编程,C++ 支持面向对象程序设计.类是 C++ 的核心特性,通常被称为用户定义的类型. 类用于指定对象的形式,它包含了数据表示法和用于处理数据的方法.类中的 ...
- 吴裕雄--天生自然C++语言学习笔记:C++ 日期 & 时间
C++ 标准库没有提供所谓的日期类型.C++ 继承了 C 语言用于日期和时间操作的结构和函数.为了使用日期和时间相关的函数和结构,需要在 C++ 程序中引用 <ctime> 头文件. 有四 ...
- 吴裕雄--天生自然C++语言学习笔记:C++ 字符串
C++ 提供了以下两种类型的字符串表示形式: C 风格字符串 C++ 引入的 string 类类型 C 风格的字符串起源于 C 语言,并在 C++ 中继续得到支持.字符串实际上是使用 null 字符 ...
- 吴裕雄--天生自然C++语言学习笔记:C++ 基本语法
C++ 程序可以定义为对象的集合,这些对象通过调用彼此的方法进行交互. 对象 - 对象具有状态和行为.例如:一只狗的状态 - 颜色.名称.品种,行为 - 摇动.叫唤.吃.对象是类的实例. 类 - 类可 ...
- 吴裕雄--天生自然C++语言学习笔记:C++简介
C++ 是一种中级语言,它是由 Bjarne Stroustrup 于 年在贝尔实验室开始设计开发的.C++ 进一步扩充和完善了 C 语言,是一种面向对象的程序设计语言.C++ 可运行于多种平台上,如 ...
- 吴裕雄--天生自然C++语言学习笔记:C++ STL 教程
C++ STL(标准模板库)是一套功能强大的 C++ 模板类,提供了通用的模板类和函数,这些模板类和函数可以实现多种流行和常用的算法和数据结构,如向量.链表.队列.栈. C++ 标准模板库的核心包括以 ...
- 吴裕雄--天生自然C++语言学习笔记:C++ Web 编程
什么是 CGI? 公共网关接口(CGI),是一套标准,定义了信息是如何在 Web 服务器和客户端脚本之间进行交换的. CGI 规范目前是由 NCSA 维护的,NCSA 定义 CGI 如下: 公共网关接 ...
随机推荐
- python中metaclass的工作原理
class TMetaclass(type): def __new__(cls, name, bases, attrs): print(cls, name, bases, attrs) return ...
- [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 ...
- 「PA2014」Kuglarz
传送门 memset0好评 解题思路 其实这是一道图论题 不难发现,如果知道了 \(\sum1...i\) 和 \(\sum1...j\) 的奇偶性,那么就可以得知 \(\sum i+1...j\) ...
- Lesson 44 Patterns of culture
What influences us from the moment of birth? Custom has not commonly been regarded as a subject of a ...
- k-近邻算法python代码实现(非常全)
1.k近邻算法是学习机器学习算法最为经典和简单的算法,它是机器学习算法入门最好的算法之一,可以非常好并且快速地理解机器学习的算法的框架与应用.它是一种经典简单的分类算法,当然也可以用来解决回归问题.2 ...
- Vue项目中v-for无法渲染数据
在Vue项目中,我们想要实现下面的布局效果 后端返回的数据格式如下,可以看出产品列表五张图的数据位于同一个数组中 而我的html结构如下: 我希望直接渲染左边一张大图,然后右边的四张小图通过v-for ...
- 全面理解Java中的String数据类型
1. 首先String不属于8种基本数据类型,String是一个对象. 因为对象的默认值是null,所以String的默认值也是null:但它又是一种特殊的对象,有其它对象没有的一些特性. 2. ne ...
- Adapter之spinner
前言: 在写代码当中有时候会用到下拉列表,下面我们讲一下spinner 正文: 因为比较简单,和之前的listView很像,所以直接上代码 <Spinner android:layout_wid ...
- vscode spring boot配置文件application.properties不提示解决方式
背景 因实际的编程环境是jdk1.6,vscode安装了spring boot tools开发后,application.properties无法提示.spring boot tools的功能之一就是 ...
- 开发自己的 chart【转】
Kubernetes 给我们提供了大量官方 chart,不过要部署微服务应用,还是需要开发自己的 chart,下面就来实践这个主题. 创建 chart 执行 helm create mychart 的 ...