怎么有时候全错,有时候对一个,有时候对两个,有没有大佬试试这段代码

P5730 【深基5.例10】显示屏

mtr329 @ 2020-06-30 16:08:56

#include<iostream>
#include<string>
#include<iomanip>
#include<vector>
#include<iterator>
using namespace std;

void print(char(&screen)[5][500], int num, int &col) {
    switch (num) {
        case 0: {
            for (int i = 0; i < 5; i++) {
                screen[i][col] = 'X';
            }
            for (int i = 0; i < 5; i++) {
                screen[i][col + 1] = '.';
            }
            screen[0][col+1] = screen[4][col + 1] = 'X';
            for (int i = 0; i < 5; i++) {
                screen[i][col + 2] = 'X';
            }

            col = col + 3;
            break;
        }
        case 1: {
            for (int i = 0; i < 5; i++) {
                screen[i][col] = '.';
            }
            for (int i = 0; i < 5; i++) {
                screen[i][col + 1] = '.';
            }
            for (int i = 0; i < 5; i++) {
                screen[i][col+2] = 'X';
            }
            col = col + 3;
            break;
        }
        case 2: {
            for (int i = 0; i < 5; i++) {
                screen[i][col] = 'X';
            }

            for (int i = 0; i < 5; i++) {
                screen[i][col+1] = 'X';
            }

            for (int i = 0; i < 5; i++) {
                screen[i][col+2] = 'X';
            }
            screen[1][col] = screen[1][col + 1] = screen[3][col + 1] = screen[3][col + 2] = '.';
            col = col + 3;
            break;
        }
        case 3: {
            for (int i = 0; i < 5; i++) {
                screen[i][col] = 'X';
            }

            for (int i = 0; i < 5; i++) {
                screen[i][col + 1] = 'X';
            }

            for (int i = 0; i < 5; i++) {
                screen[i][col + 2] = 'X';
            }
            screen[1][col] = screen[1][col + 1] = screen[3][col + 1] = screen[3][col] = '.';
            col = col + 3;
            break;
        }
        case 4: {
            for (int i = 0; i < 5; i++) {
                screen[i][col] = 'X';
            }

            for (int i = 0; i < 5; i++) {
                screen[i][col + 1] = '.';
            }

            for (int i = 0; i < 5; i++) {
                screen[i][col + 2] = 'X';
            }
            screen[2][col + 1] = 'X';
            screen[3][col] = screen[4][col] = '.';
            col = col + 3;
            break;
        }
        case 5: {
            for (int i = 0; i < 5; i++) {
                screen[i][col] = 'X';
            }

            for (int i = 0; i < 5; i++) {
                screen[i][col + 1] = 'X';
            }

            for (int i = 0; i < 5; i++) {
                screen[i][col + 2] = 'X';
            }
            screen[1][col+2] = screen[1][col + 1] = screen[3][col + 1] = screen[3][col] = '.';
            col = col + 3;
            break;
        }
        case 6: {
            for (int i = 0; i < 5; i++) {
                screen[i][col] = 'X';
            }

            for (int i = 0; i < 5; i++) {
                screen[i][col + 1] = 'X';
            }

            for (int i = 0; i < 5; i++) {
                screen[i][col + 2] = 'X';
            }
            screen[1][col + 1] = screen[1][col + 2] = screen[3][col + 1] = '.';
            col = col + 3;
            break;
        }
        case 7: {
            for (int i = 0; i < 5; i++) {
                screen[i][col] = '.';
            }

            for (int i = 0; i < 5; i++) {
                screen[i][col + 1] = '.';
            }

            for (int i = 0; i < 5; i++) {
                screen[i][col + 2] = 'X';
            }
            screen[0][col] = screen[0][col + 1] = 'X';
            col = col + 3;
            break;
        }
        case 8: {
            for (int i = 0; i < 5; i++) {
                screen[i][col] = 'X';
            }

            for (int i = 0; i < 5; i++) {
                screen[i][col + 1] = 'X';
            }

            for (int i = 0; i < 5; i++) {
                screen[i][col + 2] = 'X';
            }
            screen[1][col + 1] = screen[3][col + 1] = '.';
            col = col + 3;
            break;
        }
        case 9: {
            for (int i = 0; i < 5; i++) {
                screen[i][col] = 'X';
            }

            for (int i = 0; i < 5; i++) {
                screen[i][col + 1] = 'X';
            }

            for (int i = 0; i < 5; i++) {
                screen[i][col + 2] = 'X';
            }
            screen[1][col + 1] = screen[3][col] = screen[3][col + 1] = '.';
            col = col + 3;
            break;
        }
        default:{
            for (int i = 0; i < 5; i++) {
                screen[i][col] = '.';
            }
            col = col + 1;
            break;
        }
    }
}

int test01(const string &s, char (&screen)[5][500]) {
    int i = 0, col = 0;
    for (i = 0; i < s.size() -1; i++) {
        int t = s[i] - '0';
        print(screen, t, col);
        print(screen, -1, col);
    }
    int t = s[i] - '0';
    print(screen,t , col);
    return col;
}

int main(){
    char screen[5][500];
    string s;
    int weishu;
    cin >> weishu;
    cin >> s;
    if (s[s.size() - 1] == '\r') {
        s.pop_back();
    }
    int l = test01(s, screen);
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j <= l; j++) {
            cout << screen[i][j];
        }
        cout << endl;
    }

    return 0;
}

by tangrunxi @ 2020-06-30 16:43:10

@mtr329 还有时候对三个


|