C++学习笔记

发布于:2023-01-10 ⋅ 阅读:(237) ⋅ 点赞:(0)

教材《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 main()
{
	cout << "Enter the number of hours: "; 
	int H;
	cin >> H;
	cout << "Enter the number of hours: ";
	int M;
	cin >> M; 
	Time(H, M);
}
void Time(int h,int m)
{
	cout << "Time : "<<h<<":" << m << endl;
}

22.8.20

第三章 处理数据

3-1简单变量

3.1 limits.cpp     头文件limites

#include<iostream>
#include<climits>     //头文件climites定义了各种限制的符号名称。(eg:INT_MAX为int的最大值)
#define a 32          //类似于查找替换,climites文件中包含与该语句类似的语句行

int main()
{
	using namespace std;
	int n_int = INT_MAX;// or int n_int(INT_MAX)  (另一种初始化语法
	short n_short = SHRT_MAX;
	long n_long = LONG_MAX;
	long long n_llong = LLONG_MAX;

	cout << "int is " << sizeof(int) << " bytes." << endl;
	cout << "int is " << sizeof n_int << " bytes." << endl; 
	cout << "int is " << sizeof (n_int) << " bytes." << endl;
	//两种表达方式结果相同,对变量名sizeof可不使用()
	cout << "short is " << sizeof(short) << " bytes." << endl;
	cout << "long is " << sizeof(long) << " bytes." << endl;
	cout << "long long is " << sizeof(long long) << " bytes." << endl;
	cout << endl;

	cout << "Maximum values: " << endl;
	cout << "int: " << n_int << endl;
	cout << "short: " << n_short << endl;
	cout << "long: " << n_long << endl;
	cout << "unsigned long: " << ULONG_MAX << endl;
	cout << "long long: " << n_llong << endl<<"\n";

	cout << "Minmum int value = " << INT_MIN << endl;
	cout << "Bites per byte = " << CHAR_BIT << endl;  //一个字节(byte)的位数

	
	cout << "a = " << a << endl;
	int n_int2(INT_MAX);
	cout << "n_int2 = " << n_int2 << endl << endl;
	//大括号的初始化方式,用于数组和结构
	int c{ 3 };
	int d = { 4 };
	int e{};
	cout << "c = " << c << endl;
	cout << "d = " << d << endl;
	cout << "e = " << e << endl;

	return 0;
}

2022.8.21

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

#include<iostream>
#define ZERO 0
#include<climits>
int main()
{
	using namespace std;
	short sam = SHRT_MAX;      /*short表示范围为-32768到+32768,则无符号的short表示范围为0到65535;
	                             如果超越了限制,其值为范围另一端的取值            */
	unsigned short sue = sam;  //注意 unsigned 本身是 unsigned int 的缩写

	cout << "Sam has " << sam << " dollars and Sue has " << sue;
	cout << " dollars deposited." << endl
		<< "Add $1 to each accout." << endl << "Now ";
	sam = sam + 1;
	sue = sue + 1;
	cout << "Sam has " << sam << " dollars and Sue has " << sue;
	cout << " dollars deposited.\nPoor Sam!" << endl;
	sam = ZERO;
	sue = ZERO;
	cout << "Sam has " << sam << " dollars and Sue has " << sue;
	cout << " dollars deposited." << endl
		<< "Take $1 from each account." << endl << "Now ";
	sam = sam - 1;
	sue = sue - 1;
	cout<<"Sam has " << sam << " dollars and Sue has " << sue;
	cout << " dollars deposited."<<endl<<"Lucky Sue!";
}

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

#include<iostream>
int main() 
{
	using namespace std;
	int chest = 42;        //第一位为 1-9,为10进制的数
	int waist = 0x42;      //前两位为 0x或0X 表示16进制的数
	int inseam = 042;      //第一位为 0,第二位为1-7,则为八进制的数
	                       //这些数都将以二进制的数被储存在计算机中
	cout << "Monsieur cuts a striking figure!\n";
	cout << "chest = " << chest << " <42 in decimal> \n";
	cout << "waist = " << waist << " <0x42 in hex> \n";
	cout << "inseam = " << inseam << " <042 in octal> \n";
}

3.4 hexoct2.cpp       

#include<iostream>
using namespace std;   // hex位于名称空间std中
int main()
{
	int chest = 42;
	int waist = 42;
	int inseam = 42;

    //dec、hex、oct,分别用于指示cout以十进制、十六进制和八进制格式显示整数
	cout << "Monsieur cuts a striking figure!\n";
	cout << "chest = " << chest << " < decimal for 42 > \n";    //cout的默认显示格式为十进制
	cout << hex;                      
	cout << "waist = " << waist << " < hexadecimal for 42 > \n";//修改格式之前,原来的格式一直有效
	cout << oct;                      
	cout << "inseam = " << inseam << " < octal for 42 > \n";
}

