C++ 学习

简单程序设计

1
2
3
4
5
6
7
8
#include <iostream>
using namespace std; // 针对命令空间的指令
int main() {
// insert code here...
cout << "Hello, World!\n"<<endl; //endl 表示换行符
cout << "Welcome to C++ 1"<<endl;
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// if 实现选择语句
#include <iostream>
using namespace std;
int main() {
int year;
bool isLeapYear;

cout<<"Enter the year: ";
cin>>year; //将提取符作用在流类对象cin上,键盘输入
isLeapYear = ((year%4 == 0 && year%100!=0) || (year%400 == 0));

if (isLeapYear)
cout<<year<<" is a leap year"<<endl; // endl 插入换行符,并刷新流
else
cout<<year<<" is not a leap year"<<endl;

return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 嵌套的if语句

#include <iostream>
using namespace std;

int main() {
int x, y;
cout<<"Enter x and y:";
cin>>x>>y;

if (x!=y)
if (x>y)
cout<<"x>y"<<endl;
else
cout<<"x<y"<<endl;
else
cout<<"x=y"<<endl;
return 0;
}