C++面向对象程序设计第一章部分课后习题

七、

由于一个函数不能既作为重载函数又作为默认参数的函数,所以我这里设置的两个函数名不同,分别为max和max2

详细C++代码如下:

#include<iostream>
using namespace std;

int max(int a, int b, int c)
{
	return a > b ? a : (b > c ? b : c);
}

int max2(int a, int b)
{
	return a > b ? a : b;
}

int main()
{
	int max(int a, int b, int c = 10);
	int max2(int a, int b = 20);
	cout << "please input a, b, c:";
	int a, b, c;
	cin >> a >> b >> c;
	int tmp = max(a, b);
	cout << tmp<< endl;
	tmp = max2(c);
	cout << tmp << endl;
	return 0;
}

我的样例运行结果如下:
在这里插入图片描述

八、

我这里既用了普通引用,也用了形参引用

详细C++代码如下:

#include<iostream>
using namespace std;

void swap(int &a, int &b)
{
	int tmp;
	tmp = a;
	a = b;
	b = tmp;
}

int main()
{
	cout << "Please input a and b:";
	int a, b;
	cin >> a >> b;
	cout << "------------------------" << endl;
	int &c = a;
	int &d = b;
	int tmp;
	if(c > d)
	{
		tmp = c;
		c = d;
		d = tmp;
	}
	cout << c << " " << d << endl;
	cout << "------------------------" << endl;
	cout << "Please input a and b:";
	cin >> a >> b;
	if(a > b)
		swap(a, b);
	cout << "------------------------" << endl;
	cout << a << " " << b << endl;
	return 0;
}

我的样例运行结果如下:
在这里插入图片描述

九、

详细C++代码如下:

#include<iostream>
using namespace std;

void sort(int &a, int &b, int &c)
{
	int tmp;
	if(a > b)
	{
		tmp = a;
		a = b;
		b = tmp;
	}
	if(a > c)
	{
		tmp = a;
		a = c;
		c = tmp;
	}
	if(b > c)
	{
		tmp = b;
		b = c;
		c = tmp;
	}
}

int main()
{
	int a, b, c;
	cout << "please input a, b and c:";
	cin >> a >> b >> c;
	sort(a, b, c);
	cout << a << " "<< b << " " << c<< endl;
	return 0;
}

我的样例结果如下:
在这里插入图片描述

十、

详细C++代码如下:

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string str1 = "China";
	string str2 = "America";
	str1 = str1 + str2;
	cout << str1 << endl;
	return 0;
}

十一、

详细C++代码如下:

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string str1;
	cin >> str1;
	int i;
	int len = str1.length();
	for(i = len - 1; i >= 0; i--)
		cout << str1[i];
	return 0;
}

十二、

详细C++代码如下:

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string str[5];
	int i;
	for(i = 0; i < 5; i++)
	{
		cout << "Please input string" << i + 1 << ":";
		cin >> str[i];
		cout << "----------------------"<< endl;
	}
	cout << "Input Stop!" << endl;
	cout << "Ouput five strings:"<< endl;
	for(i = 0; i < 5; i++)
	{
		cout << "string" << i + 1 << ":" << str[i] << endl; 
	}
	int k;
	for(i = 0; i < 4; i++)
	{
		k = i;
		for(int j = i + 1; j < 5; j++)
		{
			if(str[k] > str[j])
			{
				k = j;
			}
		}
		string tmp;
		tmp = str[i];
		str[i] = str[k];
		str[k] = tmp;
	}
	cout << "Sorted Strings:" << endl;
	for(i = 0; i < 5; i++)
		cout << str[i] << "   ";
	return 0;
}

我的样例结果如下:
在这里插入图片描述

十三、

解法一、

#include<iostream>
using namespace std;

void sort(int a[], int n)
{
	int i, j, k, tmp;
	for(i = 0; i < n - 1; i++)
	{
		k = i;
		for(j = i + 1; j < n; j++)
		{
			if(a[k] > a[j])
				k = j;	
		} 
		tmp = a[i];
		a[i] = a[k];
		a[k] = tmp;
	}
}

