3

代码:

#include <iostream>
#include <iomanip>
using namespace std;

class Ascii_output {
public:
    void run() {
        print_ascii();
    }
private:
    void print_ascii() {
        int i, j;                                                           // i is         used to print the first element of each row
                                                                        // j is used to print subsequent columns of a given row
    char ch;                                                            // ch stores the character which is to be printed
    cout << left;

    for (i = 32; i < 64; i++) {                                         // 33 rows are printed out (64-32+1)
        ch = i;
        if (ch != '\n')                                                 // replaces any newline printouts with a blank character
            cout << setw(3) << i << " " << setw(6) << ch;
        else
            cout << setw(3) << i << " " << setw(6);

        for (j = 1; j < 7; j++) {                                       // decides the amount of columns to be printed out, "j < 7" dictates this
            ch += 32*j;                                                 // offsets the column by a multiple of 32
            if (ch != '\n')                                             // replaces any newline printouts with a blank character
                cout << setw(3) << i+(32*j) << " " << setw(6) << ch;
            else
                cout << setw(3) << i+(32*j) << " " << setw(6);
        }

        cout << endl;
    }
    }
};

输出: 在此处输入图像描述

为什么在值 96 - 255 处我没有得到正确缩进的输出和奇怪的字符?

4

2 回答 2

6

这条线没有做正确的事情:

ch += 32*j;

你想数 32,要么

ch += 32;

或者

ch = i + 32*j;

我强烈建议在输出期间使数字和字符值匹配。所以改变

cout << setw(3) << i+(32*j) << " " << setw(6) << ch;

cout << setw(3) << int(ch) << " " << setw(6) << ch;
于 2012-11-06T14:20:53.760 回答
0

127 以上的字符不是标准 od ASCII 的一部分。Windows 中出现的 127 个以上字符取决于所选字体。

http://msdn.microsoft.com/en-us/library/4z4t9ed1%28v=vs.71%29.aspx

于 2012-11-06T14:09:42.913 回答