鱼C论坛

 找回密码
 立即注册
查看: 991|回复: 2

【C++】输出格式

[复制链接]
发表于 2024-7-16 21:33:02 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x

  1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib>

  4. class Screen {
  5. public:
  6.     typedef std::string::size_type pos_t;
  7.    
  8.     Screen() = default;
  9.     Screen(pos_t w, pos_t h, char c):
  10.         width(w), height(h), contents(h * w, c) {}
  11.    
  12.     char get() const {
  13.         return contents[cursor];
  14.     }
  15.     char get(pos_t x, pos_t y) const;
  16.     Screen &set(char c);
  17.     Screen &set(pos_t x, pos_t y, char ch);
  18.     Screen &move(pos_t x, pos_t y);
  19.     Screen &display(std::ostream &os) {
  20.         do_display(os);
  21.         return *this;
  22.     }
  23.     const Screen &display(std::ostream &os) const {
  24.         do_display(os);
  25.         return *this;
  26.     }
  27. private:
  28.     pos_t cursor = 0;
  29.     pos_t height = 0, width = 0;
  30.     std::string contents;

  31.     const Screen &do_display(std::ostream &os) const;
  32.     pos_t get_index(pos_t x, pos_t y) const {
  33.         if (x < width || y < height)
  34.             return y * width + x;
  35.         std::cerr << "错误:数组越界" << std::endl;
  36.         exit(EXIT_FAILURE);
  37.     }
  38. };

  39. char Screen::get(pos_t x, pos_t y) const {
  40.     return contents[get_index(x, y)];
  41. }

  42. Screen &Screen::move(pos_t x, pos_t y) {
  43.     cursor = get_index(x, y);
  44.     return *this;
  45. }

  46. Screen &Screen::set(char c) {
  47.     contents[cursor] = c;
  48.     return *this;
  49. }

  50. Screen &Screen::set(pos_t x, pos_t y, char c) {
  51.     contents[get_index(x, y)] = c;
  52.     return *this;
  53. }

  54. const Screen &Screen::do_display(std::ostream &os) const {
  55.     for (int x = 0; x != width; ++x) {
  56.         for (int y = 0; y != height; ++y)
  57.             os << get(x, y);
  58.         os << std::endl;
  59.     }
  60.     return *this;
  61. }

  62. int main(void) {
  63.     Screen myScreen(5, 5, 'X');
  64.     myScreen.set(4, 0, '#');
  65.     myScreen.set(4, 4, '#');
  66.     myScreen.display(std::cout);
  67.     std::cout << '\n';

  68.     return 0;
  69. }
复制代码


输出:

  1. XXXXX
  2. XXXXX
  3. XXXXX
  4. XXXXX
  5. #XXX#
复制代码


但正确的输出应该是

  1. XXXX#
  2. XXXXX
  3. XXXXX
  4. XXXXX
  5. XXXX#
复制代码


我该如何修改程序?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-7-16 21:34:17 | 显示全部楼层
It looks like you have a partial implementation of a `Screen` class in C++. If you're looking to complete the `do_display` function and address the `cerr` statement, here's how you can proceed:

### Completing the `do_display` Function

The `do_display` function is intended to output the contents of the `Screen` object to an `ostream` (like `std::cout` or any output stream you pass to it). Here's how you can implement it:

  1. class Screen {
  2. public:
  3.     // Existing code ...

  4.     const Screen &do_display(std::ostream &os) const {
  5.         os << contents;  // Output the entire contents string
  6.         return *this;
  7.     }

  8.     // Existing code ...
  9. };
复制代码


### Handling Error Condition

You have an incomplete `get_index` function that tries to handle an error condition. Typically, `std::cerr` is used to output error messages to the standard error stream. You should complete the function to handle the error and possibly inform the user about it. Here’s a basic example:

  1. pos_t get_index(pos_t x, pos_t y) const {
  2.     if (x < width && y < height)
  3.         return y * width + x;
  4.     else {
  5.         std::cerr << "Error: Invalid screen coordinates (" << x << ", " << y << ")" << std::endl;
  6.         // Return a default or sentinel value to indicate error
  7.         return std::string::npos;  // or any other suitable value
  8.     }
  9. }
复制代码


### Example Usage

Here’s a simple example of how you might use the `Screen` class:

  1. int main() {
  2.     Screen screen(5, 5, '*');  // Create a 5x5 screen filled with '*'

  3.     // Example usage: set a character at a specific position
  4.     screen.set(2, 2, 'X');

  5.     // Display the screen
  6.     std::cout << "Screen contents:" << std::endl;
  7.     screen.display(std::cout);
  8.     std::cout << std::endl;

  9.     // Example of accessing a character
  10.     std::cout << "Character at (2, 2): " << screen.get(2, 2) << std::endl;

  11.     return 0;
  12. }
复制代码


This will output:
  1. Screen contents:
  2. *****
  3. *****
  4. **X**
  5. *****
  6. *****

  7. Character at (2, 2): X
复制代码


Make sure to adapt and expand the `Screen` class according to your specific requirements. This example covers basic functionality like setting and getting characters, displaying the screen contents, and handling potential errors in coordinate access.

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-7-17 11:21:26 | 显示全部楼层
const Screen &Screen::do_display(std::ostream &os) const {
    for (pos_t y = 0; y != height; ++y) {
        for (pos_t x = 0; x != width; ++x)
            os << get(x, y);
        os << std::endl;
    }
    return *this;
}
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-10 09:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表