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++ 实例的更多相关文章

  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. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 表格:表示信息变化的操作

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  2. S7-300 符号IO域的组态 HMI变量指针化的方法以及应用,在一个IO域显示多个温度值的办法

    应用工业场景 例如需要测试很多个节点的温度值的时候,需要监控的位置很多,如果HMI的画面很小, 可以使用符号IO域和变量的间接寻址 符号IO域接近于VB中的combo 控件 实现上图的界面 : 上图界 ...

  3. 一探torch.nn究竟“What is torch.nn really?”

    来自: https://pytorch.org/tutorials/beginner/nn_tutorial.html <What is torch.nn really?>这文章看起来不能 ...

  4. JS 自动返回每个月的天数

    );//M代表月份,可以自己手动设置或者选择 date.setDate() var num = date.getDate(); console.log(num) //输出每个月的天数 var time ...

  5. 题解 LG P2264

    这是题解P2264 先讲一下Trie,其实Trie也名前缀树,就是说:如果Trie中某串是某串的前缀,那么我们可以共用这个串也就是这样: 插入h.hk.jc,jcfa 那么,h节点会给h和hk共用,j ...

  6. java 解析json格式数据(转)

    2012-07-30 16:43:54|  分类: java |  标签:java  json  |举报|字号 订阅     有时候我们可能会用到json格式的数据进行数据的传输,那么我们怎么把接收到 ...

  7. 三 Hibernate持久化状态&主键生成策略

    持久化类 持久化:将内存中的一个对象持久化到数据库中的过程 持久化类:Java类+映射文件.Java中一个类与数据库的表建立了映射关系,那么这个类称为持久化类. 持久化类的编写规则: 对持久化类提供一 ...

  8. 深入理解 C# 协变和逆变 (转载)

      深入理解 C# 协变和逆变 msdn 解释如下: “协变”是指能够使用与原始指定的派生类型相比,派生程度更大的类型. “逆变”则是指能够使用派生程度更小的类型. 解释的很正确,大致就是这样,不过不 ...

  9. 深入了解memcached

    一.memcached如何支持高并发 Memcached使用多路复用 I/O模型(如epoll.select等).传统阻塞 I/O中,系统可能会因为某个用户连接还没做好 I/O准备而一直等待,直到这个 ...

  10. 如果shell到win上出现乱码怎么办

    如果shell到win上出现这样的乱码怎么办? 如果出现了乱码,不慌一条命令搞定它!!! chcp 65001