c++ primer plus 第三章处理数据

3、1简单变量

程序必须记录个基本属性:

1、信息将存在哪里

2、要存储什么值

3、存储何种类型的信息

int a = 5;

存储整数,并用名称a表示该整数值。

实际上是程序将找到一块能够存储整数的内存,将该内存单元标记为a,并将5复制到该内存单元中。然后可以使用a来访问该单元。

&检索地址符号

计算机内存的基本单元式位(bit)。

8位内存块可以设置出256中不同的组合,也就是2的8次方

字节(byte)通常指的是8位的内存单元

sizeof预算符返回类型或变量的长度,单位为字节

程序3.1

#include <iostream>
#include <climits>

int main(void)
{
    using namespace std;

    int n = 3;//int n{3}; int n(3); int n = {3};
    int n_int = INT_MAX;
    short n_short = SHRT_MAX;
    long n_long = LONG_MAX;
    long long n_llong = LLONG_MAX;

    cout << "n = " << n << endl;

    cout << "int is " << sizeof(int) << " bytes." << endl;
    cout << "short is " << sizeof n_short << " bytes." << endl;
    cout << "long is " << sizeof n_long << " bytes." << endl;
    cout << "long long is " << sizeof n_llong << " bytes." << endl;

    cout << "Maximum values: " << endl;
    cout << "int: " << n_int << endl;
    cout << "short: " << n_short << endl;
    cout << "long: " << n_long << endl;
    cout << "long long: " << n_llong << endl;

    return 0;
}

 

n = 3
int is 4 bytes.
short is 2 bytes.
long is 4 bytes.
long long is 8 bytes.
Maximum values:
int: 2147483647
short: 32767
long: 2147483647
long long: 9223372036854775807

1、运算符sizeof

sizeof(int)  或者sizeof int 

括号是可选的

头文件limits定义了符号常量

climits包含了类似下面的语句

#define INT_MAX 32767

用INT_MAX替换32767

 

无符号和有符号变量

shor sam t和unsigned short sue

分别将他们的最大值加1

无符号变量sue设为0减去1编程65535

如果超过限制就回去他的另一边找值。

整型字面值 

如果第一位是1-9则为十进制

如果第一位是0第二位是1-7则为八进制

如果前两位是0x则是十六进制

在iostream中还提供了控制符dec、hex和oct分别为十进制、十六进制和八进制

cout<<hex修改cout显示整数方式。需要使用using namespace std;

char类型专门存储字符。

#include <iostream>

int main(void)
{
    using namespace std;

    char ch;

    cout << "Enter a character:" << endl;
    cin >> ch;

    cout << "Hello, thank you for the " << ch << " character." << endl;

    return 0;
}
Enter a character:
m
Hello, thank you for the m character.

程序中输入的是m,而不是对应的字符编码109,程序打印的是m而不是字符编码109。

查看内存可以知道,109是存储在变量ch中的值。

cin将键盘输入的m转化为109.

输出的时候,cout将109转化为所显示的字符m

字符用单引号''

字符串有那个双引号“”

char ch;

cin>>ch;

输入5,读取字符5,将其对应字符编码asscii是53

int ch;

cin >> ch;

输入5,读取字符5,存为数字5

成员函数cout.put()

是一个重要的oop概念。通过类对象cout来使用put()函数。

cout.put('$')输出字符而不是对应的ascii码

\n换行符

char16_t无符号16位

char32_t无符号32位

3、2const限定符

const关键字来修改变量声明和初始化。

const int moths = 12;

程序中使用moths就代表12了;

浮点数

E6代表的是10的6次方。

 3、4类型转换

将一个值赋值给取值范围更大的不会导致什么问题。

将一个值赋值给取值范围小的可能会出现问题,降低精度。

强制类型转换:

int thorn

(long) thorn

long(thorn)

这两种方式都是可以的

auto声明

让编译器能够根据初始值的类型推断变量的类型。

如果使用auto而不指定类型

auto n = 100;                 n是int

auto x = 1.5;                   x是double

auto y = 1.3e5                 y是long double

3、5总结:

c ++ 的基本类型分为两组;

一组是存储为整数的值组成

另一组是存储浮点数格式的值组成的。

整型有有无符号的区别

整型有:bool,   char,  signed char , unsigned char ,  short , unsigned short ,int ,  unsigned int , long , unsigned long, long long , unsigned long long

浮点数:float , double ,long double

3、6复习题:

 

1、 根据合适的数据运算需要选择合适的数据类型与之进行匹配

2、a)short a = 80;
     b)unsigned int b = 42110;
     c)unsigned int c = 3000000000;

3、c++没有提供自动防止超出整数类型的功能,需要程序员自己来选择的合适的数据类型。C++并没有做规定,具体的值由开发平台和编译器决定。

4、33int类型

33l long类型

5、在asciii码下是等价的

6、char a = 99

