| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 981 人关注过本帖
标题:字符串统计
只看楼主 加入收藏
lihouqi
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2017-3-14
结帖率:100%
收藏
 问题点数:0 回复次数:4 
字符串统计
零件9.STEP.rar (4.29 KB)

    在这个文件中,数据段都是以‘#’开始,‘;’结尾。先查找每个ADVANCED_FACE行,看里面的括号里有几个‘#xxx(数字)’(比如有的行:#130 = ADVANCED_FACE ( 'NONE', ( #317 ), #213, .T. ) ;里面的括号就一个#317,而#108 = ADVANCED_FACE ( 'NONE', ( #188, #223 ), #266, .T. ) ;里面的括号有两个,#188和#233),一个的就直接定位到括号里对应的行,比如对前一个ADVANCED_FACE找到行:#317 = FACE_OUTER_BOUND ( 'NONE', #301, .T. ) ;,继续找到行:#301 = EDGE_LOOP ( 'NONE', ( #68, #27, #233, #308 ) ) ;,到EDGE_LOOP为止,此时这个EDGE_LOOP里有4个数,那么这个ADVANCED_FACE就有4个数。超过两个以上的就要分别定位到对应的行,比如找到行:#188 = FACE_OUTER_BOUND ( 'NONE', #309, .T. ) ;,继续找到行:#309 = EDGE_LOOP ( 'NONE', ( #297, #183 ) ) ;此时看EDGE_LOOP里的括号有几个#xxx(这里是两个#297和#183),记下个数2;同理找到行:#223 = FACE_BOUND ( 'NONE', #337, .T. ) ;,继续找到行:#337 = EDGE_LOOP ( 'NONE', ( #142, #29, #161, #239 ) ) ;,此时EDGE_LOOP里的括号里有4个#xxx,那么这个ADVANCED_FACE就有6个数(2+4)。 同理ADVANCED_FACE里面的括号里有两个以上的数用类似的方法。然后输出每个ADVANCED_FACE有多少个数。求高手指导 QQ:915601564.不胜感激!
搜索更多相关主题的帖子: 字符串 统计 
2017-03-16 23:48
Alien_Lee
Rank: 8Rank: 8
来 自:Linux帝国
等 级:蝙蝠侠
威 望:7
帖 子:149
专家分:739
注 册:2016-7-19
收藏
得分:0 
根本没明白你这个是什么意思。

  DEBUG的过程就是进步的过程,每一个小错误都是大问题!...
2017-03-17 11:28
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
这种题目,难点不在于代码,而在于不知道文件格式。根据已有的文件内容猜测其格式,猜测常常却是错误的

程序代码:
#include <fstream>
#include <vector>
#include <utility>
#include <string>
#include <sstream>
#include <cassert>
#include <iostream>
using namespace std;

std::vector<unsigned> parse_ADVANCED_FACE( const std::string& body );
unsigned parse_FACE_BOUND( const std::string& body );
std::vector<unsigned> parse_EDGE_LOOP( const std::string& body );

int main( void )
{
    vector< std::pair<string,string> > lines;
    {
        ifstream stepfile( "9.STEP" );
        if( !stepfile )
        {
            cerr << "Failed to open the STEP file.\n";
            return 1;
        }

        for( std::string line; getline(stepfile,line) && line!="DATA;"; );

        for( std::string line; getline(stepfile,line) && line!="ENDSEC;"; )
        {
            char numsign; unsigned idx; char eqsign; string key; string body;
            if( getline(getline(istringstream(line)>>numsign>>idx>>eqsign>>ws,key,'('),body) && numsign=='#' && idx!=0 && eqsign=='=' )
            {
                key.erase( key.find_last_not_of(" \t")+1 );
                lines.resize( max(lines.size(),idx) );
                assert( lines[idx-1].first.empty() && lines[idx-1].second.empty() );
                lines[idx-1] = make_pair(key,body);
            }
            else assert( false );
        }
    }

    for( size_t i=0; i!=lines.size(); ++i )
    {
        if( lines[i].first == "ADVANCED_FACE" )
        {
            std::vector<unsigned> edgenums;

            std::vector<unsigned> facenums = parse_ADVANCED_FACE( lines[i].second );
            for( size_t j=0; j!=facenums.size(); ++j )
            {
                size_t pos = facenums[j]-1;
                if( pos<lines.size() && (lines[pos].first=="FACE_OUTER_BOUND" || lines[pos].first=="FACE_BOUND") )
                {
                    unsigned bound_begin = parse_FACE_BOUND( lines[pos].second );
                    if( bound_begin!=0 && bound_begin-1<lines.size() && lines[bound_begin-1].first=="EDGE_LOOP" )
                    {
                        std::vector<unsigned> edgenum = parse_EDGE_LOOP( lines[bound_begin-1].second );
                        edgenums.insert( edgenums.end(), edgenum.begin(), edgenum.end() );
                    }
                    else assert( false );
                }
                else assert( false );
            }

            cout << '#' << (i+1) << '\t' << lines[i].first << '(' << edgenums.size() << "):";
            for( size_t j=0; j!=edgenums.size(); ++j )
                cout << "\t#" << edgenums[j];
            cout << '\n';
        }
    }

    return 0;
}

