当前位置:文档之家› 第九章:结构体

第九章:结构体

第九章:结构体
第九章:结构体

1、定义两个日期结构体变量(包括年、月、日)。输入两个日期值,比较两个日期的大

小(越靠后越大),输出较大的日期。

2、将1题中输入的日期进行验证,保证日期的正确性。(1)不可为负数;(2)年份

1900-2056之间,月份在1-12之间;(3)日期在1-31日以内(注意各月份的日期数判断及闰年的判断)(Year % 4 ==0 && Year % 100 !=0 || Year % 400 ==0)

3、输入两个日期,编程交换两个日期值,保证第一个日期是较大的日期(日期的大小比

较用函数表达)。

4、计算某日期是一年中的第几天。

5、计算两日期间相差的天数。

6、利用基姆拉尔森计算公式:W= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400) mod 7,计算

任意日期是星期几。说明:1)在公式中d表示日期中的日数,m表示月份数,y表示年数。2)注意:在公式中有个与其他公式不同的地方:把一月和二月看成是上一年的十三月和十四月,例:如果是2004-1-10则换算成:2003-13-10来代入公式计算。

7、打印出任意月份的日历表。

8、输入5位同学的一组信息(结构数组),包括学号、姓名、数学成绩、计算机成绩,

求得每位同学的平均分和总分,然后按照总分从高到低排序。

9、有一批图书(利用结构数组),每本书有:书名(name),作者(author) , 编号(num),出版

日期(date)四个数据,希望输入后按书名的字母顺序将各书的记录排列好,供以后查询。

今输入一本书的书名,如果查询到库中有此书,打印出此书的书名,作者,编号和出版日期。如果查不到此书,则打印出“无此书”。

10、利用指针重做9题:分别设计函数(1)结构数组的数据输入;(2)结构数组的输

出;(3)数组排序;(4)查询;

关于链表:

11、参考第8题,编写函数,建立动态链表,输入几位同学的信息(学号、姓名、数学、

计算机、平均分、总分),然后在屏幕上输出这些数据。

12、将11题中,各同学的总分与平均分计算出来,并输出所有数据;

13、建立函数,为上题中的链表添加一个新的节点。

14、建立按姓名查找某同学记录的函数,找到返回该节点的地址,未找到返回NULL;

15、在某个同学纪录之前插入一个新的节点。(某同学记录之后插入一个新的节点)

16、删除找到的某个同学记录的节点。

17、单向链表逆置;.

18、对单链表按照数学成绩从高到低排序;(只交换除指针成员next之外的其他成员变量

的值)

19、对两个链表进行连接;两个有序链表合并(保持有序);

20、利用指针,交换链表中任意两个节点;(p,q为需要交换的两个节点,首先保证

p<=q,并作为传递的参数,函数中分为几种情形:

21、利用20题函数,重写链表的排序;

(以下例题开始使用带头节点的链表,可以减小编程的难度)

22、利用链表完成习题:有17个人围成一圈(编号为0~16),从第 0号的人开始从 1报数,

凡报到 3的倍数的人离开圈子,然后再数下去,直到最后只剩下一个人为止。问此人原来的位置是多少号?

23、利用插入法,每输入一个节点数据就按顺序插入到链表的某个位置,输入完成即链表排序完

成,输出链表的各节点数据。

24、编写程序,建立环形链表及双向链表(带头节点),并正向、反向输出双向链表中所

有节点数据;

25、编写双向链表的节点插入、删除、交换、排序函数

26、编写一个完整、简易的成绩管理系统:

说明:

1)结构体定义:Student——学号num,姓名name,班级classes,语文chinese,数学math,英语english,总分sum;

2)系统功能:a.成绩录入; b.添加数据(在链表的末尾添加);c.删除某生成绩数

据;d.成绩修改;e.成绩排序(第一关键字班级,第二关键字总分);f.成绩输出(指定班级输出或全部输出,要输出一项人数信息);

27、

参考答案

第2题:

main()

{

struct Date{

int year;

int month;

int day;

}d1;

printf("input date 1(year-month-day):\n");

scanf("%d-%d-%d",&d1.year,&d1.month,&d1.day);

if(yzyear(d1.year)&&yzmonth(d1.month)&&yzday(d1.year,d1.month,d1.day)) printf("yes");

else

printf("no");

getch();

}

yzyear(int year)

{

if(year<1900||year>2056)

return 0;

return 1;

}

yzmonth(int month)

{

if(month<1||month>12)

return 0;

return 1;

}

yzday(int year,int month,int day)

{

switch (month)

{

case 1:case 3:case 5:case 7:case 8:case 10:case 12:

if(day>=1&&day<=31)

return 1;

break;

case 4:case 6:case 9:case 11:

if(day>=1&&day<=30)

return 1;

break;

case 2:

if((year%4==0&&year%100!=0||year%400==0)&&day>=1&&day<=29) return 1;

else if(!(year%4==0&&year%100!=0||year%400==0)&&day>=1&&day<=28) return 1;

break;

}

return 0;

}

第3题:

struct Date{

int year,month,day;

};

int main(void)

{

struct Date d1,d2,tmp;

int x;

printf("input d1(yyyy-mm-dd):\n");

scanf("%d-%d-%d",&d1.year,&d1.month,&d1.day);

printf("input d2(yyyy-mm-dd):\n");

scanf("%d-%d-%d",&d2.year,&d2.month,&d2.day);

x=later(d1,d2);

if(x<0){

tmp=d1;d1=d2;d2=tmp;

}

printf("%d-%d-%d",d1.year,d1.month,d1.day);

printf(" %d-%d-%d",d2.year,d2.month,d2.day);

getch();

return 0;

}

later(struct Date d1,struct Date d2)

{

if(d1.year!=d2.year)

return d1.year-d2.year;

else if(d1.month!=d2.month)

return d1.month-d2.month;

else if(d1.day!=d2.day)

return d1.day-d2.day;

else

return 0;

}

第4题:

struct Date{

int year;

int month;

int day;

};

int main(void)

{

struct Date d1;

int n;

printf("input date 1(year-month-day):\n");

scanf("%d-%d-%d",&d1.year,&d1.month,&d1.day);

n=daysInYear(d1);

printf("from %d-1-1 to %d-%d-%d,%d days",d1.year,d1.year,d1.month,d1.day,n); getch();

return 0;

}

int daysInYear(struct Date d)

{

int m[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};

int i,sum=0;

if(leapyear(d.year))

m[2]=29;

for(i=1;i

sum+=m[i];

sum+=d.day;

return sum;

}

int leapyear(int year)

{

if(year%4==0&&year%100!=0||year%400==0)

return 1;

return 0;

}

第5题:(利用前几题的函数)

struct Date{

int year;

int month;

int day;

};

int main(void)

{

struct Date d1,d2,tmp;

int n;

printf("input date 1(year-month-day):\n");

scanf("%d-%d-%d",&d1.year,&d1.month,&d1.day);

printf("input date 2(year-month-day):\n");

scanf("%d-%d-%d",&d2.year,&d2.month,&d2.day);

if(later(d1,d2)<0){

tmp=d1;d1=d2;d2=tmp;

}

n=datediff(d1,d2);

printf("from %d-%d-%d to %d-%d-%d,%d

days",d2.year,d2.month,d2.day,d1.year,d1.month,d1.day,n); getch();

return 0;

}

int datediff(struct Date d1,struct Date d2)

{

int i,sum=0;

for(i=d2.year;i

{

if(leapyear(i))

sum+=366;

else

sum+=365;

}

sum-=daysInYear(d2);

sum+=daysInYear(d1);

return sum;

}

第6题:(0:星期天 1:星期一…………)

int weekday(struct Date d1)

{

int y=d1.year,m=d1.month,d=d1.day,w;

if(m==1||m==2)

{

m+=12;

y--;

}

w=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400) % 7;

return (w+1)%7;

}

第7题:

calendar(struct Date d1)

{

struct Date d2=d1;

int y=d1.year,m=d1.month,d=d1.day,w,i;

int mm[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; if(leapyear(y))

mm[2]=29;

d2.day=1;

w=weekday(d2);

printf("SUN\tMON\tTRU\tWEN\tTHU\tFRI\tSAT\n");

for(i=0;i

printf("\t");

for(i=1;i<=mm[m];i++)

{

if(i==d)

{

textcolor(YELLOW);

clreol();

printf("%d\t",i);

normvideo();

clreol();

}

else

printf("%d\t",i);

if((i+w)%7==0)

printf("\n");

}

}

第8题:

struct Student{

int num;

char name[20];

int math;

int computer;

float aver;

int sum;

};

int main(void)

{

struct Student stu[5],tmp;

int i,j;

printf("input student infomation:\n");

for(i=0;i<5;i++)

{

printf("----------STUDENT %d--------\nSID:",i+1); scanf("%d",&stu[i].num);

printf("NAME:");

scanf("%s",stu[i].name);

printf("MATH:");

scanf("%d",&stu[i].math);

printf("COMPUTER:");

scanf("%d",&stu[i].computer);

}

for(i=0;i<5;i++)

{

stu[i].sum=stu[i].math+stu[i].computer;

stu[i].aver=stu[i].sum/2;

}

for(i=0;i<5;i++)

{

for(j=i+1;j<5;j++)

{

if(stu[i].sum

{

tmp=stu[i];stu[i]=stu[j];stu[j]=tmp;

}

}

}

for(i=0;i<5;i++)

{

printf("----------STUDENT %d--------\nSID:",i+1); printf("%d\t",stu[i].num);

printf("NAME:");

printf("%s\t",stu[i].name);

printf("MATH:");

printf("%d\t",stu[i].math);

printf("COMPUTER:");

printf("%d\t",stu[i].computer);

printf("AVER:");

printf("%f\t",stu[i].aver);

printf("SUM:");

printf("%d\t\n",stu[i].sum);

}

getch();

return 0;

}

第10题:

#include "Stdio.h"

#include "Conio.h"

#define N 2

struct Date{

int year,month,day;

};

struct Book{

char name[30];

char author[20];

int num;

struct Date pubdate;

};

int main(void)

{

struct Book books[10],*p;

int i;

p=books;

inputbook(p,N);

sort(p,N);

outputbook(p,N);

inputname(p);

window(1,1,80,25);

normvideo();

clrscr();

getch();

return 0;

}

inputname(struct Book *p)

{

char ch[30],*q;

int i;

q=ch;

while(1)

{

window(20,8+N+3,60,8+N+5);

textbackground(WHITE);

textcolor(BLACK);

clrscr();

gotoxy(2,1);

cprintf("your found:(*)");

gets(q);

if(strcmp(q,"*")==0)

break;

i=found(q,p,N);

gotoxy(2,2);clreol();

if(i>=0)

{

printf("%s\t%s\t%d\t%d-%d-%d",(p+i)->name,(p+i)->author,

(p+i)->num,(p+i)->pubdate.year,(p+i)->pubdate.month,(p+i)->pubdate.day); }

else

printf("not found!!!");

getch();

}

}

found(char *q,struct Book *p,int n)

{

int i;

for(i=0;i

{

if(strcmp(q,(p+i)->name)==0)

return i;

}

return -1;

}

inputbook(struct Book *p,int n)

{

int i;

for(i=0;i

{

window(20,5,50,10);

textbackground(WHITE);

textcolor(BLACK);

clrscr();

gotoxy(8,1);

cprintf("--BOOK INFOMATION--\n");

gotoxy(2,2);

cprintf("book name:");

gets(p->name);

gotoxy(2,3);

cprintf("author:");

gets(p->author);

gotoxy(2,4);

cprintf("num:");

scanf("%d",&(p->num));

gotoxy(2,5);

cprintf("pubdate(yyyy-mm-dd):");

scanf("%d-%d-%d",&(p->pubdate.year),&(p->pubdate.month),&(p->pubdate.day)); getchar();

p++;

}

}

outputbook(struct Book *p,int n)

{

int i;

window(20,12,60,12+N+2);

textbackground(WHITE);

textcolor(BLACK);

clrscr();

gotoxy(6,1);

printf("book infomation\n");

gotoxy(2,2);

printf("name\tauthor\tnum\tpubdate\n");

for(i=0;i

{

gotoxy(2,i+3);

printf("%s\t%s\t%d\t%d-%d-%d\n",p->name,p->author,

p->num,p->pubdate.year,p->pubdate.month,p->pubdate.day);

p++;

}

}

sort(struct Book *p,int n){

int i,j;

struct Book tmp;

for(i=0;i

{

for(j=i+1;j

{

if(strcmp((p+i)->name,(p+j)->name)>0) {

tmp=*(p+i);*(p+i)=*(p+j);*(p+j)=tmp; }

}

}

}

第11题:

#include "Stdio.h"

#include "Conio.h"

#define LEN sizeof(struct Student)

struct Student {

int num;

char name[20];

int math,computer;

float aver;

int sum;

struct Student *next;

};

int n;

int main(void)

{

struct Student *create();

struct Student *head;

head=create();

outputstu(head);

getch();

return 0;

}

struct Student *create(){

struct Student *p1,*p2,*head;

char yn;

p1=p2=head=NULL;

n=0;

while(1){

n++;

p1=(struct Student *)malloc(LEN);

inputstu(p1);

if(n==1)

head=p1;

else

p2->next=p1;

p2=p1;

printf("continue?(Y/N)");

yn=getchar();

if(yn=='Y'||yn=='y')

continue;

else

break;

}

p2->next=NULL;

return head;

}

outputstu(struct Student *head){

struct Student *p;

int i=0;

p=head;

printf("num\tname\tmath\tcomputer\n");

while(i

printf("%d\t%s\t%d\t%d\n",p->num,p->name,p->math,p->computer); p=p->next;

i++;

}

}

inputstu(struct Student *p){

printf("num:");

scanf("%d",&p->num);

getchar();

printf("name:");

gets(p->name);

printf("math:");

scanf("%d",&p->math);

printf("computer:");

scanf("%d",&p->computer);

getchar();

}

第12题:

count(struct Student *head) {

struct Student *p;

p=head;

while(p!=NULL){

p->sum=p->math+p->computer;

p->aver=(float)p->sum/2;

p=p->next;

}

}

outputall(struct Student *head){

struct Student *p;

p=head;

printf("num\tname\tmath\tcomput\taver\tsum\n");

while(p!=NULL){

printf("%d\t%s\t%d\t%d\t%5.2f\t%d\n",p->num,p->name,p->math,p->computer,p->aver,p->sum);

p=p->next;

}

}

第13题:

addone(struct Student *head){

struct Student *p,*q;

p=head;

while(p->next!=NULL)

p=p->next;

q=(struct Student *)malloc(LEN);

inputstu(q);

p->next=q;

q->next=NULL;

count(q);

}

第14、15题:

(14题:find函数,15题:insertone函数,另对前几题中函数稍做了些调整,下面把所有函数完整列出)

#include "Stdio.h"

#include "Conio.h"

#define LEN sizeof(struct Student)

struct Student {

int num;

char name[20];

int math,computer;

float aver;

int sum;

struct Student *next;

};

int n;

int main(void)

{

struct Student *create();

struct Student *newone();

struct Student *find(struct Student *head,char *ch);

struct Student *insertone(struct Student *head,char *ch);

struct Student *head,*f;

char c[20],*ch;

int xx;

head=create();

outputstu(head);

ch=c;

getchar();

printf("where you insert:");

gets(ch);

head=insertone(head,ch);

if(head!=NULL)

outputstu(head);

else

printf("not found!");

getch();

return 0;

}

struct Student *insertone(struct Student *head,char *ch){ struct Student *p,*q,*r;

q=newone();

p=find(head,ch);

if(p==NULL){

printf("not found!!!\n");

return head;

}

if(p!=head)

{

r=head;

while(r->next!=p)

r=r->next;

r->next=q;

q->next=p;

return(head);

}

else

{

head=q;

q->next=p;

return(head);

}

}

printhead(int n){

if(n==0)

printf("num\tname\tmath\tcompute\n");

else

printf("num\tname\tmath\tcomput\taver\tsum\n"); }

printbody(struct Student *p,int n){

if(n==0)

printf("%d\t%s\t%d\t%d\n",p->num,p->name,p->math,p->computer);

else

printf("%d\t%s\t%d\t%d\t%5.2f\t%d\n",p->num,p->name,p->math,p->computer,p->aver,p->sum);

}

outputone(struct Student *p){

printhead(0);

printbody(p,0);

}

struct Student *find(struct Student *head,char *ch){

struct Student *p;

p=head;

while(p)

{

if(strcmp(p->name,ch)==0)

return(p);

p=p->next;

}

return(NULL);

}

struct Student *create(){

struct Student *p1,*p2,*head;

char yn;

p1=p2=head=NULL;

n=0;

while(1){

n++;

p1=newone();

if(n==1)

head=p1;

else

p2->next=p1;

p2=p1;

printf("continue?(Y/N)");

yn=getchar();

if(yn=='Y'||yn=='y')

continue;

else

break;

}

p2->next=NULL;

return head;

}

outputstu(struct Student *head){

struct Student *p;

p=head;

printhead(0);

while(p){

printbody(p,0);

p=p->next;

}

}

inputstu(struct Student *p){

printf("num:");

scanf("%d",&p->num);

getchar();

printf("name:");

gets(p->name);

printf("math:");

scanf("%d",&p->math);

printf("computer:");

scanf("%d",&p->computer);

getchar();

}

count(struct Student *head) {

struct Student *p;

p=head;

while(p!=NULL){

p->sum=p->math+p->computer; p->aver=(float)p->sum/2;

p=p->next;

}

}

outputall(struct Student *head){

struct Student *p;

p=head;

printhead(1);

while(p!=NULL){

printbody(p,1);

p=p->next;

}

}

addone(struct Student *head){

struct Student *p,*q;

p=head;

while(p->next!=NULL)

p=p->next;

q=newone();

p->next=q;

q->next=NULL;

count(q);

}

struct Student *newone(){

struct Student *p;

p=(struct Student *)malloc(LEN);

inputstu(p);

return(p);

}

第16题:

struct Student *delone(struct Student *head,char *ch){ struct Student *p,*r;

p=find(head,ch);

if(!p){

printf("not found!!!\n");

return head;

}

if(p==head){

head=p->next;

free(p);

}

else{

r=head;

while(r->next!=p)

r=r->next;

r->next=p->next;

free(p);

}

return head;

}

第17题:

struct Student *reverselink(struct Student *head){

struct Student *p,*q;

p=head;

q=NULL;

while(head!=NULL){

head=head->next;

p->next=q;

q=p;

p=head;

}

return q;

}

第18题:

sort(struct Student *head){

struct Student tmp;

int t;

char ch[20];

struct Student *p,*q,*r;

p=head;

while(p!=NULL){

q=p->next;

while(q!=NULL){

if(p->math>q->math){

t=p->num;p->num=q->num;q->num=t;

strcpy(ch,p->name);strcpy(p->name,q->name);strcpy(q->name,ch); t=p->math;p->math=q->math;q->math=t;

t=p->computer;p->computer=q->computer;q->computer=t;

}

q=q->next;

}

p=p->next;

}

}

第19题(第二问):

struct Student *join(struct Student *head1,struct Student *head2){

struct Student *head3,*p,*q,*r;

p=head1;q=head2;r=NULL;

if(head1->math < head2 ->math){

head3=head1;

p=p->next;

}

else{

head3=head2;

q=q->next;

}

r=head3;

while(p!=NULL&&q!=NULL){

if(p->math < q->math){

r->next=p;

r=p;

p=p->next;

}

else{

r->next=q;

r=q;

q=q->next;

}

}

if(p!=NULL)

r->next=p;

if(q!=NULL)

r->next=q;

return head3;

}

第20题:

struct Student *exchange(struct Student *head,struct Student *p,struct Student *q){ struct Student *findprenode(struct Student *head,struct Student *p);

struct Student *p_pre,*p_nex,*q_pre,*q_nex;

p_pre=p_nex=q_pre=q_nex=NULL;

if(p==q)

return head;

if(p->next!=q){

if(p==head){

q_pre=findprenode(head,q);

q_nex=q->next;

head=q;

q->next=p->next;

q_pre->next=p;

p->next=q_nex;

}

else{

p_pre=findprenode(head,p);

p_nex=p->next;

q_pre=findprenode(head,q);

q_nex=q->next;

p_pre->next=q;

q->next=p_nex;

q_pre->next=p;

p->next=q_nex;

}

}

else{

if(p==head){

q_nex=q->next;

head=q;

q->next=p;

p->next=q_nex;

}

else{

p_pre=findprenode(head,p);

q_nex=q->next;

p_pre->next=q;

q->next=p;

p->next=q_nex;

}

}

return head;

}

struct Student *findprenode(struct Student *head,struct Student *p){

struct Student *p_pre;

p_pre=head;

while(p_pre->next!=p)

p_pre=p_pre->next;

return p_pre;

}

第21题:

交换结点函数,不需要返回值,将最后的return head去除;为方便交换结点函数的调用,sort1函数中,为链表临时增加一个表头,不参与排序;

struct Student *newhead(){

struct Student *p;

p=(struct Student *)malloc(LEN);

p->num=0;

strcpy(p->name,"head");

p->math=p->computer=0;

return(p);

}

struct Student *sort1(struct Student *head){

int t=0;

struct Student *p,*q,*nhead,*r;

nhead=newhead();

nhead->next=head;

p=head;

q=p->next;

while(p!=NULL){

q=p->next;

while(q!=NULL){

if(p->math>q->math){

exchange1(nhead,p,q);

r=p;p=q;q=r;

/*r=p;

p=q;

q=r->next;

*/

}

q=q->next;

}

p=p->next;

}

return nhead->next;

}

第22题:(使用带头节点的链表)

结构体指针

C++语言结构体和指针 指针也可以指向一个结构体,定义的形式一般为: struct结构体名*变量名; 下面是一个定义结构体指针的实例: 上述代码已经测试。 注意:定义已经命名的结构体指针的时候必须用已命名结构体类型定义的结构体变量的地址进行初始化。 也可以在定义结构体的同时定义结构体指针: 上述代码已经测试 注意,结构体变量名和数组名不同,数组名在表达式中会被转换为数组指针,而结构体变量名不会,无论在任何表达式中它表示的都是整个集合本身,要想取得结构体变量的地址,必 pstu赋值只能写作: struct stu *pstu = &stu1; 而不能写作: struct stu *pstu = stu1; 还应该注意,结构体和结构体变量是两个不同的概念:结构体是一种数据类型,是一种创建变量的模板,编译器不会为它分配内存空间,就像int、float、char 这些关键字本身不占用内存一样;结构体变量才包含实实在在的数据,才需要内存来存储。下面的写法是错误的,不可能去取一个结构体名的地址,也不能将它赋值给其他变量: struct stu *pstu = &stu; struct stu *pstu = stu;

获取结构体成员 通过结构体指针可以获取结构体成员,一般形式为: (*pointer).memberName 或者: pointer->memberName 对了。 ,有了它,可以通过结构体指针 直接取得结构体成员;这C语言中的唯一用途。 上面的两种写法是等效的,我们通常采用后面的写法,这样更加直观。

运行结果: Name Num Age Group Score Zhou ping 5 18 C 145.0 Zhang ping 4 19 A 130.5 Liu fang 1 18 A 148.5 Cheng ling 2 17 F 139.0 Wang ming 3 17 B 144.5 结构体指针作为函数参数 结构体变量名代表的是整个集合本身,作为函数参数时传递的整个集合,也就是所有成员,而不是像数组一样被编译器转换成一个指针。如果结构体成员较多,尤其是成员为数组时,传送的时间和空间开销会很大,影响程序的运行效率。所以最好的办法就是使用结构体指针,这时由实参传向形参的只是一个地址,非常快速。 要铭记的一点就是:数组名称始终代表数组的指针指向第一个元素,数组名称加一始终指向下一个数组元素。

第九章:结构体

1、定义两个日期结构体变量(包括年、月、日)。输入两个日期值,比较两个日期的大 小(越靠后越大),输出较大的日期。 2、将1题中输入的日期进行验证,保证日期的正确性。(1)不可为负数;(2)年份 1900-2056之间,月份在1-12之间;(3)日期在1-31日以内(注意各月份的日期数判断及闰年的判断)(Year % 4 ==0 && Year % 100 !=0 || Year % 400 ==0) 3、输入两个日期,编程交换两个日期值,保证第一个日期是较大的日期(日期的大小比 较用函数表达)。 4、计算某日期是一年中的第几天。 5、计算两日期间相差的天数。 6、利用基姆拉尔森计算公式:W= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400) mod 7,计算 任意日期是星期几。说明:1)在公式中d表示日期中的日数,m表示月份数,y表示年数。2)注意:在公式中有个与其他公式不同的地方:把一月和二月看成是上一年的十三月和十四月,例:如果是2004-1-10则换算成:2003-13-10来代入公式计算。 7、打印出任意月份的日历表。 8、输入5位同学的一组信息(结构数组),包括学号、姓名、数学成绩、计算机成绩, 求得每位同学的平均分和总分,然后按照总分从高到低排序。 9、有一批图书(利用结构数组),每本书有:书名(name),作者(author) , 编号(num),出版 日期(date)四个数据,希望输入后按书名的字母顺序将各书的记录排列好,供以后查询。 今输入一本书的书名,如果查询到库中有此书,打印出此书的书名,作者,编号和出版日期。如果查不到此书,则打印出“无此书”。 10、利用指针重做9题:分别设计函数(1)结构数组的数据输入;(2)结构数组的输 出;(3)数组排序;(4)查询; 关于链表: 11、参考第8题,编写函数,建立动态链表,输入几位同学的信息(学号、姓名、数学、 计算机、平均分、总分),然后在屏幕上输出这些数据。 12、将11题中,各同学的总分与平均分计算出来,并输出所有数据; 13、建立函数,为上题中的链表添加一个新的节点。 14、建立按姓名查找某同学记录的函数,找到返回该节点的地址,未找到返回NULL; 15、在某个同学纪录之前插入一个新的节点。(某同学记录之后插入一个新的节点) 16、删除找到的某个同学记录的节点。 17、单向链表逆置;. 18、对单链表按照数学成绩从高到低排序;(只交换除指针成员next之外的其他成员变量 的值) 19、对两个链表进行连接;两个有序链表合并(保持有序);

结构体的指针应用

什么是结构体? 简单的来说,结构体就是一个可以包含不同数据类型的一个结构,它是一种可以自己定义的数据类型,它的特点和数组主要有两点不同,首先结构体可以在一个结构中声明不同的数据类型,第二相同结构的结构体变量是可以相互赋值的,而数组是做不到的,因为数组是单一数据类型的数据集合,它本身不是数据类型(而结构体是),数组名称是常量指针,所以不可以作为左值进行运算,所以数组之间就不能通过数组名称相互复制了,即使数据类型和数组大小完全相同。 定义结构体使用struct修饰符,例如: struct test { float a; int b; }; 上面的代码就定义了一个名为test的结构体,它的数据类型就是test,它包含两个成员a和b,成员a的数据类型为浮点型,成员b的数据类型为整型。由于结构体本身就是自定义的数据类型,定义结构体变量的方法和定义普通变量的方法一样。 test pn1; 这样就定义了一个test结构体数据类型的结构体变量pn1,结构体成员的访问通过点操作符进行,pn1.a=10 就对结构体变量pn1的成员a进行了赋值操作。注意:结构体生命的时候本身不占用任何内存空间,只有当你用你定义的结构体类型定义结构体变量的时候计算机才会分配内存。 结构体,同样是可以定义指针的,那么结构体指针就叫做结构指针。 结构指针通过->符号来访问成员,下面我们就以上所说的看一个完整的例子: #include #include using namespace std; struct test//定义一个名为test的结构体 { int a;//定义结构体成员a int b;//定义结构体成员b }; void main() { test pn1;//定义结构体变量pn1 test pn2;//定义结构体变量pn2 pn2.a=10;//通过成员操作符.给结构体变量pn2中的成员a赋值 pn2.b=3;//通过成员操作符.给结构体变量pn2中的成员b赋值

第九章 结构体

一、概念题 F T T T F T T 二、判断题 1. 结构体中的成员不可以单独使用(F)。 2. 成员名可以与程序中的变量名相同,二者不代表同一对象(T)。 3. 不能将一个结构体变量作为一个整体进行输入输出(T )。 4. 结构体变量所占内存长度是各成员占的内存长度之和(T )。 5. 结构体中的成员不可以单独使用(F)。 9. 一个结构体变量的指针就是该变量所占内存段的起始地址(T)。 10. 用结构体变量作实参,形参也必须是同类型的结构体变量(T)。 三、单选题 1. 设变量定义如下,则对其中的结构分量num正确的引用是( )。 struct student { int num ; char name[20]; float score; } stud[10]; A. stud[1].num=10; B. student.stud.num=10; C. struct.stud.num=10; D. struct student.num=10; 2. 已知职工记录描述如下,设变量w中的“生日”是“1993年10月25日”,下列对“生日”的正确赋值方式是()。 struct worker { int no; char name[20]; char sex; struct birth{ int day; int month; int year;}a; }; struct worker w; A day=25;month=10;year=1993; B w.birth.day=25; w.birth.month=10; w.birth.year=1993; C w.day=25; w.month=10; w.year=1993; D w.a.day=25; w.a.month=10; w.a.year=1993; 3. 对于以下的变量定义,语句( )在语法和语义上都是正确的。 struct node { float x,y; char s[10];

指针和结构体练习题.

第十章指针 一.选择题 1.变量的指针,其含义是指该变量的。 A)值 B)地址 C)名 D)一个标志 2.已有定义int k=2;int *ptr1,*ptr2;且ptr1和ptr2均已指向变量k,下面不能正确执行的赋值语句是。 A)k=*ptr1+*ptr2 B)ptr2=k C)ptr1=ptr2 D)k=*ptr1*(*ptr2 3.若有说明:int *p,m=5,n;以下程序段正确的是。 A)p=&n ; B)p = &n ; scanf(“%d”,&p; scanf(“%d”,*p; C)scanf(“%d”,&n; D)p = &n ; *p=n ; *p = m ; 4.已有变量定义和函数调用语句:int a=25;print_value(&a;下面函数的输出结果是。 void print_value(int *x { printf(“%d\n”,++*x; } A)23 B)24 C)25 D)26 5.若有说明:int *p1, *p2,m=5,n;以下均是正确赋值语句的选项是。 A)p1=&m; p2=&p1 ; B)p1=&m; p2=&n; *p1=*p2 ; C)p1=&m; p2=p1 ; D)p1=&m; *p1=*p2 ; 6.若有语句:int *p,a=4;和p=&a;下面均代表地址的一组选项是。 A)a,p,*&a B)&*a,&a,*p C)*&p,*p,&a D)&a,&*p,p 7.下面判断正确的是。 A)char *a=”china”; 等价于char *a; *a=”china” ; B)char str[10]={“china”}; 等价于char str[10]; str[ ]={“china”;}

