2006年北理复试上机题
1、写一个程序判断字符串中数字的位置(不限制使用面向对象编程)例如:输入 a3b4c5,输出 2 4 6 。
#include<iostream> #include<cstring> using namespace std; int main() { char s[200]; cin >> s; bool f = false; for (int i = 0; i < strlen(s); i++) { if (s[i] >= '0'&&s[i] <= '9') { if (f)cout << " " << i + 1; else { f = true; cout << i + 1; } } } return 0; }
2、写一个类,能接受int型的变量,接收变量后能存储原变量(譬如 12345)和其反向变量(54321) ,最多处理数量为 10 个,当输入达到 10 个或者输入变量为 0 的时候停止。并且在类销毁前输出存储的所有变量。 例如:
输入:12345 2234 0
输出:12345 54321
2234 4322
#include<iostream> #include<cstring> using namespace std; class INT { private: int a[10], cnt; public: INT() :cnt(0) {} void init() { while (cnt < 10) { cin >> a[cnt]; if (a[cnt] == 0)break; cnt++; } } void show() { for (int i = 0; i < cnt; i++) { cout << a[i] << " "; int r = 0, t = a[i]; while (t != 0) { r = r * 10 + t % 10; t /= 10; } cout << r << endl; } } }; int main() { INT v; v.init(); v.show(); return 0; }
3、写一个 CTriangle 类,要求可以接受CTriangle(y,x)形式的构造,创建在坐标系中的直角三角形样子如下:
A
| \
| \
| \
| \
B -------C
三点的坐标分别是 A(0,y)、B(0,0)、C(x,0),实现+运算,并且能够处理键盘连续输入若干个(少于十个)三角形,并且连加(相加时候三角形边长长度相加,方向同第一个三角形)。输入0 后结束并输出最后得出的三角形的三个坐标值。
#include<iostream> #include<cstring> using namespace std; class CTriangle { private: int y, x; public: CTriangle(int yy, int xx) :y(yy), x(xx) {} CTriangle operator +(CTriangle r) { CTriangle t(y + r.y, x + r.x); return t; } void draw() { for (int i = 0; i < y; i++) { cout << "|"; for (int j = 0; j < i; j++)cout << " "; cout<<"\\"<<endl; } for (int i = 0; i <= x; i++)cout << "-"; cout << endl; } void show() { cout << "此时三角形顶点坐标是:A(0," << y << "),B(0,0),C(" << x << ",0)" << endl; } }; int main() { int y, x; cout << "请输入三角形初始坐标:" << endl; cin >> y >> x; CTriangle ct(y, x); ct.show(); ct.draw(); cout << "请输入相加的三角形初始坐标,输入0 0即结束:" << endl; while (cin>>y>>x) { if (y == 0 || x == 0)break; CTriangle c(y, x); ct = ct + c; } ct.show(); ct.draw(); return 0; }
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号