第5周编程作业(miuafhiwu)

1编写字符串反转函数mystrrev(20分)
题目内容:

编写字符串反转函数mystrrev,该函数的功能是将指定字符串中的字符顺序颠倒(前变后,后变前)。然后再编写主函数验证之。注意,输入输出应在主函数中进行。

函数原型为 void mystrrev(char str[])

输入格式:

一个字符串,不会超过100个字符长,中间可能包含空格

输出格式:

输入字符串的反转后的字符串

注意:本题应使用字符数组实现,不能使用字符串处理库函数,不能使用string类。

输入样例:

Hello,everyone

输出样例:

enoyreve,olleH

//编写字符串反转函数mystrrev
#include<iostream>
using namespace std;
void mystrrev(char s[])
{
	char* p, * q;
	p = s;
	q = s;
	char temp;
	while (*q)
		q++;
	q = q - 1;//q指向字符串最后一个字符,不包括'\0'
	while (p < q)
	{		
		temp = *p;
		*p = *q;
		*q = temp;
		p++;
		q--;
	}//倒置操作
}
int mylength(char s[])
{
	int len = 0;
	while (s[len] != '\0')
		len++;
	return len;
}
int main()
{
	char a[101];
	cin.getline(a, 100);
	mystrrev(a);
	cout << a;
	return 0;
}

2编写一组求数组中最大最小元素的函数(20分)
题目内容:

编写一组求数组中最大最小元素的函数。该组函数的原型为
int imax(int array[], int count); // 求整型数组的最大元素
int imin(int array[], int count); // 求整型数组的最小元素
其中参数count为数组中的元素个数,函数的返回值即为求得的最大或最小元素之值。要求同时编写出主函数进行验证。

输入格式:

第一个数为数组元素的个数(1-100),后面为所有数组元素。

输出格式:

分两行输出,第一行为最大值,第二行为最小值

输入样例:

5
90 89 30 45 55

输出样例:

90

30

//编写一组求数组中最大最小元素的函数
#include<iostream>
using namespace std;
int imax(int a[],int n)// 求整型数组的最大元素
{
	int max = -9999;
	for ( int i = 0; i < n; i++)
	{
		if (a[i] > max)
		{
			max = a[i];
		}
	}
	return max;
}
int imin(int a[], int n) // 求整型数组的最小元素
{
	int min = 9999;
	for (int i = 0; i < n; i++)
	{
		if (a[i] < min)
			min = a[i];
	}
	return min;
}
int main()
{
	int n;
	cin >> n;
	int a[101];
	for (int i = 0; i < n; i++)
		cin >> a[i];
	cout << imax(a,n) << endl;
	cout << imin(a,n) << endl;
	return 0;
}

3编写函数判断一个整数是否为素数(20分)
题目内容:

编写函数int isprime(int a);用来判断整数a是否为素数,若是素数,函数返回1,否则返回0。调用该函数找出任意给定的n个整数中的素数。 注意,1不是素数。
输入格式:

一系列待判断的正整数,以空格隔开,以0表示输入结束。

输出格式:

只输出输入的正整数中的素数,顺序和输入一致。数据间以一个英文空格隔开,最后一个数据后没有空格!!! 注意,1不是素数。

输入样例:

9 8 7 210 101 0

输出样例:

7 101

//编写函数判断一个整数是否为素数
#include<iostream>
using namespace std;
typedef struct Node{
	int data;
	struct Node* next;
}node;//链表
bool isprime(int a)
{
	bool sign = 1;
	if (a == 1)
		return false;
	for (int i = 2; i < a; i++)
	{
		if (a % i == 0)
		{
			sign = false;
			break;
		}
	}
	return sign;
}
int main()
{
	node* head = NULL;
	node* q=new node, * t;
	int num;
	cin >>num;
	do {
		cin >> num;
		if (num)
		{
			node* p = new node;
			p->data = num;
			p->next = NULL;
			if (head == NULL)
			{
				head = p;
			}
			else
			{
				q->next = p;;
			}
			q = p;
		}
	} while (num);
	t = head;
	int cnt = 0;
	while (t)
	{
		if (isprime(t->data))
		{
			if (cnt == 0)
				cout << t->data;
			else
				cout << " " << t->data;
			cnt++;
		}
		t = t->next;
	}
	cout << endl;
	return 0;
}

4编写函数去除字符串中包含的非字母字符(不包括空格),并将小写字母转换成大写字母(20分)
题目内容:

编写函数去除字符串中包含的非字母字符(不包括空格),并将小写字母转换成大写字母。

注意,不在函数中输出。输入输出应在主函数中进行。

输入格式:

待转换的字符串,字符串间会包含空格,长度不超过200。

输出格式:

转换后的字符串

注意:本题应使用字符数组实现,不能使用字符串处理库函数,不能使用string类。

输入样例:

happy new year!

输出样例:

HAPPY NEW YEAR

//编写函数去除字符串中包含的非字母字符(不包括空格),并将小写字母转换成大写字母
#include<iostream>
using namespace std;
void mydelete(char a[], char b[])
{
	char* p = a;
	char* q = b;
	int i = 0;
	while (*p)
	{
		if ((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z') || *p == ' ')
		{
			*q = *p;
			++q;
			i++;
		}
		p++;
	}
	b[i] = '\0';
}
void mychange(char s[])
{
	char* p=s;
	while (*p)
	{
		if (*p >= 'a' && *p <= 'z')
		{
			*p = *p + 'A' - 'a';
		}
		++p;
	}
}
int main()
{
	char a[101], b[101];
	cin.getline(a, 100);
	mydelete(a, b);
	mychange(b);
	cout << b << endl;
	return 0;
}

5编写函数计算一个英文句子中的单词个数(20分)
题目内容:

编写函数计算一个英文字符串中的单词个数。

输入格式:

一个最长500个字母的英文字符串,不包含数字和特殊字符,但可能包含一些英文标点符号(逗号、句点、问号)。标点符号独立出现时不视为一个单词。 单词间可能包含一个或多个空格。

输出格式:

该句子的单词个数

注意:本题应使用字符数组实现,不能使用字符串处理库函数,不能使用string类。

输入样例:

We hope everyone watches them with warmth.

输出样例:

7

//编写函数计算一个英文句子中的单词个数
#include <iostream>
using namespace std;
bool isword(char* p, char* q);
int  mycount(char str[])//分割字符串并且计数
{
	char* p = str;
	char* q = p;
	int counter = 0;
	while (*q)
	{
		while (*q != ' ' && *q)  q++;
		if (isword(p, q))	 counter++;
		p = q + 1;
		q = p;
	}
	return counter;
}
bool isword(char* p, char* q)//判断是不是单词
{
	bool sign = false;
	while (p != q)
	{
		if ((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z'))
		{
			sign = true;
			break;//只要有字母就是
		}
		p++;
	}
	return sign;
}

int main()
{
	char s[520];
	cin.getline(s, 520);
	int num = mycount(s);
	cout << num << endl;
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值