Thread: Array Problem
View Single Post
Old 07-18-2005, 01:58 AM   #10 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,694
redhead is on a distinguished road
A solution with the use of vectors

If you were wondering, this is my solution where I use vectors instead of arrays.
Like I told you, it's probably of no use to you.
Makefile
Code:
CC=g++
MAKE=make
CFLAGS= -Wall -O2
LINKFLAGS=
LIBS=
EXE= arrays
SRC=employee.cpp arrays.cpp
OBJ=$(SRC:.cpp=.o)

all: $(SRC) $(EXE)

arrays: $(OBJ) $(EXE)
        $(CC) $(CFLAGS) -o $(EXE) $(OBJ) $(LINKFLAGS) $(LIBS)

clean:
        rm -f *~ core \#*\# .\#* *.o $(EXE)
employee.h
Code:
#ifndef __EMPLOYEE__H__
#define __EMPLOYEE__H__

#include <vector>
#include <string>

class employee
{
private:
  std::string name;
  std::vector <double> sales;
  std::vector <double> earnings;
  /* not allowed to make an employee without a name */
  employee(void){};
public:
  employee(std::string n){name=n;};
  void add_sale(double sale);
  std::string get_name(void){return name;};
  void show_sales(int info);
  double get_min(int choice);
  double get_max(int choice);
  double get_total(int choice);
  double get_average(int choice);
};


#endif
employee.cpp
Code:
#include "employee.h"
#include <iostream>
#include <iomanip>


void employee::add_sale(double earning)
{
  sales.push_back(earning);
  earnings.push_back((earning*0.09) + 200.0);
}
void employee::show_sales(int info)
{
  double total = 0.0, min = sales[0], max = 0.0, temp = 0.0, count = 0.0;
  std::vector <int> ranges(9,0);


  switch(info)
    {
    case 1:
      std::cout << "  Sales & Earnings for ";
      std::cout << name << std::endl;

      /* This could have been accomplished by calling
       * this->get_max();
       * this->get_min();
       * this->get_total();
       * and this->get_average();
       * But since that would require atleast 3 loops through
       * the vector, it is easier to calculate everything
       * in a single loop
       */
      for(unsigned int i = 0; i < sales.size(); ++i)
        {
          if(sales[i] < min)
            min = sales[i];
          if(max < sales[i])
            max = sales[i];
          if(min > max)
            {
              temp = max;
              max = min;
              min = temp;
            }
          total += sales[i];
          ++count;
        }
      std::cout << "                 .----------.----------.----------.----------." << std::endl;
      std::cout << "                 |   total  |   min.   |   max.   |   avg.   |" << std::endl;
      std::cout << ".----------------|----------|----------|----------|----------|" << std::endl;
      std::cout.precision(2);
      std::cout.flags(std::ios::fixed);
      std::cout << "|      Sales     |";
      std::cout << " $";
      std::cout.width(7);
      std::cout << total << " |";
      std::cout << " $";
      std::cout.width(7);
      std::cout << min << " |";
      std::cout << " $";
      std::cout.width(7);
      std::cout << max << " |";
      std::cout << " $";
      std::cout.width(7);
      std::cout << (total / count) << " |" << std::endl;
      /* This could have been accomplished by calling
       * this->get_max();
       * this->get_min();
       * this->get_total();
       * and this->get_average();
       * But since that would require atleast 3 loops through
       * the vector, it is easier to calculate everything
       * in a single loop
       */
      min=earnings[0];
      max=0.0;
      total=0.0;
      for(unsigned int i = 0; i < earnings.size(); ++i)
        {
          if(earnings[i] < min)
            min = earnings[i];
          if(max < earnings[i])
            max = earnings[i];
          if(min > max)
            {
              temp = max;
              max = min;
              min = temp;
            }
          total += earnings[i];
        }

      std::cout << "|----------------|----------|----------|----------|----------|" << std::endl;
      std::cout.precision(2);
      std::cout.flags(std::ios::fixed);
      std::cout << "|    Earnings    |";
      std::cout << " $";
      std::cout.width(7);
      std::cout << total << " |";
      std::cout << " $";
      std::cout.width(7);
      std::cout << min << " |";
      std::cout << " $";
      std::cout.width(7);
      std::cout << max << " |";
      std::cout << " $";
      std::cout.width(7);
      std::cout << (total / count) << " |" << std::endl;
      std::cout << "'----------------'----------'----------'----------'----------'" << std::endl;
      break;
    default:
      std::cout << "  Ranges of Sales for ";
      std::cout << name << std::endl;
      for(unsigned int i = 0; i < sales.size(); ++i)
for(unsigned int j=0, amount=(1000-(ranges.size()-1)*100); j < ranges.size(); ++j, amount+=100)
          if(sales[i] >= amount-100 && sales[i] < amount)
          {
            ranges[j] = ranges[j]+1;
            break;
          }

      std::cout << ".------------.-------.-----------.-------.-----------.-------." << std::endl;
      std::cout << "|   Range    | Sales |   Range   | Sales |   Range   | Sales |" << std::endl;
      std::cout << "|------------|-------|-----------|-------|-----------|-------|" << std::endl;
      for(unsigned int j=0, amount=(1000-(ranges.size()-1)*100); j < ranges.size(); ++j, amount+=100)
        {
          if(!(j%3))
            std::cout << "|  ";
          else
            std::cout << "| ";
          std::cout << amount-100 << " - " << amount-1 << " | ";
          std::cout.width(5);
          std::cout << ranges[j] << " ";
          if(!((j+1)%3))
            std::cout << "|" << std::endl;
        }
      std::cout << "'------------'-------'-----------'-------'-----------'-------'" << std::endl;
      break;
    }
  std::cout << std::endl;
}


