/*********** 1. A plain text file "in.txt" contains lines indicating unit price of fruit, fruit name, and availability status code: 1.99 mangoes Y 0.99 banans Y 3.49 pineapples N .............. Write a C++ program that reads in this text file, creates two linked lists of "Fruit" objects, one whose prices are less than or equal to a specified amount, p, and those with prices above p. Include a display function so that the user can display each list. ************/ /**********in.txt*************** 1.49 mac_apples Y 1.59 bananas Y 2.99 figs N 0.99 navel_oranges Y 2.49 grapefruit_pink N 2.59 grapefruit_yellow Y 3.99 pomagranates Y 1.99 mangoes Y 3.99 pineapples Y 1.89 honeycrisp_apples Y 2.39 mccoun_apples N 1.49 red_grapes Y ******************************/ #include #include using namespace std; class Fruit{ public: float price; string name; char avail; Fruit* next; Fruit(){next=NULL;} //default constructor sets the pointer "next" to NULL. void display(){ Fruit* p=this; int i=0; while(p!= NULL){ cout<name<<"-->\t"<price<next; i++; } } void add(Fruit* frp){ // Add Fruit pointed to by frp at the tail of the list Fruit* temp=this; // A pointer to move around the list; starts with 'this' object if(temp == NULL){ // Fruit List is empty; so add data from frp to this Fruit this->name=frp->name; this->price=frp->price; this->avail=frp->avail; } else { // First we need to locate the tail of the list while (temp->next != NULL){ temp=temp->next; // go to the end of the list } // temp->next is now pointing to NULL temp->next=frp; //make temp->next point to frp, the pointer to Fruit to be added. } } }; int main(){ ifstream infile; int i=0; float price; string name; char avail; float p=2.0; //limiting value Fruit fr[100]; infile.open("in.txt"); //open input file while(!infile.eof()){ infile>>price>>name>>avail; fr[i].name=name; fr[i].price=price; fr[i].avail=avail; i++; } //at this point, i is TWO more than the # of fruits in the input file /******Check read-in list *********/ cout<<"\n\n\n****************Fruits List**********"<add(&fr[j]); // We need & infront of fr[j] because the argument to add() function is a pointer } else{ List2->add(&fr[j]); } } cout<<"\n\n*******************Fruits with prices below $2.00*********************"<display(); cout<<"\n\n******************Fruits with prices more than $2.00******************"<display(); cout<