0

我需要输入一个数据文件(如下):

        14        16        18        20        22        24        
        27        30        35        40        45        50

        8         10        11        12        13        14        
        16        18        21        24        27        30

        0.4       0.5       0.5       0.5       0.5       0.5        
        0.6       0.6       0.6       0.6       0.6       0.6

        16        17        18        19        20        21        
        22        23        25        26        27        29

        2.0       2.2       2.2       2.3       2.4       2.5        
        2.6       2.7       2.8       2.9       3.1       3.2

        1.7       1.8       2.0       2.2       2.4       2.5        
        2.8       3.0       3.4       3.8       3.8       3.8

在选择第一列时,将输出这些阀门。

这是我的代码;谁能看到任何错误?

void TimberType() {

    ifstream inFile;
    inFile.open("TableOfValues.txt", ios::in );
    string TimberValues;
    inFile >> TimberValues;
    inFile >> bendStr >> tensPara >> tensPerp >> compPara >> compPerp >> shear >> mElasModPara >> elasMod5Para >> mElasModPerp >> mShearMod >> density >> mDensity;


    cout << "Please enter timber type " << endl;
    cout << "C14 = 1        C16 = 2        C18 = 3        C20 = 4" << endl;
    cout << "C22 = 5        C24 = 6        C27 = 7        C30 = 8" << endl;
    cout << "C35 = 9        C40 = 10    C45 = 11    C50 = 12" << endl;
    cin >> timberType;

    {
        switch (timberType) {
        case 1:
            timberType = 1;
            cout << "You have selected timber type C14" << endl;
            cout << TimberValues[1];
            break;
        case 2:
            timberType = 2;
            cout << "You have selected timber type C16" << endl;
            cout << TimberValues[2];
            break;
        case 3:
            timberType = 3;
            cout << "You have selected timber type C18" << endl;
            cout << TimberValues[3];
            break;
        case 4:
            timberType = 4;
            cout << "You have selected timber type C20" << endl;
            cout << TimberValues[4];
            break;
        case 5:
            timberType = 5;
            cout << "You have selected timber type C22" << endl;
            cout << TimberValues[5];
            break;
        case 6:
            timberType = 6;
            cout << "You have selected timber type C24" << endl;
            cout << TimberValues[6];
            break;
        case 7:
            timberType = 7;
            cout << "You have selected timber type C27" << endl;
            cout << TimberValues[7];
            break;
        case 8:
            timberType = 8;
            cout << "You have selected timber type C30" << endl;
            cout << TimberValues[8];
            break;
        case 9:
            timberType = 9;
            cout << "You have selected timber type C35" << endl;
            cout << TimberValues[9];
            break;
        case 10:
            timberType = 10;
            cout << "You have selected timber type C40" << endl;
            cout << TimberValues[10];
            break;
        case 11:
            timberType = 11;
            cout << "You have selected timber type C45" << endl;
            cout << TimberValues[11];
            break;
        case 12:
            timberType = 12;
            cout << "You have selected timber type C50" << endl;
            cout << TimberValues[12];
            break;

        }

        cout << "Bending Strength: " << bendStr << "N/mm^2" << endl;
        cout << "Tension in Parallel: " << tensPara << "N/mm^2" << endl;
        cout << "Tension in Perpendicular: " << tensPerp << "N/mm^2" << endl;
        cout << "Compression in Parallel: " << compPara << "N/mm^2" << endl;
        cout << "Compression in Perpendicular: " << compPerp << "N/mm^2" << endl;
        cout << "Shear: " << shear << "N/mm^2" << endl;

    }
}

目前,我只收到 0 值的弯曲强度、平行张力、垂直张力、平行压缩、垂直压缩和剪切。

任何想法将不胜感激。谢谢你。

4

3 回答 3

0

为简单起见,您可以使用数组将值读入 const int ROW = 12, COL = 6; 整数表[行][COL] = {}; istream 文件("TablesOfValues.txt");

for(int i = 0; i < ROW && file; ++i){
  for(int j = 0; j < COL && file; ++j){
        file >> table[i][j];
  }
}

