文章目录
处理数据
简单变量
变量名
- 名称中使用字母、数字、下划线
- 名称第一个字符不能是数字
- 区分大小写
- 不能使用关键字为名称
- C++对名称的长度没有限制,有的平台会有限制
- 以两个下划线或下划线和大写字母大头的名称被保留给实现使用。
整型
任何语言只能表示所有整数的一个子集。
基本整形:char; short; int; long; long long。以及unsigned
- short至少16位
- int至少与short一样长
- long至少32位,且至少与int一样长
- long long至少64位,且至少和long一样长
#include<iostream>
#include<climits>
using namespace std;
int main(void)
{
int n_int = INT_MAX;
cout << "int is " << sizeof(int) << " bytes."<<endl;
cout << "int: " << n_int;
return 0;
}
无符号类型
例如:short的范围是-32768+32767;无符号版本是065535;
整型字面值
- 第一位1~9,十进制
- 第一位0,八进制
- 前两位0X/0x,十六进制
默认情况下,cout以十进制格式显示整数
#include<iostream>
using namespace std;
int main(void)
{
int chest = 42;
int waist = 0x42;
int inseam = 042;
cout << "Monsieur cuts a striking figure!" << endl;
cout << "chest = " << chest << " (42 in decimal)" << endl;
cout << "waist = " << waist << " (0x42 in hex)" << endl;
cout << "inseam = " << inseam << " (042 in octal)" << endl;
return 0;
}
输出结果:
Monsieur cuts a striking figure!
chest = 42 (42 in decimal)
waist = 66 (0x42 in hex)
inseam = 34 (042 in octal)
若cout要以十六进制、八进制输出则:
要使用cout << hex
;cout << oct
#include <iostream>
using namespace std;
int main(void)
{
int chest = 42;
int waist = 0x42;
int inseam = 042;
cout << "Monsieur cuts a striking figure!" << endl;
cout << "chest = " << chest << " (in decimal)" << endl;
cout << hex;
cout << "waist = " << waist << " (in hex)" << endl;
cout << oct;
cout << "inseam = " << inseam << " (in octal)" << endl;
return 0;
}
输出结果:
Monsieur cuts a striking figure!
chest = 42 (in decimal)
waist = 42 (in hex)
inseam = 42 (in octal)
C++如何确定常量的类型
整型常量,如1983,除非有理由存储为其他类型,默认存储为int
类型
char类型:字符和小整数
char
类型是专位存储字符(字母和数字)而设计的。存储的是字符对应的编码。
产检的符号集是ASCII字符集。字符集中的字符用数值编码(ASCII码)表示。
ASCII码 : A : 65;a : 97
cin
;cout
可以读取字符,并转化为相应的ASCII,再存储或输出。
输入字符M:
#include<iostream>
using namespace std;
int main(void)
{
char ch;
cout << "Enter a character: ";
cin >> ch;
cout << "Hello! Thank you for the " << ch << " character." << endl;
return 0;
}
通过cout.put()
成员函数输出字符
#include <iostream>
using namespace std;
int main(void)
{
char ch = 'M'; // 存储的是M对应的ASCII值
int i = ch; // 赋值的时候把ch里面存储的ASCII取了出来
cout << "The ASCII code fot " << ch << " is " << i << endl;
cout << "Add one to the character code: " << endl;
ch = ch + 1;
i = ch;
cout << "The ASCII code for " << ch << " is " << i << endl;
cout << "Displaying char ch using cout.put(ch): ";
cout.put(ch);
cout.put('!');
cout << endl
<< "Done" << endl;
return 0;
}
输出结果是:
The ASCII code fot M is 77
Add one to the character code:
The ASCII code for N is 78
Displaying char ch using cout.put(ch): N!
Done
转义字符
字符名称 | C++代码 |
---|---|
换行符 | \n |
水平制表符 | \t |
垂直制表符 | \v |
退格 | \b |
回车 | \r |
振铃 | \a |
反斜杠 | \\ |
问号 | \? |
单引号 | \’ |
双引号 | \" |
const
限定符
使用const
关键字来修改变量的声明和初始化
const int Months = 12; //常量被初始化后,其值就被固定,不允许修改
将变量的首字母大写,如Months,以区分变量和常量。
创建的通用格式是:
const type name = value;
以下代码写法不好:
const int toes; //报错常量未被初始化
toes = 10; //报错常量不允许被修改
浮点数
带小数的数字
计算机分为两部分存储:一部分表示值,另一部分表示缩放因子
书写浮点数
- 标准小数点表示法:
12.34
- E表示法:
2.52e+8
浮点类型
C++有3种浮点类型:float
double
long double
float
至少32位double
至少48位,且不少于float
long double
至少和double
一样多
显示float
和double
的精度区别:
#include<iostream>
using namespace std;
int main(void)
{
cout.setf(ios_base::fixed, ios_base::floatfield);
float tub = 10.0 / 3.0;
const float million = 1.0e6;
cout << "tub = " << tub;
cout << " , A million tubs = " << tub * million << endl;
cout << "Ten million tubs = " << 10 * tub * million << endl;
double mint = 10.0 / 3.0;
cout << "mint = " << mint;
cout << " , A million mints = " << mint * million << endl;
cout << "Ten million mints = " << 10 * mint * million << endl;
return 0;
}
输出结果:
tub = 3.333333 , A million tubs = 3333333.250000
Ten million tubs = 33333332.000000
mint = 3.333333 , A million mints = 3333333.333333
Ten million mints = 33333333.333333
double
比float
类型的精度要高很多
浮点数的优缺点
优点:表示的范围比整数大得多
缺点:运算慢、精度降低
以下代码说明了浮点运算会降低精度这一缺点:
#include<iostream>
using namespace std;
int main(void)
{
float a = 2.34e22f;
float b = a + 1.0f;
cout << "a = " << a << endl;
cout << "b - a = " << b - a << endl;
return 0;
}
输出结果:
a = 2.34e+22
b - a = 0
a
和b
相差为1.0
,计算结果却是无误差。
问题在于,2.34E+22是一个小数点左边有23位的数字。加上1,就是在第23位加1。但float类型只能表示数字中的前6位或前7位,因此修改第23位对这个值不会有任何影响。
C++算数运算符
+ - * / %
类型转换
初始化和赋值进行转换
so_long = thirty
小范围类型得数可以赋值给大范围的变量,原变量不变,所存数据被扩展- 大范围得值传递给小范围的值,会出现问题
以{}
方式初始化时进行的转换
- 列表初始化不允许缩窄
- 不允许使用变量,有超出范围得风险
表达式中的转换
- 一些类型在出现时便会自动转换
- 有些类型在与其他类型同时出现在表达式中时将被转换
- 计算时将做数据类型的提升
强制类型转换
C++11中的auto声明
关键字,编译器根据初始值的类型推断变量的类型
复习题
- int, short, long, long long
-
short a =80; unsigned int b = 42110;
- 程序员自己预估
- 默认C++整数在不超出int类型的范围的应用情况下,默认优先使用int类型
- 基于ASCII的平台下,两者是等价的
-
-
char ch = 88; cout << ch << endl;
cout << (char) 88 << endl;
-
- 放得下不会溢出
编程练习
- 编写一个小程序,要求用户使用一个整数指出自己的身高(单位为英寸),然后将身高转换为英尺和英寸。该程序使用下划线字符来指示输入位置。另外,使用一个const符号常量来表示转换因子。
运行代码:
#include<iostream>
using namespace std;
const int FOOT_TO_INCH = 12;
int main(void)
{
int h;
cout << "Please enter your height int inches_";
cin >> h;
cout << "Your height convert to " << h / FOOT_TO_INCH;
cout << " FOOT and " << h % FOOT_TO_INCH << " INCHES height." << endl;
return 0;
}
输出结果:
Please enter your height int inches_85
Your height convert to 7 FOOT and 1 INCHES height.
- 编写一个小程序,要求以几英尺几英寸的方式输入其身高,并以磅为单位输入其体重。(使用3个变量来存储这些信息。)该程序报告其BMI (Body Mass Index,体重指数)。为了计算BMI,该程序以英寸的方式指出用户的身高(1英尺为12英寸),并将以英寸为单位的身高转换为以米为单位的身高(1英寸=0.0254米)。然后,将以磅为单位的体重转换为以千克为单位的体重(1千克=2.2磅)。最后,计算相应的BMI——体重(千克)除以身高(米)的平方。用符号常量表示各种转换因子。
运行代码:
#include<iostream>
using namespace std;
const int FOOT_TO_INCH = 12;
const double INCH_TO_METER = 0.0254;
const double KILLGRAM_TO_POUND = 2.2;
int main(void)
{
int h_foot, h_inch;
double w_pound;
cout << "Please enter your height foot: ";
cin >> h_foot;
cout << "Please enter your height inches: ";
cin >> h_inch;
cout << "Please enter your weight in pounds: ";
cin >> w_pound;
double h = (h_foot * FOOT_TO_INCH + h_inch) * INCH_TO_METER;
double w = (w_pound / KILLGRAM_TO_POUND);
double bmi = w / (h * h);
cout << "Your BMI is " << bmi << endl;
return 0;
}
输出结果:
Please enter your height foot: 7
Please enter your height inches: 1
Please enter your weight in pounds: 325
Your BMI is 31.6924
- 编写一个程序,要求用户以度、分、秒的方式输入一个纬度,然后以度为单位显示该纬度。1度为60分,1分等于60秒,请以符号常量的方式表示这些值。对于每个输入值,应使用一个独立的变量存储它。下面是该程序运行时的情况:
Enter a latitude in degrees,minutes,and seconds:
First, enter the degrees: 37
Next,enter the minutes of arc: 51
Finally, enter the seconds of arc: 19
37 degrees, 51 minutes, 19 seconds = 37.8553 degrees
运行代码:
#include<iostream>
using namespace std;
int main(void)
{
int deg, min, sec;
cout << "Enter a latitude in degrees,minutes, and seconds: " << endl;
cout << "First, enter the degree: ";
cin >> deg;
cout << "Next, enter the minutes of arc: ";
cin >> min;
cout << "Finally, enter the seconds of arc: ";
cin >> sec;
double wei = deg + min / 60.0 + sec / 60.0 / 60.0;
cout << deg << " degrees, " << min << " minutes, " << sec << " seconds = " << wei << " degrees" << endl;
return 0;
}
输出结果:
Enter a latitude in degrees,minutes, and seconds:
First, enter the degree: 37
Next, enter the minutes of arc: 51
Finally, enter the seconds of arc: 19
37 degrees, 51 minutes, 19 seconds = 37.8553 degrees
- 编写一个程序,要求用户以整数方式输入秒数(使用long或 long long变量存储),然后以天、小时、分钟和秒的方式显示这段时间。使用符号常量来表示每天有多少小时、每小时有多少分钟以及每分钟有多少秒。该程序的输出应与下面类似:
Enter the number of seconds: 31600000
31600000 seconds = 365 days, 17 hours, 46 minutes, 40 seconds
运行代码:
#include<iostream>
using namespace std;
int main(void)
{
long sec;
int seconds, mintues, hours, days;
cout << "Enter the number of seconds: ";
cin >> sec;
seconds = sec % 60;
mintues = sec / 60;
hours = mintues / 60;
mintues %= 60;
days = hours / 24;
hours %= 60;
cout << sec << " seconds = " << days << " days, " << hours << " hours, " << mintues << " minutues, " << seconds << " seconds" << endl;
return 0;
}
输出结果:
Enter the number of seconds: 31600000
31600000 seconds = 365 days, 17 hours, 46 minutues, 40 seconds
- 编写一个程序,要求用户输入全球当前的人口和美国当前的人口(或其他国家的人口)。将这些信息存储在long long变量中,并让程序显示美国(或其他国家)的人口占全球人口的百分比。该程序的输出应与下面类似:
Enter the world's population: 6898758899
Enter the population of the US: 310783781
The population of the us is 4.50492% of the world population.
运行代码:
#include<iostream>
using namespace std;
int main(void)
{
long long wp, up;
cout << "Enter the world's population: ";
cin >> wp;
cout << "Enter the population of the US: ";
cin >> up;
double pre = (double)up / wp * 100;
cout << "THe population of the US is " << pre << "% "
<< "of the world population." << endl;
return 0;
}
输出结果:
Enter the world's population: 6898758899
Enter the population of the US: 310783781
THe population of the US is 4.50492% of the world population.
- 编写一个程序,要求用户输入驱车里程(英里)和使用汽油量(加仑),然后指出汽车耗油量为一加仑的里程。如果愿意,也可以让程序要求用户以公里为单位输入距离,并以升为单位输入汽油量,然后指出欧洲风格的结果——即每100公里的耗油量(升)。
#include<iostream>
using namespace std;
int main(void)
{
double miles, gallon;
cout << "Enter the distance in miles: ";
cin >> miles;
cout << "Enter the fuel consume in gallon: ";
cin >> gallon;
double consume = miles / gallon;
cout << "The fuel consume is " << consume << " miles/gallon." << endl;
return 0;
}
输出结果:
Enter the distance in miles: 100
Enter the fuel consume in gallon: 10
The fuel consume is 10 miles/gallon.
- 编写一个程序,要求用户按欧洲风格输入汽车的耗油量(每100公里消耗的汽油量(升)),然后将其转换为美国风格的耗油量——每加仑多少英里。注意,除了使用不同的单位计量外,美国方法(距离/燃料)与欧洲方法(燃料/距离)相反。100公里等于62.14英里,1加仑等于3.875升。因此,19mpg 大约合12.41/100km,127mpg 大约合8.71/100km。
运行代码:
#include<iostream>
using namespace std;
int main(void)
{
double ec, uc;
cout << "Enter the fuel consume in europe(L/100km): ";
cin >> ec;
uc = 3.875 / ec * 62.14;
cout << "The fuel consume in US is: " << uc << " mile/gallon." << endl;
return 0;
}
输出结果:
Enter the fuel consume in europe(L/100km): 8.5
The fuel consume in US is: 28.3285 mile/gallon.