void sort(float a[], int n)
{
	int i, j, k;
	float tmp;				//Important!!! 
	for(i = 0; i < n - 1; i++)
	{
		k = i;
		for(j = i + 1; j < n; j++)
		{
			if(a[k] > a[j])
				k = j;	
		} 
		tmp = a[i];
		a[i] = a[k];
		a[k] = tmp;
	}
}

void sort(double a[], int n)
{
	int i, j, k;
	double tmp;				//Important!!! 
	for(i = 0; i < n - 1; i++)
	{
		k = i;
		for(j = i + 1; j < n; j++)
		{
			if(a[k] > a[j])
				k = j;	
		} 
		tmp = a[i];
		a[i] = a[k];
		a[k] = tmp;
	}
}

int main()
{
	int n;
	cout << "please input n:";
	cin >> n;
	int a[n];
	float b[n];
	double c[n];
	cout << "------------------" << endl;
	cout << "please input "<< n <<" int:"; 
	for(int i = 0; i < n; i++)
	{
		cin >> a[i];
	}
	cout << "------------------" << endl;
	sort(a, n);
	cout << "output sorted int:";
	for(int i = 0; i < n; i++)
		cout << a[i] << "   ";
	 
	cout << "\n------------------" << endl;
	cout << "please input "<< n <<" float:"; 
	for(int i = 0; i < n; i++)
	{
		cin >> b[i];
	}
	cout << "------------------" << endl;
	sort(b, n);
	cout << "output sorted float:";
	for(int i = 0; i < n; i++)
		cout << b[i] << "   ";
		
	cout << "\n------------------" << endl;
	cout << "please input "<< n <<" double:"; 
	for(int i = 0; i < n; i++)
	{
		cin >> c[i];
	}
	cout << "------------------" << endl;
	sort(c, n);
	cout << "output sorted double:";
	for(int i = 0; i < n; i++)
		cout << c[i] << "   ";
	return 0;
}

方法二、

用指针来作为形参是因为只有不同类型的整数指针才能区分不同类型的整数,比方说int类型的数据只能对应int *的,float的只能对应float *的

详细C++代码如下:

#include<iostream>
using namespace std;

void sort(int *a, int n)
{
	int i, j, k, tmp;
	for(i = 0; i < n - 1; i++)
	{
		k = i;
		for(j = i + 1; j < n; j++)
		{
			if(a[k] > a[j])
				k = j;	
		} 
		tmp = a[i];
		a[i] = a[k];
		a[k] = tmp;
	}
}

void sort(float *a, int n)
{
	int i, j, k;
	float tmp;
	for(i = 0; i < n - 1; i++)
	{
		k = i;
		for(j = i + 1; j < n; j++)
		{
			if(a[k] > a[j])
				k = j;	
		} 
		tmp = a[i];
		a[i] = a[k];
		a[k] = tmp;
	}
}

void sort(double *a, int n)
{
	int i, j, k;
	double tmp;
	for(i = 0; i < n - 1; i++)
	{
		k = i;
		for(j = i + 1; j < n; j++)
		{
			if(a[k] > a[j])
				k = j;	
		} 
		tmp = a[i];
		a[i] = a[k];
		a[k] = tmp;
	}
}

int main()
{
	int n;
	cout << "please input n:";
	cin >> n;
	int a[n];
	float b[n];
	double c[n];
	cout << "------------------" << endl;
	cout << "please input "<< n <<" int:"; 
	
	for(int i = 0; i < n; i++)
	{
		cin >> a[i];
	}
	cout << "------------------" << endl;
	sort(a, n);
	cout << "output sorted int:";
	for(int i = 0; i < n; i++)
		cout << a[i] << "   ";
	 
	cout << "\n------------------" << endl;
	cout << "please input "<< n <<" float:"; 
	for(int i = 0; i < n; i++)
	{
		cin >> b[i];
	}
	cout << "------------------" << endl;
	sort(b, n);
	cout << "output sorted float:";
	for(int i = 0; i < n; i++)
		cout << b[i] << "   ";
		
	cout << "\n------------------" << endl;
	cout << "please input "<< n <<" double:"; 
	for(int i = 0; i < n; i++)
	{
		cin >> c[i];
	}
	cout << "------------------" << endl;
	sort(c, n);
	cout << "output sorted double:";
	for(int i = 0; i < n; i++)
		cout << c[i] << "   ";
	return 0;
} 

