Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 对字符中的位进行计数并对位进行计数_C_Bit - Fatal编程技术网

C 对字符中的位进行计数并对位进行计数

C 对字符中的位进行计数并对位进行计数,c,bit,C,Bit,这只是我的第二节编程课。有30个房间,我们要看看每个房间里都有什么,然后统计。我已经使用for循环遍历了30个房间,我知道我必须使用一个计数器来查看每个房间中都有什么。我不知道如何数一数一个字的位数。下面是示例输入/输出,以及到目前为止我在代码中拥有的内容 样本输入: 9 23 @Z 1 20 @@ 2 21 @A 3 22 @# 4 23 @1 5 22 @@ 6 22 @@ 7 22 @@ 8 22 @@ 9 23 @Z Her

这只是我的第二节编程课。有30个房间,我们要看看每个房间里都有什么,然后统计。我已经使用for循环遍历了30个房间,我知道我必须使用一个计数器来查看每个房间中都有什么。我不知道如何数一数一个字的位数。下面是示例输入/输出,以及到目前为止我在代码中拥有的内容

样本输入:

9   23  @Z
1   20  @@
2   21  @A
3   22  @#
4   23  @1
5   22  @@
6   22  @@
7   22  @@
8   22  @@
9   23  @Z  Here be trolls � not!
10  23  @+
12  23  @@
13  24  @@
11  22  @@
14  22  @2
15  21  @1
16  20  @@
17  19  @@
18  20  @@
19  19  @@
20  18  @@
21  17  @*
22  16  @*
23  15  @%
0   14  @7
0   gold_bar
1   silver_bar
2   diamond
3   copper_ring
4   jumpy_troll
5   air
6   angry_troll
7   plutonium_troll
如果钥匙是:

0 gold_bar
1 silver_bar
2 diamond
3 copper_ring
4 jumpy_troll
5 air
6 angry_troll
7 plutonium_troll
行是9 23@Z,那么9,23处的房间(字符Z带有二进制:01011010)有项目1、3、4、6。银条,铜环,神经质的巨魔,愤怒的巨魔

