C++语言程序设计(第三版)作业

来源:互联网 发布:c语言求整数为质数的和 编辑:程序博客网 时间:2024/06/03 18:05

//2-36
#include "stdafx.h"
using namespace std;
enum ball {red,yellow,bule,white,black};
void main()
{ int firstBall,secondBall,thirdBall;
 int count=0;
 for (firstBall=red;firstBall<black;firstBall++)
 {
  for (secondBall=firstBall+1;secondBall<black;secondBall++)
  {
   for (thirdBall=secondBall+1;thirdBall<=black;thirdBall++)
   {
    count++;
   }
  }
 }
 cout<<count;
 int i;
 cin>>i;
}

//3-2
#include "stdafx.h"
using namespace std;
void main()
{
 int intOne;
 int &rSomeRef = intOne;

 intOne = 5;
 cout << "intOne;/t" << intOne << endl;
 cout << "rSomeRef:/t" << rSomeRef <<endl;
 cout << "&intOne:/t" << &intOne << endl;
 cout << "&rSomeRef:/t" << &rSomeRef << endl;

 int intTwo = 8;
 rSomeRef=intTwo;
 cout << "/nintOne;/t" << intOne << endl;
 cout << "intTwo;/t" << intTwo << endl;
 cout << "rSomeRef:/t" << rSomeRef <<endl;
 cout << "&intOne:/t" << &intOne << endl;
 cout << "&intTwo:/t" << &intTwo << endl;
 cout << "&rSomeRef:/t" << &rSomeRef << endl;


 int temp;
 cin >> temp;
}

//3-8
#include "stdafx.h"
using namespace std;
int FToC (int F);
main()
{
 int Fat;
 cout<<"请输入一个华氏温度";
 cin>>Fat;
 cout<<Fat<<"华氏度="<<FToC(Fat)<<"摄氏度";
 int i;
 cin>>i;
}
int FToC (int F)
{
 return ((F-32)*5/9);
}

//3-9
#include "stdafx.h"
using namespace std;
bool primenum(int number)
{
 bool i=true;
 int x;
 for (x=2;x<number;x++)
 {
  if ((number%x)==0)
   i=false;
 }
 return i;
}
main()
{
 int number;
 cout<<"请输入一个整数";
 cin>>number;
 if (primenum(number))
  cout<<number<<"是质数";
 else
  cout<<number<<"不是质数";
 int i;
 cin>>i;
}

//3-10
#include "stdafx.h"
using namespace std;
int common_measure(int num1,int num2);
int common_multiple(int num1,int num2);
int compare(int num1,int num2,char t);
main ()
{
 int num1,num2;
 cout<<"请输入两个整数"<<endl;
 cin>>num1>>num2;
 cout<<"它们的最大公约数为:"<<common_measure(num1,num2)<<endl;
 cout<<"它们的最大公倍数为:"<<common_multiple(num1,num2)<<endl;
 
 int temp;
 cin>>temp;
}

int common_measure(int num1,int num2)
{
 int i;
 for(i=compare(num1,num2,'n') ; i>1 ; i--)
 {
  if ( ((num1%i) == 0 )&&( (num2%i) == 0 ) )
  {
   return i;
  }
 }
}

int common_multiple(int num1,int num2)
{
 int i;
 for (i=compare(num1,num2,'x') ; i<=num1*num2 ; i++)
 {
  if ( ((i%num1) == 0 )&&( (i%num2) == 0 ) )
  {
   return i;
  }
 }
}

int compare(int num1,int num2,char t)
{
 
 if (t=='x')
  return num1>num2 ? num1 : num2;
 else
  return num1<num2 ? num1 : num2;

}


//3-12
#include "stdafx.h"
using namespace std;
int sum(int n)
{
 if (n==1)
  return 1;
 else
  return (sum(n-1)+n);
}
main()
{
 int number;
 cout<<"请输入一个数字";
 cin>>number;
 cout<<"sum is "<<sum(number);
 int temp;
 cin>>temp;
}

//3-14
#include "stdafx.h"
using namespace std;
int fib(int n)       //用递归做的函数,速度不说了,超TM慢
{
 if ((n==1)||(n==2))
 {
  return 1;
 }
 return (fib(n-1)+fib(n-2));
}
int fib2(int n)       //用for做的函数,速度比递归快的多
{
 int i=n;
 int result_n=1;
 int result_n_1=1;
    int t;
 for (i=1;i<n-1;i++)
 { t=result_n; 
  result_n=result_n+result_n_1;
  result_n_1=t;
 }
 return result_n;
}
main ()
{
 int number;
 cout<<"输入一个数";
 cin>>number;
 cout<<fib(number);
 int temp;
 cin>>temp;
}

//3-15
#include "stdafx.h"
using namespace std;
float Pn(int x,int n);
main ()
{
 int number,xx;
 cout<<"输入Pn(x)中n=?";
 cin>>number;
 cout<<"输入Pn(x)中x=?";
 cin>>xx;
 cout<<Pn(xx,number);
 int temp;
 cin>>temp;
}
float Pn(int x,int n)
{
    if (n==0)
 {
  return 1;
 }
 else
 {
  if(n==1)
   return x;
  else
   return (float(((2*n-1)*x*Pn(x,n-1)-(n-1)*Pn(x,n-2))/n));
   
 }

}

//4-8
#include "stdafx.h"
using namespace std;
class Dog
{
public:
 Dog()
 {
  age=0;
  weight=0;
 };
 void setDog(int d_age,int d_weight);
 void showDog();
private:
 int age;
 int weight;
};

void Dog::setDog(int d_age,int d_weight)
{
 age=d_age;
 weight=d_weight;
}
void Dog::showDog()
{
 cout<<"Dog's age is"<<age<<".Dog is "<<weight<<" kg."<<endl;
}

main()
{
 Dog white;
 white.showDog();
 white.setDog(4,14);
 white.showDog();
}


//4-9
#include "stdafx.h"
using namespace std;
class point
{
public:
 point(){X=0;Y=0;}
 point(int x,int y){X=x;Y=y;}
 void setpoint(int x,int y);
 int GetX(){return X;}
 int GetY(){return Y;}
private:
 int X;
 int Y;
};
void point::setpoint(int x,int y)
{
 X=x;
 Y=y;
}

class Rectangle
{
public:
 Rectangle(point t_LBpoint,point t_RTpoint)
 {
  LBpoint=t_LBpoint;
  RTpoint=t_RTpoint;
 };
 Rectangle(Rectangle& rec);
 void setRec(point t_LBpoint,point t_RTpoint);
 int getarea();
private:
 point LBpoint;
 point RTpoint;
 int areaVal;
};
int Rectangle::getarea()
{
 areaVal=-(RTpoint.GetX()-LBpoint.GetX())*(RTpoint.GetY()-LBpoint.GetY());
 return areaVal;
}
Rectangle::Rectangle(Rectangle& rec):LBpoint(rec.LBpoint),RTpoint(rec.RTpoint)
{
 areaVal=rec.areaVal;
}
void Rectangle::setRec(point t_LBpoint,point t_RTpoint)
{
 LBpoint=t_LBpoint;
 RTpoint=t_RTpoint;
}

//4-10
#include "stdafx.h"
using namespace std;
class date
{
public:
 date()
 {
  year=2000;
  month=1;
  day=1;
 }
 date(int tyear,int tmonth,int tday);
 void setdate(int tyear,int tmonth,int tday);
 int getyear();
 int getmonth();
 int getday();
private:
 int year;
 int month;
 int day;
};
date::date(int tyear,int tmonth,int tday)
{
    year=tyear;
 month=tmonth;
 day=tday;
}
void date::setdate(int tyear,int tmonth,int tday)
{
    year=tyear;
 month=tmonth;
 day=tday;
}
int date::getday()
{
 return day;
}
int date::getmonth()
{
 return month;
}
int date::getyear()
{
 return year;
}
class employee
{
public:
 employee(int tnum,char tgender,date tbirthday,int tid);
 ~employee(){};
 employee(employee& otheremp);
 int getnum();
 char getgender();
 date getbirthday();
 int getid();
 void putin(int tnum,char tgender,date tbirthday,int tid);
 void showemployee();
private:
 int num;
 char gender;
 date birthday;
 int id;
};

