C语言:从p所指字符串中找出ASCII码最大的字符,将其放在第一个位置上,并将该字符前的原字符向后顺序移动。-使字符串的前导*号不得多于n个,若多余n个,则删除多余的*号,

//fun函数:从p所指字符串中找出ASCII码最大的字符,将其放在第一个位置上,并将该字符前的原字符向后顺序移动。

 1 #include <stdio.h>
 2 void fun( char *p )
 3 {   char   max,*q;   int   i=0;
 4     max=p[i];
 5     while( p[i]!=0 )
 6     {   if( max<p[i] )
 7     {  max=p[i];
 8 /**********found**********/
 9     q = p + i;//先找到最大值,记录最大值的位置。
10     }
11         i++;
12     }
13 /**********found**********/
14     while(q>p )
15     {  *q=*(q-1);//进行顺序后移。
16        q--;
17     }
18     p[0]=max;
19 }
20 void main()
21 {  char   str[80];
22    printf("Enter a string:  "); gets(str);
23    printf("
The original string:      ");  puts(str);
24    fun(str);
25    printf("
The string after moving:  ");  puts(str); printf("

");
26 }

//fun函数:根据一下公式求圆周率值,并作为函数返回值。Π/2=1+1/3+1/3*2/5+1/3*2/5*3/7+...

 1 #include <math.h>
 2 #include <stdio.h>
 3 double fun(double  eps)
 4 {  double  s,t;     int  n=1;
 5    s=0.0;
 6 /************found************/
 7    t=1.0;
 8    while( t>eps)
 9    {  s+=t;
10       t=t * n/(2*n+1);
11       n++;
12    }
13 /************found************/
14    return (s*2);
15 }
16 void main()
17 {  double  x;
18    printf("
Please enter a precision: "); scanf("%lf",&x);
19    printf("
eps=%lf, Pi=%lf

",x,fun(x));
20 }

//规定输入的字符串中只包含字母和*号,fun函数:使字符串的前导*号不得多于n个,若多余n个,则删除多余的*号,若少于或等于n个,则不做处理,字符串尾部和中间的*号不删除。

 1 #include <stdio.h>
 2 void  fun( char *a, int  n )
 3 {
 4     char *b;
 5     b = a;
 6     int i = 0;
 7     while (!('A' <= *b&&*b <= 'Z'))
 8     {
 9         //printf("%c
", *b);
10         b++;
11         i++;
12     }
13     if (i > n)
14     {
15         a = a + n;//注意位置不在while里面
16         while (*b != '')
17         {
18             *a = *b;
19             a++;
20             b++;
21         }
22         *a = ''; 
23     }
24 }
25 
26 void main()
27 {  char  s[81];  int  n;void NONO ();
28    printf("Enter a string:
");gets(s);
29    printf("Enter n :  ");scanf("%d",&n);
30    fun( s,n );
31    printf("The string after deleted:
");puts(s);
32    NONO();
33 }
34 void NONO ()
35 {/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */
36   FILE *in, *out ;
37   int i, n ; char s[81] ;
38   in = fopen("in.dat","r") ;
39   out = fopen("out.dat","w") ;
40   for(i = 0 ; i < 10 ; i++) {
41     fscanf(in, "%s", s) ;
42     fscanf(in, "%d", &n) ;
43     fun(s,n) ;
44     fprintf(out, "%s
", s) ;    
45   }
46   fclose(in) ;
47   fclose(out) ;
48 }

//使用数组完成:

    void  fun( char *a, int  n )
{
   char b[81];
   int z = 0;
   while (*a != '')
   {
    b[z] = *a ;
    z++; a++;
   }
   a = a - z;//指针位置返还
   b[z] = '';//标记结束
   int i = 0;
   while (!(b[i]>='A'&&b[i] <= 'Z'))
   {
    i++;
   }
   if (i > n)
   {
    a = a + n;
    while (b[i]!='')
    {
     *a = b[i];
     //printf("%c ", *a);
     a++;
     i++;
    }
    *a ='';
   }   
}
原文地址:https://www.cnblogs.com/ming-4/p/10551482.html