2sd[理学]信息实践题汇总2sd[理学]信息实践题汇总
实验三 C++程序设计基础
1:编写以下程序,观察编译结果和运行结果。请解释运行的结果。
float fn1(float r) {
int temp = r*r*3.14f ;
return temp ;
}
float& fn2(float r) {
int temp = r*r*3.14f ;
return temp ;
}
void main() {
float a = fn1(5.0f) ;
float& b = fn1(5.0f) ;//error
float...
2sd[理学]信息实践题汇总
实验三 C++程序
基础
1:编写以下程序,观察编译结果和运行结果。请解释运行的结果。
float fn1(float r) {
int temp = r*r*3.14f ;
return temp ;
}
float& fn2(float r) {
int temp = r*r*3.14f ;
return temp ;
}
void main() {
float a = fn1(5.0f) ;
float& b = fn1(5.0f) ;//error
float c = fn2(5.0f) ;
float& d = fn2(5.0f) ;
cout<
int& level( int *grade,int size,int &tA,int &tB) { int i,sum;
for(i=0,sum=0;i=80) return tA; else return tB;
}
void main()
{ int a[][4]={{60,80,90,75},{75,85,65,77},{80,88,90,98},
{89,100,78,81},{62,68,69,75},{86,85,77,91}};
int i,A=0,B=0;
for(i=0;i分析并写出下列程序的运行结果,然后上机运行验证。
#include
void main()
{
int a[10]={76,83,54,62,40,75,90,93,77,85};
int b[5]={ 60,70,80,90,101};
int c[5]={0};
for(int i=0;i<10;i++)
{
int j=0;
while(a[i]>=b[j])j++;
c[j]++;
}
for(i=0;i<5;i++)cout<
#include"Date.h"
using namespace std;
Date::Date(int _month,int _day,int _year){
setmonth(_month);
setday(_day);
setyear(_year);} Date::Date(Date& a){
cout<<"拷贝构造函数被调用"<0)?_month:1;} void Date::setday(int _day){
day=(judge(month,_day))?_day:1;} void Date::setyear(int _year){
year=(_year>=0)?_year:1970;}
int Date::getmonth(){return month;} int Date::getday(){return day;}
int Date::getyear(){return year;}
void Date::displayDate(){
cout<0)return 1;
else return 0;}
else if((!(year%4)&&(year%100))||!(year%400)){
if(_day<(a[_month]+2)&&_day>0)return 1;
else return 0;}
else if(_day<(a[_month]+1)&&_day>0)return 1;
else return 0;}
//mian.cpp
#include
#include "Date.h"
using namespace std;
int main()
{
Date a(2,28,1992);
Date b(a);
b.judgeyear(a);
a.displayDate();
a.nextday();
a.displayDate();
a.nextday();
a.displayDate();
a.setDate(12,31,1992);
a.displayDate();
a.nextday();
a.displayDate();
b.displayDate();
system("PAUSE");
return 0;
}
2.计算两点之间的距离。定义点类point,再定义一个类Line类,该类有一方法返回两点之间的距离,其数据成员为两个点类对象。注意有组合的情况下如何使用构造函数。
#include
#include
using namespace std;
class point{
private:
double x;
double y;
public:
double getxvalue(){return x;}
double getyvalue(){return y;}
point(double _x=0,double _y=0){x=_x;y=_y;}
void showvalue(){cout<<'('<>a[0]>>a[1]>>a[2]>>a[3]){b.setvalue(a[0],a[1],a[2],a[3]);b.s();}
return 0;}
3:拷贝构造函数在哪些情况下会被自动调用,请编写程序举例说明。 (至少要
说明三种情况)
实验五 类和对象2
1. 编写一个Student类,其中有id和name属性。现有要求能随时显示系统运行期间有多少个该类的对象。(提示:使用静态变量)
//Student.h
#ifndef Student_H
#define Student_H
class Student{
public:
Student(char* ID="",char* NAME="");
~Student();
void show();
void setStudent(char* ID,char* NAME);
static void shownum();
private:
char* id;
char* name;
static int num;}; #endif
//Student.cpp
#include
#include"Student.h"
using namespace std;
#include
int Student::num=0;
Student::Student(char* ID,char* NAME){
id=new char[strlen(ID)+1];
if(id)strcpy(id,ID);
name=new char[strlen(NAME)+1];
if(name)strcpy(name,NAME);
num++;
cout<<"ID: "<
#include"Student.h"
using namespace std;
int main()
{
Student::shownum();
Student a;
Student::shownum();
Student b;
Student::shownum();
a.~Student();
Student::shownum();
b.~Student();
Student::shownum();
system("PAUSE");
return 0;
}
2.编写一个类,用户使用这个类的时候只能用这个类得到一个相同的实例。(即:单例模式)
#include
using namespace std;
class onlyou{
private:
onlyou(int n){var=n;}
int var;
public:
static onlyou* onlyou::Getobject(int n){
static onlyou theone(n);
return &theone;}
int getval(){return var;}
void setval(int n){var=n;}
void showval(){cout<>n;
onlyou *theone=onlyou::Getobject(n);
cin>>n;
onlyou *thesame=onlyou::Getobject(n);
if(thesame==theone)cout<<"the same.\n";
theone->showval();
cin>>n;
theone->setval(n);
theone->showval();
cin>>n;
thesame->setval(n);
theone->showval();
system("pause");
return 0;
}
3:编写一个,,,类,包含名字,体重,价格等信息,并且有一个成员函数用于打印这些信息,然后编写一个含有,只,,,的对象数组,遍历该数组,打印出每只,,,的信息。
#include
#include
using namespace std;
class Dog{
public:
Dog(/*har* _name="Amy",*/int _weight=60,int _price=100){
/*name=new char[strlen(_name)+1];
strcpy(name,_name);*/
weight=_weight;
price=_price;}
void show(){cout<*"name:"<答案 见附录,注意先不要看答案,那样就失去了练习的意义) # include
# include
struct Jose {
int code ;
Jose* next ;
};
void main() {
int numberOfBoy , interval ;
cout << "please input the number of boy: \n" ;
cout << "please input the interval: \n" ;
cin >> numberOfBoy >> interval ;
/**
* 构造初始的环形链表
**/
Jose* pHead = new Jose ;
pHead->code = 1 ;
pHead->next = NULL ;
Jose* pCurrent = pHead;
for (int i = 2 ; i <= numberOfBoy ; i++ ) {
Jose* pNext = new Jose ;
pNext->code = i ;
pNext->next = NULL ;
pCurrent->next = pNext ;
pCurrent = pNext ;
}
pCurrent->next = pHead ;
/**
* 打印环形链表,看是否正确
**/
pCurrent = pHead ;
for ( i = 1 ; i <= numberOfBoy ; i++ ) {
cout << pCurrent->code << " " ;
pCurrent = pCurrent->next ;
}
/**
* pivot为链表哨兵,用来数数
**/
Jose* pivot ;
pCurrent = pHead ;
while(pCurrent != pCurrent->next) {
pivot = pCurrent ;
int j = 1 ;
/**
* 由于是第三个,所以没隔两个走一个人,为了维护链表,所以需要找到Lose的前一个人
**/
while(j < interval -1 ) {
pivot = pivot->next ;
j++ ;
}
Jose* pLose = pivot->next ;
pivot->next = pivot->next->next ;
pCurrent = pivot->next ;
cout << pLose->code << endl ;
delete pLose ;
}
cout << endl ;
cout << "***" << endl ;
cout << pCurrent->code << endl ; }
# include
class Boy {
public:
int code ;
Boy* next ;
};
class Ring {
public:
Ring(int num , int interval) {
pHead = NULL ;
pivot = NULL ;
pTail = NULL ;
this->num = num ;
this->interval = interval ;
for (int i = 1 ; i <= num ; i++ ) {
this->addBoy(i) ;
}
}
void setBegin(int n) ;
void go() ;
Boy* getWinner() ;
~Ring() {
delete pivot ;
}
private :
int num ;
int interval ;
Boy* pHead ; //pHead和pTail的作用在于建立环时使用
Boy* pTail ;
Boy* pivot ; //用于当前指向哪个小孩,初始时指向第一个小孩,可以通过setBegin进行修改
void count(int n) ;
Boy* deleteNext(Boy* pNext) ;
void addBoy(int i) ;
};
void Ring::setBegin(int n) {
int i = 1 ;
while(i < n) {
pivot = pivot->next ;
i++ ;
}
}
void Ring::addBoy(int i) {
if (pHead == NULL ) {
pHead = new Boy ;
pHead->code = i ;
pHead->next = pHead ;
pivot = pHead ;
pTail = pHead ;
}else {
Boy* pBoy = new Boy ;
pBoy->code = i ;
pBoy->next = pHead ;
pTail->next = pBoy ;
pTail = pBoy ;
}
cout << "添加了BOY : " << i << endl ;
}
Boy* Ring::deleteNext(Boy* pNext) {
Boy* p = pNext->next ;
pNext->next = pNext->next->next ;
return p ;
}
void Ring::count(int n) {
int i = 1 ;
while(i < n - 1 ) {
pivot = pivot->next ;
i++ ;
}
Boy* pDelete = deleteNext(pivot) ;
pivot = pDelete->next ;
cout << "退出的小孩是 : " << pDelete->code << endl ;
delete pDelete ;
}
void Ring::go() {
cout<<"Begin From "<code<next 说明只剩下了一个孩子
while(pivot != pivot->next) {
count(interval) ;
}
}
Boy* Ring::getWinner() {
return pivot ;
}
void main() {
Ring r(5,3) ;
r.setBegin(4) ;
r.go() ;
cout << "----------------" << endl ;
cout << r.getWinner()->code << endl ; }
#include
using namespace std;
class chliden{
private:
int num;
chliden *next;
chliden *last;
static chliden *first;
public:
chliden(int _n){num=_n;next=last=0;}
static void begin(int n){
chliden *back;
chliden *temp=new chliden(1);
back=chliden::first=temp;
for(int i=2;inext=temp;
temp->last=back;
back=temp;}
back->next=chliden::first;
chliden::first->last=back;}
static void game(int n,int k){
begin(n);
int i=1;
chliden *point=chliden::first,*temp;
while(point->next!=point){
while(i++-k)point=point->next;
i=1;
cout<<"The chliden, "<num<<", is out."<last->next=point->next;
point->next->last=point->last;
delete point;
point=temp;}
cout<<"The winner is "<num<<" child.\n";
delete point;}
};
chliden *chliden::first=0;
int main(){
int n,k;
while(cin>>n>>k)
chliden::game(n,k);
return 0;}
实验七 类和对象
1. 编写一个类Address,其中有两个成员变量都是char* pCity和char*
pStreet,另一个类为Person,其中有三个成员变量,分别为char* pName,int
id,以及指向Address的Address* pAdd,在main方法中创建Person p1,然
后调用定义Person p2(p1),观察现象并解决运行时的问题。
代码:
#include
#include
using namespace std;
class Address{
public:
Address(char* PcITY="",char* PsTREET=""){
pCity=new char[strlen(PcITY)+1];
pStreet=new char[strlen(PsTREET)+1];
strcpy(pCity,PcITY);
strcpy(pStreet,PsTREET);}
Address(Address& a){
pCity=new char[strlen(a.pCity)+1];
pStreet=new char[strlen(a.pStreet)+1];
strcpy(pCity,a.pCity);
strcpy(pStreet,a.pStreet);}
~Address(){delete [] pCity;
delete [] pStreet;}
/* char* getpCity(){return pCity;}
char* getpStreet(){return pStreet;}*/
void
display(){cout<<"City:"<display(); pAdd-
}
private:
char* pName;
long long id;
Address* pAdd;
};
int main(){
Person p1("林梓钘",20100830110ll,"佛山","汾江中路");
Person p2(p1);
p1.display();
p2.display();
system("pause");
return 0;}
2:编写父类A,在其中分别有public,protected,private的成员变量和函数,编写类B分别以public,protected,private方式继承A,编写类C分别以public,protected,private方式继承B。1:解释在以上情况下,在B和C的内部访问A中的成员变量和函数时有何影响。2:分别在main方法中定义B和C的对象,尝试访问从A中继承来的成员函数和变量。
3:声明一个点类和直线类,编写一程序,求一点到直线的距离。
#include
#include
using namespace std;
class line;
class point{
friend double juli(line& a,point& b);
private:
double x;
double y;
public:
double getxvalue(){return x;}
double getyvalue(){return y;}
point(double _x=0,double _y=0){x=_x;y=_y;}
void showvalue(){cout<<'('<>a[0]>>a[1]>>a[2]){b.setvalue(a[0],a[1],a[2]);cout<
using namespace std;
class vehicle{
public:
vehicle(vehicle& a){
wheels=a.wheels;
weight=a.weight;}
vehicle(){
wheels=0;
weight=0;}
vehicle(int W1,int W2){
wheels=W1;
weight=W2;}
int getwh(){return wheels;}
int getwe(){return weight;}
void display(){cout<
#include
using namespace std;
class Person{
protected:
char* name;
int age;
long long no;
public:
Person(char* N,int a,long long n){
name= new char[strlen(N)+1];
strcpy(name,N);
age=a;
no=n;}
~Person(){delete []name;}
virtual void
display(){cout<
#include
using namespace std;
class Array
{
friend ostream &operator <<(ostream & ,const Array &);
friend istream &operator >>(istream & ,const Array &);
public:
Array(int arraySize=10);
Array(Array &);
~Array();
int getSize();//得到数组大小
const Array & operator =(const Array &);//数组赋值
int operator ==(const Array &) const;//数组等值判断
int operator !=(const Array &) const;//数组不等值判断
int &operator [](int ) ;//下标操作
static int getArrayCount();//得到数组对象个数
private:
int *ptr;
int size;
static int arrayCount; //统计数组对象个数 };
int Array::arrayCount=0;
int Array::getArrayCount(){return arrayCount;}
Array::Array(int arraySize) {
++arrayCount;
size=arraySize;
ptr=new int[size];
assert(ptr!=0);
for(int i=0;i>(istream & input,const Array& a)
{
for(int i=0;i>a.ptr[i];
return input;
}
//重载输出运算符
ostream & operator <<(ostream &output,const Array &a)
{
for(int i=0;i>a1>>a2;
cout<<"a1 : "<
#include
#include
using namespace std;
class String {
friend ostream& operator<<(ostream& , const String & ) ;
friend istream& operator>>(istream& , String & ) ; public:
String(const char* = "") ;
String(const String& ) ;
~String() ;
const String& operator=(const String& ) ;
const String& operator=(const char* s) ;
const String& operator+=(const String& ) ;
String& operator+(const String& ) ;
String& operator()(int , int ) ; private:
char* sPtr ;
int length ;
};
String::String(const char* sPtr ) {
length = strlen(sPtr) ;
this->sPtr = new char[length + 1] ;
assert(this->sPtr!=0) ;
strcpy(this->sPtr,sPtr) ;
}
String::String(const String& s) {
length = strlen(s.sPtr) ;
this->sPtr = new char[length + 1] ;
assert(this->sPtr!=0) ;
strcpy(this->sPtr,s.sPtr) ; }
String::~String() {
delete [] sPtr ;
}
ostream& operator<<(ostream& output, const String& s) {
output<>(istream& input,String& s) {
char temp[1024] ;
input >> temp ;
s = temp ;
return input ;
}
const String& String::operator =(const String& s) {
if (&s != this) {
delete[] sPtr ;
length = s.length ;
sPtr = new char[length + 1] ;
assert(sPtr != NULL ) ;
strcpy(sPtr,s.sPtr) ;
}
return *this ;
}
const String& String::operator =(const char *s) {
delete[] sPtr ;
length = strlen(s) ;
sPtr = new char[length + 1] ;
assert(sPtr != NULL ) ;
strcpy(sPtr,s) ;
return *this ;
}
String& String::operator+(const String& right) {
char* tempPtr = this->sPtr ;
length = length + right.length ;
this->sPtr = new char[length + 1] ;
assert(sPtr != NULL ) ;
strcpy(sPtr,tempPtr) ;
strcat(sPtr,right.sPtr) ;
delete[] tempPtr ;
return *this ;
}
const String& String::operator+=(const String& right) {
char* tempPtr = this->sPtr ;
length = length + right.length ;
this->sPtr = new char[length + 1] ;
assert(sPtr != NULL ) ;
strcpy(sPtr,tempPtr) ;
strcat(sPtr,right.sPtr) ;
delete[] tempPtr ;
return *this ;
}
String& String::operator()(int index, int subLength) {
assert(index > 0 && index < length && subLength >= 0) ;
String *subStr = new String ;
assert(subStr != NULL) ;
int size ;
if ((subLength == 0) || (index + subLength > length)) {
size = length - index + 1 ;
}else{
size = subLength + 1 ;
}
delete subStr->sPtr ;
subStr->length = size ;
subStr->sPtr = new char[size] ;
assert(subStr->sPtr != NULL ) ;
int i,j;
for(i = index , j = 0 ; i < index+size - 1 ; i++ , j++ ) {
subStr->sPtr[j] = sPtr[i] ;
}
subStr->sPtr[j] = '\0' ;
return *subStr ;
}
main() {
String s("what") ;
cout<
#include
#include
using namespace std;
class Animal
{
public:
Animal(char * name,float weight)
{
this->name=new char[strlen(name)+1];
strcpy(this->name,name);
this->weight=weight;
}
virtual string sound()=0;
virtual string who(){
char str[10];
return string("My name is ") + name + string(". My
weight is ") + string(itoa(weight, str, 10));
}
virtual ~Animal(){};
private:
float weight;
char * name;
};
class Sheep:public Animal
{
public:
Sheep(char *name,float weight):Animal(name,weight)
{
this->name=new char[strlen(name)+1];
strcpy(this->name,name);
this->weight=weight;
}
string who()
{
return string("I'm a sheep. ") + Animal::who();
}
string sound()
{
return string("咩..咩..");
}
~Sheep()
{
delete [] name;
}
private:
float weight;
char *name;
};
class Cow:public Animal {
public:
Cow(char *name,float weight):Animal(name,weight)
{
this->name=new char[strlen(name)+1];
strcpy(this->name,name);
this->weight=weight;
}
string who()
{
return string("I'm a Cow. ") + Animal::who();
}
string sound()
{
return string("哞..哞..");
}
~Cow()
{
delete [] name;
}
private:
float weight;
char *name;
};
class Dog:public Animal
{
public:
Dog(char *name,float weight):Animal(name,weight)
{
this->name=new char[strlen(name)+1];
strcpy(this->name,name);
this->weight=weight;
}
string who()
{
return string("I'm a Dog. ") + Animal::who();
}
string sound()
{
return string("旺..旺..");
}
~Dog()
{
delete [] name;
}
private:
float weight;
char *name;
};
class Zoo
{
public:
void addAnimal(Animal * pNewAnimal)
{
if(number==100)
cout<<"the zoo is full"<who()<<"
"<sound()<
#include
using namespace std;
class Shape
{
public:
virtual void aera()=0;
virtual void girth()=0; private:
};
class Rectangl:public Shape {
public:
Rectangl(double length,double width)
{
this->length=length;
this->width=width;
}
void aera() { cout<<"the aera of Rectangl is :
"<r=r;
}
void aera() { cout<<"the aera of Circle is :
"<<3.14*r*r<border1=border1;
this->border2=border2;
>border3=border3; this-
s=(border1+border2+border3)/2;
}
void aera() { cout<<"the aera of Triangl is :
"<aera();
s1->girth();
s2->aera();
s2->girth();
s3->aera();
s3->girth();
system("pause");
}