实验十:指针

1.拆分实数

#include"stdio.h"
void splidfloat(float x,int*intpart,float*fracpart);
int main()
{
float x,f=0;
int i=0;
float*pf=&f;
int*pi=&i;
printf("Enter x:");
scanf("%f",&x);
splidfloat(x,pi,pf);
printf("intpart=%d\nfracpart=%f\n",i,f);
return 0;
}
void splidfloat(float x,int*intpart,float*fracpart)
{
*intpart=(int)x;
*fracpart=x-*intpart;
}

 

3.循环后移

#include"stdio.h"
#include"stdlib.h"
int main()
{
int n,m,i,j;
int*p;
printf("Enter 输入n个数,移动m位置:");
scanf("%d%d",&n,&m);
p=(int*)calloc(n,sizeof(int));
for(i=0;i<n;i++)
scanf("%d",p+i);
for(i=0;i<n;i++){
j=(m/n+1)*n-m+i;
if(j>=n)
j-=n;
printf("%d\t",*(p+j));
}
printf("\n");
return 0;
}

6.删除字符

#include"stdio.h"
#include"string.h"
#define x 100
void delchar(char s[],char c);
int main()
{
char str[x],c;
printf("输入字符串:");
gets(str);
printf("输入删除字符:");
scanf("%c",&c);
delchar(str,c);
puts(str);
return 0;
}
void delchar(char*s,char c)
{
int a,i;
a=strlen(s);
for(i=a-1;i>=0;i--)
if(*(s+i)==c)
strcpy(s+i,s+i+1);
}

7.字符串顺序

#include"stdio.h"
#include"string.h"
#define x 100
int main()
{
char s[5][x],i,j,a[x];
printf("输入五组字符串\n");
for(i=0;i<5;i++)
gets(s[i]);
for(i=1;i<5;i++)
for(j=0;j<5-i;j++)
if(strcmp(s[j],s[j+1])==1){
strcpy(a,s[j]);
strcpy(s[j],s[j+1]);
strcpy(s[j+1],a);
}
printf("排序后\n");
for(i=0;i<5;i++)
puts(s[i]);
return 0;
}

心得:本次实验,涉及内容较多,有string文件下的多个函数,有指针变量p和数组a[]相同与不同使用,也有自定义函数中指针变量传递关系。同时,翻阅质料不断测试后,也掌握了宏定义define,字节计算函数sizeof,字符串输入输出函数gets、puts,stdlib文件下的calloc,malloc等函数的使用。

猜你喜欢

转载自www.cnblogs.com/cat-of-Schrodinger/p/11028283.html