为什么我的程序不打印指定输入的输出?

时间:2013-10-24 21:51:17

标签: c if-statement switch-statement

我有一个看起来像这样的网格: enter image description here

我的程序所做的是它将计算是否获得徽章,如果徽章是否获得特定输入,它将打印出是或否(输入将是带有坐标的框) (x,y))。但出于某种原因,当我打印输出时,我的程序只计算黄色阴影区域。

在我的代码中,我通过说:

来说明不在边缘的方框
if ((x >= 2 && x <= 19) && (y >= 2 && y <= 19))

但这似乎并没有解决我的问题。注意:如果输入(x,y)不在区间[1,20]中,那么我返回-1。

这是我打印结果时得到的结果: enter image description here

如您所见,不在边缘的字段中的银色徽章的输出不会显示。我似乎无法弄清楚为什么不在边缘的盒子没有被计算/打印出来。

注意:当一个字段获得两个或更多青铜徽章时,Poly徽章会返回“是”。

注意:任意颜色徽章会在以下情况下返回徽章:

  1. 如果所有邻近的田地都没有青铜徽章(邻近地区包括西北,东北,西南和东南),一块土地可以获得0枚青铜徽章

  2. 一个领域获得1枚青铜徽章,其总体得分(声称为fieldScore)为20或以上。

  3. 一个领域获得2枚青铜徽章,并且至少有以下一种情况:该字段不会获得任何利润(声明为fieldProfit),收获时间不到80天(声明为harvestTime),或者它的阳光暴露(声称为sunExposure)大于其灌溉暴露(宣称为灌溉暴露)。

  4. 如果至少有两个直接邻居获得两枚或更多枚铜徽章,则可以获得3枚或以上的青铜徽章。 (直接邻居是北,南,东,西)。

  5. 这是我的两个银徽章功能代码(Poly徽章和任意颜色徽章):

    int countBadges(int x, int y) {
       int mavebadge, incirclebadge, herobadge, weatherbadge, inquattrobadge,
          total;
    
       mavebadge = badgeBoundaryMaven(x, y);
       incirclebadge = badgeInnerCircle(x, y);
       herobadge = badgeLocalHero(x, y);
       weatherbadge = badgeBoringWeather(x, y);
       inquattrobadge = badgeInQuattro(x, y);
    
       total = mavebadge + incirclebadge + herobadge + weatherbadge +
          inquattrobadge;
    
       return total;
    
       printf("Total number: %d\n", countBadges(2, 2));
    }
    
    int badgePoly(int x, int y) {
       int bronzebadges, polybadge;
    
       if ((x >= 1 && x <= 20) && (y >= 1 && y <= 20)) {
          bronzebadges = countBadges(x, y);
          if (bronzebadges >= 2) {
             polybadge = 1;
          }
          else {
             polybadge = 0;
          }
       }
       else {
          polybadge = -1;
       }
    
       return polybadge;
    }
    
    
    int badgeAnyColor(int x, int y) {
       int bronzebadges, northbadges, northeastbadges, northwestbadges, eastbadges,
          westbadges, southbadges, southeastbadges, southwestbadges, colorbadge;
    
       double fs, ht, fp, sunexp, irrexp;
    
       if ((x >= 1 && x <= 20) && (y >= 1 && y <= 20)) {
          fs = fieldScore(x, y);
          ht = harvestTime(x, y);
          fp = fieldProfit(x, y);
          sunexp = sunExposure(x, y);
          irrexp = irrigationExposure(x, y);
          bronzebadges = countBadges(x, y);
          northbadges = countBadges(x, y + 1);
          northeastbadges = countBadges(x + 1, y + 1);
          northwestbadges = countBadges(x - 1, y + 1);
          eastbadges = countBadges(x + 1, y);
          westbadges = countBadges(x - 1, y);
          southbadges = countBadges(x - 1, y - 1);
          southeastbadges = countBadges(x + 1, y - 1);
          southwestbadges = countBadges(x - 1, y - 1);
    
          switch (bronzebadges) {
             case 0: {
                if (x == 1 && y == 1){
                   if (northbadges == 0 && northeastbadges == 0 &&
                      eastbadges == 0){
                      colorbadge = 1;
                   }
                   else {
                      colorbadge = 0;
                   }
                }
                if (x == 20 && y == 1) {
                   if (northbadges == 0 && northwestbadges == 0 &&
                      westbadges == 0) {
                      colorbadge = 1;
                   }
                   else {
                      colorbadge = 0;
                   }
               }
                if (x == 1 && y == 20) {
                   if (eastbadges == 0 && southeastbadges == 0 &&
                      southbadges == 0) {
                      colorbadge = 1;
                   }
                   else {
                      colorbadge = 0;
                   }
               }
                if (x == 20 && y == 20) {
                   if (westbadges == 0 && southwestbadges == 0 &&
                      southbadges == 0) {
                      colorbadge = 1;
                   }
                   else {
                      colorbadge = 0;
                   }
                }
                if ((x >= 2 && x <= 19) && (y == 1)) {
                   if (westbadges == 0 && northwestbadges == 0 && northbadges == 0
                      && northeastbadges == 0 && eastbadges == 0) {
                      colorbadge = 1;
                   }
                   else {
                      colorbadge = 0;
                   }
                }
                if ((x >= 2 && x <= 19) && (y == 20)) {
                   if (westbadges == 0 && southwestbadges == 0 && southbadges == 0
                      && southeastbadges == 0 && eastbadges == 0) {
                      colorbadge = 1;
                   }
                   else {
                      colorbadge = 0;
                   }
                }
                if ((x == 20) && (y >= 2 && y <= 19)) {
                   if (northbadges == 0 && northwestbadges == 0 &&
                      westbadges == 0) {
                      colorbadge = 1;
                   }
                   else {
                      colorbadge = 0;
                   }
                }
                if ((x == 1) && (y >= 2 && y <= 19)) {
                   if (northbadges == 0 && northeastbadges == 0 && eastbadges == 0
                      && southeastbadges == 0 && southbadges == 0) {
                      colorbadge = 1;
                   }
                   else {
                      colorbadge = 0;
                   }
                }
                if ((x >= 2 && x <= 19) && (y >= 2 && y <= 19)) {
                   if (northbadges == 0 && northwestbadges == 0 && westbadges == 0
                      && southwestbadges == 0 && southbadges == 0 &&
                         southeastbadges == 0 && eastbadges == 0 &&
                            northeastbadges == 0) {
                      colorbadge = 1;
                   }
                   else {
                      colorbadge = 0;
                   }
                }
                break;
             }
             case 1: {
                if (fs >= 20) {
                   colorbadge = 1;
                }
                else {
                   colorbadge = 0;
                }
                break;
             }
             case 2: {
                if (fp <= 0 || ht < 80 || sunexp > irrexp) {
                   colorbadge = 1;
                }
                else {
                   colorbadge = 0;
                }
                break;
             }
             case 3:
             case 4:
             case 5: {
                if ((x >= 2 && x <= 19) && y == 1) {
                   if (((westbadges + northbadges) >= 2) || ((northbadges +
                      eastbadges) >= 2) || ((westbadges + eastbadges) >= 2)) {
                      colorbadge = 1;
                   }
                   else {
                      colorbadge = 0;
                   }
                }
                if (x == 1 && y == 1) {
                   if ((northbadges + eastbadges) >= 2) {
                      colorbadge = 1;
                   }
                   else {
                      colorbadge = 0;
                   }
                }
                if (x == 20 && y == 1) {
                   if ((northbadges + westbadges) >= 2) {
                      colorbadge = 1;
                   }
                   else {
                      colorbadge = 0;
                   }
                }
                if (x == 1 && y == 20) {
                   if ((southbadges + eastbadges) >= 2) {
                      colorbadge = 1;
                   }
                   else {
                      colorbadge = 0;
                   }
                }
                if (x == 20 && y == 20) {
                   if ((southbadges + westbadges) >= 2) {
                      colorbadge = 1;
                   }
                   else {
                      colorbadge = 0;
                   }
                }
                if ((x >= 2 && x <= 19) && y == 20) {
                   if (((westbadges + southbadges) >= 2) || ((southbadges +
                      eastbadges) >= 2) || ((westbadges + eastbadges) >= 2)) {
                      colorbadge = 1;
                   }
                   else {
                      colorbadge = 0;
                   }
                }
                if ((x == 1) && (y >= 2 && y <= 19)) {
                   if (((northbadges + eastbadges) >= 2) || ((eastbadges +
                      southbadges) >= 2) || ((northbadges + southbadges) >= 2)) {
                      colorbadge = 1;
                   }
                   else {
                      colorbadge = 0;
                   }
                }
                if ((x == 20) && (y >= 2 && y <= 19)) {
                   if (((northbadges + westbadges) >= 2) || ((westbadges +
                      southbadges) >= 2) || ((northbadges + southbadges) >= 2)) {
                      colorbadge = 1;
                   }
                   else {
                      colorbadge = 0;
                   }
                }
                if ((x >= 2 && x <= 19) && (y >= 2 && y <= 19)) {
                   if (((northbadges + westbadges) >= 2) || ((westbadges +
                      southbadges) >= 2) || ((southbadges + eastbadges) >= 2) ||
                         ((northbadges + southbadges) >= 2) || ((westbadges +
                            eastbadges) >= 2)) {
                      colorbadge = 1;
                   }
                   else {
                      colorbadge = 0;
                   }
                }
                break;
             }
          }
       }
       else {
          colorbadge = -1;
       }
       return colorbadge;
    }
    

1 个答案:

答案 0 :(得分:1)

您的countBadges(x,y)函数在printf之前有一个return语句,因此printf永远不会打印。您应将其移至返回上方,并在不需要时将其注释掉。你可能还有printf的其他问题。

int countBadges(int x, int y)
{
    int mavebadge, incirclebadge, herobadge, weatherbadge, inquattrobadge,
        total;

    mavebadge = badgeBoundaryMaven(x, y);
    incirclebadge = badgeInnerCircle(x, y);
    herobadge = badgeLocalHero(x, y);
    weatherbadge = badgeBoringWeather(x, y);
    inquattrobadge = badgeInQuattro(x, y);

    total = mavebadge + incirclebadge + herobadge + weatherbadge +
        inquattrobadge;

    printf("Total number: %d\n", countBadges(2, 2));
    return total;
}

你经常检查你的网格位置,所以我会建议一个函数(或宏),ongrid(x,y),并用它来检查有效的网格边界位置,

int ongrid(x,y)
{
    return ( (x>=1) && (x<=20) && (y>=1) && (y<=20) );
}

您还应检查边界条件,

  if( y + 1<=20 )
      northbadges = countBadges(x, y + 1);
  if( x + 1<=20 && y + 1<=20 )
     northeastbadges = countBadges(x + 1, y + 1);
  if( x - 1>=1 && y + 1<=20 )
     northwestbadges = countBadges(x - 1, y + 1);
  if( x + 1<=20 )
     eastbadges = countBadges(x + 1, y);
  if( x - 1<=20 )
     westbadges = countBadges(x - 1, y);
  if( y - 1>=1 )
     southbadges = countBadges(x, y - 1); //this value was probably not right
  if( x + 1<=20 && y - 1>=1 )
     southeastbadges = countBadges(x + 1, y - 1);
  if( x - 1>=1 && y - 1>=1 )
     southwestbadges = countBadges(x - 1, y - 1);

使用ongrid()函数重写以获得更清晰,

  if( ongrid(x,y + 1) )
      northbadges = countBadges(x, y + 1);
  if( ongrid(x + 1,y + 1) )
     northeastbadges = countBadges(x + 1, y + 1);
  if( ongrid(x - 1,y + 1) )
     northwestbadges = countBadges(x - 1, y + 1);
  if( ongrid(x + 1,y) )
     eastbadges = countBadges(x + 1, y);
  if( ongrid(x - 1,y) )
     westbadges = countBadges(x - 1, y);
  if( ongrid(x,y - 1) )
     southbadges = countBadges(x, y - 1);
  if( ongrid(x + 1,y - 1) )
     southeastbadges = countBadges(x + 1, y - 1);
  if( ongrid(x - 1,y - 1) )
     southwestbadges = countBadges(x - 1, y - 1);

青铜徽章的switch语句没有默认值;如果您的徽章数量明确不同,则需要处理默认值

     default : {
         break;
     }