黑马程序员匠心之作|C++教程从0到1入门编程
关于P1~P15练习
1、基本知识:helloworld,注释,变量,常量,关键字,标识符,int,sizeof(),float,char,string,bool,cin,cout
2、防止显示结果太快用:system(“pause”)
3、c++中涉及到string ,最前面必须#include< string >
4、多行注释ctrl+k,ctrl+c;取消注释ctrl+k,ctrl+u
5、一般的固定格式
#include <iostream>
using namespace std;
int main()
{
system("pause"); //防止显示结果太快
return 0;
}
6、练习Demo
#include <iostream>
using namespace std;
#include<string>//用c++字符串,需要加头文件
//注释
//1、单行注释
//2、多行注释
/*
main主函数
有且仅有一个
*/
//常量的定义方式
//1、#define宏常量
//2、const修饰的常量
//#define Day 7
//标识符命名规则
//1、不可以关键字
//2、由字符,数字,下划线组成
//3、第一个字符和下划线
//4、区分大小写
//建议,见名知意
int main()
{
//输入数据
//1、整型
//int a = 0;
//cout << "a为:" << a << endl;
//cout << "请对整型a赋值" << endl;
//cin >> a;
//cout << "a为:" << a << endl;
//2、浮点型
//float f = 3.14f;
//cout << "f为:" << f << endl;
//cout << "请对浮点型f赋值" << endl;
//cin >> f;
// << "f为:" << f << endl;
//3、字符型
/*char c = 'a';
cout << "c为:" << c << endl;
cout << "请对字符型c赋值" << endl;
cin >> c;
cout << "c为:" << c << endl;*///注释ctrl+k,ctrl+c;取消注释ctrl+k,ctrl+u
//4、字符串型
/*string str = "abc";
cout << "str为:" << str << endl;
cout << "请对字符串str赋值" << endl;
cin >> str;
cout << "str为:" << str << endl;*/ //头文件不要忘
//5、布尔型
//bool flag = false;
//cout << "flag为:" << flag << endl;
//cout << "请对布尔型flag赋值" << endl;
//cin >> flag;
//cout << "flag为:" << flag << endl; //只要非零皆为真,1010也为1
//bool类型
//创建,1为真,0为假
//bool flag = true;
//cout << flag << endl;
//bool flag1 = false;
//cout << flag1 << endl;
//字节
//cout << sizeof(flag) << endl;
//c风格
//注意1、char 字符串名[]
//注意2、等号后面加双引号
//char str[] = "hello,world";
//cout << str << endl;
//c++风格
//string s = "hello,world";
//cout << s << endl;//出错,前面必须加头文件:#include<string>
//转义字符
//换行\n
//cout << "hello,world\nfengli" << endl;
//反斜杠 \\
//cout << "hello,world\\fengli" << endl;
//水平制表\t,整齐的输出数据
//cout << "aaaa\tfengli" << endl;//\t占了8个位置,前面4个a空四个
//cout << "aa\tfengli" << endl;
//cout << "aaaaaa\tfengli" << endl;
//1、创建
//char ch = 'a';
//cout << ch<< endl;
//2、内存,字符型占一个字节
//cout << "ch占内存" <<sizeof(ch)<< endl;
//3、常见错误
//char ch2 = "b";//错误,“”不可以
//char ch3 = 'abc';//错误,多个不可以
//4、不将字符存入,存入ASCII编码
//a-97
//A-65
//cout << (int)ch << endl;
//单精度 flot
//float f1 = 3.1415926f;//不加f默认为double,然后转换成单精度。输出一个小数,默认显示6位数
//cout << "f1=" << f1 << endl;//输出结果为:3.14159
//double f2 = 3.14;
//统计占空间
//cout << "f1占空间=" << sizeof(f1) << endl;
//cout << "f2占空间=" << sizeof(f2) << endl;
//float f3 = 3e2;//3*10^2
//cout << "f3=" << f3 << endl;
//float f4 = 3e-2;//3*0.1^2
//cout << "f4=" << f4 << endl;
//short num1 = 10;//-32768~32767,2字节
//cout << "short占用内存空间为=" << sizeof(num1) << endl;
//int num2 = 10;//4
//cout << "int占用内存空间为=" << sizeof(int) << endl;
//long num3 = 10;//4
//cout << "long占用内存空间为=" << sizeof(long) << endl;
//long long num4 = 10;//8
//cout << "long long占用内存空间为=" << sizeof(long long) << endl;
//整型大小整理:short < int <= long <= long long
//不可以关键字
//int int = 10;
//字符,数字,下划线
//int abc = 10;
//int _abc = 10;
//int _123abc = 10;
//第一个字符和下划线
//int 123abc = 10;//错误
//区分大小写
//int aaa = 10;
//cout << AAA << endl;//错误
//cout << "一周有" << Day << "天" << endl;
//const int month = 12;
//month = 13; //错误,const修饰的变量也成为常量
//cout << "一年有" << month << "月" << endl;
//在屏幕中输出hello world
//cout << "Hello,World!I am" << 18 << "Today!" << endl;
//变量创建语法:数据类型 变量名 = 变量初始值
//int a = 10;
//cout << "a = " << a << endl;
system("pause");//防止显示结果太快
return 0;
}