⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 drugcalc.cpp

📁 《C/C++程序设计导论(第二版)》一书的程序源文件
💻 CPP
字号:
// IsEven()  Return true if parameter is an even value
// IN:  x is any integer.
int IsEven (int x)
{	if ((x/2)*2 == x) return (1);
	else return (0);
}


// ReadItem()  Input a single data item from a patient log line
// ASSUMPTION: input lines conform to the documented format
// OUT: sex is returned as the letter `F' or `M' (if input)
//		age is returned as an integer following the input `A' (if input)
//		weight is returned as a float following the input `W' (if input)
#include <iostream.h>
const char WEIGHT = 'W';
const char AGE = 'A';
const char MALE = 'M';
const char FEMALE = 'F';
void ReadItem (char& sex, int& age, float& weight)
{	char prefix;
	cin >> prefix;					// first get the item prefix
	if (prefix == WEIGHT)
		cin >> weight;
	else if (prefix == AGE) 
		cin >> age;
	else if (prefix == MALE) 
		sex = prefix;
	else if (prefix == FEMALE)
		sex = prefix;
	else cout << " ** illegal data item in patient input **" << endl;
}

// ReadPatient()  Input a free-form line of patient data
// ASSUMPTION: input lines conform to the documented format
// OUT: sex will be returned as the letter `F' or `M'
//		age will be returned as an integer following the input `A'
//		weight will be returned as a float following the input `W'
void ReadPatient (char& sex, int& age, float& weight)
{	ReadItem (sex, age, weight);
	ReadItem (sex, age, weight);
	ReadItem (sex, age, weight);
}

// RecomDose()  Return a drug dose based on wt. and sex
// ASSUMPTION: sex is `F' or `M', weight is a positive value
const float F_INC = 0.3;
const float M_INC = 0.5;
const int F_DOSE = 8;
const int M_DOSE = 10;
float RecomDose (char sex, float wt)
{	int size;					// number of dose units
	float dose;					// recommended dose

	if (sex == 'M')
	{	size = wt / M_DOSE;				// truncates on assignment!
		dose = size * M_INC;
	}
	else
	{	size = wt / F_DOSE;
		dose = size * F_INC;
	}
	return (dose);
}

// ActualDose()  Convert a recommended dose to a pill size
//  IN: recommend is the decimal recommended dose
int ActualDose (float recommend)
{	int intdose;					// integer recommended dose
	intdose = recommend;
	if (IsEven(intdose))
		return (intdose);
	else return (intdose-1);					// if not even, decrement by 1
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -