I am using c++ Problem A: Production line (20 points) Ask the user to read a fil
ID: 3574168 • Letter: I
Question
I am using c++
Problem A: Production line (20 points) Ask the user to read a file. The file will be in the same format as “items.txt” on the website. There will always be a list of items with a name and price followed by some amount of recipes. If a recipe for an item is not present, the only way to make the item is to buy it directly. Make a program that reads all the items and recipes, then says how much profit can be made by making each item. If an item has no recipe, you would buy that item then resell it for the same price and make a profit of 0. If an item does have a recipe, you would buy the materials to make this item and subtract this cost from the price of the final product. There will only be zero or one recipe per item. The items will always be listed first. The names of items will always be a single word (using a _ to join names that are normally multiple words). You may assume there will be less than 50 items and each recipe will use less than 50 other items to create a final product.
Example 1 (user input is underlined):
What file to load? items1.txt
Making Wood, profit=0
Making Metal, profit=0
Making Cat, profit=0
Making Spear, profit=40.2
Example 2 (user input is underlined): What file to load? items2.txt
Making Lint, profit=0
Making Rubber_Chicken, profit=0
Making Wood, profit=0
Making Glasses, profit=0
Making Substitute_Teacher, profit=65.8
Making Cat_Toy, profit=49.95
Making Scare_Crow, profit=100.58
Making Dog_Toy, profit=20
Here are what the files items1.txt and items2.txt look like
items2.txt
Explanation / Answer
/* Problem A: Production line (20 points) Ask the user to read a file. The file will be in the same format as “items.txt” on the website. There will always be a list of items with a name and price followed by some amount of recipes. If a recipe for an item is not present, the only way to make the item is to buy it directly. Make a program that reads all the items and recipes, then says how much profit can be made by making each item. If an item has no recipe, you would buy that item then resell it for the same price and make a profit of 0. If an item does have a recipe, you would buy the materials to make this item and subtract this cost from the price of the final product. There will only be zero or one recipe per item. The items will always be listed first. The names of items will always be a single word (using a _ to join names that are normally multiple words). You may assume there will be less than 50 items and each recipe will use less than 50 other items to create a final product. */
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<fstream.h>
Class items
{
public:
char item_name[30],recipes[100];
float profit;
float price;
void putData()’
void getData();
void calculate();
};
void getData()
{
}
void putData()
{
//clrscr();
int number, recipe_count;
char fname[10],data[20];
fstream file;
cout<<"Enter File name:";
cin>>fname;
file.open(fname+”.txt”,ios::in);
file.seekg(0,ios::beg);
cout<<”How many items you want to enter: ”;
cin>>number;
for(int i=0;i<number;i++)
{
cout<<”Enter items Name: ”;
gets(item_name);
cout<<”Enter profit of item: ”;
gets(profit);
file.write((char *)this,sizeof(item));
}
cout<<”How many recepie”:
cin>>recipe_count;
if(recipe_count>=1)
{
for(int i=0;i< recipe_count;i++)
{
cout<<”Enter Recipes: ”;
gets(recipes);
file.write((char *)this,sizeof(item));
}
}
file.close();
}
void calculate()
{
fstream file;
ofstream file1;
char buy_item[30];
cout<<"Enter File name you want to open:";
cin>>fname;
cout<<"Enter item you want to buy:";
cin>>buy_item;
file.open(fname+”.txt”,ios::in);
while(file.read((char*)this,sizeof(item)))
{
if(recipes==buy_item)
{
cout<”You can buy item ”;
file.close();
}
else
{
item obj;
file.open(fname+”.txt”,ios::in);
file1.open(“temp.txt”,ios::in);
while(!file.eof())
{
file.read((char *) obj,sizeof(item));
if(strcmp(buy_item,obj. recipes))
{
price=price+obj.price;
obj.profit=0;
}
if(file.eof())
{
file2.write((char *) obj,sizeof(item));
}
}
file.close();
file1.close();
remove(fname);
rename(“temp.txt”, fname+”.txt”);
}
}
file.close();
}
void main()
{
char c,fname[10];
items obj_item=new item();
obj_item.putData();
obj.calculate();
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.