*pmax=a[i];
if(*pmin>a[i])
*pmin=a[i];
*aver+=a[i];
}
*aver=*aver/n;
}
intmain()
{
floata[100];
floatmax,min,aver;
inti;
intn;
printf("请输入一个整数作为所需输入数的个数:
\n");
scanf("%d",&n);
printf("请输入%d个数\n",n);
for(i=0;i<=n-1;i++)
scanf("%f",&a[i]);
fun(a,&max,&min,&aver,n);//参数max,min,aver均按地址传递
printf("最大数为:
%f,最小数为:
%f,平均数为:
%f\n",max,min,aver);
return0;
}
3、
#include
intff(chars[],charch)
{
inti=0,num=0;
while(s[i]!
='\0')
if(s[i++]==ch)
num++;
returnnum;
}
voidmain()
{
charstr[]="dazhutou";
printf("%d",ff(str,'u'));
}
4、
#include
voidmain()
{
voidc(longintm);
longintn;
printf("请输入一个任意整数\n");
scanf("%ld",&n);
printf("用递归法将其转换成字符串是:
\n");
if(n<0)//考虑到输入可能为负数//
{
putchar('-');
n*=-1;//先将负数转换成整数,以免在自定义的函数内再讨论//
}
c(n);
printf("\n");
}
voidc(longintm)
{
longintx;
x=m/10;
if(x!
=0)//递归的结束条件//
c(x);
putchar(m%10+'0');//字符0的ASCLL码是48,任意一个一位数加上48生成的//
}//ASCLL码对应的字符就是这个任意一位数本身了//
5、
#include
#include
intfun(intx)
{
intr=0,d,c;
while
(1)
{
d=x%10;
r=r+d;
x=(x-d)/10;
if(x==0)
break;
}
returnr;
}
main()
{
intnum;
printf("inputnum\n");
scanf("%d",&num);
printf("%d\n",fun(num));
}
6、
#include
#include
voidmain()
{
intprime(int);
intn;
printf("Inputthenumber:
");
scanf("%d",&n);
if(prime(n))
printf("\n%disaprime\n",n);
else
printf("\n%disnotaprime.\n",n);
}
intprime(intNum)/*是素数返回1,否则返回0*/
{
inti,MaxDivisor=(int)sqrt(Num);
if(Num<=2)
return0;
for(i=2;i<=MaxDivisor;i++)
{
if(Num%i==0)
return0;/*Num能被i除,说明不是素数,返回0*/
}
return1;
}
7、
#include
#include
voidf(charx[],intm)
{charw[10];
inti,j,n;
n=strlen(x);
for(j=0;jw[j]=x[n-m+j];
x[n-m]='\0';
for(i=0;iw[m+i]=x[i];
printf("%s\n",w);
}
main()
{charg[10];
intm;
scanf("%s",g);
scanf("%d",&m);
f(g,m);
}
8、
#include"stdio.h"
#include"string.h"
voidRever(char*s,inti)
{
if(i>=0)
putchar(s[i]);
i--;
if(i>=0)
Rever(s,i);
}
voidmain()
{
chars[100];
gets(s);
Rever(s,strlen(s)-1);
}
9、
#include"stdio.h"
#include"string.h"
intfun(char*s,char*t)
{
intn=0;
while(*s&&*t)
if(*s++==*t++)
n++;
returnn;
}
voidmain()
{
chars[200],t[10];
scanf("%s\n",s);
scanf("%s",t);
printf("出现次数%d\n",fun(s,t));
}
P119
1、
#include
#include
voidmain()
{
FILE*fp;
charc;
if((fp=fopen("ccw.txt","r+"))==NULL)
{
printf("cannotopen");
exit(0);
}
while(!
feof(fp))
{
c=fgetc(fp);
if(c=='@')
break;
else
c-=10;
fseek(fp,-1L,1);
fputc(c,fp);
fseek(fp,0L,1);
}
}
2、
#include
intfun(char*fname,inta,intb)
{
FILE*fp;
if((fp=fopen("file.txt","w"))==NULL)
{
printf("cannotopenfile\n");
return0;
}
fprintf(fp,"%d%d%d%d%d",a+b,a-b,a*b,a/b,a%b);
printf("\nsucceed\n");
fclose(fp);
return1;
}
voidmain()
{
charfname[]="file.txt";
inta,b;
scanf("%d%d",&a,&b);
fun(fname,a,b);
}
3、
4、
#include
#include
#defineLENsizeof(structstudent)
structstudent
{
charnum[6];
charname[8];
charsex[2];
intage;
structstudent*next;
}stu[10];
voidmain()
{
structstudent*p,*pt,*head;
inti,length,iage,flag=1;
intfind=0;
while(flag==1)
{
printf("请输入链表长度(小于10):
");
scanf("%d",&length);
if(length<10)
flag=0;
}
for(i=0;i{
p=(structstudent*)malloc(LEN);
if(i==0)
head=pt=p;
else
pt->next=p;
pt=p;
printf("No.:
");
scanf("%s",p->num);
printf("name:
");
scanf("%s",p->name);
printf("sex:
");
scanf("%s",p->sex);
printf("age:
");
scanf("%d",&p->age);
}
p->next=NULL;
p=head;
printf("\nNo.namesexage\n");
while(p!
=NULL)
{
printf("%4s%8s%6s%6d\n",p->num,p->name,p->sex,p->age);
p=p->next;
}
printf("inputage:
");
scanf("%d",&iage);
pt=head;
p=pt;
if(pt->age==iage)
{
p=pt->next;
head=pt=p;
find=1;
}
else
pt=pt->next;
while(pt!
=NULL)
{
if(pt->age==iage)
{
p->next=pt->next;
find=1;
}
else
p=pt;
pt=pt->next;
}
if(!
find)
printf("notfound%d.",iage);
p=head;
printf("\nNo.namesexage\n");
while(p!
=NULL)
{
printf("%4s%8s",p->num,p->name);
printf("%6s%6d\n",p->sex,p->age);
p=p->next;
}
}
5、
#include
#include
#defineN10
typedefstructstudent
{
intnum;
floatscore;
structstudent*next;
}STU;
STU*create()
{
inti;
STU*p,*head=NULL,*tail=head;
for(i=0;i{
p=(STU*)malloc(sizeof(STU));
scanf("%d%f",&p->num,&p->score);
p->next=NULL;
if(p->num<0)
{
free(p);
break;
}
if(head==NULL)
head=p;
else
tail->next=p;
tail=p;
}
returnhead;
}
voidoutput(STU*p)
{
while(p!
=NULL)
{
printf("%d\t%.2f\n",p->num,p->score);
p=p->next;
}
}
STU*link(STU*p1,STU*p2)
{
STU*p,*head;
if(p1->score>p2->score)
{
head=p=p1;
p1=p1->next;
}
else
{
head=p=p2;
p2=p2->next;
}
while(p1!
=NULL&&p2!
=NULL)
{
if(p1->score>p2->score)
{
p->next=p1;
p=p1;
p1=p1->next;
}
else
{
p->next=p2;
p=p2;
p2=p2->next;
}
}
if(p1!
=NULL)
p->next=p1;
else
p->next=p2;
returnhead;
}
intmain(intargc,char*argv[])
{
STU*a,*b,*c;
printf("\n请输入链表a的信息,学号小于零时结束输入:
格式(学号成绩)\n");
a=create();
printf("\n请输入链表b的信息,学号小于零时结束输入:
格式(学号成绩)\n");
b=create();
printf("\n链表a的信息为:
\n");
output(a);
printf("\n链表b的信息为:
\n");
output(b);
c=link(a,b);
printf("\n合并后的链表信息为:
\n");
output(c);
return0;
}
6、
#include
#include
#defineMAXSIZE100
typedefintdatatype;
typedefstructlink_node{
datatypedata;
structlink_node*next;
}node;
voidinit(node**p)
{
intx;
node*pre,*q;
pre=*p;
scanf("%d",&x);
while(x!
=0)
{
q=(node*)malloc(sizeof(node));
q->data=x;
if(!
pre)
{
*p=pre=q;
}
else{
pre->next=q;
pre=q;
}
scanf("%d",&x);
}
pre->next=NULL;
}
node*nizhuan(node*p)
{
node*pre,*q;
node*r;
r=p;
pre=p;
if(!
p||!
p->next)returnp;
else
{
q=pre->next;
while(q->next)
{
pre=q;
q=q->next;
pre->next=p;
p=pre;
}
q->next=p;
p=q;
r->next=NULL;
returnp;
}
}
voiddisplay(node*p)
{
node*q;
q=p;
if(!
q)printf("单链表为空!
");
else{
while(q)
{
printf("%5d",q->data);
q=q->next;
}
}
printf("\n");
}
main()
{
nod