C 语言实训教程
----指针专项练习
1、熟练掌握指针的概念及使用。
2、掌握数组指针、指针数组、指向结构体的指针、指向函数的指针的使用。
3、掌握命令行参数的使用。
4、掌握链表的使用。
1、从命令行输入两个数,按由小到大输出。(主要考察命令行参数的使用) 2、编写一个函数 swap(int *a,int *b),用于交换两个数
3、统计一字符串在另一个字符串中出现的次数。
4、写一个函数,求一个字符串的长度,在 main 函数中输入字符串,并输出其长度。
(不采用库函数 strlen)
5、写一个函数实现字符串的复制(不采用库函数strcpy)。
6、编一个函数 fun(int *a,int n,int *odd,int *even),函数的功能是分别求出数组中所有奇数之和以及所有偶数之和。形参 n 给了数组中数据的个数:利用指针 odd 返回奇数之和,利用指针 even 返回偶数之和。例如:数组中的值依次为:1,8,2,3,11,6;则利用指针 odd 返回奇数之和 24;利用指针 even 返回偶数之和 8。
7、有 n 个人围成一圈,顺序排号。 从第一个人开始报数(从 1 到 3 报数), 凡报到 3 的人退出圈子,问最后留下的是原来第几号的那位(用指针完成)。
8、编写一个函数,输入 n 为偶数时,调用函数求 1/2+1/4+…+1/n,当输入 n 为奇数时,调用函数 1/1+1/3+…+1/n(利用指向函数的指针)
9、单链表的创建、插入、删除、排序及遍历,根据已有代码,在“******”处填入代码,完成程序。
#include<stdlib.h> #include<stdio.h> struct roommate
{
char num[11]; char name[20]; int age;
char birthplace[20]; struct roommate *next;
};
struct roommate *head,*cthis,*cnew; void init_record(void)
{
*****************
}
void ins_record(void)
{
******************
}
void del_record(void)
{
******************
}
void listall(void)
{
******************
}
void main()
{
char ch; int flag=1;
head=NULL; while(flag)
{
printf("\ntype 'a' to append new record"); printf("type 'i' to insert new record"); printf("type 'd' to delete a record"); printf("type 'l' to list all records"); ch=getchar();getchar();
switch(ch)
{
case 'e':init_record();break; case 'i':ins_record();break; case 'd':del_record();break; case 'l':listall();break; default:flag=0;
}
}
}
10、对上题建立的单链表增加按年龄排序的功能。