读书人

帮帮忙啊该如何解决

发布时间: 2012-07-31 12:33:46 作者: rapoo

帮帮忙啊,急
01.02 23 6954.23
02.13 6 56.2
01.06 56 1258.3
01.09 21 12.3
02.18 3 2.3
要求用map来做在一个循环体内完成,输出用循环,要求是依据“\t”分成三段,如果第一段有小数点保留小数点一位,接下去比较,下面是累加的结果,输出的结果是

01.0 100 8212.53
02.1 9 58.5

[解决办法]
好了,代码如下:

C/C++ code
#include <string>#include <iostream>#include <map>#include <fstream>using namespace std;string period_processing(string str)        // 如果有小数点,则仅保留小数点后面1位{    size_t pos = str.find_first_of(".");    if(pos != string::npos)    {        str = str.substr(0, pos + 2);    }    return str;}typedef struct _aLine{    char c1[30];    int c2;    double c3;}aLine;int main(int argc, char** argv){    char row[100];                        // 用于获取文件中的一行    int pos1;                            // 每行中第一个空格的位置    int pos2;                            // 每行中第二个空格的位置    char c[20] = {0};    map<string, aLine> lineMap;    ifstream fis("E:/testfile.txt");    // 假定文件在E盘的根目录下,文件名为testfile.txt    ofstream fos("E:/result.txt");        // 将得到的结果写到另外一个文件,文件名为result.txt    if(!fis)    {        cout << "Can not open file to read..." << endl;        exit(1);    }    if(!fos)    {        cout << "Can not open file to write..." << endl;        exit(1);    }    map<string, aLine>::iterator iter;    while(fis)    {        memset(row, 0, 100);        fis.getline(row, 100);            // 从文件中读取一行        if(row[0] == 0) break;        string aline(row);        pos1 = aline.find_first_of(" ");        pos2 = aline.find_last_of(" ");        string c1 = period_processing(aline.substr(0, pos1));                    // 获取每行的第一列内容        int c2 = atoi((aline.substr(pos1 + 1, pos2 - pos1 - 1)).c_str());        // 获取每行的第二列内容        double c3 = atof((aline.substr(pos2 + 1)).c_str());                        // 获取每行的第三列内容        iter = lineMap.find(c1);        // 在map中查找是否有key为c1的        if(iter != lineMap.end())        // 如果有        {            (iter->second).c2 += c2;    // 将第二个字段相加            (iter->second).c3 += c3;    // 将第二个字段相加        }        else                            // 如果没有,则向map中插入新的aLine对象        {            aLine aline;            strcpy(aline.c1, c1.c_str());            aline.c2 = c2;            aline.c3 = c3;            lineMap.insert(pair<string, aLine>(c1, aline));        }    }    fis.close();    // 输出到屏幕和文件(可以根据需要,注释其中一种输出)    for(iter = lineMap.begin(); iter != lineMap.end(); ++iter)    {        static int j = 0;        if(j != 0)        {            cout << endl;            fos << endl;        }        memset(c, 0, 20);        sprintf(c, "%.2f", (iter->second).c3);        // 仅仅为输出数据的格式之所需        cout << iter->first << "\t" << (iter->second).c2 << "\t" << c;        // 输出到屏幕        fos << iter->first << "\t" << (iter->second).c2 << "\t" << c;        // 输出到文件        ++j;    }    fos.close();    cout << endl;    return 0;}/*// 文件E:/testfile.txt中的内容,顺序已经打乱01.011 365 14996540.1201.091 1 1937.4801.111 1 8591.2701.131 1 837.3501.141 1 4957.1101.241 1 31151.5201.314 3 194994.8901.011 1 4160.3301.092 1 2224.201.314 3 30716.3*//*// 文件E:/result.txt中的内容(屏幕输出与此相同)01.0    368    15004862.1301.1    3    14385.7301.2    1    31151.5201.3    6    225711.19*/ 

读书人网 >C++

热点推荐