c程序设计 第九章 结构体

一、概念题 二、判断题 1. 结构体中的成员不可以单独使用(F)。 2. 成员名可以与程序中的变量名相同,二者不代表同一对象(T)。 3. 不能将一个结构体变量作为一个整体进行输入输出(T)。 4. 结构体变量所占内存长度是各成员占的内存长度之和(T)。 5. 结构体中的成员不可以单独使用(F )。 9. 一个结构体变量的指针就是该变量所占内存段的起始地址(T)。 10. 用结构体变量作实参,形参也必须是同类型的结构体变量(T )。 三、单选题 1. 设变量定义如下,则对其中的结构分量num正确的引用是( )。 struct student { int num ; char name[20]; float score; } stud[10]; A. stud[1].num=10; B. student.stud.num=10; C. struct.stud.num=10; D. struct student.num=10; 2. 已知职工记录描述如下,设变量w中的“生日”是“1993年10月25日”,下列对“生日”的正确赋值方式是()。 struct worker { int no; char name[20]; char sex; struct birth{ int day; int month; int year;}a; }; struct worker w; A day=25;month=10;year=1993; B w.birth.day=25; w.birth.month=10; w.birth.year=1993; C w.day=25; w.month=10; w.year=1993; D w.a.day=25; w.a.month=10; w.a.year=1993; 3. 对于以下的变量定义,语句( )在语法和语义上都是正确的。 struct node { float x,y; char s[10];

C语言中不同的结构体类型的指针间的强制转换详解

C语言中不同类型的结构体的指针间可以强制转换,很自由,也很危险。只要理解了其内部机制,你会发现C是非常灵活的。 一. 结构体声明如何内存的分布, 结构体指针声明结构体的首地址, 结构体成员声明该成员在结构体中的偏移地址。 变量的值是以二进制形式存储在内存中的,每个内存字节对应一个内存地址,而内存存储的值本身是没有整型,指针,字符等的区别的,区别的存在是因为我们对它们有不同的解读,param的值就是一个32位值,并且存储在某个内存单元中,通过这个32位值就能找到param所指向的结构的起始地址,通过这个起始地址和各个结构所包含变量离起始地址的偏移对这些变量进行引用, param->bIsDisable只是这种引用更易读的写法,只要param是指向 PAINT_PARAM的指针,那么param的值就肯定存在,param存在,偏移量已知,那么param->bIsDisable就肯定存在,只是要记住,param->bIsDisable只是代表了对param一定偏移地址的值。 不是说某个地址有那个结构体你才能引用,即使没有,你也能引用,因为你已经告诉了编译器param变量就是指向一个PAINT_PARAM结构体的变量并且指明了param的值,机器码的眼中是没有数据结构一说的,它只是机械的按照 指令的要求从内存地址取值,那刚才的例子来说,peg->x,peg->y的引用无论 0x30000000是否存在一个eg结构体都是合法的,如果0x30000000开始的8 个字节存在eg结构体,那么引用的就是这个结构体的值,如果这个位置是未定义的值,那么引用的结果就是这8个字节中的未定义值,内存位置总是存在的,而对内存中值的引用就是从这些内存位置对应的内存单元取值。 举个例子: typedefstruct_eg { int x; int y; }eg;

C语言习题及答案(第九章)

9-3编写程序,使用结构体类型,输出一年十二个月的英文名称及相应天数。 解:#include "stdio.h" struct date { char month[10] ; int daynumber ; } main() { int i ; struct date a[12]={{"January",31},{"February",29},{"March",31},{"Aprial",30},{ "May",31},{"June",30},{"july",31},{"August",31},{"September",30}, {"October",31},{"November",30},{"December",31}} ; for(i=0;i<12;i++); printf("%d 月:%s %d\n",i+1,a[i].month,a[i].daynumber) ; } 思考:如何对结构体变量进行初始化?对结构体变量的引用为何要体现为分量(或成员)的引用? 9-4 编写程序求空间任一点到原点的距离,点用结构体描述。并请考虑求空间中任意两点的距离的程序。 解:#include "stdio.h" #include "math.h" struct point { float x ; float y ; float z ; } main() { double d1,d2,d ; struct point p1,p2 ; printf("请输入第一个点的坐标:");

scanf("%f,%f,%f",&p1.x,&p1.y,&p1.z); printf("请输入第二个点的坐标:"); scanf("%f,%f,%f",&p2.x,&p2.y,&p2.z); d1=sqrt(p1.x*p1.x+p1.y*p1.y+p1.z*p1.z); d2=sqrt(p2.x*p2.x+p2.y*p2.y+p2.z*p2.z); d=sqrt((p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y)+(p2.z-p1.z)*( p2.z-p1.z)); printf("第一个点到原点的距离:%f\n",d1); printf("第二个点到原点的距离:%f\n",d2); printf("两点间的距离:%f\n",d); } 9-5 编写输入、输出10个朋友数据的通讯录程序,每个朋友数据包括姓名、地址、邮编、电话、传呼、手机等数据。 解:#include "stdio.h" struct AddressBook { char name[10] ; char address[30] ; char mailnumber[7] ; char telphone[12] ; char byphone[16] ; char movephone[1] ; } main() { int i ; struct AddressBook fd[10] ; for(i=0;i<10;i++) { printf("请输入第%d个朋友的信息:\n",i+1); printf("姓名:"); scanf("%s",&fd[i].name) ; printf("地址:");

