【问题标题】:How to work on array of bitmaps without '['如何在没有“[”的情况下处理位图数组
【发布时间】:2020-06-01 11:26:18
【问题描述】:

我编写了一个程序,它可以在没有位运算符的情况下检查 32 位整数的奇偶校验。除了声明之外,我也不能使用] 符号。我的程序已经很好了,但我不知道如何在没有[ 符号的情况下让它工作。我尝试了很多方法,但我的程序不想编译。这是我的程序:

#ifndef bit_set
#define bit_set
struct bit{
    unsigned b0 : 1;
    unsigned b1 : 1;
    unsigned b2 : 1;
    unsigned b3 : 1;
    unsigned b4 : 1;
    unsigned b5 : 1;
    unsigned b6 : 1;
    unsigned b7 : 1;
};
union bit_set
{
    unsigned int x;
    struct bit foo[4];
}word;
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "bit_set.h"

int main(void) {
    printf("Input number: ");
    if (scanf("%u", &word.x) == 0) {
        printf("Incorrect input");
        return 1;
    }
    int sum = 0;
    for (int i = 0; i < 4; i++) {
        sum += word.foo[i].b0 + word.foo[i].b1 + word.foo[i].b2 + word.foo[i].b3 + word.foo[i].b4 + word.foo[i].b5 + word.foo[i].b6 + word.foo[i].b7;
    }
    sum % 2 ? printf("NO") : printf("YES");
    return 0;
}  

【问题讨论】:

  • 欢迎来到 SO。您可能还记得a[i]*(a+i) 相同。
  • 我尝试了例如word.*(foo+i).b0,但它不会编译。除非我理解错了,而你的意思是别的。
  • @DariuszMajcherczyk 优先级:. 的运算符优先级高于 *,因此您必须编写 (*(foo+i)).b0。这反过来又是可怕的代码,除了如何成为一个糟糕的 C 程序员之外,没有什么可以从这个练习中学到的。
  • 也许它应该学习优先级,我不知道。这个(*(foo+i)).b0 也不起作用。
  • 你试过(*(word.foo+i).b0吗?

标签: c bitmap parity


【解决方案1】:

除了声明我也不能使用]号

这没有任何意义。但在这种情况下,您可以改用三元组:

word.foo??(i??).b0

或者,如果您更喜欢二合字母:

word.foo<:i:>.b0

【讨论】:

    【解决方案2】:

    我希望代码没有那么长的 printf 和看起来不太好的位图,所以我实现了数组。不幸的是,如果没有[ 签名,就不可能正确完成任务。现在我设法以一种丑陋的方式完成了这项任务:

    #ifndef bit_set
    #define bit_set
    struct bit{
        unsigned b0 : 1;
        unsigned b1 : 1;
        unsigned b2 : 1;
        unsigned b3 : 1;
        unsigned b4 : 1;
        unsigned b5 : 1;
        unsigned b6 : 1;
        unsigned b7 : 1;
        unsigned b02 : 1;
        unsigned b12 : 1;
        unsigned b22 : 1;
        unsigned b32 : 1;
        unsigned b42 : 1;
        unsigned b52 : 1;
        unsigned b62 : 1;
        unsigned b72 : 1;
        unsigned b03 : 1;
        unsigned b13 : 1;
        unsigned b23 : 1;
        unsigned b33 : 1;
        unsigned b43 : 1;
        unsigned b53 : 1;
        unsigned b63 : 1;
        unsigned b73 : 1;
        unsigned b04 : 1;
        unsigned b14 : 1;
        unsigned b24 : 1;
        unsigned b34 : 1;
        unsigned b44 : 1;
        unsigned b54 : 1;
        unsigned b64 : 1;
        unsigned b74 : 1;
    };
    union bit_set
    {
        unsigned int x;
        struct bit foo;
    }word;
    #endif
    
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <stdlib.h>
    #include <math.h>
    #include <time.h>
    #include "bit_set.h"
    
    int main(void) {
        printf("Input number: ");
        if (scanf("%u", &word.x) == 0) {
            printf("Incorrect input");
            return 1;
        }
        int sum = 0;
        sum += word.foo.b7+ word.foo.b6 +word.foo.b5 + word.foo.b4 + word.foo.b3 + word.foo.b2 + word.foo.b1 + word.foo.b0 + word.foo.b72 + word.foo.b62 + word.foo.b52 + word.foo.b42 + word.foo.b32 + word.foo.b22 + word.foo.b12 + word.foo.b02 + word.foo.b73 + word.foo.b63 + word.foo.b53 + word.foo.b43 + word.foo.b33 + word.foo.b23 + word.foo.b13 + word.foo.b03 + word.foo.b74 + word.foo.b64 + word.foo.b54 + word.foo.b44 + word.foo.b34 + word.foo.b24 + word.foo.b14 + word.foo.b04;
        sum % 2 ? printf("NO") : printf("YES");
        return 0;
    }  
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-29
      • 2018-12-10
      • 2021-03-18
      • 2012-08-05
      • 1970-01-01
      • 1970-01-01
      • 2012-03-29
      相关资源
      最近更新 更多