예제 #1
0
int main()
{
    Shopper shopper(100);
    int n;
    cout << "Enter number of products: ";
    cin >> n;
    cin.get();

    for(int i = 0; i < n; i++)
    {
        Product p;
        p.read();
        shopper.addProduct(p);
        cout << endl;
    }

    double maxWeight;
    cout << "Enter max weight: ";
    cin >> maxWeight;

    while(shopper.totalWeight() > maxWeight)
    {
        char* nameOfHeaviestProduct = shopper.nameOfHeaviestProduct();
        shopper.removeProduct(nameOfHeaviestProduct);
    }

    shopper.print();
    shopper.buyProducts();

    cout << "Now your cash is " << shopper.getCash() << "\n";

    return 0;
}
예제 #2
0
void process_products(istream& fs)
{  vector<Product*> products;
   bool more = true;
   while (more)
   {  Product* p = new Product;
      try
      {  if (p->read(fs)) products.push_back(p);
         else more = false;
      }   
      catch(runtime_error& r)  // runtime_error& r)
      {  cout << "1. " << r.what() << endl;
         for (int i = 0; i < products.size(); i++)
                                delete products[i];
         throw;                       
         return;                                                                     
      }                     
    }     
   /* processing products */       
   
   cout << "Print:" << endl;
   for (int i = 0; i < products.size(); i++)
   {  products[i]->print();
      delete products[i];
   }      
}
예제 #3
0
int main()
{  
   Product best;
   Product secondBest;

   bool more = true;
   while (more)
   {  
      Product next;
      next.read();
      if (next.is_better_than(best)) {
	secondBest = best;
	best = next;
      }
      else {
	if(next.is_better_than(secondBest)) {
	  secondBest = next;
	}
      }


      cout << "More data? (y/n) ";
      string answer;
      getline(cin, answer);
      if (answer != "y") more = false;
   }

   cout << "The best value is ";
   best.print();
   cout << "The second best value is ";
   secondBest.print();

   return 0;
}
예제 #4
0
파일: bestval2.cpp 프로젝트: maxoyn/lab
/* ИЗПОЛЗВАНЕ НА КЛАСА */
int main()
{  Product best;       /* дефиниране на обект от класа */
   bool more = true;
   while (more)
   {  Product next;
      next.read();    /* извикване на член-функция на класа */
      if (next.is_better_than(best)) best = next;
      cout << "More data? (y/n) ";
      string answer;
      getline(cin, answer);
      if (answer != "y") more = false;
   }
   cout << "The best bang for the buck is ";
   best.print();        /* извикване на член-функция на класа */
   return 0;
}
예제 #5
0
int main(int argc, char **argv) {
    Product best;
    
    bool more = true;
    while (more)
    {
        Product next;
        next.read();
        if (next.is_better_than(best)) best = next;
        
        cout << "More data? (y/n)";
        string answer;
        getline(cin, answer);
        if (answer != "y") more = false;
    }
    
    cout << "The best bang for the buck is ";
    best.print();
    
    return 0;
}
예제 #6
0
void addProduct(vector<Product>& store) {
    cout << "\nAdding a new product:\n";
    Product prod;
    prod.read();
    store.push_back(prod);
}