1.调试分析课本每一个例题,有可能的话更改成2-3个方法的新程序;
例题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 stu1;
stu1.num=1001;
stu1.birthday.month=8;
stu1.birthday.day=20;
stu1.birthday.year=1980;
cout<<stu1.birthday.month<<" ";
cout<<stu1.birthday.day<<" ";
cout<<stu1.birthday.year<<endl;
return 0;
}
例题2:
#include<iostream>
using namespace std;
int main()
{
struct
{
int num;
int age;
}
stu1,stu2;
stu1.num=1001;
stu1.age=20;
stu2=stu1;
cout<<stu2.num<<endl;
cout<<stu2.age<<endl;
return 0;
}
例题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=1;i<3;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"<<" Num"<<" Score"<<endl;
for(int k=0;k<3;k++)
cout<<stu[k].num<<" "<<stu[k].name<<" "<<stu[k].score<<endl;
return 0;
}
例题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;
}
例题5:
#include<iostream>
using namespace std;
struct student
{
int num;
char name[20];
float score;
};
void print(student *ps)
{
cout<<ps->num<<"\t"<<ps->name<<"\t\t"<<ps->score<<endl;
}
int main()
{
student stu[3]={{1001,"Liu Jin",75},{1002,"Li Lan",82},
{1003,"Ma Kai",80}};
for(int i=0;i<3;i++)
{
print(&stu[i]);
}
return 0;
}
例题6:
/********建立单向动态链表********/
struct student
{
long 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;
}
例题7
/********输出链表********/
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);
}
例题8:
/********链表的删除操作********/
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;
}
例题9:
/********对链表的插入操作********/
student *insert(student *head,student *stud)
{
student *p0,*p1,*p2;
p1=head;
p0=stud;
if(head==NULL)
{
head=p0;
p0->next=NULL;
}
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;
}
例题10:
#include<iostream>
using namespace std;
struct student
{
int num;
float score;
student *next;
};
student *creat();
void print(student *head);
student *del(student *head,int num);
student *insert(student *head,student *stud);
int n=0;
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;
}
例题11:
#include<iostream>
using namespace std;
union pw
{
int i;
char ch[2];
};
int main()
{
cout<<"请输入一个整数,若大于127则退出"<<endl;
pw password;
while(1)
{
cin>>password.i;
if(password.i>127)
break;
cout<<password.i<<"对应的字符为:"<<password.ch<<endl;
}
return 0;
}
例题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;
}