employee::employee(int tnum,char tgender,date tbirthday,int tid)
:birthday(tbirthday)
{
 num=tnum;
 gender=tgender;
 id=tid;
}
employee::employee(employee& otheremp)
:birthday(otheremp.birthday)
{
 num=otheremp.num;
 gender=otheremp.gender;
 id=otheremp.id;
}
int employee::getnum(){return num;}
date employee::getbirthday(){return birthday;}
char employee::getgender(){return gender;}
int employee::getid(){return id;}
void employee::putin(int tnum,char tgender,date tbirthday,int tid)
{
 birthday=tbirthday;
 num=tnum;
 gender=tgender;
 id=tid;
}
void employee::showemployee()
{
 cout<<" num="<<num<<" gender="<<gender<<" id="<<id<<endl;
}

main()
{
 date birthday(2004,3,23);
 employee my(2703,'M',birthday,816);
 employee your(my);
 my.showemployee();
 your.showemployee();
 your.putin(9876,'F',birthday,51025);
 your.showemployee();
 date tempdate;
 tempdate=my.getbirthday();
 cout<<tempdate.getday()<<endl;
}


//4-11
#include "stdafx.h"
using namespace std;
class  Rectangle
{
public:
 Rectangle(float twhiget,float thight);
 void setRec(float twhiget,float thight);
 float acreage();
protected:
 float whight;
 float hight;
};
Rectangle::Rectangle(float twhight,float thight)
{
 whight=twhight;
 hight=thight;
}
void Rectangle::setRec(float twhight,float thight)
{
 whight=twhight;
 hight=thight;
}
float Rectangle::acreage()
{
 return whight*hight;
}
main()
{
 Rectangle box(54.1,15.1);
 cout<<box.acreage()<<endl;
}

//4-14
#include "stdafx.h"
using namespace std;
class tree
{
public:
 tree(int tage){ages=tage;}
 void grow(int year);
 void age();
protected:
 int ages;
};
void tree::age()
{
 cout<<ages<<endl;
}
void tree::grow(int year)
{
 ages=ages+year;
}
main()
{
 tree green(5);
 green.age();
 green.grow(2);
 green.age();
}


//6-4
sizeof oneArray/sizeof oneArray[0];


//6-16
#include "stdafx.h"
#include <string>
using namespace std;
void main()
{
 int a;
 int* p=&a;
 int &r=a;
 *p=10;
 r=5;
}


//6-5
#include "stdafx.h"
#include <string>
using namespace std;
void main()
{
 for(int a[5][3],i=0;i<15;i++)
  *(*a+i)=i;
}


//6-17
#include "stdafx.h"
#include <string>
using namespace std;
void main()
{
 int* p;
 p=new int(9);
 cout<<"the value at p:"<<*p;
 delete p;
}


//6-18
#include "stdafx.h"
#include <string>
using namespace std;
int* Fn1();
void main()
{
 int* a=Fn1();
 cout<<"the value at a is:"<<*a;
 delete a;
}
int* Fn1()
{
 int* p=new int(5);
 return p;
}


