I am using c++ Here is the problem Reuse your code from part A. Find the item wi
ID: 3575170 • Letter: I
Question
I am using c++
Here is the problem
Reuse your code from part A. Find the item with the most profit and make that item. Repeat this process 20 times and show your total profit over the period. However, after making an item, the cost of the materials you buy increases by 10%. If multiple materials are used in a single recipe, each the cost is increased multiple times. For example, making a “Spear” in “items1.txt” takes 2 pieces of “Wood”. Each “Wood” initially costs 2.5. Buying the first piece of wood increases this from 2.5 to 2.75 and the second increases it from 2.75 to 3.025. This price increase is applied after you buy the wood. So your the first “Spear” made has a wood cost of 2 x 2.5 = 5, while the second “Spear” has a wood cost of 2 x 3.025 = 6.05. If no items make profit, simply make the first item on the list. Example 1 (user input is underlined):
What file to load? items1.txt
Time 1: Making Spear, total profit = 40.2
Time 2: Making Spear, total profit = 78.8
Time 3: Making Spear, total profit = 115.525
Time 4: Making Spear, total profit = 150.046
Time 5: Making Spear, total profit = 181.976
Time 6: Making Spear, total profit = 210.849
Time 7: Making Spear, total profit = 236.113
Time 8: Making Spear, total profit = 257.108
Time 9: Making Spear, total profit = 273.043
Time 10: Making Spear, total profit = 282.975
Time 11: Making Spear, total profit = 285.772
Time 12: Making Wood, total profit = 285.772
Time 13: Making Wood, total profit = 285.772
Time 14: Making Wood, total profit = 285.772
Time 15: Making Wood, total profit = 285.772
Time 16: Making Wood, total profit = 285.772
Time 17: Making Wood, total profit = 285.772
Time 18: Making Wood, total profit = 285.772
Time 19: Making Wood, total profit = 285.772
Time 20: Making Wood, total profit = 285.772
Example 2 (user input is underlined):
What file to load? items2.txt
Time 1: Making Scare_Crow, total profit = 100.58
Time 2: Making Scare_Crow, total profit = 199.216
Time 3: Making Scare_Crow, total profit = 295.713
Time 4: Making Scare_Crow, total profit = 389.856
Time 5: Making Scare_Crow, total profit = 481.409
Time 6: Making Scare_Crow, total profit = 570.114
Time 7: Making Scare_Crow, total profit = 655.682
Time 8: Making Scare_Crow, total profit = 737.801
Time 9: Making Scare_Crow, total profit = 816.124
Time 10: Making Scare_Crow, total profit = 890.268
Time 11: Making Scare_Crow, total profit = 959.815
Time 12: Making Scare_Crow, total profit = 1024.3
Time 13: Making Scare_Crow, total profit = 1083.22
Time 14: Making Substitute_Teacher, total profit = 1138.72
Time 15: Making Substitute_Teacher, total profit = 1190.77
Time 16: Making Scare_Crow, total profit = 1240.51
Time 17: Making Cat_Toy, total profit = 1289.79
Time 18: Making Cat_Toy, total profit = 1338.63
Time 19: Making Cat_Toy, total profit = 1386.76
Time 20: Making Cat_Toy, total profit = 1433.75
Here is my code so far
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <algorithm>
#include <sstream>
using namespace std;
string findnextstring(string str, int beginindex)
{
int z=0;
z=str.find(' ',beginindex);
z=z-beginindex;
str=str.substr(beginindex,z);
return str;
}
int dividestring(string str2, string a[], int maxsize)
{
int y;
int num = 0;
for (y=0; y<maxsize; y++)
{
a[y] = findnextstring(str2,num);
num = num + a[y].length() + 1;
if(num >= str2.length())
{
y++;
}
}
return y;
}
int main()
{
ifstream thefile;
string filename;
cout << "What file to load? ";
cin >> filename;
thefile.open(filename.c_str());
if (thefile.fail())
{
cout << "ERROR";
exit(1);
}
string itemsname[50];
double itemsvalue[50];
double profit[50];
string myrecap[50];
string rname = myrecap[0];
int itemcount = 0;
string myfilelines;
while(getline(thefile, myfilelines))
{
if(myfilelines.substr(0,5) == "Item:")
{
int begin = myfilelines.find_first_of(' ') + 1;
int nextfreespace = myfilelines.find(" ", begin);
itemsvalue[itemcount] = atof(myfilelines.substr(nextfreespace).c_str());
itemsname[itemcount] = myfilelines.substr (begin, myfilelines.find_first_of(' ', begin) - begin);
profit[itemcount]=0;
itemcount++;
}
else if(myfilelines.substr(0,7) == "Recipe:")
{
int maxsize = myfilelines.length();
int count = dividestring(myfilelines,myrecap,maxsize);
double profit1 = 0;
for(int k=2; k< count; k++)
{
for(int i= 0; i<itemcount; i++)
{
if((myrecap[k] == itemsname[i]) && (myrecap[k] != "+") && (myrecap[k] != ";"))
{
profit1 += itemsvalue[i];
}
}
}
for(int i=0; i<itemcount; i++)
{
if((myrecap[1] == itemsname[i]))
{
profit[i]=itemsvalue[i]-profit1;
}
}
}
}
for(int i=0; i<itemcount; i++)
{
cout << "Making " << itemsname[i] << ", " << "profit = " << profit[i]<<endl;
}
thefile.close();
return 0;
}
here are the files items1.txt and items2.txt
items1.txt
items2.txt
Explanation / Answer
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <algorithm>
#include <sstream>
using namespace std;
string findnextstring(string str, int beginindex){
int z=0;
string str1;
z=str.find(' ',beginindex);
z=z-beginindex;
return str.substr(beginindex,z);
}
int dividestring(string str2, string a[], int maxsize){
int y;
int num = 0;
for (y=0; y<maxsize; y++){
a[y] = findnextstring(str2,num);
num = num + a[y].length() + 1;
if(num >= str2.length()){
y++;
}
}
return y;
}
int main(){
try
{
ifstream thefile;
string filename;
cout << "What file to load? ";
cin >> filename;
thefile.open(filename.c_str());
if (thefile.fail()){
cout << "ERROR";
}
string itemsname[50];
double itemsvalue[50];
double profit[50];
string myrecap[50];
string rname = myrecap[0];
int itemcount = 0;
string myfilelines;
while(getline(thefile, myfilelines)){
if(myfilelines.substr(0,5) == "Item:"){
int begin = myfilelines.find_first_of(' ') + 1;
int nextfreespace = myfilelines.find(" ", begin);
itemsvalue[itemcount] = atof(myfilelines.substr(nextfreespace).c_str());
itemsname[itemcount] = myfilelines.substr (begin, myfilelines.find_first_of(' ', begin) - begin);
profit[itemcount]=0;
itemcount++;
} else if(myfilelines.substr(0,7) == "Recipe:"){
int maxsize = myfilelines.length();
int count = dividestring(myfilelines,myrecap,maxsize);
double profit1 = 0;
for(int k=2; k< count; k++){
for(int i= 0; i<itemcount; i++){
if((myrecap[k] == itemsname[i]) && (myrecap[k] != "+") && (myrecap[k] != ";")){
profit1 += itemsvalue[i];
}
}
}
for(int i=0; i<itemcount; i++){
if((myrecap[1] == itemsname[i])){
profit[i]=itemsvalue[i]-profit1;
}
}
}
}
for(int i=0; i<itemcount; i++){
cout << "Making " << itemsname[i] << ", " << "profit = " << profit[i]<<endl;
}
thefile.close();
return 0;
}
catch (std::exception const &exc)
{
std::cerr << "Exception caught " << exc.what() << " ";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.