#include
#include
using namespace std;
int main()
{
int a[3][4];
srand(time(0));
for(int i = 0;i < 3; i++)
for(int j = 0; j < 4; j++ )
a[i][j] = rand();
for(int i = 0;i < 3; i++)
{
for(int j = 0; j < 4; j++ )
cout << a[i][j] << " ";
cout << endl;
}
int max[3],min[4];
for(int i = 0; i < 3; i++)
{
max[i] = a[i][0];
for(int j = 1; j < 4; j++)
if(a[i][j] > max[i])
max[i] = a[i][j];
}
for(int j = 0; j < 4; j++)
{
min[j] = a[0][j];
for(int i = 1; i < 3; i++ )
if(a[i][j] < min[j])
min[j] = a[i][j];
}
bool flag = false;
for(int i = 0;i < 3; i++)
for(int j = 0; j < 4; j++)
if(a[i][j] == max[i] && a[i][j] == min[j])
{
cout << "a[" << i << "][" << j << "] = " << a[i][j] << endl;
flag = true;
}
if(!flag)
cout << "don't have" << endl;
return 0;
}
#include
#include
using namespace std;
int main()
{
int a[3][4];
srand(time(0));
for(int i = 0;i < 3; i++)
for(int j = 0; j < 4; j++ )
a[i][j] = rand();
for(int i = 0;i < 3; i++)
{
for(int j = 0; j < 4; j++ )
cout << a[i][j] << " ";
cout << endl;
}
int max[3],min[4];
for(int i = 0; i < 3; i++)
{
max[i] = a[i][0];
for(int j = 1; j < 4; j++)
if(a[i][j] > max[i])
max[i] = a[i][j];
}
for(int j = 0; j < 4; j++)
{
min[j] = a[0][j];
for(int i = 1; i < 3; i++ )
if(a[i][j] < min[j])
min[j] = a[i][j];
}
bool flag = false;
for(int i = 0;i < 3; i++)
for(int j = 0; j < 4; j++)
if(a[i][j] == max[i] && a[i][j] == min[j])
{
cout << "a[" << i << "][" << j << "] = " << a[i][j] << endl;
flag = true;
}
if(!flag)
cout << "don't have" << endl;
return 0;
}
selfString.h
#ifndef SELFSRING_H
#define SELFSRING_H
#include
using namespace std;
class selfSring
{
friend void fun(const selfSring &,const selfSring &);
public:
selfSring(ifstream &);
virtual ~selfSring();
private:
char *dataStr;
int length;
};
#endif // SELFSRING_H
selfString.cpp
#include "selfSring.h"
#include
#include
#include
using namespace std;
const static int SIZE = 1000;
selfSring::selfSring(ifstream &input)
{
char *temp = new char[SIZE];
input.getline(temp,SIZE);
length = strlen(temp);
dataStr = new char[length];
strcpy(dataStr,temp);
delete[] temp;
}
void fun(const selfSring &str1,const selfSring &str2)
{
char s1[100][20] = {""}; //拆分str1[]句子,分解成单词保存在s1中
char s2[100][20] = {""}; //拆分str2[]句子,
char tstr1[str1.length];
char tstr2[str2.length];
strcpy(tstr1, str1.dataStr);
strcpy(tstr2, str2.dataStr);
char *temp; //分解句子str1
temp = strtok(tstr1," .");
int len1 = 0;
while(temp)
{
strcpy(&s1[len1++][0],temp);
temp = strtok(NULL," .");
}
//分解句子str2
temp = strtok(tstr2," .");
int len2 = 0;
while(temp)
{
strcpy(&s2[len2][0],temp);
len2++;
temp = strtok(NULL," .");
}
//保留公共单词
size_t maxlen = 0,count = 0;
char common[100][20] = {""};
char s[20];
for(int i = 0; i <= len1; i++) //小于等于长度
{
strcpy(s,&s1[i][0]);
for(int j = 0; j <= len2; j++) //小于等于长度
if(strcmp(s,&s2[j][0]) == 0)
{
strcpy(&common[count++][0],s);
if(maxlen < strlen(s))
maxlen = strlen(s);
}
}
//输出最长公共单词
for(int i = 0; i <= count; i++)
{
if(strlen(&common[i][0]) == maxlen)
cout << &common[i][0] << " ";
}
}
selfSring::~selfSring()
{
delete[] dataStr;
}
main.cpp
#include
#include
#include "selfSring.h"
using namespace std;
int main()
{
ifstream input("input.txt",ios::in);
if(!input)
{
cerr << "file cannot be opened!!!" << endl;
exit(1);
}
selfSring str1(input);
selfSring str2(input);
fun(str1,str2);
input.close();
return 0;
}
input.txt
hello world my friend mmnnbb
yes mmnnbb am you my father friend