double employee::get_min(int choice)
{
  double min;
  if(choice == 1)
    {
      min = earnings[0];
      for(unsigned int i = 1; i < earnings.size(); ++i)
        {
          if(earnings[i] < min)
            min = earnings[i];
        }
    }
  else
    {
      min = sales[0];
      for(unsigned int i = 1; i < sales.size(); ++i)
        {
          if(sales[i] < min)
            min = sales[i];
        }
    }
  return min;
}

double employee::get_max(int choice)
{
  double max;
  if(choice == 1)
    {
      max = earnings[0];
      for(unsigned int i = 1; i < earnings.size(); ++i)
        {
          if(earnings[i] > max)
            max = earnings[i];
        }
    }
  else
    {
      max = sales[0];
      for(unsigned int i = 1; i < sales.size(); ++i)
        {
          if(sales[i] > max)
            max = sales[i];
        }
    }
  return max;
}

double employee::get_total(int choice)
{
  double total = 0.0;
  if(choice == 1)
    for(unsigned int i = 0; i < earnings.size(); ++i)
      total += earnings[i];
  else
    for(unsigned int i = 0; i < sales.size(); ++i)
      total += sales[i];
  return total;
}

double employee::get_average(int choice)
{
  double total = this->get_total(choice);
  if(choice == 1)
    return (total / (double) earnings.size());
  else
    return (total / (double) sales.size());
}
arrays.cpp
Code:
/*
 * A program to calculate the number of employees
 * who receive a salary in a set number of ranges.
 * Orriginaly assignment 4.10, but for complete usage
 * has been remodeled.
 *
 * Assignment 4.10.1 AKA: Assignment 4.10 with vectors
 */

#include "employee.h"
#include <iostream>


std::string get_employee(void);
double get_sale(std::string name);
void reset_istream(void);
void wait_for_enter(void);
void show_usage(char* name);
int show_menu(void);


int main(void)
{
  double sale;
  int choice;
  std::string name;
  std::vector <employee> employees;
  char ch;

  /* have a conversation with the user, on what they like to see */
  while(true)
    {
      choice = show_menu();
      switch(choice)
        {
        case 1:
          /* Lets create the employees in this input round */
          while(true)
            {
              name = get_employee();
              if(name == "q" || name == "Q")
                break;
              employee temp(name);
              while(true)
                {
                  sale = get_sale(name);
                  if(sale == 0.0)
                    break;
                  temp.add_sale(sale);
                }
              employees.push_back(temp);
            }
          break;
        case 2:
          /* show the earnings along with sales */
          for(unsigned int i=0; i < employees.size(); ++i)
            {
              employees[i].show_sales(1);
              if(i%2)
                {
                  wait_for_enter();
                  reset_istream();
                }
            }
          wait_for_enter();
          reset_istream();
          break;
        case 3:
          /* show the ranges of sales */
          for(unsigned int i=0; i < employees.size(); ++i)
            {
              employees[i].show_sales(2);
              if(i%2)
                {
                  wait_for_enter();
                  reset_istream();
                }
            }
          wait_for_enter();
          reset_istream();
          break;
        }
      /* exit */
      if(choice == 4)
        break;
    }
  return 0;
}

std::string get_employee(void)
{
  std::string name;
  std::cout << "Enter name of employee, use Q to quit: ";
  std::cout.flush();
  getline(std::cin, name);
  return name;
}


double get_sale(std::string name)
{
  double sale;
  std::cout << name << "'s " << "Sale (use 0 to stop): $";
  std::cout.flush();
  std::cin >> sale;
  reset_istream();
  return sale;
}

int show_menu(void)
{
  int answer;
 start_here:
  std::cout << "\t\t.------------------------------." << std::endl
            << "\t\t| Assignment 4.10 with vectors |" << std::endl
            << "\t\t|------------------------------|" << std::endl
            << "\t\t| 1) Add employees             |" << std::endl
            << "\t\t|     and their sales.         |" << std::endl
            << "\t\t| 2) Show employees            |" << std::endl
            << "\t\t|     earnings                 |" << std::endl
            << "\t\t| 3) Show employees            |" << std::endl
            << "\t\t|     ranges of sales          |" << std::endl
            << "\t\t| 4) Exit                      |" << std::endl
            << "\t\t'------------------------------'" << std::endl
            << "\t\t Selection: ";
  std::cout.flush();
  std::cin >> answer;
  if(answer < 1 || answer > 4)
    goto start_here;
  reset_istream();
  return answer;
}

void reset_istream(void)
{
  if(std::cin.eof())
    std::cin.clear();
  else
    {
      std::cin.clear();
      std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }
}

void wait_for_enter(void)
{
  std::cout << "press <enter> to continue...";
  // Reset failstate, just in case.
  std::cin.clear();
  std::string line;
  getline(std::cin, line);
}
For ease of access you can find the source code as a tar-ball.

If you have any questions feel free to ask, I'll try to explain everything uppon request.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote