// this is the code so far i have #include<iostream> #include <stdlib.h> #includ
ID: 674135 • Letter: #
Question
// this is the code so far i have
#include<iostream>
#include <stdlib.h>
#include <string>
using namespace std;
typedef struct Node{
string Name;
int ID;
int age;
char smoker;
char HBP;
char HFD;
int Points;
Node *ptr;
}Node;
//need to display records for 6 patients
//this program is not working, needs to fix , please try to fix and make it to input 6 patients first and then
//1) Display all patients
//2) Display total number of smokers
//3) Search for patient with highest point
//4) Move the patient with highest point to the end.
//please try to run before you send me the solution thanks.
void displayPatient(Node *p){
int agePoints, hbpPoints, hfdPoints, smkPoints;
for (int i= 0;i <2; i++)
{
cout << " Please enter the name of the patient : ";
cin>>p->Name;
cout << "Please enter the ID";
cin >> p->ID;
cout << " Please enter the age of the patient : ";
cin >> p->age;
cout << " Is he/she a smoker (y/n) : ";
cin >> p->smoker;
cout << " Does he/she have high blood pressure? (y/n) : ";
cin >> p->HBP;
cout << " Does he/she have a high fat diet? (y/n) : ";
cin >> p->HFD;
cout << endl;
p->ptr = NULL;
if (p-> age < 18)
{
agePoints = 2;
}
else if (p->age > 18 && p->age <30)
{
agePoints = 3;
}
else
{
agePoints = 4;
}
if (p->smoker == 'Y' || p->smoker == 'y')
{
p->smoker = 'Y';
smkPoints = 4;
}
else
{
p->smoker = 'N';
smkPoints = 0;
}
if (p->HBP == 'Y' || p->HBP == 'y')
{
p->HBP = 'Y';
hbpPoints = 4;
}
else
{
p->HBP = 'N';
hbpPoints = 0;
}
if (p->HFD == 'Y' || p->HFD == 'y')
{
p->HFD = 'Y';
hfdPoints = 1;
}
else
{
p->HFD = 'N';
hfdPoints = 0;
}
p->Points = agePoints + smkPoints + hbpPoints + hfdPoints;
}
}
void displayAllPatients(Node *root){
Node *temp = root;
while(temp != NULL){
displayPatient(temp);
temp = temp->ptr;
}
}
void smokers(Node *root){
Node *temp = root;
int count = 0;
while(temp != NULL){
if(temp->smoker == 'Y')
count++;
temp = temp->ptr;
}
cout<<count;
}
void moveHighestPatientToEnd(Node *root,Node *patient,Node *previousPatient){
Node *temp = root;
while(temp->ptr != NULL){
temp = temp->ptr;
}
previousPatient->ptr = patient->ptr;
temp->ptr = patient;
patient->ptr = NULL;
}
void highestPoint(Node *root){
Node *temp = root;
int highestPoint = 0;
Node *patient;
Node *previous;
while(temp != NULL){
if(temp->Points > highestPoint){
highestPoint = temp->Points;
patient = temp;
}
previous = temp;
temp = temp->ptr;
}
displayPatient(patient);
moveHighestPatientToEnd(root,patient,previous);
}
int main()
{
int choice;
Node *root = NULL;
while(1)
{
cout<<"Patient Menu ";
cout<<"============================================= ";
cout<<"1. Display the patients ";
cout<<"2. Display total number of smokers ";
cout<<"3. Search for patient with highest point ";
cout<<"4. Move the patient with highest point to the end. ";
cout<<"3. Exit the program";
cout<<"============================================= ";
cout<<"Enter your selection: ";
cin>>choice;
switch(choice)
{
case 1:
displayAllPatients(root);
break;
case 2:
cout<<"The number of smokers are: ";
smokers(root);
break;
case 3:
highestPoint(root);
break;
case 4:
moveHighestPatientToEnd(root);//need to fix here
break;
case 5:
exit(1);
default: cout<<"Invalid choice.";
}
}
}
Explanation / Answer
Answer:
Program code:
// Patient-Smokers-BP.cpp :
#include "stdafx.h"
#include <iostream>
#include<iostream>
#include <stdlib.h>
#include <string>
using namespace std;
typedef struct Node
{
string Name;
int ID;
int age;
char smoker;
char HBP;
char HFD;
int Points;
Node *ptr;
};
Node *startPtr = NULL;
//need to display records for 6 patients
//this program is not working, needs to fix , please try to fix and make it to input 6 patients first and then
//1) Display all patients
//2) Display total number of smokers
//3) Search for patient with highest point
//4) Move the patient with highest point to the end.
//please try to run before you send me the solution thanks.
void getInfo(Node **root)
{
Node *p;
int agePoints, hbpPoints, hfdPoints, smkPoints;
for (int i = 0;i <6; i++)
{
p=new Node;
cout<<"Enter details of patient "<<(i+1)<<": "<<endl;
cout << " Please enter the name of the patient: ";
cin>>p->Name;
cout << " Please enter the ID: ";
cin >> p->ID;
cout << " Please enter the age of the patient: ";
cin >> p->age;
cout << " ts he/she a smoker (y/n) : ";
cin >> p->smoker;
cout << " toes he/she have high blood pressure? (y/n): ";
cin >> p->HBP;
cout << " Does he/she have a high fat diet? (y/n): ";
cin >> p->HFD;
cout << endl;
p->ptr = NULL;
if (p-> age < 18)
{
agePoints = 2;
}
else if (p->age > 18 && p->age <30)
{
agePoints = 3;
}
else
{
agePoints = 4;
}
if (p->smoker == 'Y' || p->smoker == 'y')
{
p->smoker = 'Y';
smkPoints = 4;
}
else
{
p->smoker = 'N';
smkPoints = 0;
}
if (p->HBP == 'Y' || p->HBP == 'y')
{
p->HBP = 'Y';
hbpPoints = 4;
}
else
{
p->HBP = 'N';
hbpPoints = 0;
}
if (p->HFD == 'Y' || p->HFD == 'y')
{
p->HFD = 'Y';
hfdPoints = 1;
}
else
{
p->HFD = 'N';
hfdPoints = 0;
}
p->Points = agePoints + smkPoints + hbpPoints + hfdPoints;
if (*root == NULL)
{
*root = p;
}
else
{
Node *p1 = *root;
while (p1->ptr != NULL)
p1 = p1->ptr;
p1->ptr = p;
}
}
}
void displayPatient(Node *p)
{
if(p!=NULL)
{
cout<<endl;
cout << "Patient id: " <<p->ID <<endl;
cout << " Name : " << p->Name << endl;
cout << " Age : " << p->age << endl;
cout << " Smoker : " << p->smoker << endl;
cout << " Blood Pressure : " << p->HBP << endl;
cout << " Fat Diet : " << p->HFD << endl;
cout << " Points : " << p->Points << endl;
}
}
void displayAllPatients(Node *root)
{
Node *temp = root;
while(temp != NULL)
{
displayPatient(temp);
temp = temp->ptr;
}
}
void smokers(Node *root)
{
Node *temp = root;
int count = 0;
while(temp != NULL){
if(temp->smoker == 'Y')
count++;
temp = temp->ptr;
}
cout<<count<<endl;
}
void moveHighestPatientToEnd(Node **root)
{
Node *temp, *prevNode, *highestPointsNode, *prevHighestPointsNode;
prevNode = prevHighestPointsNode = highestPointsNode = temp = *root;
while(temp != NULL)
{
if(highestPointsNode->Points > temp->Points)
{
prevHighestPointsNode = prevNode;
highestPointsNode = temp;
}
prevNode = temp;
temp = temp->ptr;
}
prevHighestPointsNode = highestPointsNode->ptr;
temp = *root;
while(temp->ptr != NULL)
{
temp =temp->ptr;
}
temp->ptr = highestPointsNode;
highestPointsNode->ptr = NULL;
}
void highestPoint(Node *root)
{
Node *temp, *highestPointsNode;
highestPointsNode = temp = root;
while(temp != NULL)
{
if(highestPointsNode->Points > temp->Points)
highestPointsNode = temp;
temp = temp->ptr;
}
displayPatient(highestPointsNode);
}
int main()
{
int choice;
struct Node *patient = NULL, *previous = NULL;
struct Node *root = NULL;
getInfo(&root);
while(1)
{
cout<<" Patient Menu ";
cout<<"============================================= ";
cout<<"1. Display the patients ";
cout<<"2. Display total number of smokers ";
cout<<"3. Search for patient with highest point ";
cout<<"4. Move the patient with highest point to the end. ";
cout<<"5. Exit the program ";
cout<<"============================================= ";
cout<<"Enter your selection: ";
cin>>choice;
switch(choice)
{
case 1:
displayAllPatients(root);
break;
case 2:
cout<<"The number of smokers are: ";
smokers(root);
cout<<endl;
break;
case 3:
highestPoint(root);
break;
case 4:
moveHighestPatientToEnd(&root);
cout<<endl;
break;
case 5:
system("pause");//in-order to view the data entered
//exit(1);
default: cout<<"Invalid choice.";
}
}
system("pause");
return 0;
}
Sample Output:
Enter details of patient 1:
Please enter the name of the patient: Robert
Please enter the ID: 123
Please enter the age of the patient: 34
ts he/she a smoker (y/n) : y
toes he/she have high blood pressure? (y/n): y
Does he/she have a high fat diet? (y/n): y
Enter details of patient 2:
Please enter the name of the patient: William
Please enter the ID: 1124
Please enter the age of the patient: 43
ts he/she a smoker (y/n) : y
toes he/she have high blood pressure? (y/n): n
Does he/she have a high fat diet? (y/n): y
Enter details of patient 3:
Please enter the name of the patient: Oliver
Please enter the ID: 23
Please enter the age of the patient: 33
ts he/she a smoker (y/n) : n
toes he/she have high blood pressure? (y/n): n
Does he/she have a high fat diet? (y/n): n
Enter details of patient 4:
Please enter the name of the patient: Patrick
Please enter the ID: 125
Please enter the age of the patient: 26
ts he/she a smoker (y/n) : n
toes he/she have high blood pressure? (y/n): y
Does he/she have a high fat diet? (y/n): n
Enter details of patient 5:
Please enter the name of the patient: Ukrain
Please enter the ID: 127
Please enter the age of the patient: 54
ts he/she a smoker (y/n) : n
toes he/she have high blood pressure? (y/n): y
Does he/she have a high fat diet? (y/n): y
Enter details of patient 6:
Please enter the name of the patient: Sam
Please enter the ID: 128
Please enter the age of the patient: 34
ts he/she a smoker (y/n) : y
toes he/she have high blood pressure? (y/n): y
Does he/she have a high fat diet? (y/n): n
Patient Menu
=============================================
1. Display the patients
2. Display total number of smokers
3. Search for patient with highest point
4. Move the patient with highest point to the end.
5. Exit the program
=============================================
Enter your selection: 1
Patient id: 123
Name : Robert
Age : 34
Smoker : Y
Blood Pressure : Y
Fat Diet : Y
Points : 13
Patient id: 1124
Name : William
Age : 43
Smoker : Y
Blood Pressure : N
Fat Diet : Y
Points : 9
Patient id: 23
Name : Oliver
Age : 33
Smoker : N
Blood Pressure : N
Fat Diet : N
Points : 4
Patient id: 125
Name : Patrick
Age : 26
Smoker : N
Blood Pressure : Y
Fat Diet : N
Points : 7
Patient id: 127
Name : Ukrain
Age : 54
Smoker : N
Blood Pressure : Y
Fat Diet : Y
Points : 9
Patient id: 128
Name : Sam
Age : 34
Smoker : Y
Blood Pressure : Y
Fat Diet : N
Points : 12
Patient Menu
=============================================
1. Display the patients
2. Display total number of smokers
3. Search for patient with highest point
4. Move the patient with highest point to the end.
5. Exit the program
=============================================
Enter your selection: 2
The number of smokers are: 3
Patient Menu
=============================================
1. Display the patients
2. Display total number of smokers
3. Search for patient with highest point
4. Move the patient with highest point to the end.
5. Exit the program
=============================================
Enter your selection: 3
Patient id: 23
Name : Oliver
Age : 33
Smoker : N
Blood Pressure : N
Fat Diet : N
Points : 4
Patient Menu
=============================================
1. Display the patients
2. Display total number of smokers
3. Search for patient with highest point
4. Move the patient with highest point to the end.
5. Exit the program
=============================================
Enter your selection: 4
Patient Menu
=============================================
1. Display the patients
2. Display total number of smokers
3. Search for patient with highest point
4. Move the patient with highest point to the end.
5. Exit the program
=============================================
Enter your selection: 5
Press any key to continue . . .
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.