zoukankan      html  css  js  c++  java
  • C语言:根据形参c中指定的英文字母,按顺序打印出若干后继相邻字母,-主函数中放入一个带头节点的链表结构中,h指向链表的头节点。fun函数找出学生的最高分-使用插入排序法对字符串中的字符进行升序排序。-从文件中找到指定学号的学生数据,读入次学生数据,

    //根据形参c中指定的英文字母,按顺序打印出若干后继相邻字母,输出字母的大小与形参c一致,数量由形参d指定。例如:输入c为Y,d为4,则输出ZABC。

     1 #include  <stdio.h>
     2 #pragma warning (disable:4996)
     3 void fun(char c, int d) { 
     4   int i;
     5   char A[26], a[26], *ptr;
     6 /**********found**********/
     7   for (i=0; i<26; i++) {
     8     A[i] = 'A' + i;
     9     a[i] = 'a' + i;
    10   }
    11 /**********found**********/
    12   if ((c >= 'a') && (c<= 'z')) ptr = a;
    13   else   ptr = A;
    14 /**********found**********/
    15   for (i=1; i<=d; i++) printf("%c", ptr[(c-ptr[0]+i) % 26] );
    16 }
    17 main( ) { 
    18   char c; int d;
    19   printf("please input c & d:
    ");
    20   scanf("%c%d", &c, &d);
    21   fun(c, d);
    22 }

     //N名学生的成绩已在主函数中放入一个带头节点的链表结构中,h指向链表的头节点。fun函数找出学生的最高分,返回。

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #define   N   8
     4 struct  slist
     5 {  double   s;
     6    struct slist  *next;
     7 };
     8 typedef  struct slist  STREC;
     9 double  fun( STREC *h  )
    10 {
    11     STREC *p, *q;
    12     double max;
    13     p = h;
    14     q = h->next;
    15     max = q->s;
    16     while (q != NULL)
    17     {
    18         p = q;
    19         q = q->next;
    20         if (p->s > max) max = p->s;
    21     }
    22     return max;
    23 }
    24 
    25 STREC * creat( double *s)
    26 { STREC  *h,*p,*q;   int  i=0;
    27   h=p=(STREC*)malloc(sizeof(STREC));p->s=0;
    28   while(i<N)
    29   { q=(STREC*)malloc(sizeof(STREC));
    30     q->s=s[i]; i++; p->next=q; p=q;
    31   }
    32   p->next=0;
    33   return  h;
    34 }
    35 void outlist( STREC *h)
    36 { STREC  *p;
    37   p=h->next;   printf("head");
    38   do
    39   { printf("->%2.0f",p->s);p=p->next;}
    40   while(p!=0);
    41   printf("
    
    ");
    42 }
    43 void main()
    44 {  double  s[N]={85,76,69,85,91,72,64,87}, max;void NONO ();
    45    STREC  *h;
    46    h=creat( s );   outlist(h);
    47    max=fun( h );
    48    printf("max=%6.1f
    ",max);
    49    NONO();
    50 }
    51 void NONO ()
    52 {/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */
    53   FILE *in, *out ;
    54   int i,j ; double  s[N],max;
    55   STREC *h ;
    56   in = fopen("in.dat","r") ;
    57   out = fopen("out.dat","w") ;
    58   for(i = 0 ; i < 10 ; i++) {
    59     for(j=0 ; j < N; j++) fscanf(in, "%lf,", &s[j]) ;
    60     h=creat( s );
    61     max=fun( h );
    62     fprintf(out, "%6.1lf
    ", max) ;
    63   }
    64   fclose(in) ;
    65   fclose(out) ;
    66 }

    //另一种方式:

     1 double fun(STREC *h)
     2 {
     3      double max=h->s;
     4      while(h!=NULL)
     5      {
     6           if(max<h->s)max=h->s;
     7           h=h->next;
     8      }
     9      return max;
    10 }

    //使用插入排序法对字符串中的字符进行升序排序。插入法基本算法:先对头两个字符进行排序,然后把第三个字符插入到前两个字符中,插入后前三个字符依然有序,再插入第四个字符到前三个中。

     1 #include <stdio.h>
     2 #include <string.h>
     3 #define    N   80
     4 void  insert(char  *aa)
     5 {  int  i,j,n;     char  ch;
     6 /**********found**********/
     7    n=strlen(aa);
     8    for( i=1; i<n ;i++ ) {
     9 /**********found**********/
    10        ch=aa[i];
    11        j=i-1;
    12        while ((j>=0) && ( ch<aa[j] ))
    13        {   aa[j+1]=aa[j];//后移操作
    14            j--;        
    15        }
    16        aa[j+1]=ch;
    17    }
    18 }
    19 void main( )
    20 {   char  a[N]="QWERTYUIOPASDFGHJKLMNBVCXZ";
    21     printf ("The original string :       %s
    ", a);
    22     insert(a) ;
    23     printf("The string after sorting :  %s
    
    ",a );
    24 }

    //从文件中找到指定学号的学生数据,读入次学生数据,对该学生的分数进行修改,使每门课的分数加3分,修改后重写文件中学生的数据。

     1 #include  <stdio.h>
     2 #define    N  5
     3 typedef struct  student {
     4   long  sno;
     5   char  name[10];
     6   float  score[3];
     7 } STU;
     8 void fun(char  *filename, long  sno)
     9 { FILE  *fp;
    10   STU  n;      int  i;
    11   fp = fopen(filename,"rb+");
    12 /**********found**********/
    13   while (!feof(fp))//文件结束检测函数feof
    14   {  fread(&n, sizeof(STU), 1, fp);
    15 /**********found**********/
    16      if (n.sno==sno)  break;
    17   }
    18   if (!feof(fp))
    19   {  for (i=0; i<3; i++)  n.score[i] += 3;
    20 /**********found**********/
    21     fseek(fp, -(long)sizeof(STU), SEEK_CUR);//文件定位函数fseek,用来移动文件内部位置指针。第二个参数表示位移量
    22     fwrite(&n, sizeof(STU), 1, fp);
    23   }
    24   fclose(fp);
    25 }
    26 void main()
    27 { STU  t[N]={ {10001,"MaChao", 91, 92, 77}, {10002,"CaoKai", 75, 60, 88},
    28              {10003,"LiSi", 85, 70, 78},  {10004,"FangFang", 90, 82, 87},
    29              {10005,"ZhangSan", 95, 80, 88}}, ss[N];
    30   int  i,j;      FILE  *fp;
    31   fp = fopen("student.dat", "wb");
    32   fwrite(t, sizeof(STU), N, fp);
    33   fclose(fp);
    34   printf("
    The original data :
    ");
    35   fp = fopen("student.dat", "rb");
    36   fread(ss, sizeof(STU), N, fp);
    37   fclose(fp);
    38   for (j=0; j<N; j++)
    39   {  printf("
    No: %ld  Name: %-8s      Scores:  ",ss[j].sno, ss[j].name);
    40      for (i=0; i<3; i++) printf("%6.2f ", ss[j].score[i]);
    41      printf("
    ");
    42   }
    43   fun("student.dat", 10003);
    44   fp = fopen("student.dat", "rb");
    45   fread(ss, sizeof(STU), N, fp);
    46   fclose(fp);
    47   printf("
    The data after modifing :
    ");
    48   for (j=0; j<N; j++)
    49   {  printf("
    No: %ld  Name: %-8s      Scores:  ",ss[j].sno, ss[j].name);
    50      for (i=0; i<3; i++)  printf("%6.2f ", ss[j].score[i]);
    51      printf("
    ");
    52   }
    53 }
  • 相关阅读:
    在Postgre中设置max_connections时,为什么需要使用连接池 (译)
    既然python的多线程是"伪多线程",那么多线程访问共享资源的时候,还需要线程锁吗
    Debugging: MISCONF Redis is configured to save RDB snapshots(译)
    MySQL集群故障转移的时候在基于python的客户端连接层自动failover实现
    exec: "com.docker.cli": executable file not found in %PATH%
    windows 快捷操作系列
    Docker Build时查看当前镜像目录树
    vs2019生成的dockerfile 手动 build
    Python Records库使用举例
    Jupyter Notebook安装扩充插件与常见问题
  • 原文地址:https://www.cnblogs.com/ming-4/p/10536056.html
Copyright © 2011-2022 走看看