指针与结构体 上机

指针 1.在主函数中输入一个字符串str,调用函数统计字符串中出现的字母(含大 小写)、数字、空格及其他字符出现的次数,在主函数中输出统计结果。要求写三个版本的程序:(1)用指针作参数返回统计结果。(2)用引用做参数返回统计结果(引用做参数效率更高,代码更简单。)(3)用数组做参数返回统计结果(当返回多个同类型结果时用数组做参数更简单)。 1.#include using namespace std; void stat(char *str,int *letters,int *digits,int *others){ char c; for(char *str;*str!='\0';str++) {c=*str; if((c>'a'&&c<'z')||(c>'A'&&c<'Z')) (*letters)++; else if('0'<=c&&c<='9') (*digits)++; else (*others)++; } } void main(){ char str[100]; cin.getline(str,100); int letters=0; int digits=0; int others=0; stat(str,&letters,&digits,&others); cout<<"letters="< #include using namespace std; void stat(char *str,int *a){ char c; for(int i=0;str[i]!='\0';i++) {c=str[i];

函数、指针与结构体练习题_参考答案

函数 (一)选择题 1.以下正确的说法是_________. 建立函数的目的之一是a)提高程序的执行效率 b)提高程序的可读性 c)减少程序的篇幅 d)减少程序文件所占存 2.以下正确的函数原型声明形式是________. a)double fun(int x,int y) b)double fun(int x; int y) c)double fun(int x, int y); d)double fun(int x,y); 3.C语言规定,简单变量做实参时,它和对应形参之间的数据传递方式为______. A)地址传递 B)单向值传递 C)由实参传给形参,再由形参传回给实参 D)由用户指定传递方式 4.C语言允许函数值类型缺省定义,此时该函数值隐含的类型是______. a)float b)int c)long d)double 5.已有以下数组定义和f函数调用语句,则在f函数的说明中,对形参数组array 的错误定义方式为________. int a[3][4]; f(a); a)f(int array[][6])

