7.1 定义一个结构体变量(包括年月日)输入年月日,能计算该日是本年中的第几天
#include <iostream>
using namespace std;
struct
{int year;
int month;
int day;
}date;
int main()
{int i,days;
int day_tab[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
cout<<"input year,month,day:";
cin>>date.year>>date.month>>date.day;
days=0;
for (i=1;i<date.month;i++)
days+=day_tab[i];
days+=date.day;
if ((date.year%4==0 && date.year%100!=0 || date.year%400==0) && date.month>=3)
days+=1;
cout<<date.month<<"/"<<date.day<<" is the "<<days
<<"th day in "<<date.year<<"."<<endl;
return 0;
}
7.2 写一个函数days实现上面计算
#include <iostream>
using namespace std;
struct y_m_d
{int year;
int month;
int day;
};
int main()
{y_m_d date;
/* 对days函数的声明 */
int days(int,int,int);
int day_sum;
cout<<"input year,month,day:";
cin>>date.year>>date.month>>date.day;
day_sum=days(date.year,date.month,date.day);
cout<<date.month<<"/"<<date.day<<" is the "<<day_sum
<<"th day in "<<date.year<<endl;
return 0;
}
/* 定义days函数 */
int days(int year,int month,int day)
{int day_sum,i;
int day_tab[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
day_sum=0;
for (i=1;i<month;i++)
day_sum+=day_tab[i];
day_sum+=day;
if ((year%4==0 && year%100!=0 || year%4==0) && month>=3)
day_sum+=1;
return(day_sum);
}
7.3 编写一个函数print,打印学生成绩数组,数组有5个学生的数据(学号 姓名 三门课的成绩 ),主函数输入,print函数输出
#include <iomanip>
using namespace std;
const int n=5;
struct student
{
char num[6];
char name[8];
int score[4];
}stu[n];
int main()
{
void print(student stu[6]);
int i,j;
for (i=0;i<n;i++)
{cout<<"input scores of student "<<i+1<<":"<<endl;
cout<<"NO.:";
cin>>stu[i].num;
cout<<"name:";
cin>>stu[i].name ;
for (j=0;j<3;j++)
{cout<<"score "<<j+1<<":";
cin>>stu[i].score[j];
}
cout<<endl;
}
print(stu);
return 0;
}
void print(student stu[6])
{
int i,j;
cout<<"NO. name score1 score2 score3 "<<endl;
for(i=0;i<n;i++)
{cout<<stu[i].num <<" "<<estw(10)<<stu[i].name <<" ";
for (j=0;j<3;j++)
cout<<setw(3)<<stu[i].score [j]<<" ";
cout<<endl;
}
}
7.4 编写一个函数print 输入5个学生的数据
#include <iostream>
#include <iomanip>
using namespace std;
const int n=5;
struct student
{ char num[6];
char name[8];
int score[4];
}stu[n];
int main()
{void input(student stu[]);
void print(student stu[]);
input(stu);
print(stu);
return 0;
}
void input(student stu[])
{int i,j;
for (i=0;i<n;i++)
{cout<<"input scores of student "<<i+1<<":"<<endl;
cout<<"NO.: ";
cin>>stu[i].num;
cout<<"name: ";
cin>>stu[i].name;
for (j=0;j<3;j++)
{cout<<"score "<<j+1<<":";
cin>>stu[i].score[j];
}
}
}
void print(student stu[])
{int i,j;
cout<<" NO. name score1 score2 score3"<<endl;
for (i=0;i<n;i++)
{cout<<stu[i].num<<" "<<setw(10)<<stu[i].name<<" ";
for (j=0;j<3;j++)
cout<<setw(3)<<stu[i].score[j]<<" ";
cout<<endl;
}
}
7.5 10个学生,包括学号姓名三门课成绩。输入数据,打印三门课总平均成绩,以及最高分的学生全部数据
#include <iostream>
#include <iomanip>
using namespace std;
const int n=10;
struct student
{ char num[6];
char name[8];
int score[4];
float avr;
} stu[n];
int main()
{ int i,j,max,maxi,sum;
float average;
for (i=0;i<n;i++)
{cout<<"input scores of student "<<i+1<<endl;;
cout<<"NO.:";
cin>>stu[i].num;
cout<<"name:";
cin>>stu[i].name;
for (j=0;j<3;j++)
{cout<<"score "<<j+1<<":";
cin>>stu[i].score[j];
}
cout<<endl;
}
average=0;
max=0;
maxi=0;
for (i=0;i<n;i++)
{sum=0;
for (j=0;j<3;j++)
sum+=stu[i].score[j];
stu[i].avr=sum/3.0;
average+=stu[i].avr;
if (sum>max)
{max=sum;
maxi=i;
}
}
average/=n;
cout<<" NO. name score1 score2 score3 average"<<endl;
for (i=0;i<n;i++)
{cout<<setw(8)<<stu[i].num<<" "<<setw(10)<<stu[i].name<<" ";
for (j=0;j<3;j++)
cout<<setw(3)<<stu[i].score[j]<<" ";
cout<<stu[i].avr<<endl;
}
cout<<"average="<<average<<endl;
cout<<"The highest score is :"<<stu[maxi].name<<", score total:"<<max<<endl;
return 0;
}
7.6 写一个函数creat,用来建立一个动态链表。各节点的数据由键盘输入。
#include <iostream>
using namespace std;
#define NULL 0
struct student
{long num;
float score;
struct student *next;
};
int main()
{student a,b,c,*head,*p;
a.num=10001; a.score=89.5;
b.num=10003; b.score=90;
c.num=10007; c.score=85; //为结点的num和score成员赋值
head=&a; //将结点a的起始地址赋给头指针head
a.next=&b; //将结点b的起始地址赋给a结点的next成员
b.next=&c; //将结点c的起始地址赋给b结点的next成员
c.next=NULL; //c结点的next成员不存放其他结点地址
p=head; //使p指针指向a结点
do
{cout<<p->num<<" "<<p->score<<endl; //输出p指向的结点的数据
p=p->next; //使p指向下一结点
}while(p!=NULL); //输出完c结点后p的值为NULL
return 0;
}
7.7 将此链表中各节点的数据依次输出。
int n;
void print(student *head)
{student *p;
cout<<"Now,These "<<n<<" records are:"<<endl;
p=head;
if(head!=NULL)
do
{cout<<p->num<<" "<<p->score<<endl;
p=p->next;
}while(p!=NULL);
}
7.8 写一个函数del,用来删除动态链表中一个指定的节点(指定学号,删除学生)
int n;
student *del(student *head,long num)
{student *p1,*p2;
if (head==NULL) //是空表
{cout<<"list null!"<<endl; return(head);}
p1=head; //使p1指向第一个结点
while(num!=p1->num && p1->next!=NULL)
//p1指向的不是所要找的结点且后面还有结点
{p2=p1; p1=p1->next;}//p1后移一个结点
if(num==p1->num) //找到了
{if(p1==head) head=p1->next; //若p1指向的是首结点,把第二个结点地址赋予head
else p2->next=p1->next;//否则将下一结点地址赋给前一结点地址
cout<<"delete:"<<num<<endl;
n=n-1;
}
else cout<<"cannot find "<<num; //找不到该结点
return(head);
}
7.9 写一个函数insert,用来向动态链表插入一个节点
int n;
student *insert(student *head,student *stud)
{student *p0,*p1,*p2;
p1=head; //使p1指向第一个结点
p0=stud; //指向要插入的结点
if(head==NULL) //原来的链表是空表
{head=p0;p0->next=NULL;}//使p0指向的结点作为头结点
else
{while((p0->num>p1->num) && (p1->next!=NULL))
{p2=p1; //使p2指向刚才p1指向的结点
p1=p1->next;} //p1后移一个结点
if(p0->num<=p1->num)
{if(head==p1) head=p0; //插到原来第一个结点之前
else p2->next=p0; //插到p2指向的结点之后
p0->next=p1;}
else
{p1->next=p0; p0->next=NULL;}} //插到最后的结点之后
n=n+1; //结点数加1
return (head);
}
7.10 以上四个函数组成一个程序,由主程序先后调用这些函数,实现链表的建立、输出、删除、插入
#include <iostream>
using namespace std;
#define NULL 0
struct student
{long num;
float score;
student *next;
};
int n;
int main()
{ student *creat(void);
student *del(student *,long);
student *insert(student *,student *);
void print(student *);
student *head,stu;
long del_num;
cout<<"input records:"<<endl;
head=creat(); //返回头指针
print(head); //输出全部结点
cout<<endl<<"input the deleted number:";
cin>>del_num; //输入要删除的学号
head=del(head,del_num); //删除后链表的头地址
print(head); //输出全部结点
cout<<endl<<"input the inserted record:"; //输入要插入的结点
cin>>stu.num>>stu.score;
head=insert(head,&stu); //返回地址
print(head); //输出全部结点
cout<<endl<<"input the inserted record:"; //输入要插入的结点
cin>>stu.num>>stu.score;
head=insert(head,&stu); //返回地址
print(head);
return 0;
}
student *creat(void) //建立链表的函数
{student *head;
student *p1,*p2;
n=0;
p1=p2=new student; //开辟一个新单元,并使p1,p2指向它
cin>>p1->num>>p1->score;
head=NULL;
while(p1->num!=0)
{n=n+1;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=new student;
cin>>p1->num>>p1->score;
}
p2->next=NULL;
return(head);
}
student *del(student *head,long num) //删除结的函数
{student *p1,*p2;
if (head==NULL) //是空表
{cout<<"list null!"<<endl; return(head);}
p1=head; //使p1指向第一个结点
while(num!=p1->num && p1->next!=NULL) //p1指向的不是所要找的结点且后面还有结点
{p2=p1; p1=p1->next;} //p1后移一个结点
if(num==p1->num) //找到了
{if(p1==head) head=p1->next; //若p1指向的是首结点,把第二个结点地址赋予head
else p2->next=p1->next; //否则将下一结点地址赋给前一结点地址
cout<<"delete:"<<num<<endl;
n=n-1;
}
else cout<<"cannot find "<<num; //找不到该结点
return(head);
}
student *insert(student *head,student *stud) //插入结点的函数
{student *p0,*p1,*p2;
p1=head; //使p1指向第一个结点
p0=stud; //指向要插入的结点
if(head==NULL) //原来的链表是空表
{head=p0;p0->next=NULL;} //使p0指向的结点作为头结点
else
{while((p0->num>p1->num) && (p1->next!=NULL))
{p2=p1; //使p2指向刚才p1指向的结点
p1=p1->next;} //p1后移一个结点
if(p0->num<=p1->num)
{if(head==p1) head=p0; //插到原来第一个结点之前
else p2->next=p0; //插到p2指向的结点之后*/
p0->next=p1;}
else
{p1->next=p0; p0->next=NULL;}} //插到最后的结点之后
n=n+1; //结点数加1
return (head);
}
void print(student *head) //输出链表的函数
{student *p;
cout<<"Now,These "<<n<<" records are:"<<endl;
p=head;
if(head!=NULL)
do
{cout<<p->num<<" "<<p->score<<endl;
p=p->next;
}while(p!=NULL);
}
OR 9.7用函数组成一个程序,实现链表的建立、输出、删除、插入,并且排序_ssoul11的博客-CSDN博客
(可以C语言改一改直接用了)
(太长不看,扔个链接日后有缘再看)
毕业了毕业了,开始备战六级了