![](https://i-blog.csdnimg.cn/blog_migrate/3a022e168725f2552c7896228ff66b1d.png)
#include <iostream>
using namespace std;
class Num
{
int a;
public:
Num():a(0){}
void set(int a);
void Sum();
void Porduct();
void PrimeNumber();
};
//设置a的值
void Num::set(int a)
{
this->a = a;
}
//实现1-a的数字之和
void Num::Sum()
{
int sum = 0;
for(int i=1;i<=a;i++)
{
sum += i;
}
cout << "1到a的和 sum = " << sum << endl;
}
//实现1-a的数字乘积
void Num::Porduct()
{
int mul = 1;
for(int i=1;i<=a;i++)
{
mul *= i;
}
cout << "1到a的乘积 mul = " << mul << endl;
}
//输出1-a之间的所有质数
void Num::PrimeNumber()
{
int i = 0,j = 0;
int flag = 0;
cout << "1-a的所有质数:" ;
for(i=2;i<=a;i++)
{
flag = 0;
for(j=2;j<i;j++)
{
if(i%j == 0)
{
flag = 1;
}
}
if(flag == 0)
{
cout << i << " " ;
}
}
cout << endl;
}
int main()
{
Num n1;
n1.set(5);
n1.Sum();
n1.Porduct();
n1.PrimeNumber();
return 0;
}
![](https://i-blog.csdnimg.cn/blog_migrate/82c43ebcf2b2fc7481f79082b89acff0.png)
![](https://i-blog.csdnimg.cn/blog_migrate/97b418072959fc95adebedc1fb28ca20.png)
#include <iostream>
using namespace std;
class A
{
char c;
public:
A():c('0'){}
void MyGetChar();
};
class B
{
char c;
public:
B():c('a'){}
void MyGetChar();
};
//A的MyGetChar
void A::MyGetChar()
{
cout << this->c;
this->c ++;
if(this->c > '9')
{
this->c = '0';
}
}
//B的MyGetChar
void B::MyGetChar()
{
cout << this->c;
this->c ++;
if(this->c > 'z')
{
this->c = 'a';
}
}
int main()
{
A a1;
B b1;
for(int i=0;i<50;i++)
{
if(i<20)
{
a1.MyGetChar();
}
if(i<26)
{
b1.MyGetChar();
}
}
return 0;
}
![](https://i-blog.csdnimg.cn/blog_migrate/a5ff078d1179dc18c33cf49eb43721c6.png)
![](https://i-blog.csdnimg.cn/blog_migrate/8bb937f9843b963c35c8aa164cae2909.png)
#include <iostream>
#include <cstring>
using namespace std;
class Let
{
char c[20];
public:
Let(){}
void dump(string str)
{
int j=0;
for(int i=0;i<str.length();i++)
{
if((str.at(i)>='a' && str.at(i)<='z') || (str.at(i)>='A' && str.at(i)<='Z'))
{
c[j] = str.at(i);
j++;
}
}
int len = strlen(c);
for(int i=0;i<len;i++)
{
cout << c[i] ;
}
cout << endl;
}
};
class Num
{
char a[20];
public:
Num(){}
void dump(string str)
{
int j=0;
for(int i=0;i<str.length();i++)
{
if((str.at(i)>='0' && str.at(i)<='9'))
{
a[j] = str.at(i);
j++;
}
}
int len = strlen(a);
for(int i=0;i<len;i++)
{
cout << a[i] ;
}
cout << endl;
}
};
//排序函数
string sort(string str)
{
int i=0,j=0;
char temp = 0;
for(i=1;i<str.length();i++)
{
for(j=0;j<str.length()-i;j++)
{
if(str.at(j)>str.at(j+1))
{
temp = str.at(j);
str.at(j) = str.at(j+1);
str.at(j+1) = temp;
}
}
}
return str;
}
int main()
{
Let l;
Num n;
string arr;
cout << "输入字符串:" ;
cin >> arr;
string str = sort(arr);
l.dump(str);
n.dump(str);
return 0;
}
![](https://i-blog.csdnimg.cn/blog_migrate/7aaabede62a58549c6715423eaccdf7d.png)