//6-20
#include "stdafx.h"
#include <iostream>
using namespace std;
class SimpleCircle
{
public:
 SimpleCircle(float);
 float Area();
 float perimeter();
 ~SimpleCircle();
private:
 float* itsRadius;
 const double pi;
};
SimpleCircle::SimpleCircle(float radius):pi(3.1415926)
{
 itsRadius=new float(radius);
}
float SimpleCircle::Area()
{
 return pi*(*itsRadius)*(*itsRadius);
}
float SimpleCircle::perimeter()
{
 return 2*pi*(*itsRadius);
}
SimpleCircle::~SimpleCircle()
{
 delete itsRadius;
}
main()
{
 SimpleCircle C1(123);
 cout<<C1.Area()<<endl;
}


//6-21
#include "stdafx.h"
#include <iostream>
using namespace std;
int WordNum(char s[]);
main()
{
 char s[]="VNHFgtrfdewqasdHGGHFHGFHGFHGF";
 cout<<WordNum(s)<<endl;
}
int WordNum(char s[])
{
 int n=0;
 for(int i=0;s[i]!='/0';i++)
 {
  if((s[i]<='Z'&&s[i]>='A')||(s[i]<='z'&&s[i]>='a'))
   n++;
 }
 return n;
}

//6-22
#include "stdafx.h"
#include <iostream>
using namespace std;
int index(char *s,char *t)
{
 bool same=true;
 for(int i=0;s[i]!='/0';i++)
 {
  if(s[i]==t[0])
  {
   for(int j=1;t[j]!='/0';j++)
   {
    if (s[i+j]!=t[j])
    {
     same=false;
     break;
    }
   }
   if(same)
    return i;
  }
 }
 return -1;
}

main()
{
 char s[]="jiaxun";
 char t[]="xun";
 cout<<index(s,t);
}


//6-23
#include "stdafx.h"
#include <iostream>
using namespace std;
reverse(char *s)
{
 if (*s!='/0')
 {
  reverse(s+1);
  cout<<*s;
 }
}
main()
{
 char string[]="abcdefg";
 reverse(string);
 cin.get();
}

//6-24
#include "stdafx.h"
#include <iostream>
using namespace std;
main()
{
 const int N=8;
 int students[N];
 int sum=0;
 cout<<"输入"<<N<<"个同学的成绩"<<endl;
 for(int i=0;i<N;i++)
 {
  cout<<"第"<<i+1<<"个同学的成绩:"<<endl;
  cin>>students[i];
  sum+=students[i];
 }
 cout<<"平均成绩为:"<<float(sum)/float(N)<<endl;
}


//6-25
#include "stdafx.h"
#include <iostream>
using namespace std;
class MyString
{
public:
 MyString();
 MyString(char* NewStr);
 MyString(MyString &OtherStr);
 ~MyString();
 int Length();
 MyString operator + (MyString s2);
 char& operator [] (int n);
 void operator = (const MyString OtherStr);
 void operator += (MyString OtherStr);
 operator char *(void) const;
private:
 char* Str;
 int getLen(char* s);
 char*tempStr;
};
MyString::MyString()
{
 tempStr=new char[255];
 Str=0;
}
MyString::MyString(char* NewStr)
{
 tempStr=new char[255]; 
 int NewStrLength=getLen(NewStr);
 Str=new char[NewStrLength];
 for(int i=0;i<NewStrLength;i++)
 Str[i]=NewStr[i];
}
MyString::MyString(MyString &OtherStr)
{

 tempStr=new char[255];
 for(int i=0;i<255;i++)
  tempStr[i]=OtherStr.tempStr[i];

 if (OtherStr.Str==0)
  Str=0;
 else
 {
  int OtherStrLength=OtherStr.Length()+1;
  Str=new char[OtherStrLength];
  for(int i=0;i<OtherStrLength;i++)
   Str[i]=OtherStr[i];
 }
}
int MyString::getLen(char* s)
{
 int i=0;
 while(s[i]!='/0')
  i++;
 return i+1;
}
int MyString::Length()
{
 return getLen(Str)-1;
}
MyString::~MyString()
{
 delete[] Str;
 delete[] tempStr;
}
MyString MyString::operator + (MyString s2)
{
 int length=getLen(Str);
 int i;
 for(i=0;i<(length-1);i++)
 tempStr[i]=Str[i];
 int s2Len=s2.Length()+1;
 for(int j=0;i<s2Len+length;i++,j++)
 {
  tempStr[i]=s2.Str[j];
 }
 return MyString(tempStr);
}
char& MyString::operator [](int n)
{
 if (n<0||n>(getLen(Str)))
 {
  cout<<"下标越界"<<endl;
  cin.get();
 }
 return Str[n];
}
void MyString::operator =(MyString OtherStr)
{
 delete[] Str;
 int OtherStrLen=OtherStr.Length()+1;
 Str=new char[OtherStrLen];
 for(int i=0;i<OtherStrLen;i++)
  Str[i]=OtherStr.Str[i];
}
void MyString::operator +=(MyString OtherStr)
{
 *this=*this+OtherStr;
}
MyString::operator char *(void) const
{
 return Str;
}
main()
{
 MyString s("123456789");
 MyString s1(s);
 for(int i=0;i<s1.Length();i++)
 cout<<s1[i]<<endl;
 cout<<s1.Length()<<endl<<endl;
 MyString s2(s+s1);
 cout<<s2.Length();
 MyString s3;
 s3=s2;
 s3=s1+s2;
 cout<<s3;
 cin.get();
}

//6-26
#include "stdafx.h"
#include <iostream>
using namespace std;
void chang(int c[3][3]);
main()
{
 int c[3][3]={{1,2,3},{4,5,6},{7,8,9}};
 chang(c);
 for(int i=0;i<3;i++)
 {
  for(int j=0;j<3;j++)
  {
   cout<<c[i][j]<<" ";
  }
  cout<<endl;
 }

}
void chang(int c[3][3])
{
 int temp[3][3];
 for(int i=0;i<3;i++)
 {
  for(int j=0;j<3;j++)
  {
   temp[j][i]=c[i][j];
  }
 }
 for(int i=0;i<3;i++)
 {
  for(int j=0;j<3;j++)
  {
   c[i][j]=temp[i][j];
  }
 }

}

//6-27
#include "stdafx.h"
#include <iostream>
using namespace std;
void chang(int *c,int m,int n);
main()
{

 int c[3][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}};
 chang((int*)c,3,4);
 cin.get();
}
void chang(int *c,int m,int n)
{
 int* temp=new int[m*n];
 for(int i=0;i<m;i++)
 {
  for(int j=0;j<n;j++)
  {
   *(temp+(j*m+i))=*(c+i*n+j);
  }
 }
 for(int i=0;i<n;i++)
 {
  for(int j=0;j<m;j++)
  {
  cout<<temp[i*m+j]<<" ";
  }
        cout<<endl;
 }
}

//6-28
#include "stdafx.h"
#include <string>
using namespace std;
class employee
{
public:
 employee(char tgender,int tid,int tzipcode,string tcity,string taddress,string tname);
 employee(employee& otheremp);
 void chage_name(string newname);
 void dispaly();
private:
 char gender;
 int id;
 string address;
 string name;
 string city;
    int zipcode;
};
employee::employee(char tgender,int tid,int tzipcode,string tcity,string taddress,string tname)
{
 gender=tgender;
 id=tid;
 zipcode=tzipcode;
 city=tcity;
 address=taddress;
 name=tname;
}
void employee::chage_name(string newname){name=newname;}
void employee::dispaly()
{
 cout<<"姓名:"<<name<<" "<<gender<<endl;
 cout<<"  id:"<<id<<endl;
 cout<<"邮编:"<<zipcode<<" 城市:"<<city<<endl;
 cout<<"地址:"<<address<<endl<<endl;
}
main()
{
 employee jiaxun('m',20042703,611330,"绵阳","西南科技大学*****","张三");
 jiaxun.dispaly();
 jiaxun.chage_name("李四");
 jiaxun.dispaly();
}