b)f(int array[3][]) c)f(int array[][4]) d)f(int array[2][5]) 6.以下程序的正确运行结果是_________. #include void num() { extern int x,y;int a=15,b=10; x=a-b; y=a+b; } int x,y; main() { int a=7,b=5; x=a+b; y=a-b; num(); printf("%d,%d\n",x,y); } a)12,2 b)不确定c)5,25 d)1,12 7.以下正确的描述是____________. a)C语言的预处理功能是指完成宏替换和包含文件的调用 b)预处理指令只能位于C源程序文件的首部 c)凡是C源程序中行首以"#"标识的控制行都是预处理指令 d)C语言的编译预处理就是对源程序进行初步的语法检查 8.在"文件包含"预处理语句的使用形式中,当#include后面的文件名用< >(尖括号)括起时,找寻被包含文件的方式是_______. a)仅仅搜索当前目录 b)仅仅搜索源程序所在目录

第九章 习题及答案

第九章习题 一、选择题 1.以下选项中不能正确把cl定义成结构体变量的是( ) A)typedef struct B)struct color cl { int red; { int red; int green; int green; int blue; int blue; } COLOR; COLOR cl; }; C)struct color D)struct { int red; { int red; int green; int green; int blue; int blue; } cl; } cl; 2.有以下说明和定义语句 struct student { int age; char num[8];}; struct student stu[3]={{20,"200401"},{21,"200402"},{10\9,"200403"}}; struct student *p=stu; 以下选项中引用结构体变量成员的表达式错误的是( ) A) (p++)->num B)p->num C)(*p).num D)stu[3].age 3.有以下结构体说明、变量定义和赋值语句 struct STD {char name[10]; int age; char sex; }s[5],*ps; ps=&s[0]; 则以下scanf函数调用语句中错误引用结构体变量成员的是( )。 A)scanf(“%s”,s[0].name); B)scanf(“%d”,&s[0].age); C)scanf(“%c”,&(ps->sex)); D)scanf(“%d”,ps->age); 4.以下叙述中错误的是() A)可以通过typedef增加新的类型 B)可以用typedef将已存在的类型用一个新的名字来代表 C)用typedef定义新的类型名后,原有类型名仍有效 D)用typedef可以为各种类型起别名,但不能为变量起别名 5.有以下程序段() typedef struct node { int data; struct node *next; } *NODE; NODE p;

