| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 978 人关注过本帖
标题:求问:结构
只看楼主 加入收藏
吕宏
Rank: 1
等 级:新手上路
帖 子:49
专家分:0
注 册:2016-4-12
结帖率:86.67%
收藏
已结贴  问题点数:18 回复次数:3 
求问:结构
先说题目吧
图片附件: 游客没有浏览图片的权限,请 登录注册

这是题目范例,就是要最终得到的效果
图片附件: 游客没有浏览图片的权限,请 登录注册

我现在可以得到分部的部分就是输一个数,得到一个输出
图片附件: 游客没有浏览图片的权限,请 登录注册

#include <stdio.h>
   struct bits
   {      
   unsigned char bit0 : 1;
   unsigned char bit1 : 1;
   unsigned char bit2 : 1;
   unsigned char bit3 : 1;
   unsigned char bit4 : 1;
   unsigned char bit5 : 1;
   unsigned char bit6 : 1;
   unsigned char bit7 : 1;
 };
   union w {      struct bits a;     unsigned char t; }m;
   void f0(unsigned char bit) {      printf("The Interrupt Service Routine isr0 is called!\n"); }
   void f1(unsigned char bit) {      printf("The Interrupt Service Routine isr1 is called!\n"); }
   void f2(unsigned char bit) {      printf("The Interrupt Service Routine isr2 is called!\n"); }
   void f3(unsigned char bit) {      printf("The Interrupt Service Routine isr3 is called!\n"); }
   void f4(unsigned char bit) {      printf("The Interrupt Service Routine isr4 is called!\n"); }
   void f5(unsigned char bit) {      printf("The Interrupt Service Routine isr5 is called!\n"); }
   void f6(unsigned char bit) {      printf("The Interrupt Service Routine isr6 is called!\n"); }
   void f7(unsigned char bit) {      printf("The Interrupt Service Routine isr7 is called!\n"); }
  int main(int argc, char const *argv[])
  {    unsigned int n;
  void (*p_fun[8])(unsigned char b);
  
  scanf("%d", &n);
 
   m.t = n;
  
  p_fun[0] = f0;
  p_fun[1] = f1;
  p_fun[2] = f2;
  p_fun[3] = f3;
  p_fun[4] = f4;
  p_fun[5] = f5;
  p_fun[6] = f6;
  p_fun[7] = f7;
  if(m.a.bit0)          p_fun[0](m.a.bit0);
  if(m.a.bit1)          p_fun[1](m.a.bit1);
  if(m.a.bit2)          p_fun[2](m.a.bit2);
  if(m.a.bit3)          p_fun[3](m.a.bit3);
  if(m.a.bit4)          p_fun[4](m.a.bit4);
  if(m.a.bit5)          p_fun[5](m.a.bit5);
  if(m.a.bit6)          p_fun[6](m.a.bit6);
  if(m.a.bit7)          p_fun[7](m.a.bit7);
  
  return 0;
  }
但是在到输入全部的值出错了,直接显示无法运行,求教,这是我改的一部分代码,其他没改
 int main(int argc, char const *argv[])
  {      
  void (*p_fun[8])(unsigned char b);
  int p,q,n[10];
  scanf("%d", &q);
  for(p=1;p<=q;p++)
  {scanf("%d", n[p]);}
  for(p=1;p<=q;p++)
  {
   m.t = n[p];
  想知道错在哪里,怎么改正
这是原代码
int main(int argc, char const *argv[])
  {    unsigned int n;
  void (*p_fun[8])(unsigned char b);
  
  scanf("%d", &n);
 
   m.t = n;

搜索更多相关主题的帖子: include 
2016-05-31 09:31
grmmylbs
Rank: 14Rank: 14Rank: 14Rank: 14
等 级:贵宾
威 望:54
帖 子:1409
专家分:5845
注 册:2016-2-14
收藏
得分:18 
这样的吗?

程序代码:
#include <stdio.h>
struct bits
{
    unsigned char bit0 : 1;
    unsigned char bit1 : 1;
    unsigned char bit2 : 1;
    unsigned char bit3 : 1;
    unsigned char bit4 : 1;
    unsigned char bit5 : 1;
    unsigned char bit6 : 1;
    unsigned char bit7 : 1;
};
union w { struct bits a;     unsigned char t; }m;
void f0(unsigned char bit) { printf("The Interrupt Service Routine isr0 is called!\n"); }
void f1(unsigned char bit) { printf("The Interrupt Service Routine isr1 is called!\n"); }
void f2(unsigned char bit) { printf("The Interrupt Service Routine isr2 is called!\n"); }
void f3(unsigned char bit) { printf("The Interrupt Service Routine isr3 is called!\n"); }
void f4(unsigned char bit) { printf("The Interrupt Service Routine isr4 is called!\n"); }
void f5(unsigned char bit) { printf("The Interrupt Service Routine isr5 is called!\n"); }
void f6(unsigned char bit) { printf("The Interrupt Service Routine isr6 is called!\n"); }
void f7(unsigned char bit) { printf("The Interrupt Service Routine isr7 is called!\n"); }
int main(int argc, char const *argv[])
{
    void(*p_fun[8])(unsigned char b);
    int p, q, n[10];
    scanf("%d", &q);
    p_fun[0] = f0;
    p_fun[1] = f1;
    p_fun[2] = f2;
    p_fun[3] = f3;
    p_fun[4] = f4;
    p_fun[5] = f5;
    p_fun[6] = f6;
    p_fun[7] = f7;
    for (p = 1; p <= q; p++)
    {
        scanf("%d", &n[p]);
    }
    for (p = 1; p <= q; p++)
    {
        m.t = n[p];
        printf("%d:\n",n[p]);
        if (m.a.bit0)          p_fun[0](m.a.bit0);
        if (m.a.bit1)          p_fun[1](m.a.bit1);
        if (m.a.bit2)          p_fun[2](m.a.bit2);
        if (m.a.bit3)          p_fun[3](m.a.bit3);
        if (m.a.bit4)          p_fun[4](m.a.bit4);
        if (m.a.bit5)          p_fun[5](m.a.bit5);
        if (m.a.bit6)          p_fun[6](m.a.bit6);
        if (m.a.bit7)          p_fun[7](m.a.bit7);
        printf("\n");
    }
    return 0;
}
2016-05-31 09:42
吕宏
Rank: 1
等 级:新手上路
帖 子:49
专家分:0
注 册:2016-4-12
收藏
得分:0 
GG,就是一个&的问题吗
2016-05-31 09:54
grmmylbs
Rank: 14Rank: 14Rank: 14Rank: 14
等 级:贵宾
威 望:54
帖 子:1409
专家分:5845
注 册:2016-2-14
收藏
得分:0 
是的
2016-05-31 10:01
快速回复:求问:结构
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.033236 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved