鱼C论坛

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

【C++】输出格式

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

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

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

x
#include <iostream>
#include <string>
#include <cstdlib>

class Screen {
public:
    typedef std::string::size_type pos_t;
    
    Screen() = default;
    Screen(pos_t w, pos_t h, char c):
        width(w), height(h), contents(h * w, c) {}
    
    char get() const {
        return contents[cursor];
    }
    char get(pos_t x, pos_t y) const;
    Screen &set(char c);
    Screen &set(pos_t x, pos_t y, char ch);
    Screen &move(pos_t x, pos_t y);
    Screen &display(std::ostream &os) {
        do_display(os);
        return *this;
    }
    const Screen &display(std::ostream &os) const {
        do_display(os);
        return *this;
    }
private:
    pos_t cursor = 0;
    pos_t height = 0, width = 0;
    std::string contents;

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

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

Screen &Screen::move(pos_t x, pos_t y) {
    cursor = get_index(x, y);
    return *this;
}

Screen &Screen::set(char c) {
    contents[cursor] = c;
    return *this;
}

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

const Screen &Screen::do_display(std::ostream &os) const {
    for (int x = 0; x != width; ++x) {
        for (int y = 0; y != height; ++y)
            os << get(x, y);
        os << std::endl;
    }
    return *this;
}

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

    return 0;
}

输出:
XXXXX
XXXXX
XXXXX
XXXXX
#XXX#

但正确的输出应该是
XXXX#
XXXXX
XXXXX
XXXXX
XXXX#

我该如何修改程序?
想知道小甲鱼最近在做啥?请访问 -> 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:
class Screen {
public:
    // Existing code ...

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

    // Existing code ...
};

### 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:
pos_t get_index(pos_t x, pos_t y) const {
    if (x < width && y < height)
        return y * width + x;
    else {
        std::cerr << "Error: Invalid screen coordinates (" << x << ", " << y << ")" << std::endl;
        // Return a default or sentinel value to indicate error
        return std::string::npos;  // or any other suitable value
    }
}

### Example Usage

Here’s a simple example of how you might use the `Screen` class:
int main() {
    Screen screen(5, 5, '*');  // Create a 5x5 screen filled with '*'

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

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

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

    return 0;
}

This will output:
Screen contents:
*****
*****
**X**
*****
*****

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 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> 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;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-11-23 17:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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