关闭

c++上机实践题

919人阅读 评论(0) 收藏 举报

 1.Annie的宠物小屋里有12个笼子,每个笼子可以放不同的动物,包括猫,狗和蛇,但同一时刻一个笼子只能放0只或1只宠物,本题要求完成:
(1)实现一个简单的管理系统,可以增加,删除指定笼子中的宠物,查询每个笼子中存放宠物的情况,统计宠物的种类和数量.
(2)定义描述宠物小屋的类Shelves,其中包括12个笼子用于存放各种宠物;
(3)定义虚拟基类Animal,至少包含纯虚函数Showme;
(4)定义派生类Cat,Dog,Snake,具体实现上述纯虚函数showme,显示该宠物的种类,名称,着色,体重和喜欢的食物.
(5)重载输入>>操作符,使得可以通过cin直接读入宠物的着色,体重,和喜欢的食物;
(6)编写main函数,测上述所要求的各种功能;

//

#include<iostream>

#include"animal.h"
using std::cout;
using std::cin;
using std::endl;
int Cat::Catnum=0;
Cat::Cat()
{
 type=0;
 weight=0;

 name=new char[20];
 color =new char [20];
 food=new char [20];
 Catnum++;
 
}
void Cat::showme()
{
 cout<<"I am a Cat."<<endl;
 cout<<" My name is "<<name<<endl;
 cout<<" My color is "<<color<<endl;
 cout<<"My weight is "<<weight<<endl;
 cout <<" My favorite foood is "<<food <<endl;
 return ;

}
int Cat::number()
{
 return Catnum;

}
std::istream & operator>>(std::istream & is ,Cat &cat)
{
 cout<<" the cat's name is :";
 is>>cat.name;
 cout<<endl<<"the cat's color is:";
 is>>cat.color;
 cout<<endl<<" the cat's weight is :";
 is>>cat.weight;
 cout<<endl<<"the cat's fovarite food is :";
 cin>>cat.food;
 return is;

}
void Cat::deleteme()
{
 Catnum--;

}
Cat::~Cat()
{

}
int Dog::Dognum=0;
Dog::Dog()
{
 type=1;
 name=new char [20];
 color =new char [20];
 food=new char [20];
 weight=0;

 Dognum++;

}
void Dog::showme()
{
 cout<<endl<<"I am a Dog"<<endl;
 cout<<"My name is "<<name<<endl;
 cout<<" My color is "<<color<<endl;
 cout<<"My weight is  "<<weight<<endl;
 cout<<"My favorite food is "<<food<<endl;
 return ;

}
int Dog::number()
{
 return Dognum;

}
std::istream & operator>>(std::istream &is ,Dog &dog)
{
 cout<<"the dog's name is";
 is>>dog.name;
 cout<<endl<<" the dog's color is ";
 is>>dog.color;
 cout<<endl<<"the dog's weight is ";
 is>>dog.weight;
 cout<<endl<<"the dog's fovarite food is:";
 is>>dog.food;
 return is;


}
void Dog::deleteme()
{
 Dognum--;

}
Dog::~Dog()
{}
int Snake::Snakenum=0;
Snake::Snake()
{
 type=2;
 name=new char [20];
 color=new char [20];
 food=new char [20];
 Snakenum++;
}
void Snake::showme()
{
 cout<<endl<<"I am a Snake"<<endl;
 cout<<"My name is "<<name<<endl;
 cout<<" My color is "<<color<<endl;
 cout<<"My weight is  "<<weight<<endl;
 cout<<"My favorite food is "<<food<<endl;
 return ;


}
int Snake::number()
{
 return Snakenum;

}
std::istream & operator>>(std::istream &is ,Snake &snake)
{
 cout<<"the snake's name is";
 is>>snake.name;
 cout<<endl<<" the snake's color is ";
 is>>snake.color;
 cout<<endl<<"the snake's weight is ";
 is>>snake.weight;
 cout<<endl<<"the snake's fovarite food is:";
 is>>snake.food;
 return is;


}
void Snake::deleteme()
{
 Snakenum--;

}
Snake::~Snake()
{}

