以前的C代码

函数调用

 1 #include <stdio.h>
 2 int add(int a,int b)
 3 {
 4    int s;
 5    s=a+b;
 6    return s;
 7 }
 8 int main()
 9 {
10     printf("%d",add(3,5));
11     getch();
12     return 0;   
13 }

不知道

 1 #include <stdio.h>
 2 float p(int n)
 3 {
 4    int s;
 5     int i;
 6     s=1;
 7    if(n<0)
 8    printf("数据出错!");
 9    else
10    for(i=1;i<=n;i++)
11    s=s*i; 
12    return s;
13 }
14 float q(int n)
15 {
16    int s;
17     int i;
18     s=0;
19    if(n<0)
20    printf("数据出错!");
21    else
22    for(i=1;i<=n;i++)
23    s=s+p(i); 
24    return s;
25 }
26 int main()
27 {printf("%f",q(5));
28     
29     getch();
30     return 0;   
31 }

文件引用

 1 #include <stdio.h>
 2 #include "b.c"
 3 
 4 int main()
 5 {    
 6     printf("%f\n",p(5));
 7      printf("%f\n",q(5));
 8         getchar();
 9         return 0;   
10 }
main函数
 1 #include <stdio.h>
 2 #include "a.c"
 3 
 4 float q(int n)
 5 {
 6    int s;
 7     int i;
 8     s=0;
 9    if(n<0)
10    printf("数据出错!");
11    else
12    for(i=1;i<=n;i++)
13    s=s+p(i); 
14    return s;
15 }
b.c
 1 #include <stdio.h>
 2 float p(int n)
 3 {
 4    int s;
 5     int i;
 6     s=1;
 7    if(n<0)
 8    printf("数据出错!");
 9    else
10    for(i=1;i<=n;i++)
11    s=s*i; 
12    return 0;
13 }
a.c

在文件中加法

 1 #include<stdio.h>
 2 main()
 3 {
 4       FILE *fin,*fout;    
 5       int f1=1,f2=1,f3,i,n;
 6       fin=fopen("pro.in","r");
 7       fout=fopen("pro.out","w");
 8       fprintf(fout,"I am hujun who is your ghost\n");
 9       fscanf(fin,"%d",&n);
10       fprintf(fout,"%d,%d,",f1,f2);
11       for(i=3;i<=n;i++)
12       { f3=f1+f2;
13         if(i==n)
14         fprintf(fout,"%d.\n",f3);
15         else
16         fprintf(fout,"%d,",f3);
17         f1=f2;
18         f2=f3;
19       }
20       fprintf(fout,"the sum is %d\n",f1+f2+f3);
21       fclose(fin);
22       fclose(fout);
23       return 0;
24 }
25 
26 
27 /  将数换行/ 
读取文件并相加

冒泡排序

 1 #include <stdio.h>
 2 #include "creatdata.c"
 3 #define ARRAYLEN 6
 4 void BubbleSort(int a[],int n)
 5 {
 6     int i,j,t;
 7     for(i=0;i<n-1;i++)
 8     {
 9         for(j=n-1;j>i;j--)
10         {
11             if(a[j-1]>a[j])
12             {
13                 t=a[j-1];
14                 a[j-1]=a[j];
15                 a[j]=t;
16             }
17         }
18         printf("第%2d遍:",i+1); 
19         for(j=0;j<n;j++)
20             printf("%d ",a[j]);
21         printf("\n");
22     }
23 }
24 void BubbleSort1(int a[],int n)
25 {
26     int i,j,t,flag=0;        //flag用来标记是否发生交换
27     for(i=0;i<n-1;i++)
28     {
29         for(j=n-1;j>i;j--)
30         {
31             if(a[j-1]>a[j])//交换数据 
32             {
33                 t=a[j-1];
34                 a[j-1]=a[j];
35                 a[j]=t;
36                 flag=1;
37             }
38         }
39         printf("第%2d遍:",i+1); 
40         for(j=0;j<n;j++)
41             printf("%d ",a[j]);
42         printf("\n");
43         if(flag==0)    //没发生交换,直接跳出循环
44             break;
45         else
46             flag=0;
47     }
48 }
49 int main()
50 {
51     int i,a[ARRAYLEN];
52     for(i=0;i<ARRAYLEN;i++)
53         a[i]=0;
54     if(!creatdata(a,ARRAYLEN,100))
55     {
56         printf("生成随机数不成功!\n");
57         getch();
58         return 1;
59     }
60     printf("原数据:"); 
61     for(i=0;i<ARRAYLEN;i++)
62         printf("%d ",a[i]);
63     printf("\n");
64     BubbleSort1(a,ARRAYLEN);
65     printf("排序后:"); 
66     for(i=0;i<ARRAYLEN;i++)
67         printf("%d ",a[i]);
68     printf("\n");
69     getch();
70     return 0;   
71 }
冒泡排序
 1 #include <stdlib.h> 
 2 #include <stdio.h> 
 3 #include <time.h>
 4 int creatdata(int arr[],int n,int max)
 5 {
 6   int i,j,flag;
 7  srand(time(0));
 8   for(i=0;i<n;i++)
 9   {
10    do
11         {
12             arr[i]=rand()%100;
13             flag=0;
14             for(j=0;j<i;j++)
15             {
16                 if(arr[i]==arr[j])
17                     flag=1;
18             }
19          }while(flag);       
20    printf("%4d",arr[i]);
21   }
22   return 1;
23 }
creatdata.c

求平均数

 1 /* Note:Your choice is C IDE */
 2 #include "stdio.h"
 3 float aver(float a[5])
 4 {
 5  int i;
 6  float av,s=a[0];
 7  for(i=1;i<5;i++)
 8  s=s+a[i];
 9  av=s/5;
10  return av;    
11 }
12 void main()
13 {
14   float sco[5],av;
15   int i;
16   printf("\n input 5 scores: \n");
17   for(i=0;i<5;i++)
18   scanf("%f",&sco[i]);
19   av=aver(sco);
20   printf("average score is %6.2f\n",av);
21 }
求平均数

球跳高

 1 /* Note:Your choice is C IDE */
 2 #include "stdio.h"
 3 main()
 4 {
 5    float sn=100.0,hn=sn/2;
 6    int n;
 7    for(n=2;n<=10;n++)
 8    {
 9        sn=sn+2*hn;
10        hn=hn/2;
11    }
12    printf("the total road is %f\n",sn);
13    printf("the ghost tenth is %f",hn);
14     
15 }
球跳高

水仙花

 1 /* Note:Your choice is C IDE */
 2 #include "stdio.h"
 3 main()
 4 {
 5  int i,j,k,n;
 6  printf("water flower is :");
 7  for(n=100;n<1000;n++)
 8     {
 9       i=n/100;
10       j=n/10%10;
11       k=n%10;
12       if(i*100+j*10+k==i*i*i+j*j*j+k*k*k)
13       {
14       printf("%-5",n);
15       }
16     }
17   printf("\n"); 
18 }
水仙花

兔子繁殖

 1 /* Note:Your choice is C IDE */
 2 #include "stdio.h"
 3 main()
 4 {
 5  long f1,f2;
 6  int i;
 7  f1=f2=1;
 8  for(i=1;i<21;i++)
 9    {
10        printf("%12ld,%12ld",f1,f2);
11        if(i%2==0)
12        printf("\n");
13        f1=f1+2;
14        f2=f1+f2;
15    }
16 }
兔子繁殖

分解因数

 1 /* Note:Your choice is C IDE */
 2 #include "stdio.h"
 3 main()
 4 {
 5  int n,i;
 6  printf("input the number ");
 7  scanf("%d",&n);
 8  printf("%d=",n);
 9  for(i=2;i<n;i++)
10  { while(n!=i)
11   { 
12   if(n%i==0)
13    {
14        printf("%d*",i);
15        n=n/i;
16    }
17   else
18   break;
19   } 
20  }
21 printf("%d",n);
22 }
分解因数

 递归

 1 #include <stdio.h>
 2 int p(int n)
 3 {
 4     
 5     if(n==0||n==1) return 1;
 6     else
 7     return  p(n-1)+p(n-2);
 8 }
 9 
10 int main()
11 {
12     int i;
13     for(i=0;i<20; i++)
14     printf("%6d",p(i));
15     
16     getchar();
17     return 0;   
18 }

读取文件内数并比较大小

 1 #include<stdio.h> 4 int max(p,n)
 5 {
   int *p,n;
6 int m,*pend; 7 m=*p; 8 pend=p+n; 9 for(p=p+1;p<pend;p++){ 10 if(*p>m) m=*p; 11 } 12 return m; 13 } 14 15 int main() 16 { 17 FILE *fin,*fout; 18 int i,num[10]; 19 fin=fopen("pro3.in","r"); 20 fout=fopen("pro3.out","w"); 21 22 for(i=0;i<10;i++){ 23 fscanf(fin,"%d",&num[i]); 24 } 25 for(i=0;i<10;i++){ 26 fprintf(fout,"%d ",num[i]); 27 } 28 fprintf(fout,"\n%d",max(num,10)); 29 30 fclose(fin); 31 fclose(fout); 32 return 0; 33 }

读文件

 1 #include<stdio.h>
 2 
 3 int main()
 4 {
 5     FILE *fin,*fout;
 6     int i,num[10],*p;
 7     fin=fopen("pro4.in","r");
 8     fout=fopen("pro4.out","w");
 9     p=num;
10     for(i=0;i<10;i++)
11       fscanf(fin,"%d",p++);  
12     p=num;     
13     for(i=0;i<10;i++)
14       fprintf(fout,"%d ",*p++); 
15 
16     fclose(fin);
17     fclose(fout);
18 return 0;
19 }

读文件

 1 #include<stdio.h>
 2 int main()
 3 {
 4     FILE *fout;
 5     int a,*p;
 6    
 7     fout=fopen("pro1.out","w");
 8     a=5;
 9     p=&a;
10     fprintf(fout,"%d %d ",&a,p);
11     fprintf(fout,"%d %d ",*(&a),*p);
12     fprintf(fout,"%d ",a++);
13     fprintf(fout,"%d ",(*p)++);
14     fprintf(fout,"%d ",p);
15     fprintf(fout,"%d ",*p++);
16     fprintf(fout,"%d ",p);
17     fclose(fout);
18 return 0;
19 }

不知道

#include<stdio.h>
#include<string.h>
struct student
{char name[20];
 int chi;
      }stu,st[3];
int main()
{
    FILE *fin,*fout;
    int i,j;
    fin=fopen("pro1.in","r");
    fout=fopen("pro1.out","w");
    for(i=0;i<3;i++)
       fscanf(fin,"%s%d",&st[i].name,&st[i].chi);
    for(i=0;i<3;i++)
       for(j=i+1;j<3;j++)
         if(st[i].chi<st[j].chi)
           {strcpy(stu.name,st[i].name);
            stu.chi=st[i].chi;
            strcpy(st[i].name,st[j].name);
            st[i].chi=st[j].chi;
            strcpy(st[j].name,stu.name);
            st[j].chi=stu.chi; }          
    for(i=0;i<3;i++)
       fprintf(fout,"%s\n",st[i].name);
    fclose(fin);
    fclose(fout);
return 0;
}

不知道

#includ<stdio.h>
int main()
{   
    FILE *fout;
    fout=fopen("pro1.out","w");  
    int a=5;
    int *p;
    *p=a;
    fprintf(fout,"&a=%d",&a);
    fprintf(fout,"p=%d",p);
    fprintf(fout,"*p=%d",*p);
    fprintf(fout,"*(&a)=%d",*(&a));
    fprintf(fout,"a++=%d",a++);
    fprintf(fout,"(*p)++=%d",(*p)++);
    fprintf(fout,"*p++=%d",*p++);
    return 0;
    
    }
#include<stdio.h>
int main()
{
    FILE *fout;
    int a,*p;
   
    fout=fopen("pro1.out","w");
    a=5;
    p=&a;
    fprintf(fout,"%d %d ",&a,p);
    fprintf(fout,"%d %d ",*(&a),*p);
    fprintf(fout,"%d ",a++);
    fprintf(fout,"%d ",(*p)++);
    fprintf(fout,"%d ",p);
    fprintf(fout,"%d ",*p++);
    fprintf(fout,"%d ",p);
    fclose(fout);
return 0;
}

汉诺塔

/* Note:Your choice is C IDE */
#include <stdio.h>
void move(int n,int x,int y,int z)
{  
    if(n==1)
    printf("%c->%c\n",x,z);
    else
    {
        move(n-1,x,z,y);
        printf("%c->%c\n",x,y);
        move(n-1,y,x,z);
    }
}
void main()
{
  int h;
  printf("\n input number: \n");
  scanf("%d",&h);
  printf("the step to moving %2d diskes:\n",h);
  move(h,'a','b','c');
}
/* Note:Your choice is C IDE */
#include <stdio.h> 
 main()
{
  FILE  *fin,*fout;
  int h;
  void move(int n,int x,int y,int z);
  fin=fopen("p1.in","r");
  fout=fopen("p1.out","w");
  fprintf(fin,"\n input number: \n");
  fscanf(fin,"%d",&h);
  fprintf(fout,"the step to moving %2d diskes:\n",h);
  move(h,'a','b','c');
}
void move(int n,int x,int y,int z)
{  
    FILE *fout;
    fout=fopen("p1.out","a+");
    if(n==1)
    fprintf(fout,"%c->%c\n",x,z);
    else
    {
        move(n-1,x,z,y);
        fprintf(fout,"%c->%c\n",x,y);
        move(n-1,y,x,z);
    }
}

 

posted on 2017-09-10 21:09  hujun1992  阅读(208)  评论(0编辑  收藏  举报

导航