gpt4 book ai didi

c - 输出有时会失败

转载 作者:行者123 更新时间:2023-11-30 18:11:04 25 4
gpt4 key购买 nike

我正在尝试使用 C 程序查找当月总工资。我的输出有时会失败。将代码和输出放在下面:

#include <stdio.h>
#include <conio.h>

void main() {
int id;
float hours;
float mgsalary;
clrscr();
printf(
" Press ID : \n Press 1 if Staff: \n Press 2 if Supervisor: \n Press 3 if Manager: \n Press 4 if President: \n");
scanf("%d", &id);
printf(" Enter hours per week \n");
scanf("%f", &hours);
if (id == 1) {
printf(" Position ID: 1 \n");
mgsalary = (hours * 62.5) * 4.28;
printf(" Positon/Rank: Staff \n");
printf(" Rate per hour: 62.5 php \n");
printf(" Monthly Gross Salary : %f php \n ", mgsalary);
} else if (id == 2) {
mgsalary = (hours * 125) * 4.28;
printf(" Position ID: 2 \n");
printf(" Position/Rank: Supervisor \n");
printf(" Rate per hour: 125 php \n");
printf(" Monthly Gross Salary : %f php \n", mgsalary);
} else if (id == 3) {
mgsalary = (hours * 187.5) * 4.28;
printf(" Position ID: 3 \n");
printf(" Position/Rank: Manager \n");
printf(" Rate per hour: 187.5 php \n");
printf(" Monthly Gross Salary : %f php \n", mgsalary);
} else if (id == 4) {
mgsalary = (hours * 375) * 4.28;
printf(" Position ID: 4 \n");
printf(" Position/Rank: President \n");
printf(" Rate per hour: 375 php \n");
printf(" Monthly Gross Salary: %f php \n", mgsalary);
} else
printf(" Invalid Input");
getch();
}

//运行程序(输出)

Press 1 if Staff: 
Press 2 if Supervisor:
Press 3 if Manager:
Press 4 if President:

//我按 1 选择了员工

Enter numbers of hours work:

//我输入了 33 作为工作小时数

Position ID: 1 
Positon/Rank: Staff
Rate per hour: 62.5 php
Monthly Gross Salary : 8872.5000 php (sometimes it came out 10000000.000 php or null)

最佳答案

以下建议代码:

  1. 消除了来自 conio.h 的不可移植调用
  2. 正确检查 scanf() 错误
  3. 格式化代码以提高可读性
  4. 使用 switch() 语句而不是一系列 if() 语句
  5. 使用有意义的名称正确声明“神奇”数字
  6. 使用enum语句为用户选择输入提供有意义的名称
  7. 正确地将所有文字值声明为 float 而不是默认的 double
  8. 干净地编译
  9. main() 函数使用有效签名
  10. 编辑命名等,根据评论进行更正

现在,建议的代码:

#include <stdio.h>   // perror(), getchar(), fprintf(), printf(), scanf()
//#include <conio.h>
#include <stdlib.h> // exit(), EXIT_FAILURE

#define WEEKS_PER_MONTH 4.28f
#define STAFF_WEEKLY_RATE 62.5f
#define SUPERVISOR_WEEKLY_RATE 125.0f
#define MANAGER_WEEKLY_RATE 187.5f
#define PRESIDENT_WEEKLY_RATE 375.0f

enum
{
dummy,
STAFF,
SUPERVISOR,
MANAGER,
PRESIDENT
};


int main( void )
{
int id;
float hours;
float mgsalary;
int ch;



/* clrscr(); Suggest using the ANSI excape sequences */
printf( "%s\n",
" Press ID : \n"
" Press 1 if Staff: \n"
" Press 2 if Supervisor: \n"
" Press 3 if Manager: \n"
" Press 4 if President: \n");
id = getchar();

printf(" Enter hours per week \n");
if( scanf("%f", &hours) != 1 )
{
fprintf( stderr, "scanf for hours failed\n" );
exit( EXIT_FAILURE );
}


switch( id )
{
case STAFF:
printf(" Position ID: 1 \n");
mgsalary = (hours * STAFF_WEEKLY_RATE) * WEEKS_PER_MONTH;
printf(" Positon/Rank: Staff \n");
printf(" Rate per hour: 62.5 php \n");
printf(" Monthly Gross Salary : %f php \n ", mgsalary);
break;

case SUPERVISOR:
mgsalary = (hours * SUPERVISOR_WEEKLY_RATE) * WEEKS_PER_MONTH;
printf(" Position ID: 2 \n");
printf(" Position/Rank: Supervisor \n");
printf(" Rate per hour: 125 php \n");
printf(" Monthly Gross Salary : %f php \n", mgsalary);
break;

case MANAGER:
mgsalary = (hours * MANAGER_WEEKLY_RATE) * WEEKS_PER_MONTH;
printf(" Position ID: 3 \n");
printf(" Position/Rank: Manager \n");
printf(" Rate per hour: 187.5 php \n");
printf(" Monthly Gross Salary : %f php \n", mgsalary);
break;

case PRESIDENT:
mgsalary = (hours * PRESIDENT_WEEKLY_RATE) * WEEKS_PER_MONTH;
printf(" Position ID: 4 \n");
printf(" Position/Rank: President \n");
printf(" Rate per hour: 375 php \n");
printf(" Monthly Gross Salary: %f php \n", mgsalary);
break;

default:
printf(" Invalid Menu Selection\n");
break;
} /* end switch */

while( (ch = getchar()) != EOF && ch != '\n' );
getchar();
}

关于c - 输出有时会失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52462625/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com