實(shí)驗(yàn)11 ?指針綜合實(shí)驗(yàn)
任務(wù) ?使用指針編寫綜合應(yīng)用程序
1.某班有M個學(xué)生,開設(shè)三門課程(高數(shù)、英語、計算機(jī))的成績,編程輸入學(xué)生的學(xué)號及三門課程的成績,求各人的平均分,并顯示。
參考程序如下:
#include "stdio.h"
#define M 5
struct student
{
????int num;
????int score[3]; ?
????float average;
}stu[M];
main( )
{
???struct student *p;
???for(p=stu;p<stu+M;p++)
???{
??scanf("%d %d %d %d",&(p->num),&(p->score[0]),&(p->score[1]),&(p->score[2]));
??????p->average=(p->score[0]+p->score[1]+p->score[2])/3.0;
???}
???printf("平均分為:\n\n");
???for(p=stu;p<stu+M;p++)
???{ ??printf("%5d%5d%5d%5d%7.2f\n",p->num,p->score[0],p->score[1],p->score[2],p->average);
???}
}
2.編寫一函數(shù),求一個字符串的長度。在main(?)函數(shù)中輸入字符串,并輸出其長度。
參考程序如下:
#include <stdio.h>
#include <string.h>
int len(char *s)
{
????int k=0;
????for(;*s!='\0';s++)
??????k++;
????return k;
}
main( )
{
????char str1[80],*p=str1;
????scanf("%s",str1);
????printf("\nlength of the string is:%d",len(str1));
}
3.從鍵盤輸入一行文字,找出其中大寫字母、小字字母、數(shù)字及其他字符各有多少?
參考程序如下:
#include <stdio.h>
main( )
{
????int i,j,k,m;
????char *p,a[100];
????p=a;
????i=j=k=m=0;
????scanf("%s",p);
????for(;*p!='\0';p++)
????{
??????if(*p>=97&&*p<=122 ) ?i++;
??????else if(*p>=65&&*p<=90) ?j++;
??????else if(*p>=48&&*p<57) ?k++;
??????else ?m++;
????}
????printf("Lower case char:%d,Up case cahr:%d,number char:%d,other char:%d",i,j,k,m);
}
4.利用指針編寫程序,實(shí)現(xiàn)對數(shù)組進(jìn)行從小到大的排序(冒泡法)。
參考程序如下:
#include<stdio.h>
main( )
{
????int a[10];
????int *p,t,k,i,j;
????p=a;
????for(i=0;i<10;i++)
????{ scanf("%d",p);p++; }
p=a;
????for(i=0;i<9;i++)
{ ??k=i;
????????for(j=i+1;j<10;j++)
if(p[k]>p[j])k=j;
????????if(k!=i)
{ t=p[k];p[k]=p[i];p[i]=t;}
}
????for(i=0;i<10;i++)
printf("%5d",p[i]);
}
5.100人圍成一圈,從第1個人開始,每數(shù)到3的人出圈,問最后一個出圈的人是哪個?
參考程序如下:
#include ?<stdio.h>
void main( )
{
????int a[100],i,n,k,*p;
????p=a;
????for(i=0;i<100;i++)
??????*(p+i)=i+1;
????n=100;
????i=k=0;
????while(n>1)
????{
????????if(*(p+i)!=0) ?k++;
????????if(k==3) ?{*(p+i)=0;k=0;n--;}
????????i++;
????????if(i==100) ?i=0;
????}
????while(*p==0) ?p++;
????printf("the last is %d\n",*p);
}
實(shí)驗(yàn)12 ?結(jié)構(gòu)體、共用體與枚舉
任務(wù)1 ?結(jié)構(gòu)體、共用體與枚舉構(gòu)造數(shù)據(jù)類型的定義與使用
1.運(yùn)行下面程序,熟悉結(jié)構(gòu)體變量及指向結(jié)構(gòu)體變量指針的使用。
#include<stdio.h>
void main( )
{
????struct xs
????{ int num;
????????char name[20];
????????float score;
????}stu1={001,"Zhang san",90.5};
????struct xs stu2,*p;
????stu2=stu1;
????printf("%d %s %f\n",stu2.num,stu2.name,stu2.score);
????p=&stu2;
????printf("%d %s %f\n",p->num,p->name,p->score);
}
運(yùn)行結(jié)果為:
1 Zhang san 90.500000
1 Zhang san 90.500000
2.運(yùn)行下面程序,并分析指向結(jié)構(gòu)體數(shù)組的指針變量的使用,理解指針與結(jié)構(gòu)體數(shù)組的關(guān)系及結(jié)構(gòu)體成員的引用方式。
#include<stdio.h>
struct xs
{ ?int num;
???char name[20];
???char sex;
???int age;
}stu[3]={{10101,"Li Lin",'M',18},
?????????{10102,"Zhang Fun",'M',19},
?????????{10104,"Wang Min",'F',20}};
main( )
{
???struct xs *p;
???for(p=stu;p<stu+3;p++)
???printf("%d %s %c %d\n",p->num,p->name,p->sex,p->age);
}
運(yùn)行結(jié)果為:
10101 Li Lin M 18
10102 Zhang Fun M 19
10104 Wang Min F 20
思考:printf("%d%s%c%d\n",p->num,p->name,p->sex,p->age);如用結(jié)構(gòu)體數(shù)組元素的方式表示,該引用方式如何表示?
代碼修改如下:
#include<stdio.h>
struct xs
{ ?int num;
???char name[20];
???char sex;
???int age;
}stu[3]={{10101,"Li Lin",'M',18},
?????????{10102,"Zhang Fun",'M',19},
?????????{10104,"Wang Min",'F',20}};
main( )
{
???struct xs *p;
???int i;
???for(i=0;i<3;i++)
???printf("%d %s %c %d\n",stu[i].num,stu[i].name,stu[i].sex,stu[i].age);
}
3.以下程序通過定義學(xué)生結(jié)構(gòu)體數(shù)組,存儲了若干名學(xué)生的學(xué)號、姓名和3門課的成績。函數(shù)fun(?)的功能是將存放學(xué)生數(shù)據(jù)的結(jié)構(gòu)體數(shù)組,按照姓名的字典序(從小到大)排序。
請在程序的下劃線處填入正確的內(nèi)容并把下劃線刪除, 使程序得出正確的結(jié)果。
#include ?<stdio.h>
#include ?<string.h>
struct student {
????long ?sno;
????char ?name[10];
????float ?score[3];
};
void fun(struct student ?a[], int ?n)
{
????/**********found**********/
????struct student ?t;
????int ?i, j;
????/**********found**********/
????for (i=0; i<?n-1 ; i++)
???????for (j=i+1; j<n; j++)
????/**********found**********/
???????if (strcmp(a[i].name,a[j].name)?> 0)
???????{ ?t = a[i]; ??a[i] = a[j]; ?a[j] = t; ?}
}
void main( )
{ ?struct student ?s[4]={{10001,"ZhangSan", 95, 80, 88},{10002,"LiSi", 85, 70, 78},{10003,"CaoKai", 75, 60, 88}, {10004,"FangFang", 90, 82, 87}};
???int ?i, j;
???printf("\n\nThe original data :\n\n");
???for (j=0; j<4; j++)
???{ ?printf("\nNo: %ld ?Name: %-8s ?????Scores: ?",s[j].sno, s[j].name);
??????for (i=0; i<3; i++) ?printf("%6.2f ", s[j].score[i]);
??????printf("\n");
???}
???fun(s, 4);
???printf("\n\nThe data after sorting :\n\n");
???for (j=0; j<4; j++)
???{ ?printf("\nNo: %ld ?Name: %-8s ?????Scores: ?",s[j].sno, s[j].name);
??????for (i=0; i<3; i++) ?printf("%6.2f ", s[j].score[i]);
??????printf("\n");
???}
}
?
4.下面程序通過對學(xué)生學(xué)號、姓名、出生年月信息的輸入與輸出,分析并理解指向結(jié)構(gòu)體類型變量的正確使用方法,以及結(jié)構(gòu)體嵌套定義。
#include <stdio.h>
#include <stdlib.h> /*使用malloc( )需要包括該頭文件*/
struct data/*定義結(jié)構(gòu)體*/
{
????int day,month,year;
};
struct stu/*定義結(jié)構(gòu)體*/
{
????char name[20];
????long num;
????struct data birth; /*嵌套的結(jié)構(gòu)體類型成員*/
} ;
void main(?) /*定義main( ) 函數(shù)*/
{
????struct stu *xs; ???????????????????????/*定義結(jié)構(gòu)體類型指針*/
????xs=(struct stu *)malloc(sizeof(struct stu)); /*為指針變量分配安全的地址*/
????printf("Input name,number,year,month,day:\n");
????scanf("%s",xs->name); ???????????????/*輸入學(xué)生姓名、學(xué)號、出生年月日*/
????scanf("%ld", &xs->num);
????scanf("%d %d %d", &xs->birth.year,&xs->birth.month,&xs->birth.day);
????printf("\nOutput name,number,year,month,day\n" );/*打印輸出各成員項(xiàng)的值*/
????printf("%s%ld %5d// %d// %d\n",xs->name,xs->num,xs->birth.year,
????xs->birth.month,xs->birth.day);
}
運(yùn)行結(jié)果如下:
Input name,number,year,month,day:
HanTingyu 22 1985 11 1
HanTingyu22 1985//11//1(書上多空了很多的空格)
5.驗(yàn)證下面程序,熟悉共用體變量的使用。
#include ?<stdio.h>
union ab{
??????int a;
??????char b[2];
};
void main( )
{
??????union ab t;
??????t.a=0x1234;
??????printf("t.a=%x\nt.b[1]=%x\nt.b[0]=%x\n",t.a,t.b[1],t.b[0]);
}
程序輸出結(jié)果如下:
t.a=1234
t.b[1]=12
t.b[0]=34
任務(wù)2 ?鏈表的定義與使用
給定程序中,函數(shù)fun(?)的功能是將帶頭節(jié)點(diǎn)的單向鏈表結(jié)點(diǎn)數(shù)據(jù)域中的數(shù)據(jù)從小到大排序。即若原鏈表結(jié)點(diǎn)數(shù)據(jù)域從頭至尾的數(shù)據(jù)為10、4、2、8、6,排序后鏈表結(jié)點(diǎn)數(shù)據(jù)域從頭至尾的數(shù)據(jù)為2、4、6、8、10。
請在程序的下劃線處填入正確的內(nèi)容并將下劃線刪除, 使程序得出正確的結(jié)果。
#include ?<stdio.h>
#include ?<stdlib.h>
#define ???N ???6
typedef struct node {
????int ?data;
????struct node ?*next;
} NODE;
void fun(NODE ?*h)
{ ?NODE ?*p, *q; ???int ?t;
???/**********found**********/
???p =??h->next ?;
???while (p) {
???/**********found**********/
???????q = ?p->next ?;
???????while (q) {
???/**********found**********/
???????????if (p->data ?> ?q->data)
???????????{ ?t = p->data; ?p->data = q->data; ?q->data = t; ?}
???????????q = q->next;
???}
???p = p->next;
??}
}
NODE *creatlist(int ?a[])
{ ?NODE ?*h,*p,*q; ???????int ?i;
???h = (NODE *)malloc(sizeof(NODE));
???h->next = NULL;
???for(i=0; i<N; i++)
???{ ?q=(NODE *)malloc(sizeof(NODE));
??????q->data=a[i];
??????q->next = NULL;
??????if (h->next == NULL) ?h->next = p = q;
??????else ???{ ?p->next = q; ?p = q; ??}
???}
??????return ?h;
}
void outlist(NODE ?*h)
{ ?NODE ?*p;
???p = h->next;
???if (p==NULL) ?printf("The list is NULL!\n");
???else
???{ ?printf("\nHead ?");
??????do
??????{ ?printf("->%d", p->data); p=p->next; ?}
??????while(p!=NULL);
??????printf("->End\n");
???}
}
main( )
{ ?NODE ?*head;
???int ?a[N]= {0, 10, 4, 2, 8, 6 };
???head=creatlist(a);
???printf("\nThe original list:\n");
???outlist(head);
???fun(head);
???printf("\nThe list after sorting :\n");
???outlist(head);
}
?