3.5 chartype.cpp    关于char

#include<iostream>
using namespace std;
int main()
{
	char ch;
	cout << "Enter a character: " << endl;
	cin >> ch;
	cout << "Hola! ";
	cout <<"Thank you for the " << ch << " character." << endl;

}

2022.8.3

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

成员函数 cout.put(): 

类定义了如何表示和控制数据。成员函数归类所有,描述了操控类数据的方法。例如类ostream有一个put()成员函数,用来输出字符。只能通过类的特定对象(例如这里的cout对象)来使用成员函数。要通过对象(cout)使用成员函数,必须用句点将对象名和函数名称(put())连接起来。句点被称为成员运算符。cout.put()指:通过类对象cout来使用函数put().

#include<iostream>
int main()
{
	using namespace std;
	char ch = 'M';        //'M'表示字符M的数值编码
	int i = ch;
	cout << "The ASCII code for " << ch << " is " << i << endl; 
	//值的类型将引导cout选择如何显示值,此时的ch显示的为M

	cout << "Add one to the character code:" << endl;
	ch = ch + 1;
	i = ch;
	cout << "The ASCII code for " << ch << " is " << i << endl;
	cout << "Displaying char ch using cout.put(ch): ";
	cout.put(ch);
	cout.put('!');
	cout << endl << "Done!" << endl;

    char alarm = '\a';              //转义序列:\a表示振铃字符;\r表示回车
	cout << alarm << "Don't do that again!\a\n";// or cout << "\a";
	cout<<"Ben \"Buggsie\" Hacker\nwas here!\n";
}

3.7 bondini.cpp   转义序列

应当向处理常规字符(如q)那样处理转义序列(如\n):

cout << '\n';
cout << "\n";
#include<iostream>
int main()
{
	using namespace std;
	cout << "\aOperation \"HyperHype\" is now activated!\n";
	cout << "Enter your agent code:________\b\b\b\b\b\b\b\b";
	//8个退格字符\b是为了让光标退到8个字符前,即该句子中的:后
	long code;
	cin >> code;
	cout << "\aYou entered " << code << "...\n";
	cout << "\aCode verifide! Proceed with Plan Z3!\n";
}

 3-2 const限定符

使用const关键字来修改变量声明和初始化。

const int Months=12;

常量(如Months)被初始化后,其值就被固定了,编译器将不允许再修改该常量的值。

注:const与#define相比的优点之一是可以明确指定类型。

宽字符类型wchar_t
传统的字符数据类型为char,占用一个字节,存放的数据内容为ASCII编码,最多可以存放255种字符,

wchar_t为宽字符类型或双字符类型,它占用两个字节,因此能够存放更多的字符。
 


3-3 浮点数

3.8 floatnum.cpp

#include<iostream>
int main()
{
	using namespace std;
	cout.setf(ios_base::fixed, ios_base::floatfield);
	float tub = 10.0 / 3.0;
	double mint = 1.0 / 3.0;
	const float million = 1.0e6;

	cout << "tub = " << tub;
	cout << ", a million tubs = " << million * tub;
	cout << ",\nand ten million tubs = " << 10 * million * tub << endl;

	cout << "mint = " << mint << " and a million mints = "
	     << million * mint << endl;
}

3.9 fltadd.cpp

#include<iostream>
int main()
{
	using namespace std;
	float a = 2.34e+22f;   
	float b = a + 1.0f;
	/*float类型只能表示数字中的前6位或前7位,
	因此修改第32位对这个值不会有任何影响*/
	cout << "a = " << a << endl
		<< "b-a = " << b - a << endl;
}

2022.8.25

3.10 arith.cpp  运算符

#include<iostream>
int main()
{
	using namespace std;
	int u, d;

	cout.setf(ios_base::fixed, ios_base::floatfield);
	cout << "Enter a number: ";
	cin >> u;
	cout << "Enter another number: ";
	cin >> d;

	cout << "u = " << u << ", d = " << d << endl;
	cout << "u + d = " << u + d << endl;
	cout << "u - d = " << u - d << endl;
	cout << "u * d = " << u * d << endl;
	cout << "u / d = " << u / d << endl;          //若两个操作数都是整数,则结果为商的整数部分
	cout << "u % d = " << u % d << endl;
	/*%运算符求模,它生成第一个数除以第二个数后的余数。两个操作数必须是整型,若其中一个操作数为负数,
	则结果满足如下规则: (a/b)* b+a % b = a   */
}

3.11 divide.cpp    /

#include<iostream>
int main() 
{
	using namespace std;
	cout.setf(ios_base::fixed, ios_base::floatfield);
	cout << "Integer division: 9/5 = " << 9 / 5 << endl;
	cout << "Floating-point division: 9.0/5.0 = ";
	cout << 9.0 / 5.0 << endl;
	cout << "Mixed division: 9.0/5 = " << 9.0 / 5 << endl;
	cout << "double constants: 1e7f/9.0f = " << 1e7 / 9.0 << endl; //浮点常量在默认情况下为double类型
	cout << "float constants: 1e7f/9.0f = " << 1.e7f / 9.0f << endl;

}

