码迷,mamicode.com
首页 > 其他好文 > 详细

实验五

时间:2019-12-16 11:35:11      阅读:82      评论:0      收藏:0      [点我收藏+]

标签:john   oci   return   delete   --   fun   mic   The   gets   

// Õâ¸ö³ÌÐòÓÃÓÚ¹Û²ìÊý×éÖеÄÒ»×éÊý¾ÝÔªËØÔÚÄÚ´æÖÐÊÇ·ñÊÇÁ¬Ðø´æ·ÅµÄ
// ÒÔ¼°Êý×éÔªËصÄÖ±½Ó·ÃÎÊÓë¼ä½Ó·ÃÎÊ 
#include <stdio.h> 
#include <stdlib.h> 
const int N=3;
int main() {
    char a[N] = {Y, E, S}; // ¶¨ÒåһάÊý×éa£¬°üº¬3¸öÕûÐÍÊý¾Ý£¬²¢¶ÔÆä³õʼ»¯£¬3¸öÔªËسõʼֵ·Ö±ðÊÇ1£¬2£¬3 
    int i;
    
    // ÒÔ"µØÖ·£ºÖµ"µÄÐÎʽ´òÓ¡Êý×éaÖÐÿһ¸öÊý¾ÝÔªËصĵØÖ·£¬ºÍÊý¾ÝÔªËØÖµ
    printf("ͨ¹ýÊý×éÃû¼°Ï±êÖ±½Ó·ÃÎÊÊý×éÔªËØ:\n");
    for(i=0; i<N; i++)
        printf("%d: %c\n", &a[i], a[i]);
        
    // ÒÔ"µØÖ·£ºÖµ"µÄÐÎʽ´òÓ¡Êý×éaÖÐÿһ¸öÊý¾ÝÔªËصĵØÖ·£¬ºÍÊý¾ÝÔªËØÖµ
    printf("ͨ¹ýµØÖ·¼ä½Ó·ÃÎÊÊý×éÔªËØ:\n");
    for(i=0; i<N; i++)
        printf("%d: %c\n", a+i, *(a+i));
        
    system("pause");

    return 0;
} 

技术图片

// Á·Ï°£ºÊ¹Óöþ·Ö²éÕÒ£¬ÔÚÒ»×éÓÐÐòÔªËØÖвéÕÒÊý¾ÝÏî
//  ÐβÎÊÇÊý×飬ʵ²ÎÊÇÊý×éÃû 
#include  <stdio.h>
#include <stdlib.h> 
const int N=5;

int binarySearch(int x[], int n, int item); // º¯ÊýÉùÃ÷ 

int main() {
    int a[N]={2,7,19,45,66};
    int i,index, key;
    
    printf("Êý×éaÖеÄÊý¾Ý:\n");
    for(i=0;i<N;i++)
       printf("%d ",a[i]);
    printf("\n");
    
    printf("ÊäÈë´ý²éÕÒµÄÊý¾ÝÏî: ");
    scanf("%d", &key);
    
    // µ÷Óú¯ÊýbinarySearch()ÔÚÊý×éaÖвéÕÒÖ¸¶¨Êý¾ÝÏîitem,²¢·µ»Ø²éÕÒ½á¹û¸øindex 
    // ²¹×ã´úÂë¢Ù 
    index=binarySearch(a,N,key);
    
    if(index>=0) 
        printf("%dÔÚÊý×éÖУ¬Ï±êΪ%d\n", key, index);
    else
        printf("%d²»ÔÚÊý×éÖÐ\n", key); 
   
   system("pause");
   return 0;
}

//º¯Êý¹¦ÄÜÃèÊö£º
//ʹÓöþ·Ö²éÕÒËã·¨ÔÚÊý×éxÖвéÕÒÌض¨Öµitem£¬Êý×éx´óСΪn 
// Èç¹ûÕÒµ½£¬·µ»ØÆäϱê 
// Èç¹ûûÕÒµ½£¬·µ»Ø-1 
int binarySearch(int x[], int n, int item) {
    int low, high, mid;
    
    low = 0;
    high = n-1;
    
    while(low <= high) {
        mid = (low+high)/2;
        
        if (item == x[mid])
            return mid;
        else if(item<x[mid])
            high = mid - 1;
        else
            low = mid + 1;
    }
    
    return -1;
}

技术图片

