[关闭]
@Arbalest-Laevatain 2018-07-04T04:06:45.000000Z 字数 4285 阅读 809

Anyview 第十、十一章

C语言


第十章

10.011 请编写一个函数func(char s[], char t[], int n),

由数组s中长度为n的字符序列构造其逆序列,并存储在数组t中。
例如,由给定字符序列s=“are”求得逆序列t=“era”;由s=“time”
求得t=“emit”。
void func(char s[], char t[], int n)
/* 数组s的前n个元素存放给定的字符序列,
数组t的前n个元素存放s的逆序列。
注意:数组的下标从0开始。
*/
{
for (int i=0;i {
int j=n;
t[j-1-i]=s[i];
}
}

10.015 对长度为n的字符串s1,除首、尾字符外,将其余字符

按ASCII码降序排列,组合成一个新的字符串s2。

要求实现函数:

  1. void func(char *s1, char *s2, int n);
  2. /* s1为字符串的起始地址,
  3. s2为新字符串的起始地址,
  4. n为字符串的长度。
  5. 要求:s1串不能发生改变,
  6. s2串存放新的字符串。
  7. */
  8. {
  9. int i,j;
  10. strcpy(s2,s1);
  11. for (i=1;i<n-1;i++)
  12. {
  13. for (j=2;j<n-i;j++)
  14. {
  15. if (s2[j]>s2[j-1])
  16. {
  17. char t=s2[j];
  18. s2[j]=s2[j-1];
  19. s2[j-1]=t;
  20. }
  21. }
  22. }
  23. }

10.016 对字符串s1,除首、尾字符外,将其余字符

按ASCII码降序排列,组合成一个新的字符串s2。

要求实现函数:

  1. void func(char *s1, char *s2);
  2. /* s1为字符串的起始地址,
  3. s2为新字符串的起始地址,
  4. 注意:字符串尾字符之后跟随着一个结束符‘\0’,
  5. 即ASCII码为0的字符,结束符不属于字符串。
  6. 要求:s1串不能发生改变,
  7. s2串存放新的字符串。
  8. */
  9. {
  10. int i,j;
  11. int n=strlen(s1);
  12. strcpy(s2,s1);
  13. for (i=1;i<n-1;i++)
  14. {
  15. for (j=2;j<n-i;j++)
  16. {
  17. if (s2[j]>s2[j-1])
  18. {
  19. char t=s2[j];
  20. s2[j]=s2[j-1];
  21. s2[j-1]=t;
  22. }
  23. }
  24. }

/******

10.018 以字符串s第m(>=0)个字符开始的所有字符,

按升序的次序构成字符串t。
******/

  1. void substr(char *s, int m, char *t)
  2. /* s为字符串的起始地址,
  3. m>=0,
  4. t为新字符串的起始地址,
  5. 注意:字符串尾字符之后跟随着一个结束符‘\0’,
  6. 即ASCII码为0的字符,结束符不属于字符串。
  7. 要求:s串不能发生改变,
  8. t串存放新的字符串。
  9. */
  10. {
  11. char *p=s+m;
  12. while (*p!='\0')
  13. {
  14. *t=*p;
  15. p++;
  16. }
  17. }

10.033 编写函数,计算年份year中第yearday天相应的月和日。例如,

调用函数month_day(2000,61,&m,&d)之后,m=3,d=1,即2000年的第
61天是3月1日。

要求实现函数:
int month_day(int year, int yearday, int *pmonth, int pday)
/
year是年,
yearday是天数,
若year和yearday合理,
则*pmonth和*pday是计算得出的月和日,函数返回1;
否则,函数返回0。
*/

  1. int month_day(int year, int yearday, int *pmonth, int *pday)
  2. /* year是年,
  3. yearday是天数,
  4. 若year和yearday合理,
  5. 则*pmonth和*pday是计算得出的月和日,函数返回1;
  6. 否则,函数返回0。
  7. */
  8. {
  9. int month[12]={31,28,31,30,31,30,31,31,30,31,30,31};
  10. int i,z,y,flag=0;
  11. int *p1,*p2;
  12. if (year>0 && yearday>0)
  13. {
  14. if ((year%4==0 && year %100 !=0) || (year%400==0))
  15. {
  16. flag=1;
  17. }
  18. if (flag)
  19. {
  20. month[1]+=1;
  21. if (yearday>366)
  22. return 0;
  23. }
  24. else
  25. {
  26. if (yearday>365)
  27. return 0;
  28. }
  29. i=0,z=0;
  30. y=yearday;
  31. while (y>0)
  32. {
  33. y-=month[i];
  34. i++;
  35. z++;
  36. }
  37. y+=month[i-1];
  38. if (y==0)
  39. {
  40. z-=1;
  41. y=month[z-1];
  42. }
  43. *pmonth=z;
  44. *pday=y;
  45. return 1;
  46. }
  47. return 0;
  48. }

10.044 请编写一个函数func,通过略去非数字字符,将字符串s转换为

一个整数(不得调用C语言提供的将字符串转换为整数的函数)。

要求实现函数:

  1. long func(char *s)
  2. /* s是一个数字字符串的地址,
  3. 函数返回值为由s含有的数字字符转换得到的数(包含正负数情况)
  4. */
  5. {
  6. long sum=0;
  7. int e=0,i,j=1;
  8. char *t=s;
  9. while (*s!='\0')
  10. {
  11. if (*s>='0' && *s<='9')
  12. {
  13. if (e>0)
  14. j=10;
  15. else
  16. j=1;
  17. sum=sum*j+*s-'0';
  18. e++;
  19. }
  20. s++;
  21. }
  22. if (*t=='-')
  23. sum=-sum;
  24. return sum;
  25. }

10.105 请编写一个函数findmax(int []s, int n),返回数组s中n(>0)个整数

中的最大值。注意:要求在函数中采用指针(而不是下标)来处理数组元素。int findmax(int s[], int n)

  1. int findmax(int s[], int n)
  2. /* 返回s中n(>0)个整数的最大值。
  3. 注意:要求在函数中采用指针(而不是下标)来处理数组元素。
  4. */
  5. {
  6. }

第十一章

11.033 链表L存储了多个人的信息。写一函数,求这些人中年龄最大

(即出生日期最小)者的名字。
结构体类型定义如下:

  1. struct date{int year; int month; int day;}; //日期结构体类型
  2. struct studentNode //链表结点的结构体类型
  3. { char name[10]; //人名
  4. struct date birth; //出生日期
  5. struct studentNode *next
  6. };

*/

  1. char *oldest(struct studentNode *L)
  2. /* 若L是空表,则返回空指针null
  3. 否则返回表中年龄最大者的名字
  4. */
  5. {
  6. struct studentNode *p;
  7. struct studentNode *max;
  8. char *n;
  9. max=L;
  10. if (NULL==L) return NULL;
  11. p=L->next;
  12. while (p)
  13. {
  14. if (max->birth.year > p->birth.year)
  15. max=p;
  16. else if(max->birth.year == p->birth.year)
  17. {
  18. if (max->birth.month > p->birth.month)
  19. max= p;
  20. else if(max->birth.month == p->birth.month)
  21. {
  22. if (max->birth.day > p->birth.day)
  23. max= p;
  24. }
  25. }
  26. p=p->next;
  27. }
  28. return max->name;
  29. }

11.073 课程链表结点的结构体类型定义如下:

  1. struct courseNode //课程链表结点的结构体类型
  2. { int cID; //课程号,取值0~99
  3. char name[10]; //课程名
  4. float credit; //学分,取值0~5
  5. int semester; //学期,取值1~8
  6. struct courseNode *next;
  7. };
  1. 结构体链表Lc存储了各学期多门课程的信息。写一函数,
  2. 求学期s的总学分。
  3. */
  4. float creditSum(struct courseNode *Lc, int s)
  5. /* 若Lc是空表,则返回0;
  6. 否则返回学期s的总学分
  7. */
  8. {
  9. struct courseNode *p;
  10. float sum=0.0;
  11. if (NULL==Lc) return 0.0;
  12. p=Lc;
  13. while (p)
  14. {
  15. if (p->semester==s)
  16. sum+=p->credit;
  17. p=p->next;
  18. }
  19. return sum;
  20. }

11.173 课程链表结点的结构体类型定义如下:

  1. struct courseNode //课程链表结点的结构体类型
  2. { int cID; //课程号,取值0~99
  3. char name[10]; //课程名
  4. float credit; //学分,取值0~5
  5. int semester; //学期,取值1~8
  6. struct courseNode *next;
  7. };
  8. ```
  9. 结构体链表Lc存储了多门课程的信息。写一函数,将课程号
  10. c的课程的学分修改为t
  11. */
  1. struct courseNode *creditChange(struct courseNode *Lc, int c, float t)
  2. /* 若课程c不存在,则修改不成功,返回null;
  3. 否则修改该课程的学分为t,返回指向该课程结点的指针。
  4. */
  5. {
  6. struct courseNode *p;
  7. int flag=0;
  8. if (NULL==Lc) return NULL;
  9. if (c==Lc->cID)
  10. {
  11. p=Lc;
  12. p->credit=t;
  13. return p;
  14. }
  15. p=Lc->next;
  16. while (Lc->next)
  17. {
  18. if (Lc->next->cID==c)
  19. {
  20. p=Lc->next;
  21. p->credit=t;
  22. flag++;
  23. }
  24. Lc=Lc->next;
  25. }
  26. if (flag)
  27. return p;
  28. else
  29. return NULL;
  30. }

11.183 课程链表结点的结构体类型定义如下:

  1. struct courseNode //课程链表结点的结构体类型
  2. { int cID; //课程号,取值0~99
  3. char name[10]; //课程名
  4. float credit; //学分,取值0~5
  5. int semester; //学期,取值1~8
  6. struct courseNode *next;
  7. };

结构体链表Lc存储了多门课程的信息。写一函数,将课程号为c的课程结点删除。

要求实现下列函数:

  1. struct courseNode *deleteCourse(struct courseNode **Lc, int c)
  2. /* 若在链表Lc中课程c不存在,则删除不成功,返回null;
  3. 否则从链表Lc中删除该课程结点,并返回指向该课程结点的指针。
  4. */
  5. {
  6. struct courseNode*p;
  7. p=*Lc;
  8. while (p!=NULL)
  9. {
  10. if (p->next->cID==c)
  11. {
  12. struct courseNode*t=p->next;
  13. p->next=p->next->next;
  14. return t;
  15. }
  16. if (p->next==NULL)
  17. {
  18. if (p->cID==c)
  19. {
  20. Lc[0]=NULL;
  21. return p;
  22. }
  23. }
  24. p=p->next;
  25. }
  26. return NULL;
  27. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注