3.12 modulus.cpp  %

#include<iostream>  //将磅转换成英石,一英石等于14磅
int main()
{
	using namespace std;
	const int Lbs_per_stn = 14;
	int lbs;

	cout << "Enter your weight in pounds: ";
	cin >> lbs;
	int stone = lbs / Lbs_per_stn;
	int pounds = lbs % Lbs_per_stn;
	cout << lbs << " pounds are " << stone << " stone, " << pounds << " pound(s).\n";
	
	cout << bool(2);  //结果为1
}

3.13 assign.cpp

#include<iostream> //初始化进行转换
int main() 
{
	using namespace std;
	cout.setf(ios_base::fixed, ios_base::floatfield);
	float tree(3);
	int guess = 3.9832;
	int debt = 7.2e12;
	cout << "tree = " << tree << endl;
	cout << "guess = " << guess << endl;
	cout << "debt = " << debt << endl;
} 

2022.9.6

3-7 编程练习

1.

#include<iostream>         //英寸转英尺和英寸,1英尺=12英寸
int main()
{
	using namespace std;
	const int tr = 12;
	int inch,feet;
	cout << "Enter your hight in feets __\b\b";
	cin >> feet;
	inch = feet / tr;
	int feets = feet % tr;
	cout << feet << " feets is " << inch << " inches " << feets << " feets\n";

}

2.

#include<iostream>         //输入身高和体重,报告BMI
int main()
{
	using namespace std;
	const int itrf = 12;
	const float itrm = 0.0254;
	const float ptrkg = 2.2;
	int inch,feet,pound;

	cout << "Enter your hight in inches and feets: _\b";
	cin >> inch >> feet;
	cout << "Enter your weight in pounds: ___\b\b\b";
	cin >> pound;
	int i = inch * itrf + feet;
	double m = i * itrm;
	double kg = pound / ptrkg;
	double BMI = kg / m / m;

	cout <<"Your BMI is " << BMI << " . \n";

}

3.

#include<iostream>         //输入度分秒的纬度,以度为单位显示该纬度
int main()
{
	using namespace std;
	const double tr = 60;
	int d,m,s;

	cout << "Enter a latitude in degrees, minutes, and seconds:\n";
	cout << "First, enter the degrees:";
	cin >>d;
	cout << "Next, enter the minutes of arc:";
	cin >> m;
	cout << "Finally, enter the seconds of arc:";
	cin >> s;
	double dd = d + m / tr + s / tr / tr;

	cout << d << " degrees, " << m << " minutes, " << s << " seconds = " << dd << " degrees. \n";

}

4.

#include<iostream>         //输入秒数,以天、小时、分钟和秒的方式显示这段时间
int main()
{
	using namespace std;
	const int d2h = 24;
	const int h2m = 60;
	const int m2s = 60;

	long long second;
	cout << "Enter the number of seconds:\n";
	cin >> second;
	int d, h, m, s;
	int a = d2h * h2m * m2s;
	d = second / a;
	h = second % a / h2m / m2s;
	m = (second - d * a - h * h2m * m2s) / m2s;
	s = (second - d * a - h * h2m * m2s) % m2s;
	cout << second << " seconds = " << d << " days, " << h << 
		" hours, " << m << " minutes, " << s << " seconds";
	
}

5.

#include<iostream>         //输入人数以显示人数占全球的百分比
int main()
{
	using namespace std;
	
	long long wp,up;
	cout << "Enter the world's population: ";
	cin >> wp;
	cout << "Enter the population of the US: ";
	cin >> up;
	double pre = (double) up / wp*100;
	cout << "The population of the US is "<<pre<<"% of the world population.\n";
	
}

6.

#include<iostream>         //计算每100公里的耗油量
int main()
{
	using namespace std;
	
	double km,L;
	cout << "Enter the driving distance in kilometer: ";
	cin >> km;
	cout << "Enter fuel consumption in liters: ";
	cin >> L;
	double cons100=L/km*100;
	cout << "The fuel consumption per 100 kms is "<< cons100<<" /100km";
	
}

7.

#include<iostream>  //输入欧洲风格油耗量(每100km消耗的汽油量(升)),将其转换为美国风格的耗油量——每加仑多少英里
int main()
{
	using namespace std;

	double eur,amer;
	cout << "Enter European style fuel consumption: ";
	cin >> eur;
	amer = 62.14 /(eur/ 3.875);
	cout << "The corresponding American style fuel consumption  is "<<amer<<"mpg\n";
	
}


网站公告

今日签到

点亮在社区的每一天
去签到