//数字字符顺序后移,经常考,要牢记于心
char *fun(char *s)
{ int i, j, k, n; char *p, *t;
n=strlen(s)+1;
t=(char*)malloc(n*sizeof(char));
p=(char*)malloc(n*sizeof(char));
j=0; k=0;
for(i=0; i<n; i++)
{ if(((s[i]>='a')&&(s[i]<='z'))||((s[i]>='A')&&(s[i]<='Z'))) {
/**********found**********/
t[j]=s[i]; j++;}
else
{ p[k]=s[i]; k++; }
}
/**********found**********/
for(i=0; i<k; i++) t[j+i]=p[i];//取值范围0~k,非字母元素放在字母元素后面
/**********found**********/
t[j+k]= '\0';//字符串结束符
return t;
}
void fun (char *s, char *t1, char *t2 , char *w)
{
char *p , *r, *a;
strcpy( w, s );
/************found************/
while ( *w )
{ p = w; r = t1;
while ( *r )
/************found************/
if( *r == *p )
{ r++; p++; }
else break;
if ( *r == '\0' ) a = w;
w++;
}
r = t2;
while ( *r ){ *a = *r; a++; r++; }
}
//文件操作
void fun(char *s, int a, double f)
{
/**********found**********/
FILE* fp;//文件类型为指针
char str[100], str1[100], str2[100];
int a1; double f1;
fp = fopen("file1.txt", "w");
fprintf(fp, "%s %d %f\n", s, a, f);
/**********found**********/
fclose(fp);//在fo做打开操作时,进行不同的操作前,应该将其关闭
fp = fopen("file1.txt", "r");
/**********found**********/
fscanf(fp,"%s%s%s", str, str1, str2);//把文本文件输出
fclose(fp);
a1 = atoi(str1);
f1 = atof(str2);
printf("\nThe result :\n\n%s %d %f\n", str, a1, f1);
}
//结构体操作
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <stdio.h>
#include <malloc.h>
#define N 10
typedef struct ss
{ char num[10];
int s;
} STU;
STU *fun(STU a[], int m)
{ STU b[N],*t;
int i, j,k;
/*************found**************/
t=(STU*)calloc(m,sizeof(STU));//强制转换成stu类型
for(i=0;i<N;i++) b[i]=a[i];
for(k=0;k<m;k++)
{ for (i=j=0;i<N;i++)
if(b[i].s>b[j].s) j=i;
/*************found**************/
strcpy(t[k].num,b[j].num);//结构体变量的转换
t[k].s=b[j].s;
b[j].s=0;
}
return t;
}
void outresult(STU a[],FILE *pf)
{ int i;
for(i=0;i<N;i++)
fprintf(pf, "No=%s Mark=%d\n ",
a[i].num, a[i].s);
fprintf(pf, "\n\n ");
}
void main()
{ STU a[N]={{ "A01 ",81},{ "A02 ",89},{ "A03 ",66},{ "A04 ",87},{ "A05 ",77},
{ "A06 ",90},{ "A07 ",79},{ "A08 ",61},{ "A09 ",80},{ "A10 ",71}};
STU *pOrder;
int i, m;
system("CLS");
printf("*****THE RESULT*****\n");
outresult(a,stdout);
printf("\nGive the number of the students who have better score: ");
scanf("%d",&m);
while(m>10)
{ printf("\nGive the number of the students who have better score: ");
scanf("%d",&m);
}
pOrder=fun(a,m);
printf("***** THE RESULT*****\n");
printf("The top :\n");
for(i=0;i<m;i++)
printf("%s %d\n",pOrder[i].num, pOrder[i].s);
free(pOrder);
}
//删除数组中相同的数,从小到大排列
//法一:
int fun(int a[], int n)
{
int i, k,t;
int b[80] = { 0 };
k= a[0];
b[0] = a[0];
t = 1;
for (i = 0; i < n; i++)
{
if (a[i] != k)
{
k = a[i];
a[t++] = k;
}
}
return t;
}
//法二:
int fun(int a[], int n)
{
int i,j=1;
for(i=1;i<n;i++)
if(a[j-1)!=a[i]) a[j++]=a[i];
//与前一个数不相同,则要保留
return j;
}