第十次作业
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;
}![](https://img2018.cnblogs.com/blog/1619595/201906/1619595-20190619201021980-1542781716.jpg)
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;
}
![](https://img2018.cnblogs.com/blog/1619595/201906/1619595-20190619201021980-1542781716.jpg)
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<n;k++)
scanf("%d",&a[k]);
for(i=0;i<n;i++)
scanf("%d",&b[i]);
for(i=0,k=0;i<x,k<n;k++,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'; }
![](https://img2018.cnblogs.com/blog/1619595/201906/1619595-20190619201323310-1540132958.jpg)
7.字符排序
#include<stdio.h>
#include<string.h>
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<n;i++)
scanf("%s",sx[i]);
for(i=0;i<n-1;i++){
index=i;
for(j=i+1;j<n;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<string.h>
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<n;i++)
scanf("%s",sx[i]);
for(i=0;i<n-1;i++){
index=i;
for(j=i+1;j<n;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;
}
![](https://img2018.cnblogs.com/blog/1619595/201906/1619595-20190619201414845-1196345099.jpg)
心得:指针和地址非常重要,要灵活的运用。对于指针在字符和数组中的应用,要清晰的掌握地址变量,每一个体都值得去研究。