//now tables[0][0] = 14
//now tables[1][2] = 35
//added a slash to first comment
于 2012-12-09T17:52:26.073 回答
0
       #include <iostream>
        #include <array>

        string elem[12][7] = {
        //First column is the row number (assuming they're related to timber so starting at 1)
        // Please note these are strings
        { "1",   "14",  "16",   "18",  "20",  "22",  "24"  }
        { "2",   "27",  "30",   "35",  "40",  "45",  "50"  }
        { "3",   "8",   "10",   "11",  "12",  "13",  "14"  }
        { "4",   "16",  "18",   "21",  "24",  "27",  "30"  }
        { "5",   "0.4", "0.5",  "0.5", "0.5", "0.5", "0.5" }
        { "6",   "0.6", "0.6",  "0.6", "0.6", "0.6", "0.6" }
        { "7",   "16",  "17",   "18",  "19",  "20",  "21"  }
        { "8",   "22",  "23",   "25",  "26",  "27",  "29"  }
        { "9",   "2.0", "2.2",  "2.2", "2.3", "2.4", "2.5" }
        { "10",  "2.6", "2.7",  "2.8", "2.9", "3.1", "3.2" }
        { "11",  "1.7", "1.8",  "2.0", "2.2", "2.4", "2.5" }
        { "12",  "2.8", "3.0",  "3.4", "3.8", "3.8", "3.8" }
        }

     void TimberType ()
        {
            ifstream inFile;
            inFile.open ("TableOfValues.txt", ios :: in);
            string TimberValues;
            inFile>>TimberValues;
            inFile>>bendStr>>tensPara>>tensPerp>>compPara>>compPerp>>shear>>mElasModPara>>elasMod5Para>>mElasModPerp>>mShearMod>>density>>mDensity;


            cout << "Please enter timber type " << endl;
            cout << "C14 = 1        C16 = 2     C18 = 3     C20 = 4" << endl;
            cout << "C22 = 5        C24 = 6     C27 = 7     C30 = 8" << endl;
            cout << "C35 = 9        C40 = 10    C45 = 11    C50 = 12" << endl;
            cin >> timberType;  

        {
            switch (timberType)
            {
            case 1:
                timberType = 1;
                cout << "You have selected timber type C14" << endl;
                cout << TimberValues [1];
                break;
            case 2:
                timberType = 2;
                cout << "You have selected timber type C16" << endl;
                cout << TimberValues [2];
                break;
            case 3: 
                timberType = 3; 
                cout << "You have selected timber type C18" << endl;
                cout << TimberValues [3];
                break;
            case 4:
                timberType = 4;
                cout << "You have selected timber type C20" << endl;
                cout << TimberValues [4];
                break;
            case 5:
                timberType = 5;
                cout << "You have selected timber type C22" << endl;
                cout << TimberValues [5];
                break;
            case 6:
                timberType = 6; 
                cout << "You have selected timber type C24" << endl;
                cout << TimberValues [6];
                break;
            case 7:
                timberType = 7;
                cout << "You have selected timber type C27" << endl;
                cout << TimberValues [7];
                break;
            case 8:
                timberType = 8;
                cout << "You have selected timber type C30" << endl;
                cout << TimberValues [8];
                break;
            case 9:
                timberType = 9; 
                cout << "You have selected timber type C35" << endl;
                cout << TimberValues [9];
                break;
            case 10:
                timberType = 10;
                cout << "You have selected timber type C40" << endl;
                cout << TimberValues [10];
                break;
            case 11:
                timberType = 11;
                cout << "You have selected timber type C45" << endl;
                cout << TimberValues [11];
                break;
            case 12:
                timberType = 12; 
                cout << "You have selected timber type C50" << endl;
                cout << TimberValues [12];
                break;

            }

// This is the code you need to edit to search the array

void searchString(string A[],int size, string target)
{
    int j;
    {
    for(j=0; j < size; j++)
        if(A[j] == target)
            cout << A[j] << endl;
        else cout << "Not Found";
    }
}


            cout << "Bending Strength: "<< bendStr << "N/mm^2" << endl;
            cout << "Tension in Parallel: "<< tensPara << "N/mm^2" << endl;
            cout << "Tension in Perpendicular: "<< tensPerp << "N/mm^2" <<endl;
            cout << "Compression in Parallel: "<< compPara << "N/mm^2" << endl;
            cout << "Compression in Perpendicular: "<< compPerp << "N/mm^2" << endl;
            cout << "Shear: "<< shear << "N/mm^2" << endl;

        }
        }

I believe there is a simple search command that will let you reference the timber numbers based on their number. Please post the final code here because I'm actually working on something like this and would love to know how it worked for you.
于 2012-12-09T17:55:31.177 回答
0

以下是一些建议:
替换switch为表查找:

struct Timber_Text_Entry
{
    unsigned int index;
    char const   text;
};

const Timber_Text_Entry Timber_Type_Names[] =
{
    { 1, "C14"}, { 2, "C16"}, {3, "C18"}, {4, "C20"},
//...
};
const unsigned int NUMBER_OF_TIMBER_TYPES =
    sizeof(Timber_Type_Names) / sizeof(Timber_Type_Names[0]);

// Printing the menu contents:
for (unsigned int i = 0; i < NUMBER_OF_TIMBER_TYPES; ++i)
{
  cout << Timber_Type_Names[i].text
       << " = "
       << Timber_Type_Names[i].index;
  if ((i % 4) == 3) cout << "\n";
}

// The switch statement replaced:
if ((timberType > 1) && (timberType <= NUMBER_OF_TIMBER_TYPES))
{
    // This should search for the index == timberType instead
    // of using timberType as an index into the array.
    // The code below assumes the order of the entries will
    // not change.
    // The "-1" is because timberTypes start at 1 and the
    // array indices start at 0.
    cout << "You have selected timber type ";
    cout << Timber_Type_Names[timberType - 1].text;
    cout << endl;
}
else
{
    cout << "Invalid timber selection: " << timberType << endl;
}
于 2012-12-09T19:59:14.783 回答