学习C++的第一天

这篇文章介绍了C++的基础知识,包括使用iostream库进行输入输出,利用namespace管理命令空间,以及如何声明和使用变量。文中展示了如何进行算术运算,如变量赋值和浮点数计算,并提到了精度问题。此外,还讨论了const常量的使用和函数调用。
摘要由CSDN通过智能技术生成
#include<iostream>
#include<cmath>
void simo(int n);
//using namespace std;
int main(void)
{
	//std 是库
	//namespace是命令空间
	using namespace std;//可以去除,用这种方式std::cout

	//cout表示输出对象
	cout << "C++ RULES";
	cout << endl;//重起一行
	cout << "helloll";
	//第二种形式换行
	cout << "You wont regret it!" << endl;

	int carrots;
	carrots = 25;

	int a;
	int b;
	int c;
	//把5传递个3个变量,最后3个变量的值都是5
	a = b = c = 5;
	cout << endl;
	cout << "I have ";
	cout << carrots;
	cout << endl;
	carrots = carrots - 1;
	cout << "Crunch,Crunch,Now I Have " << carrots << "carrots." << endl;

	int carrostr;
	cout << "how many carrtos do you have" << endl;
	//代表输入,进行捕获
	cin >> carrostr;
	cout << "Here are two more.";
	carrostr = carrostr + 2;
	cout << "Now you have" << carrostr << "carrostr." << endl;

	double area;
	cout << "Enter the floor area, in square feet , of your home: ";
	cin >> area;
	//计算出平方
	double side;
	side = sqrt(area);

	cout << "That's the equivalent of asquare " << side << "feet to the side" << endl;

	simo(3);
	cout << "pick an integer: ";
	int count;
	cin >> count;
	simo(count);

	bool is_ready = true;

	int ans = true; //true 将被转换为1
	int promise = false;//将被转换为0

	//同样,任何数字类型或指针隐私转换为bool值
	bool start = -100; //true
	bool stop = 0; //false

	//const 限定符,和java中final有点相似,也可以用define,但const相对较好,可以使用C++作用域规则将定义限制在函数或文件中,也可以用于更复杂的类型
	const int Months = 12;

	//在c++中,和java不同,多了long double类型 ,通常,float至少为32位,double为64位,long double 为80 , 96,或128位 
	cout.setf(ios_base::fixed, ios_base::floatfield);
	float tub = 10.0 / 3.0;
	const float million = 1.0E6;
	cout << "tub = " << tub << endl; //3.333333
	//如若乘100万被
	cout << "a million tubs = " << million * tub << endl; //3333333.250000;
		//如若扩大1000万背,则超出范围值,后面会有0值出现,不同于java,java超出范围会报错。
	cout << "Ten million tubs = " << 10 * million * tub << endl;//33333332.000000

	//可以使用double来接收
	//double mint = 10.0 / 3.0;
	//cout << "mint = " << mint << endl;
	//cout << "a million mints = " << million * mint << endl;

	//浮点数在做运算时,精度会降低
	float a = 2.34E22;
	float b = a + 1.0;

	//浮点数运算比整数慢,精度会有所降低
	cout << "a = " << a << endl;
	cout << "b - a = " << b - a << endl;

	return 0;

}

void simo(int n)
{
	using namespace std;

	cout << "Simon toes" << n << " toms/" << endl;


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值