⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 c指针答案.txt

📁 关于C语言 指针 部分的习题及答案 供大家参考
💻 TXT
📖 第 1 页 / 共 3 页
字号:
void fali2(char course[5][10],int num[4],float score[4][5],float aver[4])

                                         /*找两门以上课程不及格的学生的函数*/

{ int i,j,k,label;

  printf("= = = = = = = =Student who is fail = = = = = = = = = = = =\n");   

  printf("  NO.");

  for(i=0;i<5;i++)

    printf("%10s",course[i]);

  printf("   average\n");

  for(i=0;i<4;i++)

  { label=0;

    for(j=0;j<5;j++)

      if((score[i][j])<60.0)  label++;

    if(label>=2)

        { printf("%5d",num[i]);

          for(k=0;k<5;k++)

            printf("%10.2f",score[i][k]);

          printf("%10.2f\n",aver[i]);

        }

  }

}

 

void good(char course[5][10],int num[4],float score[4][5],float aver[4])

                       /*找成绩优秀的学生(各门85分以上或平均90分以上)的函数*/

{ int i,j,k,n;

  printf("= = = = = = = =Student whose score is good= = = = = = = =\n");

  printf("  NO.");

  for(i=0;i<5;i++)

    printf("%10s",course[i]);

  printf("   average\n");

  for(i=0;i<4;i++)

  { n=0;

    for(j=0;j<5;j++)

      if((score[i][j])>85.0) n++;

    if((n==5)||(aver[i]>=90))

    { printf("%5d",num[i]);

      for(k=0;k<5;k++)

        printf("%10.2f",score[i][k]);

      printf("%10.2f\n",aver[i]);

    }

  }

}

 

运行情况如下:

Input course:                                    (输入课程名称)

English↙

Computer↙

Math↙

Physics↙

Chemistry↙

Input NO. and scoures:                           (输入学号和各门课成绩)

NO.,English,Computer,Math,Physics,Chemistry      (按此顺序输入)

101,34,56,88,99,89↙

102,77,88,99,67,78↙

103,99,90,87,86,89↙

104,78,89,99,56,77↙

 

 

course 1: English ,average score:  72.00.        (第一门课英语的平均成绩)

 

 

= = = = = = = =Student who is fail= = = = = = = = = = = =   (有两门课不及格者)

  NO.   English  Computer      Math   Physics Chemistry    average

  101     34.00     56.00     88.00     99.00     89.00     73.20 

 

 

= = = = = = = =Student whose score is good= = = = = = = =    (成绩优良者)

  NO.   English  Computer      Math   Physics Chemistry    average

  103     99.00     90.00     87.00     86.00     89.00     90.20

 

    说明:程序中num是存放4个学生学号的一维数组,course是存放5门课名称的二维字符数组,score是存放4个学生5门课成绩的二维数组,aver是存放每个学生平均成绩的数组,pnum是指向num数组的指针变量,pcou是指向course数组的指针变量,psco是指向score数组的指针变量,pave 是指向aver数组的指针变量。见图lO.5。

 


 

    函数的形参调用用数组,调用函数时的实参用指针变量。形参也可以不用数组而用指针变量,请读者自己分析。

 

返  回

 

10.16 输入一个字符串,内有数字和非数字字符,如:
            a123x456ㄩ17960?302tab5876
将其中连续的数字作为一个整数,依次存放到一数组a中。例如:123放在a[0]中,456放在a[1]中……统计共有多少个整数,并输出这些数。

解:程序如下:(xt10-16.c)

#include <stdio.h>

main()

