📄 练习.txt
字号:
10.1
#include <iostream.h>
struct math
{
float mid;
float fina;
};
void main()
{
math stu1;
cout<<"input the mid-term and final-term scores:"<<endl;
cin>>stu1.mid>>stu1.fina;
cout<<"the average scores is:"<<(stu1.mid+stu1.fina)/2<<endl;
}
10.2
#include <iostream.h>
#include <string.h>
#include <math.h>
struct person
{
char name[20];
person* next;
};
void insert(person* (&head));
void main()
{
person staff[5];
person* head=staff;
for(int i=0;i<5;i++)
{
cin>>staff[i].name;
if(i<4)
{
staff[i].next=&(staff[i+1]);
}
else
staff[i].next=NULL;
}
cout<<endl;
insert(head);
cout<<endl<<"in main..."<<endl;
while(head->next!=NULL)
{
cout<<head->name<<endl;
head=head->next;
}
cout<<head->name<<endl;
}
void insert(person* (&head))
{
person* pCurrent=head;
person* pivot=head;
person* m=new person;
cout<<"input the name to be added:"<<endl;
cin>>m->name;
char* p1=pCurrent->name;
char* p2="jone";
//first situation
if(!strcmp(p1,p2))
{
cout<<"the first situation."<<endl;
m->next=head;
head=m;
return;
}
pivot=pCurrent;
pCurrent=pCurrent->next;
p1=pCurrent->name;
//注意让while循环的条件,若以strcmp()为条件,则出现-1情况,
//故本题采用fabs(strcmp(p1,p2));另外注意不能用!strcmp()为条件,这样与预想相反
//注意:p1若不在循环里给其赋值的话,始终指向第一个结构元素的name,这样结果永远都是一样!
while(fabs(strcmp(p1,p2))&&pCurrent->next!=NULL)
{
pivot=pCurrent;
pCurrent=pCurrent->next;
p1=pCurrent->name;
}
//cout<<"testing... "<<pCurrent->name<<endl;
if(pCurrent->next==NULL)
{
cout<<"the end situation."<<endl;
m->next=NULL;
pCurrent->next=m;
return;
}
if(!strcmp(p1,p2))
{
cout<<"the mid situation."<<endl;
m->next=pivot->next;
pivot->next=m;
return;
}
cout<<"error!"<<endl;
}
10.3
#include <iostream.h>
struct data
{
long int ele;
data* next;
};
void inverse(data* (&head));
void main()
{
//initialize a linked list
data ori[4];
for(int i=0;i<4;i++)
{
cin>>ori[i].ele;
if(i<3)
ori[i].next=&(ori[i+1]);
else
ori[i].next=NULL;
}
//initialize the head of linked list
data head=
{20041302,ori};
head.next=ori;
data* tmp=&head;
inverse(tmp);
cout<<"after changing..."<<endl;
i=0;
while(tmp->next!=NULL)
{
cout<<tmp->ele<<endl;
tmp=tmp->next;
}
cout<<tmp->ele<<endl;
}
void inverse(data* (&head))
{
data* tmp=head;
data* p[4];
//reorder
for(int i=3;i>=0;i--)
{
p[i]=tmp->next;
tmp=p[i];
}
//reindirection
for(i=0;i<3;i++) p[i]->next=p[i+1];
p[3]->next=NULL;
head->next=p[0];
}
10.4
#include <iostream.h>
struct Lnode
{
double data;
Lnode* next;
};
void ShowList(Lnode* list)
{
cout<<"Func:ShowList:"<<endl;
while(list!=NULL)
{
cout<<list->data<<endl;
list=list->next;
}
cout<<"Func End."<<endl;
}
void AddToEnd(Lnode* newl,Lnode* (&head))
{
Lnode* tmp=head;
if(head==NULL)
{
head=newl;
return;
}
while(tmp->next!=NULL)
tmp=tmp->next;
tmp->next=newl;
}
Lnode* GetNode()
{
Lnode* item;
item=new Lnode;
if(item)
{
item->next=NULL;
item->data=0.0;
}
else
cout<<"Nothing allocated."<<endl;
return item;
}
void DeleteList(Lnode* head) //高!
{
while(head!=NULL)
{
Lnode* p;
p=head;
head=head->next;
delete p;
}
}
void main()
{
Lnode* head=NULL;
Lnode* temp;
temp=GetNode();
while(temp)
{
cout<<"data?";
cin>>temp->data;
if(temp->data>0)
AddToEnd(temp,head);
else //once input a negative, the program exit.
break;
temp=GetNode(); //continue to get next node
}
ShowList(head);
DeleteList(head);
}
10.5
#include <iostream.h>
struct list
{
int num;
list* next;
};
list l1[3]=
{{21,&l1[1]},{22,&l1[2]},{23,NULL}};
list l2[3]=
{{31,&l2[1]},{32,&l2[2]},{33,NULL}};
void showlist(list* head)
{
while(head!=NULL) //注:此处亦可写成while(head);
{
cout<<head->num<<endl;
head=head->next;
}
}
void link(list* &head1,list* head2)
{
list* h_tmp=head1;
while(h_tmp->next!=NULL)
h_tmp=h_tmp->next;
h_tmp->next=head2;
}
void main()
{
list* head1=&l1[0];
list* head2=&l2[0];
link(head1,head2);
showlist(head1);
}
10.6
#include <iostream.h>
struct stud
{
long int ID_num;
char name[20];
char gender;
int age;
stud* next;
};
stud pupil[8]=
{
{10031,"daniel",'m',12,&pupil[1]},
{10038,"ryu",'m',12,&pupil[2]},
{10037,"ruby",'m',11,&pupil[3]},
{10034,"kevin",'m',12,&pupil[4]},
{10032,"cathy",'f',11,&pupil[5]},
{10033,"lily",'f',12,&pupil[6]},
{10036,"kate",'f',11,&pupil[7]},
{10035,"ken",'m',12,NULL}
};
void show(stud* head)
{
while(head)
{
cout<<head->name<<endl;
head=head->next;
}
}
void isort(stud* (&head),int size);
void main()
{
int n=8;
stud* head=pupil;
isort(head,n);
show(head);
}
void isort(stud* (&a),int size)
{
//stud* a=head;
stud inserter;
stud* tmp;
int index;
for(int i=1;i<size;i++)
{
inserter=a[i];
index=i-1;
while(index>=0 && inserter.ID_num<a[index].ID_num)
{
tmp=a[index+1].next;
a[index+1]=a[index];
a[index+1].next=tmp; //to protect the next pointer of a[index+1]
index--;
}
tmp=a[index+1].next; //protect the a[index+1].next
a[index+1]=inserter;
a[index+1].next=tmp;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -