C++ Primer Plus笔记: 2023.07.25

1.下列代码:

int age = 39 ;
const int * pt = &age ;
* pt += 1 ;
cin >> *pt ;

对pt的声明指出,指针pt指向了const int,因此不能用pt来进行值的修改,否则会导致如下错误:

error: assignment of read-only location ‘* pt’
    8 |  * pt += 1 ;
      |  ~~~~~^~~~

以及这样:

error: no match for ‘operator>>(operand types are ‘std::istream’ {aka ‘std::basic_istream<char>} and ‘const int’)
    9 |  cin >> *pt ;
      |  ~~~ ^~ ~~~
      |  |      |
      |  |      const int
      |  std::istream {aka std::basic_istream<char>}

2.C++禁止将const的地址赋给非const指针。否则:

error: invalid conversion from ‘const float*’ to ‘float*[-fpermissive]
   16 |  float * pm = &g_moon ;
      |               ^~~~~~~
      |               |
      |               const float*

3.在大多数情况下,数组名pa都是数组第一个元素的地址,即&pa[0]。因此,它是单个指针的地址。但&pa是整个数组(所有的元素块在一起)的地址。从数值上说,pa和&pa的值相同,但它们的类型不同。一种差别是,pa+1为数组中下一个元素的地址,而&pa+1为数组pa后面一个12字节内存块的地址(这里假设地址为4个字节,且数组有3个元素)。另一个差别是,要得到第一个元素的值,只需要对pa接触一次引用,但需要对&pa解除两次引用:

**&pa == *pa == pa[0] 

第七章编程题:
(1)

#include 
using namespace std ;

double x, y ;
double Harmonic_mean(double x, double y) ;

int main()
{
	cout << "Enter two numbers: " ;
	while(cin >> x >> y && x && y )
	{
		cout << "The harmonic mean is "
		     << Harmonic_mean(x, y) 
		     << endl << endl ;
		cout << "Enter two numbers: " ;
	}
}

double Harmonic_mean(double x, double y)
{
	return 2.0 * x * y / (x + y) ;
}

(2)

#include 
using namespace std ;

double Golf[10] = {0} ;
int count = 0 ;

void InputGrade() ;
void DisplayGrade() ;
void AverageDrade() ;

int main()
{
	cout << "Enter the grades of golf: " ;
	InputGrade() ;
	DisplayGrade() ;
	AverageDrade() ;
	return 0 ;
}

void InputGrade()
{
	while(count < 10 && cin >> Golf[count] )
		count ++ ;
}

void DisplayGrade()
{
	cout << "Show all of grades: " ;
	for (int i=0; i<count; i++)
		cout << Golf[i] << " " ;
	cout << endl ;
}

void AverageDrade()
{
	double sum = 0 ;
	for (int i=0; i<count; i++)
		sum += Golf[i] ;
	cout << "The average grade is " << sum/count << endl ;
}

(3)

#include 
using namespace std ;

struct Box
{
	char maker[40] ;
	float height ;
	float width ;
	float length ;
	float volume ;
} ;

void By_value(Box b) ;
void By_address(Box * b) ;

int main()
{
	Box box ;
	cout << "Enter the value of this structure's members:\n" ;
	cin.getline(box.maker, 40) ;
	cin >> box.height >> box.width >> box.length >> box.volume ;
	
	By_value(box) ;
	By_address(&box) ;
	By_value(box) ;
}

void By_value(Box b)
{
	cout << b.maker << " "
	     << b.height << " " 
	     << b.width << " "
	     << b.length << " "
	     << b.volume << endl ;
}

void By_address(Box * b)
{
	b->volume = b->height * b->width * b->length ;
}

(4)

#include 
using namespace std ;

long double Probability1(double field1, double pricks) ;
long double Probability2(double field2) ;

int main()
{
	double field1, field2 ;
	cout << "Enter final field number: " ;
	cin >> field1 ;
	cout << "Enter the final number of the second field: " ;
	cin >> field2 ;
	cout << "The probability of winning = "
	     << Probability1(field1, 5) * Probability2(field2)
	     << endl ;
}

long double Probability1(double field1, double pricks)
{
	long double result = 1.0 ;
	for(int n = field1, p = pricks; p>0; n--, p--)
	{
		result *= n/p ;
	}
	return (1/result) ;
}

long double Probability2(double field2)
{
	return (1/field2) ;
}

(5)

#include 
using namespace std ;

int Factorial(int n) ;

