- 实验作业
1.调试分析课本每一个例题,有可能的话更改成2-3个方法的新程序;
2.编程实现课本每一个编程习题。
3. 建立一个链表,每一个学号包括学号、姓名、性别、成绩,输入一个成绩,如果在链表中节点成绩等入输入的成绩,则将其删除。
4.有两个链表,结点数据信息相同。将其合并成一个链表,节点数据不能重复。
//****8.1********//
#include<iostream>
using namespace std;
struct date
{
int month;
int day;
int year;
};
struct student
{
int num;
char name[20];
struct date birthday;
char addr[30];
};
int main()
{
student stul;
stul.num=1001;
stul.birthday.month=8;
stul.birthday.day=20;
stul.birthday.year=1980;
cout<<stul.num<<" ";
cout<<stul.birthday.month<<" ";
cout<<stul.birthday.day<<" ";
cout<<stul.birthday.year<<" "<<endl;
return 0;
}
//****8.2********//
#include<iostream>
using namespace std;
int main()
{
struct stu
{
int num;
int age;
}stu1,stu2;
stu1.num=1001;
stu1.age=20;
stu2=stu1;
cout<<stu2.num<<endl;
cout<<stu2.age<<endl;
return 0;
}
//****8.3********//
#include<iostream>
using namespace std;
struct student
{
int num;
char name[20];
float score;
};
int main()
{
student stu[3]={{1001,"Liu Jin",75},{1002,"Li Lan",82},{1003,"Ma Kai",80}};
student temp;
for(int i=0;i<2;i++)
for(int j=0;j<2-j;j++)
if(stu[j].score<stu[j+1].score)
{
temp=stu[j];stu[j]=stu[j+1];stu[j+1]=temp;
}
cout<<"Num"<<" Name"<<" Score"<<endl;
for(int k=0;k<3;k++)
cout<<stu[k].num<<" "<<stu[k].name<<" "<<stu[k].score<<endl;
return 0;
}
//****8.4********//
#include<iostream>
using namespace std;
struct student
{
int num;
char name[20];
float score;
};
int main()
{
student stu[3]={{1001,"Liu Jin",75},{1002,"Li Lan",82},{1003,"Ma Kai",80}};
student *s=stu;
cout<<"Num"<<"\tName"<<"\t\tScore"<<endl;
for(;s<stu+3;s++)
cout<<s->num<<"\t"<<s->name<<"\t\t"<<s->score<<endl;
return 0;
}
//******8.5****//
#include<iostream>
using namespace std;
struct student
{
int num;
char name[20];
float score;
};
void print(student *p)
{
cout<<p->num<<"\t"<<p->name<<"\t"<<p->score<<endl;
}
int main()
{
student stu[3]={{1001,"Liu Jin",75},{1002,"Li Lan",82},{1003,"Ma Kai",80}};
cout<<"Num"<<"\tName"<<"\tScore"<<endl;
for(int i=0;i<3;i++)
{
print(&stu[i]);
}
return 0;
}
//******8.10****//
#include<iostream>
using namespace std;
struct student
{
int num;
float score;
student *next;
};
int n=0;
student *creat()
{
student *head,*p1,*p2;
head=NULL;
p1=new(student);
p2=p1;
cout<<"请输入学生学号和成绩,当学号为0时,停止输入"<<endl;
cin>>p1->num>>p1->score;
while(p1->num!=0)
{
n++;
if(n==1)
head=p1;
else
{
p2->next=p1;
p2=p1;
}
p1=new(student);
cin>>p1->num>>p1->score;
}
delete p1;
p2->next=NULL;
return head;
}
void print(student *head)
{
student *p;
p=head;
if(p==NULL)return;
do
{
cout<<p->num<<" "<<p->score<<endl;
p=p->next;
}while(p!=NULL);
}
student *del(student *head,int num)
{
student *p1,*p2;
if(head==NULL)
{
cout<<"list null"<<endl;
return head;
}
p1=head;
while(num!=p1->num&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(num=p1->num)
{
if(p1==head)
head=p1->next;
else
p2->next=p1->next;
cout<<"delete: "<<num<<endl;
n--;
}
else
cout<<num<<"not been found!"<<endl;
return head;
}
student *insert(student *head,student *stud)
{
student *p0,*p1,*p2;
p1=head;
p0=stud;
if(head==NULL)
{
head=p0;
p0=p1->next;
}
else
while((p0->num>p1->num)&&(p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if(p0->num<p1->num)
{
if(head==p1)
head=p0;
else
p2->next=p0;
p0->next=p1;
}
else
{
p1->next=p0;
p0->next=NULL;
}
n++;
return head;
}
int main()
{
student *head=creat();
cout<<"新建的链表为:"<<endl
<<"学号\t成绩"<<endl;
print(head);
int num;
cout<<"请输入要删除的学号:";
cin>>num;
head=del(head,num);
cout<<"目前的链表为:"<<endl;
print(head);
student *pt=new student;
cout<<"请输入要插入学生的学号和成绩:";
cin>>pt->num>>pt->score;
head=insert(head,pt);
cout<<"目前的链表为:"<<endl;
print(head);
return 0;
}
//*******8.11*****//
#include<iostream>
using namespace std;
union pw
{
int i;
char ch[10];
};
int main()
{
pw password;
cout<<"请输入一个整数,若大于127则退出"<<endl;
cin>>password.i;
if(password.i<127)
cout<<password.i<<"对应的字符为:"<<password.ch<<endl;
else
cout<<" "<<endl;
return 0;
}
//*******8.12*****//
#include<iostream>
using namespace std;
int main()
{
enum en{plus,minus,times}op1;
int x,y;
cout<<"请输入两个数:";
cin>>x>>y;
op1=plus;
while(op1<=times)
{
switch(op1)
{
case plus:cout<<x<<"+"<<y<<"="<<x+y<<endl;break;
case minus:cout<<x<<"-"<<y<<"="<<x-y<<endl;break;
case times:cout<<x<<"*"<<y<<"="<<x*y<<endl;break;
}
int i=(int)op1;
op1=(en)(++i);
}
return 0;
}