第一章 简单编程 (2)
1.9 统计与替换字符串中的关键字
建立一个类WordNum,统计一个英文字符串中的英文单词个数。字符串中的各英文单i司以一个或多个空格分隔。如字符串"I am a student〃中的英文单词个数为4。
#include<iostream>
#include<string>
#include<cctype>//头文件
using namespace std;
class WordNum
{
public:
WordNum(string s)
{
num = 0;
str = s;
}
void process();
void print()
{
cout << str << endl;
cout << "num=" << num << endl;
}
private:
int num;
string str;
};
void WordNum::process()
{
int word = 1;//word为状态变量,标记一个单词的开始
for (int i = 0; i < str.size(); i++)
{
//if ((str[i]>='a'&&str[i] <= 'z'|| str[i] >= 'A'&&str[i] <= 'Z') && word)
if(isalpha(str[i])&&word)
{
++num;
word = 0;//单词开始后,将woixl罝为0,下一个字母就认为是该单词中的字母,单词个数不会加1
}
else
{
if (str[i]==' ')
{
word = 1;//査询到空格时,认为一个单词结束,将word置为1,若再碰到字母,则表示新的单词开始,单词个数加1
}
}
}
}
/*定义一个WORDNUM类的对象w,并用字符串“She is a nice girl”初始化对象w。
通过w调用成员函数统计并输出字符串中英文单词个数*/
int main()
{
WordNum w("She is a nice girl ");
w.process();
w.print();
system("pause");
return 0;
}
1.10 二维数组元素换位
试定义一个类Matrix,并完成测试工作。
//10、二维数组元素换位
#include<iostream>
#include<algorithm>
using namespace std;
#define M 3
#define N 4
class Martrix
{
public:
Martrix(int b[M][N])
{
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
a[i][j] = b[i][j];
}
}
}
void process();//换位处理
void show()
{
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
cout << a[i][j] << "\t";
}
cout << endl;
}
}
private:
int a[M][N];
};
void Martrix::process()
{
int max, min;
max = min = a[0][0];
int maxi, maxj, mini, minj;
maxi = maxj = mini = minj = 0;
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
if (max<a[i][j])
{
max = a[i][j];
maxi = i;
maxj = j;
}
if (min>a[i][j])
{
min = a[i][j];
mini = i;
minj = j;
}
}
}
swap(a[0][0], a[maxi][maxj]);
if ( mini == 0 && 0 == minj)
{
//swap(a[0][0], a[maxi][maxj]);
swap(a[M - 1][N - 1], a[maxi][maxj]);
}
else
{
//swap(a[0][0], a[maxi][maxj]);
swap(a[M - 1][N - 1], a[mini][minj]);
}
}
void main()
{
int b[M][N] = { {1,4,5,6},{7,2,10,11},{8,9,12,3} };
Martrix m(b);
m.process();
m.show();
system("pause");
}
1.11 二维数组循环右移
//1.11 二维数组循环右移
#include<iostream>
#include<algorithm>
using namespace std;
#define M 3
#define N 4
class TDarray
{
public:
TDarray(int b[M][N])
{
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
a[i][j] = b[i][j];
}
}
}
void move();//右移
void show()
{
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
cout << a[i][j] << "\t";
}
cout << endl;
}
}
private:
int a[M][N];
};
void TDarray::move()
{
int last_column;
for (int i = 0; i < M; i++)
{
last_column = a[i][N - 1];
for (int j = N-1; j > 0; j--)
{
a[i][j] = a[i][j - 1];
}
a[i][0] = last_column;
}
}
void main()
{
int b[M][N] = { { 1,2,3,4 },{ 5,6,7,8 },{ 9,10,11,12} };
TDarray m(b);
m.move();
m.show();
system("pause");
}
/*
//1.12.cpp 数组中数据线性变换
#include<iostream>
//#include<vector>
using namespace std;
class Data
{
public:
Data(double a[],double nem_min,double new_max)
{
this->new_min = nem_min;
this->new_max = new_max;
for (int i = 0; i < 10; i++)
{
this->a[i] = a[i];
}
}
void fun();
void print()
{
for (int i = 0; i < 10; i++)
{
cout << b[i] << "\t";
}
cout << endl;
}
private:
double a[10], b[10];
double new_min = 0, new_max = 0;
};
void Data::fun()
{
double min, max;
min = max = a[0];
for (int i = 1; i < 10; i++)
{
if (max<a[i])
{
max = a[i];
}
if (min>a[i])
{
min = a[i];
}
}
for (int i = 0; i < 10; i++)
{
b[i] = (a[i] - min) / (max - min)*(new_max - new_min) + new_min;
}
}
void main()
{
double a[10] = { 1,2,3,4,0,5,6,7,8,9 };
Data d(a,0,1);
d.fun();
d.print();
system("pause");
}
*/
//1.13.cpp自然数集中找合数 composite_number
#include<iostream>
//#include<vector>
using namespace std;
class Noprime
{
public:
Noprime(int n)
{
this->n = n;
p = new int[n];
}
~Noprime() { if(!p) delete []p; }
void Search();
void print()
{
for (int i = 0; i < n; i++)
{
cout << p[i] << "\t";
}
cout << endl;
}
private:
int n = 0;
//vector<int> v;
int* p=NULL;
bool isPrime(int x)
{
for (int i = 2; i <= sqrt(x); i++)
{
if (x%i == 0)return false;
}
return true;
}
};
void Noprime::Search()
{
for (int i = 3; ; i++)
{
int counter = 0;
for (int j = i; j < i+n; j++)
{
if (!isPrime(j))
p[counter++] = j;
else
{
break;
}
}
if (n == counter)
{
break;
}
}
}
void main()
{
Noprime num(10);
num.Search();
num.print();
system("pause");
}
/**/
//1.14.cpp 字符串交叉插入
#include<iostream>
#include<cstring>
using namespace std;
class String
{
public:
String(char *c)
{
size = strlen(c);
p = new char[size + 1];
for (int i = 0; i < size; i++)
{
p[i] = c[i];
}
p[size] = 0;
}
~String()
{
if (!p)
{
delete[]p;
}
}
void process(String s);
void print()
{
cout << p << endl;
}
private:
char *p;
int size;
};
void String::process(String s)
{
int new_size = size + s.size;
char *new_p = new char[new_size];
//int j=0;
for (int i = 0; i < new_size; i=i+2)
{
new_p[i] = p[i/2];
new_p[i + 1] = s.p[i/2];
}
new_p[new_size] = 0;
if (!p)
{
delete[]p;
}
p = new_p;
size = new_size;
}
void main()
{
//char *c = "sghduhj";
//char c[9];
//c[0] = 's'; c[1] = 'g'; c[2] = 'h'; c[3] = 0;
//cout << c << endl;
String s1("abcde"), s2("ABCDE");
s1.process(s2);
s2.process(s1);
s1.print();
s2.print();
system("pause");
}
//1.15.cpp 找出回文数 Palindrome
#include<iostream>
#include<vector>
using namespace std;
class Palindrome
{
public:
Palindrome(int low,int up)
{
this->low = low;
this->up = up;
}
void isPalinAndStore()
{
for (int i = low; i <= up; i++)
{
int x = i*i;
if (isPalin(x))
{
v.push_back(i);
}
}
}
void outputResults()
{
cout << "总个数为:" << v.size() << endl;
cout << "x" << "\t" << "x*x" << endl;
for (int i = 0; i < v.size(); i++)
{
cout << v[i] << "\t" << v[i] * v[i] << endl;
}
}
private:
int low, up;
vector<int> v;
bool isPalin(int x)
{
vector<int> v_tmp;
while (x)
{
v_tmp.push_back(x % 10);
x = x / 10;
}
for (int i = 0,j=v_tmp.size()-1; i <= j; i++,j--)
{
if (v_tmp[i]!=v_tmp[j])
{
return false;
}
}
return true;
}
};
void main()
{
Palindrome p(100, 1000);
p.isPalinAndStore();
p.outputResults();
system("pause");
}
/**/
// 1.16.cpp 二维数组中数的位置交换
#include<iostream>
#include<algorithm>
using namespace std;
#define M 3
#define N 4
class Martrix
{
public:
Martrix(int b[M][N])
{
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
a[i][j] = b[i][j];
}
}
}
void change();//调换处理
void show()
{
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
cout << a[i][j] << "\t";
}
cout << endl;
}
}
private:
int a[M][N];
};
void Martrix::change()
{
int max = a[0][0];
int maxj = 0;
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
if (max<a[i][j])
{
max = a[i][j];
maxj = j;
}
}
}
for (int i = 0; i < M; i++)
{
swap(a[i][0], a[i][maxj]);
}
}
void main()
{
int b[M][N] = { { 1,5,9,8 },{ 7,8,3,2 },{ 1,6,8,1 } };
Martrix m(b);
m.show(); cout << endl << endl;
m.change();
m.show();
system("pause");
}