C++ primer plus 第七章 读书笔记

       C++的指针属于比较难理解的内容,本章节中有相当一部分在讲述指针、数组的关系与运用。

7.1 函数基本知识

  1. 提供函数定义。分为两类函数有返回值的函数和无返回值的函数,无返回值的函数为void函数。
    void cheers(int n)
    {
        for (int i = 0; i < n; i++)
            cout<<"cheers=:";
        cout<<endl;
    }

    上述函数中int n意味调用函数cheers()时,需要将一个int类型参数传递给函数。

  2. 提供函数原型。调用函数之前需要在main函数之前声明函数。
    void fufu(float a, b)//此为不正确声明变量方式。
    void fufu(float a, float b)//正确声明方式。

     

  3. 调用函数。函数中定义的局部变量如x, n, i等,在另外一个函数中也定义这样的同名变量,不会带来冲突。

下属代码演示了一个接受两个参数的函数

#include <iostream>
using namespace std;

void n_chars(char, int);

int main()
{
	int times;
	char ch;
	cout << "enter a character:";
	cin >> ch;
	cout << "\n";
	while (ch!='q')
	{
		cout << "enter a integer:";
		cout << "enter a character:";
		cout << "\n";
		cin >> times;
		if (!cin)
		{
			cout << "alpha only!!!! please enter again." << endl;
			cin.clear();
			cin.ignore();//清空读取缓存
			continue;
		}
		n_chars(ch, times);
		cout << "enter another character or press q to quit." << endl;
		cin >> ch;
	}
	cout << "the value of times is " << times << endl;

	return 0;
}

void n_chars(char c, int n)//不停输出c的值,直到n==0
{
	while (n-- > 0)
	{
		cout << c;
	}
	cout << endl;
}

形参跟局部变量的区别。

彩票中奖概率计算程序。

#include <iostream>
using namespace std;

long double probability(unsigned numbers, unsigned picks);

int main()
{
	double total, choices;
	long double P;
	cout << "enter the total number of choices on the game card and\n"
		<< "the number of picks allowed." << endl;
	while ((cin>>total)&&(cin>>choices)&&(choices<=total))
	{
		P = probability(total, choices);
		cout << "you have a chance in " << P << "of winning." << endl;
		cout << "please enter anohter two numbers, or press non-digit to quit." << endl;
	
	}

	return 0;
}

long double probability(unsigned numbers, unsigned picks)//不停输出c的值,直到n==0
{
	long double R = 1.0;
	unsigned n;
	unsigned p;
	for ( n = numbers, p =picks ; p > 0; n--,p--)
	{
		R = R * (n / p);
	}
	return 1/R;
}

7.3.1 C++函数如何使用指针来处理数组

数组名即为指针,指向数组中第一个元素的地址

#include <iostream>
using namespace std;

const int s = 8;
int cookies[s];
int parameter;
int sum_arr(int arr[], int n);

int main()
{
	int cookies[s] = { 1,2,4,8,16,32,64,128 };
	cout << cookies << " = arry address." << sizeof(cookies) << " = sizeof cookies." << endl;
	int sum = sum_arr(cookies, s);
	cout << sum << "cookies eaten." << endl;
	sum = sum_arr(cookies, 3);
	cout << sum << "first three ate." << endl;
	sum = sum_arr(cookies+4, 4);
	cout << sum << "last four ate." << endl;
	while (1)
	{

	}
	return 0;
}

int sum_arr(int arr[], int n)//arr为指向数组的指针,sizeof(arr)输出指针的大小,此处为4。
{
	int total = 0;
	cout << arr << " = arr, " << sizeof(arr) << "= sizeof arr." << endl;
	for (int i = 0; i < n; i++)
	{
		total = total + arr[i];
	}
	return total;
}

上述代码中 main函数中cookies就是指向cookies数组的指针。

#include <iostream>
int c_in_str(const char * str, char ch);

int main()
{
	using namespace std;
	char mmm[15] = "minimum";
	const char *wail = "ululate";
	int ms = c_in_str(mmm, 'm');
	int us = c_in_str(wail, 'u');
	cout << ms << " m characters in " << mmm << endl;
	cout << us << " u characters in " << wail << endl;
	getchar();
	getchar();
	return 0;
}

