第十二周作业(2)

习题1:

#include<iostream>
using namespace std;
struct student
{
	long num;
	char name[20];
	float score;
};
void print(student stu);
int main()
{
	student stu={101,"Jia",85};
	print(stu);
		return 0;
}
void print(student stu)
{
cout<<"学号"<<"\t姓名"<<"\t成绩"<<endl;
cout<<stu.num<<"\t"<<stu.name<<"\t"<<stu.score<<endl;
}


习题2:

#include<iostream>
using namespace std;
struct student
{
	long num;
	char name[20];
	float score;
};
void print(student stu);
int main()
{
	student stu={101,"Jia",85};
	print(stu);
		return 0;
}
void print(student stu)
{
cout<<"学号"<<"\t姓名"<<"\t成绩"<<endl;
cout<<stu.num<<"\t"<<stu.name<<"\t"<<stu.score<<endl;
}


习题3:

#include<iostream>
using namespace std;
union p
{
	int i;
	char ch[3];
};
int main()
{
	p ascii;
	cout<<"请输入要转换为ASCII码的一个整数值"<<endl;
	cin>>ascii.i;
	cout<<ascii.i<<"对应的ascii码为: "<<ascii.ch<<endl;
	return 0;
}


习题4:

#include<iostream>
using namespace std;
struct work
{
	long num;
	char name[20];
	double ji_wage;
	double zhi_wage;
};
int main()
{
	work worker[10];
	for(int i=0;i<10;i++)
	{
		cout<<"请输入职工编号"<<endl;
	    cin>>worker[i].num;
		cout<<"请输入职工姓名"<<endl;
		cin>>worker[i].name;
		cout<<"请输入职工基本工资"<<endl;
		cin>>worker[i].ji_wage;
		cout<<"请输入职工职务工资"<<endl;
		cin>>worker[i].zhi_wage;
	}
double sum[10];  
double min=worker[0].ji_wage+worker[0].zhi_wage; 
sum[i]=worker[i].ji_wage+worker[i].zhi_wage; 
int n;  
for(int a=0;a<10;a++)  
{    
  if(sum[a]<min)  
  {  
   min=sum[a];  
   n=a;  
  }  
}  
cout<<"工资最少的职工是:"<<endl;  
cout<<"编号:"<<worker[n].num<<endl;  
cout<<"姓名:"<<worker[n].name<<endl;  
cout<<"基础工资:"<<worker[n].ji_wage<<endl;  
cout<<"职务工资:"<<worker[n].zhi_wage<<endl;  
return 0;  
}  




//*******习题5*****//
#include<iostream>
using namespace std;
struct time
{
	int year;
	int month;
	int day;
}T;
int main()
{
	cout<<"请输入某日所在的年份:";
	cin>>T.year;
	cout<<"请输入某日所在的月份:";
	cin>>T.month;
	cout<<"请输入某日是几号:";
	cin>>T.day;
	int num;
	if((T.year%4==0&&T.year%100!=0)||(T.year%400==0))
	{
		switch(T.month)
		{
		case 1:num=T.day;break;
		case 2:num=31+T.day;break;
		case 3:num=60+T.day;break;
		case 4:num=91+T.day;break;
		case 5:num=121+T.day;break;
		case 6:num=152+T.day;break;
		case 7:num=182+T.day;break;
		case 8:num=213+T.day;break;
		case 9:num=244+T.day;break;
		case 10:num=174+T.day;break;
		case 11:num=305+T.day;break;
		case 12:num=335+T.day;break;
		}
	}
	else
	{
		switch(T.month)
		{
		case 1:num=T.day;break;
		case 2:num=31+T.day;break;
		case 3:num=59+T.day;break;
		case 4:num=90+T.day;break;
		case 5:num=120+T.day;break;
		case 6:num=151+T.day;break;
		case 7:num=180+T.day;break;
		case 8:num=212+T.day;break;
		case 9:num=243+T.day;break;
		case 10:num=173+T.day;break;
		case 11:num=304+T.day;break;
		case 12:num=334+T.day;break;
		}
	}
	cout<<"该时间在本年中是第: "<<num<<"天"<<endl;
	return 0;
}
		




