zoukankan      html  css  js  c++  java
  • C语言:去除一个字符串中所有的空格。-函数fun传入形参m,求t=1/2-1/3+1/4.....+1/m的值。-判断形参a指定的矩阵是不是“幻方“。

    //函数fun功能:判断形参a指定的矩阵是不是“幻方“,若是返回1。(”幻方”:每列,每行,对角线,反对角线相加都相等)

     1 #include  <stdio.h>
     2 #define   N   3
     3 int fun(int  (*a)[N])
     4 { int  i,j,m1,m2,row,colum;
     5   m1=m2=0;
     6   for(i=0; i<N; i++)
     7   { j=N-i-1;  m1+=a[i][i];  m2+=a[i][j];  }
     8   if(m1!=m2) return  0;
     9   for(i=0; i<N; i++) {
    10 /**********found**********/
    11      row=colum= 0;
    12      for(j=0; j<N; j++)
    13      {  row+=a[i][j];  colum+=a[j][i];  }
    14 /**********found**********/
    15      if( (row!=colum) || (row!=m1) ) return 0;
    16   }
    17 /**********found**********/
    18   return  1;
    19 }
    20 void main()
    21 { int  x[N][N],i,j;
    22   printf("Enter number for array:
    ");
    23   for(i=0; i<N; i++)
    24     for(j=0; j<N; j++)  scanf("%d",&x[i][j]);
    25   printf("Array:
    ");
    26   for(i=0; i<N; i++)
    27   {  for(j=0; j<N; j++)  printf("%3d",x[i][j]);
    28      printf("
    ");
    29   }
    30   if(fun(x)) printf("The Array is a magic square.
    ");
    31   else printf("The Array isn't a magic square.
    ");
    32 }

    //函数fun传入形参m,求t=1/2-1/3+1/4.....+1/m的值。

     1 #include  <stdlib.h>
     2 #include  <conio.h>
     3 #include  <stdio.h>
     4 double fun(int m)
     5 {
     6   double t=1.0;
     7   int i;
     8   for(i=2;i<=m;i++)
     9 /*************found**************/
    10       t=t-(double)1/i;//i为int类型变量,需要转换类型。
    11 /*************found**************/
    12       return t;
    13 }
    14 void main()
    15 {int m;
    16  system("CLS");
    17  printf("
    Please enter 1 integer numbers:
    ");
    18  scanf("%d",&m);
    19  printf("
    
    The result is %1f
    ",
    20  fun(m));
    21 }

    //输入一串字符串,去除之中的所有空格。

    //下面为自己书写的代码,略有瑕疵。

     1 #include <stdio.h>
     2 #include <ctype.h>
     3 #include <conio.h>
     4 #include <stdlib.h>
     5 void fun (char *str)
     6 {
     7     char s[81];
     8     int i = 0,x=0,c=0;
     9     while (*str != '')//注意这里是单引号
    10     {
    11         if (*str!=' ')//这里也是单引号
    12         {
    13             s[i] = *str;
    14             printf("%c", s[i]);
    15             i++;//切记这句代码的位置
    16         }
    17         str++;
    18         x++;
    19     }
    20     str = str - x;
    21     //str = s;//不可以这样赋值,因为s为数组名,不可以直接用数组赋值。
    22     for (int j = 0; j < i; j++)
    23     {
    24         *str = s[j];
    25         printf("%c", *str);
    26         str++;
    27         c++;
    28         if (j == i - 1) { *str = ''; }//这里一定要加结束符,不然会出错。
    29     }
    30     str = str - c;
    31 }
    32 void main()
    33 {
    34   char str[81];
    35   char Msg[]="Input a string:";
    36   int n;
    37   FILE *out;
    38   printf(Msg);
    39   gets(str);
    40   puts(str);
    41   fun(str); 
    42   printf("*** str: %s
    ",str); 
    43   /******************************/
    44   out=fopen("out.dat","w");
    45   fun(Msg);
    46   fprintf(out,"%s",Msg);
    47   fclose(out);
    48   /******************************/
    49 }

    //标准答案:

     1 void fun(char *str)
     2 {
     3     int i=0;
     4     char *p=str;
     5     while(*p)
     6     {
     7          if(*p!=' ')
     8          {
     9               str[i++]=*p;
    10           }
    11            p++;
    12     }
    13     str[i]='';
    14 }    
  • 相关阅读:
    闰年测试
    EditBox的测试用例设计
    测试工程中的评审
    测试框架
    github
    第一次上机实验
    对软件测试的初步认识
    白盒测试
    Date : 日期对象
    C++ 格式化输出 及 输入 流
  • 原文地址:https://www.cnblogs.com/ming-4/p/10439357.html
Copyright © 2011-2022 走看看