c++入门(2):c++入门(2-1)-- 变量与基本类型 -- c++ primer

c++入门(2-1)-- 变量与基本类型

作者:Raphael Song 如需转载,请注明出处。

The way to learn a new programming language is to write programs.

让我们开始!

如果你还没有接触过编程,想先体验一下编程的乐趣。请看我的c++入门(1)--输入输出,代码运行顺序及其控制。此文开始学习变量以及基本类型。

Types determine the meaning of the data and operations in our programs.

原始内置类型(Primitive Build-in Types)

两类:算术类型(arithmetic types)和void。我们主要讲解算术类型。

算术类型(Arithmetic Types)

分为两类:整形(integral types)和浮点型(floating-point types)。其中整形又包含字符型(character types)和布尔型(boolean types)。

作为初学者,首先要掌握的类型有:bool(布尔类型)、int(整形)、float(单精度浮点型)、double(双精度浮点型)、char(字符型)。

下面依次来看一下:

bool(布尔类型)对应着布尔代数。它的值只可能有两种:true (1)/ false(0)。

int(整形)对应着数学中的整数:-2,-1,0,1,2...

float(单精度浮点型)跟double(双精度浮点型)都是对应着数学中的小数:-1.3,2.78.... 两者的区别是:float只能表示6位有效数字,而double可以表征10位有效数字的小数。

char(字符型)都应着字符。当然字符既有数字(1,2,3...),也有符号(。,!...)还有字母(a,b,c...A,B,C...)

代码样例

让我们通过代码具体体会一下如何在代码中使用这些类型。

计算两个整数相加。

#include<iostream>
using namespace std;

int main() {
	int a = 1;
	int b = 2;
	int c = a + b;
	cout << c << endl;
	return 0;
}

计算两个小数相加。

#include<iostream>
using namespace std;

int main() {
	double a = 1.1;
	double b = 2.2;
	double c = a + b;
	cout << c << endl;
	return 0;
}

判断两个数字相加是否正确。

#include<iostream>
using namespace std;

int main() {
	double a = 1.1;
	double b = 2.2;
	double c = a + b;
	double d = 3.3;
	cout << c << endl;
	if (c - d < 0.0000000001) {
		cout << "yes" << endl;
	} else {
		cout << "no" << endl;
	}
	
	return 0;
}

代码练习

项目1:实现一个口算能力测试程序。

#include<iostream>
using namespace std;

int main() {
	double a;
	cout << "3453 + 28594 = " << endl;
	cin >> a ; 
	if ( a-3453-28594 == 0) 
		cout << "yes" << endl;
	else 
		cout << "no" << endl;
	
	cout << "8932752+932 = "   << endl;
	cin >> a ;
	if (a-8932752-932 == 0) 
		cout << "yes" << endl;	
	else 
		cout << "no" << endl;
	
	cout << "8499384+9508930-8395 = "  << endl;
	cin >> a ;
	if (-0.00000001 < a-8499384-9508930+8395 && a-8499384-9508930+8395< 0.00000001) 
		cout << "yes" << endl;
	else 
		cout << "no" << endl;
	
	cout << "948494+93290283-2234 = "  << endl;
	cin >> a ;
	if (-0.00000001 < a-948494-93290283+2234 && a-948494-93290283+2234 < 0.00000001) 
		cout << "yes" << endl;
	else 
		cout << "no" << endl;
	
	cout << "452+94950 = "  << endl;
	cin >> a ;
	if (-0.00000001 < a-452-94950 && a-452-94950 < 0.00000001) 
		cout << "yes" << endl;
	else 
		cout << "no" << endl;
	
	cout << "3258+1298283 = "  << endl;
	cin >> a ;
	if (-0.00000001 < a-3258-1298283 && a-3258-1298283 < 0.00000001) 
		cout << "yes" << endl;
	else 
		cout << "no" << endl;
	return 0;
}

项目2:改进入门(1)中的猜数字程序。新增范围提示。

#include<iostream>
using namespace std;
//猜小了猜大了 
int main() {
	int num;
	cout << "我心里想了一个数字,你能猜到吗?试试看呗。" << endl;
	for(int i = 0; i < 10;i++) {
		cin >> num;
		if(num == 520) {
			cout << "我爱你!" << endl;
			break;
		} else {
			
			if(i == 9){
				cout << "很抱歉,你的十次机会已经用完。" << endl;
			} else {
				if (num > 520){
					cout << "不对,你猜大了。" << endl;
				}
				else {
					cout << "不对,你猜小了。" << endl;
				} 
				
				cout << "你还有" << 9 - i << "次机会" << endl;	
			}
			 
		}
	}
	return 0;
}


类型转换(Type Conversions)

长类型转换为短类型是会被截断,从而丢失信息;短类型可以安全的转换为长类型。

字面理解(Literals)

1,2,3等会被字面理解为int,而1.1,2.5等会被自动理解为double;20被理解为decimal,024被理解为octal,而0x14会被理解为hexadecimal。进一步的,‘a’被理解为character,而“hello World!”被理解为string。关于这样的字面理解还有很多,需要大家持续积累。


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

拉斐尔在读论文

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值