第九章使用结构体类型处理组合数据 c语言

第九章使用结构体类型处理组合数据 1.定义和使用结构体变量 2.结构体数组 3.结构体指针 4.用结构体变量和结构体变量的指针作函数参数 5.用指针处理链表 6.共用体、枚举类型 正文 1.定义和使用结构体变量 若只保存某个学生的学号:可以使用int 变量。 若保存所有学生的学号:可以使用int 型的数组。 同理,若保存所有学生的姓名:可以使用char型的数组。 若保存所有学生某科成绩:可以使用float 型的数组。 但是,如果要同时保存某一个学生的学号,姓名,性别、入学时间及各科成绩,该用什么保存? 自己建立结构体类型 将一个学生的学号、姓名、性别、年龄和地址分别用以下变量来表示: int num; char name[20]; char sex; int age; char addr[30]; Num name sex age score addr 100101 Li Fun M 18 87.5 Beijing 声明一个结构体类型的一般形式为: struct 结构体名 {成员表列=类型名+成员名}; 如:struct student { int num;char name[20];char sex; int age;float score;char addr[30]; } 可以采取以下3种方法定义结构体类型变量:

(1)先声明结构体类型再定义变量名 例如:struct student student1, student2; | | | 结构体类型名结构体变量名 定义了student1和student2为struct student类型的变量,即它们具有struct student 类型的结构. student1 100102 WangLi F 20 98 Beijing student2 100101 ZhangXin M 19 90.5 Shanghai 在定义了结构体变量后,系统会为之分配内存单元。 例如:student1和student2在内存中各占63个字节(4+20+1+4+4+30=63)。(我们的VC) 注意: 将一个变量定义为标准类型(基本数据类型)与定义为结构体类型不同之处在于后者不仅要求指定变量为结构体类型,而且要求指定为某一特定的结构体类型,因为可以定义出许许多多种具体的结构体类型。 (2)在声明类型的同时定义变量 这种形式的定义的一般形式为: struct结构体名 { 成员表列 }变量名表列; struct student {int num; char name[20]; char sex; int age; float score; char addr[30]; }student1,student2;//它的作用与第一种方法相同,即定义了两个struct //student 类型的变量student1 student2 (3) 直接定义结构体类型变量 //注意: (2)对结构体中的成员(即“域”),可以单独使用,它的作用与地位相当于普通变量。 (3)成员也可以是一个结构体变量。 (4)成员名可以与程序中的变量名相同,二者不代表同一对象。 其一般形式为: struct { 成员表列 }变量名表列; 即不出现结构体名。 例如:struct date

