简易的交互程序(带输入验证)

// ConsoleApplication2.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <stdio.h>
#include<string.h>
#include<stdlib.h>

char ch;
#define A "Enter the number corresponding to the desired pay rate or action:"
#define tax1 0.15
#define tax2 0.20
#define tax3 0.25
#define state1 300*0.15
#define state2 150*0.25
void ShowTheManu();
int caculate(int,int);
const float BasicSalary[] = { 8.75f,9.33f,10.00f,11.20f };
char info[40];
int quit = 0;
int RequestTime();
int main()
{
	ShowTheManu();
	while (quit != 1)
	{
		ch = getchar();
		scanf_s("%*[^\n]%*c");
		if (iswdigit(ch) && ch > '0' && ch <= '5')
		{
			switch(ch)
			{
				case '1':

					caculate(RequestTime(),atoi(&ch));
					printf("%s\n", info);
					getchar();
					ShowTheManu();
					break;
				case '2':
					caculate(RequestTime(),atoi(&ch));
					printf("%s\n", info);
					getchar();
					ShowTheManu();
					break;
				case '3':
					caculate(RequestTime(),atoi(&ch));
					printf("%s\n", info);
					getchar();
					ShowTheManu();
					break;
				case '4':
					caculate(RequestTime(),atoi(&ch));
					printf("%s\n", info);
					getchar();
					ShowTheManu();
					break;
				case '5':
					quit = 1;
					break;
			}
		}
		else
		{
			printf("please enter a opction among above\n");
			ShowTheManu();
		}
	}
}


void ShowTheManu()
{
	for (unsigned int  count = 0; count < strlen(A); count++)
		printf("*");
	printf("\n\n");
	printf("%s\n", A);
	printf("1) $%2.2f/hr					        2) $%2.2f/hr\n"
		   "3) $%2.2f/hr						4) $%2.2f/hr\n"
		   "5) quit\n", BasicSalary[0],BasicSalary[1],BasicSalary[2],BasicSalary[3]);
	for (unsigned int  count = 0; count < strlen(A); count++)
		printf("*");
	printf("\n");
}

int caculate(int n,int a)
{
	float sum;
	float after;
	float tax;
	if ((n - 40) > 0)
		sum = BasicSalary[a - 1] * 1.5f*n;
	else
		sum = BasicSalary[a - 1] * n;
	if (sum <= 300)
		tax = (float)(sum * tax1);
	else if (sum <= 450)
		tax = (float)(state1 + (sum - 300) * tax2);
	else
		tax = (float)(state1 + state2 + (sum - 450)* tax3);
	after = sum - tax;
	sprintf_s(info, "total:%6.2f	tax:%6.2f	pure:%6.2f", sum, tax, after);
	return 0;
}

int RequestTime()
{
	int t;
	while (1)
	{
		printf("please enter your work time(by hours)\n");
		if (scanf_s("%d", &t) == 0 || t <= 0)
		{
			printf("please enter a right time(by hours)\n");
			scanf_s("%*[^\n]%*c");
		}
		else
			break;
	}
	return t;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值