#include 预处理指令
#include 预处理指令
include 预处理指令 引入iostream =输入输出流
自己引入的需要使用”“最后加,h
系统的使用<>
第三题:
// 19-编程练习3.cpp: 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <array>
using namespace std;
int main()
{
array <float, 4> s = {};
cout << "请输入你的第一次成绩:";
cout << endl;
cin >> s[0];
cout << "请输入你的第二次成绩:";
cout << endl;
cin >> s[1];
cout << "请输入你的第三次成绩:";
cout << endl;
cin >> s[2];
s[3] = (s[0] + s[1] + s[2]) / 3;
cout << "你的平均成绩是:" << s[3] << endl;
return 0;
}
01
-在解决方案下面创建项目
注释用//废除代码 一般用于程序的解释 //是单行注释 只对本行有用 /*/多行注释 对程序运行没有影响
快捷键注释 全选ctrl+k 松开在按ctrl+C ctrl+K ctrl+u取消注释
语句分号结束
include
int函数返回语句
大括号是函数体
main程序主要函数 入口函数
\n”下一行
匿名空间可以放在外部和内部 using namespace cout 是输出加上<<和‘’:
endl:换行的意思
不能加''
/n可以放在字符串
ctrl+k 加ctrl+f 格式化 Tab缩进
void不需要返回值
练!!!
1.输入身高(米),转换成厘米,并打印出来
2.输入一个四位数,并打印出来
#include <string>;
#include <iostream>;
#include d"stdafx.h";
using namesoace std;
int main()
{
stringr str1="nihao";
string str2="hello";
getline(cin,str1);
cout<<str1;
returrn 0;
string s=str1+str2;
s+=str1;
cout<<s<<endl;
cout<<s.size()<<
}
枚举类型关键字 enum herotype{
tank,0
adc,1
assidt 2
};
int main(){
heroType heroType=tank;
hero
}
#include <iostream>
using namespace std;
int main()
{
// || 或
/*int vip = 1;
if(vip==1 || vip==2)
{
cout<<"获得优惠";
}*/
//bool res1 = true || false; // 1
//boo res2 = false || true; // 1
//bool res3 = false || false; // 0
//bool res4 = true || true; // 1
//cout<<res1<<" "<<res2<<" "<<res3<<" "<<res4<<endl;
//bool res1 = 4==4 || 4==9; // 1
//bool res2 = 9>0|| 3>4; // 1
//bool res3 = 2<8 || 3<9; // 1
//bool res4 = 5<3 || 9<7; // 0
//cout<<res1<<" "<<res2<<" "<<res3<<" "<<res4<<endl;
// && 与
//int age = 20;
//if(age>=18 && age<=30)
//{
//cout<<"你是青年"<<endl;
//}
//bool res1 = 4==4 && 3==3; // 1
//bool res2 = 8==8 && 6==4; // 0
//bool res3 = 5>4 && 3>9; // 0
//bool res4 = 4>8 && 5<10; // 0
//bool res5 = 3<9 && 2>9; // 0
//cout<<res1<<" "<<res2<<" "<<res3<<" "<<res4<<" "<<res5<<endl;
// !
//cout<<!true<<endl; // 0
//cout<<!(10>9)<<endl; // 0
// && and || or ! not
return 0;
}
// 条件运算符 问号 冒号
// ? :
// 表达式1 ?表达式2:表达式3
//if(表达式1)
//{
//表达式2
//}
//else
//{
//表达式3
//}
//int i = true ? 100 : 90;
//cout<< i<<endl; // 100
//int vip =1;
// 使用string,需要引入#include <string>
//string res = vip==1 ? "有优惠" : " 正常价格";
int vip = 1;
vip == 1 ? cout<<"有优惠" : cout<<:"正常价格";
#include <iostream>
using namespace std;
enum HeroType
{
Tank = 1,
Magic = 4,
ADC = 7,
Assist
};
int main()
{
//switch(integer-expression) // 整数表达式
//{
//case label1:
//statement1;
//break;
//case label2:
//statement2;
//break;
//default:
//code
//break;
//}
/* HeroType type = Tank;
switch(type)
{
case Magic :
cout<<"使用魔法师技能"<<endl;
cout<<"创建魔法师模型"<<endl;
break;
case Tank:
cout<<"使用坦克技能"<<endl;
cout<<"创建坦克模型"<<endl;
break;
case ADC:
cout<<"使用射手技能"<<endl;
cout<<"创建射手模型"<<endl;
break;
case Assist:
cout<<"使用辅助技能"<<endl;
cout<<"创建辅助模型"<<endl;
break;
default:
cout<<"没有找到对应的英雄类型"<<endl;
break;
} */
int vip = 4;
switch(vip)
{
case 0:
cout<<"没有礼物"<<endl;
break;
case 1:
cout<<"赠送10个钻石"<<endl;
break;
case 2:
cout<<"赠送30个钻石"<<endl;
break;
case 3:
cout<<"赠送50个钻石"<<endl;
break;
case 4:
cout<<"赠送100个钻石"<<endl;
break;
default :
break;
}
}
结果:
使用坦克技能
创建坦克模型
赠送100个钻石
int c=(int)23.8 吧23.8强制转换为23 吧23强制赋值给了c
int vip = 2;
switch(vip)
{
case 0:
cout<<"没有礼物"<<endl;
break;
case 1:
cout<<"赠送10个钻石"<<endl;
break;
case 2:
cout<<"赠送30个钻石"<<endl;
case 3:
cout<<"赠送50个钻石"<<endl;
break;
case 4:
cout<<"赠送100个钻石"<<endl;
break;
default :
break;
}
结果:
赠送30个钻石
赠送50个钻石
for(int i=1;i<10;i++)
{
if(i%3==0)
{
continue;
}
cout << i <<endl;
// 1
// 2
// 4
// 5
// 7
// 8
}
auto 自动确定类型
auto 缺点