Shelves::Shelves()
{
 for(int i=0;i<12;i++)
 {
  shelves[i]=NULL;

 }
}
int Shelves::add(int n,Animal * animal)
{
 int successful=1;
 if(shelves[n]!=NULL)
 {
  cout<<"the "<<n<<" the shelve is full"<<endl;
  successful=0;
  return successful;

 }
 shelves[n]=animal;
 cout<<" successful!"<<endl;
 return successful;

}
void Shelves::query(int n)
{
 if(shelves[n]==NULL)
 {
  cout<<"the "<<n<<" the shelve is empty!"<<endl;
  return ;

 }
 shelves[n]->showme();
 return ;

}
void Shelves::Delete(int n)
{
 if(shelves[n]==NULL)
 {
  cout<<"the "<<n<<" shelves is empty"<<endl;
  return ;

 }
  shelves[n]->deleteme();
 shelves[n]=NULL;
 cout<<"successful!"<<endl;
 return ;

}
void Shelves::stat()
{
 cout<<"there ar "<<Cat::number()<<"cats"
 <<Dog::number()<<"dog,and"<<Snake::number()
  <<"snake."<<endl;
 return ;

}

//

#ifndef ANIMAL_H
#define ANIMAL_H

#include<iosfwd>
class Animal
{
public:
 virtual void showme()=0;
 virtual void deleteme()=0;
 ~Animal(){}
protected:
 char *name ;
 char *color;
 float weight;
 char *food;
 int type;

};
class Cat:public Animal
{
public:
 Cat();
 void showme();
 static int number();
 friend std::istream & operator >>(std::istream & is,Cat &cat);
 void deleteme();
 ~Cat();
private:
 static int Catnum;

};
class Dog:public Animal
{
public:
 Dog();
 void showme();
 static int number();
 friend std::istream & operator>>(std::istream & is,Dog &dog);
 void deleteme();
 ~Dog();
private:
 static int Dognum;

};
class Snake:public Animal
{
public:
 Snake();
 void showme();
 static int number();
 friend std::istream & operator>>(std::istream & is ,Snake &snake);
 void deleteme();
 ~Snake();
private:
 static int Snakenum;

};
class Shelves
{
public:
 Shelves();
 int add(int n,Animal *animal);
 void Delete(int n);
    void query(int n);
 void stat();
private:
 Animal * shelves[12];

};
#endif

//

#include<iostream>
#include"animal.h"
using std::cout;
using std::cin;
using std::endl;

int main()
{
 Shelves * shelves=new Shelves;
 Cat *cat;
 Dog *dog;
 Snake *snake;
 int n;
 int i=0;
 char ch;
 while(i!=5)
 {
  cout<<" please chiose(1-5):"<<endl;
  cout<<" 1:add an animal"<<endl;
  cout<<"2:delete an animal"<<endl;
  cout<<" 3:query"<<endl;
  cout<<"4:stat"<<endl;
  cout<<"5:exit"<<endl;
  cin>>i;
  switch(i)
  {
  case 1:
   cout<<"which cage?";
   cin>>n;
   cout<<"c:cat d:dog s:snake"<<endl;
   cout<<"please choise an animal:";
   cin>>ch;
   switch(ch)
   {
   case'c':
    cat=new Cat;
    cin>>*cat;
    if(!shelves ->add(n,cat))
     cat->deleteme();
     break;
   case'd':
    dog=new Dog;
    cin>>*dog;
    if(!shelves->add(n,dog))
     dog->deleteme();
    break;
   case's':
    snake=new Snake;
    cin>>*snake;
    if(!shelves->add(n,snake))
     snake->deleteme();
    break;
   default:
    cout<<" your choise is wrong"<<endl;

    break;

   }
   break;
   case 3:
    cout<<" thich cage ?";
    cin>>n;
    shelves->query(n);
    break;
   case 2:
    cout<<"which cage?";//由于不想重写所以就改了数字;
    cin>>n;
    shelves->Delete(n);
    break;
   case 4:
    shelves->stat();
    break;
   case 5:
    break; 

  }
 }
 return 0;

}

