使用C ++替换txt文件中的文本数字字符串..帮助我.. [英] Replace the text numbers string in a txt file using C++.. Help Me..

查看:216
本文介绍了使用C ++替换txt文件中的文本数字字符串..帮助我..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

For example:
 Before Document: 
        hai hello my daily salary is two thousand and five and your salary is five billion. my age is twenty-five.

After Document:
        hai hello my daily salary is # and your salary is #. my age is  #.

All the text numbers and i put the # symbol.. Please give me the correct code..

I am trying this code: 

< br $> b $ b



#include <iostream>
#include <fstream>
#include <string>
 
using namespace std;
 
ifstream myfile_in ("sample.txt");
ofstream myfile_out ("Lista2.txt");
string line;
 
void find_and_replace( string &source,  string find, string replace ) { 
 
    size_t j;
 
    for ( ; (j = source.find( find )) != string::npos ; ) {
        source.replace( j, find.length(), replace );
        }
 
        myfile_out << source <<endl;
        cout << source << endl;
        }
 
int main () {
 
  if (myfile_in.is_open())
  {
    int i = 0,j;
    //string strcomma ;
   // string strspace ;
 
    while (! myfile_in.eof() )
    {
 
      getline (myfile_in,line);
 
	  string strcomma= "two";
	  string strspace = "#";
	
 
      find_and_replace( line , strcomma , strspace );
  
   
      i++;
 
    }
 
    myfile_in.close();
 
  }
 
  else cout << "Unable to open file(s) ";
 
  system("PAUSE");
  return 0;
}

推荐答案

你将不得不更加努力地看待这个问题:蛮力和无知你试图使用的方法只对少数值有用 - 你有很多它们:零(和它的同义词),一,二......十九,二十,三十......一百,一千,一千,百万,等等,加上它们可能会被换行符,标点符号,和等字断开。您也应该考虑这些。



我这样做的方法是将整个输入文件解析为单词,然后将单词标记为数字 ,标点符号,连词,不是数字等等,然后处理令牌以寻找可行的数字组合。然后写出结果。

否则二十七将转换为# - #,此处没有人会将变成# - #juding will,和一千八百三十六没有便士太多了#,# - #没便士



这不是一个从粗野中受益的任务强迫方法,这里没有人会把代码递给你!
You are going to have to look at the problem a little harder: the "brute force and ignorance" approach you are trying to use is only really useful for a small number of values - you have a lot of them: zero (and it's synonyms), one, two...nineteen, twenty, thirty..ninety, hundred, thousand, million, and so forth, plus they may be interrupted by line breaks, punctuation, the word "and", and so forth. You should consider these as well.

The way I would do it is to parse the whole input file into words, and then "tokenise" the words to "number", "punctuation", "conjunction", "not a number" and so on, then process the tokens looking for viable number combinations. Then write out the result.
Otherwise "twenty-seven" will be converted to "#-#", "and no-one here will" will become "#-# hear will", and "one thousand, eight hundred and thirty-six and no pence" too at best "#, #-# no pence"

It's not a task that benefits from a brute force approach, and no-one here is going to just hand you the code to do it!


这篇关于使用C ++替换txt文件中的文本数字字符串..帮助我..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