All pastes #2104556 Raw Edit

Mine

public text v1 · immutable
#2104556 ·published 2012-01-20 16:43 UTC
rendered paste body
/* 
Name:           Toh Weiqing
Matric Number:  U098494N

Description: Generates all the possible combinations using a truth 
table with some binary notation and selects the highest satisfaction
that is within budget. 

*/
#include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <iomanip>
using namespace std;

struct item {   
       string item, type;
       double price;
       int satis;
       bool buy;
};

void maximise (vector<item>& shoplist, double totalbudget){
     
     float a = 2;
     int length = shoplist.size(), n= pow(a,length);
     int count, maxsat = 0, satisfaction, combi;
     double budget;
     
     //Iterate through each combination to find 
     //the combination with the maximum satisfaction.
     for (int i=1;i<=n-1;i++){
         count = i;
         budget = 0;
         satisfaction = 0;
         
         for (int j=length;j>=0;j--){
             if (count>=pow(a,j)){
                count = count - pow(a,j);
                budget += shoplist[j].price;
                satisfaction += shoplist[j].satis;
             }
         }
         //Records the combination with the highest satisfaction
         //and is within budget
         if ((budget<=totalbudget) && (satisfaction > maxsat)){
            combi = i;
            maxsat = satisfaction;
         }
     }
     
     //Set the optimal combination
     for (int j=length;j>=0;j--){
         if (combi>=pow(a,j)){
            shoplist[j].buy = true;
            combi = combi - pow(a,j);
         }
     }
     
     //Prints the necessary output
     cout<<"Buy:"<<endl;
     
     budget = 0;
     satisfaction = 0;
     
     for (int i=0;i<length;i++){
         if (shoplist[i].buy){
             cout<<shoplist[i].item<<" for "<<shoplist[i].price<<endl;
             budget += shoplist[i].price;
             satisfaction += shoplist[i].satis;
         }
     }
     
     cout<<"Satisfaction:"<<endl<<fixed<<setprecision(2)<<satisfaction<<endl;
     cout<<"Spent:"<<endl<<setprecision(2)<<budget<<endl;
     
     return;
}

int main(){
    
    vector<item> shoplist;
    char dummy;
    int n;
    double B;
    item a;
    a.buy = false;
    cin>>n;
    
    for (int i=0;i<n;i++){
        cin>>a.item>>a.price>>a.satis>>dummy>>a.type;
        shoplist.push_back(a);
        cout<<"woohoo"<<endl;
    }
    
    cout<<"Please enter budget: "<<endl; 
    cin>>B;
    cout<<endl;
    
    maximise(shoplist, B); 
    
    system("pause");
    return 0;
}