<typenpname T>/T& ARRAY<T>/index>=m_size/throw/try
friend/ostream&/operator -/cl.area/cin>>cl.rad/"rad="<<cl.rad<<" area="<<cl.area
学生类的构造与析构:
#include<string>
#include<iostream>
using namespace std;
class Student
{
private :
int num;
string name;
char sex;
public :
Student(int n,string nam,char s)
{num=n;
name=nam;
sex=s;
cout<<"Constructor called."<<endl;
}
~Student( )
{cout<<"Destructor called."<<endl;}
void display( )
{cout<<"num:"<<num<<endl;
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl<<endl; }
};
体育俱乐部I(构造函数):
void Coach::show()
{
cout<<name<<" "<<winRate<<"%";
}
Club::Club(string n1, int y, string n2, int wr):c(n2,wr)
{
year=y;
name=n1;
}
void Club::show()
{
cout<<name<<" "<<year<<endl;
c.show();
}
2017final友元函数之全班同学的平均绩点:
double averagegrade(student *stu, int count)
{
int i,j;
double sum1=0,sum2=0,sum;
for(i==0;i<count;i++)
for(j=0;stu[i].score[j]!=-1;j++)
{
sum1+=stu[i].score[j]*(stu[i].grade[j]/10-5);
sum2+=stu[i].score[j];
}
if(sum2==0||sum1==0)
return 0;
return sum1/sum2;
}
大整数求和(运算符重载):
include<algorithm>
class BigInt
{
private:
string str;
public:
BigInt() {}
BigInt(string s)
{
str = s;
}
friend istream& operator>>(istream& is,BigInt &b);
friend ostream& operator<<(ostream& os,const BigInt &b);
friend BigInt operator + (const BigInt &b1,const BigInt &b2);
};
istream& operator>>(istream& is,BigInt &b)
{
is >> b.str;
return is;
}
ostream& operator<<(ostream& os,const BigInt &b)
{
os << b.str;
return os;
}
BigInt operator + (const BigInt &b1,const BigInt &b2)
{
BigInt B;
string s1 = b1.str;
string s2 = b2.str;
reverse(s1.begin(),s1.end());//将两个大数逆序
reverse(s2.begin(),s2.end());
int num = 0;
int num1[110] = {0};
int num2[110] = {0};
int sum[110] = {0};
int len = 0;
int len1 = s1.size();
int len2 = s2.size();
if(len1 >= len2)
len = len1;
else
len = len2;
for(int i = 0; i < len1; i++)
{
num1[i] = s1[i] - '0';
}
for(int i = 0; i < len2; i++)
{
num2[i] = s2[i] - '0';
}
for(int i = 0; i < len; i++)
{
num = num1[i] + num2[i] + sum[i];
int flag = num / 10;
if(flag == 0)
{
sum[i] = num;
}
else
{
sum[i+1] = 1;
sum[i] =num- 10;
}
}
string STR;//将整数数组转化为字符串
for(int i = 0; i < len; i++)
{
char ch = sum[i] + '0';
STR = STR + ch;
}
if(sum[len] == 1)//判断最高位
{
char ch = 1 + '0';
STR = STR + ch;
}
reverse(STR.begin(),STR.end());//重新逆序
B.str = STR;
return B;
}
时间相加:
Time::Time(int h,int m,int s)
{
hours=h;
minutes=m;
seconds=s;
}
void Time::DispTime()
{
cout<<hours<<"h:"<<minutes<<"m:"<<seconds<<"s"<<endl;
}
Time Time::operator +(Time & k)
{
Time temp;
temp.minutes=this->minutes+k.minutes;
temp.seconds=this->seconds+k.seconds;
temp.hours=this->hours+k.hours;
if(temp.seconds>59)
{
temp.seconds-=60;
temp.minutes++;
}
if(temp.minutes>59)
{
temp.minutes-=60;
temp.hours++;
}
return temp;
}
派生类的定义和使用:
class Animal{
public:
void speak(){
cout<<"animal language!"<<endl;
}
};
class Cat:public Animal{
private:
string m_strName;
public:
Cat(string m_strName):m_strName(m_strName){}
void print_name(){
cout<<"cat name: "<<m_strName<<endl;
}
};
多重继承派生类构造函数:
class Graduate:public Teacher,public Student
{
public:
Graduate(string nam,int a,char s,string t,float sco,float w)
:Student(nam,s,sco),Teacher(nam,a,t)
{
wages=w;
}
void show()
{
cout<<"name:"<<name<<endl;
cout<<"age:"<<age<<endl;
cout<<"sex:"<<sex<<endl;
cout<<"score:"<<score<<endl;
cout<<"title:"<<title<<endl;
cout<<"wages:"<<wages<<endl;
}
private:
float wages;
};
虚函数的应用:
class CMyClassB:public CMyClassA{
int va;
public:
CMyClassB(int v):CMyClassA(3*v){
va=v;
cout<<"B:"<<va<<endl;
}
void print(){
cout<<va<<endl;
}
};
数组排序输出(函数模板):
template <class T>
void sort(T *data,int size)
{
int i,j,min;
T t;
for(i=0; i<size; i++)
cin>>data[i];
for(i=0; i<size-1; i++)
{
min=i;
for(j=i+1; j<size; j++)
if(data[j]<data[min])
min=j;
t=data[i];
data[i]=data[min];
data[min]=t;
}
}
筛法求质数:
vector<int> sieve(int n)
{
vector<int>a;
int i,j,*flag=new int[n+1];
for(i=2; i<=n; i++)
flag[i]=1;
for(i=2; i*i<=n; i++)
{
if(flag[i])
{
for(j=i+i; j<=n; j+=i)
flag[j]=0;
}
}
for(i=2; i<=n; i++)
if(flag[i])
a.push_back(i);
return a;
}
例4-3游泳池改造预算:
#include<iostream>
using namespace std;
class circle
{
public :
const double PI=3.1415926;
void setr(double r);
void langan(float r)
{
float c;
c=70*PI*(r+3);
cout<<"Fencing Cost is $"<<c<<endl;
}
void guodao(float r)
{
float s1;
s1=PI*r*r;
float s2;
s2=PI*(r+3)*(r+3);
cout<<"Concrete Cost is $"<<(s2-s1)*20<<endl;
}
};
int main()
{
circle y1;
double r;
cin>>r;
y1.langan(r);
y1.guodao(r);
return 0;
}
复数类的操作:
#include<iostream>
using namespace std;
class Complex{
double real,image;
public:
Complex()
{
real=0;
image=0;
}
void get(double r,double i)
{
real=r;
image=i;
}
void fan()
{
real=-real;
image=-image;
}
void Print()
{
cout<<"("<<real<<", "<<image<<")"<<endl;
}
friend void jiao(Complex ob1,Complex ob2)
{
cout<<"("<<ob1.real+ob2.real<<", "<<ob1.image+ob2.image<<")"<<endl;
}
};
int main(void)
{
double c11,c12,c21,c22;
cin>>c11>>c12>>c21>>c22;
Complex ob1,ob2;
ob1.get(c11,c12);
ob2.get(c21,c22);
jiao(ob1,ob2);
ob2.fan();
jiao(ob1,ob2);
ob2.fan();
ob2.Print();
}
运算符重载:
#include<iostream>
#include<math.h>
using namespace std;
class Fs{
int fz,fm;
public:
void display();
Fs(int x = 0, int y = 0);
Fs operator+(Fs &op2)
{
int max = (this->fm > op2.fm) ? this->fm : op2.fm;
do
{
if (max % this->fm == 0 && max % op2.fm == 0)
{
break;
}
else
++max;
} while (true);
Fs temp;
this->fz *= max / this->fm;
op2.fz *= max / op2.fm;
this->fz += op2.fz;
temp.fz=this->fz;
temp.fm=max;
max = 1;
for (int i = 1; i <= abs(this->fz); i++)
{
if (temp.fz % i == 0 && temp.fm%i == 0)
max = i;
}
temp.fm /= max;
temp.fz/= max;
return temp;
}
};
Fs::Fs(int x,int y){
fz = x;
fm = y;
}
void Fs::display(){
cout << fz;
if(fm!=1&&fz!=0)
cout << " " << fm;
}
int main(){
int a[4];
cin >> a[0] >> a[1] >> a[2] >> a[3];
Fs x1(a[0],a[1]), x2(a[2],a[3]),x3;
x3 = x1 + x2;
x3.display();
}
日程安排(多重继承+重载):
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<string>
#include<vector>
#include<iomanip>
#include<iostream>
using namespace std;
class Date
{
protected:
int year;
int month;
int day;
public:
Date(int a = 0, int b = 0, int c = 0):year(a), month(b), day(c) {};
};
class Time
{
protected:
int hour;
int minute;
int second;
public:
Time(int a = 0, int b = 0, int c = 0):hour(a), minute(b), second(c) {};
};
class Schedule : protected Date, protected Time
{
public:
bool operator < (const Schedule & s)
{
if(year<s.year)
{
return true;
}
if(year==s.year&&month<s.month)
{
return true;
}
if(year==s.year&&month==s.month&&day<s.day)
{
return true;
}
int sum1=hour*3600+minute*60+second;
int sum2=s.hour*3600+s.minute*60+s.second;
if(year==s.year&&month==s.month&&day==s.day&&sum1<sum2)
{
return true;
}
return false;
}
Schedule(int y = 99999, int m = 0, int d = 0, int h = 0, int min = 0, int s = 0, int id = 0)
:Date(y, m, d), Time(h, min, s)
{
ID=id;
};
void show()
{
cout<<ID<<": "<<year<<"/"<<month<<"/"<<day<<" "<<hour<<":"<<minute<<":"<<second<<endl;
}
protected:
int ID;
};
int main()
{
int y,m,d,h,min,s,id;
Schedule mx;
cin>>id;
while(id!=0)
{
scanf("%d/%d/%d%d:%d:%d",&y,&m,&d,&h,&min,&s);
Schedule sch(y,m,d,h,min,s,id);
if(!(mx<sch))
{
mx=sch;
}
cin>>id;
//sch.show();
}
cout<<"The urgent schedule is No.";
mx.show();
return 0;
}
汽车收费:
#include<iostream>
include<string>
using namespace std;
class Vehicle
{
protected:
string NO;//编号
public:
Vehicle(string n)
{
NO = n;
}
virtual int fee()=0;//计算应收费用
};
class Car:public Vehicle
{
int guest;
int weight;
public:
Car(string no,int g,int w):Vehicle(no)
{
guest=g;
weight=w;
}
int fee()
{
return guest*8+weight*2;
}
};
class Truck:public Vehicle
{
int weight;
public:
Truck(string no,int w):Vehicle(no)
{
weight=w;
}
int fee()
{
return weight*5;
}
};
class Bus:public Vehicle
{
int guest;
public:
Bus(string no,int g):Vehicle(no)
{
guest=g;
}
int fee()
{
return guest*3;
}
};
int main()
{
Car c("",0,0);
Truck t("",0);
Bus b("",0);
int i, ty, weight, guest;
string no;
while(1)
{
cin>>ty;
if(ty==0)
break;
cin>>no;
switch(ty)
{
case 1:
cin>>guest>>weight;
c=Car(no, guest, weight);
cout<<no<<' '<<c.fee()<<endl;
break;
case 2:
cin>>weight;
t=Truck(no, weight);
cout<<no<<' '<<t.fee()<<endl;
break;
case 3:
cin>>guest;
b=Bus(no, guest);
cout<<no<<' '<<b.fee()<<endl;
break;
}
}
return 0;
}
师生信息管理:
#include<iostream>
using namespace std;
class Person
{
protected:
int NO;//编号
public:
virtual void display()=0;//输出相关信息
};
class Student:public Person
{
int score[5];
int pd;
double sum;
public:
Student(int n,int a,int b,int c,int d,int e)
{
sum=pd=0;
NO=n;
int k[5]={a,b,c,d,e};
for(int i=0;i<5;i++)
{
score[i]=k[i];
if(score[i]==-1)
pd++;
else
sum+=score[i];
}
}
virtual void display()
{
cout<<NO<<" "<<pd;
if(pd!=5)
printf(" %.1f",sum/(5-pd));
cout<<endl;
}
};
class Teacher:public Person
{
int num[3];
int sum;
public:
Teacher(int n,int a,int b,int c)
{
NO=n;
sum=0;
int k[3]={a,b,c};
for(int i=0;i<3;i++)
{
num[i]=k[i];
sum+=num[i];
}
}
virtual void display()
{
cout<<NO<<" "<<sum<<endl;
}
};
int main()
{
Person *pp[10];
int type;
cin>>type;
int n,a,b,c,d,e;
int i=0;
while(type!=0)
{
cin>>n;
switch(type)
{
case 1:
cin>>a>>b>>c>>d>>e;
pp[i]=new Student(n,a,b,c,d,e);
break;
case 2:
cin>>a>>b>>c;
pp[i]=new Teacher(n,a,b,c);
}
pp[i]->display();
i++;
cin>>type;
}
}
数据的最大值问题(重载+函数模板):
include<iostream>
include<cmath>
include<sstream>
include<string>
using namespace std;
int main()
{
int ty;
cin>>ty;
while(ty!=-1)
{
if(ty==1)
{
int n,m=-1e9;//设置m为最小的一个数
while(cin>>n&&n!=0)
{
m=max(n,m);
}
cout<<m<<endl;
}
if(ty==2)
{
double n,m=-1e9;
while(cin>>n&&n!=0)
{
m=max(n,m);
}
cout<<m<<endl;
}
if(ty==3)
{
int a,b,c,s1=0;
int d,e,g,s2=0;
string s;
getline(cin,s);
stringstream ss(s);
while(ss>>a>>b>>c)
{
s1=a*3600+b*60+c;
if(s1>s2)
{
d=a,e=b,g=c;
}
a=0,b=0,c=0;
}
cout<<d<<" "<<e<<" "<<g<<endl;
}
if(ty==4)
{
int a,b,c,s1=0;
int d=0,e=0,g=0,s2=0;
string s;
getline(cin,s);
stringstream ss(s);
while(ss>>a>>b>>c)
{
if(a>d)
{
d=a,e=b,g=c;
}
else if(a==d)
{
if(b>e)
{
d=a,e=b,g=c;
}
else if(b==e)
{
if(c>g)
{
d=a,e=b,g=c;
}
}
}
a=0,b=0,c=0;
}
cout<<d<<" "<<e<<" "<<g<<endl;
}
cin>>ty;
}
return 0;
}
数据的间距问题:
#include<iostream>
#include<cmath>
using namespace std;
class Complex
{
private:
double a, b;
public:
Complex()
{
a = 0;
b = 0;
}
Complex(double x, double y)
{
a = x;
b = y;
}
};
template < class T >
double dist(T a, T b)
{
return abs(a-b);
}
double s(double m1, double m2)
{
return pow(abs(m2-m1), 2);
}
int main()
{
int n;
while(cin >> n, n != 0)
{
if(n == 1)
{
int a, b;
cin >> a >> b;
cout << dist(a, b) << endl;
}
else if(n == 2)
{
float a, b;
cin >> a >> b;
cout << dist(a, b) << endl;
}
else if(n == 3)
{
double a, b;
double a1, b1, a2, b2;
cin >> a1 >> b1 >> a2 >> b2;
a = s(a1, a2);
b = s(b1, b2);
cout << sqrt(a+b) << endl;
}
}
return 0;
}
组最大数:
include<iostream>
include<algorithm>
#include<vector>
using namespace std;
bool campare(string i, string j)
{
return (i+j) > (j+i);
}
int main()
{
int n;
while(cin >> n)
{
vector<string> tmp(n, "");
for(int i=0; i<n; i++)
{
cin >> tmp[i];
}
sort(tmp.begin(), tmp.end(), campare);
for(int i=0; i<n; i++)
{
cout << tmp[i];
}
cout << endl;
}
}
对称排序:
#include<iostream>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<string>
#include<vector>
#include<iomanip>
using namespace std;
int main()
{
int n,i,j=1,k=1;
cin>>n;
while(n)
{
cout<<"SET "<<j++<<endl;
string s;
vector<string> a;
for(i=0; i<n; i++)
{
cin>>s;
a.push_back(s);
}
for(i=0; i<n; i=i+2)
{
cout<<a[i]<<endl;
}
if(n%2==0)
{
i=n-1;
}
else
{
i=n-2;
}
for(; i>0; i=i-2)
{
cout<<a[i]<<endl;
}
cin>>n;
}
return 0;
}
pta代码分享