1. 程式人生 > >C語言:根據形參c中指定的英文字母,按順序打印出若幹後繼相鄰字母,

C語言:根據形參c中指定的英文字母,按順序打印出若幹後繼相鄰字母,

*** def fopen pan fprintf rb+ += 鏈表結構 文件

//根據形參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:\n
"); 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("\n\n"); 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\n",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\n", 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\n", a);
22     insert(a) ;
23     printf("The string after sorting :  %s\n\n",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("\nThe original data :\n");
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("\nNo: %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("\n");
42   }
43   fun("student.dat", 10003);
44   fp = fopen("student.dat", "rb");
45   fread(ss, sizeof(STU), N, fp);
46   fclose(fp);
47   printf("\nThe data after modifing :\n");
48   for (j=0; j<N; j++)
49   {  printf("\nNo: %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("\n");
52   }
53 }

C語言:根據形參c中指定的英文字母,按順序打印出若幹後繼相鄰字母,