C语言程序设计:第9章 结构体 课件教案

C语言程序设计课件教案 第九章结构体 教学目的 掌握结构体的定义、存储、引用和初始化的基本语法,学会引用结构体中的每一个成员变量。掌握结构体数组的定义、存储、引用和初始化的基本语法。教学要求 1.熟练掌握结构体的定义、存储、引用和初始化的基本语法 2.学会引用结构体中的每一个成员变量 3.熟练掌握使用typedef定义结构体 4.熟练掌握结构体数组的定义、存储、引用和初始化的基本语法 重点和难点 1.结构体的应用 2.结构体成员变量的引用 3.结构体数组的应用 教学支撑环境与教学资源: 1.笔记本电脑 2.VC2010 3.投影仪 教学方法 讲授法、练习法、讨论法 授课课时 6课时 教学过程 ---------------------------------AM--------------------------------- 一、课程引入 同时存储全班同学的C语言成绩、PS成绩等需要定义多个类型不同的变量,可以采用定义结构体的方式解决。 二、结构体的定义 C语言中,可以自己构造出自己想要的数据类型,这种类型叫做复合数据类型。复合数据类型------结构体--是由一系列具有相同类型或不同类型的数据构成的数

据集合 结构体:不同的数据类型组成的组合型的数据结构 1.结构体的定义 struct 结构体名 { 成员列表 }; struct student { int num; int age; float score; double avg; }; 说明: 1>定义了一个结构体类型,结构体的关键字---struct,结构体名为student; 2>这个结构体包含了多个不同的基本数据类型。 注意: 1>结构体的类型可以多种,名字由关键字struct和结构体名组合而成 2>访问成员的规则: (1)结构体普通变量通过"."来访问成员,例如:pe.id (2)结构体指针变量通过"->"来访问成员, 例如:p->id(大致提一下) 三、定义结构体变量 1.先声明,再定义 struct 结构体名 { 数据类型成员变量名; 数据类型成员变量名; 数据类型成员变量名; ...; }; 语法:struct 结构体名变量名; 例如: struct student//student为结构体名 { int num; int c_score; int ps_score; float avg; };//定义结构体 int main(void) { struct student Tom;//Tom为结构体变量

第九章 结构体与共用体

第九章结构体与共用体 一、选择题 1.在说明一个结构体变量时系统分配给它的存储空间是。(0级) A)该结构体中第一个成员所需存储空间 B)该结构体中最后一个成员所需存储空间 C)该结构体中占用最大存储空间的成员所需存储空间 D)该结构体中所有成员所需存储空间的总和 2.若有以下说明和语句: struct worker { int no; char ﹡name; }work, ﹡p=&work; 则以下引用方式不正确的是。(1级) A) work.no B) (﹡p).no C) p->no D)work->no3.有如下定义: struct date { int year, month, day; }; struct worklist { char name[20]; char sex; struct date birthday; }person; 对结构体变量person的出生年份进行赋值时,下面正确的赋值语句是。 (1级) A) year=1958 B) birthday.year=1958 C) person.birthday.year=1958 D) person.year=1958 4.以下对结构体类型变量的定义中不正确的是。(1级) A)#define STUDENT struct student B) struct student STUDENT { int num; { int num; float age; float age; }std1; }std1; C) struct D) struct { int num; { int num; float age; float age; } student; }std1; struct student std1; 5.设有以下说明语句 struct stu { int a; float b; }stutype; 则下面的叙述不正确的是。(1级) A)struct是结构体类型的关键字 B)struct stu是用户定义的结构体类型 C)stutype是用户定义的结构体类型名 D)a和b都是结构体成员名 6.C语言结构体类型变量在程序执行期间。(0级) A)所有成员一直驻留在内存中 B)只有一个成员主留在内存中 C)部分成员驻留在内存中 D)没有成员驻留在内存中

C语言复习题指针结构体

C语言复习题_指针&结构体 一、选择 1、若有以下定义:char s[20]="programming",*ps=s; 则不能代表字符'o'的表达式是A。 A) ps+2 B) s[2] C) ps[2] D) ps+=2,*ps 2、若有以下定义和语句: int a[10]={1,2,3,4,5,6,7,8,9,10},*p=a;则不能表示a数组元素的表达式是B。 A) *p B) a[10] C) *a D) a[p-a] 3、已知int *p,a; p=&a; 这里的运算符& 的含义D。 A) 位与运算B) 逻辑与运算C) 取指针内容D) 取变量地址 4、定义结构体如下: struct student { int num; char name[4]; int age; }; 则printf(“%d”,sizeof(struct student))的结果为: 12。 5、若有定义如下:int i=3,*p=&i; 显示i的值的正确语句是B。 A) printf(“%d”,p); B) printf(“%d”,*p); C) printf(“%p”,*p); D) printf(“%p”,p); 6、在定义结构体时,下列叙述正确的是A。 A) 系统不会分配空间 B) 系统会按成员大小分配空间 C) 系统会按最大成员大小分配空间 D) 以上说法均不正确 7、指针是一种D。 A) 标识符B) 变量C) 运算符D) 内存地址 8、定义struct s {int x; char y[6];} s1;,请问正确的赋值是C。 A) s1.y=”abc”; B) s1->y=”abc”; C) strcpy(s1.y,”abc”); D) s1.strcpy(y,”abc”); 9、已知定义“int x =1, *p”,则合法的赋值表达式是A。 A) p =&x B) p = x C) *p =&x D) *p =*x

结构体指针的典型应用(通过姓名查找相关信息)

