如何从使用替换更改为使用交换

问题描述 投票:0回答:0
#include <iostream>
    #include <fstream>
    #include <algorithm>
    #include <string>

    using namespace std;

    // global constant and variables
    const string INFILENAME = "sudokutest.txt";
    const char   PRE_NUM1   = '9';
    const char   PRE_NUM2   = '8';
    const char   PRE_NUM3   = '7';
    const char   HELP_NUM   = 'A';
    const char   POS_NUM1   = '7';
    const char   POS_NUM2   = '9';
    const char   POS_NUM3   = '8';

    char     table1[9][9];           // sudoku table start
    char     table2[9][9];           // sudoku table finish

    // fuction prototypes
    void swap(string& buf, char& value);
    void loadTable(char table[][9]);
    void printTable(char table[][9]);

    int main()
    {
        loadTable(table1);    // load tables
        cout << "      Start      " << endl;
        printTable(table1);   // display table1 
        cout << endl;
        cout << "      Finish      " << endl;
        printTable(table2);   // display table2

        return 0;
    }


    /* 1
    Function       : swap the numbers stated in constants
    Precondition   : none
    Postcondition  : table1 and table2 are load using the swaped numbers
    Comment        : using 'A' to avoide duplication of swap
    */

    //swap
    void swap(string& buf, char& value)
    {
        string s = buf;

        replace(s.begin(), s.end(), PRE_NUM1, HELP_NUM);
        replace(s.begin(), s.end(), PRE_NUM2, POS_NUM2);
        replace(s.begin(), s.end(), PRE_NUM3, POS_NUM3);
        replace(s.begin(), s.end(), HELP_NUM, POS_NUM1);

        value = s.at(0);
    }



程序按预期运行,但我想尝试使用交换方法。我试过只放在 main 里面,但它说了一些关于超载的事情。我不知道我是缺少先决条件还是做错了什么。任何帮助或评论表示赞赏。

c++
© www.soinside.com 2019 - 2024. All rights reserved.