分类: C/C++
2011-08-04 20:07:40
1.设计一个用户类user,
具有成员变量
uid(用户id)
pwd(用户口令)
counter(当前登录用户数),
成员函数
login(uid,pwd)(登录)
logout()
showusers()(显示当前登录的用户数)
/*
* 模拟多用户登陆
*
* Created on: 2011-8-4
* Author: lzy
*/
#include
#include
using namespace std;
class User { /*类声明部分*/
private:
const static char uid[], pwd[]; /*声明静态常量用户名和密码*/
static int counter; /*计数用户变量*/
User() {
} /*构造函数声明为私有不充许在类外定义对象*/
public:
static User *login(); /*用户通过静态登陆函数生成对象*/
void logout(User *user); /*用户退出*/
void showusers() {
cout << "登陆用户个数:" << counter << endl;
}
};
/*初始化静态变量*/
const char User::uid[] = "root";
const char User::pwd[] = "123456";
int User::counter = 0;
/*
* 用户登陆程序
* 用户名与密码输入正确,则分配内存
* 登陆成功返回生成对象的地址
*/
User * User::login() {
char buf[10];
cout << "输入用户名:";
cin >> buf;
if (strcmp(uid, buf))
cout << "无此用户名" << endl;
else { /*用户名输入正确*/
cout << "输入密码:";
cin >> buf;
if (strcmp(pwd, buf))
cout << "密码错误!" << endl;
else { /*密码输入正确*/
counter++; /*用户计数加一*/
cout << counter << "号用户登陆成功:" << endl;
User *p = new User; /*给指针分配内存空间*/
return p;
}
}
return NULL; /*登陆失败返回空*/
}
/*
* 用户退出实现函数
* 入口参数:user->对象地址
*/
void User::logout(User *user) {
cout << counter << "号用户退出登陆!" << endl;
counter--; /*用户计数减一*/
delete user;
}
int main(void) {
User *people[10]; /*定义User用户指针数组大小为10,最多充许有10个用户登陆*/
User *user; /*临时User对象指针变量*/
int chose, counter = 0;
while (1) {
cout << "\n 1 -> 登陆\t" << "2 -> 退出\t" << "3 -> 登陆数:" << endl << " > ";
cin >> chose;
switch (chose) {
case 1:
user = User::login(); /*用户登陆*/
if (user != NULL
)
people[counter++] = user; /*保存对象地址*/
break;
case 2:
user->logout(people[--counter]); /*退出登陆,*/
if (counter == 0) {
cout << "程序已退出!";
return 0;
}
break;
case 3:
user->showusers(); /*显示用户登陆数*/
break;
}
}
return 0;
}
2.一个简单允许客户透支的银行信用卡系统的设计要求如下:
(1) 客户余额的输出采用类似n元n角n分这样的形式。
(2) 允许客户存款、取款和转账,并允许客户取款时透支,即允许客户欠款。
(3) 设计一个主程序进行基本情况的演示。
银行信用卡客户的操作允许存款、取款、转账和输出,并允许取款时透支。因此,银行信用卡类应该有客户账号、姓名和余额成员变量,应该有存款、取款、转账和输出成员函数
/*
* Created on: 2011-8-4
* Author: lzy
*/
#include
#include
using namespace std;
class CreditCard {
private:
float money;
public:
CreditCard() :
money(0) {
}
void Deposit(); /*存款*/
void Withdrawals(); /*取款*/
void Transfer(); /*转账*/
void Output(); /*查寻余额*/
};
void CreditCard::Deposit() {
int temp;
cout << "输入要存金额:";
cin >> temp;
money += temp;
cout << "存款成功!" << endl;
}
void CreditCard::Withdrawals() {
int temp;
cout << "输入要取金额:";
cin >> temp;
money -= temp;
cout << "取款成功!" << endl;
}
void CreditCard::Transfer() {
int temp;
cout << "输入账号:";
cin >> temp;
cout << "输入要转金额:";
cin >> temp;
money -= temp;
cout << "转账成功!" << endl;
}
void CreditCard::Output() {
cout << "余额:" << money << endl;
}
int main(void) {
CreditCard user;
int chose;
while (1) {
cout << "\n 1 ->存款\t" << "2 ->取款\t" << "3 ->转账\t" << "4 ->查寻余额\t"
<< "0 ->退出" << endl;
cin >> chose;
switch (chose) {
case 1:
user.Deposit();
break;
case 2:
user.Withdrawals();
break;
case 3:
user.Transfer();
break;
case 4:
user.Output();
break;
case 0:
cout << "程序退出!" << endl;
return 0;
}
}
return 0;
}
3.写一个类栈类,要求完成进栈与出栈
/*
* 栈顺序存储,压栈、出栈
*
* Created on: 2011-8-4
* Author: lzy
*/
#include
#include
using namespace std;
template<class T>
class SeqStack {
private:
int top, MaxSize;
T *stack; //栈数组
public:
SeqStack(int i) :MaxSize(i), top(-1) {stack = new T[MaxSize];}
~SeqStack() {delete[] stack;}
bool IsEmpty() const {return top == -1;}
bool IsFull() const {return top == MaxSize - 1;}
bool Push(const T &x); //元素x入栈
bool Pop(T &x); //元素出栈,保存在X
};
template<class T>
bool SeqStack<T>::Push(const T &x) {
if (IsFull() == true) { //判断栈是否已満
cout << "栈已满" << endl;
return false;
}
stack[++top] = x; //压栈
return true;
}
template<class T>
bool SeqStack<T>::Pop(T &x) {
if (IsEmpty() == true) {
cout << "栈已空" << endl;
return false;
}
x = stack[top--];
return true;
}
int main(void) {
SeqStack<int> sq(10);
int temp;
cout << "压栈:";
for (int i = 0; i < 10; i++) {
sq.Push(i);
cout << '\t' << i;
}
cout << "\n出栈:";
for (int i = 0; i < 10; i++) {
sq.Pop(temp);
cout << '\t' << temp;
}
return 0;
}
4.写一个冒泡排序的类
提示:
a.一个整型数组
b.对数组中的元素进行冒泡排序
#include
#define MAXSIZE 10
using namespace std;
template <class T>
class Sort
{
public:
void quSort(T num[], int size);
};
template <class T>
void Sort
{
int i, j;
T temp;
for(i=size-1; i>0; i--)
{
for(j=0; j
{
if(num[j]>num[j+1])
{
temp = num[j];
num[j] = num[j+1];
num[j+1] = temp;
}
}
}
}
int main(void)
{
int num[MAXSIZE];
for(int i=0; i
num[i] = rand()%100;
Sort<int> a;
a.quSort(num, MAXSIZE);
for(int i=0; i
cout<
cout<
return 0;
}
另一种排序方法:
template <class T>
void Sort
{
int i, j;
T *p, temp;
for(i=size; i>0; i--)
{
for(p = num, j=0; j
{
*p < num[j] ? p=num+j:NULL; /*P指针指向最大值的地址*/
}
/*数据交换*/
temp = *p;
*p = num[j-1];
num[j-1] = temp;
}
}