文章目录
第二章 变量与代数思维
2.3 电子秤代码实现
练习代码
#include <iostream>
using namespace std;
int main()
{
float apple_price = 3.5; //苹果单价,3.5/公斤
float banana_price = 4.2; //香蕉单价,4.2/公斤
float apple_weight = 0.0; //苹果重量,初始化为0
float banana_weight = 0.0; //香蕉重量,初始化为0
float total = 0.0; //总价,初始化为0
cout << "请输入苹果重量" << endl; //提示信息
cin >> apple_weight; //输入苹果重量
cout << "请输入香蕉重量" <<endl; //提示信息
cin >> banana_weight; //输入香蕉重量
total = apple_price * apple_weight
+ banana_price * banana_weight; //计算总价
cout << "应付款" << total <<endl;
return 0;
}
结果展示
2.4 变量定义与变量类型
变量定义
变量在使用之前必须加以声明
每一个变量要有一个与其他变量不相同的合法的名字。第一个字符必须是字母或下划线,其后的字符只能是字母、数字和下划线,且不得与C/C++语言系统所保留的关键字相同
变量的数据类型
第一类是基本数据类型,包括整型、浮点型和字符型;
第二类是构造数据类型,包括数组、结构、联合、枚举等;
构造数据类型是由若干个基本数据类型的变量按特定的规律组合构造而成
1.整型
数据类型 | 数据类型 | 占用存储空间 | 表示范围 |
---|---|---|---|
int | 整型 | 4字节 | -2 147 483 648 ~ 2 147 483 647 |
unsigned int | 无符号整型 | 4字节 | 0 ~ 4 294 967 295 |
long int | 长整型 | 4字节 | -2 147 483 648 ~ 2 147 483 647 |
unsigned long int | 无符号长整型 | 4字节 | 0 ~ 4 294 967 295 |
可以看到整型和长整型并无差别,因为以前16位系统下int是2个字节,32和64为系统下为4个字节
2.实型
数据类型 | 数据类型 | 占用存储空间 | 表示范围 |
---|---|---|---|
float | 浮点型 | 4字节 | -3.4 x 1038 ~ 3.4 x 1038 |
double | 双精度型 | 8字节 | -1.7 x 10308 ~ 1.7 x 10308 |
long double | 长双精度型 | 10字节 | -3.4 x 104932 ~ 1.1 x 104932 |
3.bool
逻辑型,占1个字节,true & false
4.char
字符型,占1个字节
2.6.1 变量的初始值
如果变量定义后没有赋值,则变量的内存单元中的值是一个随机的数值,在不用的机器上、不同的环境下,单元的内容是不同的。
2.6.2 变量类型
定义变量类型:帮助计算机运用不同的规则进行运算
#include <iostream>
#include <string>
#include <complex>
using namespace std;
int main()
{
string str1 = "shenzhen,";
string str2 = "fanshen";
cout << "str1=" << str1 <<endl;
cout << "str2=" << str2 <<endl;
cout << "str1+str2=" << str1 + str2 <<endl;
complex<int> c1(3,4),c2(4,5);
cout << "c1=" << c1 <<endl;
cout << "c2=" << c2 <<endl;
cout << "c1+c2=" << c1 + c2 <<endl;
return 0;
}
运行结果
不同的变量类型所进行的运算方式不同
2.6.3 变量内存单元地址
&是一个位运算符,功能是“取变量的地址”,所以&n会返回变量n的内存单元的地址。
对于内存单元地址,cout会以16进制形式输出地址值,在这种形式输出结果中,前2个字符固定位 0x 或 0X。
对字符变量的内存时:输出内存单元存储的字符内容,且相邻的后续内存单元内容也以字符形式输出,知道碰到一个内容位0的内存单元才停止。
#include <iostream>
using namespace std;
int main()
{
int n;
float f;
double d=1.111;
char b='j';
char c='k';
double q=0;
char r='g';
cout << "address of n:" << &n <<endl;
cout << "address of f:" << &f <<endl;
cout << "address of d:" << &d <<endl;
cout << "address of b:" << &b <<endl;
cout << "address of c:" << &c <<endl;
cout << "address of q:" << &q <<endl;
cout << "address of r:" << &r <<endl;
return 0;
}
运行结果
2.6.4 存“变量地址”的变量——指针
TYPE* var; var是TYPE类型的指针变量,变量类型需要与指针类型对应。
#include <iostream>
using namespace std;
int main()
{
int n;
float f;
double d=1.111;
char c='j';
int* pn = &n;
float* pf = &f;
double* pd = &d;
char* pc = &c;
cout << "address of n:" << &n <<endl;
cout << "address of f:" << &f <<endl;
cout << "address of d:" << &d <<endl;
cout << "address of c:" << &c <<endl;
cout << "pn:" << pn << endl;
cout << "pf:" << pf << endl;
cout << "pd:" << pd << endl;
cout << "pc:" << pc << endl;
cout << "address of pn:" << &pn << endl;
cout << "address of pf:" << &pf << endl;
cout << "address of pd:" << &pd << endl;
cout << "address of pc:" << &pc << endl;
return 0;
}
运行结果
2.6.5 指针的 读/写 操作
* 获得指针变量对应的单元
#include <iostream>
using namespace std;
int main()
{
int n;
float f;
double d=1.111;
char c='j';
int* pn = &n;
float* pf = &f;
double* pd = &d;
char* pc = &c;
cout << "address of n:" << &n <<endl;
cout << "address of f:" << &f <<endl;
cout << "address of d:" << &d <<endl;
cout << "address of c:" << &c <<endl;
cout << "pn:" << pn << endl;
cout << "pf:" << pf << endl;
cout << "pd:" << pd << endl;
cout << "pc:" << pc << endl;
cout << "address of pn:" << &pn << endl;
cout << "address of pf:" << &pf << endl;
cout << "address of pd:" << &pd << endl;
cout << "address of pc:" << &pc << endl;
*pn = 999;
*pf = 888;
*pd = 555;
*pc = 'f';
cout << "n:" << n <<endl;
cout << "f:" << f <<endl;
cout << "d:" << d <<endl;
cout << "c:" << c <<endl;
cout << "*pn:" << *pn << endl;
cout << "*pf:" << *pf << endl;
cout << "*pd:" << *pd << endl;
cout << "*pc:" << *pc << endl;
return 0;
}
运行结果
2.6.6 指针的 加/减 操作
对指针加减:对指针的地址值 +/- 所对应变量类型的 size
#include <iostream>
using namespace std;
int main()
{
int n1 = 12, n2 = 87;
double d1 = 1.111, d2 = 2.22, d3 = 0;
char c1 = 'j', c2 = 'q';
cout << "n1: value= " << n1 << ", address= " << &n1 <<endl;
cout << "n2: value= " << n2 << ", address= " << &n2 <<endl;
cout << "d1: value= " << d1 << ", address= " << &d1 <<endl;
cout << "d2: value= " << d2 << ", address= " << &d2 <<endl;
cout << "d3: value= " << d3 << ", address= " << &d3 <<endl;
cout << "c1: value= " << c1 << ", address= " << &c1 <<endl;
cout << "c2: value= " << c2 << ", address= " << &c2 <<endl;
int* pn = &n1;
double* pd = &d1;
char* pc = &c1;
cout << "pn:" << pn <<endl;
cout << "pn - 1:" << pn - 1 <<endl;
cout << "pd:" << pd <<endl;
cout << "pd - 1:" << pd - 1 <<endl;
cout << "pc:" << pc <<endl;
cout << "pc - 1:" << pc - 1 <<endl;
*(pn - 1) = 666;
*(pc - 1) = 'f';
cout << "n2 =: " << n2 <<endl;
cout << "c2 =: " << c2 <<endl;
return 0;
}
运行结果
语法自测-试题答案
"_“下划线可用,但是”-"不可以
结语
给自己加个油把