The second C++ check-in, I gradually became proficient

Tip: After the article is written, the table of contents can be automatically generated. How to generate it can refer to the help document on the right


foreword

The last time our learning content was the standard input and output
Summary:
[The first check-in is here ( https://blog.csdn.net/m0_53590279/article/details/115217187 )


1. Conversion of hexadecimal system

Hexadecimal is the basis of computer language, we must master it!
As we all know: binary is bin - octal is ocx - decimal is dec - hexadecimal is hex

    我们可以利用      cout<<ocx
           ——————————cout<<dec
           ——————————cout<<hex      来控制数据的输入输出
#include<iostream>
using namespace std;
main()
{
    
    
	//dec是十进制,OCT是八进制,HEX是十六进制
	int a=010,b=10,c=0X10;
	cout<<"DEC:";//默认十进制
	cout<<"a="<<a<<" ";
	cout<<"b="<<b<<" ";
	cout<<"c="<<c<<" \n";
	
	cout<<"OCT";
	cout<<oct; //调成八进制显示数据
	cout<<"a="<<a<<" ";
	cout<<"b="<<b<<" ";
	cout<<"c="<<c<<" \n";
	
	cout<<"HEX";
	cout<<hex;//调成十六进制 显示数据
	cout<<"a="<<a<<" ";
	cout<<"b="<<b<<" ";
	cout<<"c="<<c<<" \n";
	
    cout<<"a+b+c= ";//三种进制混合运算
    cout<<dec;//恢复十进制(不会主动恢复的)
    cout<<a+b+c<<" \n";
   
    cout<<"DEC:a=";cin>>a;
    cout<<"OCT:b=";cin>>b;
    cout<<"HEX(零X):c=";cin>>c;
    cout<<"DEC:"<<dec<<"\n";//进制声明不用单独一行
	//转换为十进制输出
    cout<<"a="<<a<<"\n";
    cout<<"b="<<b<<"\n";
    cout<<"c="<<c<<"\n";
		 }

2. Input and output of floating-point and character variables

1. Floating point

The character type is declared as: float and double

name ------ float : single-precision floating-point number –||- double : double-precision floating-
point number bytes -------------- 4 --------- -----||-------- 8 -------------
Accuracy --------------- 6~7 -- -----------||----- 15~16 ----------

The code was eaten by me (QAQ / hiccup~)

2. Character type

The character type is declared as * char
character data can perform arithmetic/logic operations, and
cin can be used to assign values ​​to character variables. (Adding >> will report an error?!)

The code is as follows (example):

#include<iostream>
using namespace std;
main()//标题:字符型数据处理 
{
    
    
	char c1='A',c2;//格式化 
	
	c2=c1+32;//字符可以直接运算 
	
	cout<<"c1="<<c1<<endl;
	cout<<"c2="<<c2<<endl;
	
	cout<<c1<<':'<<int(c1)<<endl;//转换为ASCll 
	cout<<c2<<':'<<int(c2)<<endl;
	cout<<'$'<<':'<<int('$')<<endl;
	
	cout<<"c1 c2"<<endl;//字符的输入与输出 
	cin>>c1>>c2;//cin后加endl会报错 
	cout<<"c1="<<c1<<"  c2="<<c2<<endl;
	 
}

three. const variable (DEFINE in C language?)

It can only be assigned before the program is executed, and the value of the const variable can
only be used when the program is executed . It is somewhat similar to the "DEFINE" of the C language.

#include<iostream>//标题:const变量使用 
using namespace std;
const double PI=3.1415926;
/*只能在程序执行前赋值,程序执行时只能使用
不可修改const变量的值*/ 
//类似于C语言的"DEFINE" 
main() 
{
    
    
double r,l,s;
cout<<"r=";
cin>>r;

l=2*PI*r;
s=PI*r*r;

cout<<"l="<<l<<endl;
cout<<"s="<<s<<endl;	
 } 
 

Four. Enum variables ("classes" in C?)

I accidentally wrote all the knowledge points into the code QAQ

#include<iostream>
using namespace std;
main()
{
    
    
	//枚举变量像C语言里的 类 
	//定义枚举变量 
	enum color{
    
    
		RED=3,
		YELLOW=6,
		BLUE=9
	};
	
	//声明枚举变量的两种方式: 
	enum color a=RED;
	/*此时的color就像是一种类型(int,char.etc) */
	color b;
	//C语言中只支持第一种 
	
	cout<<"RED="<<RED<<endl; 
	cout<<"YELLOW="<<YELLOW<<endl; 
	cout<<"BLUE="<<BLUE<<endl; 
	
	b=a;
	a=BLUE;
	cout<<endl<<"a=BLUE,b="<<a<<endl;
	cout<<"b=RED,b="<<b<<endl;
	//可以正常的对于新增变量赋值 
	//一个问题,新增到类里面的变量是存储在'color'中的吗? 
	
	
	/*BLUE=66;报错原因 
	赋值语句的左边应该是变量,不能是表达式。
	而实际上 枚举变量中一斤存储的内容是表达式,
	不可修改*/ 
	
	b=BLUE;
	cout<<endl<<"b=BLUE;"<<endl;
	cout<<"a-b="<<a-b<<endl;
	
	/*cin>>RED;报错原因
	还是不可修改的表达式(耸肩)*/ 
}

<font color=#999AAA

This is the end of today's check-in, I accidentally sat in front of the computer for more than two hours! The spring is beautiful outside, I suggest you go out for an outing. Thanks for watching. Next chapter **(Basic Operation Types(1))**. stay tuned

Guess you like

Origin blog.csdn.net/m0_53590279/article/details/115280712