week_12_homework

课后编程题:

1.

#include <iostream>

using namespace std;
struct student
{
	long num;
	char name[20];
	float score[3];
};
void print(student stu);
int main()
{
    student stu;
    cout<<"请输入学生的学号:";
    cin>>stu.num;
    cin.get();
    cout<<"请输入学生的姓名:";
    cin.get(stu.name,20);
    cin.get();
    for(int i=0;i<3;i++)
	{
		cout<<"请输入第 "<<i+1<<" 门成绩:";
		cin>>stu.score[i];
	}

    print(stu);
    return 0;
}
void print(student stu)
{
	cout<<"姓名:"<<stu.name<<endl;
	cout<<"学号:"<<stu.num<<endl;
	cout<<"成绩:"<<endl;
	for(int i=0;i<3;i++)
		cout<<stu.score[i]<<"    ";
	cout<<endl;
}

2.

#include <iostream>

using namespace std;
struct student
{
	long num;
	char name[20];
	float score[3];
};
void print(student *stu);
int main()
{
    student stu;
    cout<<"请输入学生的学号:";
    cin>>stu.num;
    cin.get();
    cout<<"请输入学生的姓名:";
    cin.get(stu.name,20);
    cin.get();
    for(int i=0;i<3;i++)
	{
		cout<<"请输入第 "<<i+1<<" 门成绩:";
		cin>>stu.score[i];
	}

    print(&stu);
    return 0;
}
void print(student *stu)
{
	cout<<"姓名:"<<stu->name<<endl;
	cout<<"学号:"<<stu->num<<endl;
	cout<<"成绩:"<<endl;
	for(int i=0;i<3;i++)
		cout<<stu->score[i]<<"    ";
	cout<<endl;
}

3.

#include <iostream>

using namespace std;
union asc
{
	int num;
	char ch[2];
};
int main()
{
    asc ascii;
    cout<<"请输入要转换成ASCII码的整数:";
    cin>>ascii.num;
    cout<<"其对应的ASCII码为:"<<ascii.ch<<endl;
    return 0;
}

4.

#include <iostream>
using namespace std;
struct worker
{
 long num;
 char name[20];
 double basic_wage;
 double job_wage;
};
int main()
{
    worker work[10];
    for(int i=0;i<10;i++)
	{
		cout<<"Enter the num:";
		cin>>work[i].num;
		cin.get();
		cout<<"Enter the name:";
		cin.get(work[i].name,20);
		cin.get();
		cout<<"Enter the basic wages:";
		cin>>work[i].basic_wage;
		cin.get();
		cout<<"Enter the job wages:";
		cin>>work[i].job_wage;
		cin.get();
	}
	double sum_wage[10];
	double min=work[0].basic_wage+work[0].job_wage;
	int n=0;
	for(int i=1;i<10;i++)
	{
		sum_wage[i]=work[i].basic_wage+work[i].job_wage;
		if(sum_wage[i]<min)
		{
			min=sum_wage[i];
			n=i;
		}
	}
	cout<<"The minimum wage workers:"<<endl;
	cout<<"Name:"<<work[n].name<<endl;
	cout<<"Num:"<<work[n].num<<endl;
	cout<<"Basic wages:"<<work[n].basic_wage<<endl;
	cout<<"Job wages:"<<work[n].job_wage<<endl;
    return 0;
}



5.

 

#include <iostream>

using namespace std;
struct time
{
	int year;
	int month;
	int day;
};
int main()
{
    cout<<"请输入一个时间(年,月,日):";
    time a_time;
    cin>>a_time.year>>a_time.month>>a_time.day;
    int day_year;
    if((a_time.year%4==0&&a_time.year%100!=0)||a_time.year%400==0)
	{
		switch(a_time.month)
		{
			case 1:day_year=a_time.day;break;
			case 2:day_year=31+a_time.day;break;
			case 3:day_year=60+a_time.day;break;
			case 4:day_year=91+a_time.day;break;
			case 5:day_year=121+a_time.day;break;
			case 6:day_year=152+a_time.day;break;
			case 7:day_year=182+a_time.day;break;
			case 8:day_year=213+a_time.day;break;
			case 9:day_year=244+a_time.day;break;
			case 10:day_year=274+a_time.day;break;
			case 11:day_year=305+a_time.day;break;
			case 12:day_year=335+a_time.day;break;
		}
	}
	else
	{
		switch(a_time.month)
		{
			case 1:day_year=a_time.day;break;
			case 2:day_year=31+a_time.day;break;
			case 3:day_year=59+a_time.day;break;
			case 4:day_year=90+a_time.day;break;
			case 5:day_year=120+a_time.day;break;
			case 6:day_year=151+a_time.day;break;
			case 7:day_year=181+a_time.day;break;
			case 8:day_year=212+a_time.day;break;
			case 9:day_year=243+a_time.day;break;
			case 10:day_year=273+a_time.day;break;
			case 11:day_year=304+a_time.day;break;
			case 12:day_year=334+a_time.day;break;
		}
	}
    cout<<"该日是本年中第 "<<day_year<<" 天"<<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;
			}
		}

}




练习题:

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

#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;


}

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

#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
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值