XJTU2020秋C++第十次作业

本文展示了C++编程中的一些基本操作,包括查找字符串子串、转换字母大小写、计算最大公约数、分解偶数为素数之和、递归计算序列和、数组最小值、斐波那契数列、整数数组查找、去除字符串首尾空格等。这些实例涵盖了字符串处理、算法应用及基础数学概念,适合C++初学者巩固基础知识。
摘要由CSDN通过智能技术生成

目录

1. 编写函数int index(char t[], char s[]),用于确定字符串t是不是s的子串。若是,返回子串t在s中第一次出现时的第一个字符的下标;若不是,返回-1。编写主函数调用该函数查找子串。

2. 编写将字符串中所有小写字母转换为大写字母的函数。编写主函数调用该函数。

3. 编写函数,求两个数的最大公约数。编写主函数调用该函数。

4. 编写函数,从键盘上输入一个大于4的偶数,然后将从4开始到该数之间的所有偶数分解为两个素数之和,显示出每个整数的分解情况,例如: 4=2+2,6=3+3,8=3+5等。编写主函数调用该函数。

5. 编写求1+2+...+n的递归函数,然后使用主函数调用。

6. 编写求数组最小值的递归函数,然后使用主函数调用。

7. 编写求斐波那契数列前n项的递归函数,然后使用主函数调用。

8. 使用整数指针编写查找一个整数是否存在于整数数组中,找到,则返回下表,否则返回-1;然后使用主函数调用。

9. 使用字符指针编写函数去掉字符串尾部的空格,然后使用主函数通过具体字符串调用。

10. 使用字符指针编写函数去掉字符串首部的空格,然后使用主函数通过具体字符串调用。


1. 编写函数int index(char t[], char s[]),用于确定字符串t是不是s的子串。若是,返回子串t在s中第一次出现时的第一个字符的下标;若不是,返回-1。编写主函数调用该函数查找子串。

 

#include<iostream>
#include<cstring> 
using namespace std;
int find(char t[100],char s[100])
{
	int j=-1;
	int k;
	for(int i=0;i<strlen(s);i++)
	{
		j=i;
		for(k=0,j;k<strlen(t)&&s[j]==t[k];j++,k++);
	if(k==strlen(t))
		{
			return i;
		}	
	} 
	if(k!=strlen(t)) return -1;
}
int main()
{
	const int N=100;
	char t[N],s[N];
	cin>>t;
	cin>>s;
	cout<<find(t,s);
	return 0;
}

2. 编写将字符串中所有小写字母转换为大写字母的函数。编写主函数调用该函数。

 

#include<iostream>
using namespace std;
void tran(char *p)
{
	while(*p!='\0')
	{
		if(*p>='a'&&*p<='z')
		{
			*p=*p-'a'+'A';
		}
		p++;
	} 
}
int main()
{
	const  int N=100; 
	char x[N];
	cin.getline(x,N);
	char *p=x;
	tran(p);
	cout<<x;
 } 

3. 编写函数,求两个数的最大公约数。编写主函数调用该函数。

 

#include<iostream>
#include<cmath>
using namespace std;
//判断x,y大小 
//对x进行循环,for双循环 
void comp(int *x,int *y) 
{
	if(*x>*y)
	{
		int temp=*x;
		*x=*y;
		*y=temp;
	}
}
int common_divisor(int x,int y)
{
	int n=1;
	for(int i=1;i<=x;i++)
	{
		if(x%i==0&&y%i==0) n=i;
	}
	return n;
}
int main()
{
	int x,y;
	cin>>x;
	cin>>y;
	comp(&x,&y);
	//cout<<x<<endl<<y;
	cout<<common_divisor(x,y);
	return 0;
	
}

4. 编写函数,从键盘上输入一个大于4的偶数,然后将从4开始到该数之间的所有偶数分解为两个素数之和,显示出每个整数的分解情况,例如: 4=2+2,6=3+3,8=3+5等。编写主函数调用该函数。

#include<iostream>
using namespace std;
int isprime_num(int x)//判断是否为素数 
{
	int n=2;
	for(n;n<x;n++)
	{
		if(x%n==0) return 0;
	}
	if(n==x)
	return 1;
}
void split_num(int x)
{
	int a=1; 
	while(a<=x/2){
	if(isprime_num(a)&&isprime_num(x-a))
	{
	cout<<x<<'='<<a<<'+'<<x-a<<endl;
	}
	a++;
	}
}
int main()
{
	int x;
	cin>>x;
	if(x<=4||x%2!=0)
	{
		cout<<"您的输入不符合格式,请重新运行程序并且输入";return 0;
	}
	//cout<<isprime_num(x);
	split_num(x);
	return 0;
}

5. 编写求1+2+...+n的递归函数,然后使用主函数调用。

 

#include<iostream>
using namespace std;
int digui(int n)
{
	int sum=0;
	 while(n!=0)
	 {
	 	sum+=n;
	 	n--;
	 }
	 return sum;
}
int main()
{
	int n;
	cin>>n;
	cout<<digui(n); 
 } 

6. 编写求数组最小值的递归函数,然后使用主函数调用。

#include<iostream>
using namespace std;
int min_num_array(int x[100],int n)
{
	int i=0;
	int min=x[i];
	for(i;i<n;i++)
	{
		if(min>x[i])
		min=x[i];
	}
	return min;
	
}
int main()
{
	int x[100];
	int n=0;
	while (cin>>x[n])
	{
		n++;
		if(cin.get()=='\n') break;
	}
	cout<<"数组最小值为:"<<min_num_array(x,n);
	return 0;
}

7. 编写求斐波那契数列前n项的递归函数,然后使用主函数调用。

斐波那契数列fib(n)定义如下: fib(0)=0 ,fib(1)=1, fib(n)=fib(n-1)+fib(n-2)

 

#include<iostream>
using namespace std;
int fib(int n)
{
	if(n==0)
	return 0;
	else if(n==1)
	return 1;
	else if(n>1)
	return fib(n-1)+fib(n-2);
}
int main()
{
	int n;cin>>n;
	cout<<"fib"<<'('<<n<<')'<<'='<<fib(n);
	return 0;
}

8. 使用整数指针编写查找一个整数是否存在于整数数组中,找到,则返回下表,否则返回-1;然后使用主函数调用。

 

 #include<iostream>
using namespace std;
int find(int x[100],int n,int m)//
{
	for(int i=0;i<n;i++)
	{
		if(x[i]==m) return i;
	}
	return -1;
}
int main()
{
	int x[100];
	int n=0;
	int m;
	cout<<"请输入一串数字作为数组,回车键表示结束:";
	while(cin>>x[n])
	{
		n++;
		if(cin.get()=='\n')break;
	}
	cout<<"请输入要查找的数字:";
	cin>>m;
	cout<<find(x,n,m);
	return 0;
}

#include<iostream>
using namespace std;
int find(int *p,int n,int m)
{
	for(int i=0;i<n;p++,i++)
	{
		if(*p==m) return i;
	}
	return -1;
}
int main()
{
	int x[100];
	int*p=x;
	int n=0;
	int m;
	cout<<"请输入一串数字作为数组,回车键表示结束:";
	while(cin>>x[n])
	{
		n++;
		if(cin.get()=='\n')break;
	}
	cout<<"请输入要查找的数字:";
	cin>>m;
	cout<<find(p,n,m);
	return 0;
 } 

9. 使用字符指针编写函数去掉字符串尾部的空格,然后使用主函数通过具体字符串调用。

 

#include<iostream>
#include<cstring>
using namespace std;
void remove(char *p)
{
	while(*p==' ')
	{
		//cout<<'a'<<endl; 
		p--;
	}
	p++;
	*p='\0';
}
int main()
{
	char x[100];
	cin.getline(x,100);
	cout<<"初始字符串:"<<x<<"结尾"<<endl;
	int n=strlen(x);
	char *p=&x[n-2];
//	cout<<x;
	remove(p);
	cout<<"处理后字符串:"<<x<<"结尾";
	return 0;
}

10. 使用字符指针编写函数去掉字符串首部的空格,然后使用主函数通过具体字符串调用。

#include<iostream>
#include<cstring>
using namespace std;
char  *remove(char *p)
{
	while(*p==' ')
	{
		p++;
	}
	return p;
}
int main()
{
	char x[100];
	cin.getline(x,100);
	cout<<"初始字符串"<<x<<endl;
	char *p=x;
	p=remove(p);
	strcpy(x,p);
	cout<<"处理后字符串"<<x;
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值