Implement bag
hw5.cpp
//#ifndef MAIN_SAVITCH_BAG2_H
//#define MAIN_SAVITCH_BAG2_H
#include <iostream>
#include <cstdlib> // Provides size_t
#include <cassert>
#include <cstring>
#include <algorithm> // Provides copy function
//#include “SortedBag.h”
//#include “Student.h”
using namespace std;
class Student
{
public:
int a;
};
template<class T>
struct Node {
Node() : m_next(this), m_prev(this) {}
Node(const T& val, Node<T>* n =0, Node<T>* p =0): m_val(val)
{ m_next = n? n : this; m_prev = p? p : this; }
bool is_singleton() const { m_next == this && m_prev == this; }
T m_val;
Node<T>* m_next; // pointer to successor element.
Node<T>* m_prev; // pointer to predecessor element.
};
template<class T>
class SortedBag {
public:
SortedBag() : m_data(0), m_size(0), m_asc(true), m_curr(0) {}
SortedBag(const SortedBag&);
void operator =(const SortedBag&);
~SortedBag();
bool erase_one(const T&);
long erase(const T&);
void insert(const T&);
void operator +=(const SortedBag&);
long size() const { return m_size; }
long count(const T&) const;
// SIMPLE ITERATOR
void begin(const bool ascending =true);
bool end() const;
void operator++();
void operator–();
T&get();
private:
Node<T>* m_data; // pointer to ring structure.
long m_size; // number of elements in the Bag.
bool m_asc; // flag to indicate iteration in ascending order or not.
Node<T>* m_curr; // iterator’s current position.
};
template<class T>
SortedBag<T> operator+(const SortedBag<T>&, const SortedBag<T>&);
// template<class T>
//const SortedBag<T>::size_type SortedBag::DEFAULT_CAPACITY;
template<class T>
SortedBag<T>::SortedBag()
{
Node<T>* temp;
temp = new(struct Node<T>);
temp->m_val = NULL;
temp->m_next = NULL;
if (start == NULL)
{
temp->m_prev = NULL;
m_data = temp;
}
m_size = 0;
m_asc = true;
m_curr = NULL;
}
template<class T>
SortedBag<T>::SortedBag(const SortedBag& source)
{
m_data = source.m_data;
m_size = source.m_size;
m_curr = source.m_curr;
m_asc = source.m_asc;
}
template<class T>
long SortedBag<T>::erase(const T& target)
{
struct Node<T> *tmp, *q;
if (m_data->m_val == target)
{
tmp = m_data;
m_data = m_data->m_next;
m_data->m_prev = NULL;
//cout<<“Element Deleted”<<endl;
free(tmp);
m_size–;
return m_size;
}
q = m_data;
while (q->m_next->m_next != NULL)
{
if (q->m_next->m_val == target)
{
tmp = q->m_next;
q->m_next = tmp->m_next;
tmp->m_next->m_prev = q;
//cout<<“Element Deleted”<<endl;
free(tmp);
m_size–;
return m_size;
}
q = q->m_next;
}
if (q->m_next->m_val == target)
{
tmp = q->m_next;
free(tmp);
q->m_next = NULL;
//cout<<“Element Deleted”<<endl;
m_size–;
return m_size;
}
cout<<“Element “<<target<<” not found”<<endl;
}
template<class T>
bool SortedBag<T>::erase_one(const T& index)
{
struct Node<T> *tmp, *q;
m_curr = m_data;
if (index > m_size)
return false;
while (index– > 0){
m_curr = m_curr->m_next;
}
long m = erase(m_curr->m_val);
if(m){
return true;
}
return false;
}
template<class T>
void SortedBag<T>::insert(const T& entry)
{
struct Node<T> *temp;
temp = new(struct Node<T>);
temp->m_val = entry;
m_curr = m_data;
while(m_curr!=NULL && m_curr->m_val < entry){
m_curr = m_curr->m_next;
}
temp->m_next = m_curr;
temp->m_prev = m_curr->m_prev;
m_curr->m_prev = temp;
temp->m_prev->m_next = temp;
cout<<“Element Inserted”<<endl;
}
int main()
{
SortedBag<Student> L;
cout<<“TEST”<<endl;
return 0;
}
bag4.cpp
// FILE: bag4.template
// TEMPLATE CLASS IMPLEMENTED: bag<Item> (see bag4.h for documentation)
// NOTE:
// Since node is a template class, this file is included in node2.h.
// Therefore, we should not put any using directives in this file.
// INVARIANT for the bag ADT:
// 1. The number of items in the bag is in the member variable used;
// 2. The actual items of the bag are stored in a partially filled array.
// The array is a dynamic array, pointed to by the member variable data.
// 3. The size of the dynamic array is in the member variable capacity.
#include <algorithm> // Provides copy
#include <cassert> // Provides assert
#include <cstdlib> // Provides rand
namespace main_savitch_6A
{
// MEMBER CONSTANTS *********************************************:
template<class Item>
const typename bag<Item>::size_type bag<Item>::DEFAULT_CAPACITY;
// CONSTRUCTORS and DESTRUCTORS *********************************:
template<class Item>
bag<Item>::bag(size_type initial_capacity)
{
data = new Item[initial_capacity];
capacity = initial_capacity;
used = 0;
}
template<class Item>
bag<Item>::bag(const bag<Item>& source)
// Library facilities used: algorithm
{
data = new Item[source.capacity];
capacity = source.capacity;
used = source.used;
std::copy(source.data, source.data + used, data);
}
template<class Item>
bag<Item>::~bag( )
{
delete [ ] data;
}
// MODIFICATION MEMBER FUNCTIONS (alphabetically): ***************:
template<class Item>
typename bag<Item>::size_type bag<Item>::erase(const Item& target)
{
size_type index = 0;
size_type many_removed = 0;
while (index < used)
{
if (data[index] == target)
{
–used;
data[index] = data[used];
++many_removed;
}
else
–index;
}
return many_removed;
}
template<class Item>
bool bag<Item>::erase_one(const Item& target)
{
size_type index; // The location of target in the data array
// First, set index to the location of target in the data array,
// which could be as small as 0 or as large as used-1.
// If target is not in the array, then index will be set equal to used.
for (index = 0; (index < used) && (data[index] != target); ++index)
; // No work in the body of this loop.
if (index == used) // target isn’t in the bag, so no work to do
return false;
// When execution reaches here, target is in the bag at data[index].
// So, reduce used by 1 and copy the last item onto data[index].
–used;
data[index] = data[used];
return true;
}
template<class Item>
void bag<Item>::insert(const Item& entry)
{
if (used == capacity)
reserve(used+1);
data[used] = entry;
++used;
}
template<class Item>
void bag<Item>::operator =(const bag<Item>& source)
// Library facilities used: algorithm
{
Item *new_data;
// Check for possible self-assignment:
if (this == &source)
return;
// If needed, allocate an array with a different size:
if (capacity != source.capacity)
{
new_data = new Item[source.capacity];
delete [ ] data;
data = new_data;
capacity = source.capacity;
}
// Copy the data from the source array:
used = source.used;
std::copy(source.data, source.data + used, data);
}
template<class Item>
void bag<Item>::operator +=(const bag<Item>& addend)
// Library facilities used: algorithm
{
if (used + addend.used > capacity)
reserve(used + addend.used);
std::copy(addend.data, addend.data + addend.used, data + used);
used += addend.used;
}
template<class Item>
void bag<Item>::reserve(size_type new_capacity)
// Library facilities used: algorithm
{
Item *larger_array;
if (new_capacity == capacity)
return; // The allocated memory is already the right size
if (new_capacity < used)
new_capacity = used; // Can’t allocate less than we are using
larger_array = new Item[new_capacity];
std::copy(data, data + used, larger_array);
delete [ ] data;
data = larger_array;
capacity = new_capacity;
}
// CONST MEMBER FUNCTIONS (alphabetically): *********************:
template<class Item>
typename bag<Item>::size_type bag<Item>::count
(const Item& target) const
{
size_type answer;
size_type i;
answer = 0;
for (i = 0; i < used; ++i)
if (target == data[i])
++answer;
return answer;
}
template<class Item>
Item bag<Item>::grab( ) const
// Library facilities used: cassert, cstdlib
{
size_type i;
assert(size( ) > 0);
i = (std::rand( ) % size( )); // i is in the range of 0 to size( ) – 1.
return data[i];
}
// NON-MEMBER FUNCTIONS: ****************************************:
template<class Item>
bag<Item> operator +(const bag<Item>& b1, const bag<Item>& b2)
{
bag<Item> answer(b1.size( ) + b2.size( ));
answer += b1;
answer += b2;
return answer;
}
}
bag5.cpp
// FILE: bag5.template
// CLASS implemented: bag (see bag5.h for documentation)
// NOTE:
// Since bag is a template class, this file is included in node2.h.
// INVARIANT for the bag class:
// 1. The items in the bag are stored on a linked list;
// 2. The head pointer of the list is stored in the member variable head_ptr;
// 3. The total number of items in the list is stored in the member variable
// many_nodes.
#include <cassert> // Provides assert
#include <cstdlib> // Provides NULL, rand
#include “node2.h” // Provides node
namespace main_savitch_6B
{
template<class Item>
bag<Item>::bag( )
// Library facilities used: cstdlib
{
head_ptr = NULL;
many_nodes = 0;
}
template<class Item>
bag<Item>::bag(const bag<Item>& source)
// Library facilities used: node2.h
{
node<Item> *tail_ptr; // Needed for argument of list_copy
list_copy(source.head_ptr, head_ptr, tail_ptr);
many_nodes = source.many_nodes;
}
template<class Item>
bag<Item>::~bag( )
// Library facilities used: node2.h
{
list_clear(head_ptr);
many_nodes = 0;
}
template<class Item>
typename bag<Item>::size_type bag<Item>::count(const Item& target) const
// Library facilities used: cstdlib, node2.h
{
size_type answer;
const node<Item> *cursor;
answer = 0;
cursor = list_search(head_ptr, target);
while (cursor != NULL)
{
// Each time that cursor is not NULL, we have another occurrence of
// target, so we add one to answer, and move cursor to the next
// occurrence of the target.
++answer;
cursor = cursor->link( );
cursor = list_search(cursor, target);
}
return answer;
}
template<class Item>
typename bag<Item>::size_type bag<Item>::erase(const Item& target)
// Library facilities used: cstdlib, node2.h
{
size_type answer = 0;
node<Item> *target_ptr;
target_ptr = list_search(head_ptr, target);
while (target_ptr != NULL)
{
// Each time that target_ptr is not NULL, we have another occurrence
// of target. We remove this target using the same technique that
// was used in erase_one.
++answer;
–many_nodes;
target_ptr->set_data( head_ptr->data( ) );
target_ptr = target_ptr->link( );
target_ptr = list_search(target_ptr, target);
list_head_remove(head_ptr);
}
return answer;
}
template<class Item>
bool bag<Item>::erase_one(const Item& target)
// Library facilities used: cstdlib, node2.h
{
node<Item> *target_ptr;
target_ptr = list_search(head_ptr, target);
if (target_ptr == NULL)
return false; // target isn’t in the bag, so no work to do
target_ptr->set_data( head_ptr->data( ) );
list_head_remove(head_ptr);
–many_nodes;
return true;
}
template<class Item>
Item bag<Item>::grab( ) const
// Library facilities used: cassert, cstdlib, node2.h
{
size_type i;
const node<Item> *cursor;
assert(size( ) > 0);
i = (std::rand( ) % size( )) + 1;
cursor = list_locate(head_ptr, i);
return cursor->data( );
}
template<class Item>
void bag<Item>::insert(const Item& entry)
// Library facilities used: node2.h
{
list_head_insert(head_ptr, entry);
++many_nodes;
}
template<class Item>
void bag<Item>::operator +=(const bag<Item>& addend)
// Library facilities used: node2.h
{
node<Item> *copy_head_ptr;
node<Item> *copy_tail_ptr;
if (addend.many_nodes > 0)
{
list_copy(addend.head_ptr, copy_head_ptr, copy_tail_ptr);
copy_tail_ptr->set_link( head_ptr );
head_ptr = copy_head_ptr;
many_nodes += addend.many_nodes;
}
}
template<class Item>
void bag<Item>::operator =(const bag<Item>& source)
// Library facilities used: node2.h
{
node<Item> *tail_ptr; // Needed for argument to list_copy
if (this == &source)
return;
list_clear(head_ptr);
many_nodes = 0;
list_copy(source.head_ptr, head_ptr, tail_ptr);
many_nodes = source.many_nodes;
}
template<class Item>
bag<Item> operator +(const bag<Item>& b1, const bag<Item>& b2)
{
bag<Item> answer;
answer += b1;
answer += b2;
return answer;
}
}
Parmankulov_Dzhonibek_HW4.cpp
//Dzhonibek Parmankulov Project 4 using Bag template
//Cpp.sh
#include <iostream>
#include<iomanip>
#include <math.h>
#include<cstring>
#include<vector>
#include <algorithm>
#include<ctime>
#include <cassert>
#include<cstdlib>
using namespace std;
template<class Item>
class Bag {
public:
// TYPEDEFS and MEMBER CONSTANTS
typedef Item value_type;
typedef std::size_t size_type;
static const size_type DEFAULT_CAPACITY = 30;
// CONSTRUCTORS and DESTRUCTOR
Bag(size_type initial_capacity = DEFAULT_CAPACITY);
Bag(const Bag& source);
void operator =(const Bag& source);
~Bag() { delete [] data; }
// MODIFICATION MEMBER FUNCTIONS
void reserve(size_type new_capacity);
bool erase_one(const value_type& target);
size_type erase(const value_type& target);
void insert(const value_type& entry);
void operator +=(const Bag& addend);
// CONSTANT MEMBER FUNCTIONS
size_type size( ) const { return used; }
size_type count(const value_type& target) const;
// SIMPLE ITERATOR
void begin() { current = 0; }
bool end() const { return current >= used; }
int operator++() { assert(!end()); current++; }
int& get() { assert(!end()); return data[current]; }
private:
value_type *data; // Pointer to partially filled dynamic array
size_type used; // How much of array is being used
size_type capacity; // Current capacity of the Bag
size_type current; // Iterator’s current position.
};
// NONMEMBER FUNCTIONS for the Bag class
template<class Item>
Bag<Item> operator +(const Bag<Item>& b1, const Bag<Item>& b2);
#include <algorithm> // Provides copy function
template<class Item>
const typename Bag<Item>::size_type Bag<Item>::DEFAULT_CAPACITY;
template<class Item>
Bag<Item>::Bag(size_type initial_capacity)
{
data = new value_type[initial_capacity];
capacity = initial_capacity;
used = 0;
}
template<class Item>
Bag<Item>::Bag(const Bag<Item>& source)
// Library facilities used: algorithm
{
data = new value_type[source.capacity];
capacity = source.capacity;
used = source.used;
copy(source.data, source.data + used, data);
}
template<class Item>
void Bag<Item>::reserve(size_type new_capacity)
// Library facilities used: algorithm
{
value_type *larger_array;
if (new_capacity == capacity)
return; // The allocated memory is already the right size.
if (new_capacity < used)
new_capacity = used; // Can’t allocate less than we are using.
larger_array = new value_type[new_capacity];
copy(data, data + used, larger_array);
delete [ ] data;
data = larger_array;
capacity = new_capacity;
}
template<class Item>
typename Bag<Item>::size_type Bag<Item>::erase(const value_type& target)
{
size_type index = 0;
size_type many_removed = 0;
while (index < used) {
if (data[index] == target) {
–used;
data[index] = data[used];
++many_removed;
}
else ++index;
}
return many_removed;
}
template<class Item>
bool Bag<Item>::erase_one(const value_type& target)
{
size_type index; // The location of target in the data array
// First, set index to the location of target in the data array,
// which could be as small as 0 or as large as used-1.
// If target is not in the array, then index will be set equal to used.
index = 0;
while ((index < used) && (data[index] != target))
++index;
if (index == used) // target isn’t in the Bag, so no work to do
return false;
// When execution reaches here, target is in the Bag at data[index].
// So, reduce used by 1 and copy the last item onto data[index].
–used;
data[index] = data[used];
return true;
}
template<class Item>
void Bag<Item>::insert(const value_type& entry)
{
if (used == capacity) reserve(used+1);
data[used] = entry;
++used;
}
template<class Item>
void Bag<Item>::operator +=(const Bag<Item>& addend)
// Library facilities used: algorithm
{
if (used + addend.used > capacity)
reserve(used + addend.used);
copy(addend.data, addend.data + addend.used, data + used);
used += addend.used;
}
template<class Item>
void Bag<Item>::operator =(const Bag<Item>& source)
// Library facilities used: algorithm
{
value_type *new_data;
// Check for possible self-assignment:
if (this == &source) return;
// If needed, allocate an array with a different size:
if (capacity != source.capacity) {
new_data = new value_type[source.capacity];
delete [ ] data;
data = new_data;
capacity = source.capacity;
}
// Copy the data from the source array:
used = source.used;
copy(source.data, source.data + used, data);
}
template<class Item>
typename Bag<Item>::size_type Bag<Item>::count(const value_type& target) const
{
size_type answer;
size_type i;
answer = 0;
for (i = 0; i < used; ++i)
if (target == data[i]) ++answer;
return answer;
}
template<class Item>
Bag<Item> operator +(const Bag<Item>& b1, const Bag<Item>& b2)
{
Bag<Item>answer(b1.size( ) + b2.size( ));
answer += b1;
answer += b2;
return answer;
}
class Course
{
public:
vector< vector<int>>courses;
};
class Student
{
public:
vector<int> ssid;
string fname;
string lname;
vector<string> firstname,lastname;
vector<int> records,from;
vector< vector<int>> enrolled;
vector< vector<int>> courses;
vector<int> sumofgrades;
vector<double> gpa;
Bag<Course> m_courses;
//int courses[22][];*
};
//courses.resize(22, vector<int>);
/*template <int T>
Bag(T)
{
vector< vector<T>> a;
a[0][0].push_back(T);
}*/
/* boxmuller.c Implements the Polar form of the Box-Muller
Transformation
(c) Copyright 1994, Everett F. Carter Jr.
Permission is granted by the author to use
this software for any application provided this
copyright notice is preserved.
*/
extern float ranf()
{ float num2,num;
num2=rand()%10000;
num=num2/10000;
return num;
} /* ranf() is uniform in 0..1 */
float box_muller(float m, float s) /* normal random variate generator */
{ /* mean m, standard deviation s */
float x1, x2, w, y1;
static float y2;
static int use_last = 0;
if (use_last)
/* use value from previous call */
{
y1 = y2;
use_last = 0;
}
else
{
do {
x1 = 2.0 * ranf() – 1.0;
x2 = 2.0 * ranf() – 1.0;
w = x1 * x1 + x2 * x2;
} while ( w>= 1.0 );
w = sqrt( (-2.0 * log( w ) ) / w );
y1 = x1 * w;
y2 = x2 * w;
use_last = 1;
}
return( m + y1 * s );
}
void out_results(Student a)
{
int i,t,s,u,j,sz=0;
u=10;
for(i=0;i<u; i++)
{
cout<<a.lastname[i]<<“,”;
sz=a.lastname[i].size();
for(j=sz; j<=15; j++)
{cout<<” “; }
cout<<“\t”<<a.firstname[i];
sz=a.lastname[i].size()+a.firstname[i].size();
for(j=sz; j<=30; j++)
{cout<<” “; }
cout<<“\t”<<a.enrolled[i].size();
cout<<” courses\tGPA:”<<a.gpa[i]<<endl;
/*cout<<“This student was moved from “<<a.records[i]+1<<endl<<endl;*/
}
}
/////////////////////////////////////////////////////
void sort_descending(Student a, Student *b)
{
int i,j,len,max_id;
double max_grade;
len=a.gpa.size();
for(i=0;i<len;i++)
{
max_grade=0;
max_id=i;
for(j=0; j<len; j++)
{
if(a.gpa[j]>max_grade)
{
max_grade=a.gpa[j];
max_id=j;
}
}
b->gpa.push_back(a.gpa[max_id]);
b->enrolled.push_back(a.enrolled[max_id]);
b->sumofgrades.push_back(a.sumofgrades[max_id]);
b->firstname.push_back(a.firstname[max_id]);
b->lastname.push_back(a.lastname[max_id]);
b->records.push_back(max_id);
b->from.push_back(i);
a.gpa[max_id]=-1;
}
}
void gradethecourse(vector<int>& grade,int & sumofgrades)
{
int a,b,t,k,midterm1,rndmid1,midterm2,rndmid2,final,rndfinal,sum,sumrnd,fgrade;
a=60;
b=12;
t=box_muller(a,b);
midterm1=t;
rndmid1=t;
//cout<<“Midterm 1:”<<midterm1<<endl;
a=55;
b=13;
t=box_muller(a,b);
midterm2=t;
rndmid2=t;
//cout<<“Midterm 2:”<<midterm2<<endl;
a=46;
b=14;
t=box_muller(a,b);
final=t;
rndfinal=t;
//cout<<“Final:”<<final<<endl;
sum=midterm1+midterm2+final;
//cout<<“Sum of the grades:”<<sum<<endl;
//cout<<endl;
if(midterm2>midterm1+29)
{
//cout<<“Value:”<<students.rndmid1[i]<<endl;
k=(rndmid1*85/100);
if((midterm1%10)!=0)
{k=k+1;}
rndmid1=k;
//cout<<“Midterm 1 after rounding:”<<rndmid1<<endl;
}
if(final>midterm2+29)
{
k=rndmid2*120/100;
if((midterm2%10)!=0)
{k=k+1;}
rndmid2=k;
//cout<<“Midterm 2 after rounding:”<<rndmid2<<endl;
}
if((midterm1-midterm2)>24)
{
//cout<<“value:”<<students.rndmid1[i]<<endl;
k=rndmid1*85/100;
if((midterm1%10)!=0)
{k=k+1;}
rndmid1=k;
//cout<<“Midterm 1 after rounding:”<<rndmid1<<endl;
}
if((midterm2-final)>24)
{
//cout<<“value:”<<students.rndmid2[i]<<endl;
k=rndmid2*85/100;
if((midterm2%10)!=0)
{k=k+1;}
rndmid2=k;
//cout<<“Midterm 2 after rounding:”<<rndmid2<<endl;
}
sumrnd=rndmid1+rndmid2+rndfinal;
//cout<<“Rounded sum of grades:”<<sumrnd<<endl;
//cout<<endl;
fgrade=sumrnd/3;
grade.push_back(fgrade);
sumofgrades+=fgrade;
//sumofgrades.push_back();
}
/////
void course_statistics(vector<int> course, int cs)
{
vector<int> sortedgrades,agrades,bgrades,cgrades,dgrades,fgrades;
double avg=0,gpa=0;
sortedgrades=course;
int persent, amount,i,persent2,numenrolled=0,sum=0;
sort(sortedgrades.begin(), sortedgrades.end());
amount=sortedgrades.size();
persent=amount*15/100;
for(i=0; i<persent; i++)
{
agrades.push_back(sortedgrades[i]);
}
persent=amount*15/100;
persent2=amount*35/100;
for(i=persent; i<persent2; i++)
{
bgrades.push_back(sortedgrades[i]);
}
persent=amount*35/100;
persent2=amount*60/100;
for(i=persent; i<persent2; i++)
{
cgrades.push_back(sortedgrades[i]);
}
persent=amount*60/100;
persent2=amount*80/100;
for(i=persent; i<persent2; i++)
{
dgrades.push_back(sortedgrades[i]);
}
persent=amount*80/100;
persent2=amount*100/100;
for(i=persent; i<persent2; i++)
{
fgrades.push_back(sortedgrades[i]);
}
numenrolled=sortedgrades.size();
if(numenrolled>0)
{
for(i=0; i<numenrolled; i++)
{
sum=sum+sortedgrades[i];
}
avg=sum/numenrolled;
gpa=avg/20;
gpa=gpa-1;
}
double grdpersent;
grdpersent=agrades.size()*100;
grdpersent=grdpersent/numenrolled;
//grdpersent=grdpersent/(10);
cout<<cs;
cout<<“\t”<<numenrolled;
cout<<“\t”<<grdpersent;
grdpersent=bgrades.size()*100;
grdpersent=grdpersent/numenrolled;
cout<<“\t\t”<<grdpersent;
grdpersent=cgrades.size()*100;
grdpersent=grdpersent/numenrolled;
//grdpersent=grdpersent/(10);
cout<<“\t\t”<<grdpersent;
grdpersent=dgrades.size()*100;
grdpersent=grdpersent/numenrolled;
//grdpersent=grdpersent/(10);
cout<<“\t\t”<<grdpersent;
grdpersent=fgrades.size()*100;
grdpersent=grdpersent/numenrolled;
//grdpersent=(grdpersent)%100;
cout<<“\t\t”<<grdpersent;
cout<<“\t\t”<<gpa;
}
/////////////////////////////////////////////////////////////////
int main()
{
int s,i,j,p,r,a,b,t,amount,m,temprand=0;
int crs_list[]={102, 104, 204, 210, 212, 217, 220, 221, 301, 304, 317, 318, 322, 332, 335, 340, 412, 420, 450, 470, 480, 599 };
vector<int> enrolled;
float k;
char chrs;
int sumofgrades=0;
double tmpgpa;
Student students,sorted_students;
students.courses.resize(22, vector<int> (0));
Course clascourse;
srand(time(0));
//cout<<“—Randomly Generated Students—“<<endl;
amount=300;
for(i=0; i<amount; i++)
{
sumofgrades=0;
enrolled.clear();
//cout<<“Student “<<i+1<<“:”<<endl;
students.fname.clear();
//. students.ssn.clear();
for(m=1;m<10; m++)
{
if (m==1)
{
temprand=0;
}
r=rand()%9+1;
// cout<<r<<” “;
temprand=temprand+abs(r);
if(m<9)
{
temprand=temprand*10;
}
}
students.ssid.push_back(temprand);
/*cout<<“SSID:”<<students.ssid[i]<<endl;*/
/*s=students.ssid[i]/1000000;
cout<<“SSID:”<<s;
s=students.ssid[i]%1000000;
s=s/10000;
cout<<“-“<<s;
s=students.ssid[i]%10000;
cout<<“-“<<s<<endl;*/
a=12;
b=2;
t=box_muller(a,b);
r=rand()%26+65;
chrs=(char) r;
students.fname+=chrs;
for(j=1;j<t; j++)
{
r=rand()%26+97;
chrs=(char) r;
students.fname+=chrs;
}
students.firstname.push_back(students.fname);
//cout<<“First Name:”<<students.firstname[i]<<endl;
students.lname.clear();
t=box_muller(a,b);
r=rand()%26+65;
chrs=(char) r;
students.lname+=chrs;
for(j=1;j<t; j++)
{
r=rand()%26+97;
chrs=(char) r;
students.lname+=chrs;
}
students.lastname.push_back(students.lname);
//cout<<“Last Name:”<<students.lastname[i]<<endl;
//cout<<endl;
int crs;
crs=(rand()%4)+3;
//cout<<“Number of Courses “<<crs<<endl;
for(int w=1; w<=crs; w++)
{
int cr;
cr=rand()%22;
if(w==1)
{
enrolled.push_back(crs_list
[cr]
);
}
else
{
for(int y=0; y<enrolled.size(); y++)
{
while(cr==enrolled[y])
{
cr=rand()%22;
y=0;
}
}
enrolled.push_back(crs_list
[cr]
);
}
gradethecourse(students.courses
[cr]
,sumofgrades);
// students.courses[2].push_back(1);
//students.courses[4].push_back(2);
}
students.sumofgrades.push_back(sumofgrades);
tmpgpa=sumofgrades/enrolled.size();
tmpgpa=(tmpgpa/20)-1;
students.gpa.push_back(tmpgpa);
students.enrolled.push_back(enrolled);
for(int y=0; y<students.enrolled[i].size(); y++)
{
//cout<<“CS”<<students.enrolled[i][y]<<endl;
}
/*cout<<“sum of grades:”<<students.sumofgrades[i]<<endl;
cout<<endl;*/
}
for(i=0; i<22; i++)
{
/*cout<<“CS “<<crs_list[i]<<” Grades:”<<endl;*/
for(int y=0; y<students.courses[i].size(); y++)
{
//cout<<” “<<students.courses[i][y];
}
//cout<<endl;
}
cout<<“CS\t#ENROLL\tA\t\tB\t\tC\t\tD\t\tF\t\tAVG-GPA”<<endl;
for(i=0; i<22; i++)
{
course_statistics(students.courses[i],crs_list[i]);
clascourse.courses.push_back(students.courses[i]);
cout<<endl;
}
students.m_courses.insert(clascourse);// all courses inserted to Bag
sort_descending(students, &sorted_students);
cout<<“\nTop-Ten-Students (by GPA)\n”<<endl;
out_results(sorted_students);
return 0;
}
Solution
hw5.cpp
//#ifndef MAIN_SAVITCH_BAG2_H
//#define MAIN_SAVITCH_BAG2_H
#include <iostream>
#include <cstdlib> // Provides size_t
#include <cassert>
#include <cstring>
#include <algorithm> // Provides copy function
//#include “SortedBag.h”
//#include “Student.h”
using namespace std;
class Student
{
public:
Student() : a(0) {}
Student(int _a) : a(_a) {}
bool operator != (const Student& o)
{
return a != o.a;
}
bool operator == (const Student& o)
{
return a == o.a;
}
bool operator > (const Student& o)
{
return a > o.a;
}
bool operator < (const Student& o)
{
return a < o.a;
}
void operator–()
{
a–;
}
int a;
};
template<class T>
struct Node {
Node() : m_next(this), m_prev(this) {}
Node(const T& val, Node<T>* n =0, Node<T>* p =0): m_val(val)
{ m_next = n? n : this; m_prev = p? p : this; }
bool is_singleton() const { m_next == this && m_prev == this; }
T m_val;
Node<T>* m_next; // pointer to successor element.
Node<T>* m_prev; // pointer to predecessor element.
};
template<class T>
class SortedBag {
public:
SortedBag();
SortedBag(const SortedBag&);
void operator =(const SortedBag&);
~SortedBag();
bool erase_one(const T&);
long erase(const T&);
void insert(const T&);
void operator +=(const SortedBag&);
long size() const { return m_size; }
long count(const T&) const;
// SIMPLE ITERATOR
void begin(const bool ascending =true);
bool end() const;
void operator++();
void operator–();
T&get();
private:
Node<T>* m_data; // pointer to ring structure.
long m_size; // number of elements in the Bag.
bool m_asc; // flag to indicate iteration in ascending order or not.
Node<T>* m_curr; // iterator’s current position.
};
template<class T>
SortedBag<T> operator+(const SortedBag<T>&, const SortedBag<T>&);
// template<class T>
//const SortedBag<T>::size_type SortedBag::DEFAULT_CAPACITY;
template<class T>
SortedBag<T>::SortedBag()
: m_data(0), m_size(0), m_asc(true), m_curr(0)
{}
template<class T>
SortedBag<T>::~SortedBag()
{
if (m_data)
{
while (m_data->m_next != m_data)
{
Node<T> *next(m_data->m_next);
m_data->m_next = next->m_next;
delete next;
}
delete m_data;
m_data = NULL;
}
}
template<class T>
SortedBag<T>::SortedBag(const SortedBag& source)
: m_data(0), m_size(0), m_asc(source.m_asc), m_curr(0)
{
*this = source;
}
template<class T>
void SortedBag<T>::operator=(const SortedBag &source)
{
m_data = 0;
m_size = 0;
m_curr = 0;
if (!source.m_data)
{
return;
}
Node<T> *tmp(source.m_data);
do
{
insert(tmp->m_val);
tmp = tmp->m_next;
}
while (tmp != source.m_data);
}
template<class T>
long SortedBag<T>::erase(const T& target)
{
Node<T> *tmp, *q;
if (m_data->m_val == target)
{
if (m_data->m_next == m_data)
{
delete m_data;
m_data = NULL;
m_curr = NULL;
m_size–;
//cout << “Element Deleted. Total size: ” << m_nSize << endl;
return m_size;
}
tmp = m_data;
m_data->m_prev->m_next = m_data->m_next;
m_data = m_data->m_next;
m_data->m_prev = tmp->m_prev;
delete tmp;
m_size–;
//cout << “Element Deleted. Total size: ” << m_nSize << endl;
return m_size;
}
q = m_data;
do
{
if (q->m_val == target)
{
Node<T> *next(q->m_next);
Node<T> *prev(q->m_prev);
next->m_prev = prev;
prev->m_next = next;
delete q;
m_size–;
q = next;
// cout << “Element Deleted. Total size: ” << m_nSize << endl;
}
else
{
q = q->m_next;
}
} while (q != m_data);
return m_size;
}
template<class T>
bool SortedBag<T>::erase_one(const T& index)
{
return erase(index);
}
template<class T>
void SortedBag<T>::insert(const T& entry)
{
Node<T> *temp;
temp = new(struct Node<T>);
temp->m_val = entry;
if (m_data == NULL)
{
m_data = temp;
m_curr = m_data;
m_size++;
//cout << “Element Inserted. Total size: ” << m_size << endl;
return;
}
if (m_size == 1)
{
temp->m_next = m_data;
temp->m_prev = m_data;
m_data->m_prev = temp;
m_data->m_next = temp;
if (m_asc && m_data->m_val > entry ||
!m_asc && m_data->m_val < entry)
{
m_data = temp;
}
m_size++;
//cout << “Element Inserted. Total size: ” << m_size << endl;
return;
}
Node<T>* nodeInsertBefore(m_data);
Node<T>* currentIt(m_data);
do
{
if (m_asc && currentIt->m_val > entry)
{
nodeInsertBefore = currentIt;
if (nodeInsertBefore == m_data)
{
m_data = temp;
}
break;
}
if (!m_asc && currentIt->m_val < entry)
{
nodeInsertBefore = currentIt;
if (nodeInsertBefore == m_data)
{
m_data = temp;
}
break;
}
currentIt = currentIt->m_next;
}
while (currentIt != m_data);
temp->m_next = nodeInsertBefore;
temp->m_prev = nodeInsertBefore->m_prev;
nodeInsertBefore->m_prev = temp;
temp->m_prev->m_next = temp;
m_size++;
//cout<<“Element Inserted: ” << entry << ” Total size: “<< m_size << endl;
}
template<class T>
void SortedBag<T>::operator+=(const SortedBag & bag)
{
SortedBag sortedBag(bag);
sortedBag.begin(true);
while (!sortedBag.end())
{
insert(sortedBag.get());
++sortedBag;
}
insert(sortedBag.get());
}
template<class T>
long SortedBag<T>::count(const T & target) const
{
if (m_data == NULL)
{
return 0;
}
Node<T> *temp(m_data);
long count(0);
while (temp->m_next != m_data)
{
if (temp->m_val == target)
{
count++;
}
temp = temp->m_next;
}
return count;
}
template<class T>
void SortedBag<T>::begin(const bool ascending)
{
if (ascending)
{
m_curr = m_data;
}
else
{
m_curr = m_data->m_prev;
}
}
template<class T>
bool SortedBag<T>::end() const
{
return m_curr == m_data->m_prev;
}
template<class T>
void SortedBag<T>::operator++()
{
m_curr = m_curr->m_next;
}
template<class T>
void SortedBag<T>::operator–()
{
m_curr = m_curr->m_prev;
}
template<class T>
T & SortedBag<T>::get()
{
return m_curr->m_val;
}
void compare(SortedBag<Student>& L1, SortedBag<Student>& L2)
{
if (L1.size() != L2.size())
{
cout<< ” have different size” << endl;
}
else
{
cout<< ” have identical size” << endl;
L1.begin();
L2.begin();
bool contentIdentical = true;
while (!L1.end() && !L2.end())
{
if (L1.get() != L2.get())
{
contentIdentical = false;
break;
}
++L1;
++L2;
}
if (contentIdentical)
{
cout<< ” have identical content” << endl;
}
else
{
cout<< ” have different content” << endl;
}
}
}
int main()
{
SortedBag<Student> L1, L2, L3;
for (int i = 0; i < 200; i++)
{
L1.insert(Student(rand()));
}
L1.begin(true);
while (!L1.end())
{
L2.insert(L1.get());
++L1;
}
L2.insert(L1.get());
cout<< “1. Compare L1 and L2:” << endl;
compare(L1, L2);
L1.begin(false);
do
{
L3.insert(L1.get());
–L1;
} while (!L1.end());
cout<< “1. Compare L1 and L3:” << endl;
compare(L1, L3);
L1.begin(true);
Student first(L1.get());
L1.begin(false);
Student last(L1.get());
L1.erase(first);
L1.erase(last);
L2.erase(first);
L2.erase(last);
cout<< “2. Compare L1 and L2:” << endl;
compare(L1, L2);
L2.begin(true);
while (!L2.end())
{
L1.erase(L2.get());
++L2;
}
L1.erase(L2.get());
cout<< “2. L1 size: ” <<L1.size() << endl;
L3 = L2;
int deletedItems(0);
while (deletedItems < 50)
{
int index = rand() % L2.size();
L2.begin(true);
for (int i = 0; i < index; i++)
{
++L2;
}
L1.insert(L2.get());
L2.erase(L2.get());
deletedItems++;
}
L1 += L2;
cout<< “3. Compare L1 and L3:” << endl;
compare(L1, L3);
return 0;
}