C++学习笔记

教材《C++ Primer Plus (第六版) 中文版》 ,这里有教材中的示例源文件以及部分练习题代码。

目录

一些技巧

第二章

2.2 carrot.cpp 

2.3 getinfo.cpp    输入和输出

2.4 sqrt.cpp

2.5 ourfunc.cpp  自定义函数

2.6 convert.cpp  

2.7 编程练习

第三章 处理数据

3-1简单变量

3.1 limits.cpp     头文件limites

3.2 exceed.cpp       无符号类型超出限制的演示

3.3 hexoct.cpp      不同进制数的表示

3.4 hexoct2.cpp       

3.5 chartype.cpp    关于char

3.6 morechar.cpp  成员函数 cout.put()

3.7 bondini.cpp   转义序列

 3-2 const限定符

3-3 浮点数

3.8 floatnum.cpp

3.9 fltadd.cpp

3.10 arith.cpp  运算符

3.11 divide.cpp    /

3.12 modulus.cpp  %

3.13 assign.cpp

3-7 编程练习


一些技巧

1.重命名变量:选中变量,按住Ctrl的同时按两次R,即可重命名。

2.一个项目多个包含main的源文件:在不需要运行的源文件处,鼠标右击>>属性>>从生成中排除右边选择是。

3.按Tab即可选中输入框中的词。


22.8.10

第二章

2.1

#include<iostream>     //编译指令 使用cin和cout进行输入和输出的程序必须包含头文件iostream

int main()       /*function heading 函数头,作为接口:
                 返回一个值{int(整数值) 函数返回类型};
				 括号部分为形参列表:从调用函数传递给被调用函数的信息;*/
{
	using namespace std;   //using编译指令,  将类、函数和变量放置在名称空间std(使std::cout可简化成cout等,避免名称混淆)

	cout << "come up and C++ me some time.";        //cout输出流;<<将右侧信息插入到流中

	cout << endl;                                   // endl;换行 也可以写成"\n"

	cout << "You won't regret it!" << endl;

	return 0;    //main()函数中可不写这句
}

2022.8.19

2.2 carrot.cpp 

//carrots.cpp--food processing program
//use and dispaly a variable

#include <iostream>

int main()
{
	using namespace std;

	int carrots;
	carrots = 25;
	cout << "I have ";
	cout << carrots << " carrots." << endl;
	cout << "Crunch,crunch.Now I have " << carrots - 1 << " carrots."<< endl;
	return 0;
}

2.3 getinfo.cpp    输入和输出

//input and output
#include <iostream>
int main()
{
	using namespace std;
	int carrots;
	cout << "How many carrots do you have?" << endl;
	
	cin >> carrots;                //the next line concatenates output(连续输出,若输入c则赋值carrots为0
	cout << "Here are two more.";
	carrots = carrots + 2;         //cout语句的输出紧跟在前一条cout语句的输出后面
	cout << "Now you have " << carrots << " carrots." << endl;

	/*cout << "Now you have " 
	       << carrots 
	       << " carrots."
	       << endl;
	*/
	return 0;
}

2.4 sqrt.cpp

#include<iostream>
#include<cmath>       //提供sqrt()的函数原型,原型只描述函数接口(发送给函数的信息和返回的信息)

int main()
{
	using namespace std;

	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;
}

2.5 ourfunc.cpp  自定义函数

#include<iostream>
void simon(int);
//可以将编译指令using namespace std;放在函数前面,使函数都能访问名称空间std。
int main()
{
	using namespace std;
	simon(3);
	cout << "Pick an integer:";//integer(整数)
	int count;
	cin >> count;
	simon(count);
	cout << "Done!" << endl;
	return 0;
}

void simon(int n)
{
	using namespace std;
	cout << "Simon says touch your toes " << n << " times." << endl;
}

2.6 convert.cpp  

#include<iostream>
int stonetrans(int);
int main()
{
	using namespace std;
	int stone;     //选中变量,按住Ctrl+R 再按一次R;即可重命名该变量
	cout << "Enter the wight in stone: ";
	cin >> stone;
	int pounds = stonetrans(stone);   //直接定义并赋值
	cout << stone << " stone = " << pounds << " pounds." << endl;
}
int stonetrans(int n)
{
	return 14*n;                 //return 后面可以跟表达式       
}

2.7 编程练习

1、2

#include<iostream>
using namespace std;
int main()
{
	//cout << "NAME PLACE"<<endl;                  //第1题

	/*cout << "请输入一个以long为单位的距离";      //第2题
	int lon;
	cin >> lon;
	cout << lon << " long = " << lon * 220 << " 码 .";
	*/
	
}

3

void fir(int);
void sec(int);
using namespace std;
int main()
{
	fir(1);
	fir(1);
	sec(2);
	sec(2);
}
void fir(int n)
{
	cout << "Three blind mice\n";
}
void sec(int n)
{
	cout << "See how they run"<<endl;
}

5.

#include<iostream>

double c2f(int);

using namespace std;
int main()
{
	cout << "Please enter a Celsius value: "; 
	int C;
	cin >> C;
	cout << C << " degrees Celsius is " << c2f(C) << " degrees Fahrenheit."<<endl;
}
double c2f(int n)
{
	return n * 1.8 + 32;
}

7.

#include<iostream>

void Time(int,int);  //函数可以有多个接受的参数

using namespace std;
int mai
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值