【问题标题】:using gotoxy positioning in a "for loop"在“for循环”中使用gotoxy定位
【发布时间】:2019-09-18 23:47:08
【问题描述】:
int strtnumber,endnumber,remainder,i;
    gotoxy(2, 0);
    printf("Enter Start value: "); //enters start number
    cin >> strtnumber;
    gotoxy(2, 1);
    printf("Enter End Value: ");    //enters end number
    cin >> endnumber;
    cout << "==================================================="<< endl;
    for (; strtnumber <= endnumber; strtnumber++) {
        remainder = strtnumber % 2;

            if (remainder == 1) {

                    cout << strtnumber << endl;
            }

        if (remainder == 0) {

            cout << strtnumber << endl;
        }

    }

我想用 gotoxy 分隔奇数和偶数 这是我想要实现的输出:

         ODD    |   EVEN
          1     |     2
          3     |     4
          5     |     6

【问题讨论】:

  • gotoxy 来自哪个库? AFAIK 这不是标准的 c++ 函数
  • 混合 C IO、C++ IO 和一些特定于实现的控制台命令来移动光标是......会很混乱。
  • 除非您更新这些列中的计数,否则最好使用普通的 std::coutstd::setw
  • 嗯。这些不算数,是吗。你最好使用std::cout and std::setw`。

标签: c++ function loops positioning


【解决方案1】:

我想用 gotoxy 分隔奇数和偶数 这是 我想要实现的输出:

几乎我使用过的每个系统都有可用的 ansi 终端。甚至嵌入式系统也会(通过 RS232)连接到 VT100 或同等设备。

PC 配备了 ansi term 模拟器,或者可以轻松下载和安装模拟器。


注意:提供的 cmets 很清楚,Ansi i/o 不是 C++ 的一部分。

但您可以轻松找到示例代码来调用您可能需要的 ansi 终端函数。

这是我自己的课程的一个有用部分,它在每个版本的 Ubuntu Linux、多个 Linux 终端仿真器和 vxWorks 等上都有效。我目前使用安装了 Lubuntu 的仿真器。没找到名字,但是帮助引用了Qt_io:

#include <iostream>
using std::cout, std::endl;

#include <string>
using std::string, std::to_string;


class Ansi_t   // provide access to features of ansi-compatible terminal
{
public:

   string clrscr (void)
   { return '\033' + string("[H") + '\033' + string("[2J"); }

   //              --v------v-  C++ 2d array indexing is 0 based
   string gotoRC(int r, int c) {
      return  ('\033' + string("[") +
               to_string(r+1) + ';' + to_string(c+1) + 'H');
   } //                  0 ^                    0 ^
   //                      1,1 is top left of ansi screen

   // tbr - add other ansi functions here

}; // class Ansi_t

// usage: declare an instance,  Ansi_t ansi;
// invoke method:    std::cout << ansi.clrscr() << std::endl;
// invoke method:    std::cout << ansi.gotoRC() << std::endl;
// to specify a named escape sequence  ^^^^^^^^
// Ubuntu gnome-terminal ansi term cursor locations are
// 1-based with  origin 1,1 at top left corner


class Hello_t  // demo executable
{
   Ansi_t ansi;  // declare an instance 

public:
   Hello_t()  { cout << ansi.clrscr() << ansi.gotoRC(9, 18) << "Hello "; }
   ~Hello_t() { cout << " world!\n\n\n" << endl; }
   int operator()() { cout << "C++"; return 0; }   // functor entrance
};

int main(int , char* * ) { return (Hello_t()()); }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-31
    • 2020-07-06
    • 1970-01-01
    相关资源
    最近更新 更多