當前位置:編程學習大全網 - 編程語言 - 求 C++高手解答 壹條編程題,求原代碼. 懸賞20點

求 C++高手解答 壹條編程題,求原代碼. 懸賞20點

現作的, 有bug的話站內短信我

要處理的文件必須先改成英文名( 只含ASCII的名字 )

fstream不能直接打開中文名文件, 而setlcale和locale::global也會造成不能顯示或報錯( 網上的方法和我自己嘗試都不行 ), 我也不想用c或api來代替, 只能湊合壹下了; 或者妳自己改下代碼, 用其他文件打開方式

#include <fstream>

#include <iostream>

#include <string>

#include <ctype.h>

using namespace std;

class FileWordReplacer

{

protected:

static FileWordReplacer *singleInstance;

ifstream fileIn;

ofstream fileOut;

FileWordReplacer(){}

string& StringReplace( string &szSentence, string wordOld, string wordNew ) const;

string& StringReplaceEx( string &szSentence, string wordOld, string wordNew ) const;

string& StringToUper( string &szSource ) const;

public:

static FileWordReplacer* GetInstance()

{

if( NULL == singleInstance )

return singleInstance = new FileWordReplacer;

return singleInstance;

}

bool FileReplace( string szFileName, string wordOld, string wordNew, bool bDiv );

};

bool FileWordReplacer::FileReplace( string szFileName, string wordOld, string wordNew, bool bDiv )

{

fileIn.rdbuf()->open( szFileName.c_str(), ios::in );

if( !fileIn.is_open() )

{

cerr << "File does'nt exist." << endl;

return false;

}

fileOut.rdbuf()->open( (szFileName "_").c_str(), ios::out );

string szRead;

while( getline( fileIn, szRead ) )

fileOut

<< (bDiv ? StringReplace( szRead, wordOld, wordNew ) : StringReplaceEx( szRead, wordOld, wordNew ) )

<< endl;

fileIn.rdbuf()->close();

fileOut.rdbuf()->close();

string szCmd = "del " szFileName;

system( szCmd.c_str() );

szCmd = "ren " szFileName "_ ";

size_t nPos = szFileName.find_last_of('\\') 1;

szCmd = szFileName.substr( nPos, szFileName.size() - nPos );

system( szCmd.c_str() );

return true;

}

string& FileWordReplacer::StringReplace( string &szSentence, string wordOld, string wordNew ) const

{

size_t nPos = 0;

while( string::npos != (nPos = szSentence.find( wordOld, nPos)) )

{

szSentence.replace( nPos, wordOld.size(), wordNew );

nPos = wordNew.size();

}

return szSentence;

}

string& FileWordReplacer::StringReplaceEx( string &szSentence, string wordOld, string wordNew ) const

{

string szUper = szSentence;

StringToUper( szUper );

string wordUper = wordOld;

StringToUper( wordUper );

const size_t nDef = wordNew.size() - wordOld.size();

size_t nPos = 0;

int nTimes = 0;

while( string::npos != (nPos = szUper.find( wordUper, nPos)) )

{

szSentence.replace( nPos nDef * nTimes, wordOld.size(), wordNew );

nPos = wordUper.size();

nTimes ;

}

return szSentence;

}

string& FileWordReplacer::StringToUper( string &szSource ) const

{

string::iterator it = szSource.begin();

while( szSource.end() != it )

{

if( islower( *it ) )

*it = toupper( *it );

it ;

}

return szSource;

}

FileWordReplacer* FileWordReplacer::singleInstance = NULL;

int main()

{

string szFileName;

cout << "輸入文件名 : (可以是絕對路徑或相對路徑)" << endl;

cin >> szFileName;

string wordOld, wordNew;

cout << "輸入想要替換的單詞 :" << endl;

cin >> wordOld;

cout << "輸入新的單詞:" << endl;

cin >> wordNew;

bool bDiv;

while( true )

{

cout << "區分大小寫? (Y/N)" << endl;

char c;

cin >> c;

if( 'y' == c || 'Y' == c )

{

bDiv = true;

break;

}

else if( 'n' == c || 'N' == c )

{

bDiv = false;

break;

}

}

if ( FileWordReplacer::GetInstance()->FileReplace( szFileName, wordOld, wordNew, bDiv ) )

cout << "\n替換成功" << endl;

return 0;

}

c語言的, 這個可以用中文文件名

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <malloc.h>

#define TRUE 1

#define FALSE 0

#define NULL 0

#define EOF (-1)

#define BUFFER_SIZE 4 * 1024 //當文件單行超過1k字節時, 增加該值, 是 4 * 最大行長

typedef int BOOL;

typedef struct tagWORD

{

char szValue[256];

int nLength;

}WORD, *PWORD;

typedef struct tagSENTENCE

{

char *szValue;

int nLength;

}SENTENCE, *PSENTENCE;

// 從用戶處獲取文件名

void GetFileName( char *szFileName );

// 從用戶處獲取新舊兩句單詞(短語)

void GetPairWord( PWORD pWordOld, PWORD pWordNew );

// 從用戶處咨詢是否區分大小寫

BOOL IsDiv();

// 將指定文件中的舊單詞替換為新單詞, 以bDiv為是否區分大小寫的標誌

void FileWordReplace( const char *szFileName, PWORD pWordOld, PWORD pWordNew, BOOL bDiv );

int main()

{

char szFileName[64];

WORD wordOld, wordNew;

BOOL bDiv;

//用戶輸入文件名

GetFileName( szFileName );

//用戶輸入新舊單詞

GetPairWord( &wordOld, &wordNew );

//詢問用戶是否區分大小寫

bDiv = IsDiv();

//替換文件內容

FileWordReplace( szFileName, &wordOld, &wordNew, bDiv );

return 0;

}

//從文件中讀取壹行句子

int ReadSentence( FILE *pFile, PSENTENCE pSentence )

{

char szFormat[20] = {0};

szFormat[0] = '%';

sprintf( szFormat 1, "%d[^\n]", BUFFER_SIZE / 4 - 1 );

memset( pSentence->szValue, 0, BUFFER_SIZE );

fscanf( pFile, szFormat, pSentence->szValue );

pSentence->nLength = (int)strlen( pSentence->szValue );

return fgetc( pFile ) * !pSentence->nLength;

}

//將壹條句子寫進文件中

int WriteSentence( FILE *pFile, PSENTENCE pSentence )

{

int nWrited;

nWrited = (int)fwrite( pSentence->szValue, sizeof(char), pSentence->nLength, pFile );

fputc( '\n', pFile );

return nWrited 1;

}

//判斷壹個字母是否是小寫

BOOL IsLower( char c )

{

return c >= 'a' && c <= 'z' ;

}

//將壹個字符轉化為大寫

char ToUper( char c )

{

return c - 'a' 'A';

}

//用壹條壹般句子生成壹條不含小寫字母的句子

char* MakeUper( const char *szSource, const int nLength )

{

char *szTemp = (char*)malloc( sizeof(char) * BUFFER_SIZE );

int i;

for( i=0; i<nLength; i )

szTemp[i] = IsLower( szSource[i] ) ? ToUper( szSource[i] ) : szSource[i];

szTemp[i] = 0;

return szTemp;

}

//將句子中的單詞(短語)替換成新單詞, 區分大小寫

int StringWordReplace( PSENTENCE pSentence, PWORD pWordOld, PWORD pWordNew )

{

char *szTemp = (char*)malloc( sizeof(char) * BUFFER_SIZE );

int nPos = 0, nTimes = 0;

const int nDif = pWordNew->nLength - pWordOld->nLength;

while( nPos <= pSentence->nLength - pWordOld->nLength )

{

if( !strncmp( pSentence->szValue nPos, pWordOld->szValue, pWordOld->nLength ) )

{

strcpy( szTemp nPos nTimes * nDif, pWordNew->szValue );

nPos = pWordOld->nLength;

nTimes ;

}

else

{

szTemp[nPos nTimes * nDif] = pSentence->szValue[nPos];

nPos ;

}

}

strcpy( szTemp nPos nTimes * nDif, pSentence->szValue nPos );

pSentence->nLength = nDif * nTimes;

free( pSentence->szValue );

pSentence->szValue = szTemp;

return nTimes;

}