std::vector<unsigned> parse_ADVANCED_FACE( const std::string& body )
{
    std::vector<unsigned> idxs;
    {
        size_t a = body.find( '(' );
        size_t b = body.find( ')', a );
        if( a!=string::npos && b!=string::npos && body.substr(0,a)==" 'NONE', " )
        {
            istringstream iss( body.substr(a+1,b-a-1) );
            for( string field; getline(iss,field,','); )
            {
                char numsign; unsigned idx;
                istringstream iss(field);
                if( (iss>>ws>>numsign>>idx>>ws) && iss.eof() && idx!=0 )
                {
                    idxs.push_back( idx );
                }
                else assert( false );
            }
        }
        else assert( false );
    }

    return idxs;
}

unsigned parse_FACE_BOUND( const std::string& body )
{
    std::vector<unsigned> idxs; //  'NONE', #314, .T. ) ;
    {
        size_t a = body.find( '#' );
        size_t b = body.find( ',', a );
        if( a!=string::npos && b!=string::npos && body.substr(0,a)==" 'NONE', " )
        {
            unsigned idx;
            istringstream iss( body.substr(a+1,b-a-1) );
            if( (iss>>idx>>ws) && iss.eof() )
            {
                return idx;
            }
        }
    }

    assert( false );
    return 0;
}

std::vector<unsigned> parse_EDGE_LOOP( const std::string& body )
{
    std::vector<unsigned> idxs;
    {
        size_t a = body.find( '(' );
        size_t b = body.find( ')', a );
        if( a!=string::npos && b!=string::npos && body.substr(0,a)==" 'NONE', " )
        {
            istringstream iss( body.substr(a+1,b-a-1) );
            for( string field; getline(iss,field,','); )
            {
                char numsign; unsigned idx;
                istringstream iss(field);
                if( (iss>>ws>>numsign>>idx>>ws) && iss.eof() && idx!=0 )
                {
                    idxs.push_back( idx );
                }
                else assert( false );
            }
        }
        else assert( false );
    }

    return idxs;
}

输出:
#28     ADVANCED_FACE(4):       #56     #193    #338    #214
#77     ADVANCED_FACE(3):       #212    #187    #51
#108    ADVANCED_FACE(6):       #297    #183    #142    #29     #161    #239
#130    ADVANCED_FACE(4):       #68     #27     #233    #308
#185    ADVANCED_FACE(4):       #179    #208    #44     #303
#211    ADVANCED_FACE(4):       #78     #324    #320    #61
#235    ADVANCED_FACE(3):       #154    #281    #60
#259    ADVANCED_FACE(4):       #234    #106    #24     #153
#307    ADVANCED_FACE(4):       #71     #206    #279    #169
#329    ADVANCED_FACE(4):       #104    #312    #132    #263

2017-03-17 14:46
qdcs
Rank: 6Rank: 6
等 级:侠之大者
威 望:5
帖 子:171
专家分:458
注 册:2016-12-22
收藏
得分:0 
soliwork软件

我是硬件工程师
2017-03-17 14:59
lihouqi
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2017-3-14
收藏
得分:0 
回复 3楼 rjsp
多谢指导,大神你用的不是VC++6.0吗?老是报错。。。
图片附件: 游客没有浏览图片的权限,请 登录注册
2017-03-17 19:34
快速回复:字符串统计
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018459 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved