一些关键字
¤ include
¤ iostream, math, string, iomanip
¤ cin, cout, new, delete
¤ class
¤ public, private, protected,const,static
¤ template
¤ friend
¤ operator
¤ virtual
1. 链表处理
#include <stdlib.h>
#include <iostream>
using namespace std;
struct Node{
int num;
string name;
int gra;
struct Node *next; };//链表
struct Node *Creat_Stu_Doc();
struct Node *Creat_Stu_Doc()//创建链表
{
Node* head=new Node;
Node* p=new Node;
Node* q=NULL;
head->next=p;//头结点
while(1)
{
cin>>p->num;//输入
if(p->num!=0)//判断是否是最后一个元素,也可用计数器
{
cin>>p->name;
cin>>p->gra;
q=new Node;//创建下一个结点
p->next=q;//将指向下一节点的指针放入next
p=p->next;//使指针p指向下一节点
}
else
{
p->next=NULL;//若为最后一个结点则将该结点的next赋值为NULL(作为链表结束标志)
break;
}
}
return head;
}
struct Node *DeleteDoc(struct Node *head,int min_score);
struct Node *DeleteDoc(struct Node *head,int min_score)// 删除结点
{
Node* p=NULL;
Node* q=NULL;
p=head;
while(p->next!=NULL)//结束条件
{
if((p->next->gra)<min_score&&p->next->num!=0)//寻找需要删除的结点,但不理会尾结点
{
q=p->next;
p->next=q->next;//将指向被删除结点的指针 替换 为指向被删除结点的下一节点的指针
delete q;//将被删除结点的内存空间释放
}
else p=p->next;//若未删除结点,则使指针p指向下一节点(若已删除结点,则下一节点自动更新)
}
return head;
}
void Ptrint_Stu_Doc(struct Node *head);
void Ptrint_Stu_Doc(struct Node *head)//打印结果
{
Node* p=NULL;
Node* q=NULL;
p=head->next;
while(p->next!=NULL)
{
cout<<p->num<<" "<<p->name<<" "<<p->gra<<endl;
p=p->next;
}
}
void delete_Stu_Doc(struct Node *head);
void delete_Stu_Doc(struct Node *head)
{
Node* p=NULL;
Node* q=NULL;
p=head;
while(p!=NULL)
{
q=p;
p=p->next;
delete q;
}//释放整条链表的内存
}
int main()
{
int min_score;
Node* head=NULL;
head=Creat_Stu_Doc();
cin>>min_score;
head=DeleteDoc(head,min_score);
Ptrint_Stu_Doc(head);
delete_Stu_Doc(head);
return 0;
}
}
struct Node *DeleteDoc(struct Node *head,int min_score);
struct Node *DeleteDoc(struct Node *head,int min_score)// 删除结点
{
Node* p=NULL;
Node* q=NULL;
p=head;
while(p->next!=NULL)//结束条件
{
if((p->next->gra)<min_score&&p->next->num!=0)//寻找需要删除的结点,但不理会尾结点
{
q=p->next;
p->next=q->next;//将指向被删除结点的指针 替换 为指向被删除结点的下一节点的指针
delete q;//将被删除结点的内存空间释放
}
else p=p->next;//若未删除结点,则使指针p指向下一节点(若已删除结点,则下一节点自动更新)
}
return head;
}
void Ptrint_Stu_Doc(struct Node *head);
void Ptrint_Stu_Doc(struct Node *head)//打印结果
{
Node* p=NULL;
Node* q=NULL;
p=head;
while(p->next->next!=NULL)
{
cout<<p->next->num<<" "<<p->next->name<<" "<<p->next->gra<<endl;
p=p->next;
}
p=head;
while(p->next!=NULL)
{
q=p;
p=p->next;
delete q;
}//释放整条链表的内存
delete p;
}
int main()
{
int min_score;
Node* head=NULL;
head=Creat_Stu_Doc();
cin>>min_score;
head=DeleteDoc(head,min_score);
Ptrint_Stu_Doc(head);
return 0;
}
2. 类的定义、设计和使用
3. 静态成员的使用
#include <iostream>
using namespace std;
const int N=3;
class Group{
private:
int num;
static int blackList[N];
static int size;//静态成员的定义
public:
Group();
Group(int num, bool bSign);
static void addToList(int num);//静态成员函数操作静态数据成员
static void removeFromList(int num);
static void displayList();
};
void Group::displayList(){
if(size==0) cout<<"NULL BLACKLIST!"<<endl;
else{
for(int i=0;i<size-1;i++) cout<<blackList[i]<<' ';
cout<<blackList[size-1]<<endl;
}
}
int Group::size=0;//静态成员的初始化
int Group::blackList[N]={0};
void Group::addToList(int num)
{
if(size<N)
{
blackList[size]=num;
size++;
}
else
{
blackList[0]=blackList[1];
blackList[1]=blackList[2];
blackList[2]=num;
}
}
void Group::removeFromList(int num)
{
int i;
for(i=0;i<size;i++)
{
if(num==blackList[i])break;
}
for(;i<size;i++)
{
blackList[i]=blackList[i+1];
}
size--;
}
Group::Group()
{}
Group::Group(int num, bool bSign)
:num(num)
{
if(bSign)addToList(num);
}
int main(){
int i, j, k, num, task, count=0;
Group g[100];
cin>>task;
while(task!=0){
switch(task){
case 1: cin>>num>>k;
if(k==999) {
g[count++]=Group(num, true);
cin>>task;
}else{
g[count++]=Group(num, false);
task = k;
}
break;
case 2: cin>>num;
Group::addToList(num);
Group::displayList();
cin>>task;
break;
case 3: cin>>num;
Group::removeFromList(num);
Group::displayList();
cin>>task;
break;
}
}
return 0;
}
4. 函数模板的使用
#include<iostream>
using namespace std;
template<class unmtype>//类模板
class MySet
{
private:
unmtype data[100];
int i;//记录元素个数
public:
MySet()
{i=0;}
void add(unmtype n);
void deleted(unmtype n);
void find(unmtype n);
};
template<class unmtype>//必须加
void MySet<unmtype>::add(unmtype n)
{
int j;
if(i==99) cout<<"Full Set."<<endl;
else
{
if(i==0)
{
cout<<"0"<<endl;
data[i]=n;
i++;
}
else
{
for(j=0;j<i;j++)
{
if(data[j]==n)
{
cout<<n<<" is already exist!"<<endl;//元素已存在
break;
}
}
if(j==i)
{
cout<<j<<endl;
data[j]=n;
i++;
}
}
}
}
template<class unmtype>
void MySet<unmtype>::deleted(unmtype n)
{
int j,k;
if(i==0)
{
cout<<n<<" is not exist!"<<endl;
}
else
{
for(j=0;j<i;j++)
{
if(data[j]==n) break;
}
if(j==i) cout<<n<<" is not exist!"<<endl;
else
{
for(k=j;k<i-1;k++)
{
data[k]=data[k+1];
}
cout<<j<<endl;
i--;
}
}
}
template<class unmtype>
void MySet<unmtype>::find(unmtype n)
{
int j;
if(i==0) cout<<n<<" is not exist!"<<endl;
else
{
for(j=0;j<i;j++)
{
if(data[j]==n)
{
cout<<j<<endl;
break;
}
}
if(j==i) cout<<n<<" is not exist!"<<endl;
}
}
int main()
{
int flag,b;
MySet<int>a;
MySet<float>a2;
MySet<string>a3;
cin>>flag;
while(flag!=0)
{
switch(flag)
{
case 1: {
int c;
cin>>b>>c;
switch(b)
{
case 1:a.add(c);break;
case 2:a.deleted(c);break;
case 3:a.find(c);break;
}
break;
}
case 2:{
float c;
cin>>b>>c;
switch(b)
{
case 1:a2.add(c);break;
case 2:a2.deleted(c);break;
case 3:a2.find(c);break;
}
break;
}
case 3:{
string c;
cin>>b>>c;
switch(b)
{
case 1:a3.add(c);break;
case 2:a3.deleted(c);break;
case 3:a3.find(c);break;
}
break;
}
}
cin>>flag;
}
return 0;
}
5. 运算符重载
6. 继承
#include <iostream>
#include <string>
#include <math.h>
#include <stdio.h>
using namespace std;
class Time{ //时间基类
public:
int second;
int minute;
int hour;
Time(int h=0,int mi=0,int s=0) //构造函数
:hour(h),minute(mi),second(s)
{}
void operator+=(int n) //运算符重载
{
this->second+=n;
this->turn(); //合法性转化函数
}
void operator-=(int n)
{
this->second-=n;
this->turn();
}
void turn() //合法性转化函数
{
if(this->second>=60||this->second<0)
{
this->minute=this->minute+(this->second+60)/60-1;
this->second=(this->second+60)%60;
}
if(this->minute>=60||this->minute<0)
{
this->hour=this->hour+(this->minute+60)/60-1;
this->minute=(this->minute+60)%60;
}
if(this->hour>=24||this->hour<0)
{
this->hour=(this->hour+24)%24;
}
}
};
class Time_12hours
:public Time{ //12小时制子类(24小时制转12小时制)
public:
Time_12hours(Time& r) //转换构造函数
{
if(r.hour>=12)type="PM";
else type="AM";
Time::hour=r.hour%12;
Time::minute=r.minute;
Time::second=r.second;
}
string type;
};
int main()
{
int h=0,mi=0,s=0,id=0,n;
char c;
while(1)
{
cin>>id;
if(id==0)break;
cin>>h>>mi>>s>>c>>n;
if(id==121) //12小时制 AM
{
Time a(h,mi,s); //12小时制转24小时制
if(c=='+')a+=n;
else a-=n;
Time_12hours b=a; //24小时制转12小时制
cout<<b.type<<" ";
printf("%02d:%02d:%02d\n",b.hour,b.minute,b.second);
}
else if(id==122)
{
Time a(h+12,mi,s);
if(c=='+')a+=n;
else a-=n;
Time_12hours b=a;
cout<<b.type<<" ";
printf("%02d:%02d:%02d\n",b.hour,b.minute,b.second);
}
else
{
Time a(h,mi,s);
if(c=='+')a+=n;
else a-=n;
printf("%02d:%02d:%02d\n",a.hour,a.minute,a.second);
}
}
return 0;
}
7. 多态
#include<iostream>
#include <string>
#include <math.h>
#include <stdio.h>
using namespace std;
class Pet{
public:
virtual void display(int goal)=0;//输出目标日期的身长和体重
};
class Cat:public Pet{
public:
string name;//姓名
int l;//身长
int w;//体重
int n;//当前日期
void display(int goal){cout<<name<<" "<<l+goal-n<<" "<<w+2*(goal-n)<<endl;}//输出目标日期的身长和体重
Cat(string n, int ll, int ww, int c)
:l(ll),w(ww),n(c){name=n;}
};
class Dog:public Pet{
public:
string name;//姓名
int l;//身长
int w;//体重
int n;//当前日期
void display(int goal){cout<<name<<" "<<l+2*(goal-n)<<" "<<w+1*(goal-n)<<endl;}//输出目标日期的身长和体重
Dog(string n, int ll, int ww, int c)
:l(ll),w(ww),n(c){name=n;}
};
int main()
{
Pet *pt[10];
int goal;//因为目标时间放在最后而被迫多态
int i,j,num,ca,count,lenth=0;
for(i=0;;i++)
{
string name;
cin>>ca;
if(ca!=1&&ca!=2){goal=ca;break;}
else if(ca==1)
{
int l,w,n;
cin>>name>>l>>w>>n;
pt[i]=new Cat(name,l,w,n);
}
else
{
int l,w,n;
cin>>name>>l>>w>>n;
pt[i]=new Dog(name,l,w,n);
}
}
for(j=0;j<i;j++)pt[j]->display(goal);
return 0;
}