{ char str[50], *pstr;

  int i,j,k,m,e10,digit,ndigit,a[10],*pa;

  printf("Input a string: \n");

  gets(str);

  printf("\n");

  pstr=&str[0];                             /* 字符指针pstr置于数组str首地址*/

  pa=&a[0];                                 /* 指针pa置于数组a首地址*/

  ndigit=0;                                 /* ndigit代表有多少个整数*/

  i=0;                                      /* 代表字符串中字符的位置*/

  j=0;                                      /* 代表连续数字的位数*/

  while(*(pstr+i)!='\0')

  { if((*(pstr+i)>='0')&&(*(pstr+i)<='9')) j++;

    else

    { if(j>0)

      { digit=*(pstr+i-1)-48;                 /*将个数位赋予digit */

        k=1;

        while(k<j)              /*将含有两位以上数的其他位的数值累计于digit */

        { e10=1;

          for(m=1;m<=k;m++)

          e10=e10*10;                          /*e10代表该位数所应乘的因子*/

          digit=digit+(*(pstr+i-1-k)-48)*e10;  /*将该位数的数值累加于digit*/

          k++;                                 /*位数k自增*/

        }

        *pa=digit;                             /*将数值赋予数组a*/

        ndigit++;

        pa++;                                  /*指针pa指向a数组下一元素*/

        j=0;

      }

    }

    i++;

  }

  if(j>0)                                    /*以数字结尾字符串的最后一个数据*/

  { digit=*(pstr+i-1)-48;                    /*将个数位赋予digit*/

    k=1;

    while(k<j)                   /*将含有两位以上数的其他位的数值累计于digit*/

    { e10=1; 

      for(m=1;m<=k;m++)

        e10=e10*10;                         /*e10代表该位数所应乘的因子*/

      digit=digit+(*(pstr+i-1-k)-48)*e10;   /*将该位数的数值累加于digit*/

      k++;                                  /*位数k自增*/

    }

    *pa=digit;                              /*将数值赋予数组a*/

    ndigit++;

    j=0;

  }

  printf("There are %d numbers in this line. They are:\n",ndigit);

  j=0;

  pa=&a[0];

  for(j=0;j<ndigit;j++)                      /*打印数据*/

    printf("%d ",*(pa+j));

  printf("\n");

}

 

运行情况如下:

Input a string: 

a123x456 17960? 302tab5876↙

There are 6 numbers in this line. They are:

123 456 1790 302 5876

 

返  回

 

10.17写一函数,实现两个字符串的比较。即自己写一个strcmp函数,函数原型为:
                 int stremp(char *p1,char *p2)
设p1指向字符串s1,p2指向字符串s2。要求:当s1=s2时,返回值为0。当s1不等于s2时,返回它们二者的第一个不同字符的ASCII码差值(如“BOY”与“BAD”,第二字母不同,“O”与“A”之差为79-65=14);如果s1>s2,则输出正值;如果s1<s2,则输出负值。

解:程序如下:(xt10-17.c)

#include <stdio.h>

main()

{ int strcmp(char *p1,char *p2);

  int m;

  char str1[20],str2[20],*p1,*p2;

  printf("Input two strings:\n");

  scanf("%s",str1);

  scanf("%s",str2);

  p1=&str1[0];

  p2=&str2[0];

  m=strcmp(p1,p2);

  printf("result: %d\n",m);

}

 

int strcmp(char *p1,char *p2)            /*两个字符串比较的函数*/

{ int i;

  i=0;

  while(*(p1+i)==*(p2+i))

    if(*(p1+i++)=='\0') return(0);         /*相等时返回结果0*/

  return(*(p1+i)-*(p2+i));     /*不等时返回结果为第一个不等字符ASCII码的差值*/

}

 

运行情况如下:

① Input two strings:

   CHINA↙

   Chen↙

   Result: -32

② Input two strings:

   hello! ↙

   Hello! ↙

   Result: 0

③ Input two stings:

   dog↙

   cat↙

   result: 1

 

返  回

 

10.18编一个程序,打入月份号,输出该月的英文月名。例如,输入“3”,则输出“March”,要求用指针数组处理。

解:程序如下:(xt10-18.c)

#include <stdio.h>

main()

{ char *month_name[13]={"illegal month","January","February","March","April",

   "May","June","July","August","September","October","November","December"};

  int n;

  printf("Input month: ");

  scanf("%d",&n);

  if((n<=12)&&(n>=1))

    printf("It is %s.\n",*(month_name+n));

  else

    printf("It is wrong.\n");

}

 

运行结果:

① Input month: 2↙

   It is February.

② Input month: 8↙

   It is August.

③ Input month: 13↙

   It is wrong.

 

返  回

 

10.19编写一个函数alloc(n),用来在内存区新开辟一个连续的空间(n个字节)。此函数的返回值是一个指针,指向新开辟的连续的空间的起始地址。再写一个函数free(p),将地址p开始的各单元释放(不能再被程序使用,除非再度开辟)。
    提示,先在内存规定出一片相当大的连续空间(例如1000个字节)。然后开辟与释放都在此空间内进行。假设指针变量p原已指向未用空间的开头,调用alloc(n)后,开辟了n个字节可供程序使用(例如,可以赋值到这些单元中)。现在需要使p的值变成p+n,表示空白未用区从p+n地址开始,同时要将新开辟区的起始位置(p)作为函数值返回,以表示可以利用从此点开始的单元。如果更新开辟的区太大(n大),超过了预设的空间(1000字符),则alloc(n)函数返回指针NULL,表示开辟失败。
    alloc(n)应返回一个指向字符数据的指针(因为开辟的区间是以字节为单位被利用的)。

 

解:程序如下:(xt10-19.c)

