1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
#include <iostream>
#include <string>
#include <iomanip>
#include <algorithm>
void display_text_line( int line_number, const std::string& name, long bday )
{
// replace with printf if the EV3 implementation does not support C++ streams
std::cout << std::setw(2) << line_number << ". " << std::setw(25) << name << " "
<< std::setw(6) << std::setfill('0') << bday << std::setfill(' ') << '\n' ;
}
void display_lines( int start_line_number, const std::string name[], const long bday[],
int num_items, int lines_per_page = 7 )
{
const int end_line_number = std::min( start_line_number+lines_per_page, num_items ) ;
for( int i = std::max( start_line_number, 0 ) ; i < end_line_number ; ++i )
display_text_line( i+1, name[i], bday[i] ) ;
}
// simulated button press (for testing); replace these with the actual EV3 functions
#include <cctype>
static char button_pressed = 0 ;
void waitForButtonPress() { std::cin >> button_pressed ; button_pressed = std::toupper(button_pressed) ; }
bool getButtonPress( char c ) { return button_pressed == std::toupper(c) ; }
///////////////////////////////////////////////////////////////////////
int main()
{
const int N_ITEMS = 21 ;
const std::string names[N_ITEMS] = { "Gary Oldman", "Casey Affleck", "Leonardo DiCaprio",
"Eddie Redmayne", "Matthew McConaughey", "Daniel Day-Lewis", "Jean Dujardin",
"Forest Whitaker", "Jamie Foxx", "Sean Penn", "Denzel Washington", "Russell Crowe",
"Jack Nicholson", "Tom Hanks", "Al Pacino", "Janet Gaynor", "Norma Shearer",
"Helen Hayes", "Katharine Hepburn", "Claudette Colbert", "Bette Davis" };
const long bdays[N_ITEMS] = { 580321, 750812, 741111, 820106, 690411, 570429, 720619,
610615, 671213, 600816, 541228, 640407, 370422, 400425, 61006, 841006, 20811,
1010, 71012, 30913, 80405 };
const char BUTTONUP = 'U' ;
const char BUTTONDOWN = 'D' ;
int start_line_number = 0 ;
bool quit = false ;
while( !quit )
{
std::cout << "\n----------------------------------------------\n" ;
display_lines( start_line_number, names, bdays, N_ITEMS ) ;
std::cout << "\nenter 'U' for up, 'D' for down, anything else to quit: " ;
waitForButtonPress() ;
if( getButtonPress(BUTTONUP) ) --start_line_number ;
else if( getButtonPress(BUTTONDOWN) ) ++start_line_number ;
else quit = true ;
}
}
|