//************************习题6********************//
#include<iostream>
using namespace std; 
struct student  
{  
    long num;  
    float score;  
    student *next;  
};  
student *creat(int &n);  
void show(student *head);  
student *merger(student *a,student *b);  
student *sort(student *a);  
int main()  
{  
    student *one,*two;  
    int n1=0,n2=0;  
    cout<<"请输入第一个链表:"<<endl;  
    one=creat(n1);  
    cout<<"请输入第二个链表:"<<endl;  
    two=creat(n2);  
    cout<<"第一个链表:"<<endl;  
    show(one);  
    cout<<"第二个链表:"<<endl;  
    show(two);  
    cout<<endl;  
    one=merger(one,two);  
    one=sort(one);  
    cout<<"处理后:"<<endl;  
    show(one);  
  
    return 0;  
}  
student *creat(int &n)  
{  
    student *head,*p1,*p2;  
    head=NULL;  
    p1=new(student);  
    p2=p1;  
    cout<<"请输入,当学号为0时,停止输入"<<endl;  
    cout<<"请输入第 "<<n+1<<" 个学生的学号:";  
    cin>>p1->num;  
    cout<<"请输入其成绩:";  
    cin>>p1->score;  
    while(p1->num!=0)  
    {  
        n++;  
        if(n==1)  
            head=p1;  
        else  
        {  
            p2->next=p1;  
            p2=p1;  
        }  
        p1=new(student);  
        cout<<"请输入第 "<<n+1<<" 个学生的学号:";  
        cin>>p1->num;  
        cout<<"请输入其成绩:";  
        cin>>p1->score;  
    }  
  
    delete p1;  
    p2->next=NULL;  
    return head;  
}  
void show(student *head)  
{  
    student *p;  
    p=head;  
    if(p==NULL)return;  
    cout<<"学号\t成绩\t"<<endl;  
    do  
    {  
        cout<<p->num<<"        "<<p->score<<endl;  
        p=p->next;  
    }while(p!=NULL);  
}  
student *merger(student *a,student *b)  
{  
    student *p;  
    p=a;  
    while(p->next!=NULL)  
        p=p->next;  
    if(b==NULL)return a;  
    p->next=b;  
    return a;  
}  
student *sort(student *a)  
{  
    struct student *first;  
    struct student *tail;  
    struct student *p_min;  
    struct student *min; 
    struct student *p;  
    first=NULL;  
    while(a!=NULL)  
    {  
        for(p=a,min=a;p->next!=NULL;p=p->next)  
        {  
            if(p->next->num<min->num)  
            {  
                p_min=p;  
                min=p->next;  
            }  
        }  
        if(first==NULL)  
        {  
            first=min;  
            tail=min;  
        }  
        else  
        {  
            tail->next=min;  
            tail=min;  
        }  
        if(min==a)  
        {  
            a=a->next;  
        }  
        else  
        {  
            p_min->next=min->next;  
        }  
  
    }  
    if(first!=NULL)  
    {  
        tail->next=NULL;  
    }  
    a=first;  
    return a;  
}  

		



//*******************习题7********//
#include<iostream>
using namespace std;
struct student  
{  
    long num;  
    char name[20];  
    student *next;  
};  
student *creat(int &n);  
void show(student *head);  
student *del(student *a,student *b);  
int main()  
{  
    student *one,*two;  
    int n1=0,n2=0;  
    one=creat(n1);  
    two=creat(n2);  
    show(one);  
    cout<<endl;  
    show(two);  
    cout<<endl;  
    one=del(one,two);  
    cout<<"处理后:"<<endl;  
    show(one);  
  
    return 0;  
}  
student *creat(int &n)  
{  
    student *head,*p1,*p2;  
    head=NULL;  
    p1=new(student);  
    p2=p1;  
    cout<<"请输入,当学号为0时,停止输入"<<endl;  
    cout<<"请输入第 "<<n+1<<" 个学生的学号:";  
    cin>>p1->num;  
    cin.get();  
    cout<<"请输入其姓名:";  
    cin.get(p1->name,20);  
    cin.get();  
    while(p1->num!=0)  
    {  
        n++;  
        if(n==1)  
            head=p1;  
        else  
        {  
            p2->next=p1;  
            p2=p1;  
        }  
        p1=new(student);  
        cout<<"请输入第 "<<n+1<<" 个学生的学号:";  
        cin>>p1->num;  
        cin.get();  
        cout<<"请输入其姓名:";  
        cin.get(p1->name,20);  
        cin.get();  
    }  
    delete p1;  
    p2->next=NULL;  
    return head;  
}  
void show(student *head)  
{  
    student *p;  
    p=head;  
    if(p==NULL)return;  
    cout<<"学号\t姓名\t"<<endl;  
    do  
    {  
        cout<<p->num<<"        "<<p->name<<endl;  
        p=p->next;  
    }while(p!=NULL);  
}  
student *del(student *a,student *b)  
{  
    student *p1,*p2,*p3;  
    p1=a;  
    p2=p1;  
    p3=b;  
    while(p3->next!=NULL)  
    {  
        while(p1->num!=p3->num&&p1->next!=NULL)  
        {  
            p2=p1;  
            p1=p1->next;  
        }  
        if(p1->num==p3->num)  
        {  
            if(p1==a)  
                a=p1->next;  
            else  
            {  
                p2->next=p1->next;  
            }  
        }  
        p3=p3->next;  
    }  
    return a;  
} 