#define NULL 0                /*当开辟失败时返回标志*/

#define ALLOCSIZE 1000        /*可以开辟的最大的空间*/

char allocbuf[ALLOCSIZE];     /*开辟一个字符数组,作为存储区*/

char *allocp=allobuf;         /*指针指向存储区的起端*/

char *alloc(int n)            /*开辟存储区函数,开辟存储区后返回指针*/

{ if(allocp+n<=allocbuf+ALLOCSIZE)

  { sallocp+=n;

    return(allocp-n);         /*返回一个指针,它指向存区的开始位置*/

  }

  else

    return(NULL);             /*当存区不够分配时,返回一个空指针*/

}

 

void free(char *p)            /*释放存储区函数*/

{ if(p>=allocbuf && p>alloebuf+ALLOCSIZE)

    allocp=p;

}

 

    说明:定义一个全局指针变量a11ocp,它指向指定的存储区中下一个可用的元素。开始时,allocp指向此存储区allocbuf的开头,当调用alloc(n)函数后,allocp指向a11ocbuf中的第n个元素(见图l0.6中的allocp')。

 


 

 

    如果调用时用以下语句:

    pt=alloc(n);

则pt的值为刚才所开辟的空间的首地址(allocp'-n)。

    在调用free函数时,如果写出以下调用语句:

    free(pt);

则把allocp的值改成pt,即使得allocp指向刚才开辟空间的开头,恢复allocp的原值,就相当于释放此段空间,使这段空间可以做其他用途。

 

返  回

 

10.20用指向指针的指针的方法对5个字符串排序并输出。

解:程序如下:(xt10-20.c)

#include <stdio.h>

#define LINEMAX 20                    /*定义字符串的最大长度*/

main()

{ void sort(char **p);

  int i;

  char **p,*pstr[5],str[5][LINEMAX];

  for(i=0;i<5;i++)

    pstr[i]=str[i];     /*将第i个字符串的首地址赋予指针数组pstr的第i个元素*/

  printf("Input 5 strings:\n");

  for(i=0;i<5;i++)

    scanf("%s",pstr[i]);

  p=pstr;

  sort(p);

  printf("strings sorted:\n");

  for(i=0;i<5;i++)

    printf("%s\n",pstr[i]);

}

 

void sort(char **p)                   /*冒泡法对5个字符串排序的函数*/

{ int i,j;

  char *temp; 

  for(i=0;i<5;i++)

  { for(j=i+1;j<5;j++)

    { if(strcmp(*(p+i),*(p+j))>0)     /*比较后交换字符串地址*/

      { temp=*(p+i);

        *(p+i)=*(p+j);

        *(p+j)=temp;

      }

    }

  }

}

 

运行情况如下:

Input 5 strings:

China↙

America↙

India↙

Philippines↙

Canada↙

strings sorted:

America

Canada

China

India

Philippines

 

返  回

 

10.21用指向指针的指针的方法对n个整数排序并输入。要求将排序单独写成一个函数。n和整数在主函数中输入。最后在主函数中输出。

解:程序如下:(xt10-21.c)

#include <stdio.h>

main()

{ void sort(int **p,int n);

  int i,n,data[10],**p,*pstr[10];

  printf("Input n: ");

  scanf("%d",&n);

  for(i=0;i<n;i++)

    pstr[i]=&data[i];        /*将第i个整数的地址赋予指针数组pstr的第i个元素*/

  printf("Input %d integer numbers:\n",n);

  for(i=0;i<n;i++)

    scanf("%d",pstr[i]);

  p=pstr;

  sort(p,n);

  printf("Now, the sequence is:\n");

  for(i=0;i<n;i++)

    printf("%d ",*pstr[i]);

  printf("\n");

}

 

void sort(int **p,int n)

{ int i,j,*temp;

  for(i=0;i<n-1;i++)

  { for(j=i+1;j<n;j++)

    { if(**(p+i)>**(p+j))         /*比较后交换整数的地址*/

      { temp=*(p+i);

        *(p+i)=*(p+j);

        *(p+j)=temp;

      }

    }

  }

}

 

运行情况如下;

Input n:7↙

Input 7 integer numbers:

34 98 56 12 22 65 1↙

Now, the sequence is: 

1 12 22 34 56 65 98

    说明:data数组用来存放n个整数;pstr是指针数组,每一个元素指向data数组中的一个元素;p是指向指针的指针。请参考图10.7。图10.7(a)表示的是排序前的情况。图10.7(b)表示的是排序后的情况。在图中可以看到,data数组中数的次序没有变化,而pstr指针数组中的各元素的值(也就是它们的指向)改变了。


 

 


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -