fprintf(fp,"%d",*p);}system("pause");
fclose(fp);
}
11、已知数组a中的元素已按由小到大顺序排列,以下程序的功能是将输入的一个数插入数组a中,插入后,数组a中的元素仍然由小到大顺序排列*/
main()
{inta[10]={0,12,17,20,25,28,30};/*a[0]为工作单元,从a[1]开始存放数据*/
intx,i,j=6;
/*j为元素个数*/
printf("Enteranumber:
");
scanf("%d",&x);
a[0]=x;
i=j;
/*从最后一个单元开始*/
while(a[i]>x)
{a[i+1]=a[i];i--;}
/*将比x大的数往后移动一个位置*/
a[++i]=x;
j++;
/*插入x后元素总个数增加*/
for(i=1;i<=j;i++)printf("%8d"
a[i]);
printf("\n");
system("pause");
}
c1
12、/*编写函数replace(char*s,charc1,charc2)实现将s所指向的字符串中所有字符用c2替换,字符串、字符c1和c2均在主函数中输入,将原始字符串和替换后的字符串显示在屏幕上,并输出到文件p10_2.out中*/
#include
replace(char*s,charc1,charc2)
{while(*s!
='\0')
{if(*s==c1)
*s=c2;
s++;
main()
{FILE*fp;
charstr[100],a,b;if((fp=fopen("p10_2.out","w"))==NULL)
{printf("cannotopenthefile\n");
exit(0);}
printf("Enterastring:
\n");
gets(str);
printf("Entera&&b:
\n");
scanf("%c,%c",&a,&b);printf("%s\n",str);
fprintf(fp,"%s\n",str);
replace(str,a,b);
printf("Thenewstringis%s\n",str);
fprintf(fp,"Thenewstringis%s\n",str);
fclose(fp);
system("pause");
}
13、/*在一个字串si中查找一子串s2,若存在则返回子串在主串中的起始位置,不存在则返回-1。
*/
main()
{chars1[6]="thisis";chars2[5]="is";printf("%d\n",search(s1,s2));
system("pause");
}
intsearch(chars1[],chars2[])
{inti=0,j,len=strlen(s2);
while(s1[i]){
for(j=0;jif(s1[i+j]!
=s2[j])break;
if(j>=len)returni;
elsei++;
}
return-1;
}
14、/*用指针变量输出结构体数组元素。
*/
structstudent
{
intnum;
char*name;
charsex;
intage;}stu[5]={{1001,"lihua",'F',18},{1002,"liuxing",'M',19},{1003,"huangke",'F',19},{1004,"fengshou",'F',19},{1005,"Wangming",'M',18}};
main()
{inti;
structstudent*ps;
printf("Num\tName\t\t\tSex\tAge\t\n");
/*用指针变量输出结构体数组元素。
*/for(ps=stu;psprintf("%d\t%-10s\t\t%c\t%d\t\n",ps->num,ps->name,ps->sex,ps->age);
/*用数组下标法输出结构体数组元素学号和年龄。
*/for(i=0;i<5;i++)
printf("%d\t%d\t\n",stu[i].num,stu[i].age);system("pause");
}
15、/*建立一个有三个结点的简单链表:
*/
#defineNULL0
structstudent
{
intnum;
char*name;
intage;
structstudent*next;
};
voidmain()
{
structstudenta,b,c,*head,*p;
a.num=1001;a.name="lihua";a.age=18;/*对结点成员进行赋值*/
b.num=1002;b.name="liuxing";b.age=19;
c.num=1003;c.name="huangke";c.age=18;
head=&a;/*建立链表,a为头结点*/
a.next=&b;
b.next=&c;
c.next=NULL;
p=head;/*输出链表*/do{
printf("%5d,%s,%3d\n",p->num,p->name,p->age);p=p->next;
}while(p!
=NULL);
system("pause");
}
16、/*输入一个字符串,判断其是否为回文。
回文字符串是指从左到右读和从右到左读完全相同的字符串。
*/
#include
#include
#include
main()
{chars[100];
inti,j,n;
printf("输入字符串:
\n");
gets(s);
n=strlen(s);
for(i=0,j=n-1;iif(s[i]!
=s[j])break;
if(i>=j)printf("是回文串\n”);
elseprintf("不是回文串\n");
system("pause");
}
17、/*冒泡排序,从小到大,排序后结果输出到屏幕及文件myf2.out*/
#include
voidfun(inta[],intn)
{inti,j,t;
for(i=0;i<=n_1;i++)
for(j=0;j
if(a[j]>a[j+1]){t=a[j];a[j]=a[j+1];a[j+1]=t;}
}
main()
{inta[10]={12,45,7,8,96,4,10,48,2,46},n=10,i;
FILE*f;
if((f=fopen("myf2.out","w"))==NULL)
printf("openfilemyf2.outfailed!
\n");
fun(a,10);
for(i=0;i<10;i++)
{printf("%4d",a[i]);
fprintf(f,"%4d",a[i]);
}
fclose(f);
system("pause");
}
18、编写函数countpi,利用公式
兀1121231234
—1■
23353573579…
计算n的近似值,当某一项的值小于10-5时,认为达到精度要求,请完善函数。
将结果显示
在屏幕上并输出到文件p7_3.out中。
#include
doublecountpi(doubleeps)/*eps为允许误差*/
{
intm=1;
doubletemp=1.0,s=0;
while(temp>=eps)
{s+=temp;
temp=temp*m/(2*m+1);
m++;
}
return(2*s);}main()
{FILE*fp;
doubleeps=1e-5,pi;
if((fp=fopen("p7_3.out","w"))==NULL)
{printf("cannotopenthefile\n");
exit(0);
}
pi=countpi(eps);
printf("pi=%lf\n",pi);fprintf(fp,"pi=%lf\n",pi);fclose(fp);system("pause");
}
*
***