equal.cpp

来自「软件课程设计(1) 是一些非常经典的程序」· C++ 代码 · 共 47 行

CPP
47
字号
/////////////判断数组a与b的前n个元素值是否按下标对应完全相同,是则返回true////////////
///////////否则返回false。并编制主函数对它们进行调用,以验证其正确性//////////////////

#include<iostream>
using namespace std;

#define N 10

//递归
bool equ (char *a,char *b,int n)
{ 
   if(a[n-1]!=b[n-1]) return false;
   else if(n==1) return true;
   else equ(a,b,n-1);
}

//迭代
/*bool equ (char *a,char *b,int n)
{
   bool t;
   for(int i=0;i<n;i++)
   {
	   if(a[i]!=b[i]) {t=false;break;}
	   else t=true;
   }
   return t;
}*/

int main()
{
	char a1[N];char a2[N];
	int N1;
	cout<<"please input the number of the arrays:";
	cin>>N1;
	cout<<"please input the first array:";
	for(int j=0;j<N1;j++)
		cin>>a1[j];
    cout<<"\nplease input the other array:";
	for(j=0;j<N1;j++)
		cin>>a2[j];
	int m;
	cout<<"\nPlease input how many numbers do you want to compare:";
    cin>>m;
	if(equ(a1,a2,m))cout<<"\n\nabsolutely the same!\n\n";
	else cout<<"\n\ntwo arrays are different!\n\n";
	return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?