7、不同的平台和编译器对应的long和long long类型的大小是不同的,
  如果long长度为4字节,则存放在double类型中不会出现舍入误差,如果long long类型为8字节,则存放在double类型中会出现舍入误差。

8、74

4

0

4.5

3

9,int sum = (int)x1 + (int)x2;  / int sum = int(x1) + int(x2);
  int sum = (int)(x1 + x2)   / int sum = int(x1+x2);

3、7编程练习

 1、

#include <iostream>

const int FOOT_TO_INCH = 12;

int main(void)
{
	using namespace std;

	int height;

	cout << "Please enter your height int inches_";
	cin >> height;

	cout << "Your height convert to " << height/FOOT_TO_INCH;
	cout << " foot and " << height%FOOT_TO_INCH << " inches height." << endl;

	return 0;
}

 

Please enter your height int inches_175
Your height convert to 14 foot and 7 inches height.

 

2、

#include <iostream>

const int FOOT_TO_INCH = 12;
const double INCH_TO_METER = 0.0254;
const double KILLOGRAM_TO_POUND = 2.2;

int main(void)
{
	using namespace std;

	int height_foot, height_inch;
	double weight_pound, height, weight, bmi;

	cout << "Please enter your height foot:";
	cin >> height_foot;
	cout << "Please enter your height inches:";
	cin >> height_inch;
	cout << "Please enter your weight in pounds:";
	cin >> weight_pound;

	height = (height_foot*FOOT_TO_INCH + height_inch)*INCH_TO_METER;
	weight = weight_pound / KILLOGRAM_TO_POUND;

	bmi = weight / (height * height);

	cout << "Your BMI is " << bmi << endl;

	return 0;
}

3、

#include <iostream>

const int DEGREE_TO_MINUTE = 60;
const int DEGREE_TO_SECOND = 3600;

int main(void)
{
	using namespace std;

	int degree, minute, second;
	double degree_style;

	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;

	degree_style = degree + (double)minute/DEGREE_TO_MINUTE + (double)second/DEGREE_TO_SECOND;

	cout << degree << " degrees, " << minute << " minutes, " << second << " seconds = " << degree_style << " degrees" << endl;

	return 0;
}

4、

#include <iostream>

const int DAY_HOUR = 24;
const int HOUR_TO_MINUTE = 60;
const int MINUTE_TO_SECOND = 60;

int main(void)
{
	using namespace std;

	long long seconds;
	int days, hours, minutes;

	cout << "Please enter the number of seconds:";
	cin >> seconds;

	days = seconds / (DAY_HOUR*HOUR_TO_MINUTE*MINUTE_TO_SECOND);
	seconds = seconds % (DAY_HOUR*HOUR_TO_MINUTE*MINUTE_TO_SECOND);

	hours = seconds / (HOUR_TO_MINUTE*MINUTE_TO_SECOND);
	seconds = seconds % (HOUR_TO_MINUTE*MINUTE_TO_SECOND);

	minutes = seconds / MINUTE_TO_SECOND;
	seconds = seconds % MINUTE_TO_SECOND;

	cout << days << " days," << hours << " hours," << minutes << " minutes," << seconds << " seconds." << endl;

	return 0;
}

5、

#include <iostream>

int main(void)
{
	using namespace std;

	long long global_amount, american_amount;
	double population_percent;

	cout << "Enter the world's population: ";
	cin >> global_amount;
	cout << "Enter the population of US: ";
	cin >> american_amount;

	population_percent = 100 * (double)american_amount / (double)global_amount;

	cout << "The population of the US is " << population_percent << "% of the world population." << endl; 

	return 0;
}

6、

#include <iostream>

int main(void)
{
	using namespace std;

	double distance_in_miles, fuel_in_gallon;
	double distance_in_kilometer, fuel_in_litre;
	double fuel_consume;

	cout << "Enter the distance in miles: ";
	cin >> distance_in_miles;
	cout << "Enter the fuel consume in gallon: ";
	cin >> fuel_in_gallon;

	fuel_consume = distance_in_miles / fuel_in_gallon;
	cout << "The fuel consume is " << fuel_consume << " miles/gallon." << endl;

	cout << "Enter the distance in kilometer: ";
	cin >> distance_in_kilometer;
	cout << "Enter the fuel consume in litre: ";
	cin >> fuel_in_litre;

	fuel_consume = (fuel_in_litre / distance_in_kilometer) * 100;
	cout << "The fuel consume is " << fuel_consume << " L/100km." << endl;
	
	return 0;
}

7、

#include <iostream>

const double GALLON_TO_LITRE = 3.875;
const double HKM_TO_MILE = 62.14;

int main(void)
{
	using namespace std;

	double fuel_consume_eur, fuel_consume_us;

	cout << "Enter the fuel consume in europe(L/100Km): ";
	cin >>fuel_consume_eur;

	fuel_consume_us = (GALLON_TO_LITRE * HKM_TO_MILE) / fuel_consume_eur;

	cout << "The fuel consume is " << fuel_consume_us << " mile/gallon." << endl;

	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值