int main()
{
	int n ;
	cout << "Enter an integer: " ;
	while(cin >> n)
	{
		cout << "The result = "
		     << Factorial(n) << endl ;
		cout << "\nEnter an integer: " ;
	}
}

int Factorial(int n)
{
	if (n == 1)
		return 1 ;
	else return n * Factorial(n-1) ;
}

(6)

#include 
using namespace std ;

double * array = NULL ;
int Fill_array(double a[], int length) ;
void Show_array(double a[], int count) ;
void Reverse_array(double a[], int count) ;

int main()
{
	int length ;
	cout << "Please enter the length of array: " ;
	cin >> length ;
	array = new double[length] ; 
	int count = Fill_array(array, length) ;
	Show_array(array, count) ;
	Reverse_array(array, count) ;
	Show_array(array, count) ;
	Reverse_array(array+1, count-2) ;
    Show_array(array, count) ;
	delete [] array ;
}

int Fill_array(double a[], int length)
{
	int count = 0 ;
	cout << "Please enter double value to fill this array:\n" ;
	while (count < length && cin >> a[count])
		count ++ ;
	return count ;
}

void Show_array(double a[], int count)
{
	cout << "Show this array's content:\n" ;
	for (int i=0; i<count; i++)
		cout << a[i] << " " ;
	cout << endl ;
}

void Reverse_array(double a[], int count)
{
	double *begin = a, *end = a + (count-1) ;
	while (begin < end)
	{
		double temp ;
		temp = *end ;
		*end = *begin ;
		*begin = temp ;
		begin += 1 ;
		end -= 1 ;
	} 
}

(7)

#include 
using namespace std ;

const int Max = 5 ;
double * fill_array(double ar[], int limit) ;
void show_array(const double *begin, const double *end) ;
void revalue(double r, double *begin, double *end) ;

int main()
{
	double properties[Max] ;
	double * end = NULL ;		
	end = fill_array(properties, Max) ;
	show_array(properties, end) ;
	if (end)
	{
		cout << "Enter revaluation factor: " ;
		double factor ;
		while (!(cin >> factor))
		{
			cin.clear() ;
			while (cin.get() != '\n')
				continue ;
			cout << "Bad input; Please enter a number: " ;
		}
		revalue(factor, properties, end) ;
		show_array(properties, end) ;
	}
	cout << "Done.\n" ;
	cin.get() ;
	cin.get() ;
	return 0 ;
}

double * fill_array(double ar[], int limit)
{
	double temp, *end = ar ;
	int i ;
	for (i=0; i<limit; i++)
	{
		cout << "Enter value #" << (i+1) << ": " ;
		cin >> temp ;
		if (!cin)
		{
			cin.clear() ;
			while (cin.get() != '\n')
				continue ;
			cout << "Bad input; input process terminated.\n" ;
			break ;
		}
		else if (temp < 0) break ;
		ar[i] = temp ;
		end = ar + i ;
	}
	return end ;
}

void show_array(const double *begin, const double *end)
{
	int i = 0 ;
	for (const double * p = begin; p <= end; i++, p++)
	{
		cout << "Property #" << (i+1) << ": $" ;
                cout << *p << endl ;
	}
}

void revalue(double r, double *begin, double *end)
{
	for (double * p = begin; p <= end; p += 1)
		*p *= r ;
}

(8)
a:

#include 
#include 
using namespace std ;

const int Seasons = 4 ;
const char * Snames[4] = {"Spring", "Summer", "Fall", "Winter"} ;

void fill(double * pa) ;
void show(const double * da) ;

int main()
{
	double expenses[Seasons] ;
	fill(expenses) ;
	show(expenses) ;
	return 0 ;
}

void fill(double * pa)
{
	for (int i=0; i<Seasons; i++)
	{
		cout << "Enter " << Snames[i] << " expenses: " ;
		cin >> pa[i] ;
	}
}

void show(const double * da)
{
	double total = 0.0 ;
	cout << "\nEXPENSES\n" ;
	for (int i=0; i<Seasons; i++)
	{
		cout << Snames[i] << ": $" << da[i] << endl ;
		total += da[i] ;
	}
	cout << "Total Expenses: $" << total << endl ;
}

b:

#include 
using namespace std ;

const int Seasons = 4 ;
const char * Snames[4] = {"Spring", "Summer", "Fall", "Winter"} ;
struct Expense{
	double money[Seasons] ;
} ;

