C++练习题(1)

不使用库函数,实现strcpy功能

不使用库函数,复制源字符串到目标字符串中,即实现strcpy函数的功能。

思路:
1、函数的参数类型和返回类型肯定是字符指针,便于操作;
2、增加程序健壮性,对于参数需要进行检查,并且对于可能的错误进行异常抛出。
3、利用一个临时字符指针变量存放目标字符串的首地址。
4、将源字符串从首字母到最后的‘\0’赋值给目标字符串,最后返回临时字符指针变量。

#include <iostream>
using namespace std;

char* my_strcpy(char* strdes, const char* strsrc)
{
	if (NULL == strdes || NULL == strsrc) {
		throw "参数错误";
	}
	char* temp = strdes;
	while ((*strdes++ = *strsrc++) != '\0');
	return temp;
}
int main() {
	char a[20];
	const char b[] = "hello world!";
	my_strcpy(a, b);
	cout << a << endl;
	return 1;
}

char *a、char a[]、char a[20]都可以初始化长度可变的字符串,但是*a和char a[]初始化不能使用cin交互,而char a[20]可以通过cin交互。
*a会报使用了未初始化的局部变量,char a[]会报为a未知大小。

逆序输出字符串

不使用库函数,写一个函数,接收一个字符串作为输入参数,将其逆序输出。

思路:检查字符串‘\0’的位置,然后从该位置向前输出字符。

#include <iostream>
using namespace std;
void output_reverse_string(char a[20])
{
	if (NULL == a)  throw("error parameter");
	int i = 0,k;
	while (i < 20) {
		if (a[i] == '\0') {
			k = i-1;
			break;
		}
		i++;
	}
	cout << "reverst result is:";
	for(;k>=0;k--)cout << a[k];
	cout << endl;
}
int main() {
	char a[20];
	cout << "please input a string:";
	cin >> a;
	output_reverse_string(a);
	return 1;
}

可使用string库函数,写一个函数,接收一个字符串作为输入参数,将其逆序输出。

#include <iostream>
#include <string>
using namespace std;
int main() {
	char a[20];
	cout << "please input a string:";
	cin >> a;
	int k;
	k = strlen(a);
	for (; k >= 0; k--)cout << a[k];
	cout << endl;
	return 1;
}

对数组元素进行冒泡排序

使用冒泡法对数组元素排序

#include <iostream>
int* bubble(int *intlist, int len) {
	/*思想:每一趟将最大值放置最后面,即从前往后,只要前面的值大于后面的值,就交换位置,10个数就跑9躺*/
	int temp, i,j;
	for (i = 1; i < len; i++) {
		for (j = 0; j < len - i; j++) {
			temp = intlist[j];
			if (intlist[j] > intlist[j + 1]) {
				intlist[j ] = intlist[j+1];
				intlist[j + 1] = temp;
			}
		}
	}
	return intlist;
}
int main() {
	int a[] = {11,25,14,58,36,10,5,9,7,6 };
	int len;
	len = sizeof(a) / 4;
	for (int i = 0; i < len; i++)cout << a[i]<<" ";
	int* b = bubble(a,len);
	cout << endl;
	for (int i = 0; i < len; i++)cout << b[i]<< " ";
	return 1;
}

利用数组输出Fibonacci数列

斐波那契数列: F(1)=1,F(2)=1, F(n)=F(n - 1)+F(n - 2)(n ≥ 3,n ∈ N*)

#include <iostream>
int main() {
	int a[20] = { 1,1 };
	for (int i = 2; i < 20; i++)a[i] = a[i - 1] + a[i - 2];
	for (int j = 0; j < 20; j++)cout << a[j] << "  ";
	return 1;
}

判断闰年年份

闰年是能被四整除,但不能被100整除或者是可以被400整除。

#include <iostream>
bool leap_year(int year) {
	if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) return true;
	else return false;
}
int main() {
	int year;
	cout<< "please input year:";
	cin >> year;
	if (leap_year(year)) cout << endl << year << "是闰年";
	else cout << endl << year << "不是闰年";
	return 1;
}

输出1000以内的素数

素数:大于一的整数,且只能被1和自己整除。

思路:使用for循环的两层遍历,外层遍历从2到1000的所有可能是素数的数字,内层遍历用于筛选出属于素数的整数。

void mersenne(int rang) {
	for (int i = 2; i <= rang; i++) {
		static int flag = 1;
		for (int j = 2; j <= i/2; j++) {
			if (i % j == 0) {
				flag = 0;
				break;
			}
			else flag = 2;
		}
		if (flag > 0) cout << i << "  ";
	}
}
mersenne(1000)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章