0

是否可以使用控制台用户界面进行分页?假设我有 30 条员工记录,我只想显示 10 条。用户按下箭头按钮,他们可以看到接下来的 10 条列表。可能吗?

EXEC SQL DECLARE EmployeeReportCursorJawatan CURSOR FOR
        SELECT EMP_ID, NAMA, IC, JAWATAN, GAJI_ASAS, TEL_RUMAH, TEL_BIMBIT, ALAMAT FROM PEKERJA WHERE upper(JAWATAN) LIKE upper(:str);

    EXEC SQL OPEN EmployeeReportCursorJawatan;
    /* loop through the cursor and break the loop when end*/
    EXEC SQL WHENEVER NOT FOUND DO break;
    while (1)
    {
        /* fetch the data from cursor and display*/
        EXEC SQL FETCH EmployeeReportCursorJawatan INTO :emp_idSearch, :namaSearch, :icSearch, :jawatanSearch, :gajiSearch, :homeSearch, :mobileSearch, :alamatSearch;

    cout<<"\nID\t\t: " << emp_idSearch;
    cout<<"\nName\t\t: " << namaSearch;
    cout<<"\nIC\t\t: " << icSearch;
    cout<<"\nPost\t\t: " << jawatanSearch;
    cout<<"\nSalary\t\t: RM " << gajiSearch;
    cout<<"\nPhone number (H): " << homeSearch;
    cout<<"\nPhone number (M): " << mobileSearch;
    cout<<"\nAddress\t: " << alamatSearch;
    cout<<"\n---------------------------------------------------------\n";

        totalCustomer++;
    }
4

2 回答 2

0

合乎逻辑的解决方案是安装more在机器上,并让用户通过它管道输出。但实施起来并不难。基本上,您决定在一页上放置多少行或条目,然后跟踪当前页面顶部的索引。然后输出如下内容:

int currentTop = 0;
bool finished = false;
while ( !finished ) {
    clearScreen();
    for ( int i = 0; i < maxOnPage && i + currentTop < entryCount; ++ i ) {
        displayEntry( i + currentTop );
    }
    std::cout << "Continue: ";
    std::string command;
    std::cin >> command;
    switch ( parseCommand( command ) ) {
    case advanceOneLine:
        ++ currentTop;
        break;

    case advanceOnePage:
        currentTop += maxOnPage;
        break;

    case finish:
        finished = true;
        break;

    default:
        //  ???
        break;
    }
}
于 2012-12-28T15:00:29.097 回答
0

是的,它实际上比你想象的要容易得多。这是我编写的程序中的一段代码,它正是这样做的,重要的是简单地创建一个静态变量来表示用户所在的“页面”。破败不堪

  • 使用静态变量来表示页码
  • 使用迭代器和 std::advance 将迭代器推送到当前页码
  • 从页面的第一个元素开始循环到最后一个元素(在这个程序中,我让每个页面显示我的 lsit 的 10 个元素

void ExList::Display()
{
    using std::setw; //save time
    //begin iter starts at page number * 10 since each page has 10 elements
    list<Workout>::iterator PageNum = wrkout.begin(); std::advance(PageNum, AdvanceVar * 10);
    //advance the iter 10 places past PageNum so the loop will run 10 times
    list<Workout>::iterator iterEnd = PageNum; std::advance(iterEnd, 10);
    //formatting for my 'exercise tracker' program
    std::cout << setw(20) << "Page: " << AdvanceVar + 1 << "/" << (((int) wrkout.size()) /  10) + 1
     << std::endl << std::endl;
    std::cout << std::left          //left justified
    << setw(TAB) << "Date"          //print titles
    << setw(TAB) << "Type"
    << setw(TAB) << "Distance"
    << setw(TAB) << "Units"
    << setw(TAB) << "Additional Comments\n"
    << "---------------------------------------------------------------------\n";
    //loop through 10 times displaying the proper elements
    for(PageNum; PageNum != iterEnd && PageNum != wrkout.end(); PageNum++)
    {
        std::cout << std::left
        << setw(TAB) << PageNum->Date
        << setw(TAB) << ConvertEnum(PageNum->TYPE)
        << setw(TAB) << PageNum->distance
        << setw(TAB) << PageNum->units
        << setw(TAB) << "Select to review\n";
    }

    std::cout << "---------------------------------------------------------------------\n";


}

如果您想查看整个程序以获得更深入的程序,请给我发送电子邮件

于 2012-12-25T21:23:51.607 回答