1、写一个程序判断字符串中数字的位置(不限制使用面向对象编程)例如:输入 a3b4c5,输出 2 4 6 。
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str;
cin >> str;
int num = str.length();
for (int i = 0; i < num; i++) {
if (str[i] >= '0'&& str[i] <= '9')
cout << i + 1 << " ";
}
system("pause");
return 0;
}
2、写一个类,能接受int 型的变量,接收变量后能存储原变量(譬如 12345)和其反向变量(54321) ,最多处理数量为 10 个,当输入达到 10 个或者输入变量为 0 的时候停止。并且在类销毁前输出存储的所有变量。 例如:
输入:12345,2234,0
输出:12345 54321
2234 4322
#include<iostream>
#include<vector>
using namespace std;
class INT{
private:
int a;
public:
INT() {};
INT(int i) :a(i) {};
void show() {
int s = a, i = 0;
int b[10];
while (s) {
b[i] = s % 10;
s = s / 10;
i++;
}
s = 0;
for (int j = 0; j < i; j++) {
s = s * 10 + b[j];
}
cout << s << endl;;
}
};
int main()
{
int a, i = 0;
vector<int> sa;
while (cin >> a) {
if (a == 0 || i == 10)
break;
else {
i++;
sa.push_back(a);
}
}
vector<int>::iterator num;
for (num = sa.begin(); num != sa.end();num++) {
INT s(*num);
cout << *num << " ";
s.show();
}
system("pause");
return 0;
}
3、写一个 CTriangle 类,要求可以接受CTriangle(y,x)形式的构造,创建在坐标系中的直角三角形样子如下:
A
| \
| \
| \
| \
B -------C
三点的坐标分别是 A(0,y)、B(0,0)、C(x,0)实现+运算,并且能够处理键盘连续输入若干个(少于十个)三角形,并且连加(相加时候三角形边长长度相加,方向同第一个三角形)。输入0 后结束并输出最后得出的三角形的三个坐标值
#include<iostream>
using namespace std;
class ctriangle {
private:
double x, y;
public:
ctriangle() {};
ctriangle(double i, double j) :x(i), y(j) {};
void showgrap() {
cout << "当前三角形的图案为:" << endl;
for (int i = 1; i <= y; i++) {
cout << "|";
for (double j = 1; j < i*(x / y); j++)
cout << " ";
cout << "\\"<<endl;
}
for (int k = 1; k <= x; k++)
cout << "—";
cout << endl;
}
ctriangle operator +(ctriangle aa) {
return ctriangle(aa.x + x, aa.y + y);
}
int showx() {
return x;
}
int showy() {
return y;
}
};
int main()
{
double x, y, a, b;
cin >> a >> b;
ctriangle s(a, b);
s.showgrap();
while (cin >> x >> y) {
if (x == 0)
break;
ctriangle c(x, y);
s = s + c;
s.showgrap();
}
cout << "A(0," << s.showy() << ") " << "B(0,0) " << "C(" << s.showx() << ",0)";
system("pause");
return 0;
}
网友评论