1、建立点的类,具有数据成员x,y、构造函数、成员函数show()输出点的坐标,测试这个类,求出两点之间距离。
2.从键盘读入一个5*5二维数组,统计该数组的能被3整除的数并显示在屏幕上。
3.键盘随机输入9个整数,找出其中最大数和最小数所在的位置,并把两者对调,然后输出调整后的9个数。
4.从键盘随机输入包括字母、数字的长度不超过15个字符组成的字符串,将其中的数字挑出,组成新的字符串并将此字符串输出。
#include <cstdlib>
#include <iostream>
using namespace std;
class Point
{
private:
int x, y;
public:
Point(int x = 0, int y = 0) : x(x), y(y){};
void show()
{
cout << "(" << x << "," << y << ")";
}
float len(Point &bp)
{
int a = abs(bp.x - x);
int b = abs(bp.y - y);
return sqrt(a * a + b * b);
}
};
void test1()
{
Point a(1, 2), b(4, 6);
a.show();
cout << endl;
cout << a.len(b) << endl;
}
//
void test2()
{
int a[5][5];
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
cin >> a[i][j];
}
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
if (a[i][j] % 3 == 0)
cout << a[i][j] << " ";
}
cout << endl;
}
}
//
void test3()
{
int a[9], max = 0, min = 0, t;
for (int i = 0; i < 9; i++)
{
cin >> a[i];
if (a[i] > a[max])
max = i;
if (a[i] < a[min])
min = i;
}
t = a[min];
a[min] = a[max];
a[max] = t;
for (int i = 0; i < 9; i++)
cout << a[i] << " ";
}
//
void test4()
{
char str[15] = {0}, ch;
int inx = 0;
for (int i = 0; i < 15; i++)
{
ch = cin.get();
if (ch >= '0' && ch <= '9')
str[inx++] = ch;
}
cout << str;
}
int main()
{
test1();
test2();
test3();
test4();
return 0;
}