void fill(Expense * e) ;
void show(const Expense e) ;

int main()
{
	Expense e ;
	fill(&e) ;
	show(e) ;
	return 0 ;
}

void fill(Expense * e)
{
	for (int i=0; i<Seasons; i++)
	{
		cout << "Enter " << Snames[i] << " expenses: " ;
		cin >> e->money[i] ;
	}
}

void show(const Expense e)
{
	double total = 0.0 ;
	cout << "\nEXPENSES\n" ;
	for (int i=0; i<Seasons; i++)
	{
		cout << Snames[i] << ": $" << e.money[i] << endl ;
		total += e.money[i] ;
	}
	cout << "Total Expenses: $" << total << endl ;
}

(9)

#include 
using namespace std ;

const int SLEN = 30 ;
struct student {
	char fullname[SLEN] ;
	char hobby[SLEN] ;
	int ooplevel ;
} ;

int getinfo(student pa[], int n) ;
void display1(student st) ;
void display2(const student * ps) ;
void display3(const student pa[], int n) ;

int main()
{
	cout << "Enter class size: " ;
	int class_size ;
	cin >> class_size ;
	
	while (cin.get() != '\n')
		continue ;
	
	student * ptr_stu = new student[class_size] ;
	int entered = getinfo(ptr_stu, class_size) ;
	for (int i=0; i<entered; i++)
	{
		display1(ptr_stu[i]) ;
		display2(&ptr_stu[i]) ;
	}
	display3(ptr_stu, entered) ;
	delete [] ptr_stu ;
	cout << "Done\n" ;
	return 0 ;
}

int getinfo(student pa[], int n)
{
	int count = 0 ;
	cout << "Enter the information of Student "
	     << (count + 1) << ":" << endl ;
	while (count < n)
	{
		cout << "Name: " ;
		if((cin.get(pa[count].fullname, SLEN).get()) == -1)
			break ;
		cout << "Hobby: " ;
		cin.get(pa[count].hobby, SLEN).get() ;
		cout << "Ooplevel: " ;
	    cin >> pa[count].ooplevel ;
		cin.get() ;
		count ++ ;
		if (count < n)
			cout << "Enter the information of Student "
	             << (count + 1) << ":" << endl ;	
	}
	cout << endl ;
	return count ;
}

void display1(student st)
{
	cout << "Student's Name: " << st.fullname << endl ;
	cout << "Student's Hobby: " << st.hobby << endl ;
	cout << "Student's Ooplevel: " << st.ooplevel << endl ;
	cout << "*************************************" << endl ;
}

void display2(const student * ps)
{
	cout << "student's name: " << ps->fullname << endl ;
    cout << "student's hobby: " << ps->hobby << endl ;
    cout << "student's ooplevel: " << ps->ooplevel << endl ; 
	cout << "#####################################" << endl ;
}

void display3(const student pa[], int n)
{
	cout << "Show all of students' information: " << endl ;
	for (int i=0; i<n; i++)
	{
		cout << "Name of Student " << (i+1) 
		     << ": " << pa[i].fullname << endl ;
		cout << "Hobby of Student " << (i+1) 
             << ": " << pa[i].hobby << endl ;
		cout << "Ooplevel of Student " << (i+1) 
             << ": " << pa[i].ooplevel << endl ;
		cout << endl ;
	}
}

(10)

#include 
using namespace std ;

const int Choice = 4 ;

double calculate(double a, double b, double (*p)(double, double)) ;
double add(double x, double y) ;
double subtraction(double x, double y) ;
double multiplication(double x, double y) ;
double division(double x, double y) ;

int main()
{
	double (*pf[Choice])(double, double) = 
		{add, subtraction, multiplication, division} ;
	double x, y ;
	cout << "Enter two double values casually:\n" ;
	while (cin >> x && cin >> y)
	{
		for (int i=0; i<Choice; i++)
		{
			cout << "Calculate " << (i+1) << ": "
			     << calculate(x, y, pf[i]) << endl ;
		}
		cout << "\nEnter two double values casually:\n" ;
	}
}

double calculate(double a, double b, double (*p)(double, double))
{
	return p(a, b) ;
}

double add(double x, double y)
{
	return x + y ;
}

double subtraction(double x, double y)
{
	return x - y ;
}

double multiplication(double x, double y)
{
	return x * y ;
}

double division(double x, double y)
{
	return x/y ;
}

你可能感兴趣的:(C++,Primer,Plus,c++)