Google Answers Logo
View Question
 
Q: c++ programing question ( Answered 3 out of 5 stars,   3 Comments )
Question  
Subject: c++ programing question
Category: Computers
Asked by: purplepit-ga
List Price: $25.00
Posted: 04 Apr 2003 07:58 PST
Expires: 04 May 2003 08:58 PDT
Question ID: 185969
Given the program below, can anybody modify the template display
function so the displayed lists have [brackets around them,] and
commas between each item, plus if the list is longer than 5 items,
then only the first 3 and the last two items are displayed, the
missing items should be replaced by 3 dots in a row(...)

the program also needs to include the defenition of the display
function()so that it runs without errors!!!!!!


********************************************************************************

#pragma warning(disable: 4786) // advised for MSVC++5.0

#include <list>
#include <string>
#include <iostream>
#include <algorithm>

using namespace std;

template<class iT>
void display (iT start, iT finish)

 {
   for (iT it=start; it != finish; it++)
   {
    cout<<*it<< ' ';
   }
}

void main()

{

       list<string> critters;
       int i;

  critters.insert(critters.begin(),"antelope"); // antelope
  critters.insert(critters.begin(),"bear");     // bear antelope
  critters.insert(critters.begin(),"cat");      // cat bear antelope

  display(critters.begin(),critters.end());

  *find(critters.begin(),critters.end(),"cat") = "cougar"; // cougar
bear antelope

  display(critters.begin(),critters.end());

  // Put a zebra at the beginning, an ocelot ahead of antelope,
  // and a rat at the end.
  critters.push_front("zebra");
  critters.insert(find(critters.begin(),critters.end(),"antelope"),"ocelot");
  critters.push_back("rat");
  display(critters.begin(),critters.end());

  critters.sort();
  display(critters.begin(),critters.end());

  // Now let's erase half of the critters.
  int half = critters.size() / 2;
  for (i = 0; i < half; i++)
  {
    critters.erase(critters.begin());
  }
  display(critters.begin(),critters.end());
  
}
Answer  
Subject: Re: c++ programing question
Answered By: hammer-ga on 04 Apr 2003 09:38 PST
Rated:3 out of 5 stars
 
Here is the replacement display function.  NOTE: The Answer box may
wrap lines of code incorrectly.
void display (iT start, iT finish) 
{
iT        it;
int        cnt;
int        loop;
int        no_go;
int        end_list_items;

// Change these variables to change how many
// items your list displays
int        start_list_items = 3;
int        max_list_items = 5;

    end_list_items = max_list_items - start_list_items;
    cnt = 0;

    // Find out if we need the ... by counting elements
    for (it=start; it != finish; it++) 
       { 
        cnt++;
    }

    loop = 1;
    no_go = 0;
    cout << '[';
    for (it=start; it != finish; it++) 
       { 
        if(cnt > max_list_items)
        {
            if((loop > start_list_items) && (loop <= (cnt -
end_list_items)))
            {
                no_go = 1;
                if(loop == (start_list_items + 1)) cout << "... ";
            }
            else
            {
                no_go = 0;
            }
        }
        if(no_go == 0)
        {
                cout << *it; 
            if(loop < cnt)
            {
                if((cnt > max_list_items) && (loop ==
start_list_items))
                {
                    cout << " ";
                }
                else
                {
                    cout << ", ";
                }
            }
        }
        loop++;
    }
   cout << "]\n";
} 

- Hammer
purplepit-ga rated this answer:3 out of 5 stars
Thank you very much...
Purplepit

Comments  
Subject: Re: c++ programing question
From: ppmart2-ga on 04 Apr 2003 09:21 PST
 
have good code


#include <list> 
#include <string> 
#include <iostream> 
#include <algorithm> 
 
using namespace std; 
 
