分类: C/C++
2007-02-27 20:49:46
10.12用指针数组处理上一题目,字符串不等长。
解:程序如下:(xt10-12.c)
#include
#include
main()
{ void sort(char *p[]);
int i;
char *p[10],str[10][20];
for(i=0;i<10;i++)
p[i]=str[i]; /*将第i个字符串的首地址赋予指针数组p的第i个元素*/
printf("Input 10 strings:\n");
for(i=0;i<10;i++)
scanf("%s",p[i]);
sort(p);
printf("Now, the sequence is:\n");
for(i=0;i<10;i++)
printf("%s\n",p[i]);
}
void sort(char *p[])
{ int i,j;
char *temp;
for(i=0;i<9;i++)
for(j=0;j<9-i;j++)
if(strcmp(*(p+j),*(p+j+1))>0)
{ temp=*(p+j);
*(p+j)=*(p+j+1);
*(p+j+1)=temp;
}
}
运行情况如下:
Input 10 strings:
China↙
Japan↙
Yemen↙
Pakistan↙
Mexico↙
Korea↙
Brazil↙
Iceland↙
Canada↙
Mongolia↙
Now, the sequence is:
Brazil
Canada
China
Iceland
Japan
Korea
Mexico
Mongolia
Pakistan
Yemen
10.13 写一个用矩形法求定积分的通用函数,分别求:
(说明:sin、cos、exp 已在系统的数学函数库中,程序开头要用 #include
解:程序如下:(xt10-13.c)
#include
#include
main()
{ float integral(float (*p)(float),float a,float b,int n);
float a1,b1,a2,b2,a3,b3,c,(*p)(float);
float fsin(float); /*声明fsin函数*/
float fcos(float); /*声明fcos函数*/
float fexp(float); /*声明fexp函数*/
int n=20;
printf("Input a1,b1: "); /*输入求sinx定积分的下限和上限*/
scanf("%f,%f",&a1,&b1);
printf("Input a2,b2: "); /*输入求cosx定积分的下限和上限*/
scanf("%f,%f",&a2,&b2);
printf("Input a3,b3: "); /*输入求定积分的下限和上限*/
scanf("%f,%f",&a3,&b3);
p=fsin;
c=integral(p,a1,b1,n); /*求出sinx的定积分*/
printf("The integral of sin(x) is: %f\n",c);
p=fcos;
c=integral(p,a2,b2,n); /*求出cosx的定积分*/
printf("Tht integral of cos(x) is: %f\n",c);
p=fexp;
c=integral(p,a3,b3,n); /*求出ex的定积分*/
printf("The integral of exp(x) is: %f\n",c);
}
float integral(float(*p)(float),float a,float b,int n)
/*用矩形法求定积分的通用函数*/
{ int i;
float x,h,s;
h=(b-a)/n;
x=a;
s=0;
for(i=1;i<=n;i++)
{ x=x+h;
s=s+(*p)(x)*h;
}
return(s);
}
float fsin(float x) /*计算sinx的函数*/
{ return sin(x);}
float fcos(float x) /*计算cosx的函数*/
{return cos(x);}
float fexp(float x) /*计算ex的函数*/
{return exp(x);}
运行情况如下,
Input a1,b1:0,1↙
Input a2,b2: -l,1↙
Input a3,b3: 0,2↙
The integral of sin(x) is: O.480639
The integral of cos(x) is: 1.681539
The integral of exp(x) is: 6.713833
说明:sin、cos和exp是系统提供的数学函数。在程序中定义3个函数fsin、fCOS和fexp分别用来计算sin(x)、cos(x)和exp(x)的值,在main函数中要声明这3个函数。在main函数中定义p为指向函数的指针变量,定义形式是“float(*p)(float)”,表示p指向的函数有一个实型形参,p可指向返回值为实型的函数。在main函数中有“p=fsin;”,表示将fsin函数的人口地址赋给p。在调用integral函数时,用p作为实参,把fsin函数的人口地址传递给形参p(形参p也定义为指向函数的指针变量),这样形参P也指向fsin函数,(*p)(x)就相当于fsin(x)。fsin(x)的值就是sinx的值。因此通过调用integral函数求出了sinx的定积分。求其余两个函数定积分的情况与此类似。
10.14将n个数按输入顺序的逆序排列,用函数实现。
解:程序如下:(xt10-14.c)
#include
main()
{ void sort(int *p,int m);
int i,n;
int *p,num[20];
printf("input n: ");
scanf("%d",&n);
printf("please input these numbers:\n");
for(i=0;i
scanf("%d",&num[i]);
p=&num[0];
sort(p,n);
printf("Now, the sequence is:\n");
for(i=0;i
printf("%d ",num[i]);
}
void sort(int *p,int m)
{ int i;
int temp,*p1,*p2;
for(i=0;i
{ p1=p+i;
p2=p+(m-1-i);
temp=*p1;
*p1=*p2;
*p2=temp;
}
}
运行结果:
input n: l0↙
please input these numbers:
l0 9 8 7 6 5 4 3 2 1 ↙
Now,the sequence is:
1 2 3 4 5 6 7 8 9 10
10.15有一个班4个学生,5门课。(1)求第一门课的平均分;(2)找出有两门以上课程不及格的学生,输出他们的学号和全部课程成绩及平均成绩;(3)找出平均成绩在90分以上或全部课程成绩在85分以上的学生。分别编3个函数实现以上3个要求。
解:程序如下:(xt10-15.c)
#include
main()
{ void avsco(float *psco,float *pave);
void avcour1(char *pcou,float *psco);
void fali2(char course[5][10],int num[4],float score[4][5],float aver[4]);
void good(char course[5][10],int num[4],float score[4][5],float aver[4]);
int i,j,*pnum,num[4];
float score[4][5],aver[4],*psco,*pave;
char course[5][10],*pcou;
printf("Input course: \n");
pcou=course[0];
for(i=0;i<5;i++)
scanf("%s",course[i]);
printf("Input NO. and scoures: \n");
printf("NO.");
for(i=0;i<5;i++)
printf(",%s",course[i]);
printf("\n");
psco=&score[0][0];
pnum=&num[0];
for(i=0;i<4;i++)
{ scanf("%d",pnum+i);
for(j=0;j<5;j++)
scanf(",%f",psco+5*i+j);
}
pave=&aver[0];
printf("\n\n");
avsco(psco,pave); /*求出每个学生的平均成绩*/
avcour1(pcou,psco); /*求出第一门课的平均成绩*/
printf("\n\n");
fali2(pcou,pnum,psco,pave); /*找出2门课不及格的学生*/
printf("\n\n");
good(pcou,pnum,psco,pave); /*找出成绩好的学生*/
}
void avsco(float *psco,float *pave) /*求每个学生的干均成绩的函数*/
{ int i,j;
float sum,average;
for(i=0;i<4;i++)
{ sum=0.0;
for(j=0;j<5;j++)
sum=sum+(*(psco+5*i+j)); /*累计每个学生的各科成绩*/
average=sum/5; /* 计算平均成绩*/
*(pave+i)=average;
}
}
void avcour1(char *pcou,float *psco) /*第一门课的平均成绩的函数*/
{ int i;
float sum,average1;
sum=0.0;
for(i=0;i<4;i++)
sum=sum+(*(psco+5*i)); /*累计每个学生的得分*/
average1=sum/4; /*计算平均成绩*/
printf("course 1: %s ,average score: %6.2f. \n", pcou,average1);
}
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数组的指针变量
|
函数的形参调用用数组,调用函数时的实参用指针变量。形参也可以不用数组而用指针变量,请读者自己分析。
10.16 输入一个字符串,内有数字和非数字字符,如:
a123x456ㄩ17960?302tab5876
将其中连续的数字作为一个整数,依次存放到一数组a中。例如:123放在a[0]中,456放在a[1]中……统计共有多少个整数,并输出这些数。
解:程序如下:(xt10-16.c)
#include
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
{ 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
{ 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
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 58
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
解:程序如下:(xt10-17.c)
#include
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
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.
提示,先在内存规定出一片相当大的连续空间(例如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个元素
pt=alloc(n);
则pt的值为刚才所开辟的空间的首地址(allocp'-n)。
在调用free函数时,如果写出以下调用语句:
free(pt);
则把allocp的值改成pt,即使得allocp指向刚才开辟空间的开头,恢复allocp的原值,就相当于释放此段空间,使这段空间可以做其他用途。
10.20用指向指针的指针的方法对5个字符串排序并输出。
解:程序如下:(xt10-20.c)
#include
#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
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
pstr[i]=&data[i]; /*将第i个整数的地址赋予指针数组pstr的第i个元素*/
printf("Input %d integer numbers:\n",n);
for(i=0;i
scanf("%d",pstr[i]);
p=pstr;
sort(p,n);
printf("Now, the sequence is:\n");
for(i=0;i
printf("%d ",*pstr[i]);
printf("\n");
}
void sort(int **p,int n)
{ int i,j,*temp;
for(i=0;i
{ for(j=i+1;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是指向指针的指针。