2.请实现一个简单的银行储蓄系统,承担活期用户的存款和取款业务,要求如下:
(1) 实现描述银行的类Bank,记录系统中现有哪些储户,(可用数组实现但注意越界),定义了生成储户的函数append,按照账户删除储户的函数Delete,按账号查询储户的函数query,并显示结果.
(2)定义储户类Account,具有属性账号,存款人姓名和余额,操作saving withdraw和showme.
函数saving 存储业务,函数withdraw处理取款业务,(余额不足时不予以取并给提示信息),showme函数显示储户所有信息.
(3)编写main函数,测试上述所要求的各种功能,即可以根据菜单命令增加,删除,和查询储户,以及储户存款和取款操作.

#include<iostream>
#include<cstring>

#include"bank.h"
using std::cout;
using std::cin;
using std::endl;
Account::Account()
{
 ID=0;
 strcpy(name,"");
 balance=0;


}
Account::Account(int id,const char *names,double balances)
{
 ID=id;
 name=new char[50];
 strcpy(name,names);
 balance=balances;
}
void Account::saving()
{
 double number;
 cout<<"please input saving number: ";
 cin>>number;
 balance=balance+number;
 cout<<"now the balance of your account is "<<balance <<endl;
 return ;
 

}
int Account::withdrow()
{
 double number;

 cout<<" please input withdraw number:";
 cin>>number;
 if(balance<number)
 {
  cout<<"sorry,now thd balance of your account is less than "<<number;
  cout<<"."<<endl<<"so you can not withdraw !"<<endl;
  return 0;
 }
 balance =balance-number;
 cout<<"now the balance of your account is "<<balance<<endl;
 return 1;


}
void Account::showme()
{
 cout<<" account ID: "<<ID<<endl;
 cout<<"name: "<<name<<endl;
 cout<<"balance: "<<balance<<endl;
 return ;

 

}
int Account::getID()
{
 return ID;

}
Bank::Bank()
{
 for(int i =0;i<50;i++)
  account[i]=NULL;
 maxID=0;
 accnum=0;

 

}
void Bank::append()
{
 if(accnum==50)
 {
  cout<<" sorry,the bank is full ,so can not add new account!"<<endl;
  return ;

 }
 int id;
 char *names=new char [50];
 double balance;
 cout<<endl<<"please input the name of the account: ";
 cin>>names;
 id=maxID;
 balance=0;
 Account *acc=new Account(id,names,balance);
 account[accnum]=acc;
 cout<<"append successful "<<endl;
 account[accnum]->showme();
 maxID++;
 accnum++;
 return ;


}
void Bank::deletes()
{
 int ID;
 cout<<" please input the account ID that you want to delete";
 cin>>ID;
 int flag=1;
 int i=0;
 while ((i<accnum)&&(flag))
 {
  if(ID==account[i]->getID())
  {
   flag=0;

  }

  else

  {
   i++;
  }

 }
  if(flag)
  {
   cout<<"the account does not exist !"<<endl;

  }
  else{

 
 for( int j=i;j<accnum;j++)
 {
  account[j]=account[j+1];
  
 }
 delete account[accnum-1];
 accnum--;
 cout<<" delete successful "<<endl;
  }
 return ;


}
void Bank::query()
{
 int ID;
 cout<<" please input the account ID that you want to query: ";
 cin>>ID;
 int flag=1;
 int i=0;
 while((i<accnum)&&(flag))
 {
  if(ID==account[i]->getID())
   flag=0;
  else
   i++;
 }
  
  if(flag)
   cout<<endl<<" the account does not exist "<<endl;
  else
  {
   account[i]->showme();
   int choice =0;
   while (choice!=3)
   {
    cout<<" 1: save money "<<endl;
    cout<<" 2: withdraw money "<<endl;
    cout<<"3: return "<<endl;
    cout<<" please input your choice:";
    cin>>choice;
    cout<<endl;
    switch( choice)
    {
    case 1:
     account[i]->saving();
     break;
    case 2:
     account[i]->withdrow();
     break;
    case 3:
     break;

    }
   }
 

 
  

}
 return ;

}#ifndef BANK_H
#define BANK_H
class Account
{
public:
 Account();
 Account(int ,const char *,double);
 void saving();
 int withdrow();
 void showme();
 int getID();
private:
 int ID;
 char *name;
 double balance;


};
class Bank
{
public:
 Bank();
 void append();
 void deletes();
 void query();
private:
 Account *account[50];
 int maxID;
 int accnum;

};
#endif


#include<iostream>
#include"bank.h"
using std::cout;
using std::cin;
using std::endl;

int main()
{
 Bank bank;
 int choice=0;
 cout<<" welcome to bank system "<<endl;
 while(choice!=4)
 {
  cout<<" 1: add an account "<<endl;
  cout<<"2: delete an account "<<endl;
  cout<<" 3: query an account "<<endl;
  cout<<" 4: exit "<<endl;
  cout<<" please input your choice:";
  cin >>choice;
  switch(choice)
  {
  case 1:
   bank.append();
   break;
  case 2:
   bank.deletes();
   break;
  case 3:
   bank.query();
   break;
  case 4:
   break;


  }

 }
 return 0;

}

 3.(1)定义基类MYgraph,至少包含纯虚函数 Area,计算图形面积.
(2)从基类MYgraph中派生圆形类MYcircle和矩形类MYrectangle,其中圆形信息包括圆形半径
和圆心,矩形信息包括矩形的长和宽,具体实现上述纯虚函数Area,计算圆形和矩形的面积.
(3)重载输入>>操作符,使得可以通过cin 直接读入上述圆形类和矩形类的对象值.
(4)编定main 函数,测试上述所要求的各种功能,即可以读入圆或矩形的对象值,计算读入图形的面积并显示.


#include"string.h"
#include<iostream>
int main()
{mycircle circle;
myrectangle rectangles;
std::cin>>circle;
std::cout<<"the area of the circle is "<<circle.area()<<'/n';
std::cin>>rectangles;
std::cout<<"the area of the rectangle is "<<rectangles.area()<<'/n';
return 0;

 

}

#include<cmath>
#include<iostream>
#include"string.h"
//using std::istream;

mycircle ::mycircle()
{
 centerx=0;
 centery=0;
 radius=0;

}
double mycircle::area()
{
 double area;
 area=3.14*radius*radius;
 return area;

}
istream &operator >>(istream & is,mycircle &circle)
{
 std::cout<<"please input x coordinate of the circle center(interger):"<<'/n';

 is>>circle.centerx;
  std::cout<<"please input y coordinate of the circle center(integer)"<<'/n';
  is>>circle.centery;
 std::cout<<"please input the radius of the circle (integer)"<<'/n';
 is>>circle.radius;
 return is;


}
myrectangle ::myrectangle()
{
 length=0;
 width=0;

}
double myrectangle::area()
{
 double area;
 area=length*width;
 return area;

}
istream &operator >>(istream &is ,myrectangle& rectangle)
{
   std::cout<<"please in put the length of the rectangle (integer)"<<'/n';
   is>>rectangle.length;
   std::cout<<"please input the width of the rectangle (integer)"<<'/n';
   is>>rectangle.width;
 return is;


}

#ifndef STRING_H
#define STRING_H

#include<iomanip>
#include<iostream>
using std::istream;

class graphy
{
public:
 virtual double area()=0;

};
class mycircle:graphy
{
public:
 mycircle();
 double area();
 friend istream &operator >>(istream & is,mycircle &circle);


private:
 int centerx;
 int centery;
 int radius;

};
class myrectangle :graphy
{
public:
 myrectangle();
 double area();
 friend istream &operator >>(istream & is ,myrectangle &rectangle);
private:
 int length;
 int width;
 


};
#endif
4.(1) 实现描述栈的类Stack ,其中定义了栈的大小size,(即栈中元素的个数),并包括进栈函数Push
,出栈函数Pop和显示栈顶元素的函数Top,(自已定义了一个gettop函数).
(2)定义基类element,至少包含纯虚函数showme;
(3)从基类element中派生整数类MYinteger和字符串类mystring,具体实现上述纯虚函数showme,
显示该元素的类型和相应值.
(3)重载">>"操作符,使得可以通过cin直接读入上述整数类和字符类的对象值.
编定main 测试以上功能