int c_in_str(const char * str, char ch)
{
	int count = 0;
	while (*str)
	{
		if (*str==ch)
			count++;
		str++;
	}
	return count;
}

上述代码中,const char *wail 如果不加const就无法编译通过;

#include <iostream>
char * buildstr(char c, int n);

int main()
{
	using namespace std;
	char ch;
	int times;
	cout << "enter an character:";
	cin >> ch;
	cout << "enter an integer:";
	cin >> times;
	char *ps = buildstr(ch, times);
	cout << ps << endl;
	delete[] ps;
	ps = buildstr('+', 20);
	cout << ps << "-done-" << ps << endl;
	delete[] ps;
	getchar();
	getchar();
	return 0;
}

char * buildstr(char c, int n)
{
	char *pstr = new char[n + 1];
	pstr[n] = '\0'; //将字符串最后一个字符改成空值
	while (n-- > 0)
	{
		pstr[n] = c;
	}
	return pstr;
}

返回字符串的函数就必须在函数之前加上 “*” ,不加的话程序就不能编译完成,因为函数return的是一个指针

#include <iostream>
struct travel_time
{
	int hours;
	int mins;
};
const int mins_per_hr = 60;
travel_time sum(travel_time t1, travel_time t2);
void show_time(travel_time t);

int main()
{
	using namespace std;
	travel_time day1 = { 5, 45 };
	travel_time day2 = { 4, 55 };
	travel_time trip = sum(day1, day2);
	travel_time day3 = { 4, 32 };
	cout << "three day total: ";
	show_time(sum(trip, day3));
	getchar();
	return 0;

}
//将两次时间加到一起:A:1小时20份 B:1小时15分钟 则A+B为2小时35分钟
travel_time sum(travel_time t1, travel_time t2)
{
	travel_time total;
	total.mins = (t1.mins + t2.mins) % mins_per_hr;
	total.hours = t1.hours + t2.hours + (t1.mins + t2.mins) / mins_per_hr;
	return total;
}

void show_time(travel_time t)
{
	using namespace std;
	cout << t.hours << "hours, " << t.mins << "minutes\n";
}

struct的应用 struct名称 变量名 就是命名方式 。travel_time sum(travel_time t1, travel_time t2)表示sum函数接受两个travel_time参数,并且返回的是travel_time数值。

#include <iostream>
//声明polar rect 结构
struct polar
{
	double distance;
	double angle;
};
struct rect
{
	double x;
	double y;
};
void rect_to_polar(const rect *pxy, polar *pda);
void show_polar(const polar *pda);


int main()
{
	using namespace std;
	rect rplace;
	polar pplace;
	cout << "enter the x and y:";
	while (cin>> rplace.x,cin >> rplace.y)
	{
		rect_to_polar(&rplace, &pplace);
		show_polar(&pplace);
		cout << "next x and y(no_digits to quit)";
	}
	cout << "done." << endl;
	return 0;
}
//直角坐标系转换为极坐标系 void函数无返回值的需求 无return
//从rect结构中用指针读取数值并进行运算,用polar指针读取运算结果并赋予distance和angle
void rect_to_polar(const rect *pxy, polar *pda)
{
	using namespace std;
	pda->distance = sqrt(pxy->x*pxy->x + pxy->y*pxy->y);
	pda->angle = atan2(pxy->y, pxy->x);
}

void show_polar(const polar *pda)
{
	using namespace std;
	const double Rad_to_deg = 57.29577951;
	cout << "distance: " << pda->distance << endl;
	cout << "angle: " << pda->angle*Rad_to_deg << endl;
}

用指针读取struct类型数据,很精巧。

#include <iostream>
#include <string>
using namespace std;
const int Size = 5;
void display(const string sa[], int n);


int main()
{
	using namespace std;
	string list[Size];
	cout << "enter you string list, " << Size << " favorite astronomical sights." << endl;
	for (int i = 0; i < Size; i++)
	{
		cout << i + 1 << ":";
		getline(cin, list[i]);
	}
	cout << "display your list:"<<endl;
	display(list, Size);
	getchar();
	return 0;
}

void display(const string sa[], int n)
{
	for (int i = 0; i < n; i++)
	{
		cout << i + 1 << ":" << sa[i] << endl;
	}
}

在上述代码中list[size]的实际结构为list[fdaf,gdfdg,llkj,jiefjd,jifafe]。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值