美文网首页
高级语言程序设计实验(双数号题)

高级语言程序设计实验(双数号题)

作者: anyaliang | 来源:发表于2017-05-11 14:45 被阅读0次

    实验二

    #include<stdio.h>
    
    int main() {
        long n = 0, high = 0, low = 0, change = 0;
        int flag1 = 0, flag2 = 0; //flag for odd/even number
    
        scanf("%lx",&n);
        printf("n: %#lx\n",n); //输入一个长整型数字
    
        high = (n>>16) & 0x0000ffff; //move high 16 bits to low
        low = n & 0x0000ffff;
        flag1 = (high%2)? 1: 0;
        flag2 = (low%2)? 1: 0;
        printf("0为偶数,1为奇数\n") ;
        printf("high: %#lx ------%d\nlow: %#lx------%d\n",high,flag1,low,flag2);
        //判断高十六位和第十六位的奇偶
    
        change = ((n&0x00ff0000)>>8) + ((n&0x0000ff00)<<8) + ((n&0x000000ff)<<24) + ((n&0xff000000)>>24) ;
        printf("after change: %#lx\n",change) ;
    
        return 0 ;
    }
    

    实验四

    #include <stdio.h>
    
    int main(void) {
        int input_no = 0;
        int split[4] = {10};
        int i = 0;
        int order[4] = {10};
        int tmp = 0;
    
        printf("enter a number between 1000 and 9999: ");
    
        scanf("%d", &input_no);
    
        while(input_no<1000||input_no>9999) {
            printf("invalid input, enter your number again:");
            scanf("%d", &input_no);
        } //不在范围内的时候报错
    
        while(input_no) {
            split[i] = input_no % 10;
            input_no /= 10;
            i++;
        } //将该数字拆分成位,存在数组里
    
        printf("reverse number is: ");
        for(int i=0; i<4; i++) {
            printf("%d", split[i]);
        } //反序打印数组里的数字
    
        printf("\n");
    
        for(int m=1; m<4; m++) {
            for(int n=0; n<4-m; n++) {
                if(split[n]>split[n+1])
                {
                    tmp = split[n];
                    split[n] = split[n+1];
                    split[n+1] = tmp;
                }
            }
        } //冒泡排序,降序
    
        printf("the smallest number can produce is: ");
        for(int f=0; f<4; f++) {
            printf("%d", split[f]);
        }//打印排序后的数组,从大到小,从而构建“最大数字”
    
        printf("\n");
    
        printf("the largest number can produce is: ");
        for(int p=3; p>=0; p--) {
            printf("%d", split[p]);
        }//反序打印,构建最小数字
    
        printf("\n");
    
        return 0;
    }
    

    实验六

    #include <stdio.h>
    
    void bubble_sort(int arr[], int total);
    void selection_sort(int a[], int total);
    void Merge(int a[], int b[], int sorted[], int alen, int blen);
    
    
    int main(void){
        int arr_1[10] = {0};
        int arr_2[10] = {0};
        int tmp1 = 0, tmp2 = 0;
        int sorted[20] = {0};
        int max = 0;
    
        printf("enter first array: \n");
        for(int i=0; i<10; i++){
            scanf("%d ", &arr_1[i]);
        }
        printf("enter another array: \n");
        for(int i=0; i<10; i++){
            scanf("%d ", &arr_2[i]);
        }
    
        bubble_sort(arr_1, 10);
        selection_sort(arr_2, 10);
        Merge(arr_1, arr_2, sorted, 10, 10);
    
        for(int j=0; j<20; j++){
            printf("%d ", &sorted[j]);
        }
    
        return 0;
    }
    
    void bubble_sort(int arr[], int total){
        int tmp = 0;
        for(int i=0; i<total-1; i++){
            for(int j=0; j<total-1-i; j++){
                if(arr[j+1]<arr[j]){
                    tmp = arr[j+1];
                    arr[j+1] = arr[j];
                    arr[j] = tmp;
                }
            }
        }
    }
    
    void selection_sort(int a[], int total){
        int max_index = 0, tmp = 0;
        for(int i=0; i<total-1; i++){
            max_index = i;
            for(int j=i+1; j<total; j++){
                if(a[max_index]>a[j]) max_index = j;
    
                tmp = a[max_index];
                a[max_index] = a[i];
                a[i] = tmp; //when ends a line, exchage between a[max] and **a[i]**
            }
        }
    }
    
    void Merge(int a[], int b[], int sorted[], int alen, int blen){
        int i = 0, j = 0;
        while(i<alen && j<blen){
            sorted[i+j] = a[i]>b[j]? a[i++] : b[j++];
        }
    
        while(i<alen){
            sorted[i+j] = a[i++];
        }
    
        while(j<blen){
            sorted[i+j] = b[j++];
        }
    }
    

    实验八

    #include <stdio.h>
    
    bool Like[5][5] = {
        {0, 0, 1, 1, 0},
        {1, 1, 0, 0, 1},
        {0, 1, 1, 0, 1},
        {0, 0, 0, 1, 0},
        {0, 1, 0, 0, 1}
    };
    int book[5] = {0}; //0 for not attr
    int n = 0;
    
    void Try(int i);
    
    int main(void){
       Try(0);
       return 0;
    }
    
    void Try(int i){
        int j = 0, k = 0;
        for(j=0; j<5; j++){
            if(Like[i][j]&& book[j]==0){
                book[j] = i + 1; //give book_j to ith person
                if(i==4){
                    n++;
                    printf("方案%d:\n", n);
                    for(k=0; k<5; k++){
                        printf("第%d本书分给%c\n", k, (char)(book[k]-1+'A'));
                    }
                    printf("\n");
                }
                else
                    Try(i + 1);
                book[j] = 0;
            }
        }
    }
    

    实验十

    #include <stdio.h>
    #include <string.h>
    /*
     * 默认6786xxxx-->
     * 结束时间hour:min:sec
     */
    
    int TeleAttr(char *no);
    
    int main(void) {
        int loc = 4;
        float pr = 0.000;
    
        struct Time {
            int hour;
            int min;
            int second;
        };
    
        struct Tele_Info {
            struct Time ymd; //year month date
            char call_no[50]; // 主叫码
            char called_no[50]; //被叫码
            struct Time start_time; //通话开始时间
            struct Time time_len; //通话时长
            float price; //话单价格
        }; //declaration of struct
    
    
    
        struct Tele_Info p[6]; //declaration of struct array
        struct Tele_Info tele;
        struct Tele_Info tmp;
    
        for(int i=0; i<6; i++) {
            scanf("%d-%d-%d ", &tele.ymd.hour, &tele.ymd.min, &tele.ymd.second);
            scanf("%s %s", tele.call_no, tele.called_no); //ymd
    
            scanf("%d:%d:%d", &tele.start_time.hour, &tele.start_time.min, &tele.start_time.second);
            scanf("%d:%d:%d", &tele.time_len.hour, &tele.time_len.min, &tele.time_len.second);
    
            pr = 0;
    
            loc = TeleAttr(tele.called_no);
            switch(loc){
                case 0: pr = tele.time_len.hour * 6 + (tele.time_len.min - 3 + 1) * 0.1 + 3 * 0.2; break;
                case 1: pr = tele.time_len.hour * 36 + (tele.time_len.min + 1) * 0.6; break;
                case 2: pr = tele.time_len.hour * 60 + tele.time_len.min + 1; break;
            }
            tele.price = pr;
            p[i] = tele;
        } //assign and cal price
    
        for(int m=1; m<6; m++) {
            for(int n=0; n<6-m; n++) {
                if(p[n].price<p[n+1].price)
                {
                    tmp = p[n];
                    p[n] = p[n+1];
                    p[n+1] = tmp;
                }
            }
        } //bubble sort, high->low
    
        printf("排序后: ");
        printf("\n");
        for(int s=0; s<6; s++) {
            tele = p[s];
            printf("%d", s+1);
            printf(" %d-%d-%d", tele.ymd.hour, tele.ymd.min, tele.ymd.second);
            printf(" %s %s", tele.call_no, tele.called_no);
            printf(" %d:%d:%d", tele.start_time.hour, tele.start_time.min, tele.start_time.second);
            printf(" %d:%d:%d", tele.time_len.hour, tele.time_len.min, tele.time_len.second);
            printf("\n");
        }
    
        return 0;
    }
    
    int TeleAttr(char *called_no) {
        int lena = strlen(called_no);
        if(called_no[0]=='\0') return 1; //domestic
        if(lena==8) return 0; //city
        else return 2; //foreign
    } //default 6786xxxx ->
    

    相关文章

      网友评论

          本文标题:高级语言程序设计实验(双数号题)

          本文链接:https://www.haomeiwen.com/subject/uapotxtx.html