1- 旅馆旅客管理(静态成员)
#include<iostream>
using namespace std;
#include<iomanip>
class Customer
{
public:
Customer(char* name) :CusterName(name) {
int len = strlen(name);
CusterName = new char[len + 1];
strcpy(CusterName, name);
TotalCusNum++;
CustId = TotalCusNum;
};
~Customer() {
if (CusterName != NULL) delete CusterName;
}
static void changeYear(int r) { Year = r; };
void Display()
{
cout << CusterName << " " << Year << setfill('0') << setw(4) << CustId << " " << TotalCusNum << " " << (Rent * TotalCusNum) << endl;
}
private:
static int TotalCusNum;
static int Rent;
static int Year;
int CustId;
char* CusterName;
};
int Customer::TotalCusNum = 0;
int Customer::Rent = 150;
int Customer::Year = 2014;
int main() {
int t;
cin >> t;
int num = 0;
while (t--) {
int year;
cin >> year;
Customer::changeYear(year);
while (1) {
char name[10];
cin >> name;
if (!strcmp(name, "0")) break;
Customer s1(name);
s1.Display();
}
}
2-距离计算(友元函数)
#include<iostream>
using namespace std;
#include<math.h>
class point
{
double x, y;
public:
point(double xx, double yy){x=xx;y=yy;};
friend double Distance(point& a, point& b);
};
double Distance(point& a, point& b) {
double s = sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
return s;
}
int main() {
int t;
cin >> t;
while (t--) {
double x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
point p1(x1, y1);
point p2(x2, y2);
double s=Distance(p1,p2);
cout<<(int)s<<endl;
}
}
3- 日期时间合并输出(友元函数)
#include<iostream>
#include<iomanip>
using namespace std;
class Time;
class Date {
int year;
int month;
int day;
public:
Date(int y,int m,int d) {
year=y;
month=m;
day=d;
}
friend void Display(Date &, Time &);
};
class Time {
int hour;
int minute;
int second;
public:
Time(int h,int m,int s) {
hour=h;
minute=m;
second=s;
}
friend void Display(Date &, Time &);
};
void Display(Date &D, Time &T){
cout<<D.year<<"-"<< setfill('0') << setw(2) <<D.month<<"-"<<setfill('0') << setw(2)<<D.day<<" "
<<setfill('0') << setw(2)<<T.hour<<":"<<setfill('0') << setw(2)<<T.minute<<":"<<setfill('0') << setw(2)<<T.second<<endl;
}
int main() {
int t;
cin>>t;
while(t--){
int year,month,day,hour,minute,second;
cin>>year>>month>>day>>hour>>minute>>second;
Date D(year,month,day);
Time T(hour,minute,second);
Display(D,T);
}
}
4- 判断矩形是否重叠(复合类+友元)
#include<iostream>
#include<iomanip>
using namespace std;
class Crect;
class Cpoint {
int x;
int y;
public:
friend Crect;
Cpoint() { }
Cpoint(int xx, int yy) {
x = xx;
y = yy;
}
int getx() { return x; }
int gety() { return y; }
void setx(int x1) { x = x1; }
void sety(int y1) { y = y1; }
void set(Cpoint& p) {
this->x = p.x;
this->y = p.y;
}
};
class CRect {
Cpoint p1;
Cpoint p2;
public:
CRect(Cpoint& p11, Cpoint& p22) {
p1.set(p11);
p2.set(p22);
}
void change() {
int xx=p1.getx(), yy=p1.gety(), xxx=p2.getx(), yyy=p2.gety();
if (p1.getx() > p2.getx()) {
xxx= p1.getx();
xx = p2.getx();
}
if (p1.gety() < p2.gety()) {
yy = p2.gety();
yyy = p1.gety();
}
p1.setx(xx);
p1.sety(yy);
p2.setx(xxx);
p2.sety(yyy);
}
friend void iflapp(CRect& C1, CRect& C2);
};
void iflapp(CRect& C1, CRect& C2) {
if (C1.p1.getx() > C2.p2.getx() || C1.p2.getx() <C2.p1.getx() || C1.p1.gety() < C2.p2.gety() || C1.p2.gety() > C2.p1.gety())
cout << "not overlapped" << endl;
else
cout << "overlapped" << endl;
}
int main() {
int t, x1, y1, x2, y2, x3, y3, x4, y4;
cin >> t;
while (t--) {
cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4;
Cpoint p1(x1, y1);
Cpoint p2(x2, y2);
Cpoint p3(x3, y3);
Cpoint p4(x4, y4);
CRect C1(p1, p2);
CRect C2(p3, p4);
C1.change();
C2.change();
iflapp(C1, C2);
}
}// cout << C1.p1.getx() << " " << C1.p1.gety() << " " << C1.p2.getx() << " " << C1.p2.gety() << endl;
//cout << C2.p1.getx() << " " << C2.p1.gety() << " " << C2.p2.getx() << " " << C2.p2.gety() << endl;
5-银行账户(静态成员与友元函数)
#include <iostream>
#include <string>
using namespace std;
class Account {
public:
Account(string accno, string accname, float balance);
~Account() { count--; }
void Deposit(float amount){ _balance += amount; }
void Withdraw(float amount){ _balance -= amount; }
float GetBalance() { return _balance; }
void Show() { cout << _accno << ' ' << _accname << ' ' << _balance; }
static int GetCount(){ return count; }
static float GetInterestRate() { return InterestRate; }
static void SetInterestRate(float rate) { InterestRate = rate; }
friend void Update(Account& a){ a._balance += a._balance * Account::InterestRate; }
private:
static int count;
static float InterestRate;
string _accno, _accname;
float _balance;
};
int Account::count = 0;
float Account::InterestRate = 0;
Account::Account(string accno, string accname, float balance)
:_accno(accno), _accname(accname), _balance(balance) {
count++;
}
int main() {
float rate, sum = 0;
int count;
cin >> rate >> count;
Account::SetInterestRate(rate);
Account** p = new Account * [count];
for (int i = 0; i < count; i++) {
string accno, accname;
float surplus, deposit, withdraw;
cin >> accno >> accname >> surplus >> deposit >> withdraw;
p[i] = new Account(accno, accname, surplus);
p[i]->Deposit(deposit);
p[i]->Show();
Update(*p[i]);
cout << ' ' << p[i]->GetBalance();
p[i]->Withdraw(withdraw);
cout << ' ' << p[i]->GetBalance() << endl;
sum += p[i]->GetBalance();
delete p[i];
}
cout << sum << endl;
delete[]p;
return 0;
}
6-电视机与遥控器(友元类)
//找不到了
#include<iostream>
#include<iomanip>
#include<cstring>
using namespace std;
class Customer{
private:
static int TotalCustNum;
static int Year;
static int Rent;
char* CustName;
int CustID;
public:
Customer(char* name){
TotalCustNum++;
CustID=TotalCustNum;
CustName=new char[strlen(name)+1];
strcpy(CustName,name);
}
~Customer(){delete[] CustName;}
static void changeYear(int r){ Year=r; }
void print(){
cout<<CustName<<" "<<Year<<setw(4)<<setfill('0')<<CustID<<" "<<CustID<<" "<<Rent*TotalCustNum<<endl;
}
};
int Customer::TotalCustNum=0;
int Customer::Rent=150;
int Customer::Year=2014;
int main()
{ int t;
char name[10];
int year;
cin>>t;
while(t--)
{ cin>>year;
Customer::changeYear(year);
while(1){
cin>>name;
if(name[0]=='0') //换种写法:if(!strcmp(name,"0"))
break;
Customer C(name);
C.print();
}
}
return 0;
}
///
用string
#include<iostream>
#include<iomanip>
using namespace std;
class Customer{
private:
static int TotalCustNum;
static int Year;
static int Rent;
string CustName;
int CustID;
public:
Customer(string name):CustName(name){
TotalCustNum++;
CustID=TotalCustNum;
}
~Customer(){}
static void changeYear(int r){ Year=r; }
void print(){
cout<<CustName<<" "<<Year<<setw(4)<<setfill('0')<<CustID<<" "<<CustID<<" "<<Rent*TotalCustNum<<endl;
}
};
int Customer::TotalCustNum=0;
int Customer::Rent=150;
int Customer::Year=2014;
int main()
{ int t;
string name;
int year;
cin>>t;
while(t--)
{ cin>>year;
Customer::changeYear(year);
while(1){
cin>>name;
if(name=="0")
break;
Customer C(name);
C.print();
}
}
return 0;
}
=====================================================
#include<iostream>
#include<cmath>
using namespace std;
class Point{
double x,y;
public:
Point(double xx,double yy):x(xx),y(yy){};
friend double Distance(Point &a,Point &b);
};
double Distance(Point &a,Point &b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int main()
{
int t;
cin>>t;
while(t--)
{
double x1,y1,x2,y2;
cin>>x1>>y1>>x2>>y2;
Point a(x1,y1),b(x2,y2);
cout<<(int)(Distance(a,b))<<endl;
}
return 0;
}
=====================================================
#include<iostream>
#include<iomanip>
using namespace std;
class Time;
class Date {
int y, m, d;
public:
Date(int y,int m, int d):y(y),m(m),d(d){ }
friend void print(Date& p1,Time& p2);
};
class Time {
int h, n, s;
public:
Time(int h, int n, int s):h(h),n(n),s(s){ }
friend void print(Date& p1,Time& p2);
};
void print(Date& p1,Time& p2)
{ cout<<setfill('0')<<p1.y<<"-"<<setw(2)<<p1.m<<"-"<<setw(2)<<p1.d<<" ";
cout<<setfill('0')<<setw(2)<<p2.h<<":"<<setw(2)<<p2.n<<":"<<setw(2)<<p2.s<<endl;
}
int main() {
int t, y2,m2,d2,h2,n2,s2;
cin>>t;
while(t--){
cin>>y2>>m2>>d2;
cin>>h2>>n2>>s2;
Date p(y2,m2,d2);
Time pp(h2,n2,s2);
print(p,pp);
}
return 0;
}
=====================================================
#include<iostream>
using namespace std;
class rect;
class Spoint
{ int x,y;
public:
Spoint(){}
Spoint(int x,int y):x(x),y(y){}
friend void judge(const rect &A, const rect &B);
};
class rect
{ Spoint a,b;
public:
rect(){}
//rect(Spoint& a,Spoint& b):a(a),b(b){}
rect(int x1,int y1,int x2,int y2):a(x1,y1),b(x2,y2){}
friend void judge(const rect &A, const rect &B);
};
void judge(const rect &A, const rect &B)
{ //反证法
if((A.a.x > A.b.x ? A.a.x: A.b.x) < (B.a.x < B.b.x ? B.a.x: B.b.x)||
(A.a.x < A.b.x ? A.a.x: A.b.x) > (B.a.x > B.b.x ? B.a.x: B.b.x)||
(A.a.y < A.b.y ? A.a.y: A.b.y) > (B.a.y > B.b.y ? B.a.y: B.b.y)||
(A.a.y > A.b.y ? A.a.y: A.b.y) < (B.a.y < B.b.y ? B.a.y: B.b.y))
cout<<"not overlapped" <<endl;
else
cout<<"overlapped"<<endl;
}
int main()
{
int t;
cin >> t;
for(int i = 0 ; i < t ; i++)
{
int x1,x2,y1,y2;
cin >> x1 >> y1 >> x2 >> y2;
rect A(x1,y1,x2,y2);
cin >> x1 >> y1 >> x2 >> y2;//创建矩形B
rect B(x1,y1,x2,y2);
judge(A,B);
}
return 0;
}
=====================================================
#include<iostream>
using namespace std;
class Account
{
static int count;
static float InterestRate;
string accon,accname;
float balance;
public:
Account(){count++;}
Account(string _accon,string _accname,float _balance):accon(_accon),accname(_accname),balance(_balance)
{count++;}
~Account(){count--;}
void setData(string accon1,string accname1,float balance1){
accon=accon1;
accname=accname1;
balance=balance1;
}
static void setInterestRate(float inter){
InterestRate=inter;
}
void Deposit(float amount){ //存款
balance+=amount;
}
void Withdraw(float amount){ //取款
if(amount<=balance)
balance-=amount;
}
float GetBalance(){
return balance;
}
static int GetCount(){
return count;
}
static float GetInterestRate(){
return InterestRate;
}
void Show(float deposit,float withdraw){//账号、姓名、存款后的余额、存款后结息余额、取款后余额。
cout<<accon<<" "<<accname<<" ";
Deposit(deposit);
cout<<balance<<" ";
Update(*this);
cout<<balance<<" ";
Withdraw(withdraw);
cout<<balance<<endl;
}
friend void Update(Account& a);
};
int Account::count=0;
float Account::InterestRate=0;
void Update(Account& a){//(结息余额=账户余额+账户余额*利率)。
a.balance=a.balance*(1+Account::InterestRate);
}
int main()
{
float interest,dep,withd,all=0,balance;
int t,i;
string name,num;
cin>>interest;
Account::setInterestRate(interest);
cin>>t;
Account *a=new Account[t];
for(i=0;i<t;i++)
{
cin>>num>>name>>balance>>dep>>withd;
a[i].setData(num,name,balance);
a[i].Show(dep,withd);
all+=a[i].GetBalance();
}
cout<<all<<endl;
delete []a;
return 0;
}
=====================================================
#include<iostream>
#include<string>
using namespace std;
class Tv
{ int state;
int volume;
int maxchannel;
int channel;
int mode;
int input;
string sstate[2]={"off","on"};
string smode[2]={"Cable","Antenna"};
string sinput[2]={"VCR","TV"};
public:
Tv():state(0),volume(0),maxchannel(100),channel(0),mode(0),input(0){}
Tv(int s, int v,int c,int m,int i):state(s),maxchannel(100),volume(v),channel(c),mode(m),input(i){}
void onoff()
{
state=1-state;
}
bool ison()const
{
return state?true:false;
}
bool volup()
{
if(volume<20)
return ++volume;
}
bool voldown()
{
if(volume>0)
return --volume;
}
void chanup()
{
channel++;
}
void chandown()
{
channel--;
}
void set_mode()
{
mode=1-mode;
}
void set_input()
{
input=1-input;
}
void settings()const
{
cout << sstate[state] << " "<<volume << " " << channel << " "<< smode[mode] << " "<< sinput[input] <<endl;
}
friend class Remote;
};
class Remote
{
int mode;
public:
Remote(int m):mode(m){}
bool volup(Tv &t)
{ return t.volup(); }
bool voldown(Tv &t)
{ return t.voldown(); }
void onoff(Tv &t)
{ t.onoff(); }
void chanup(Tv &t)
{ t.chanup(); }
void chandown(Tv &t)
{ t.chandown(); }
void set_chan(Tv &t,int c)
{ t.channel = c; }
void set_mode(Tv &t)
{ t.set_mode(); }
void set_input(Tv &t)
{ t.set_input(); }
};
int main()
{
string s, m, i;
int s1, m1, i1;
int v,c;
cin >> s >> v >> c >> m >> i;
s1=(s == "off")?0:1;
m1=(m == "Cable")?0:1;
i1=(i == "VCR")?0:1;
Remote tv(m1);
Tv t(s1, v, c, m1, i1);
string s2, v2, c2, m2, i2;
cin >> s2 >> v2 >> c2 >> m2 >> i2;
if (s2 == "onoff")
tv.onoff(t);
if (!t.ison()) { //电视处于关机状态,则直接输出信息,然后结束
t.settings();
return 0;
}
if (v2 == "volup")
tv.volup(t);
else
tv.voldown(t);
if (c2 == "chanup")
tv.chanup(t);
else
tv.chandown(t);
if (m2 == "set_mode")
tv.set_mode(t);
if (i2 == "set_input")
tv.set_input(t);
t.settings();
return 0;
}