求 C++高手解答 一条编程题,求原代码. 悬赏20点
现作的, 有bug的话站内短信我
要处理的文件必须先改成英文名( 只含ASCII的名字 )
fstream不能直接打开中文名文件, 而setlcale和locale::global也会造成不能显示或报错( 网上的方法和我自己尝试都不行 ), 我也不想用c或api来代替, 只能凑合一下了; 或者你自己改下代码, 用其他文件打开方式
#include
#include
#include
#include
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 << "
替换成功" << endl;
return 0;
}
c语言的, 这个可以用中文文件名
#include
#include
#include
#include
#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[^
]", 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( '
', 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 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 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("%255[^
]", 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( "%63s", szFileName ) )
{
fflush( stdin );
if( pFile = fopen( szFileName, "r" ) )
{
fclose( pFile );
break;
}
else
puts("找不到文件");
}
}
}
最后一种方法, 去除了校验等...
#include
#include
// 缓冲区, 用来放读取的内容
#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 = '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("%63s%63s", 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;
}
#includeusing namespace std;float fun1(float x){return pow(x, 2);}float fun2(float x){return 2*x + 1;}float Integral(int a, int b, float(*fun)(float)){float t = (b - a) / 100000.0;float integral = 0;for (int i = 0;i < 100000;i++){integral += t*fun(a + (i + 1)*t);}return integral;}int main(){int a=0, b=1;float y1, y2,x;cout << "输入的函数为:" << endl;cout <<"x^2" << endl;cout << "积分值为:" << Integral(a, b, fun1) << endl;cout << "输入的函数为:" << endl;cout << "2x+1" << endl;Integral(a, b, fun2);cout << "积分值为:" << Integral(a, b, fun2) << endl;system("pause");return 0;}
现作的, 有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]]网络电话软件
貂泳曲克: 经过上机验证没有问题哦 百度最近的格式总是会乱 格式调了半天…………好...
陈巴尔虎旗13487119850: 请C++高手帮助一道编程题. - ?
貂泳曲克: 总是得1,理来论上正自常的,因为递归百把m,n都减到度了问0,然后输出了n+1,就是答1#include "iostream.h" int akm(int m,int n); int main() { int a,b; cin>>a>>b; cout<<akm(a,b); } int akm(int m,int n) { if(m==0) return n+1; else { if(n==0) return akm(m-1,n); else return akm(m,n-1); } }
陈巴尔虎旗13487119850: C++编程题 求高手解答!! - ?
貂泳曲克: 1.#include "stdio.h" int main() { int i,s=0; for(i=2;i s+=i*10+4; printf("24+44+64+...+304=%d\n",s); return 0; }2.#include "stdio.h" int main() { int i=0,s=0; do { i+=2; s+=i*10+4;}while(s printf("%d\n",i-2); } 结果:1.2. 18
陈巴尔虎旗13487119850: 请C++高手帮我解答这条题目 - ?
貂泳曲克: 用递归写的:#include int main(int argc, char* argv[]) { unsigned long fac(unsigned int n); unsigned int n; unsigned long y; cout<<"输入一...
陈巴尔虎旗13487119850: 跪求C++高手 解这个编程题 - ?
貂泳曲克: float Calculate(float time) { float cash; if(time10) cash=10; return cash; } 另外,你要求用户输入0表示结束状态,再显示已经输入的停车总时间及停车总费用,这个应当在主函数中通过一个while循环进行控制,循环的调用我写的这个函数即可.
陈巴尔虎旗13487119850: c++题目一个,求高手解答 - ?
貂泳曲克: #include#include using namespace std;int main(){ string s; char temp; int len,i,j; cout<<"请输入字符:"; cin>>s; len=sizeof(s)...
陈巴尔虎旗13487119850: 这是一道C++编程题,急求大神解决!!!!!求代码...?
貂泳曲克: #include<iostream>using namespace std;void Converse(char *s, int len){ for(int i=0;i<len;i++){ if(s[i]>='a' && s[i]<='z') s[i] -= 32; }}void copy(char*a, char*b){ int i=0; while(a[i] !='\0'){ b[i]=a[i]; i++; } b[i]='\0';}void Connect(char*a, char*b){ int len = 0; while...
陈巴尔虎旗13487119850: 请求解答C++编程题目,谢谢啊!! - ?
貂泳曲克: //写了一个,经测试没有问题.#include using namespace std;template elemType sum(elemType* first, elemType* last){ elem...
陈巴尔虎旗13487119850: 求解一道C++编程题?
貂泳曲克: 你事先定义了num,然后a,b,c就已经定下来了,而且没有给num赋初值,这样的话,num的值就不是你说了算的了. 你的程序运行流程是这样的: 定义一个数叫num,值是随机数;(方便起见我们认为它是123) 它的个位数是c=3;十位数是b=...
陈巴尔虎旗13487119850: 求助!!一道c++编程题 - ?
貂泳曲克: #include<iostream>#include<string.h> using namespace std; int main() {int i,j; char t,s[200]; gets(s); for(j=0;s[j];j++); if(j%2==0) j--; for(i=1;i<j;i+=2,j-=2) { t=s[i]; s[i]=s[j]; s[j]=t; } cout<<s; return 0; }