2

嘿,这一切都是为了 C 编程。

这是向我提出的问题,有人可以告诉我如何正确地用 C 语言为以下语句编写代码吗?仅供参考,我已经回答了这个测验,但我的教授不会自己发布答案。当他给它评分时我做得很差,但为了帮助理解这一点,我将提供我的答案(尽管它们是不正确的)

1:创建一个结构,该结构将保存表示以下内容的位:

  • count:一个 4 位 BCD 数
  • 红色 LED:1 位
  • 绿色 LED:1 位
  • 电机方向:2位

将结构命名为 motorStatus

使用 typedef 来命名这个新的数据类型:mtrStatus_t

typedef unsigned char mtrStatus_t;

struct motorStatus mtrStatus_t {
    mtrStatus_t count: 4;
    mtrStatus_t redLED: 1;
    mtrStatus_t greenLED: 1;
    mtrStatus_t motorDirection: 2;
};

2:创建结构的新实例并将其命名为motor1Status

motorStatus = motor1Status;

3:编写语句初始化新的结构成员,如下所示:

  • count: 9 BCD
  • red LED: 1
  • green LED: 0
  • Motor Direction: 10

    count: 0x09; redLED: 0x01; greenLED: 0x00 motorDirection: 0x0A

4

2 回答 2

2

For the first one I would do something like this:

typedef struct motorStatus  
{
    int count: 4;
    int redLED: 1;
    int greenLED: 1;
    int motorDirection: 2;
} mtrStatus_t;

The second one is more like:

mtrStatus_t motor1status;

and finally:

motor1status.count = 0x9;
motor1status.redLED = 1;
motor1status.greenLED = 0;
motor1status.motorDirection = 0x02;

Count is a hex number because it is BCD (Binary coded decimal) http://en.wikipedia.org/wiki/Binary-coded_decimal In BCD, you use 4 bits to represent the numbers 0-9, there are some unused bit patterns, so the easy way to work with it is to just use hex (which also uses 4 bits to represent the numbers 0x0-0xf), but in BCD you just don't use the numbers 0xa-0xf.

The reason motorDirection is 0x02 is because he wants motor direction of 10, but it is a 2 bit field, so I am assuming that he means 10 binary, which is 0x02 hex.

于 2013-03-24T04:09:22.560 回答
0

Consider meeting these requirements in the order they are specified. Note that some requirements are merged together as one. This is often the case for assignments; Lecturers aren't always perfect linguists or logicians, particularly IT lecturers.

1: Create a structure that will hold bits the represent the following:

count: a 4 bit BCD number
red LED: 1 bit
green LED: 1 bit
motor direction: 2 bits

Name the structure motorStatus

Respond to this, first, without using typedef. Use the type int for bitfields. Next requirement:

Use typedef to name this new data type: mtrStatus_t

You've demonstrated the ability to write basic typedefs. typedef unsigned char mtrStatus_t; means "I define mtrStatus_t to alias unsigned char". Now, write a basic typedef like this that means "I define mtrStatus_t to alias struct motorStatus". Put it after the struct motorStatus definition, so that the compiler can see what it's aliasing.

2: Create a new instance of the structure and name it motor1Status

To clarify, your lecturer is asking you to declare a variable named motor1Status, which has the type mtrStatus_t or struct motorStatus. I presume you can declare variables, but correct me if I'm wrong.

3: Write statements to initialize the new structure members as follows:

count: 9 BCD
red LED: 1
green LED: 0
Motor Direction: 10

count: 0x09; redLED: 0x01; greenLED: 0x00 motorDirection: 0x0A

Your lecturer is asking for initialisation, not assignment. In char str[] = "fubar"; and char str[] = {'h','e','l','l','o','\0'};, str is declared and initialised to store a string corresponding to "fubar". In char str[6]; strcpy(str, "fubar"); str is declared without an initialisation, and each byte in "fubar" is copied (assigned) to the corresponding positions in str. How do you initialise a struct? Very similarly to the second str initialisation.

struct length_prefixed_string {
    size_t length;
    int zero;
    char string[];
};

/* Initialisation here: */
struct length_prefixed_string hello = { .length = 5,
                                        .string = "hello" };

This example uses a flexible array member, which can only be declared at the end of a struct: char string[];. It conveys an array of unknown length located at the end of the struct, which isn't counted towards sizeof (struct length_prefix_string). For this reason, the example stores the unknown length in the length argument. This is a little off-topic, but you can see an initialisation (as opposed to an assignment) above. This example differentiates initialisation from assignment; You can't assign the .string member of this struct with hello.string = "hello";.

Another difference is: Any members not mentioned by the initialisation will be assigned a zero value (but only when an initialisation is present), hence the value of hello.zero in the above example will be 0. For this reason, you can declare an array and zero-fill it with one statement: int array[size] = { 0 }; (another difference between assignment and initialisation: int array[size]; array = { 0 }; is invalid). Having said that, it'd be a good idea to ignore this fact for the purpose of your task, and explicitly initialise greenLED so that your marking examiner can't misunderstand.

I do hope I was helpful, here...

于 2013-03-24T05:21:11.663 回答