在这里插入图片描述

十四、

详细C++代码如下:

#include<iostream>
using namespace std;
template<typename T>
 
T sort(T* a, T n)
{
	int i, j, k;
	T tmp;
	for(i = 0; i < n - 1; i++)
	{
		k = i;
		for(j = i + 1; j < n; j++)
		{
			if(a[i] > a[j])
				k = j;	
		} 
		tmp = a[i];
		a[i] = a[k];
		a[k] = tmp;
	}
}

int main()
{
	int n;
	cout << "please input n:";
	cin >> n;
	int a[n];
	float b[n];
	double c[n];
	cout << "------------------" << endl;
	cout << "please input "<< n <<" int:"; 
	
 	for(int i = 0; i < n; i++)
 	{
 		cin >> a[i];
 	}
 	cout << "------------------" << endl;
 	sort(a, n);
 	cout << "output sorted int:";
 	for(int i = 0; i < n; i++)
 		cout << a[i] << "   ";
 		
 	cout << "\n------------------" << endl;
	cout << "please input "<< n <<" float:"; 
	for(int i = 0; i < n; i++)
	{
		cin >> b[i];
	}
	cout << "------------------" << endl;
	sort(b, (float)n);
	cout << "output sorted float:";
	for(int i = 0; i < n; i++)
		cout << b[i] << "   ";
		
	cout << "\n------------------" << endl;
	cout << "please input "<< n <<" double:"; 
	for(int i = 0; i < n; i++)
	{
		cin >> c[i];
	}
	cout << "------------------" << endl;
	sort(c, (double)n);
	cout << "output sorted double:";
	for(int i = 0; i < n; i++)
		cout << c[i] << "   ";
	return 0;
}

对比两个样例测试结果
在这里插入图片描述
在这里插入图片描述
如果对3个数据进行排序是没问题的,但是对6个却无法正确排序

大家发现问题在哪了吗,很简单,在选择排序里

正确C++代码如下:

#include<iostream>
using namespace std;
template<typename T>
 
T sort(T* a, T n)
{
	int i, j, k;
	T tmp;
	for(i = 0; i < n - 1; i++)
	{
		k = i;
		for(j = i + 1; j < n; j++)
		{
			if(a[k] > a[j])
				k = j;	
		} 
		tmp = a[i];
		a[i] = a[k];
		a[k] = tmp;
	}
}

int main()
{
	int n;
	cout << "please input n:";
	cin >> n;
	int a[n];
	float b[n];
	double c[n];
	cout << "------------------" << endl;
	cout << "please input "<< n <<" int:"; 
	
	for(int i = 0; i < n; i++)
	{
		cin >> a[i];
	}
	cout << "------------------" << endl;
	sort(a, n);
	cout << "output sorted int:";
	for(int i = 0; i < n; i++)
		cout << a[i] << "   ";
		
	cout << "\n------------------" << endl;
	cout << "please input "<< n <<" float:"; 
	for(int i = 0; i < n; i++)
	{
		cin >> b[i];
	}
	cout << "------------------" << endl;
	sort(b, (float)n);
	cout << "output sorted float:";
	for(int i = 0; i < n; i++)
		cout << b[i] << "   ";
		
	cout << "\n------------------" << endl;
	cout << "please input "<< n <<" double:"; 
	for(int i = 0; i < n; i++)
	{
		cin >> c[i];
	}
	cout << "------------------" << endl;
	sort(c, (double)n);
	cout << "output sorted double:";
	for(int i = 0; i < n; i++)
		cout << c[i] << "   ";
	return 0;
}

我的样例运行结果如下:
在这里插入图片描述

之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!

©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页