第三章

3.1

#include <stdio.h>

int binarySearch_1(int n[], int number, int d);
int binarySearch_2(int n[], int number, int d);

int main(){
    int n[] = {1,5,2,8,10,56,24,11,7};
    int result = binarySearch_1(n, 9, 8);
    printf("The Version One is %d\n", result);
    result = binarySearch_2(n, 9, 8);
    printf("The Version Two is %d\n", result + 1);
    return 0;
}

int binarySearch_1(int n[], int number, int d){
    int low = 0;
    int high = number - 1;
    int mid = 0;
    while(low <= high ){
        mid = (low + high) / 2;
        if(d < n[mid]){
            high = mid - 1;
        } else if(d > n[mid]){
            low = mid + 1;
        } else {
            return mid;
        }
    }
    return -1;
}
int binarySearch_2(int n[], int number, int d){
    int low, high, mid;
    low = 0;
    high = number - 1;
    mid = (low + high) / 2;
    while(low <= high && d != n[mid]){
        if(d < n[mid]){
            high = mid - 1;
        } else {
            low = mid + 1;
        }
        mid = (low + high) / 2;
    }
    if(d == n[mid]){
        return mid;
    } else {
        return -1;
    }
}

3.2

#include <stdio.h>


void escape(char s[], char t[]);
void escapeOpposite(char s[], char t[]);

int main(){
    char s[128];
    char t[128];
    printf("Please input a string: ");
    fgets(s, 127, stdin);
    escape(s, t);
    printf("The converted string is %s\n", t);
    escapeOpposite(t, s);
    printf("The original string is %s", s);
    return 0;
}

void escape(char s[], char t[]){
    int i = 0;
    int j = 0;
    for(i = 0; s[i]!='\0'; i++){
       switch (s[i])
       {
           case '\t':
               t[j++] = '\\';
               t[j++] = 't';
               break;
            case '\n':
               t[j++] = '\\';
               t[j++] = 'n';
                break;
           default:
               t[j++] = s[i];
               break;
       }
    }
    t[j] = '\0';
}
void escapeOpposite(char s[], char t[]){
    int i,j;
    i = j = 0;
    for(i = 0; s[i] != 0; i++){
        if(s[i] == '\\'){
            i += 1;
            switch (s[i])
            {
                case 't':
                    t[j++] = '\t';
                    break;
                case 'n':
                    t[j++] = '\n';
                    break;
                default:
                    t[j++] = '\\';
                    t[j++] = s[i];
                    break;
            }
        } else {
            t[j++] = s[i];
        }
    }
    t[j] = '\0';
}

3.3

void expand(char s1[], char s2[]){
    char c;
    int i, j;
    i = j = 0;
    while((c = s1[i++]) != '\0'){ /*fetch a char from s1[]*/
        if(s1[i] == '-' && s[i+1] >= c){
            i++;
            while(c < s1[i]){       /* expand shorthand */
                s2[j++] = c++;
            } else {
                s2[j++] = c;
            }
        }
    }
    s2[j] = '\0';
}

3.4

#include <stdio.h>
#include <string.h>
#include <limits.h>

#define abs(x) ((x) < 0 ? -(x) : (x))

void itoa(int n, char s[]);
void reverse(char s[]);

int main(){
    int n = INT_MIN;
    char s[128];
    itoa(n, s);
    printf("The result is %s\n", s);
    return 0;
}


/* itoa: convert n to characters in s - modified */
void itoa(int n, char s[]){
    int i, sign;
    sign = n;
    i = 0;
    do{
        s[i++] = abs(n % 10) + '0';
    }while((n /= 10) != 0);
    if(sign < 0){
        s[i++] = '-';
    }
    s[i] = '\0';
    reverse(s);
}
void reverse(char s[]){
    int c, i, j;
    for(i = 0, j = strlen(s) - 1; i < j; i++, j--){
        c = s[i];
        s[i] = s[j];
        s[j] = c;
    }
}

3.5

#include <stdio.h>
#include <string.h>
#include <limits.h>

#define abs(x) ((x) < 0 ? -(x) : (x))

void itob(int n, char s[], int b);
void reverse(char s[]);

int main(){
    int n;
    int b;
    char s[128];
    printf("Please input a number and a base: ");
    scanf("%d%d", &n, &b);
    itob(n, s, b);
    printf("The result is %s\n", s);
    return 0;
}

void reverse(char s[]){
    int c, i, j;
    for(i = 0, j = strlen(s) - 1; i < j; i++, j--){
        c = s[i];
        s[i] = s[j];
        s[j] = c;
    }
}

void itob(int n, char s[], int b){
    int i = 0;
    int sign = n;
    do{
        s[i++] = abs(n % b) < 9 ? (abs(n % b) + '0') : (abs(n % b) + 'a' - 10);
    }while((n /= b) != 0 );
    if(sign < 0){
        s[i++] = '-';
    }
    s[i] = '\0';
    reverse(s);
}

3.6

#include <stdio.h>
#include <string.h>
#include <limits.h>

#define abs(x) ((x) < 0 ? -(x) : (x))

void itoa(int n, char s[], int minWidth);
void reverse(char s[]);

int main(){
    int number;
    char s[128];
    int minWidth;
    printf("Please input a number and minimum width: ");
    scanf("%d%d", &number, &minWidth);
    itoa(number, s, minWidth);
    printf("The result is %s\n", s);
    return 0;
}


/* itoa: convert n to characters in s - modified */
void itoa(int n, char s[], int minWidth){
    int i, sign;
    sign = n;                           /* record sign */
    i = 0;
    do{                                 /* generate digits in reverse order */
        s[i++] = abs(n % 10) + '0';     /* get next digit */
    }while((n /= 10) != 0);             /* delete it */
    if(sign < 0){
        s[i++] = '-';
    }
    while(i < minWidth){                /* pad with blanks */
        s[i++] = ' ';
    }
    s[i] = '\0';
    reverse(s);
}
void reverse(char s[]){
    int c, i, j;
    for(i = 0, j = strlen(s) - 1; i < j; i++, j--){
        c = s[i];
        s[i] = s[j];
        s[j] = c;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值