请帮我写C编程 [英] Please help me to write C programming

查看:58
本文介绍了请帮我写C编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是c编程新手。我知道c中的一点点基础但是,现在我必须为8位DIP开关编写c代码。每当我按下一个开关然后我必须打印该开关名称为ex:如果我按下开关8意味着它必须打印开关8,如果我将按开关7和开关8意味着它必须显示两个开关..



我尝试了什么:



i尝试过女巫转换案例陈述但是,每当我将按开关8然后它是打印开关8.如果我将按开关7和开关8它没有打印任何东西请帮我如何写这个逻辑。

解决方案

< blockquote>你不能使用开关:或者至少不容易(你可以,但你需要一个循环来完成它)。

相反,只需使用一系列的 if 语句:

  #define switch8Mask 0x80 
#define switch7Mask 0x40
#define switch6Mask 0x20
...

if ((切换hValue& switch8Mask)!= 0
{
printf( Switch 8 \ n);
}
if ((switchValue& switch7Mask)!= 0
{
printf( Switch 7 \ n);
}
...


您也可以使用C中的位字段结构执行此操作。请参阅 C位字段 [ ^ ]



通过这种方式,您可以更清楚地看到交换机中的哪个位也提到代码更容易阅读。

使用哪种方法是品味问题。



首先声明结构。

成员声明 unsigned char sw1:1; 表示sw1将使用整个字节的1位。

  typedef   struct  _dip_switch 
{
unsigned char sw1: 1 ; // 最不重要的位
unsigned char sw2: 1 ;
unsigned char sw3: 1 ;
unsigned char sw4: 1 ;
unsigned char sw5: 1 ;
unsigned char sw6: 1 ;
unsigned char sw7: 1 ;
unsigned char sw8: 1 ;
} DipSwitch;





然后您可以使用此代码进行打印输出。

  unsigned   char  switchValue = 0x41;  //  此值我假设您从某处读取 

DipSwitch my_switch;

// 将输入值设置为DipSwitch结构的实例
memset(& my_switch,switchValue, sizeof unsigned ));

if (my_switch.sw1)
printf( < span class =code-string> Switch 1已设置。\\\\ nn
);
if (my_switch.sw2)
printf( Switch 2已设置。\\\\ n);
if (my_switch.sw3)
printf( Switch 3已设置。\\\\ n);
if (my_switch.sw4)
printf( Switch 4已设置。\\\\ n);
if (my_switch.sw5)
printf( Switch 5已设置。\\\\ n);
if (my_switch.sw6)
printf( Switch 6已设置。\\\\ n);
if (my_switch.sw7)
printf( Switch 7已设置。\\\\ n);
if (my_switch.sw8)
printf( Switch 8已设置。\\\ n);





重要:无论你做什么,都不要开始使用if()else if()。

这是因为你想测试输入数据中的所有位,而不仅仅是第一位匹配。

这也是为什么在这种情况下switch语句不起作用的原因。


am new to c programming. i know little bit basics in c but, now i have to write c code for 8-bit dip switches. whenever i will press one switch then i have to print that switch name for ex: if i press switch 8 means it has to print switch 8, if i will press switch 7 and switch 8 means it has to display both switches..

What I have tried:

i tried witch switch case statement but,whenever i will press switch 8 then it is printing switch 8. if i will press switch 7 and switch 8 it is not printing anything please help me how to write this logic.

解决方案

You can't use a switch for this: or at least not easily (you can, but you need a loop to do it).
Instead, just use a series of if statements:

#define switch8Mask 0x80
#define switch7Mask 0x40
#define switch6Mask 0x20
...

if ((switchValue & switch8Mask) != 0)
   {
   printf("Switch 8\n");
   }
if ((switchValue & switch7Mask) != 0)
   {
   printf("Switch 7\n");
   }
...


You can also do this with a bit field struct in C. See C Bit Fields[^]

This way it is more clear which bit in the switch you are referring too and the code is easier to read.
Which method to use is a matter of taste.

First declare the struct.
The member declaration unsigned char sw1 : 1; means that sw1 will use 1 bit of the whole byte.

typedef struct _dip_switch
{
    unsigned char sw1 : 1;	// Least significant bit
    unsigned char sw2 : 1;
    unsigned char sw3 : 1;
    unsigned char sw4 : 1;
    unsigned char sw5 : 1;
    unsigned char sw6 : 1;
    unsigned char sw7 : 1;
    unsigned char sw8 : 1;
} DipSwitch;



Then you can use this code to do the print outs.

unsigned char switchValue = 0x41;   // This value I assume you read from somewhere

DipSwitch my_switch;

// Set the input value to the instance of the DipSwitch structure
memset(&my_switch, switchValue, sizeof(unsigned char));

if (my_switch.sw1)
    printf("Switch 1 is set.\r\n");
if (my_switch.sw2)
    printf("Switch 2 is set.\r\n");
if (my_switch.sw3)
    printf("Switch 3 is set.\r\n");
if (my_switch.sw4)
    printf("Switch 4 is set.\r\n");
if (my_switch.sw5)
    printf("Switch 5 is set.\r\n");
if (my_switch.sw6)
    printf("Switch 6 is set.\r\n");
if (my_switch.sw7)
    printf("Switch 7 is set.\r\n");
if (my_switch.sw8)
    printf("Switch 8 is set.\r\n");



Important: Whatever you do, don't start to use if () else if().
This is because you want to test all bits in in the input data, not just the first match.
That is also why a switch-statement doesn't work in this case.


这篇关于请帮我写C编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