my code stock.com

garage Item analysis

Snippet options

Download: Download snippet as assignment-1.cpp.
Copy snippet: For this you need a free my code stock.com account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

#include <iostream>
#include <string>

#include <fstream>
#include <sstream>
#include <cstring>

using namespace std;
struct GarageItem{
  string type;
  int price;
  int statue;
};

void itemLeft(GarageItem  arrayOfGarageItems[],int index)
{
  int cnt2 = 0;
  for(int i=0;i<index;i++){
    if( arrayOfGarageItems[i].statue == 0 ){
      cout<< arrayOfGarageItems[i].type<< ","<< "for sale"<<","<< arrayOfGarageItems[i].price<<endl;
      cnt2++;
    }
    else if ( arrayOfGarageItems[i].statue == 1 )
      {
	cout<< arrayOfGarageItems[i].type<< ","<<"wanted"<<"," << arrayOfGarageItems[i].price<<endl;
	cnt2++;
      }
    else
      {

	continue;
      }
  }

  cout<<cnt2<<" unsold items"<<endl;
}
void bubbleSortRoutine(GarageItem  arrayOfGarageItems[],int index){
  // float time = clock();
  string tmp;
  int t;
  int c;

  for(int i=0;i<index-1;i++){

    for(int j=i+1;j<index;j++){
      /*      if(strcmp(arrayOfGarageItems[i],arrayOfGarageItems[j])>0){
	      strcpy(tmp,arrayOfGarageItems[i]);
	      strcpy(arrayOfGarageItems[i],arrayOfGarageItems[j]);
	      strcpy(arrayOfGarageItems[j],tmp);*/
      
   
      // if(arrayOfGarageItems[i].type.at(0)<=arrayOfGarageItems[j].type.at(0)){
      if(arrayOfGarageItems[i].type>=arrayOfGarageItems[j].type){
	tmp=arrayOfGarageItems[i].type;
	arrayOfGarageItems[i].type=arrayOfGarageItems[j].type;
	arrayOfGarageItems[j].type=tmp;
	t=arrayOfGarageItems[i].price;
     	arrayOfGarageItems[i].price=arrayOfGarageItems[j].price;
	arrayOfGarageItems[j].price=t;
       	c=arrayOfGarageItems[i].statue;
     	arrayOfGarageItems[i].statue=arrayOfGarageItems[j].statue;
	arrayOfGarageItems[j].statue=c;

      }
 
    }
            

  }

    
  //  itemLeft(arrayOfGarageItems,index);
  /* time = (clock() - time) / CLOCKS_PER_SEC;
     cout<<" cycle: "<<time<<" sec"<<endl;
     cout<<"\n";*/
  
      
}
void insertionSortRoutine(GarageItem  arrayOfGarageItems[],int index){
  // float time = clock();
  string tmp;
  int c;
  int t;
  int i,j;
  for(i=1; i< index;i++){
    tmp=arrayOfGarageItems[i].type;
    c=arrayOfGarageItems[i].price;
    t=arrayOfGarageItems[i].statue;
    j=i-1;
    while(j>=0 && arrayOfGarageItems[j].type> tmp){
      arrayOfGarageItems[j+1].type=arrayOfGarageItems[j].type;
      arrayOfGarageItems[j+1].price=arrayOfGarageItems[j].price;
      arrayOfGarageItems[j+1].statue=arrayOfGarageItems[j].statue;
      j=j-1;
    }
    arrayOfGarageItems[j+1].type=tmp;
    arrayOfGarageItems[j+1].price=c;
    arrayOfGarageItems[j+1].statue=t;
    
  }
  //  itemLeft(arrayOfGarageItems,index);
  /*  time = (clock() - time) / CLOCKS_PER_SEC;
      cout<<" cycle: "<<time<<" sec"<<endl;
      cout<<"\n";*/
    
}
void swap(string *a, string *b){
  string c=*a;
  *a=*b;
  *b=c;
}
void swap1(int *a, int *b){
  int c=*a;
  *a=*b;
  *b=c;
}
int  quicksort(GarageItem  arrayOfGarageItems[],int low,int high){
  
  string pivot =arrayOfGarageItems[high].type;
  /*int c=arrayOfGarageItems[high].price;
    int t=arrayOfGarageItems[high].statue;*/
  int i= (low-1);
  for(int j=low;j<=high-1;j++) {
    if(arrayOfGarageItems[j].type<= pivot){
      i++;
     
      swap(&arrayOfGarageItems[i].type,&arrayOfGarageItems[j].type);
      swap1(&arrayOfGarageItems[i].price,&arrayOfGarageItems[j].price);
      swap1(&arrayOfGarageItems[i].statue,&arrayOfGarageItems[j].statue);
    }
  }
  swap(&arrayOfGarageItems[i+1].type,&arrayOfGarageItems[high].type);
  swap1(&arrayOfGarageItems[i+1].price,&arrayOfGarageItems[high].price);
  swap1(&arrayOfGarageItems[i+1].statue,&arrayOfGarageItems[high].statue);
  return (i+1);
}
   