template<class iT> 
void display (iT start, iT finish) 
{ 
   iT it = start; 
   int count = 0; 
   while( it != finish) // find the number of item
   { 
   		count++;
   		it++;
   }
      
   if(count < 5) // if the number < 5
   {	// display normally with '[' and ','
   		int index = 1;
   		it = start;
   		cout << "[" ;
   		while( it != finish) 
   		{ 
   			cout << *it ;
   			if(index != count ) cout << ",";
   			index++;
   			it++;
   		}
   		cout << "]" ;
   }
   else
   {    // else the first 3 item and the last 2 ones are displayed
   		int index2 = 2;
   		it = start;
   		cout << "[" << 	*it << "," << *it++ << "," << *it++;
   		while(index2 != count-2)
   		{
   			it++;
   			index2++;
   		}
   		cout << ",...," << *it << "," << *it++ << "]";
   }
   cout << endl;
} 
 
void main() 
{ 
 
  list<string> critters; 
  int i; 
 
  critters.insert(critters.begin(),"antelope"); // antelope 
  critters.insert(critters.begin(),"bear");     // bear antelope 
  critters.insert(critters.begin(),"cat");      // cat bear antelope 
 
  display(critters.begin(),critters.end()); 
 
  *find(critters.begin(),critters.end(),"cat") = "cougar"; // cougar bear antelope
 
  display(critters.begin(),critters.end()); 
 
  // Put a zebra at the beginning, an ocelot ahead of antelope, 
  // and a rat at the end. 
  critters.push_front("zebra"); 
  critters.insert(find(critters.begin(),critters.end(),"antelope"),"ocelot");
  critters.push_back("rat"); 
  display(critters.begin(),critters.end()); 
 
  critters.sort(); 
  display(critters.begin(),critters.end()); 
 
  // Now let's erase half of the critters. 
  int half = critters.size() / 2; 
  for (i = 0; i < half; i++) 
  { 
    critters.erase(critters.begin()); 
  } 
  display(critters.begin(),critters.end()); 
   
}
Subject: Re: c++ programing question
From: ppmart2-ga on 04 Apr 2003 09:24 PST
 
a better one 

#include <list> 
#include <string> 
#include <iostream> 
#include <algorithm> 
 
using namespace std; 
 
template<class iT> 
void display (iT start, iT finish) 
{ 
   iT it = start; 
   int count = 0; 
   while( it != finish) // find the number of item
   { 
   		count++;
   		it++;
   }
      
   if(count <= 5) // if the number <= 5
   {	// display normally with '[' and ','
   		int index = 1;
   		it = start;
   		cout << "[" ;
   		while( it != finish) 
   		{ 
   			cout << *it ;
   			if(index != count ) cout << ",";
   			index++;
   			it++;
   		}
   		cout << "]" ;
   }
   else
   {    // else the first 3 item and the last 2 ones are displayed
   		int index2 = 2;
   		it = start;
   		cout << "[" << 	*it << "," << *it++ << "," << *it++;
   		while(index2 != count-2)
   		{
   			it++;
   			index2++;
   		}
   		cout << ",...," << *it << "," << *it++ << "]";
   }
   cout << endl;
} 
 
void main() 
{ 
 
  list<string> critters; 
  int i; 
 
  critters.insert(critters.begin(),"antelope"); // antelope 
  critters.insert(critters.begin(),"bear");     // bear antelope 
  //critters.insert(critters.begin(),"cat");      // cat bear antelope 
 
  display(critters.begin(),critters.end()); 
 
  *find(critters.begin(),critters.end(),"cat") = "cougar"; // cougar bear antelope
 
  display(critters.begin(),critters.end()); 
 
  // Put a zebra at the beginning, an ocelot ahead of antelope, 
  // and a rat at the end. 
  critters.push_front("zebra"); 
  critters.insert(find(critters.begin(),critters.end(),"antelope"),"ocelot");
  critters.push_back("rat"); 
  display(critters.begin(),critters.end()); 
 
  critters.sort(); 
  display(critters.begin(),critters.end()); 
 
  // Now let's erase half of the critters. 
  int half = critters.size() / 2; 
  for (i = 0; i < half; i++) 
  { 
    critters.erase(critters.begin()); 
  } 
  display(critters.begin(),critters.end()); 
   
}
Subject: Re: c++ programing question
From: purplepit-ga on 06 Apr 2003 14:07 PDT
 
Thank you very much, this was extremly helpful....
#Purplepit

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy