[visual-studio]简单的数组协调匹配的显示

标签: Visual-Studio
发布时间: 2014/3/1 3:43:41
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我正试图创建一个数组,接受用户输入,打破在 99 99 但多维数组不会停止考虑输入的值。另一个问题获取的值以显示上下来下面的网格中的可视化表示形式。如果需要更多的说明,请让我知道。

enter image description here

#include <iostream>
#include <ctime>

using namespace std;

int main()
{

srand(time(0));

int organisms[22][22];
int rows,
    columns;
int input[22][22];
int *output = new int[];

//filling array

for(int i=0;i<22;i++)
{

    for(int j=0;j<22;j++)
    {
        organisms[i][j]=0;
    }
}


//column output

cout<<"\t\t\t\t\t\t\t\t\t\t\t\t\tColumn\n\n";
for(columns=1;columns<21;columns++)
{
    cout<<"\t"<<columns;
}
cout<<"\n\n\n";

//Row and data output

    for(int i=0;i<20;i++)
    {

        cout<<"Row  "<<i+1<<"\t";

        for(int j=0;j<20;j++)
        {
            cout<<organisms[i][j]<<"\t";
        }
        cout<<endl;
    }
cout<<"\n\n\n";
cout<<"Enter each cell in first colony use row space column Enter format, 3 4, for example.\nEnter 99 99 to end entries.\n";

//input of values

cin.ignore(4);

int m=0;


do
{

    cin>>input[m][m];
    m++;

    if(input[m][m]!=99,99)
        break;

}while(m<20);



/*
while(input[m][m]!=99,99)
{
    cin>>input[m][m];
    m++;

}
 /*
for(int m=0;m<INT_MAX;m++)
{
    cin>>input[m][m];

    if(input[m][m]==99)
        break;
}
*/


//input array indexing






//output array

cout<<"\n\n\n";
cout<<"\t\t\t\t\t\t\t\t\t\t\t\t\tColumn\n\n";
for(columns=1;columns<21;columns++)
{
    cout<<"\t"<<columns;
}
cout<<"\n\n\n";

        for(int i=0;i<20;i++)
        {

        cout<<"Row  "<<i+1<<"\t";
        cout<<endl;

        for(int n=0;n<20;n++)
        {
            cout<<input[i][n]<<"\t";
        }

        cout<<endl;
}

cin.get();
cin.get();

return 0;
}

解决方法 1:

例如)

#include <iostream>
#include <iomanip>
#include <utility>
#include <vector>
#include <iterator>

using namespace std;

const size_t size=20;

char table[size][size];

void table_clear(void){
    for(int v = 0; v<size; ++v)
        for(int h = 0; h<size; ++h)
            table[v][h]=' ';
}

void table_put(pair<int, int> p){
    int v = p.first;
    int h = p.second;
    table[v-1][h-1] = '*';//to zero origin
}

void table_disp(){
    cout << "   ";
    for(int i=0; i<size;++i){
        cout << setw(3) << i + 1;
    }
    cout << "\n" << endl;
    for(int i=0;i<size;++i){
        cout << setw(3) << left << i + 1;
        for(int j=0;j<size;++j){
            cout << setw(3) << right << table[i][j];
        }
        cout << endl;
    }
}

int main(){
    vector<pair<int,int> > v;
    pair<int, int> end(99,99);
    pair<int, int> rc;

    cout << "Enter each cell in first colony use row space column Enter format, 3 4, for example.\n";
    cout << "Enter 99 99 to end entries." << endl;
    while(true){
        int row, column;
        cin >> row;
        cin >> column;
        rc = make_pair(row, column);
        if(rc == end)
            break;
        v.push_back(rc);
    }

    table_clear();
    for(vector<pair<int,int> >::const_iterator iter = v.begin();iter != v.end(); ++iter ) {
        table_put(*iter);
    }
    table_disp();
}
官方微信
官方QQ群
31647020