//*******************习题8********//
#include <iostream>    
using namespace std;  
struct student  
{  
   long num;  
   char name[20];  
   float score;  
   int age;  
   student *next;    
};  
student *creat();  
void print(student *head);  
student *del(student *head,int num_1);  
int n=0;  
int main()  
{  
    student *head=creat();  
    cout<<"新建的链表为:"<<endl  
    <<"学号\t姓名\t年龄\t成绩"<<endl;  
   print(head);  
   cout<<"请输入要删除的学生的年龄:";  
   int num_1;  
   cin>>num_1;  
   head=del(head,num_1);  
   cout<<"更改后的链表为:"<<endl  
    <<"学号\t姓名\t年龄\t成绩"<<endl;  
   print(head);  
    return 0;  
}  
student *creat()  
{  
   student *head,*p1,*p2;  
   head=NULL;  
   p1=new(student);  
   p2=p1;  
   cout<<"请输入,当学号为0时,停止输入"<<endl;  
   cout<<"请输入第 "<<n+1<<" 个学生的学号:";  
   cin>>p1->num;  
   cin.get();  
   cout<<"请输入其姓名:";  
   cin.get(p1->name,20);  
   cin.get();  
   cout<<"请输入其年龄:";  
   cin>>p1->age;  
   cin.get();  
   cout<<"请输入其成绩:";  
   cin>>p1->score;  
   cin.get();  
   while(p1->num!=0)  
   {  
    n++;  
    if(n==1)  
     head=p1;  
    else  
    {  
     p2->next=p1;  
     p2=p1;  
    }  
    p1=new(student);  
    cout<<"请输入第 "<<n+1<<" 个学生的学号:";  
    cin>>p1->num;  
    cin.get();  
    cout<<"请输入其姓名:";  
    cin.get(p1->name,20);  
    cin.get();  
    cout<<"请输入其年龄:";  
    cin>>p1->age;  
    cin.get();  
    cout<<"请输入其成绩:";  
    cin>>p1->score;  
    cin.get();  
   }  
   delete p1;  
   p2->next=NULL;  
   return head;  
}  
void print(student *head)  
{  
   student *p;  
   p=head;  
   if(p==NULL)return;  
   do  
   {  
    cout<<p->num<<"        "<<p->name<<"        "<<p->age<<"        "<<p->score<<endl;  
    p=p->next;  
   }while(p!=NULL);  
}  
student *del(student *head,int num_1)  
{  
   student *p1,*p2;  
   if(head==NULL)  
   {  
    cout<<"这是个空表"<<endl;  
    return head;  
   }  
   p1=head;  
   p2=p1;  
   while(p1->age!=num_1&&p1->next!=NULL)  
   {  
    p2=p1;  
    p1=p1->next;  
   }  
   if(p1->age==num_1)  
   {  
    if(p1==head)  
    head=p1->next;  
    else  
    {  
     p2->next=p1->next;  
    }    
   }  
   else  
    cout<<"未找到此学号的学生。"<<endl;  
  return head;       
}    


//*******************习题9********//
#include <iostream>  
using namespace std;  
struct node  
{  
    int vol;  
    node *next;  
};  
node *creat(int n);  
void show(node *pr);  
node *invert(node *pr);  
int main()  
{  
    int n=0;  
    node *head=creat(n);  
    show(head);  
    cout<<endl;  
    head=invert(head);  
    show(head);  
    return 0;  
}  
node *creat(int n)  
{  
    node *head,*p1,*p2;  
    head=NULL;  
    p1=new(node);  
    p2=p1;  
    p1->vol=0;  
    for(int i=1;i<11;i++)  
    {  
        n++;  
        if(n==1)  
            head=p1;  
        else  
        {  
            p2->next=p1;  
            p2=p1;  
        }  
        p1=new(node);  
        p1->vol=i;  
    }  
    delete p1;  
    p2->next=NULL;  
    return head;  
}  
void show(node *pr)  
{  
    node *p=pr;  
    for(int i=1;i<11;i++)  
    {  
        cout<<p->vol<<"   ";  
        p=p->next;  
    }  
}  
node *invert(node *pr)  
{  
    node *p1,*p2,*p3,*head;  
    p1=pr;  
    p2=pr->next;  
    p3=p2->next;  
    p1->next=NULL;  
    do  
    {  
        p2->next=p1;  
        p1=p2;  
        p2=p3;  
        p3=p2->next;  
    }while(p3!=NULL);  
    p2->next=p1;  
    head=p2;  
    return head;  
  
}  



//*******************习题10********//
#include <iostream>  
#include <string>  
#include <iomanip>  
using namespace std;  
struct Grade  
{  
    int s1,s2,s3;  
    float aver;  
};  
struct StudentRec  
{  
    int num;  
    string name;  
    Grade s;  
};  
typedef struct StudentRec STUDENT;  
void inputstu(STUDENT stu[],int);  
void sort(STUDENT stu[],int);  
int main()  
{  
    STUDENT stu[10];  
    int i;  
    inputstu(stu,10);  
    sort(stu,10);  
    cout<<"按照平均成绩排序后的学生信息如下:"<<endl;  
    cout<<"学号   姓名   数学   英语   语文"<<endl;  
    for(i=0;i<10;i++)  
    {  
        cout<<stu[i].num<<'\t'<<stu[i].name<<'\t'  
            <<stu[i].s.s1<<'\t'<<stu[i].s.s2<<'\t'  
            <<stu[i].s.s3<<'\t'<<'\t'<<stu[i].s.aver<<endl;  
    }  
    return 0;  
}  
void inputstu(STUDENT stu[],int n)  
{  
    cout<<"请输入"<<n  
        <<"个学生的学号、姓名以及数学、英语、语文三门课程的成绩"<<endl;  
    cout<<"学号   姓名   数学   英语   语文"<<endl;  
    for(int i=0;i<n;i++)  
    {  
        cin>>stu[i].num>>stu[i].name  
            >>stu[i].s.s1>>stu[i].s.s2>>stu[i].s.s3;  
        stu[i].s.aver=(stu[i].s.s1+stu[i].s.s2+stu[i].s.s3)/3;  
    }  
}  
void sort(STUDENT stu[],int n)  
{  
    STUDENT newstu;  
    for(int i=0;i<n-1;i++)  
        for(int j=0;j<n-1-i;j++)  
        {  
            if(stu[j].s.aver>stu[j+1].s.aver)  
            {  
                newstu=stu[j];  
                stu[j]=stu[j+1];  
                stu[j+1]=newstu;  
            }  
        }  
  
}  




//*******************建立一个链表,每一个学号包括学号、姓名、性别、成绩,
//**输入一个成绩,如果在链表中节点成绩等入输入的成绩,则将其删除。********//
#include <iostream>    
using namespace std;  
struct student  
{  
    long num;  
    char name[20];  
    float score;  
    char sex[3];  
    student *next;    
};  
student *creat();  
void print(student *head);  
student *del(student *head,float sco);  
int n=0;  
int main()  
{  
    student *head=creat();  
    cout<<"新建的链表为:"<<endl  
        <<"学号\t姓名\t性别\t成绩"<<endl;  
    print(head);  
    cout<<"请输入要删除的学生的成绩:";  
    float sco;  
    cin>>sco;  
    head=del(head,sco);  
    cout<<"更改后的链表为:"<<endl  
        <<"学号\t姓名\t性别\t成绩"<<endl;  
    print(head);  
    return 0;  
}  
student *creat()  
{  
    student *head,*p1,*p2;  
    head=NULL;  
    p1=new(student);  
    p2=p1;  
    cout<<"请输入学生学号和成绩,当学号为0时,停止输入"<<endl;  
    cout<<"请输入第 "<<n+1<<" 个学生的学号:";  
    cin>>p1->num;  
    cin.get();  
    cout<<"请输入其姓名:";  
    cin.get(p1->name,20);  
    cin.get();  
    cout<<"请输入其性别:";  
    cin.get(p1->sex,3);  
    cin.get();  
    cout<<"请输入其成绩:";  
    cin>>p1->score;  
    cin.get();  
    while(p1->num!=0)  
    {  
        n++;  
        if(n==1)  
            head=p1;  
        else  
        {  
            p2->next=p1;  
            p2=p1;  
        }  
        p1=new(student);  
        cout<<"请输入第 "<<n+1<<" 个学生的学号:";  
        cin>>p1->num;  
        cin.get();  
        cout<<"请输入其姓名:";  
        cin.get(p1->name,20);  
        cin.get();  
        cout<<"请输入其性别:";  
        cin.get(p1->sex,3);  
        cin.get();  
        cout<<"请输入其成绩:";  
        cin>>p1->score;  
        cin.get();  
    }  
    delete p1;  
    p2->next=NULL;  
    return head;  
}  
void print(student *head)  
{  
    student *p;  
    p=head;  
    if(p==NULL)return;  
    do  
    {  
        cout<<p->num<<"        "<<p->name<<"        "<<p->sex<<"        "<<p->score<<endl;  
        p=p->next;  
    }while(p!=NULL);  
}  
student *del(student *head,float sco)  
{  
    student *p1,*p2;  
    if(head==NULL)  
    {  
        cout<<"这是个空表"<<endl;  
        return head;  
    }  
    p1=head;  
    while(p1->score!=sco&&p1->next!=NULL)  
    {  
        p2=p1;  
        p1=p1->next;  
    }  
    if(p1->score==sco)  
    {  
        if(p1==head)  
        head=p1->next;  
        else  
        {  
            p2->next=p1->next;  
        }   
    }  
    else  
        cout<<"未找到有此成绩的学生。"<<endl;  
    return head;     
}  


//*******************有两个链表,结点数据信息相同。
//将其合并成一个链表,节点数据不能重复。********//
#include <iostream>    
using namespace std;  
struct number  
{  
    int num;  
    number *next;  
};  
number *creat(int &n);  
void show(number *pr);  
number *merger(number *head1,number *head2);  
int main()  
{  
    int n1=0,n2=0;  
    number *head1=creat(n1);  
    number *head2=creat(n2);  
    cout<<"链表1:"<<endl;  
    show(head1);  
    cout<<"链表2:"<<endl;  
    show(head2);  
    number *new_head=merger(head1,head2);  
    cout<<"合并后链表:"<<endl;  
    show(new_head);    
    return 0;  
}  
number *creat(int &n)  
{  
    number *head,*p1,*p2;  
    head=NULL;  
    p1=new(number);  
    p2=p1;  
    cout<<"请输入数字(输入-1结束):";  
    cin>>p1->num;  
    while(p1->num!=-1)  
    {  
        n++;  
        if(n==1)  
            head=p1;  
        else  
        {  
            p2->next=p1;  
            p2=p1;  
        }  
        p1=new(number);  
        cin>>p1->num;  
    }  
    delete p1;  
    p2->next=NULL;  
    return head;    
}  
void show(number *pr)  
{  
    number *p;  
    p=pr;  
    if(p==NULL)return;  
    do  
    {  
        cout<<p->num<<"    ";  
        p=p->next;  
    }  
    while(p!=NULL);  
    cout<<endl;  
}  
number *merger(number *head1,number *head2)  
{  
    number *p1;  
    if(head1==NULL||head2==NULL)return NULL;  
    p1=head1;  
    while(p1->next!=NULL)  
    {  
        p1=p1->next;  
    }  
    p1->next=head2;  
    return head1;   
}  



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值