c/c++練習–19

10878 ワード

c/c++練習–19
  • 練習問題の出所:C言語経典プログラミング282例
  • 181.与えられた16ビットのバイナリ数の奇数ビットを取り出す
    #include 
    #include 
    
    int main(void){
        int a=0,b=1,sum=0;
    
        printf("please input a number:
    "
    ); scanf("%d",&a); for(int i=0;i<8;i++){ sum |= a&b; b<<=2; } printf("%d",sum); return(EXIT_SUCCESS); }

    182.整数の後4桁をとる
    画面に8進数を入力し、出力後の4ビットに対応する数を実現します.
    #include 
    #include 
    
    int main(void){
        unsigned int    a=0,b=1;
    
        printf("please input a base-8 number:
    "
    ); scanf("%o",&a); b = a & 15; printf("%d",b); return(EXIT_SUCCESS); }

    183.通常ビット演算
    a=2,b=4,c=6,d=8のときa&c,b|d,a^d,~aの値を求める.
    #include 
    #include 
    
    int main(void){
        int a=2,b=4,c=6,d=8;
        printf("%d %d %d %d
    "
    ,a&c,b|d,a^d,~a); return(EXIT_SUCCESS); }

    184.整数と0の相違
    #include 
    #include 
    
    int main(void){
        int a=2;
        printf("%d 
    "
    ,a^0); return(EXIT_SUCCESS); }

    185.malloc関数を使用してメモリを割り当てる
    2つのメンバー、1つは整数、もう1つは構造体ポインタを含む構造体タイプポインタを作成し、malloc関数を使用して1つの構造体のメモリ領域を割り当て、2つのメンバーに値を割り当てて表示します.
    #include 
    #include 
    
    typedef struct node{
        int num;
        struct node *next;
    }*Nodedef;
    
    int main(void){
        Nodedef head;
        head = (Nodedef)malloc(sizeof(struct node));
        head->num=1;
        head->next = NULL;
        printf("%d %p
    "
    ,head->num,head->next); free(head); return(EXIT_SUCCESS); }

    186.calloc関数を使用してメモリを割り当てる
    2つのメンバー、1つは整数、もう1つは構造体ポインタを含む構造体タイプポインタを作成し、malloc関数を使用して1つの構造体のメモリ領域を割り当て、2つのメンバーに値を割り当てて表示します.
    #include 
    #include 
    
    typedef struct node{
        int num;
        struct node *next;
    }*Nodedef;
    
    int main(void){
        Nodedef head;
        head = (Nodedef)calloc(1,sizeof(struct node));
        head->num=1;
        head->next = NULL;
        printf("%d %p
    "
    ,head->num,head->next); free(head); return(EXIT_SUCCESS); }

    187.3つの配列要素を有するデータにメモリを割り当てる
    #include 
    #include 
    
    int main(void){
        int *head;
        head = (int *)malloc(3*sizeof(int));
        head[0]=1;
        head[1]=1;
        head[2]=1;
        printf("%d %d   %d
    "
    ,*head, head[1], head[2]); free(head); return(EXIT_SUCCESS); }

    188.メモリを2ビット配列に動的に割り当てる
    #include 
    #include 
    
    int main(void){
        int **head;
        head = (int **)malloc(3*sizeof(int* [3]));
        for(int i=0;i<3;i++)
            head[i] = (int *)malloc(3*sizeof(int));
        for (int i=0;i<3;i++){
            for(int j=0;j<3;j++){
                head[i][j] = i*3+j;
                printf("%d\t",head[i][j]);
            }
            printf("
    "
    ); } for(int i=0;i<3;i++)free(head[i]); free(head); return(EXIT_SUCCESS); }

    189.商品情報の動的保存
    #include 
    #include 
    
    struct goods{
        char    *name;
        int num;
        int mount;
        float   price;  
    };
    
    int main(void){
        struct goods *head;
        head = (struct goods *)malloc(sizeof(struct goods));
        head->name = "apple";
        head->num = 101;
        head->mount = 100;
        head->price = 2.01;
    
        printf("%s--%d--%d--%f
    "
    ,head->name,head->num,head->mount,head->price); free(head); return(EXIT_SUCCESS); }

    190.パラメータを持たないマクロ定義で平行四角形面積を求める
    パラメータを持たないマクロ定義を用いて平行四角形の面積を求め、平行四角形の面積=底辺*高、平行四角形
    #include 
    #include 
    
    #define LENGTH  3
    #define HIGN        4
    
    int main(void){
        printf("%d
    "
    ,LENGTH*HIGN); return(EXIT_SUCCESS); }