#include <stdio.h>
int main(int argc,char* argv[])
{
int nBase = 1500,nPerformance = 0,nTemp = 0,nRes = 0;
int nDuty = 0,nInvigilate = 0,nClass = 0;
double fReserve = 0.0;
printf("输入老师值班天数(不少于5天,不能超过22天):");
nRes = scanf("%d",&nDuty);
while(nRes == 1)
{
if (nDuty >= 5 && nDuty <= 22)
{
break;
}
else if (nDuty < 0)
{
printf("输入数据有误,请输入整数!\n");
}
else
{
printf("输入范围有误,请输入整数!\n");
}
printf("输入老师值班天数(不少于5天,不能超过22天):");
nRes = scanf("%d",&nDuty);
}
printf("输入老师监考次数(不能超过10次):");
nRes = scanf("%d",&nInvigilate);
while(nRes == 1)
{
if (nInvigilate >= 0 && nInvigilate <= 10)
{
break;
}
else if (nInvigilate < 0)
{
printf("输入数据有误,请输入整数!\n");
}
else
{
printf("输入范围有误,请输入整数!\n");
}
printf("输入老师监考次数(不能超过10次):");
nRes = scanf("%d",&nInvigilate);
}
printf("输入老师上课次数(不能超过30次):");
nRes = scanf("%d",&nClass);
while(nRes == 1)
{
if (nClass >= 0 && nClass <= 30)
{
break;
}
else if (nClass < 0)
{
printf("输入数据有误,请输入整数!\n");
}
else
{
printf("输入范围有误,请输入整数!\n");
}
printf("输入老师上课次数(不能超过30次):");
nRes = scanf("%d",&nClass);
}
nPerformance = (nDuty * 60) + (nInvigilate * 30) + (nClass * 64);
nTemp = nPerformance + nBase;
if (nTemp > 5000)
{
fReserve = (nTemp - 5000) * 0.25 + 1000 * 0.2 + 2000 * 0.15 + 2000 * 0.1;
}
else if (nTemp > 4000 && nTemp < 5000)
{
fReserve = (nTemp - 4000) * 0.2 + 2000 * 0.15 + 2000 * 0.1;
}
else if (nTemp > 2000 && nTemp < 4000)
{
fReserve = (nTemp - 2000) * 0.15 + 2000 * 0.1;
}
else if (nTemp < 2000)
{
fReserve = (float)(nTemp * 0.1);
}
printf("绩效工资总额是%d\n",nPerformance);
printf("工资总额是%d\n",nTemp);
printf("需要缴纳的公积金是%0.2f\n",fReserve);
return 0;
}
可以参考参考
#include <stdio.h>
#include <windows.h>
void Input(int &nDuty, int &nInvigilate,int &nClass);//获取值班天数、监考次数、上课次数
void Calclate(int nDuty, int nInvigilate,int nClass);//计算工资、公积金等
int main(int argc,char* argv[])
{
int nDuty = 0,nInvigilate = 0,nClass = 0;
Input(nDuty, nInvigilate, nClass);
Calclate(nDuty, nInvigilate, nClass);
return 0;
}
//输入是否为合法数据 str为获取的字符串,minValue为合法的最小值,maxValue为合法的最大值
//返回值 合法,返回输入的整数,不合法返回0
int IsLegalInput(const char * str, int minValue, int maxValue)
{
int i = 0;
int strValue = 0;
int strLength = strlen(str);//数据长度
for (i = 0; i < strLength; i++)
{
if(('0' == str[i]) || ('1' == str[i]) || ('2' == str[i]) || ('3' == str[i]) || ('4' == str[i]) || ('5' == str[i])
|| ('6' == str[i]) || ('7' == str[i]) || ('8' == str[i]) || ('9' == str[i]))
{
break;
}
else // 输入的不是整数
{
printf("输入数据有误,请输入整数!\n");
return 0;
}
}
//确认是整数了
strValue = atoi(str);
if (strValue < minValue || maxValue < strValue )
{
printf("输入范围有误,请输入%d到%d的整数!\n", minValue, maxValue);
return 0;
}
return strValue;
}
void Input(int &nDuty, int &nInvigilate,int &nClass)
{
int rt;
char input[10];
//输入老师值班天数
do
{
printf("输入老师值班天数(不少于5天,不能超过22天):");
scanf("%s",input);
rt = nDuty = IsLegalInput(input, 5, 22);//是否为合法输入
} while (!rt);
//输入老师监考次数
do
{
printf("输入老师监考次数(不能超过10次):");
scanf("%s",input);
rt = nInvigilate = IsLegalInput(input, 1, 10);//是否为合法输入
} while (!rt);
//输入老师上课次数
do
{
printf("输入老师上课次数(不能超过30次):");
scanf("%s",input);
rt = nClass = IsLegalInput(input, 1, 30);//是否为合法输入
} while (!rt);
}
//计算工资、公积金等
void Calclate(int nDuty, int nInvigilate,int nClass)
{
int nPerformance = 0;
int nTemp = 0;
int nBase = 1500;
nPerformance = (nDuty * 60) + (nInvigilate * 30) + (nClass * 64);
nTemp = nPerformance + nBase;
double fReserve = 0.0;
if (nTemp > 5000)
{
fReserve = (nTemp - 5000) * 0.25 + 1000 * 0.2 + 2000 * 0.15 + 2000 * 0.1;
}
else if (nTemp > 4000 && nTemp < 5000)
{
fReserve = (nTemp - 4000) * 0.2 + 2000 * 0.15 + 2000 * 0.1;
}
else if (nTemp > 2000 && nTemp < 4000)
{
fReserve = (nTemp - 2000) * 0.15 + 2000 * 0.1;
}
else if (nTemp < 2000)
{
fReserve = (float)(nTemp * 0.1);
}
printf("绩效工资总额是%d\n",nPerformance);
printf("工资总额是%d\n",nTemp);
printf("需要缴纳的公积金是%0.2f\n",fReserve);
}
while(1)
{
nRes = scanf("%d",&nDuty);
if (nRes == 1)
{
if (nDuty >= 5 && nDuty <= 22)
{
break;
}
else if (nDuty < 0)
{
printf("输入数据有误,请输入整数!\n");
printf("输入老师值班天数,不少于5天,不能超过22天:");
}
else
{
printf("输入范围有误,请输入整数!\n");
printf("输入老师值班天数,不少于5天,不能超过22天:");
}
}
else if (nRes == 0)
{
scanf("%*c");
printf("输入老师值班天数,不少于5天,不能超过22天:");
}
else if (nRes == EOF)
{
break; //强制中断
}
}
什么叫输入字符?scanf可以转换把任何输入,你要转换成%s??
void Fun(int &nData,int nMax,int nMin,char *Str1,char *Str2)
{
int nRes = 0;
while(1)
{
printf("%s",Str2);
nRes = scanf("%d",&nData);
if (nRes == 1)
{
if (nData >= nMin && nData <= nMax)
{
break;
}
else if (nData < 0)
{
printf("%s",Str1);
}
else
{
printf("%s",Str1);
}
}
else if (nRes == 0)
{
scanf("%*c");
}
else if (nRes == EOF)
{
break; //强制中断
}
}
}
Fun(nDuty,22,5,"输入数据有误,请输入整数!\n","输入老师值班天数,不少于5天,不能超过22天:");
这次测试不会死了。。。
#include <stdio.h>
int main(int argc,char* argv[])
{
int nBase = 1500,nPerformance = 0,nTemp = 0,nRes = 0;
int nDuty = 0,nInvigilate = 0,nClass = 0;
double fReserve = 0.0;
printf("输入老师值班天数(不少于5天,不能超过22天):");
nRes = scanf("%d",&nDuty);
while(nRes == 1)
{
if (nDuty >= 5 && nDuty <= 22)
{
break;
}
else if (nDuty < 0)
{
printf("输入数据有误,请输入整数!\n");
}
else
{
printf("输入范围有误,请输入整数!\n");
}
printf("输入老师值班天数(不少于5天,不能超过22天):");
nRes = scanf("%d",&nDuty);
}
printf("输入老师监考次数(不能超过10次):");
nRes = scanf("%d",&nInvigilate);
while(nRes == 1)
{
if (nInvigilate >= 0 && nInvigilate <= 10)
{
break;
}
else if (nInvigilate < 0)
{
printf("输入数据有误,请输入整数!\n");
}
else
{
printf("输入范围有误,请输入整数!\n");
}
printf("输入老师监考次数(不能超过10次):");
nRes = scanf("%d",&nInvigilate);
}
printf("输入老师上课次数(不能超过30次):");
nRes = scanf("%d",&nClass);
while(nRes == 1)
{
if (nClass >= 0 && nClass <= 30)
{
break;
}
else if (nClass < 0)
{
printf("输入数据有误,请输入整数!\n");
}
else
{
printf("输入范围有误,请输入整数!\n");
}
printf("输入老师上课次数(不能超过30次):");
nRes = scanf("%d",&nClass);
}
nPerformance = (nDuty * 60) + (nInvigilate * 30) + (nClass * 64);
nTemp = nPerformance + nBase;
if (nTemp > 5000)
{
fReserve = (nTemp - 5000) * 0.25 + 1000 * 0.2 + 2000 * 0.15 + 2000 * 0.1;
}
else if (nTemp > 4000 && nTemp < 5000)
{
fReserve = (nTemp - 4000) * 0.2 + 2000 * 0.15 + 2000 * 0.1;
}
else if (nTemp > 2000 && nTemp < 4000)
{
fReserve = (nTemp - 2000) * 0.15 + 2000 * 0.1;
}
else if (nTemp < 2000)
{
fReserve = (float)(nTemp * 0.1);
}
printf("绩效工资总额是%d\n",nPerformance);
printf("工资总额是%d\n",nTemp);
printf("需要缴纳的公积金是%0.2f\n",fReserve);
return 0;
}
bool Fun(int &nData,int nMax,int nMin,char *Str1,char *Str2)
{
int nRes = 0;
while(1)
{
printf("%s",Str2);
nRes = scanf("%d",&nData);
if (nRes == 1)
{
if (nData >= nMin && nData <= nMax)
{
return true;
}
else if (nData < 0)
{
printf("%s",Str1);
}
else
{
printf("%s",Str1);
}
}
else if (nRes == 0)
{
scanf("%*c");
}
else if (nRes == EOF)
{
break; //强制中断
}
}
return false;
}
int main()
{
int nBase = 1500,nPerformance = 0,nTemp = 0,nRes = 0;
int nDuty = 0,nInvigilate = 0,nClass = 0;
float fReserve = 0.0;
Fun(nDuty,22,5,"输入数据有误,请输入整数!\n","输入老师值班天数,不少于5天,不能超过22天:");
Fun(nInvigilate,10,0,"输入数据有误,请输入整数!\n","输入老师监考次数,不能超过10次:");
Fun(nClass,30,0,"输入数据有误,请输入整数!\n","输入老师上课次数,不能超过30次:");
nPerformance = (nDuty * 60) + (nInvigilate * 30) + (nClass * 64);
nTemp = nPerformance + nBase;
if (nTemp > 5000)
{
fReserve = (nTemp - 5000) * 0.25 + 1000 * 0.2 + 2000 * 0.15 + 2000 * 0.1;
}
else if (nTemp > 4000 && nTemp < 5000)
{
fReserve = (nTemp - 4000) * 0.2 + 2000 * 0.15 + 2000 * 0.1;
}
else if (nTemp > 2000 && nTemp < 4000)
{
fReserve = (nTemp - 2000) * 0.15 + 2000 * 0.1;
}
else if (nTemp < 2000)
{
fReserve = (float)(nTemp * 0.1);
}
printf("绩效工资总额是%d\n",nPerformance);
printf("工资总额是%d\n",nTemp);
printf("需要缴纳的公积金是%0.2f\n",fReserve);
system("PAUSE");
return 0;
}
用scanf就行了,再把其模块化一下:
#include
#include
void InputParam(int* pDuty,int* pInvigilate ,int *pClass)
{
int nRes = 0;
assert(pDuty);
assert(pInvigilate);
assert(pClass);
printf("输入老师值班天数(不少于5天,不能超过22天):");
nRes = scanf("%d",pDuty);
while(nRes == 1)
{
if (*pDuty >= 5 && *pDuty <= 22)
{
break;
}
else if (*pDuty < 0)
{
printf("输入数据有误,请输入整数!\n");
}
else
{
printf("输入范围有误,请输入整数!\n");
}
printf("输入老师值班天数(不少于5天,不能超过22天):");
nRes = scanf("%d",pDuty);
}
printf("输入老师监考次数(不能超过10次):");
nRes = scanf("%d",pInvigilate);
while(nRes == 1)
{
if (*pInvigilate >= 0 && *pInvigilate <= 10)
{
break;
}
else if (*pInvigilate < 0)
{
printf("输入数据有误,请输入整数!\n");
}
else
{
printf("输入范围有误,请输入整数!\n");
}
printf("输入老师监考次数(不能超过10次):");
nRes = scanf("%d",pInvigilate);
}
printf("输入老师上课次数(不能超过30次):");
nRes = scanf("%d",pClass);
while(nRes == 1)
{
if (*pClass >= 0 && *pClass <= 30)
{
break;
}
else if (*pClass < 0)
{
printf("输入数据有误,请输入整数!\n");
}
else
{
printf("输入范围有误,请输入整数!\n");
}
printf("输入老师上课次数(不能超过30次):");
nRes = scanf("%d",pClass);
}
}
struct RES
{
int nPerformance;
int nSum;
double fReserve;
};
struct RES Calc(init base,int nDuty,int nInvigilate,int nClass)
{
struct RES res ={0};
res.nPerformance = (nDuty * 60) + (nInvigilate * 30) + (nClass * 64);
res.nSum = nPerformance + nBase;
if ( res.nSum > 5000)
{
fReserve = ( res.nSum - 5000) * 0.25 + 1000 * 0.2 + 2000 * 0.15 + 2000 * 0.1;
}
else if ( res.nSum > 4000 && res.nSum < 5000)
{
fReserve = ( res.nSum - 4000) * 0.2 + 2000 * 0.15 + 2000 * 0.1;
}
else if ( res.nSum > 2000 && res.nSum < 4000)
{
fReserve = ( res.nSum - 2000) * 0.15 + 2000 * 0.1;
}
else if ( res.nSum < 2000)
{
fReserve = (float)( res.nSum * 0.1);
}
printf("绩效工资总额是%d\n",res.nPerformance);
printf("工资总额是%d\n",res.nSum);
printf("需要缴纳的公积金是%0.2f\n",res.fReserve);
return res;
}
int main()
{
int nBase = 1500;
struct RES res = {0};
int nDuty = 0,nInvigilate = 0,nClass = 0;
InputParam(&nDuty,&nInvigilate,&nClass);
res = Calc(base,nDuty,nInvigilate,nClass);
return 0;
}
#include "stdio.h"
#include "assert.h"