| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1023 人关注过本帖, 1 人收藏
标题:复杂的if else,switch case 条件判断,用表驱动方法如何优化
只看楼主 加入收藏
szchen2018
Rank: 2
等 级:论坛游民
帖 子:39
专家分:21
注 册:2021-11-20
结帖率:91.67%
收藏(1)
已结贴  问题点数:20 回复次数:1 
复杂的if else,switch case 条件判断,用表驱动方法如何优化
程序代码:
void UserSet_Effect(User * const me)
{
    int TempEffect;
    int machine;
    const INIT_STRUCT *AmpTab =NULL;

    if(me->Night)
        TempEffect = eEq_Night;
    else
        TempEffect = me->Effect;

    machine = me->channel_search;
    
    {
        if(TempEffect == eEq_Off || TempEffect == eEq_On){
            switch (machine)
            {
                case channel_RL:
                    if ((me->BitstreamInfo == (BITSTREAM_CH_L | BITSTREAM_CH_R)) 
                    || (me->BitstreamInfo == BITSTREAM_CH_L) 
                    || (me->BitstreamInfo == BITSTREAM_CH_R) 
                    ){
                        if (me->UserSrc_tbl[me->iSrc] == eAp_AUX_Src){
                            AmpTab = AmpDrcVaOn_SB_only_aux;
                        }
                        else{
                            AmpTab = AmpDrcVaOn_SB_only_digital;
                        }
                    }else
                    {
                        if (me->UserSrc_tbl[me->iSrc] == eAp_AUX_Src){
                            AmpTab = AmpDrcVaOn_SB_only_aux;
                        }
                        else{
                            AmpTab = AmpDrcVaOn_SB_only_digital;
                        }
                    }
                
                break;
                case channel_RL_Sub:
                    if ((me->BitstreamInfo == (BITSTREAM_CH_L | BITSTREAM_CH_R)) 
                    || (me->BitstreamInfo == BITSTREAM_CH_L) 
                    || (me->BitstreamInfo == BITSTREAM_CH_R) 
                    ){
                        if (me->UserSrc_tbl[me->iSrc] == eAp_AUX_Src){
                            AmpTab = AmpDrcVaOn_SB_sub_aux;
                        }
                        else{
                            AmpTab = AmpDrcVaOn_SB_sub_digital;
                        }
                    }else
                    {
                        if (me->UserSrc_tbl[me->iSrc] == eAp_AUX_Src){
                            AmpTab = AmpDrcVaOn_SB_sub_aux;
                        }
                        else{
                            AmpTab = AmpDrcVaOn_SB_sub_digital;
                        }
                    }
                
                    
                break;    
                case channel_RL_RLs:
                    if ((me->BitstreamInfo == (BITSTREAM_CH_L | BITSTREAM_CH_R)) 
                    || (me->BitstreamInfo == BITSTREAM_CH_L) 
                    || (me->BitstreamInfo == BITSTREAM_CH_R) 
                    ){
                        if (me->UserSrc_tbl[me->iSrc] == eAp_AUX_Src){
                            AmpTab = AmpDrcVaOn_SB_surr_aux;
                        }
                        else{
                            AmpTab = AmpDrcVaOn_SB_surr_digital;
                        }
                    }else
                    {
                        if (me->UserSrc_tbl[me->iSrc] == eAp_AUX_Src){
                            AmpTab = AmpDrcVaOn_SB_surr_aux;
                        }
                        else{
                            AmpTab = AmpDrcVaOn_SB_surr_digital;
                        }
                    }
                    
                break;
                case channel_RL_RLs_Sub:
                    if ((me->BitstreamInfo == (BITSTREAM_CH_L | BITSTREAM_CH_R))
                    || (me->BitstreamInfo == BITSTREAM_CH_L) 
                    || (me->BitstreamInfo == BITSTREAM_CH_R)
                    ){
                        if (me->UserSrc_tbl[me->iSrc] == eAp_AUX_Src){
                            AmpTab = AmpDrcVaOn_SB_sub_surr_aux;
                        }
                        else{
                            AmpTab = AmpDrcVaOn_SB_sub_surr_digital;
                        }
                    }else
                    {
                        if (me->UserSrc_tbl[me->iSrc] == eAp_AUX_Src){
                            AmpTab = AmpDrcVaOn_SB_sub_surr_aux;
                        }
                        else{
                            AmpTab = AmpDrcVaOn_SB_sub_surr_digital;
                        }
                    }
                
                break;
            }
        
        }else if(TempEffect == eEq_Night){
            switch (machine)
            {
                case channel_RL:
                    if ((me->BitstreamInfo == (BITSTREAM_CH_L | BITSTREAM_CH_R)) 
                    || (me->BitstreamInfo == BITSTREAM_CH_L) 
                    || (me->BitstreamInfo == BITSTREAM_CH_R) 
                    ){
                        if (me->UserSrc_tbl[me->iSrc] == eAp_AUX_Src){
                            AmpTab = AmpDrcNight_SB_only_aux;
                        }
                        else{
                            AmpTab = AmpDrcNight_SB_only_digital;
                        }
                    }else
                    {
                        if (me->UserSrc_tbl[me->iSrc] == eAp_AUX_Src){
                            AmpTab = AmpDrcNight_SB_only_aux;
                        }
                        else{
                            AmpTab = AmpDrcNight_SB_only_digital;
                        }
                    }
                
                break;
                case channel_RL_Sub:
                    if ((me->BitstreamInfo == (BITSTREAM_CH_L | BITSTREAM_CH_R))
                    || (me->BitstreamInfo == BITSTREAM_CH_L) 
                    || (me->BitstreamInfo == BITSTREAM_CH_R)
                    ){
                        if (me->UserSrc_tbl[me->iSrc] == eAp_AUX_Src){
                            AmpTab = AmpDrcNight_SB_sub_aux;
                        }
                        else{
                            AmpTab = AmpDrcNight_SB_sub_digital;
                        }
                    }else
                    {
                        if (me->UserSrc_tbl[me->iSrc] == eAp_AUX_Src){
                            AmpTab = AmpDrcNight_SB_sub_aux;
                        }
                        else{
                            AmpTab = AmpDrcNight_SB_sub_digital;
                        }
                    }
                
                    
                break;    
                case channel_RL_RLs:
                    if ((me->BitstreamInfo == (BITSTREAM_CH_L | BITSTREAM_CH_R))
                    || (me->BitstreamInfo == BITSTREAM_CH_L) 
                    || (me->BitstreamInfo == BITSTREAM_CH_R) 
                    ){
                        if (me->UserSrc_tbl[me->iSrc] == eAp_AUX_Src){
                            AmpTab = AmpDrcNight_SB_surr_aux;
                        }
                        else{
                            AmpTab = AmpDrcNight_SB_surr_digital;
                        }
                    }else
                    {
                        if (me->UserSrc_tbl[me->iSrc] == eAp_AUX_Src){
                            AmpTab = AmpDrcNight_SB_surr_aux;
                        }
                        else{
                            AmpTab = AmpDrcNight_SB_surr_digital;
                        }
                    }
                    
                break;
                case channel_RL_RLs_Sub:
                    if ((me->BitstreamInfo == (BITSTREAM_CH_L | BITSTREAM_CH_R))
                    || (me->BitstreamInfo == BITSTREAM_CH_L) 
                    || (me->BitstreamInfo == BITSTREAM_CH_R) 
                    ){
                        if (me->UserSrc_tbl[me->iSrc] == eAp_AUX_Src){
                            AmpTab = AmpDrcNight_SB_sub_surr_aux;
                        }
                        else{
                            AmpTab = AmpDrcNight_SB_sub_surr_digital;
                        }
                    }else
                    {
                        if (me->UserSrc_tbl[me->iSrc] == eAp_AUX_Src){
                            AmpTab = AmpDrcNight_SB_sub_surr_aux;
                        }
                        else{
                            AmpTab = AmpDrcNight_SB_sub_surr_digital;
                        }
                    }
                
                break;
            
           }

        
       }
       if (AmpTab != NULL)
          UserAmp(AmpTab, 1);
    }
}


[此贴子已经被作者于2022-1-10 09:59编辑过]

搜索更多相关主题的帖子: || case break 优化 switch 
2022-01-09 23:38
mindfulness
Rank: 3Rank: 3
等 级:论坛游侠
威 望:3
帖 子:27
专家分:110
注 册:2021-5-8
收藏
得分:14 
TempEffect , machine, me->BitstreamInfo 3 个维度的关系建表是最直接的。
你这关系看起来有一些重复,业务逻辑应该也可以优化一下。

/*
*  Simple love, simple C.
*/
2022-01-15 16:44
快速回复:复杂的if else,switch case 条件判断,用表驱动方法如何优化
数据加载中...
 
   



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

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