我已经写了下面的代码。它首先检查用户的输入,但是如果条件为假,它不会再次询问“输入员工的密码(1、2、3、4、5)”。
如何生成下图所示的输出?
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
int main (void)
{
char code;
printf ("\nEnter employee's number code (1,2,3,4,5): ");
scanf ("%c", &code);
//Check whether a Character Entered by User is Alphabet or not
if (isalpha(code) == 0)
{
//printf("%c is an number.\n", code);
int x = code - '0'; // convert alpha char to number
fun(x);
//printf("%d is an number.\n", x);
}
else
printf( "Unrecognised paycode. Please only enter '1' for manager, '2' hourly, '3' commission, '4' Pieceworker or '' \n");
return 0;
}
void fun(int code)
{
float total = 0;
float pay, sales, hours, salary, hourlyTotal, comm_total, pice_total;
float item_a, item_b, item_c;
float item_1, item_2, item_3;
char s;
while (code != -1)
{
switch (code)
{
case 1:
printf ("Enter the manager's paycode: ");
scanf ("%f", &pay);
printf ("Manager salary of:$%.2f\n\n", pay);
total += pay;
salary = total;
break;
case 2:
printf ("Enter hourly worker's pay rate: ");
scanf ("%f", &pay);
printf ("Enter the number of hours worked: ");
scanf ("%f", &hours);
if (hours > 40)
pay = (pay * 40) + ((hours - 40) * (pay * 1.5));
else
pay = pay * hours;
printf ("Wages are :$%.2f \t <$%.2f regular and $0.00 overtime> \n\n", pay, pay);
total += pay;
hourlyTotal = pay;
break;
case 3:
printf ("Enter commission employee's gross item_a sales: ");
scanf ("%f", &item_a);
item_a = item_a * .057;
printf ("Enter commission employee's gross item_b sales: ");
scanf ("%f", &item_b);
item_b = item_b * .064;
printf ("Enter commission employee's gross item_c sales: ");
scanf ("%f", &item_c);
item_c = item_c * .072;
sales = item_a +item_b+item_c;
pay = 250 + sales;
printf ("Commsion wage is:$%.2f <$250.00 base + $%.2f commission <$%.2f item A, $%.2f item B, $%.2f item C> \n\n", pay, sales, item_a, item_b, item_c);
total += pay;
comm_total = pay;
break;
case 4:
printf ("\nEnter the number of item_1 completed: ");
scanf ("%f", &item_1);
item_1 = item_1*22.50;
printf ("\nEnter the number of item_2 completed: ");
scanf ("%f", &item_2);
item_2 = item_2*24.50;
printf ("\nEnter the number of item_3 completed: ");
scanf("%cf", &item_3);
//if (isdigit(s))
//printf("%c number \n", s);
//item_3 = s;
item_3 = item_3*26.00;
pay = item_1 + item_2 + item_3;
printf ("Pieceworker wage is:$%.2f <item_1 $%.2f, item_2 $%.2f, item_3 $%.2f> \n\n", pay,item_1, item_2,item_3);
total += pay;
pice_total = pay;
break;
case 5:
printf ("\n Manager: Employees : 1 Total wages:$%.2f ", salary);
printf ("\n Hourly: Employees : 1 Total wages:$%.2f ", hourlyTotal);
printf ("\n Commission: Employees : 1 Total wages:$%.2f ", comm_total);
printf ("\n Pieceworker: Employees : 1 Total wages:$%.2f ", pice_total);
printf ("\n Net Total: Employees : 4 Total wages:$%.2f ", total);
break;
default:
printf ("You have entered an invalid code.\n");
}
printf ("\nEnter employee's number code (1,2,3,4,5): ");
scanf ("%d", &code);
}
printf ("\nThe total payroll for the week is: %.2f\n", total);
}
输出期望:
如何查看所有用户输入数据的天气数字或字符?
答案 0 :(得分:0)
只需不检查输入是否为int。 然后,在switch中,执行以下操作:(请注意char的引号)
addSubview