⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 引用.txt

📁 钱能主编 C++程序设计教程(第一版) 该书习题的答案代码
💻 TXT
字号:
//*************************
//**      ch9_1.cpp      **
//*************************
#include <iostream.h>

void main()
{
	int intOne;
	int& rInt=intOne;

	intOne=5;
	cout<<"intOne:"<<intOne<<endl
		<<"rInt:"<<rInt<<endl;
	cout<<endl;
	rInt=7;
	cout<<"intOne:"<<intOne<<endl
		<<"rInt:"<<rInt<<endl;
}

//*************************
//**      ch9_2.cpp      **
//*************************
#include <iostream.h>

void main()
{
	int intOne=5;
	int* ip=&intOne;
	int& rInt=intOne;
	cout<<"&intOne:"<<&intOne<<endl;
	cout<<"&ip    :"<<&ip<<endl;
	cout<<"&rInt  :"<<&rInt<<endl;
}

//*************************
//**      ch9_3.cpp      **
//*************************
#include <iostream.h>

void main()
{
	int intOne;
	int& rInt=intOne;

	intOne=5;
	cout<<"intOne:"<<intOne<<endl
		<<"rInt:"<<rInt<<endl;
	cout<<endl;

	cout<<"&intOne:"<<&intOne<<endl;
	cout<<"&rInt  :"<<&rInt<<endl;
	cout<<endl;

	int intTwo=8;
	rInt=intTwo;
	cout<<"intOne:"<<intOne<<endl
		<<"intTwo:"<<intTwo<<endl
		<<"rInt  :"<<rInt<<endl<<endl;

	cout<<"&intOne:"<<&intOne<<endl
		<<"&intTwo:"<<&intTwo<<endl
		<<"rInt   :"<<&rInt<<endl<<endl;
}

//*************************
//**      ch9_4.cpp      **
//*************************
#include <iostream.h>

void swap(int& rx, int& ry);

void main()
{
	int x=5;
	int y=6;
	cout<<"before swap, x="<<x
		<<", y="<<y<<endl;
	cout<<"&x :"<<&x<<endl
		<<"&y :"<<&y<<endl<<endl;

	swap(x,y);

	cout<<"after swap,  x="<<x
		<<", y="<<y<<endl;
	cout<<"&x :"<<&x<<endl
		<<"&y :"<<&y<<endl<<endl;
}

void swap(int& rx, int& ry)
{
	int tmp;
	cout<<"in func swap..."<<endl;
	tmp=rx;
	rx=ry;
	ry=tmp;
	cout<<"&rx:"<<&rx<<endl
		<<"&ry:"<<&ry<<endl;
	cout<<"exit swap."<<endl;
}

//*************************
//**      ch9_5.cpp      **
//*************************
#include <iostream.h>

int Factor(int n, int& rSquared, int& rCubed);

void main()
{
	int number,squared,cubed,error;
	cout<<"enter a number(0-20):";
		cin>>number;
	
	error=Factor(number,squared,cubed);

	if(error)
		cout<<"Error encountered!"<<endl;
	else
	{
		cout<<"Number :"<<number<<endl
			<<"Squared:"<<squared<<endl
			<<"Cubed  :"<<cubed<<endl;
	}
}

int Factor(int n, int& rSquared, int& rCubed)
{
	if(n>20||n<0)
		return 1;
	rSquared=n*n;
	rCubed=n*n*n;
	return 0;
}

//*************************
//**      ch9_6.cpp      **
//*************************
#include <iostream.h>

float tmp;

float fn1(float r)
{
	cout<<"in func1..."<<endl;
	tmp=r*r*3.14;
	cout<<"&tmp:"<<&tmp<<endl;
	cout<<"exit func1."<<endl;
	return tmp;
}

float& fn2(float r)
{
	cout<<"in func2..."<<endl;
	tmp=r*r*3.14;
	cout<<"&tmp:"<<&tmp<<endl;
	cout<<"exit func2."<<endl;
	return tmp;
}

void main()
{
	float a=fn1(5.0);
	const float& b=fn1(5.0);
	float c =fn2(5.0);
	float& d=fn2(5.0);

	cout<<"a:"<<a<<"	"<<"&a:"<<&a<<endl
		<<"b:"<<b<<"	"<<"&b:"<<&b<<endl
		<<"c:"<<c<<"	"<<"&c:"<<&c<<endl
		<<"d:"<<d<<"	"<<"&d:"<<&d<<endl;
	cout<<"again, b="<<b<<endl;
	cout<<"&tmp:"<<&tmp<<endl;
}

//a potential bug
#include <iostream.h>