//將句子中的單詞(短語)替換成新單詞, 不區分大小寫

int StringWordReplaceEx( PSENTENCE pSentence, PWORD pWordOld, PWORD pWordNew )

{

char *szTemp = (char*)malloc( sizeof(char) * BUFFER_SIZE );

char *szUperSentence = MakeUper( pSentence->szValue, pSentence->nLength );

char *szUperWord = MakeUper( pWordOld->szValue, pWordOld->nLength );

int nPos = 0, nTimes = 0;

const int nDif = pWordNew->nLength - pWordOld->nLength;

while( nPos <= pSentence->nLength - pWordOld->nLength )

{

if( !strncmp( szUperSentence nPos, szUperWord, pWordOld->nLength ) )

{

strcpy( szTemp nPos nTimes * nDif, pWordNew->szValue );

nPos = pWordOld->nLength;

nTimes ;

}

else

{

szTemp[nPos nTimes * nDif] = pSentence->szValue[nPos];

nPos ;

}

}

strcpy( szTemp nPos nTimes * nDif, pSentence->szValue nPos );

free( szUperSentence );

free( szUperWord );

pSentence->nLength = nDif * nTimes;

free( pSentence->szValue );

pSentence->szValue = szTemp;

return nTimes;

}

//從用戶那裏獲取壹個單詞

void GetWord( PWORD pWord )

{

memset( pWord->szValue, 0, 256 );

puts("輸入單詞(可以是中間有空格的短語), 但不能超過255個字符");

scanf("%5[^\n]", pWord->szValue );

fflush( stdin );

pWord->nLength = strlen( pWord->szValue );

}

//將舊文件刪除, 將新文件改成舊文件的名字

//因為文件中單詞替換後文件長度可能改變, 所以只能生成新文件再刪除舊的

void DelAndReNameFile( const char *szFileName, const char *szTempFileName )

{

char szCmd[128] = {0};

const char *pCTemp;

strcpy( szCmd, "del ");

strcpy( szCmd 4, szFileName );

system( szCmd );

memset( szCmd, 0, 128 );

strcpy( szCmd, "ren ");

strcpy( szCmd 4, szTempFileName );

szCmd[strlen(szCmd)] = ' ';

pCTemp = strrchr( szFileName, '\\' );

strcpy( szCmd strlen(szCmd), pCTemp ? (pCTemp 1) : szFileName );

system( szCmd );

}

//在舊文件相同的路徑下給新文件取名, 文件名是某單詞的md5碼

void GetTempFileName( const char *szFileName, char *szTempFileName )

{

const char *pCTemp;

pCTemp = strrchr( szFileName, '\\' );

if( pCTemp )

strncpy( szTempFileName, szFileName, pCTemp - szFileName 1 );

strcpy( szTempFileName strlen(szTempFileName), "22EFDBE132EABC102306BD7A334FB434" );

}

//以下幾個函數的說明在聲明處

void FileWordReplace( const char *szFileName, PWORD pWordOld, PWORD pWordNew, BOOL bDiv )

{

FILE *pFileS, *pFileD;

char szTempFileName[128] = {0};

SENTENCE sentence;

GetTempFileName( szFileName, szTempFileName );

sentence.szValue = (char*)malloc( sizeof(char) * BUFFER_SIZE );

pFileS = fopen( szFileName, "r" );

pFileD = fopen( szTempFileName, "w" );

while( EOF != ReadSentence( pFileS, &sentence ) )

{

if( bDiv )

StringWordReplace( &sentence, pWordOld, pWordNew );

else

StringWordReplaceEx( &sentence, pWordOld, pWordNew );

WriteSentence( pFileD, &sentence );

}

fclose( pFileS );

fclose( pFileD );

free( sentence.szValue );

DelAndReNameFile( szFileName, szTempFileName );

}

