如何使这段代码变得简单 [英] How do I make this code simple
问题描述
您是C编程新手,需要帮助。我想让它变得简单。就像switch语句一样,我希望每个case调用一个函数而不是把整个函数放在那里。但我一直收到错误
Hi new to C programming and need help with something. I'm trying to make it simple. Like for the switch statements I would like for each cases to call on a function instead of putting the whole function there. but I keep getting errors
#include <stdio.h>
#include <stdlib.h>
int
main(void)
{
int user; /*The choice of the user running the program and also what is used for the Switch statements*/
char ch; /*User's answer to run another program or to exit the program. */
do {
printf("\n\t\t\t\t\t\t\tMENU");
printf("\n\t\t\t\t\t\t1. Heart Rate Program\n"); /*If user presses 1, Heart Rate Program will Run*/
printf("\t\t\t\t\t\t2. Leap Year Program\n"); /*Press 2 and Leap Year program will run*/
printf("\t\t\t\t\t\t3. Chronological Order Program\n"); /*Press 3 and Chronological Order program will run*/
printf("\t\t\t\t\t\t4. Speed Ticket Program\n"); /*Press 4 and Speed ticket program will run*/
printf("\t\t\t\t\t\t5.\n");
printf("\t\t\t\t\t\t6.\n\n");
printf("\t\t\t\t\t\tEnter your choice: ");
scanf("%d", &user); /*User picks a number and goes to "user" memory allocation*/
/*Switch statement is used to determine which item the user picks and which program to run*/
switch (user) {
case 1:/*Heart Rate Program*/
{
int VALUE, LOWER=80, UPPER=115;
printf("\n\n\t\t\t\t\t\tHeart Rate:");
scanf("%d", &VALUE);
if(VALUE<LOWER)
printf("\n\t\t\t\t\t\tHeart Rate is LOW\n");
else if(VALUE>LOWER && VALUE<UPPER)
printf("\n\t\t\t\t\t\tHeart Rate is OK\n");
else
printf("\n\t\t\t\t\t\tHeart Rate is HIGH\n");
}
break;
case 2: /* Leap Year Program*/
{
int YEAR;
printf("\n\n\t\t\t\t\t\tYEAR :");
scanf("%d", &YEAR);
if(YEAR%100==0&&YEAR%400==0)
printf("\n\t\t\t\t\t\t%d is a LEAP YEAR!\n", YEAR);
else
printf("\n\t\t\t\t\t\t%d is NOT A LEAP YEAR!\n", YEAR);
}
break;
case 3:/* Chronological Order Program*/
{
int FIRST, SECOND, SMALL, LARGE;
printf("\n\n\t\t\t\t\t\tFIRST :");
scanf("%d", &FIRST);
printf("\n\t\t\t\t\t\tSECOND:");
scanf("%d", &SECOND);
if(FIRST<SECOND)
SMALL=FIRST, LARGE=SECOND;
else if(SECOND<FIRST)
SMALL=SECOND, LARGE=FIRST;
else
return 0;
printf("\n\t\t\t\t\t\t%d is the smaller number.", SMALL);
printf("\n\t\t\t\t\t\t%d is the larger number.", LARGE);
printf("\n\t\t\t\t\t\t%d, %d Ascending Order", SMALL, LARGE);
printf("\n\t\t\t\t\t\t%d, %d Descending Order\n", LARGE, SMALL);
}
break;
case 4: /* Speed Ticket Program*/
{
int SPEED=0;
printf("\n\n\t\t\t\t\t\tSPEED:");
scanf("%d", &SPEED);
if(SPEED>=100)
printf("\n\t\t\t\t\t\tP800 FINE.\n");
else if(SPEED>=80&&SPEED<100)
printf("\n\t\t\t\t\t\tP500 FINE.\n");
else
printf("\n\t\t\t\t\t\tNO FINE.\n");
}
break;
case 5:
case 6:
default :
printf("\t\t\t\t\t\tInvalid Entry! Please Try Again.\n");
}
printf("\n\n\t\t\t\t\t\tWould you like to try another one? Y/N: ");
scanf(" %c", &ch);
system("cls");
}
while (ch == 'y' || ch == 'Y');
return (0);
}
我的尝试:
我已经尝试了我在网上看到过的东西,但它告诉我我做错了。
What I have tried:
i've tried what i've seen online but it tells me that i'm doing things wrong.
推荐答案
我希望每个案例都调用一个函数而不是将整个函数放在那里。但我一直收到错误
I would like for each cases to call on a function instead of putting the whole function there. but I keep getting errors
然后去做。
例如,你可以更换
Then do it.
For instance, you may replace
case 1:/*Heart Rate Program*/
{
int VALUE, LOWER=80, UPPER=115;
printf("\n\n\t\t\t\t\t\tHeart Rate:");
scanf("%d", &VALUE);
if(VALUE<LOWER)
printf("\n\t\t\t\t\t\tHeart Rate is LOW\n");
else if(VALUE>LOWER && VALUE<UPPER)
printf("\n\t\t\t\t\t\tHeart Rate is OK\n");
else
printf("\n\t\t\t\t\t\tHeart Rate is HIGH\n");
}
break;
with
with
case 1:
heart_rate_fun();
break;
为您提供
- 声明
heart_rate_fun
void heart_rate_fun();
- 实现(定义)
heart_rate_fun
移动相关代码 - implement (define) the
heart_rate_fun
by moving the relevant code
void heart_rate_fun()
{
int VALUE, LOWER=80, UPPER=115;
printf("\n\n\t\t\t\t\t\tHeart Rate:");
scanf("%d", &VALUE);
if(VALUE<LOWER)
printf("\n\t\t\t\t\t\tHeart Rate is LOW\n");
else if(VALUE>LOWER && VALUE<UPPER)
printf("\n\t\t\t\t\t\tHeart Rate is OK\n");
else
printf("\n\t\t\t\t\t\tHeart Rate is HIGH\n");
}
这篇关于如何使这段代码变得简单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!