前言
本章要介绍的内容主要有
C++变量的命名规则
各数据类型介绍
const限定符创建的符号常量
类型转换
一、简单变量
3.1.1变量名
C++提倡使用具有一定含义的变量名(一般为翻译),例如定义一个变量表示数目,一般会这样写
int numbers;
C++变量名的命名规则主要如下所示
在名称中只能使用字母字符、数字和下划线(_);
名称的第一个字符不能是数字;
区分大写和小写字母;
不能将C++关键字用作变量名;
C++对变量名的长度没有限制但是一般对于变量名不建议太长
3.1.2整型
这里不做过多介绍,整型的变量一般使用int来进行创建。
分为有符号整型和无符号整型
3.1.3整型short、int、long和long long
short至少16位
int至少与short一样长
long至少32位,且至少与int一样长
long long 至少64位,且至少与long一样长
计算机内存的基本单位是位(bit)。
字节(byte)通常指的是八位的内存单元。也就是说一般一个字节等于八位。
1KB=1024字节
1MB=1024KB
实际上short是short int的简称,而long则是long int的简称,所以我们现在不难理解为什么int最少哟占字节要比short长,long则至少比int长。
3.1.4整型字面值
整型字面值(常量)是显式地书写的常量。
常见的十进制,八进制,十六进制等表示法在C++中可以使用cout的特性显示出来
头文件iostream提供了控制符hex、dec、和oct。分别指示cout以十六进制、十进制、八进制显示整数
#include <iostream>
using namespace std;
int main()
{
int chest=42;
int waist=42;
int inseam=42;
cout <<"Monsieur cuts a strikingfiqure!" << endl;
cout <<"chest =" << chest << "(decimal for 42)" << endl;
cout << hex;
//manipulator for changing number base
cout <<"waist =" << waist << n(hexadecimal for 2)" << endl;cout << oct;
// manipulator for changing number base
cout << "inseam="<< inseam <<" (octal for 42)" << endl;
return 0;
}
3.1.5char类型:字符和小整数
char类型是专为存储字符(字母和数字)而设计的。他是另一种整型,它足够长,能够表示目标计算机系统中的所有基本符号。
此处介绍一个新的C++显示字符函数
cout.put()函数
类似于C中的putchar()函数,用于显示单个字符。
#include<iostream>
int main()
{
using namespace std;
//assign ASCII code for M to ch
char ch ='M';
int i= ch;
store same code in an int
cout << "The ASCII code for"<< ch <<nis"<<i << endl;
cout << "Add one to the character code:" << endl;
//change character code in ch
ch = ch +1;
// save new character code in i
i= ch;
cout <<"The ASCII code for " << ch <<"is << i<< endl;
// using the cout.put() member function to display a char
cout <<"Displaying char ch using cout.put(ch):";
cout.put(ch);
// using cout.put() to display a char constant
cout.put('!');
cout << endl <<"Done" << endl;
return 0;
}
成员函数cout.put()
函数cout.put()是一个重要的C++OOP概念——成员函数——的第一个例子。
类定义了如何表示和控制数据。成员函数则归类所有描述了操纵类数据的方法。
句点(.)被称为成员运算符。cout.put()的意思是通过类对象cout来使用成员函数put()。
signed char和unsigned char
与int不同的是,char在默认情况下既不是没有符号,也不是有符号,是否有符号是由C++实现所决定的
wcha_t
程序需要处理的字符集可能无法用一个8位的字节表示。解决办法有二:
1编译器定义Char为16字节或以上的字节
2使用另一种类型wChar_t(宽字符类型)可以表示扩展字符集。wchar_t与underlying(底层类型)的长度和符号属性相同。
cin 和 cout 将输入和输出看作 char 流,因此不适于用来处理wChar_t类型,但可以使用类似的工具》》wcin 和 wcout
C++11 新增的类型: char16_t 和 char32_t
wchar_t 的长度和符号特征由实现决定,由此C++11 新增char16_t 和 char32_t,其中前后者都无符号。用u’C’、u"be good"指代char16_t ;用U’d’ 、U"very good"指代char32_t;
3.1.6Bool类型
来源于数学表示法。
C++将非零值表示为true,将零表示为false。
bool is_ready = true;
3.2. const限定符
符号名称指出了常量表示的内容。
const关键字来修改变量声明和初始化。常量被初始化后,其值就被固定,编译器将不允许再修改该常量的值。
将名称的首字母大写,以便提醒是个常量,可便于阅读性增强。如果是变量名全大写,则使用 #define 。
创建常量的通用格式为:
const type name = value;
const int Months = 12; // 一年有12个月
如果在声明const常量时没有提供值,该常量的值将是不确定,且无法更改。
const 比 #define好,原因如下:
1、它能够明确指定类型
2、可以使用C++的作用域规则将定义限制在特定的函数或文件中
3、const可用在更复杂的类型中
3.3. 浮点数
浮点数就是表示小数点部分的数字。计算机将值分为两部分存储,一部分表示值,另一部分则用于对值进行放大或缩小。
3.3.1 为什么叫浮点数?
因为小数点可移动,所以称之为“浮点数”。
表示方法:
第一种 使用常用的标准小数点表示法
3.14159
10.0
1
2
第二种 表示浮点值的方法叫做E表示法(科学计数法)。
例如:
3.45E6 3.45与10的6次方相乘,E6代表的是10的6次方。
C++算术运算符
加(+)、减(-)、乘(*)、除(/)、求模(%)
这里的算术运算符都是双目运算符。
这里主要介绍/和%运算符
1、除法运算符
除法运算符(/)的行为取决于操作数的类型。
如果两个操作数都是整数,则C++将执行整数除法。把结果的小数部分丢弃,使最后的一个结果是一个整数。
如果其中有一个(或两个)操作数是浮点数,则小数部分将保留,结果为浮点数。
2、求模运算符也叫做取余运算符
他的作用是返回余数。
类型转换
C++中有两种类型转换规则:自动类型转换和强制类型转换。
自动类型转换的规则
- 将一种算术类型的值赋给另一种算术类型的变量时,C++对值转换为接收变量的类型。
- 表达式中包含不同的类型时,C++对值进行转换
- 将参数传递给函数时,由函数原型控制,C++对值进行转换
除了C++的自动类型转换之外,还允许通过强制类型转换机制显式的进行类型转换
C++的强制类型转换格式如下
(typename) value //来源于C语言
typename (value) // C++eg:
(int) 3.5
(double) 5.2
C++11中的auto声明
C++11中新增了一个工具,让编译器能够根据初始值的类型去推断变量的类型。——》auto关键字
auto n=100;
auto x=1.5;
auto y=1.3e12L;
二、编程练习
1.
#include <iostream>
using namespace std;
const double rate = 0.083;
int main()
{
double height;
cout << "please input your height in inch:____\b\b\b\b";
cin >> height;
cout << "your height in foot is:" << height * rate << endl;
return 0;
}
2、
#include <iostream>
using namespace std;
const double f_times_inch = 12.0;
const double i_times_meter = 0.0254;
const double k_times_pound = 2.2;
int main()
{
double feet, inch, pound,m,kg;
cout << "请输入您的身高:以几英尺几英寸输入" << endl;
cout << "英尺:";
cin >> feet;
cout << "英寸:";
cin >> inch;
cout << "您的身高是:" << feet << "英尺" << inch <<"英寸" << endl;
cout << "请输入您的体重:以磅为单位输入" << endl;
cout << "体重:";
cin >> pound;
cout << "您的体重是:" << pound << "磅" << endl;
m = (feet * f_times_inch + inch) * i_times_meter;
kg = pound / k_times_pound;
cout << "您的BMI是:" << (kg / (m * m)) << endl;
return 0;
}
3.
#include <iostream>
using namespace std;
int main()
{
double degree, minute, second;
double degrees;
cout << "Enter a latitude in degrees, minutes,and seconds:" << endl;
cout << "First , enter the degree: ";
cin >> degree;
cout << "Next, enter the minutes of arc: ";
cin >> minute;
cout << "Finally, enter the seconds of arc:";
cin >> second;
degrees = degree +(double)(minute / 60) +(double)(second / 3600);
cout << degree << " degrees, " << minute << " minutes " << second << " seconds =" << degrees << " degrees" << endl;
return 0;
}
4.
#include <iostream>
using namespace std;
const int d_times_hour = 24;
const int times = 60;
int main()
{
long long second;
cout << "输入一个秒数:";
cin >> second;
cout << second << "秒 = ";
int day = second / (times * times * d_times_hour);
second -= day * (times * times * d_times_hour);
int hour = second / (times * times);
second -= hour * (times * times);
int minute = second / times;
second -= minute * times;
cout << day << "天" << hour << "小时" << minute << "分钟" << second << "秒" << endl;
return 0;
}
5.
#include <iostream>
using namespace std;
int main()
{
long long p_country;
long long p_earth;
cout << "Enter the world's population :";
cin >> p_earth;
cout << "Enter the population of the US:";
cin >> p_country;
double times = (double)p_country / (double)p_earth;
cout <<"The population of the US is "<<times * 100 << "% of the world population" << endl;
return 0;
}
6.
#include <iostream>
using namespace std;
int main()
{
double mile; //英里
double gallon; //加仑
cout << "输入英里数:";
cin >> mile;
cout << "输入加仑数:";
cin >> gallon;
cout << "平均1加仑能行驶" << mile / gallon << "英里" << endl;
return 0;
}
7.
#include <iostream>
using namespace std;
int main()
{
double l;
cout << "输入100公里消耗的汽油(单位:升)";
cin >> l;
double mile = 62.14;
double gallon = l / 3.875;
cout << mile / gallon << endl;
return 0;
}
总结
C++的基本类型分为两组:一组由存储为整数的值组成,另一组由存储为浮点格式的值组成。整型之间通过存储值时使用的内存量及有无符号来区分,整型从最小到最大依次是:bool、char、signed char、unsigned char、short、unsigned short、int、unsigned int 、long、unsigned long,等等。