Chapeter10编程题目练习(04737课后题)

第一章:
1、分别用字符和ASCII码形式输出参数值为65和66

void printASCII()
{
	cout << "01" << endl;

	char a = 'A', b = 'B';
	int ascii_1 = 65, ascii_2 = 66;//ASCII码中的5和6
	cout << "字符输出:" << a << "," << b << endl;
	cout << "ASCII码输出:" << '\t';
	cout << (char)ascii_1 << '\t' << (char)ascii_2 << endl;
	cout << endl;
}

2、编写一个为int型变量分配100个整型量空间的程序

void func2()
{
	int* p = new int[100];
	delete p;
	cout << endl;
}

3、编写完整的程序,它读入15个float值,用指针吧它们存放在一个存储块里,然后输出这些值的和以及最小值

void func3()
{
	const int num = 7;
	float a[num];
	cout << "Please insert 15 numbers." << endl;

	float sum = 0;
	float min = a[0];
	for (int i = 0; i < num; i++)
	{
		if (min > a[i])
		{
			float temp = min;
			min = a[i];
			a[i] = temp;
		}
		sum += a[i];
	}
	cout << "和为:" << sum << endl;
	cout << "最小值为:" << min << endl;
	cout << endl;
}

4、声明如下数组:int a[] = { 1,2,3,4,5,6,7,8 };
先查找4的位置,将数组a复制给数组b,将数组a的内容反转,再查找4的位置,最后分别输出数组a和b的内容

int func4Find(int a[], int len, int key)
{
	for (int i = 0; i < len; i++)
	{
		if (a[i] == key)
		{
			//此处输出的是4的位置而不是索引
			return i + 1;
		}
	}
	return 0;
}

void printArray(int a[], int len)
{
	for (int i = 0; i < len; i++)
	{
		cout << a[i] << "\t";
	}
}

void func4()
{
	const int num = 8;
	int a[num] = { 1,2,3,4,5,6,7,8 };
	//先查找4的位置
	//cout << func4Find(a, num, 4) << endl;
	int* p = find(a, a + num, 4);
	cout << *p << endl;
	//将数组a复制给数组b
	int b[num];
	copy(a, a + num, b);
	//将数组a的内容反转
	reverse(a, a + num);
	//输出a,b数组内容
	cout << "这是数组a:" << endl;
	printArray(a, num);
	cout << endl;
	cout << "这是数组b:" << endl;
	printArray(b, num);
	cout << endl;
}

第二章:
1、使用多种方法编写将两个字符串连接在一起的程序

string push_back(string str1, string str2)
{
	//iterator迭代器,类似于指针
	//参考网址:https://blog.csdn.net/lq18811566072/article/details/82155903
	string::iterator iter = str2.begin();
	for ( ; iter != str2.end(); ++iter)
	{
		str1.push_back(*iter);
	}
	return str1;
}

void AddString(string str1, string str2) 
{
	string strp = str1 + str2;
	cout << strp << endl;

	string strp1 = str1.append(str2);
	cout << strp1 << endl;

	cout << "push_back: " << push_back(str1, str2) << endl;
}

2、已知一个string的对象str的内容为“We are here!”,使用多种方法输出字符“h”
方法:遍历数组找到h,使用指针去接收;find函数的返回值;

void FindStringValue() 
{
	string val = "We are here!";
	cout << val[val.find('h')] << endl;
	cout << val.substr(val.find('h'),1) << endl;
}


void Tip02ForMain() {
	AddString("我是第一条数据哎,", "我是第二条数据哎");
	FindStringValue();
}

第三章:
1、编写一个求方程ax2+bx+c=0的根的程序,用3个函数分别求当b2−4ac大于零、等于零、小于零时的方程的根。要求从主函数输入a、b、c的值并输出结果

void tip3func1(double a, double b, double c)
{
	double x1, x2;
	double delta = b * b - 4 * a * c;
	if (delta > 0)
	{
		x1 = (-b + sqrt(delta)) / (2 * a);
		x2 = (-b - sqrt(delta)) / (2 * a);
		cout << "当b2−4ac大于零时,x1 = "<< x1 <<"," << "x2 = " << x2 << endl;
	}
	else if(delta == 0)
	{
		x1 = x2 = (-b + sqrt(delta)) / (2 * a);
		cout << "当b2−4ac等于零时,x1 = x2 =" << x1 << endl;
	}
	else 
	{
		cout << "当b2−4ac小于零时,函数无解" << endl;
	}
}

2、定义函数up(ch),如字符变量ch是小写字母就转换成大写字母并通过up返回,否则字符ch不改变。要求在短小而完全的程序中显示这个程序是怎样被调用的

char up(char ch) 
{
	return toupper(ch);
}

void tip3func2(char ch)
{
	cout << up(ch) << endl;
}

4、编写有字符型参数C和整型参数N的函数,让它显示出由字符C组成的三角形。其方式为第1行有1个字符C,第2行有2个字符C等等

void print_Triangle(char C,int N)
{
	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j <= i ; j++)
		{
			cout << C;
		}
		cout << endl;
	}
}

void tip3func4()
{
	char c;
	int n;
	cout << "Plase enter c and n:" << endl;
	cin >> c >> n;
	print_Triangle(c, n);
}

5、编写一个求字符串长度的函数strlen(),再用strlen()函数编写一个函数revers(s)的倒序递归程序,使字符串s逆序

int strlen(string str) 
{
	//return str.length();
	return str.size();
}

void revers(string& str, int left, int right)
{
	int temp = 0;
	if (left >= right) return;

	temp = str[left];
	str[left] = str[right];
	str[right] = temp;

	revers(str, left + 1, right - 1);
}

void tip3func5()
{
	string str = "asdfadsfas";
	int len = strlen(str);
	cout << len << endl;
	revers(str, 0, len - 1);
	cout << str << endl;
}

6、用函数模板实现3个数值中按最小值到最大值排序的程序

template<typename T>
void SortFromSToB(T a[], int len) 
{
	for (int i = 0; i < len; i++)
	{
		for (int j = i + 1; j < len; j++)
		{
			if (a[i] > a[j])
			{
				T temp = a[i];
				a[i] = a[j];
				a[j] = temp;
			}
		}
	}
}

void tip3func6()
{
	int a[] = { 1,5,7,4,9,8,6 };
	int len = sizeof(a) / sizeof(a[0]);
	SortFromSToB(a, len);
	for (int i = 0; i < len; i++)
	{
		cout << a[i];
	}
}

7、利用函数模板设计一个求数组元素总和的函数,并检验之

template<typename T>
T AddEleSum(T a[], int len)
{
	double temp = 0;
	for (int i = 0; i < len; i++)
	{
		temp += a[i];
	}
	return temp;
}

void tip3func7()
{
	int a[] = { 1,5,7,4,9,8,6 };
	int len = sizeof(a) / sizeof(a[0]);
	cout << AddEleSum(a, len);
}

8、重载上题中的函数模板,使它能够进行两个数组的求和

template<typename T>
T AddDoubleEleSum(T a[], int len, T b[], int len_b)
{
	return AddEleSum(a, len) + AddEleSum(b, len_b);
}
void tip3func8()
{
	int a[] = { 1,5,7,4,9,8,6 };
	int len = sizeof(a) / sizeof(a[0]);

	int b[] = { 8,5,4,1,9,0,2,5,3 };
	int len_b = sizeof(b) / sizeof(b[0]);
	cout << AddDoubleEleSum(a, len, b, len_b);
}

猜你喜欢

转载自blog.csdn.net/qq_43036676/article/details/100739097