为什么我的程序会跳过切换指令?

时间:2018-11-18 21:46:19

标签: c

我正在尝试编写一个程序,用户输入了7个 floats 之后;它们被存储到一个数组中,然后像这样被打印出来:

DAY VALUE ISTOGRAM 1 37.8 *** 2 40.6 ******

在Istogram列中*的数目由value - 34给出。

我已经编写了这段代码:

#include <stdio.h>
#define OBSERVATION 7
#define MEDIAN 34

int main() {
  float temp[OBSERVATION] = {0};

  printf("Insert the patient's temperature over the course of 7 days: ");
  for(int i = 1; i <= OBSERVATION; i++){
    scanf("%f", &temp[i]);
  }

  printf("DAY\tVALUE\tISTOGRAM\n");
  for(int i = 1; i <= OBSERVATION; i++){
    printf("%6d\t%6g\n", i, temp[i]);
  }
  for(int i = 1; i <= OBSERVATION; i++){
    switch ((int)temp[i] - MEDIAN) {
      case 0: break;
      case 1: printf("\t\t\t\t*");
              break;
      case 2: printf("\t\t\t\t**");
              break;
      case 3: printf("\t\t\t\t***");
              break;
      case 4: printf("\t\t\t\t****");
              break;
      case 5: printf("\t\t\t\t*****");
              break;
      case 6: printf("\t\t\t\t******");
              break;
      case 7: printf("\t\t\t\t*******");
              break;
      case 8: printf("\t\t\t\t********");
              break;
      case 9: printf("\t\t\t\t*********");
              break;
      case 10: printf("\t\t\t\t*********");
               break;
      case 11: printf("\t\t\t\t**********");
               break;
      case 12: printf("\t\t\t\t***********");
               break;
    }
    printf("\n");
  }

  return 0;
}

代码可以正常编译并正确输出前两列,但完全跳过了switch语句。我已经尝试检查将其转换为 int 时是否错误地将0分配给temp[i],但是它没有这样做。它只是跳过switch

在不使用*的情况下如何打印出switch列方面,您是否还有一种更为“紧凑”的方式?

2 个答案:

答案 0 :(得分:0)

我会这样重写您的代码:

#include <stdio.h>
#include "math.h"
#define OBSERVATION 7
#define MEDIAN 34

int main() {
  float temp[OBSERVATION] = {0};
  int iDifference = 0;

  printf("Insert the patient's temperature over the course of 7 days: \n");
  for(int i = 0; i < OBSERVATION; i++){
    scanf("%f", &temp[i]);
  }

然后打印标题:

  printf("DAY\tVALUE\tISTOGRAM\n");

开始行循环:

  for(int i = 0; i < OBSERVATION; i++){
    // calculate the difference integer
    iDifference = round(temp[i] - MEDIAN);
    // don't add stars if the temperature diff is lower than 0
    if(iDifference < 0) iDifference = 0;
    // print the first two columns, notice that the new line isn't added yet
    printf("%6d\t%6.2f\t", i, temp[i]);
    // print the stars
    vDrawStars(iDifference);
    // then write the newline character
    printf("\n");
  }

  return 0;
}

然后画星程序:

void vDrawStars(int prm_iCount){
    int p = 0;
    // I didn't understand the case for it but
    // printf("\t\t\t\t");
    // then draw the needed stars
    for(p = 0; p < prm_iCount; p++)
    {
        printf("*");
    }
    // no new lines, still on the same line.
}

这里是一个演示:https://onlinegdb.com/BJPyvDJRX

答案 1 :(得分:0)

您的代码无法正常工作,因为您无法访问数组temp。数组索引从零开始,因此您应该以{{1​​}}进行索引。

开关:

for(int i = 0; i < OBSERVATION; i++)

可能仅针对以下目的进行了优化:

switch ((int)temp[i] - MEDIAN) {
      case 0: break;
      case 1: printf("\t\t\t\t*");
              break;
      case 2: printf("\t\t\t\t**");
              break;
      case 3: printf("\t\t\t\t***");
              break;
      case 4: printf("\t\t\t\t****");
              break;
      case 5: printf("\t\t\t\t*****");
              break;
      case 6: printf("\t\t\t\t******");
              break;
      case 7: printf("\t\t\t\t*******");
              break;
      case 8: printf("\t\t\t\t********");
              break;
      case 9: printf("\t\t\t\t*********");
              break;
      case 10: printf("\t\t\t\t*********");
               break;
      case 11: printf("\t\t\t\t**********");
               break;
      case 12: printf("\t\t\t\t***********");
               break;
    }
  1. printf格式修饰符 const int val = (int)temp[i] - MEDIAN; if (1 <= val && val <= 12) { // make sure it's between 1 and 12 printf("\t\t\t\t%.*s", val, "***********"); } 具有两个参数-要打印的字符串的长度和字符串本身
  2. 我们打印"%.*s"个字符,val个字符。