}
intsame(intage)
{
if(age==_age)return1;
return0;
}
};
classLinkedList
{
public:
node*Node;
LinkedList*next;
LinkedList(intnum,intage,stringname,stringsexy)
{
Node=newnode(num,age,name,sexy);
next=NULL;
}
};
intmain(void)
{
intnum,age;
stringname,sexy;
cin>>num>>name>>sexy>>age;
LinkedListhead(num,age,name,sexy);
LinkedList*p=&head;
for(inti=1;i<=4;i++)
{
cin>>num>>name>>sexy>>age;
p->next=newLinkedList(num,age,name,sexy);
p=p->next;
}
cin>>age;
LinkedList*pp=&head;
intflag=0;
while(pp!
=NULL)
{
if(pp->Node->same(age))flag++;
elsepp->Node->display();
pp=pp->next;
}
if(!
flag)
{
noden(180,age,"aaa","male");
n.display();
}
return0;
}
示例代码-------------------------------------------------------------------------------------------------------
#include
#include
usingnamespacestd;
constintMAX_STR_LEN=32;
structStuNode
{
intID;
charname[MAX_STR_LEN];
chargender[MAX_STR_LEN];
intage;
structStuNode*next;
};
typedefstructStuNode*StuLink;
voidCreateHeadNode(StuLink&pHead,StuLink&pTail)
{
pHead=newStuNode;
if(pHead==NULL)return;
pHead->next=NULL;
pTail=pHead;
}
voidAddStudent(StuLink&pTail,StuNodedata)
{
StuLinkp;
p=newStuNode;
if(p==NULL)return;
p->ID=data.ID;
strcpy(p->name,data.name);
strcpy(p->gender,data.gender);
p->age=data.age;
p->next=NULL;
//Makethecurrentnodepointerpointtothelastaddeddata
pTail->next=p;
pTail=p;
}
boolRemoveStudent(StuLink&pHead,StuLink&pTail,intage)
{
boolbFind=false;
StuLinkpCurr,pPre;
pPre=pHead;
//Skiptothesecondnodebecausethefirstnodeistheheadnodewithoutdata
pCurr=pHead->next;
while(pCurr!
=NULL)
{
if(pCurr->age==age)
{
pPre->next=pCurr->next;
deletepCurr;
//Recoverthecurrentpointertofindthenextnodethathasthesamekey
pCurr=pPre->next;
bFind=true;
}
else
{
//Movetothenextnode
pPre=pCurr;
pCurr=pCurr->next;
}
}
//Recoverthetailpointerincasethelastnodeisdeleted
if(bFind&&pPre->next==NULL)pTail=pPre;
returnbFind;
}
voidOutputAllStudentsInfo(constStuLinkpHead)
{
StuLinkp;
//Skiptothesecondnodebecausethefirstnodeistheheadnodewithoutdata
p=pHead->next;
if(p==NULL)return;
while(p!
=NULL)
{
cout<ID<cout<name<cout<gender<cout<age<p=p->next;
}
}
voidInputStudentInfo(StuNode&Emp)
{
cin>>Emp.ID>>Emp.name>>Emp.gender>>Emp.age;
while(Emp.ID>=200||Emp.ID<100||
strlen(Emp.name)>=20||
strlen(Emp.gender)>=10||
Emp.age<0||Emp.age>=200)
{
cin>>Emp.ID>>Emp.name>>Emp.gender>>Emp.age;
}
}
intmain()
{
StuLinkEmpInstHead,EmpInstTail;
StuNodeEmpTemp;
inti,age;
//Createaheadnodeforthelinkedlist
CreateHeadNode(EmpInstHead,EmpInstTail);
for(i=0;i<5;i++)
{
InputStudentInfo(EmpTemp);
AddStudent(EmpInstTail,EmpTemp);
}
cin>>age;
//Removeaheadnodefromthelinkedlist
if(!
RemoveStudent(EmpInstHead,EmpInstTail,age))
{
EmpTemp.age=age;
EmpTemp.ID=180;strcpy(EmpTemp.name,"aaa");strcpy(EmpTemp.gender,"male");EmpTemp.next=NULL;
AddStudent(EmpInstTail,EmpTemp);
}
OutputAllStudentsInfo(EmpInstHead);
return0;
}
-------------------------------------------------------------------------------------------------------------------------
实验题目(共4题,第1题)
标题:
1.函数重载
时限:
1000ms
内存限制:
10000K
总时限:
3000ms
描述:
设计一菜单程序,利用函数重载实现员工月工资的计算,计算方法如下:
(1)管理人员的月工资=月薪-缺勤天数×月薪÷22;
(2)销售人员的月工资=底薪+销售金额×提成比例;
(3)计件工人的月工资=产品件数×每件报酬;
(4)计时工人的月工资=工作小时×小时报酬;
输入:
职工类别及相关信息。
职工类别:
1表示管理人员;2表示销售人员;3表示计件工人;4表示计时工人;其余字符表示退出。
相关信息:
若为管理人员,则输入月薪和缺勤天数;若为销售人员,则输入底薪、销售金额和提成比例;若为计件工人,则输入产品件数和每件报酬;若为计时工人,则输入工作小时和小时报酬。
输出:
员工月工资。
输入样例:
1〈--职工类别
5000.01〈--月薪和缺勤天数
输出样例:
4772.73
提示:
1.计算管理人员、销售人员、计件工人、计时工人的月工资的函数原型可以分别设计如下:
doublegetEarning(doublesalary,intabsenceDays);
doublegetEarning(doublebaseSalary,doublesalesSum,doublerate);
doublegetEarning(intworkPieces,doublewagePerPiece);
doublegetEarning(doublehours,doublewagePerHour);
2.菜单程序设计如下:
intmain()
{
...
cout<<"Pleaseselect..."<cout<<"1:
Manager."<cout<<"2:
SalesMan."<