#include <stdio.h>
#include <stdlib.h>
int main()
{
// contains x and y coordinate
 int first, second;
  char third[100];
char Map[30][30];

// map initialization
for(int x=0; x<30; x++){
    for(int y=0; y<30; y++){
        Map[x][y] = '.';
    }
}


while(scanf("%d %d %s",&first, &second, third) != -1) {
    // Condition 1: a zero coordinate
    if (first==0 || second==0) exit(0);
    // Condition 2: coordinate out of range
    if (first<0 || first>30 || second<0 || second>30){
        printf("Error: out of range 0-30!\n");
        exit(1);
    }
// bit counter
    for( int bit_p=0; bit_p<8; bit_p++){

    }

    Map[second-1][first-1] = third[1];


return 0;
}
样本输出:

6   gold_bar
6   silver_bar
1   diamond
4   copper_ring
4   jumpy_troll
8   air
15  angry_troll
0   plutonium_troll

在一个字中没有用于累积位计数的内置函数。对于每个房间,您需要检查8位中的每一位,如果设置了该位,则增加相应的计数变量。有很多方法可以做到这一点,特别是对于一个你想尽可能清楚地做到这一点的类

基本思想是,您希望将您在中读取的单词(“第三个”,在您的框架中)与上移到您正在测试的位位置的单个位掩码进行比较。将掩码与中的输入进行and’ing比较,并使用if语句对其进行测试。我在您的框架中加入了一个示例:

// bit counter
for( int bit_p=0; bit_p<8; bit_p++){
    if (third & (1 << bit_p))
        tally[bit_p]++;
}
//位计数器

对于(int bit_p=0;bit_p而言,没有用于在一个字中累积位计数的内置函数。对于每个房间,您需要检查8位中的每一位,如果设置了该位,则增加适当的计数变量。有很多方法可以做到这一点,但对于一个类来说,您需要尽可能清楚地进行检查

基本思想是,您希望将读入的单词(“框架中的第三个”)与上移到您正在测试的位位置的一个位掩码进行比较。将掩码与中的输入进行比较,并使用if语句进行测试。我已将一个示例放入您的框架中:

// bit counter
for( int bit_p=0; bit_p<8; bit_p++){
    if (third & (1 << bit_p))
        tally[bit_p]++;
}
//位计数器

对于(int bit_p=0;bit_p此代码检查字符,如果该位置的位为0,则用值0填充8个int的数组,如果该位置的位为1,则用值1填充

char c = 'Z';
int bits[8];
int n;
char aux = c;
for ( n=0; n<8; ++n ) {
  if ( aux & '\1' )
    bits[n] = 1;
  else
    bits[n] = 0;
  aux = aux >> 1;
}
charc='Z';
整数位[8];
int n;
char aux=c;
对于(n=0;n>1;
}
aux&'\1'
aux=aux>>1
是听说过的代码

“\1”是ASCII码为1的字符,因此其位置0处的位为1,所有其他位为0。当我们对字符“\1”进行按位and运算时,如果该字符在位0处为1,则得到1,否则得到0


aux=aux>>1
将位向左移动一个位置,以便在下一次操作中,位置0处的位将是原来位置1处的位。

此代码检查字符,如果该位置处的位为0,则用值0填充8个整数的数组,如果该位置处的位为1,则用值1填充数组

char c = 'Z';
int bits[8];
int n;
char aux = c;
for ( n=0; n<8; ++n ) {
  if ( aux & '\1' )
    bits[n] = 1;
  else
    bits[n] = 0;
  aux = aux >> 1;
}
charc='Z';
整数位[8];
int n;
char aux=c;
对于(n=0;n>1;
}
aux&'\1'
aux=aux>>1
是听说过的代码

“\1”是ASCII码为1的字符,因此其位置0处的位为1,所有其他位为0。当我们对字符“\1”进行按位and运算时,如果该字符在位0处为1,则得到1,否则得到0


aux=aux>>1
将位向左移动一个位置,以便在下一次操作中,位置0处的位将是原来位置1处的位。

一个字的各个位不能用C直接访问,因此我们必须做其他事情来获取它们

考虑您的值,这些值给出了设置的位:

0 gold_bar          00000001B  or 0x01
1 silver_bar        00000010B  or 0x02
2 diamond           00000100B  or 0x04
3 copper_ring       00001000B  or 0x08
4 jumpy_troll       00010000B  or 0x10
5 air               00100000B  or 0x20
6 angry_troll       01000000B  or 0x40
7 plutonium_troll   10000000B  or 0x80
因此,观察一下,让我们考虑一下,如果我们有以下内容:

会发生什么?
unsigned char test = 'z'
unsigned char res = test & 0x02
现在,正如你所说,“z”有一个二进制表示0111010,如果我们对2进行逻辑and运算,我们得到:

     z: 01111010B
  0x02: 00000010B
        ----------
        00000010B
表达式test&0x02的计算结果为0x02。那么,我们如何使用它呢?我们可以编写如下代码:

     if((test & 0x02) == 0x02)
         ++cntSilverBars; 
在此基础上,假设我们有适当定义的变量(即cntAuBars将声明为int cntAuBars并保留发现的金条数量):

     if((test & 0x01) == 0x01)
         ++cntAuBars;
     if((test & 0x02) == 0x02)
         ++cntAgBars;
     if((test & 0x04) == 0x04)
         ++cntDiamonds;
     if((test & 0x08) == 0x08)
         ++cntCuRings;
     if((test & 0x10) == 0x10)
         ++cntJumpyTroll;
     if((test & 0x20) == 0x20)
         ++cntSilverBars;
     if((test & 0x40) == 0x40)
         ++cntAir;
     if((test & 0x80) == 0x80)
         ++cntPuTroll;
虽然上述内容可行,但可以重新编写为:

int itemCnt[8] = {0};         // array to hold count of items, index is item type
unsigned char test;           // holds contents of room.
int loc;
for(loc = 0; loc < 8; loc++)  // loop over every bit and see if it is set
{
     unsigned char bitPos = 1 << loc;  // generate a bit-mask 
     if((test & bitPos) == bitPos)
          ++itemCnt[loc];
}

一个单词的各个位不能用C直接访问,所以我们必须做其他事情来获取它们

考虑您的值,这些值给出了设置的位:

0 gold_bar          00000001B  or 0x01
1 silver_bar        00000010B  or 0x02
2 diamond           00000100B  or 0x04
3 copper_ring       00001000B  or 0x08
4 jumpy_troll       00010000B  or 0x10
5 air               00100000B  or 0x20
6 angry_troll       01000000B  or 0x40
7 plutonium_troll   10000000B  or 0x80
因此,观察一下,让我们考虑一下,如果我们有以下内容:

会发生什么?
unsigned char test = 'z'
unsigned char res = test & 0x02
现在,正如你所说,“z”有一个二进制表示0111010,如果我们对2进行逻辑and运算,我们得到:

     z: 01111010B
  0x02: 00000010B
        ----------
        00000010B
表达式test&0x02的计算结果为0x02。那么,我们如何使用它呢?我们可以编写如下代码:

     if((test & 0x02) == 0x02)
         ++cntSilverBars; 
在此基础上,假设我们有适当定义的变量(即cntAuBars将声明为int cntAuBars并保留发现的金条数量):

     if((test & 0x01) == 0x01)
         ++cntAuBars;
     if((test & 0x02) == 0x02)
         ++cntAgBars;
     if((test & 0x04) == 0x04)
         ++cntDiamonds;
     if((test & 0x08) == 0x08)
         ++cntCuRings;
     if((test & 0x10) == 0x10)
         ++cntJumpyTroll;
     if((test & 0x20) == 0x20)
         ++cntSilverBars;
     if((test & 0x40) == 0x40)
         ++cntAir;
     if((test & 0x80) == 0x80)
         ++cntPuTroll;
虽然上述内容可行,但可以重新编写为:

int itemCnt[8] = {0};         // array to hold count of items, index is item type
unsigned char test;           // holds contents of room.
int loc;
for(loc = 0; loc < 8; loc++)  // loop over every bit and see if it is set
{
     unsigned char bitPos = 1 << loc;  // generate a bit-mask 
     if((test & bitPos) == bitPos)
          ++itemCnt[loc];
}

谢谢!我该如何输出数据,使其成为6个金条6个银条1个钻石4个铜戒指4个神经质的巨魔8个空气15个愤怒的巨魔0个钚巨魔?我需要声明变量吗?@janny数组跟踪事物。金条的数量将包含在数组的第一个元素中(即项目[0])只要确保项数组是在遍历rooms的循环之外声明的。我可能做错了什么,我写了printf(“%d”,itemCnt[0]);输出是00000000000000000000000@janny添加了显示如何打印数组的代码。还注意到我在循环中有一个错误(我修复了这个错误)谢谢!我该如何输出数据,使其成为6个金条6个银条1个钻石4个铜戒指4个神经质的巨魔8个空气15个愤怒的巨魔0个钚巨魔?我需要声明变量吗?@janny数组跟踪事物。金条的数量将包含在数组的第一个元素中(即项目[0])只要确保项数组是在遍历rooms的循环之外声明的。我可能做错了什么,我写了printf(“%d”,itemCnt[0]);输出是