int FindMax(int num[], int n, int *pMaxPos)?
int FindMin(int num[], int n, int *pMinPos)? main() {
int num[10], maxValue, maxPos, minValue, minPos, i?
printf(\"Input 10 numbers:\\n \")? for (i=0? i<10? i++) {
scanf(\"%d\
&num[i])? /* 输入 10 个数*/ }
maxValue = FindMax(num, 10, &maxPos)? /* 找最大值及其所在下标位置 */
minValue = FindMin(num, 10, &minPos)? /* 找最小值及其所在下标位置 */
printf(\"Max=%d, Position=%d, Min=%d, Position=%d\\n\
maxValue, maxPos, minValue, minPos)? }
/*函数功能:求 n个数中的最大值及其所在下标位置
函数入口参数:整型数组 num,存储 n个整数,整型变量 n,表示数组元素个数 函数出口参数:整型指针变量 pMaxPos,指向的地址单元存储最大值在数组中的下标位置
函数返回值: 最大值*/
int FindMax(int num[], int n, int *pMaxPos) {
int i, max?
max =
num[0]? /*假设 num[0]为最大*/ *pMaxPos =
0? /*假设最大值在数组中的下标位置为 0 */
for (i = 1? i < n? i++) {
if (num[i] > max) {
max = num[i]? *pMaxPos = i? } }
return max ? }
/*函数功能: 求 n个数中的最小值及其所在下标位置
函数入口参数: 整型数组 num,存储 n个整数,整型变量 n,表示数组元素个数 函数出口参数: 整型指针变量 pMinPos,指向的地址单元存储最小值在数组中的下标位置
函数返回值: 最小值*/
int FindMin(int num[], int n, int *pMinPos) {
int i, min?
min = num[0]? /*假设 num[0]为最小*/ *pMinPos =
0? /*假设最小值在数组中的下标位置为 0 */
for (i = 1?i < 10?i++) {
if (num[i] < min) {
min = num[i]? *pMinPos = i? } }
return min ? }
3. 将 5 个字符串从小到大排序后输出。 #include void main(void) { int i?char *pcolor[5]={ \"red\ \"blue\\"green\
void fsort(char *color[ ], int n)?
fsort( pcolor, 5 )? for(i = 0? i < 5? i++) printf(\"%s \pcolor[i])? } void fsort(char *color[ ], int n) { int k, j? char *temp?
for(k = 1? k < n? k++) for(j = 0? j < n?k? j++)
if(strcmp(color[j],color[j+1])>0) {
temp = color[j]?
color[j] = color[j+1]?
color[j+1] temp?
} }
4. 编写一个能对任意m×n阶矩阵进行转置运算的函数 Transpose()。 #include #define ROW 3 #define COL 4void Transpose(int (*a)[COL], int (*at)[ROW], int row, int col)?
void InputMatrix(int (*s)[COL], int row, int col)?
void PrintMatrix(int (*s)[ROW], int row, int col)? main() {
int
s[ROW][COL]? /*s 代表原矩阵*/ int
st[COL][ROW]? /*st 代表转置后的矩阵*/
printf(\"Please enter matrix:\\n\")?
InputMatrix(s, ROW, COL)? /*输入原矩阵,s 指向矩阵 s的第 0行,是行指针*/
Transpose(s, st, ROW, COL)?/*对矩阵 s 进行转置,结果存放于 st 中*/ printf(\"The transposed matrix is:\\n\")?
PrintMatrix(st, COL, ROW)? /*输出转置矩阵,*st 指向 st 的第 0 行,是行指针*/ }
/* 函数功能:对任意row行 col 列的矩阵转置
函数入口参数:指向一维整型数组的指针变量a,指向单元存放转置前的矩阵元素 整型变量 row,矩阵的行数即二维整型数组的行数
整型变量 col,矩阵的列数即二维整型数组的列数 函数出口参数:指向一维整型数组的指针变量at,指向单元存放转置后的矩阵元素 函数返回值: 无*/
void Transpose(int (*a)[COL], int (*at)[ROW], int row, int col) {
int i, j?
for (i=0? ifor (j=0? j*(*(at+j)+i) = *(*(a+i)+j)? } } }void InputMatrix(int (*s)[COL], int row, int col) /*输入矩阵元素*/ {
int i, j?
for (i=0? ifor (j=0? jscanf(\"%d\*(s+i)+j)? /*这里*(s+i)+j 等价于&s[i][j]*/ } }} void PrintMatrix(int (*s)[ROW], int row, int col) /*输入矩阵元素*/ {
int i, j?
for (i=0? ifor (j=0? jprintf(\"%d\\这里*(*(s+i)+j)等价于 s[i][j]*/ }printf(\" \\n\")? } }
第九章 习题答案 一、选择题
1?5 B D D A C 6?10 B C A D C 二、填空题
1. struct DATA d={2006,10,1}? 2. sizeof(struct node) 3. person[i].sex 4. 13431
5. (1)struct node* (2)*s (3)p
三、编程题
1. 定义一个能正常反映教师情况的结构体 teacher,包含教师姓名、性别、年龄、所在部门和
薪水; 定义一个能存放两人数据的结构体数组 tea, 并用如下数据初始化:{{ “Mary “, ‘W’,40,
‘Computer’ , 1234 },{“Andy“,
‘M’,55, ‘English’ , 1834}};要求:分别用结构体数组 tea 和指针
p输出各位教师的信息,写出完整定义、初始化、输出过程。
#include struct teacher { char name[8]? char sex? int age?char department[20]? float salary? } ?
struct teacher tea[2]= {{\"Mary \'W',40, \"Computer\" , 1234 }, {\"Andy \'M',55, \"English\" , 1834}} ? main() { int i?
struct teacher *p? for( i=0?i<2?i++)
printf(\"%s,\%c,\%d,\%s,\%f\
tea[i].name,tea[i].sex,tea[i].age,tea[i].department,tea[i].salary)?
for(p=tea?pdepartment, p?>salary)? }2. 定义一个结构体变量(包括年、月、日)。计算该日在本年中是第几天,注意闰年问题。 #include struct {int year? int month? int day? }date? main() {int days?printf(“Input year,month,day:”)?
scanf(“%d,%D,%d”,&date.year,&date.month,&date.day)? switch(date.month) {case 1:
days=date.day? break? case 2:
days=date.day+31? break? case 3:
days=date.day+59? break? case 4:
days=date.day+90? break? case 5:
days=date.day+120? break? case 6: days date.day+31? break? case 7:
days=date.day+181? break?
case 8:
days=date.day+212? break?
case 9: days date.day+243? break? case 10: days
date.day+273? break? case11:
days=date.day+304? break?
case 12: days
date.day+334? break? }
if((date.year%4
0&&date.year%100!=0||date.year%400 0)&&date.month> 3) days+=1?
printf(“\\n%d/%d is the %dth day in%d.”,date.month,date.day,days,date.year)? }
3.构建简单的手机通讯录,手机通讯录包括信息 (姓名、年龄、联系电话),要求实现新建、 查询功能。假设通信录最多容纳 50 名联系人信息。
#include #include /*手机通讯录结构定义*/struct friends_list{
char name[10]? /* 姓名 */
int age? /* 年龄 */
char telephone[13]? /* 联系电话 */ }?
int Count = 0? /* 定义全局变量 Count,记录当前联系人总数 */ void new_friend(struct friends_list friends[ ] )?
void search_friend(struct friends_list friends[ ], char *name)? int main(void) {
int choice? char name[10]? struct friends_list
friends[50]? /* 包含 50 个人的通讯录 */ do{
printf(\"手机通讯录功能选项:1:新建 2:查询 0:退出\\n\")?
printf(\"请选择功能:\")? scanf(\"%d\ switch(choice){ case 1:
new_friend(friends)?
break? case 2:
printf(\"请输入要查找的联系人名:\")?
scanf(\"%s\name)?
search_friend(friends, name)?
break? case 0: break? }
}while(choice != 0)? printf(\"谢谢使用通讯录功能!\\n\")? return 0? }
/*新建联系人*/
void new_friend(struct friends_list friends[ ]) {
struct friends_list f? if(Count 50){
printf(\"通讯录已满!\\n\")? return? }
printf(\"请输入新联系人的姓名:\")?
scanf(\"%s\ printf(\"请输入新联系人的年龄:\")?
scanf(\"%d\
printf(\"请输入新联系人的联系电话:\")?
scanf(\"%s\ friends[Count] = f? Count++? }
/*查询联系人*/
void search_friend(struct friends_list friends[ ], char *name) {
int i, flag = 0? if(Count 0){
printf(\"通讯录是空的!\\n\")? return? }
for(i = 0? i < Count? i++) if(strcmp(name,friends[i].name) 0){ /* 找到联系人*/ flag=1? break? }
if(flag){
printf(\"姓名: %s\\friends[i].name)?
printf(\"年龄: %d\\friends[i].age)?
printf(\"电话: %s\\n\
friends[i].telephone)? } else
printf(\"无此联系人!\")? }
4. 建立一个教师链表,每个结点包括学号(no),姓名(name[8]),工资(wage),写出动态创建
函数 creat 和输出函数 print。 #include #include #define NULL 0#define LEN sizeof(struct teacher)
struct teacher {int no?
char name[8]? float wage?
struct teacher * next? }? int n?
struct teacher *creat(void) { struct teacher *head?
struct teacher *p1,*p2? n=0?
p1=p2= (struct teacher *)malloc(LEN)?
scanf(“%d%s%f”,&p1?>no,p1?>name, &p1?>wage)? head=NULL?
while(p1?>no!=0) { n=n+1?
if(n 1) head p1? else p2?>next p1? p2=p1?
p1=( struct teacher *)malloc(LEN)?
scanf(“%d%s%f”,&p1?>no,p1?>name, &p1?>wage)? }
p2?>next=NULL? return(head)? }
void print(struct teacher *head
)
{ struct teacher *p? p=head?
if (head!=NULL) do{
printf(“%d\%s\%f\\n”, p?>no, p?>name, p?>wage)? p=p?>next?
} while(p!=NULL)? }
5.在上一题基础上,假如已经按学号升序排列,写出插入一个新教师的结点的函数 insert。
struct teacher insert(struct teacher *head,struct teacher *tea) { struct teacher *p0,*p1,*p2? p1=head? p0=tea?
if(head=NULL)
{head=p0? p0?>next=NULL?} else
while((p0?>no>p1?>no)&&(p1?>next!=NULL))
{ p2=p1?
p1=p1?>next?} if(p0?>no< p1?>no) { if (head p1) head=p0? else
{ p2?>next p0? p0?>next p1? } else
{ p1?>next p0?p0?>next=NULL?} n=n+1?
return(head)? }