float& fn2(float r)
{
	float tmp;
	cout<<"&tmp:"<<&tmp<<endl;
	tmp=r*r*3.14;
	cout<<"tmp="<<tmp<<endl;
	return tmp;
}

void main()
{
	float& d=fn2(5.0);
	cout<<"d="<<d<<endl;
	cout<<"&d:"<<&d<<endl;
	float a=d;
	cout<<"a="<<a<<endl;
}

//*************************
//**      ch9_7.cpp      **
//*************************
#include <iostream.h>

int array[6][4]={
	{60,80,90,75},
	{75,85,65,77},
	{80,88,90,98},
	{89,100,78,81},
	{62,68,69,75},
	{85,85,77,91}};

int getLevel(int grade[],int size);

void main()
{
	int typeA=0,typeB=0;
	int student=6;
	int gradesize=4;

	for(int i=0;i<student;i++)
		if(getLevel(array[i],gradesize))
			typeA++;
		else
			typeB++;
	
	cout<<"Number of type A is "<<typeA<<endl;
	cout<<"Number of type B is "<<typeB<<endl;
}

int getLevel(int grade[], int size)
{
	int sum=0;
	for(int i=0;i<size;i++)
		sum+=grade[i];
	sum/=size;

	if(sum>=80)
		return 1;
	else
		return 0;
}

//*************************
//**      ch9_8.cpp      ** 
//*************************
//-------------version1-------------//

#include <iostream.h>

int array[6][4]={
	{60,80,90,75},
	{75,85,65,77},
	{80,88,90,98},
	{89,100,78,81},
	{62,68,69,75},
	{85,85,77,91}};

void level(int grade[],int size,int& tA,int& tB);

void main()
{
	int typeA=0,typeB=0;
	int student=6;
	int gradesize=4;

	for(int i=0;i<student;i++)
		level(array[i],gradesize,typeA,typeB);
	
	cout<<"Number of type A is "<<typeA<<endl;
	cout<<"Number of type B is "<<typeB<<endl;
}

void level(int grade[], int size, int& tA, int& tB)
{
	int sum=0;
	for(int i=0;i<size;i++)
		sum+=grade[i];
	sum/=size;

	if(sum>=80)
		tA++;
	else
		tB++;
}

//-------------version2-------------//
#include <iostream.h>

int array[6][4]={
	{60,80,90,75},
	{75,85,65,77},
	{80,88,90,98},
	{89,100,78,81},
	{62,68,69,75},
	{85,85,77,91}};

int& level(int grade[],int size,int& tA,int& tB);

void main()
{
	int typeA=0,typeB=0;
	int student=6;
	int gradesize=4;

	for(int i=0;i<student;i++)
		level(array[i],gradesize,typeA,typeB)++;
	
	cout<<"Number of type A is "<<typeA<<endl;
	cout<<"Number of type B is "<<typeB<<endl;
}

int& level(int grade[], int size, int& tA, int& tB)
{
	int sum=0;
	for(int i=0;i<size;i++)
		sum+=grade[i];
	sum/=size;

	if(sum>=80)
		return tA;
	else
		return tB;
}

//*************************
//**      ch9_9.cpp      ** 
//*************************
#include <iostream.h>

double* fn(const double* pd)
{
	static double ad=32;cout<<"ad:"<<ad<<endl;
	ad+=*pd;
	cout<<"fn being called... the value is: "<<*pd<<endl;
	return &ad;
}

void main()
{
	double a=345.6;
	const double* pa=fn(&a);
	cout<<*pa<<endl;
	a=55.5;
	pa=fn(&a);
	cout<<*pa<<endl;
}

//**************************
//**      ch9_10.cpp      ** 
//**************************
#include <iostream.h>

double& fn(const double& pd)
{
	static double ad=32;cout<<"ad:"<<ad<<endl;
	ad+=pd;
	cout<<"fn being called... the value is: "<<pd<<endl;
	return ad;
}

void main()
{
	double a=345.6;
	double& pa=fn(a);
	cout<<pa<<endl;
	a=55.5;
	pa=fn(a);
	cout<<pa<<endl;
}

//**************************
//**      ch9_11.cpp      ** 
//**************************
#include <iostream.h>

int CircleArea()
{
	double* pd=new double;
	if(!pd)
	{
		cout<<"error memory allocation!"<<endl;
		return 1;
	}
	double& rd=*pd;
	cout<<"the radius is :";
	cin>>rd;
	cout<<"the area of circle is "<<rd*rd*3.14<<endl;
	delete &rd;
	return 0;
}

void main()
{
	if(CircleArea())
		cout<<"program failed."<<endl;
	else
		cout<<"program successed."<<endl;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -