Chinaunix首页 | 论坛 | 博客
  • 博客访问: 202527
  • 博文数量: 22
  • 博客积分: 641
  • 博客等级: 上士
  • 技术积分: 756
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-27 00:41
文章分类

全部博文(22)

文章存档

2014年(1)

2013年(1)

2012年(20)

分类: C/C++

2012-04-11 20:26:17


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <malloc.h>
  4. typedef struct Lnode{
  5.     int data;
  6.     struct Lnode *link;
  7. }Lnode,*Linklist;
  8. //主要是越界访问的问题,编译器没报错且能正常运行
  9. void test()
  10. {
  11.     char p[10],q[10];
  12.     int i;
  13.     for(i=0;i<10;i++)
  14.     {
  15.         p[i]='a';
  16.     }
  17.     strcpy(q,p);
  18.     puts(q);
  19. }
  20. void test1(char *ch)
  21. {
  22.     char string[10];
  23.     if(strlen(ch)<=10)
  24.     {
  25.         strcpy(string,ch);
  26.     }
  27.     puts(string);
  28. }
  29. void test2()
  30. {
  31.     char string[10];
  32.     char *str="0123456789";
  33.     strcpy(string,str);
  34.     puts(string);
  35. }
  36. //switch case /注意break的用法;
  37. int func(int a)
  38. {
  39.     int b;
  40.     switch(a)
  41.     {
  42.         case 1: b=30;
  43.         case 2: b=20;
  44.         case 3: b=16;
  45.         default: b=0;
  46.     }
  47.     return b;
  48. }
  49. //两个数组从末尾往前比较相同的个数
  50. int compare_array(int len1,int array1[],int len2,int array2[])
  51. {
  52.     int i;
  53.     int j;
  54.     int len;
  55.     int flag;
  56.     len=(len1<len2)? len1:len2;
  57.     flag=len;
  58. /*    if(len1>len)
  59.         {
  60.             for(i=(len-1);i>=0;i--)
  61.             {
  62.                 if(array1[len1-len+i]==array2[i])
  63.                     --flag;
  64.             }
  65.         }
  66.     if(len2>len)
  67.         {
  68.             for(j=(len-1);j>=0;j--)
  69.             {
  70.                 if(array2[len2-len+j]==array1[j])
  71.                 {
  72.                     --flag;
  73.                 }
  74.             }
  75.         }
  76.     if(len1==len2)
  77.     {
  78.         for(j=len-1;j>=0;j--)
  79.         {
  80.             if(array2[j]==array1[j])
  81.             {
  82.                 --flag;
  83.             }
  84.         }
  85.     }*/
  86.     for(i=1;i<=len;i++)
  87.     {
  88.         if(array1[len1-i]==array2[len2-i])
  89.             flag--;
  90.     }
  91.     return flag;
  92.     
  93. }
  94. //数组循环报数输出
  95. void array_iterate1(int len,int input_array[],int m,int output_array[])
  96. {
  97.     int i;
  98.     int k=1;
  99.     int j=0;
  100.     int s;
  101.     int n=m-1;
  102.     Linklist p=(Linklist)malloc(sizeof(Lnode));
  103.     Linklist pre;
  104.     Linklist curr;
  105.     p->data=input_array[0];
  106.     printf("%d.",p->data);
  107.     p->link=p;
  108.     curr=p;
  109.     printf("\ninitlink:");
  110.     for(i=1;i<len;i++)
  111.     {
  112.         Linklist newnode=(Linklist)malloc(sizeof(Lnode));
  113.         newnode->data=input_array[i];
  114.         printf("%d.",newnode->data);
  115.         newnode->link=curr->link;
  116.         curr->link=newnode;
  117.         curr=newnode;
  118.     }
  119.     pre=curr;
  120.     while(n--)
  121.     {
  122.         pre=p;
  123.         p=p->link;
  124.     }
  125.     while(len--)
  126.     {
  127.         for(s=k-1;s--;pre=p,p=p->link) ;
  128.         k=p->data;
  129.         output_array[j++]=k;
  130.         pre->link=p->link;
  131.         free(p);
  132.         p=pre->link;
  133.         
  134.     }
  135.     printf("\n");
  136.     printf("changedlink:");
  137.     for(i=0;i<5;i++)
  138.         printf("%d.",output_array[i]);
  139.     printf("\n");
  140.     
  141. }
  142. //比分计算
  143. int cal_score(int score[],int grade[],int n)
  144. {
  145.     int i;
  146.     int sum1=0,sum2=0;
  147.     int num2=0,num1=0;
  148.     for(i=0;i<n;i++)
  149.     {
  150.         if(grade[i]==2)
  151.         {
  152.             sum2=sum2+score[i];
  153.             num2++;
  154.         }
  155.         if(grade[i]==1)
  156.         {
  157.             sum1=sum1+score[i];
  158.             num1++;
  159.         }
  160.     }
  161.     if(num2==0)
  162.         return (float)sum1/num1;
  163.     else
  164.         return (float)sum1/num1*0.6+(float)sum2/num2*0.4;
  165. }
  166. //排序左右递减
  167. void sort(int input[],int n,int output[])
  168. {
  169.     int i,p,j,k,w,v,l;
  170.     for(i=(n-1);i>=0;i--)
  171.     {
  172.         for(j=0;j<i;j++)
  173.         {
  174.             if(input[j]>input[j+1])
  175.                 {
  176.                     p=input[j];
  177.                     input[j]=input[j+1];
  178.                     input[j+1]=p;
  179.                 }
  180.         }
  181.     }
  182.     for(i=0;i<n;i++)
  183.         printf("%d..",input[i]);
  184.     printf("\n");
  185.         k=n/2-1;
  186.         v=n/2+1;
  187.         w=n-2;
  188.         output[n/2]=input[n-1];
  189.         printf("%d.\n",input[n-1]);
  190.         if(n%2!=0)
  191.         {
  192.             l=n/2;
  193.             while(l--)
  194.             {
  195.                 output[k--]=input[w--];
  196.                 output[v++]=input[w--];
  197.             }
  198.         }
  199.         if(n%2==0)
  200.         {
  201.             l=n/2-1;
  202.             while(l--)
  203.             {
  204.                 output[k--]=input[w--];
  205.                 output[v++]=input[w--];
  206.             }
  207.             output[0]=input[0];
  208.         }
  209.         for(i=0;i<n;i++)
  210.             printf("%d..",output[i]);
  211.         printf("\n");
  212. }
  213. //简单选择排序
  214. void selectsort(int sort[],int nlength)
  215. {
  216.     int i,j;
  217.     int buff;
  218.     for(i=0;i<nlength;i++)
  219.     {
  220.         for(j=(nlength-1);j>i;j--)
  221.         {
  222.             if(sort[i]>sort[j])
  223.             {
  224.                 buff=sort[i];
  225.                 sort[i]=sort[j];
  226.                 sort[j]=buff;
  227.             }
  228.         }
  229.     }
  230.     printf("selectsort:");
  231.     for(i=0;i<nlength;i++)
  232.         printf("%d..",sort[i]);
  233.     printf("\n");
  234. }
  235. //冒泡排序
  236. void bullsort(int sort[],int nlength)
  237. {
  238.     int i,j;
  239.     int buff;
  240.     i=nlength-1;
  241.     for(;i>=0;i--)
  242.     {
  243.         for(j=0;j<=i;j++)
  244.         {
  245.             if(sort[j]>sort[j+1])
  246.             {
  247.                 buff=sort[j];
  248.                 sort[j+1]=sort[j];
  249.                 sort[j]=buff;
  250.             }
  251.         }
  252.     }
  253.     printf("bullsort:");
  254.     for(i=0;i<nlength;i++)
  255.         printf("%d..",sort[i]);
  256.     printf("\n");
  257. }
  258. //直接插入排序
  259. void insertsort(int sort[],int nlength)
  260. {
  261.     int i,j;
  262.     int buff;
  263.     for(i=1;i<nlength;i++)
  264.     {
  265.         if(sort[i]<sort[i-1])
  266.         {
  267.             buff=sort[i];
  268.             for(j=i-1;buff<sort[j];j--)
  269.                 sort[j+1]=sort[j];
  270.             sort[j+1]=buff;
  271.         }
  272.     }    
  273.     printf("insertsort:");
  274.     for(i=0;i<nlength;i++)
  275.         printf("%d..",sort[i]);
  276.     printf("\n");
  277. }
  278. //希尔排序--直接插入的改进
  279. void shellinsert(int dk,int sort[],int nlength)
  280. {
  281.     int i,j;
  282.     int buff;
  283.     for(i=dk;i<nlength;i++)
  284.     {
  285.         if(sort[i]<sort[i-dk])
  286.         {
  287.             buff=sort[i];
  288.             for(j=i-dk;j>=0&&buff<sort[j];j--)
  289.                 sort[j+dk]=sort[j];
  290.             sort[j+dk]=buff;
  291.         }
  292.     }    
  293. }
  294. void shellsort(int sort[],int nlength,int alength,int delta[])
  295. {
  296.     int i;
  297.     for(i=0;i<alength;i++)
  298.     {
  299.         shellinsert(delta[i],sort,nlength);
  300.     }
  301.     printf("shellsort:");
  302.     for(i=0;i<nlength;i++)
  303.         printf("%d..",sort[i]);
  304.     printf("\n");
  305. }
  306. //快速排序
  307. int patition(int low,int high,int sort[])
  308. {
  309.     int pivotkey;
  310.     int buff;
  311.     buff=sort[low];
  312.     pivotkey=sort[low];
  313.     while(low<high)
  314.     {
  315.         while(low<high&&sort[high]>=pivotkey)
  316.         {
  317.             --high;
  318.         }
  319.         sort[low]=sort[high];
  320.         while(low<high&&sort[low]<=pivotkey)
  321.         {
  322.             ++low;
  323.         }
  324.         sort[high]=sort[low];
  325.     }
  326.     sort[low]=buff;
  327.     return low;
  328. }
  329. void quiksort(int low,int high,int sort[],int nlength)
  330. {
  331.     int pivoloc;
  332.     int i;
  333.     if(low<high)
  334.     {
  335.         pivoloc=patition(low,high,sort);
  336.         quiksort(low,high-1,sort,nlength);
  337.         quiksort(pivoloc+1,high,sort,nlength);
  338.     }
  339. }
  340. int main()
  341. {
  342.     int array1[3]={1,2,3};
  343.     int array2[5]={1,1,1,2,3};
  344.     int input_array[5]={2,1,3,5,4};
  345.     int output_array[5];
  346.     int input[6]={2,1,3,4,6,5};
  347.     int output[6];
  348.     char ch[10]="huangliglo";
  349.     int score[5]={3,8,6,7,8};
  350.     int juge[5]={1,2,1,2,2};
  351.     int sort1[7]={1,7,5,2,4,9,6};
  352.     int sort2[7]={2,3,16,8,9,34,21};
  353.     int sort3[7]={78,43,21,34,56,76,2};
  354.     int delta[2]={3,1};
  355.     int a[3];
  356.     int i;
  357.     int *p,*q;
  358.     for(i=0;i<3;i++)
  359.         a[i]=i;
  360.     p=a;
  361.     q=&a[2];
  362.     int **b[3][4];
  363.     printf("%d\n",a[q-p]);
  364.     printf("%d\n",compare_array(3,array1,5,array2));
  365.     test();
  366.     test1(ch);
  367.     test2();
  368.     printf("%d\n",func(1));
  369.     printf("%d\n",sizeof(b));
  370.     array_iterate1(5,input_array,2,output_array);
  371.     printf("%d\n",cal_score(score,juge,5));
  372.     sort(input_array,5,output_array);
  373.     sort(input,6,output);
  374.     selectsort(input,6);
  375.     bullsort(input,6);
  376.     insertsort(sort1,7);
  377.     shellsort(sort2,7,2,delta);
  378.     quiksort(0,6,sort3,7);
  379.     printf("quiksort:");
  380.     for(i=0;i<7;i++)
  381.         printf("%d..",sort3[i]);
  382.     printf("\n");
  383. //    quiksort(0,6,sort1,7);
  384.     return 0;
  385. }

点击(此处)折叠或打开

  1. 2
  2. 0
  3. aaaaaaaaaa
  4. huangliglo
  5. 0123456789
  6. 0
  7. 48
  8. 2.
  9. initlink:1.3.5.4.
  10. changedlink:1.3.2.4.5.
  11. 5
  12. 1..2..3..4..5..
  13. 5.
  14. 2..4..5..3..1..
  15. 1..2..3..4..5..6..
  16. 6.
  17. 1..3..5..6..4..2..
  18. selectsort:1..2..3..4..5..6..
  19. bullsort:1..2..3..4..5..6..
  20. insertsort:1..2..4..5..6..7..9..
  21. shellsort:2..3..8..9..16..21..34..
  22. quiksort:2..21..34..43..56..76..78..

阅读(2207) | 评论(0) | 转发(1) |
0

上一篇:c语言中struct小结

下一篇:linux目录结构

给主人留下些什么吧!~~