#include"stack.h"
#include<iostream>
using std::cout;
using std::cin;


int  main()
{
 int i=0;
  integer *myint;
 mystring *mystrings;
 stack* stack1=new stack;

  while(i!=6){

  cout<<"please chiose(1-6):";
  cout<<"1:push an integer"<<'/n';
  cout<<"2:push a string "<<'/n';
  cout<<"3:pop"<<'/n';
  cout<<"4:top"<<'/n';
  cout<<"5:gettop"<<'/n';
  cout<<"6:exit"<<'/n';
    cin>>i;
 
 
 
  switch(i)
  {
  case 1:
   myint=new integer;
   cin>>*myint;
   stack1->push(myint);
   break;
  case 2:
   mystrings =new mystring;
   cin>>*mystrings;
   stack1->push(mystrings);
   break;
  case 3:
   stack1->pop();
   break;
  case 4:
   stack1 ->top();
   break;
  case 5:
   stack1->gettop();
   break;
  case 6:
   break;


  }
  }
 
 
 return 0;


}
#include<iostream>
#include"stack.h"
using std::cout;
using std::cin;

integer::integer()
{
 myint=0;


}
void integer::showme()
{
 cout<<"the element is an integer,and its value is "<<myint<<'/n';
 

}
std::istream &operator>>(std::istream & is,integer &myint)
{
 cout<<"please input the value of integer:";
 return is>>myint.myint;
}
mystring::mystring()
{
mystr="";
}
void mystring::showme()
{
 cout<<"the element is an string ,and its value "<<mystr<<'/n';

}
std::istream & operator>>(std::istream &is,mystring &myster)
{
 cout<<"please input the value of string :";
 myster.mystr=new char [20];
 return is>>myster.mystr;
}
stack::stack()
{
 
 size =0;
 for(int i=0;i<5;i++)
 {
  stacks[i]=NULL;

 }
}
void stack::pop()
{

 if(size==0)
 {
  cout<<"the stack is empty!"<<'/n';
  return ;
 }
 for (int i=0;i<size-1;i++)
 {
  stacks[i]=stacks[i+1];
 

 }
  stacks[size-1]=NULL;
  size--;
  return ;
}
void stack::top()
{
 if(size ==0)
 {
  cout<<"the stack is empty!"<<'/n';
  return ;
 }
 stacks[0]->showme();
 return ;


}
void stack::push(element *myele)
{
 if(size==5)
 {
  cout<<"the stack is full your element can not be pushed !"<<'/n';
  return ;

 }
 for (int i=size;i>0;i--)
  stacks[i]=stacks[i-1];
 stacks[0]=myele;
 size++;
 return ;

}
void stack::gettop()
{
 void top();
 void pop();
 return ;

}

#ifndef STACK_H
#define STACK_H
#include<iosfwd>

class element
{
public:
 virtual void showme()=0;

 
};
class integer:public element
{
public:
 integer();
 void showme();
 friend std::istream &operator>>(std::istream & is,integer &myint);
protected:
 int myint;

 

 
};
class mystring:public element
{

public:
 mystring();
 void showme();
 friend std::istream &operator>>(std::istream &is,mystring &myster);

protected:
 char *mystr;
};
class stack
{
public:
 stack();
 void push(element *myele);
    void pop();
 void top();
 void  gettop();
private:
 element *stacks[5];
 int size;

};

#endif

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:428134次
    • 积分:4683
    • 等级:
    • 排名:第6176名
    • 原创:34篇
    • 转载:140篇
    • 译文:0篇
    • 评论:84条
    文章分类
    最新评论