第一篇,书上的fortran用c++实现

a.cpp
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
int ne, nj, njt;
vector<int*> jh; // 2
vector<int*> jtx; // 4
vector<int*> jw; // 3
vector<int*> mw; // 6
void qjw()
{
    int counter = 1;
    for (int i = 0; i < nj; ++i)
    {
        int *toAdd = new int(3);
        int jtxIndex = -1; // search from jtx
        for (int j = 0; j < njt; ++j)
        {
            if (jtx[j][0] == i + 1)
            {
                jtxIndex = j;
                break;
            }
        }
        for (int k = 0; k < 3; ++k)
        {
            if (jtxIndex == -1)
            {
                toAdd[k] = counter++;
                continue;
            }
            int jtxValue = jtx[jtxIndex][k + 1];
            switch (jtxValue)
            {
            case 10001:
                toAdd[k] = -1;
                break;
            case 0:
                toAdd[k] = counter++;
                break;
            case 1:
                toAdd[k] = 0;
                break;
            case 1001:
                jtxValue = 1;
            default:
                // best to buffer this, and do this after loop..
                toAdd[k] = jw[jtxValue - 1][k];
                break;
            }
        }
        jw.push_back(toAdd);
    }
}
void qmw()
{
    for (int i = 0; i < ne; ++i)
    {
        int *toAdd = new int(6);
        int j1 = jh[i][0] - 1;
        int *jj1 = jw[j1];
        for (int j = 0; j < 3; ++j)
            toAdd[j] = jj1[j];
        int j2 = jh[i][1] - 1;
        int *jj2 = jw[j2];
        for (int j = 3; j < 6; ++j)
            toAdd[j] = jj2[j - 3];
        mw.push_back(toAdd);
    }
}
int main(int argc, char **argv)
{
    if (argc < 2)
        return -1;
    ifstream ifs(argv[1]);
    if (!ifs.is_open())
    {
        cout << "can not open file: " << argv[1] << endl;
        return -1;
    }
    ifs >> ne >> nj >> njt;
    for (int i = 0; i < ne; ++i)
    {
        int *e = new int(2);
        ifs >> e[0] >> e[1];
        jh.push_back(e);
    }
    for (int i = 0; i < njt; ++i)
    {
        int *jt = new int(4);
        ifs >> jt[0] >> jt[1] >> jt[2] >> jt[3];
        jtx.push_back(jt);
    }
    qjw();
    qmw();
    cout << "finding the mw(6) of elements" << endl;
    for (int i = 0; i < ne; ++i)
    {
        cout << "element no." << (i + 1) << endl;
        int *m = mw[i];
        cout << "mw = ";
        for (int j = 0; j < 6; ++j)
            cout << m[j] << " ";
        
        cout << endl;
    }
    return 0;
}
e.dat
8 10 10
9 2 9 4 10 6 10 8 1 3 3 5 5 7 9 10
1 1 1 0 2 1 1 10001 3 10001 0 0 4 3 3 10001 5 10001 0 0 6 5 5 10001 7 10001 1 0 8 7 1 10001 9 0 0 10001 10 0 0 10001
test & output
g++ a.cpp -o a
./a e.dat
finding the mw(6) of elements
element no.1
mw = 7 8 -1 0 0 -1 
element no.2
mw = 7 8 -1 -1 2 -1 
element no.3
mw = 9 10 -1 -1 4 -1 
element no.4
mw = 9 10 -1 -1 0 -1 
element no.5
mw = 0 0 1 -1 2 3 
element no.6
mw = -1 2 3 -1 4 5 
element no.7
mw = -1 4 5 -1 0 6 
element no.8
mw = 7 8 -1 9 10 -1

原文链接: https://www.cnblogs.com/hylent/archive/2011/11/02/2233732.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    第一篇,书上的fortran用c++实现

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/35532

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月8日 下午12:24
下一篇 2023年2月8日 下午12:24

相关推荐