目录
要有C语言的基础,然后从简单到复杂一点一点的看,理解
1、头文件里的输出
#include<iostream>
using std::cout;
using std::endl;
int main() {
cout << "hello" << endl;
return 0;
}
通过#include < iostream >的语句引入了一个名为iostream( input output stream,输入输出流)的头文件
而cout则是一个在这个头文件中被定义好的对象,其中包含了许多有用的输入输出相关功能。
对于cout对象而言,输出操作由操作符<<来表达,其作用是将紧随其后的双引号中的字符串输出到标准输出设备(即显示器)上,以换行符end1结尾。
using表示使用命名空间std中的指定内容(这里使用的是cout和endl)
同样,如果想使用iostream中的某个对象,那么就必须要加上对应的语句,格式为using std:xx;
其余内容和C语言就差不多了,现在即可理解下面的小程序
#include<iostream>
using std::cout;
using std::endl;
int main(){
int number;
char alpha;
number = 1;
alpha = 'A';
cout << alpha << "is" << number << "st letter" << endl;
return 0;
}
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int a;
a = 1;
cout << a+19 <<endl;
return 0;
}
2、其他头文件
用到数学表达式需要加相关的头文件。C语言中引用math库时的文件名是math.h,虽然在C++语言中,依然可以这么写,但是更好的写法应该是cmath——所有继承的C库,在C++中都应该这么写,即以c某某某的名字引入。
#include <iostream>
#include <cmath>
using std::cout;
using std::endl;
int main() {
float a;
float n;
a = 2.0f;
n = 4.0f;
cout << pow(a,n);
return 0;
}
#include <iostream>
#include <cmath>
using std::cout;
using std::endl;
int main() {
double y;
y = sqrt((1-cos(0.5))/2);
cout << y <<endl;
return 0;
}
#include <iostream>
#include <cmath>
#define PI 3.14
using std::cout;
using std::endl;
int main() {
double radius;
radius = 12.0f;
cout << 4.0/3 * PI * pow(radius , 3) << endl;
return 0;
}
3、头文件里的输入
# include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
int number;
cin >> number;
cout << "computer says" << number << endl;
return 0;
}
cin对象连续读取多个变量时,默认以空格或者回车为分界
要使用流式输入输出对象,必须使用它们对应的命名空间
cin对象可以从键盘上连续读取多个输入
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
int first;
int second;
cin >> first >> second;
cout << first+second << endl;
return 0;
}
这段代码里,无论是两个输入的数据以空格隔开还是以换行的方式隔开,都可以运行
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
int A,B,C;
cin >> A >> B >> C;
cout << A+B+C <<endl;
return 0;
}
4、一个有取址符的程序
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int a;
int *p;
p = &a;
cout << p << endl;
return 0;
}
输出的既是a的地址
5、是非
#include <iostream>
using std::cout;
int main() {
int a;
int b;
a = 1;
b = 2;
cout << (a==b);
return 0;
}
6、条件
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
int x;
int y;
// 从用户处获得输入值 x
cout << "请输入 x 的值:" << endl;
cin >> x;
if(x>=0)
{
y = 1;
}
else
{
y = -1;
}
cout << "y = " << y << endl;
return 0;
}
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
int first;
int second;
char op;
cin >> first >> op >> second;
if(op == '+'){
cout << "=" << first + second << endl;
}else if(op == '-'){
cout << "=" << first - second << endl;
}else if(op == '*'){
cout << "=" << first * second << endl;
}else if(op == '/'){
cout << "=" << first / second << endl;
}else{
cout << "=" << first % second << endl;
}
return 0;
}
7、对于一个给定底和高的直角三角形,输出它的周长和面积。
#include <iostream> // 标准库
#include <iomanip> // fixed和setprecision所在的头文件
#include <cmath> // 数学库
using std::cin; // 获取键盘输入的信息
using std::cout; // 输出符
using std::endl; // 换行符
using std::setprecision; // 功能是控制输出流显示浮点数的有效数字个数
using std::fixed; // setprecision和fixed合用可以控制小数点后面有几位。
int main()
{
double d,h;
cin >> d >> h;
cout << setprecision(2) << fixed << d+h+sqrt(d*d+h*h) << endl; //输出周长,保留两位小数并四舍五入
cout << setprecision(2) << fixed <<(d*h/2) << endl; //输出面积,保留两位小数并四舍五入
return 0;
}
8、c++中的cin与c中的scanf
当你想处理2020-10-25这样的输入格式时
用cin是酱紫的:
int year,month,day;
char op;
cin >> year >> op >> month >> op >> day;
用scanf就很简单啦:
scanf("%d-%d-%d",&year,&month,&day);
那么
问题来了
c++可以用c的方式处理这个问题吗?
当然可以
酱紫做:
#include <iostream>
#include <cstdio> //加这个头文件
using std::cin;
using std::cout;
using std::endl;
int main()
{
int year,month,day;
scanf("%d-%d-%d",&year,&month,&day); //你瞅瞅是不是c的表达方式!
9、循环
#include<iostream>
using std::cout;
using std::endl;
int main() {
cout << "2" << endl;
int digit;
int divisor;
for (digit = 3; digit <= 15; digit += 2)
{
for(divisor = 3;divisor<digit;divisor +=2)
{
cout << digit << " mod " << divisor << endl;
}
}
return 0;
}
输出结果
2
5 mod 3
7 mod 3
7 mod 5
9 mod 3
9 mod 5
9 mod 7
11 mod 3
11 mod 5
11 mod 7
11 mod 9
13 mod 3
13 mod 5
13 mod 7
13 mod 9
13 mod 11
15 mod 3
15 mod 5
15 mod 7
15 mod 9
15 mod 11
15 mod 13
10、输出质数
#include<iostream>
using std::cout;
using std::endl;
int main() {
cout<<"2"<<endl;
int digit;
int divisor;
for (digit = 3; digit <= 15; digit += 2)
{
for (divisor = 3; divisor < digit; divisor += 2)
{
if(digit % divisor == 0)
{
break;
}
}
if(digit == divisor)
{
cout<<digit<<endl;
}
}
return 0;
}
11、判断质数
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
int N;
cin >> N;
if(N==2||N==3)
{
cout << "YES" << endl;
}
for (int a = 2; a <= N/2; a++)
{
if(N % 2 == 0 )
{
cout << "NO" << endl;
break;
}
if(a == N/2)
{
cout << "YES" << endl;
}
}
return 0;
}
break语句作用是完全不考虑循环执行条件,立即从当前循环中跳出,继续执行循环后面的语句
除了break之外,还有另外两个流程控制语句: continue与goto。
continue的用法与作用都与break相差无几——区别在于break是彻底结束整个单层循环(对于嵌套循环则跳出自身所在的层),而continue的作用则是结束本次循环——循环体中continue之后的语句将不会被执行,这一轮循环直接结束,而下一轮循环正常开始。
而goto的作用则是跳转到指定的某个语句——它的使用会严重影响程序的结构,对代码可读性和可维护性产生极大的破坏,因此建议大家尽量不要使用它。