1.拆分
#include"stdio.h"
void splitfloat(float x,int *intpart,float *fracpart);
int main()
{
float x,f;
int i;
int *pi=&i;
float *pf=&f;
printf("输入一个实数x:");
scanf("%f",&x);
splitfloat(x,pi,pf);
printf("*intpart=%d\n *fracpart%f\n",i,f);
return 0;
}
void splitfloat(float x,int *intpart,float *fracpart)
{
*intpart=(int)x;
*fracpart=x-*intpart;
}
void splitfloat(float x,int *intpart,float *fracpart);
int main()
{
float x,f;
int i;
int *pi=&i;
float *pf=&f;
printf("输入一个实数x:");
scanf("%f",&x);
splitfloat(x,pi,pf);
printf("*intpart=%d\n *fracpart%f\n",i,f);
return 0;
}
void splitfloat(float x,int *intpart,float *fracpart)
{
*intpart=(int)x;
*fracpart=x-*intpart;
}
3,循环后移
#include"stdio.h"
int main()
{
int n,a[10],m,k;
int i,b[10],x;
printf("输入n个整数");
scanf("%d%d",&n,&m);
printf("输入x个整数");
scanf("%d",&x);
for(k=0;k
scanf("%d",&a[k]);
for(i=0;i
scanf("%d",&b[i]);
for(i=0,k=0;i
if((k-m)<0)
a[k]=a[k+m+1];
if((k-m)>=0)
a[k]=b[i-m];
printf("%d\t",a[k]);}
printf("\n");
return 0;
}
6、删除字符
#include"stdio.h"
#include"string.h"
void delchar(char* a,char* b,char c);
int main(void)
{
char a[100];
char b[100];
char c;
printf("Input the string:");
scanf("%s",a);
printf("Input a ch:");
scanf("%s",&c);
delchar(a,b,c);
printf("%s",b);
printf("\n");
return 0;
}
void delchar(char* a,char* b,char c)
{
int i=0,j=0;
while(a[i]!='\0'){
if(a[i]!=c){
b[j]=a[i];
j++;}
i++;
}
b[j]='\0'; }
#include"string.h"
void delchar(char* a,char* b,char c);
int main(void)
{
char a[100];
char b[100];
char c;
printf("Input the string:");
scanf("%s",a);
printf("Input a ch:");
scanf("%s",&c);
delchar(a,b,c);
printf("%s",b);
printf("\n");
return 0;
}
void delchar(char* a,char* b,char c)
{
int i=0,j=0;
while(a[i]!='\0'){
if(a[i]!=c){
b[j]=a[i];
j++;}
i++;
}
b[j]='\0'; }
7.字符排序
#include
#include
int main(void)
{
int n,i,j,index;
char sx[100][100],stemp[100];
printf("Enter n:");
scanf("%d",&n);
printf("Input %d strings:",n);
for(i=0;i scanf("%s",sx[i]);
for(i=0;i index=i;
for(j=i+1;j if(strcmp(sx[j],sx[index])<0)
index=j;
strcpy(stemp,sx[i]);
strcpy(sx[i],sx[index]);
strcpy(sx[index],stemp);
}
printf("after sorted: \n");
for(i=0;i<5;i++)
printf("%s\t",sx[i]);
printf("\n");
return 0;
}
#include
int main(void)
{
int n,i,j,index;
char sx[100][100],stemp[100];
printf("Enter n:");
scanf("%d",&n);
printf("Input %d strings:",n);
for(i=0;i
for(i=0;i
for(j=i+1;j
index=j;
strcpy(stemp,sx[i]);
strcpy(sx[i],sx[index]);
strcpy(sx[index],stemp);
}
printf("after sorted: \n");
for(i=0;i<5;i++)
printf("%s\t",sx[i]);
printf("\n");
return 0;
}
心得:指针和地址非常重要,要灵活的运用。对于指针在字符和数组中的应用,要清晰的掌握地址变量,每一个体都值得去研究。