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

📄 drugtest.cpp

📁 C/C++程序设计导论(第二版)》程序源文件
💻 CPP
字号:
// drugtest.cpp  Compare new prescription with currently
//  prescribed meds for illegal and dangerous combinations.
// ASSUMPTION: the files "patients.txt" and "drugs.txt" exist.
#include <iostream.h>
#include <fstream.h>
const int END = -1;




// CurrentlyTaking()  Test if newdrug is in the file "temp.txt"
// IN: contradrug is a drug the patient should not take.
int CurrentlyTaking (int contradrug)
{	int currently_taking, found = 0;
	ifstream fin ("temp.txt", ios::in);
	while (fin >> currently_taking && !found)
		if (currently_taking == contradrug)
			found = 1;
	fin.close();		// close so file might be re-opened in a subsequent call
	return (found);
}

// FindDrug()  Locate the correct drug record in "drugs.txt"
// ASSUMPTIONS: file "drugs.txt" is opened to `drugfile'.
//	List sentinal is END. newdrug must be found in file.
// IN: newdrug is the drug to be found.
// IN/OUT:  drugfile is opened to "drugs.txt"
void FindDrug (ifstream& drugfile, int newdrug)
{	int thisdrug;
	drugfile >> thisdrug;				// first drug for each record
	while (drugfile && thisdrug != newdrug)
		{ 	drugfile >> thisdrug;		// if wrong drug, skip over list
			while (thisdrug > 0)
				drugfile >> thisdrug;
			drugfile >> thisdrug;		// and get next drug record
		}
}

// MakeList ()  Make a list of current meds in file "temp.txt"
// ASSUMPTIONS: patfile is opened to the file "patients.txt". outfile is
//	opened to the file "temp.txt". Lowest patID is 1111, highest drug
//	number is 99. Assumes patient is in file. Sentinel is END.
// IN: 	patID is the patient ID number.
// IN/OUT:  patfile is opened to "patients.txt", outfile to "temp.txt"
void MakeList (int patID, ifstream& patfile, ofstream& outfile)
{	int drug, id;
	patfile >> id;
	while (patfile && id != patID)		// find the correct patient
		patfile >> id;
	if (patfile)
	{	patfile >> drug;			// make list of current meds
		while (drug > 0)			// in file using `outfile'
		{	outfile << "  " << drug;
			patfile >> drug;
		}
	}
}

void main ()
{	int patID, newdrug, contradrug;
	ifstream patfile ("patients.txt", ios::in);	// patient records
	ifstream drugfile ("drugs.txt", ios::in);	// drug records
	ofstream outfile ("temp.txt", ios::out);	// list of current meds

	cout << "enter patient ID and drug number: ";
	cin >> patID >> newdrug;

// Make a list of the current medications for this patient in file "temp.txt"
	MakeList (patID, patfile, outfile);
	outfile.close();
// Find the new drug in the "drugs.txt" database file
	FindDrug (drugfile, newdrug);
// Search the contra-indicated drugs for each drug the patient is taking ("temp.txt')
	if (drugfile)
		if (drugfile >> contradrug)
		  while (contradrug > 0)
		  {	if (CurrentlyTaking (contradrug))
					cout << "*WARNING; drug interaction possible!"<< endl;
			  drugfile >> contradrug;
		  }
}

⌨️ 快捷键说明

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