C++ Primer plus 第二章

前言

看书容易睡着,敲一遍代码先,按照书中的程序标号。

2.1 myfirst.cpp

代码

#include <iostream>

using namespace std;

int main()
{
	cout << "Come up and C++ me some time.";
	cout << endl;
	cout << "You won't regret it!" << endl;

	cin.get();
	return 0;
}

结果

在这里插入图片描述

知识点

1.C++程序的组成

2.2 carrot.cpp

代码

#include<iostream>

using namespace std;

int main()
{
	int carrots;
	carrots = 25;

	cout << "I have ";
	cout << carrots;
	cout << " carrots.";
	cout << endl;
	carrots = carrots - 1;
	cout << "Now I have " << carrots << " carrots." << endl;

	return 0;
}

结果

在这里插入图片描述

知识点

1.变量的声明、赋值
2.cout的分开使用和连续使用

2.3 getinfo.cpp

代码

#include <iostream>

using namespace std;

int main()
{
	int carrots;

	cout << "How many carrots do you have?" << endl;
	cin >> carrots;
	cout << "Here are two more. ";
	carrots = carrots + 2;
	cout << "Now you have " << carrots << " carrots." << endl;

	return 0;
}

结果

在这里插入图片描述
其中10是自己输入的数字。

知识点

  1. cin的使用

2.4 sqrt.cpp

代码

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
	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 a square " << side
		<< " feet to the side." << endl;
	cout << "How fascinating!" << endl;

	return 0;
}

结果

在这里插入图片描述

知识点

1.sqrt()函数用法

2.5 ourfun.cpp

代码

#include <iostream>

using namespace std;

//函数声明
void simon(int);

int main()
{
	//函数使用
	simon(3);
	
	cout << "Pick an integer: ";
	int count;
	cin >> count;
	
	//函数使用
	simon(count);
	cout << "Done!" << endl;
	return 0;
}

//函数定义
void simon(int n)
{
	cout << "Simon says touch your toes " << n << " times." << endl;
}

结果

在这里插入图片描述

知识点

1.函数声明
2.函数定义
3.函数使用

2.6 convert.cpp

代码

#include <iostream>

using namespace std;

int stonetolb(int);

int main()
{
	int stone;
	cout << "Enter the weight in stone: ";
	cin >> stone;

	int pounds = stonetolb(stone);

	cout << stone << " stone = ";
	cout << pounds << " pounds." << endl;

	return 0;
}

int stonetolb(int sts)
{
	return 14 * sts;
}

结果

在这里插入图片描述

知识点

  1. 有返回值的函数声明和定义

总结

加油!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值