void quickSortRoutine(GarageItem  arrayOfGarageItems[],int low,int high)
{
  // float time = clock();
  if(low<high) {
    int p= quicksort(arrayOfGarageItems, low,high);
    quickSortRoutine(arrayOfGarageItems,low,p-1);
    quickSortRoutine(arrayOfGarageItems,p+1,high);
  }

   
  //itemLeft(arrayOfGarageItems,high+1);
  /*   time = (clock() - time) / CLOCKS_PER_SEC;
       cout<<" cycle: "<<time<<" sec"<<endl;
       cout<<"\n";*/
}


void  orderRemainingItems(GarageItem  arrayOfGarageItems[],int index){
  int user;
  cout<<"\n";
  cout<<"sort as:  "<<endl;
  cout<<"1.  BubbleSort"<<endl;
  cout<<"2.  InsertionSort"<<endl;
  cout<<"3.  QuickSort"<<endl;
  cin>>user;
  float time = clock();
  if(user==1)   bubbleSortRoutine(arrayOfGarageItems,index);
  else if(user==2)  insertionSortRoutine(arrayOfGarageItems,index);
  else if(user==3) quickSortRoutine(arrayOfGarageItems,0,index-1);
  time = (clock() - time) / CLOCKS_PER_SEC;
  cout<<" time taken to only sort element (not print): "<<time<<" sec"<<endl;
  cout<<"\n";
  itemLeft(arrayOfGarageItems,index);
  cout<<"\n";
  
  
 
}


int main(int argc, char* argv[])
{
  GarageItem  arrayOfGarageItems[100];
  int itemSold = 0;
  ifstream ifs(argv[1]);
  ifs.is_open();
  if(ifs.fail())
    {
      cout << "failed to open" << endl;
    }
  else
    {
      string item;
      int cnt = 0;
      int value;
      string a,b,c;
      while(getline(ifs,item,'\n'))
	{
	  if(item.find("chicken")!= EOF)
	    {
	      arrayOfGarageItems[cnt].type = "chicken";
	    }

	  else if(item.find("microwave")!= EOF)
	    {
	      arrayOfGarageItems[cnt].type = "microwave";
            }
	  else if(item.find("bike")!=EOF)
	    {                arrayOfGarageItems[cnt].type = "bike";
            }
	  else if(item.find("truck")!= EOF)
	    {               arrayOfGarageItems[cnt].type = "truck";
            }
	  else if(item.find("dresser")!=EOF)
	    {                arrayOfGarageItems[cnt].type = "dresser";
            }
	  if(item.find("wanted")!=EOF)

	    {
	      arrayOfGarageItems[cnt].statue = 1;
	      stringstream ss(item);
	      ss>>a>>c>>value;
	      arrayOfGarageItems[cnt].price = value;
            }else if(item.find("sale")!=EOF)
	    {
	      arrayOfGarageItems[cnt].statue =0;
	      stringstream ss(item);
	      ss>>a>>b>>c>> value;
	      arrayOfGarageItems[cnt].price = value;
	    }
	  for(int h =0;h<cnt;h++)
	    {

	      if( arrayOfGarageItems[cnt].type ==  arrayOfGarageItems[h].type)
		{
		  if( arrayOfGarageItems[cnt].statue !=  arrayOfGarageItems[h].statue)
		    {
		      if( arrayOfGarageItems[cnt].statue == 0)
			{
			  if( arrayOfGarageItems[cnt].price<= arrayOfGarageItems[h].price)

			    {
			      cout<< arrayOfGarageItems[cnt].type<<" "<< arrayOfGarageItems[cnt].price<<endl;

			      for(int y = h ; y < cnt ; y++)
				{
   
				  arrayOfGarageItems[y] =  arrayOfGarageItems[(y+1)];
				}
			      cnt-=1;       
			      arrayOfGarageItems[cnt] = {};
			      cnt-=1;
			      break;
			    }
			  else{continue;}
			}

		      else if( arrayOfGarageItems[cnt].statue == 1)
			{
			  if( arrayOfGarageItems[cnt].price >=  arrayOfGarageItems[h].price)

			    {
			      cout<< arrayOfGarageItems[cnt].type<<" "<< arrayOfGarageItems[h].price<<endl;
			      for(int y = h ; y < cnt ; y++)

				{
				  arrayOfGarageItems[y] =  arrayOfGarageItems[(y+1)];
				}
			      cnt-=1;
			      arrayOfGarageItems[cnt] = {};
			      cnt-=1;
			      itemSold++;
			      break;
                            }
			  else
			    {
			      continue;
			    }
			}
		      else
			{
			  cout<<"error"<<endl;
			}
		    }
		  else
		    {
		      continue;
		    }
                }

	      else
		{
		  continue;
		}
            }
	  cnt++;
        }
      ifs.close();
      cout << "Items Sold: " << itemSold << endl;
      cout << "#" << endl;
      itemLeft( arrayOfGarageItems, cnt);
      cout<<"#"<<endl;
      int in;
      cout<< "press 1 to continue "<<endl;
      cin>> in;
      if(in== 1) orderRemainingItems( arrayOfGarageItems, cnt);
      
      else   return 0;
    }
}



  
  
  

Create a free my code stock.com account now.

my code stok.com is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.