#include  <stdio.h>
#include <stdlib.h> 
#define  N  10
int fun(int *a,int m)
{ 
    int low = 0, high = N-1, mid;
    /*************ERROR**************/
      while(low <= high)
      {  
        mid = (low+high)/2;
        /*************ERROR**************/
        if(m < *(a+mid))
             high = mid-1;
        /*************ERROR**************/
        else if(m > *(a+mid))
            low = mid+1;
        else  
            return(mid);
      }
      return(-1);
}

int main()
{  
    int i,a[N]={-3,4,7,9,13,24,67,89,100,180},k,m;
       printf("aÊý×éÖеÄÊý¾ÝÈçÏÂ:\n");
       for(i=0;i<N;i++)
       printf("%d ",a[i]);
       printf("\nEnter m: \n"); 
       scanf("%d",&m);
    /*************ERROR**************/
       k = fun(a,m);
       if (k>=0) 
       printf("m=%d,index=%d\n",m,k);
       else 
       printf("Not be found!\n");
    
    system("pause");
       return 0;
}

技术图片

#include <stdio.h>
#include <string.h>
#include <stdlib.h> 
void selectSort(char str[][20], int n ); // º¯ÊýÉùÃ÷£¬ÐβÎstrÊǶþάÊý×éÃû 
int main() {
    char name[][20] = {"John", "Alex", "Joseph", "Taylor", "George"};
    int i;
    
    printf("Êä³ö³õʼÃûµ¥:\n");
    for(i=0; i<5; i++)
        printf("%s\n", name[i]);
        
    selectSort(name, 5);  // µ÷ÓÃÑ¡Ôñ·¨¶ÔnameÊý×éÖеÄ×Ö·û´®ÅÅÐò
    
    printf("°´×ÖµäÐòÊä³öÃûµ¥:\n");
    for(i=0; i<5; i++)
        printf("%s\n", name[i]);
    
    system("pause");
    return 0;
} 

// º¯Êý¶¨Òå
// º¯Êý¹¦ÄÜÃèÊö£ºÊ¹ÓÃÑ¡Ôñ·¨¶Ô¶þάÊý×éstrÖеÄn¸ö×Ö·û´®°´×ÖµäÐòÅÅÐò 
void selectSort(char str[][20], int n) {
    // ²¹×ã´úÂë
    // ¡Á¡Á¡Á 
    int i,j,k;
    char temp[20]; 
    for(i=0;i<n-1;i++)
        k=i;
            for(j=i+1;j<n;j++)
            {
                if(strcmp(str[k],str[j])>0)
                    k=j;
            }
            if(k!=i)
            {
                strcpy(temp,str[i]);
                strcpy(str[i],str[k]);
                strcpy(str[k],temp);
            }      
}

技术图片

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
void  fun(char *a) {
    /*****ERROR********/
    int i=0; 
    char *p = a;
    /****ERROR***/
    while(*p && *p == *) { 
        a[i] = *p;
        i++;
        p++; 
    } 
    while(*p) { 
    /******ERROR*******/
        if(*p != *)  { 
            a[i] = *p;
            i++; 
        } 
        p++; 
    }
    /******ERROR*******/
    a[i] = 0; 
  
}

int main() {
      char s[81];
      printf("Enter a string :\n");
      gets(s);
      /***ERROR******/
      fun(s);
      printf("The string after deleted:\n");
      puts(s);
      
      system("pause");
    return 0; 
}

技术图片

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void fun(char *a) {
    /**ERROR******/
    int i=0;
    char *t = a, *f = a;
    char *q = a;
     
    while(*t)
        t++;
    t--;
    
    while(*t == *)
        t--;
        
    while(*f == *)
        f++;
    /***ERROR***/
    while (q<f) { 
        a[i] = *q;
        q++;
        i++; 
    } 
    while (q<t) {
        /***ERROR**/
        if(*q != *) { 
            a[i] = *q;
            i++; 
        } 
        q++; 
    } 
    
    while (*q) { 
        a[i] = *q;
        i++;
        q++; 
    } 
    /**ERROR**/
    a[i]=0;
}
int main () {
    char s[81];
    printf("Entre a string:\n");
    gets(s);
    /**ERROR**/
    fun(s);
    printf("The sting after deleted:\n");
    puts(s);
    
    system("pause");
    return 0;
}

技术图片

实验五

标签:john   oci   return   delete   --   fun   mic   The   gets   

原文地址:https://www.cnblogs.com/rsndm/p/12048105.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!