转换时间如何改进我的代码?

时间:2016-10-17 10:00:38

标签: c

刚开始编程,所以任何建议或建设性批评都会受到高度赞赏。我很确定有办法缩短这个,我不知道如何,特别是功能的使用。

#include <stdio.h>
#include <stdlib.h>

int input, check = 1, ans;
int menu ();
int convert24(int hour, int min, int sec, int meridiem);
int convert12(int hour, int min, int sec, int meridiem);
int result(int hour, int min, int sec, int meridiem);
void processAnother();

int main()
{
do
{
    menu();
    processAnother();
    system("cls");
}while (ans = 'y');

}
int menu ()
{
    int hour, min, sec, meridiem;
    printf("[1] Convert to 24-hour notation\n");
    printf("[2] Convert to 12-hour notation\n");
    printf("[3] Quit\n");
    scanf("%d", &input);


    switch(input)
    {
        case 1 :
            {
                printf("Enter hours (1-12):minute(1-59):seconds(1-59): ");
                scanf("%d %d %d", &hour, &min, &sec);
                printf("[1] AM or [2] PM: ");
                scanf(" %d", &meridiem);
                return convert24(hour, min, sec, meridiem);
                break;
            }
        case 2 :
            {
                printf("Enter hours(1-24):minute(1-59):seconds(1-59): ");
                scanf("%d %d %d", &hour, &min, &sec);
                printf("[1]AM or [2]PM: ");
                scanf(" %d", &meridiem);
                return convert12(hour, min, sec, meridiem);
                break;
            }
        default :
            {
                printf("Goodbye :)");
                exit(0);
            }
    }
}
int convert24(int hour, int min, int sec, int meridiem)
{
    if ((hour > 12 || hour < 0))
    {
        printf("\nError! Invalid Hour\n");
        check = 0;
    }
    if ((min >= 60 || min <0))
    {
        printf("\nError! Invalid Minute\n");
        check = 0;
    }
    if ((sec >= 60 || sec <0))
    {
        printf("\nError! Invalid Second\n");
        check = 0;
    }
    if (meridiem > 2 || meridiem < 0)
    {
        printf("\nError! Invalid Meridiem\n");
        check = 0;
    }
    if (check == 1)
    {
        if (meridiem == 1)
        {
            if (hour == 12)
            {
                hour = 0;
                result(hour, min, sec, meridiem);
            }
            else
            {
                result(hour, min, sec, meridiem);
            }
        }
        else if (meridiem == 2)
        {
            if (hour == 12)
            {
                hour = 12;
                result(hour, min, sec, meridiem);
            }
            else
            {
                hour+=12;
                result(hour, min, sec, meridiem);
            }
        }
    }
    processAnother();
}
int convert12(int hour, int min, int sec, int meridiem)
{
    if ((hour > 24 || hour < 0))
    {
        printf("\nError! Invalid Hour\n");
        check = 0;
    }
    if ((min >= 60 || min <0))
    {
        printf("\nError! Invalid Minute\n");
        check = 0;
    }
    if ((sec >= 60 || sec <0))
    {
        printf("\nError! Invalid Second\n");
        check = 0;
    }
    if (meridiem > 2 || meridiem < 0)
    {
        printf("\nError! Invalid Meridiem\n");
        check = 0;
    }
    if (check == 1)
    {
        if (meridiem == 1)
        {
            if (hour <= 12)
            {
                result(hour, min, sec, meridiem);
            }
            else
            {
                hour -= 12;
                result(hour, min, sec, meridiem);
            }
        }
        else if (meridiem == 2)
        {
            if (hour <= 12)
            {
                result(hour, min, sec, meridiem);
            }
            else
            {
                hour -= 12;
                result(hour, min, sec, meridiem);
            }
        }
    }
    processAnother();
}
int result(int hour, int min, int sec, int meridiem)
{
    char *x;
    int y;
    if(meridiem == 1)
    {
       x = "AM";
    }
    else if (meridiem == 2)
    {
       x = "PM";
    }
    if (input == 1)
    {
        y = 24;
    }
    else if (input == 2)
    {
        y = 12;
    }
    printf("The %d-hr Notation is : %d:%d:%d %s\n",y, hour, min, sec, x);
    return;
}
void processAnother()
{
    printf("Process another [y/n]");
    scanf("%c", &ans);
    return;
}

非常感谢。

1 个答案:

答案 0 :(得分:0)

Properties p = System.getProperties(); Enumeration keys = p.keys(); while (keys.hasMoreElements()) { String key = (String)keys.nextElement(); String value = (String)p.get(key); System.out.println(key + ": " + value); } ,用作ans但声明为char,它会起作用,但它很丑陋,也不会滥用全局变量,{ {1}},intinput可以在其他地方声明,其中一些甚至不需要声明。

checkans上你在第一行的20行做同样的事情,写一个函数,如果它们是错误则返回0,否则返回1。

我通常将convert12()函数放在我文件的底部,这样我就不会对文件中的其他所有函数进行原型化

每次调用大部分函数时,不要传递convert24()main()hourmin,而是创建一个sec,其中包含每个元素并传递它的指针。

meridiem应该声明为struct,请参阅here了解更多信息

对于这一个我使用多个步骤

第一

main()

可以成为:

main(void)

我遵循了之前的建议,最后移动了int convert12(int hour, int min, int sec, int meridiem) { if ((hour > 24 || hour < 0)) { printf("\nError! Invalid Hour\n"); check = 0; } if ((min >= 60 || min <0)) { printf("\nError! Invalid Minute\n"); check = 0; } if ((sec >= 60 || sec <0)) { printf("\nError! Invalid Second\n"); check = 0; } if (meridiem > 2 || meridiem < 0) { printf("\nError! Invalid Meridiem\n"); check = 0; } if (check == 1) { if (meridiem == 1) { if (hour <= 12) { result(hour, min, sec, meridiem); } else { hour -= 12; result(hour, min, sec, meridiem); } } else if (meridiem == 2) { if (hour <= 12) { result(hour, min, sec, meridiem); } else { hour -= 12; result(hour, min, sec, meridiem); } } } processAnother(); } 的电话

我知道它不会像这样工作,但你现在可以这样写:

int convert12(my_time_t *time, int input)
{
  if (are_time_correct(time, input))
    {
      if (time->meridiem == 1)
        {
          if (time->hour <= 12)
            {
            }
          else
            {
              time->hour -= 12;
            }
        }
      else if (time->meridiem == 2)
        {
          if (time->hour <= 12)
            {
            }
          else
            {
              time->hour -= 12;
            }
        }
    }
  result(time, input);
  processAnother();
}

就在我们测试meridiem介于1和2之间之前,所以我们可以像这样编写这个函数

result

对convert24()

执行相同的过程

如果您遵循我的所有指示,您应该有这样的事情:

int convert12(my_time_t *time, int input)
{
  if (are_time_correct(time, input))
    {
      if (time->meridiem == 1 && time->hour > 12)
        {
          time->hour -= 12;
        }
      else if (time->meridiem == 2 && time->hour > 12)
        {
          time->hour -= 12;
        }
    }
  result(time, input);
  processAnother();
}

你也没有测试scanf是否返回了某些东西,他们在某些情况下你编程会出现段错误