第八周作业

  • 实验作业

1.调试分析课本每一个例题,有可能的话更改成2-3个方法的新程序;

2.编程实现课本每一个编程习题。

3. 建立一个链表,每一个学号包括学号、姓名、性别、成绩,输入一个成绩,如果在链表中节点成绩等入输入的成绩,则将其删除。

4.有两个链表,结点数据信息相同。将其合并成一个链表,节点数据不能重复。


8-1

#include<iostream>  
using namespace std;  
struct date     //定义一个结构体date  
{  
    int month;  
    int day;  
    int year;  
};  
struct student  //定义一个结构体student  
{  
    int num;  
    char name[20];  
    struct date birthday;//嵌套结构体date  
    char addr[30];  
};  
int main() //主函数  
{  
    student stu1;                     //定义一个是student结构体变量stu1  
    stu1.num=1001;                      
    stu1.birthday.month=8;  
    stu1.birthday.day=20;              //给结构体成员赋值  
    stu1.birthday.year=1980;  
    cout<<stu1.num<<"  ";  
    cout<<stu1.birthday.day<<"  ";  
    cout<<stu1.birthday.month<<"  ";  
    cout<<stu1.birthday.year<<endl;    //输出结构体成员的值  
  
    return 0;  
}  


换一种定义方法

#include<iostream>  
using namespace std;  
  
struct student  //定义一个结构体student并进行初始化  
{  
    int num;  
    char name[20];  
    struct      //定义一个结构体date  
    {  
        int month;  
        int day;  
        int year;  
    }birthday;  
    char addr[30];  
}stu1={1001,"",8,20,1980,""};  
int main() //主函数  
{  
      
    cout<<stu1.num<<"  ";  
    cout<<stu1.birthday.day<<"  ";  
    cout<<stu1.birthday.month<<"  ";  
    cout<<stu1.birthday.year<<endl;    //输出结构体成员的值  
  
    return 0;  
}  

8-2

#include<iostream>  
using namespace std;  
int main()  
{  
    struct   //定义结构体并同时定义stu1,stu2两个结构体变量  
    {  
        int num;  
        int age;  
    }stu1,stu2;  
    stu1.num=1001;  
    stu1.age=20;//给stu1变量进行赋值  
    stu2=stu1;  //将stu1赋值给stu2;  
    cout<<stu2.num<<endl;  
    cout<<stu2.age<<endl;//输出stu2的值  
    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}};
                                           //定义一个结构体数组变量stu【3】,并赋初值  
    student temp;                         //定义一个结构体变量temp作为中间变量  
    for(int i=1;i<3;i++)  
        for(int j=0;j<=2-i;j++)           //用冒泡法,利用temp作为中间变量,将结构体数组变量stu【3】的值相互交换  
        {  
            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++)                   //分别输出结构体数组变量stu【3】的值  
       cout<<stu[k].num<<"  "<<stu[k].name<<"  "<<stu[k].score<<endl;   
    return 0;  
}  

8-5
#include<iostream>  
using namespace std;  
struct student                  //定义一个结构体  
{  
    int num;  
    char name[20];  
    float score;  
};  
void print(student *ps)       //形参ps被定义为指向student类型的指针  
{  
    cout<<ps->num<<"  "<<ps->name<<"  "<<ps->score<<endl;  
}  
int main()  
{  
    student stu[3]={{1001,"Liu Jin",75},{1002,"Li Lan",82},{1003,"Ma Kai",80}};
                               //定义一个结构体数组变量stu【3】,并赋初值  
    for(int i=0;i<3;i++)  
    {  
        print(&stu[i]);//调用print函数,&stu【i】是结构体数组元素stu【i】的地址  
    }  
    return 0;  
}  





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值