一、基础知识
# include <iostream>
# include <string> // 使用C++风格字符串时候,要包含这个头文件
# include <math.h>
using namespace std;
//1、 单行注释
/*2、多行注释*/
// 常量的定义方式
//1、 #define 宏常量
#define Day 7
//2、 const修饰的变量
const int month = 12;
// 标识符命名规则
// 标识符不能是关键字
// 以字母、数字、下划线
// 开头可以为字母、下划线,不能是数字
// 区分大小写
// 数据类型
// 整数
// short 短整型 2 字节 (-32768~32767)
// int 整形 4字节
// long 长整型 windows 为4字节,linux为4字节(32) 8字节(64)
// long long 长长整型 8字节
// sizeof( )求出数据类型占用内存大小
// 实型(浮点数)
//1、单精度float4字节 7位有效数字
//2、双精度double8字节 15~16有效数字
// 字符型 字符型变量显示单个字符
// char ch = 'a' 字符型变量只占用一个字节
// 在显示字符型变量时,用单引号将字符括起来,不要用双字符
// 单字符内只能有一个字符,不可以是字符串
// 字符串变量并不是把字符本身放到内存中存储。而是将其ASCII存储
// 转义字符 用于表示一些不能显示出来的ASCII字符
// 常用的转移字符:\n 换行,\\反斜杠,\t 水平制表符占八个位 整齐输出数据
// 字符串型
// C 风格字符串 char 变量名[] = "字符串值"
// C++ 风格字符串 string 变量名 = "字符串值"
// 布尔类型bool 布尔数据类型代表真或假的值 占用1个字节
// true ~ 1 为真
// false ~ 0 为假
// 数据的输入 从键盘获取数据
// cin >> a
//运算符
//算术运算符 + - * / 加减乘除
//% 取余, ++a前置递增,先变量加一,后表达运算, a++后置递增先运算,后加一
//赋值运算符 =, +=, -+, *=, /=, %=
//比较运算符 == != < > <= >= 符合输出1,不符合输出0
//逻辑运算符 !非 &&与 ||或
// 程序流程结构
// 顺序结构
// 选择结构
// 单行格式if if(条件){执行满足条件的语句}
// 多行格式if if(条件){执行满足条件的语句}else{条件不满足执行的语句}
// 多条件if if(条件1){执行满足条件1的语句}else if(条件2){条件2不满足的语句}else{条件1、2都不满足执行语句}
// 嵌套if语句 if(条件1){if(条件2){执行满足条件12的语句}else{满足条件1 但不满足条件2执行语句}}else{条件1不满足执行的语句}
// 三目运算符 表达式1?表达式2:表达式3 如果1为真执行2返回2的结果,如果1为假执行3返回3的结果
// switch语句 执行多条件分支语句
/* switch(表达式) switch 缺点 判断时候只能是整型或者字符型,不可以是一个区间 不写break
优点 结构清晰,执行效率高
{
case 结果1:执行语句;break;
case 结果2:执行语句;break;
case 结果3:执行语句;break;
default:执行语句;break;
}
*/
// 循环结构
// while(循环条件){循环语句}
// do while 循环语句
//do{循环语句}while(循环条件);do while 先执行一次循环,再判断循环条件
//满足循环条件,执行循环语句 for(起始表达式;条件表达式;末尾循环体){循环语句}
//嵌套循环,再循环体中再嵌套一层循环,解决一些实际问题
//break 语句 用于跳出选择结构或者循环结构
/*
出现再switch语句中,作用是终止case并跳出switch
出现再循环语句中, 跳出当前循环
出现在嵌循环中,跳出最近的内层循环语句
不可用于三目运算符中,因其要求的是表达式返回的是结果
*/
//continue 语句 在循环语句中,跳出本次循环中余下尚未执行的语句,继续执行下一次循环
/* 奇数输出
for(int i=0;i<10;i++){
if(i%2==0){
continue;
}
cout<<i<<endl;
}
*/
// goto语句 可以无条件跳转到具体位置
/*
cout<<"1* * *"<<endl;
cout<<"2* * *"<<endl;
goto FLAG;
cout<<"3* * *"<<endl;
cout<<"4* * *"<<endl;
FLAG:
cout<<"5* * *"<<endl;
*/
int main()
{
//输出hello world
cout<<"hello world"<< endl;
cout<<"一周总共天数:"<< Day << "天"<< endl;
cout<<"一年总共月数:"<< month << "月"<< endl;
short num1 = 10;
int num2 = 10;
long num3 = 10;
long long num4 =10;
cout<<num1 <<" short占用内存大小为:"<<sizeof(num1)<< endl;
cout<<num2 <<" int占用内存大小为:"<<sizeof(num2)<< endl;
cout<<num3 <<" long占用内存大小为:"<<sizeof(num3)<< endl;
cout<<num4 <<" long long占用内存大小为:"<<sizeof(num4)<< endl;
// 默认显示输出只显示6位有效数字
float f1 = 3.14f;
cout <<"f1= "<< f1 <<"内存占用大小为"<< sizeof(f1)<<endl;
double d1 =3.141926;
cout<<"d1= "<< d1 <<"内存占用大小为"<< sizeof(d1)<<endl;
//字符型
//97 - a
//65 - A
char ch = 'a';
cout << ch << endl;
cout<<"char字符串变量所占内存:"<<sizeof(char)<<endl;
cout << (int)ch << endl;
// 转义字符
cout<<"hello world\n";
cout<<"\\"<<endl;
cout<<"aaa\thelloworld"<<endl;
cout<<"aa\thelloworld"<<endl;
cout<<"aaaa\thelloworld"<<endl;
//字符串
char str[] = "hello world";
cout << str << endl;
string str2 = "hello world";
cout << str2 << endl;
// bool数据类型
bool flag = true;//表示为真
cout<<flag<<"bool类型所占内存空间 "<<sizeof(bool)<<endl;
// 输入数据
int a = 0;
cout <<"请给整型变量a赋值:"<< endl;
//cin >> a;
cout<<"整型变量a = "<< a <<endl;
// 算术运算符
int a1 = 10 ;
int b1 = 20 ;
cout << a1 + b1 << endl;
cout << a1 - b1 << endl;
cout << a1 * b1 << endl;
cout << a1 / b1 << endl; // 两个整数相除为整数
cout << a1 % b1 << endl;
// 前置和后置的区别
int a2 = 10;
int b2 = ++a2 * 10;
cout <<a2<<"\t"<<b2<<endl;
a2 = 10;
int b3 = a2++ * 10;
cout <<a2<<"\t"<<b3<<endl;
// 赋值运算
int b4 = 10;
b4 =100;
cout<<"b4= "<<b4<<endl;
cout<<"b4+=20 "<<(b4+=20)<<endl;
cout<<"b4-=20 "<<(b4-=20)<<endl;
cout<<"b4*=20 "<<(b4*=20)<<endl;
cout<<"b4/=20 "<<(b4/=20)<<endl;
cout<<"b4%=20 "<<(b4%=20)<<endl;
// 比较运算符
int e = 1;
int u = 8;
cout<<(e == u) <<endl;
cout<<(e != u) <<endl;
cout<<(e > u) <<endl;
cout<<(e < u) <<endl;
cout<<(e >= u) <<endl;
cout<<(e <= u) <<endl;
// 逻辑运算符
bool y = true;
bool t = true;
cout<<!y<<endl;
cout<<(!y&&t)<<endl;
cout<<(!y||t)<<endl;
// 选择循环
int score = 0;
cout<<"请输入你的分数"<<endl;
cin >> score;
if (score > 600) //if 后面无需加分号
{
cout<<"恭喜你考入一本大学"<<endl;
if(score > 700){
cout<<"恭喜你考入清华北京大学"<<endl;
}
else if (score > 650){
cout<<"恭喜你考入人民大学"<<endl;
}
}
else if (score > 500){
cout<<"恭喜你考入二本大学"<<endl;
}
else if (score > 400){
cout<<"恭喜你考入三本大学"<<endl;
}
else{
cout<<"未考上本科大学"<<endl;
}
//三目运算符
int a4 = 10;
int a5 = 20;
int a6 = 0;
a6 = a4>a5?a4:a5; // 在C++中三目运算符返回的是变量,可以继续赋值
cout<<"a6="<<a6<<endl;
//switch语句
int dscore;
cout<<"请输入你对这电影的评价"<<endl;
cin>>dscore;
switch(dscore)
{
case 10:cout<<"你的评价为电影经典"<<endl;break;
case 9:cout<<"你的评价为电影经典"<<endl;break;
case 8:cout<<"你的评价为电影不错"<<endl;break;
case 7:cout<<"你的评价为电影不错"<<endl;break;
case 6:cout<<"你的评价为电影一般"<<endl;break;
case 5:cout<<"你的评价为电影一般"<<endl;break;
default :cout<<"你的评价为电影烂片"<<endl;break;
}
// while循环
int num = -1;
while(9>num++)
{
cout<<"开始打印数字\n"<<num<<endl;
}
//do while 水仙花数 3位数,每位数的立方和等于数的本身。
int num100 = 100;
do{
if (pow(num100%10,3)+pow(num100/10%10,3)+pow(num100/100,3)==num100)
{
cout<<"水仙花数"<<num100<<endl;
}
num100++;
} while(num100<1000);
// for循环
for(int i=0;i<10;i++)
{
cout<<"for循环输出"<<endl;
cout<<i<<endl;
}
// 嵌套循环打印10*10的**
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
cout<<"* ";
}
cout<<endl;
}
// break语句
// 出现在switch 语句中
cout<<"请选择游戏难度"<<endl;
cout<<"1 普通难度"<<endl;
cout<<"2 中等难度"<<endl;
cout<<"3 困难难度"<<endl;
int select;
cin>>select;
switch(select){
case 1:
cout<<"您选择的是普通难度"<<endl; break;
case 2:
cout<<"您选择的是中等难度"<<endl; break;
case 3:
cout<<"您选择的是困难难度"<<endl; break;
default:
cout<<"您输入有误"<<endl;
}
// 出现在循环语句中
for(int i = 0;i<10;i++){
if(i==5){
break;
}
cout<<i<<endl;
}
// 出现在嵌套语句中
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
if(j == 5){
break;
}
cout<<"* ";
}
cout<<endl;
}
// continue 输出奇数
for(int i=0;i<10;i++){
if(i%2==0){
continue;
}
cout<<i<<endl;
}
// goto
cout<<"1* * *"<<endl;
cout<<"2* * *"<<endl;
goto FLAG;
cout<<"3* * *"<<endl;
cout<<"4* * *"<<endl;
FLAG:
cout<<"5* * *"<<endl;
system("pause");
return 0;
}
二、案例
1、三只小猪称体重
找出最终体重的小猪
#include<iostream>
using namespace std;
// 有三只小猪 ABC
// 分别输入三只小猪的体重,并判断哪只小猪最重
int main(){
float A,B,C;
cout<<"请输入A B C小猪的体重"<< endl;
cin >> A;
cin >> B;
cin >> C;
if (A>=B){
if(A>B){
if(A>C){
cout<<"A小猪最重"<<endl;
}
else{
cout<<"A C小猪最重"<<endl;}
}
else{
if(A>C){
cout<<"A B小猪最重"<<endl;
}
else{
cout<<"A B C小猪一样重"<<endl;
}
}
}
else{
if(B>=C){
if(B>C){
cout<<"B小猪最重"<<endl;
}
else{
cout<<"B C小猪最重"<<endl;
}
}
else{
cout<<"C小猪最重"<<endl;
}
}
system("pause");
return 0;
}
2、敲桌子
敲桌子 从1数到100.如果数字个位含有7,或者数字十位含有7,或者数字是7的倍数,打印敲桌子,其余数字直接打印输出 。
3、猜数字
猜数字0-100数字。
// 敲桌子 从1数到100.如果数字个位含有7,或者数字十位含有7,
//或者数字是7的倍数,打印敲桌子,其余数字直接打印输出
# include <iostream>
# include <string> // 使用C++风格字符串时候,要包含这个头文件
# include <math.h>
using namespace std;
int main(){
for(int i=1;i<101;i++){
(i%10==7||i/10==7||i%7==0)?cout<<"敲桌子"<<endl:cout<<i<<endl;
/*if(i%10==7||i/10==7||i%7==0){
cout<<"敲桌子"<<endl;
}
else{
cout<<i<<endl;
}*/
}
}
4、乘法口诀表打印
# include <iostream>
# include <string> // 使用C++风格字符串时候,要包含这个头文件
# include <math.h>
using namespace std;
//利用嵌套循环输出乘法口诀表
int main(){
for(int i=1;i<10;i++){
for(int j=1;j<10;j++){
if(j<=i){
cout<<j<<"*"<<i<<"="<<j*i<<" \t";
}
else{
cout<<endl;
break;
}
}
}
}