void GetPairWord( PWORD pWordOld, PWORD pWordNew )

{

puts("輸入要替換的單詞 : ");

GetWord( pWordOld );

puts("輸入新的單詞 : ");

GetWord( pWordNew );

}

BOOL IsDiv()

{

char cInput;

while( TRUE )

{

puts("區分大小寫嗎? (Y/N)");

cInput = getchar();

if( 'y' == cInput || 'Y' == cInput )

return TRUE;

else if( 'n' == cInput || 'N' == cInput )

return FALSE;

}

}

void GetFileName( char *szFileName )

{

FILE *pFile;

szFileName[63] = 0;

while( TRUE )

{

puts("輸入文件名");

if ( 1 == scanf( "cs", szFileName ) )

{

fflush( stdin );

if( pFile = fopen( szFileName, "r" ) )

{

fclose( pFile );

break;

}

else

puts("找不到文件");

}

}

}

最後壹種方法, 去除了校驗等...

#include <stdio.h>

#include <string.h>

// 緩沖區, 用來放讀取的內容

#define BUFFER_SIZE 1024

// 部分大小寫的字符串比較

int MyCmp( const char *sz1, const char *sz2, int nLen )

{

char cMax, cMin;

for( ;nLen--; sz1 , sz2 )

{

if( *sz1 == *sz2 )

continue;

cMax = *sz1 >= *sz2 ? *sz1 : *sz2;

cMin = *sz1 *sz2 - cMax;

if( (cMax <= 'z' && cMax >= 'a') && (32 == cMax - cMin) )

continue;

return *sz1 - *sz2;

}

return 0;

}

int main()

{

// 文件指針

FILE *pFileS, *pFileD;

// 輸入緩沖; 要替換的單詞; 新的單詞

char buffer[BUFFER_SIZE], wordOld[64] = {0}, wordNew[64] = {0};

// 緩沖區實際大小; 緩沖區操作標誌; 每次讀取的字節數; 要替換的單詞長度; 新單詞的長度; 區分大小寫標誌

int nTop = 0, nPoint, nRead, nOldLen, nNewLen, bDiv;

puts("輸入兩個詞");

scanf("cscs", wordOld, wordNew);

nOldLen = strlen(wordOld), nNewLen = strlen(wordNew);

puts("是否區分大小寫? 輸入1(Yes),0(No)");

scanf("%d", &bDiv);

pFileS = fopen("a.txt", "rb");

pFileD = fopen("aa.txt", "wb");

while( nRead = fread( buffer nTop, sizeof(char), BUFFER_SIZE - nTop, pFileS) )

{

nTop = nRead;

// 處理舊單詞整數倍的那部分緩沖

for( nPoint=0; nPoint<=nTop - nOldLen; )

{

// 如果要區分則用memcmp比較, 不區分用自定義比較函數比較

if( !( bDiv ? memcmp( buffer nPoint, wordOld, nOldLen) : MyCmp( buffer nPoint, wordOld, nOldLen )) )

{

// 相等, 寫進文件

fwrite( wordNew, sizeof(char), nNewLen, pFileD );

// 緩沖區標誌(相當於指針)移位

nPoint = nOldLen;

}

else

// 不相等, 將該字節寫入並移動1字節

fputc( buffer[nPoint ], pFileD );

}

// 處理剩下的緩沖區; 方法同上

if( !( bDiv ? memcmp( buffer nPoint, wordOld, nTop - nPoint ) : MyCmp( buffer nPoint, wordOld, nTop - nPoint)) )

{

memcpy( buffer, buffer nPoint, nTop - nPoint );

nTop = nTop - nPoint;

}

else

{

fwrite( buffer nPoint, sizeof(char), nTop - nPoint, pFileD );

nTop = 0;

}

}

// 關閉文件

puts("轉化成功");

fclose( pFileS ), fclose( pFileD );

return 0;

}{[MIMIcall]]網絡電話軟件

  • 上一篇:李連捷的人物履歷
  • 下一篇:各位施主,誰有歐姆龍PLC手編器的使用說明書?
  • copyright 2024編程學習大全網