{ if (i>1)
fun(i/2) ; printf(\ }
main() { int n;
scanf(\ fun(n) ; }
实验10 指针与结构体
一、实验目的
1)通过实验进一步掌握指针的概念,会定义和使用指针变量。 2)能正确使用数组的指针和指向数组的指针变量。 3)能正确使用字符串的指针和指向字符中的指针变量。 4 )掌握结构体类型变量的定义和使用。
二、实验内容
[题目1091:交换两数,由大到小输出]
下面程序,交换两数,使两数由大到小输出,请填空 #include \
void swap( int *p1, int *p2 ) { int temp; temp=*p1; *p1=*p2; *p2=temp; }
int main()
{ int a,b; int *pa,*pb;
scanf(\ pa=&a; pb=&b;
if(a
[题目1065:数组中的指针] 设有如下数组定义:
int a[3][4]={{1,3,5,7},{9,11,13,15},{17,19,21,23}};
计算下面各项的值 设数组a 的首地址为2000,一个int 类型数占四个字节)。注意:地址则输出地址,
变量则输出变量值;要求一行一个答案,不允许多余空行及空格。
1)a[2][1] 2)a[1] 3)a 4 )a+1
5)*a+1 6)*(a+1) 7)a[2]+1 8)*(a+1)+1 9)*(*(a+2)+2 )
参考程序:
#include \ main()
{ printf(\ printf(\ printf(\ printf(\ printf(\ printf(\ printf(\ printf(\ printf(\ }
[题目1092:函数实现求字符串长度]
下面程序以指针方式传递参数,由函数实现求字符串长度,请填空完成 #include \ #include \ int f(char *p)
{ return strlen(p); }
int main()
{ char s[80]; int i;
scanf(\ i=f(s);
printf(\ }
[题目1125:定义结构体类型]
要求定义一个名为student 的结构体类型,其包含如下成员: 1)字符数组name,最多可存放10 个字符; 2)字符变量sex,用于记录性别;
3)整数类型变量num,用于记录学号; 4 )float 类型变量score,用于记录成绩; 并使下列代码完整。 /*定义结构体类型*/
struct student
{ char name[20]; char sex; int num; float score; }
main() {
struct student stu;
scanf(\ stu.name);
scanf(\ &stu.sex); scanf(\ &stu.num); scanf(\ &stu.score); printf(\ printf(\ printf(\ printf(\ }
*实验11 链表
一、实验目的
1)理解链表的概念。
2)掌握结构体、指针在链表中的运用。
3)掌握链表的常用操作,包括创建、显示、添加等。
二、实验内容
[题目1098:链表结点的插入] 有结构体类型定义,
struct student
{ long num; /*学号*/ int score; /*成绩*/
struct student *next; /*指针*/ };
程序首先完成创建两个链表,要求补充完成按学号顺序插入链表结点的函数:
struct student *insert(struct student *head, struct student *stud) {
struct student *p0,*p1,*p2; p1=head; p0=stud;
if(head==NULL){head=p0;p0->next=NULL;} else
{while((p0->num>p1->num)&&(p1->next!=NULL)) { p2=p1;
p1=p1->next;}
if(p0->num<=p1->num) { if(head==p1)head=p0; else p2->next=p0; p0->next=p1; }
else {p1->next=p0;p0->next=NULL;} }
return(head);
}
[题目1099:链表的合并] 有结构体类型定义,
struct student
{ long num; /*学号*/ int score; /*成绩*/
struct student *next; /*指针*/ };
程序首先完成创建两个链表,要求补充完成实现将第二个链表合并到第一个链表未尾的函数。
struct student *merge(struct student *head, struct student *head2) {
struct student *p1; p1=head;
while(p1->next!=NULL)p1=p1->next; p1->next=head2; return(head);
}
[题目1104:链表的倒序] 有结构体类型定义,
struct student
{ long num; /*学号*/ int score; /*成绩*/
struct student *next; /*指针*/ };
程序首先完成程序创建一个链表,要求补充完成实现将链表中各结点变为倒序排列的函数。
struct student *reverse(struct student *head) {
struct student *p1,*p2,*p3; p2=head;p3=head->next; do
{ p1=p2;p2=p3;p3=p2->next;p2->next=p1; }
while(p3!=NULL); head->next=NULL; return(p2);
}
[题目1101:链表的排序] 有结构体类型定义,
struct student
{ long num; /*学号*/ int score; /*成绩*/
struct student *next; /*指针*/ };
程序首先完成程序创建一个链表,要求补充完成实现将链表中各结点按学号由小到大排序的函数。
struct student *sort(struct student *head) {
struct student *p1,*p2; p2=head;p1=head; p2=p2->next; p1->next=NULL; p1=p2;
while(p2->next!=NULL)