对录入的学生的学号、年龄、分组、成绩通过输入姓名进行查找,如果没有则输出“not exist!”。 #include "stdafx.h" #include #include "string.h" void main(){ struct stu{ char *name; int num; int age; char group; float score; }stus[] = { {"Zhou ping", 5, 18, 'C', 145.0}, {"Zhang ping", 4, 19, 'A', 130.5}, {"Liu fang", 1, 18, 'A', 148.5}, {"Cheng ling", 2, 17, 'F', 139.0}, {"Wang ming", 3, 17, 'B', 144.5} /*可以输入更多*/ }, *ps; char st_name[23]; /*字符数可以增加*/ printf("input name:"); gets(st_name); const n=sizeof(stus) / sizeof(struct stu); int cla[n]; int h; for(h=0;h

c语言结构体指针初始化===

c语言结构体指针初始化 今天来讨论一下C中的内存管理。 记得上周在饭桌上和同事讨论C语言的崛起时,讲到了内存管理方面 我说所有指针使用前都必须初始化,结构体中的成员指针也是一样 有人反驳说,不是吧,以前做二叉树算法时,他的左右孩子指针使用时难道有初始化吗 那时我不知怎么的想不出理由,虽然我还是坚信要初始化的 过了几天这位同事说他试了一下,结构体中的成员指针不经过初始化是可以用(左子树和右子树指针) 那时在忙着整理文档,没在意 今天抽空调了一下,结论是,还是需要初始化的。 而且,不写代码你是不知道原因的(也许是对着电脑久了IQ和记性严重下跌吧) 测试代码如下 1.#include 2.#include 3.#include 4. 5.struct student{ 6.char *name; 7.int score; 8.struct student* next; 9.}stu,*stu1; 10. 11.int main(){ 12. https://www.doczj.com/doc/0b3969497.html, = (char*)malloc(sizeof(char)); /*1.结构体成员指针需要初始化*/ 13. strcpy(https://www.doczj.com/doc/0b3969497.html,,"Jimy"); 14. stu.score = 99; 15. 16. stu1 = (struct student*)malloc(sizeof(struct student));/*2.结构体指针需要初始化*/ 17. stu1->name = (char*)malloc(sizeof(char));/*3.结构体指针的成员指针同样需要初始化*/ 18. stu.next = stu1; 19. strcpy(stu1->name,"Lucy"); 20. stu1->score = 98; 21. stu1->next = NULL; 22. printf("name %s, score %d \n ",https://www.doczj.com/doc/0b3969497.html,, stu.score); 23. printf("name %s, score %d \n ",stu1->name, stu1->score); 24. free(stu1); 25.return 0; 26.} #include #include #include struct student{ char *name; int score; struct student* next; }stu,*stu1; int main(){ https://www.doczj.com/doc/0b3969497.html, = (char*)malloc(sizeof(char)); /*1.结构体成员指针需要初始化*/ strcpy(https://www.doczj.com/doc/0b3969497.html,,"Jimy"); stu.score = 99; stu1 = (struct student*)malloc(sizeof(struct student));/*2.结构体指针需要初始化*/ stu1->name = (char*)malloc(sizeof(char));/*3.结构体指针的成员指针同样需要初始化*/ stu.next = stu1; strcpy(stu1->name,"Lucy"); stu1->score = 98; stu1->next = NULL; printf("name %s, score %d \n ",https://www.doczj.com/doc/0b3969497.html,, stu.score);

c语言练习题7(指针与结构体,有答案)

1、下列程序的运行结果是_B______。 void fun(int *a,int *b) { int *k: k=a;a-b;b=k; } main() { int a=3,b=6,*x=&a,*y=&b; fun(x,y); printf[”%d%d.f,a,b); } A)6 3 B)36 C)编译出错 D)0 0 PS:本题中主函数里的x、y,fun函数里的a、b、k,这些都是指针,fun函数中只是将a、b这两个指针交换了位置,而并没有改变主函数中变量a、b的值。 2、若有定义:int*p[3];,则以下叙述中正确的是____B____。 A)定义了一个基类型为int的指针变量p,该变量有三个指针 B)定义了一个指针数组p,该数组含有三个元素,每个元素都是基类型为int的指针 C)定义了一个名为+p的整型数组,该数组含有三个int类型元素 D)定义了一个可指向一维数组的指针变量p,所指一维数组应具有三个int类型元素 PS:由于运算符[]优先级比*高,int*p[3];相当于int *(p[3]);表示数组p 的三个元素都是指针变量,且每个元素都是基类型为int的指针。 3、有以下程序:

void swapl(int *a,int *b) {int *c=a; a=b,b=c; } void swap2(int *a,int *b) { int c=*a: *a=*b,*b=c; } main() (int a=lO,b=15; swapl(&a,&b); printf(”%d,%d,”,a,b); a=lO,b=15; swap2(&a,&b); printf(”%d,%dt.,a,b); } 其输出结果为_10,15,15,10___。 A)15,10,10,15 B)15,10,15,10 C)10,15,10,15 D)10,15,15,10 PS:C语言规定,实参变量对形参变量的数据传递是“值传递”,只由实参传给形参,而不能由形参传回来给实参。在内函数调用结束后,形参单元被释放,实参单元仍保留并维持原值。本题中swapl()函数中,虽然改变了形参指针的值,但实参指针的值并没有改变,所以执行第一个printf后应输出10,15,;swap2()函数实现了交换两个变量a和b的值,因此执行第二个printf后输出交换后的值15,10,所以本题答案为D。

C语言实验报告之指针、结构体等综合程序设计

一、实验的目的、要求及内容 1、通过实验掌握指针的概念,学会定义和使用指针变量。 2、能正确使用数组的指针和指向数组的指针变量。 3、能正确使用字符串指针和指向字符串的指针变量。 4、掌握结构体类型变量的定义和使用。 二、算法设计思想及内容 本实验主要完成指针和结构体的应用训练,总共要求完成三个问题的源代码的编辑、编译、连接与运行,最终产生相关的运行结果,按规定要求提交相应的实验报告。具体要求完成的问题如下: 1、采用指针方式编写程序输入10个整数,将其中最小的数与第一个数对换,把最 大的数与最后一个数对换。写3个函数:(1)输入10个数;(2)进行处理;(3) 输出处理后的10个数。 2、采用指针方式在主函数中输入字符串,在函数中求出字符串的长度,然后输出求 出的长度。 3、有5个学生,每个学生的数据包括学号、姓名、3门课的成绩,从键盘输入5个 学生的数据,要求输出3门课程的总平均成绩,以及最高分数的学生的数据(包 括学号、姓名、3门课的成绩、平均分)。 三、所使用的软硬件平台 软件平台:Microsoft Windows XP专业版SP3,Microsoft Visual C++ 6.0。 硬件平台:联想系列,Intel(R)Core(TM)i3,CPU 3.20Ghz,2.99GB内存,550GB硬盘。 四、源程序代码 1.#include int main() {int a[10]; void shuru(int a[10]); void chuli(int a[10]); void shuchu(int a[10]); printf("请输入10个数:"); shuru(a); chuli(a); printf("输出处理后的10个数为:"); shuchu(a); printf("\n"); return 0; }

相关主题
文本预览
相关文档 最新文档