
哪位大侠帮忙写一个C++ 文本处理的程序啊。。 谢谢。。紧急求救 50
要求写出的程序有以下功能。,1.查找一个单词。找到后告诉用户那个单词在TXT文件的第几行。如果单词出现在很多行,告诉用户所有的行数。然后让用户写一个单词。。。2.把上面找...
要求写出的程序有以下功能。,
1.查找一个单词。找到后告诉用户那个单词在TXT文件的第几行。如果单词出现在很多行,告诉用户所有的行数。
然后让用户写一个单词。。。
2.把上面找到的单词全部换成新单词。就是用新的替换旧的。
3.算出TXT文件夹中的字母总数。
4,算出TXT文件夹中的单词总数。。
谁如果做出来了 有重谢。。
百度分不多。。。我觉不食言。。 写一点也好。。 全部也好。。。报酬50RMB+ 展开
1.查找一个单词。找到后告诉用户那个单词在TXT文件的第几行。如果单词出现在很多行,告诉用户所有的行数。
然后让用户写一个单词。。。
2.把上面找到的单词全部换成新单词。就是用新的替换旧的。
3.算出TXT文件夹中的字母总数。
4,算出TXT文件夹中的单词总数。。
谁如果做出来了 有重谢。。
百度分不多。。。我觉不食言。。 写一点也好。。 全部也好。。。报酬50RMB+ 展开
3个回答
展开全部
本人出于对程序的喜爱,写了,精度警告不用管,都是在我的允许范围之内,是c语言版,本人不擅长c++,能满足你的所有要求,代码如下(警告不用担心,已经经过调试):
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
/************************************************************************************
************************************************************************************/
typedef unsigned int UINT ;
typedef struct Infor
{
UINT iLine ;
UINT iDistance ;
Infor * next ; //为以后用链表做准备,此程序中未使用
}_Infor , * _infor ;
/************************************************************************************
************************************************************************************/
extern UINT Count( FILE * fp ) ;
extern void Memory( FILE * fp , char * p_Array , UINT iCount ) ;
extern UINT Record( char * p_szArray , UINT iCount , _infor pInfor , char * szSeaWord ) ;
extern void Conver( char * Array , UINT iCount) ;
extern void Substitude( _infor _inforSeaWord , UINT iCounter , char * f_szName , char * szNewWord , char * szSeaword , UINT iCount ) ;
/************************************************************************************
************************************************************************************/
void main( void )
{
FILE * fp = NULL ;
char f_szName[ 20 ] = { '\0' } , szSeaword[ 20 ] = { '\0' } , szNewWord[ 20 ] = { '\0' } , * p_szArray = NULL ; //以后再改为链表 ,现在最多十个·
_Infor _InforSeaWord[ 10 ] = { 0 } ;
UINT iCount = 0 , i = 0 , iLineLable = 0 , isCounter = 0 , iCount_word = 0 , iLengthWord = 0 , iNewWordLength = 0 ;
printf( "please input your file name( 输入文件名 ) :\n" ) ;
scanf( "%s" , f_szName ) ;
if( NULL == ( fp = fopen( f_szName , "rb" ) ) )
{
printf( "can not open the file!!\n" ) ;
exit( 0 ) ;
}
printf( "please input the searching word( 输入你要查找的字符串 ) :\n" ) ;
scanf( "%s" , szSeaword ) ;
iCount = Count( fp ) ;
iLengthWord = strlen( szSeaword ) ;
if( iCount < iLengthWord )
{
printf( "your input is Error!!\n" ) ;
exit( 0 ) ;
}
rewind( fp ) ;
p_szArray = ( char * )calloc( iCount , sizeof( char ) ) ;
Memory( fp , p_szArray , iCount ) ;
Conver( p_szArray , iCount ) ;
isCounter = Record( p_szArray , iCount , _InforSeaWord , szSeaword ) ;
printf( "please input the new word that you want to substitude( 输入你要替换成的字符串 ) :\n\n" ) ;
scanf( "%s" , szNewWord ) ;
iNewWordLength = strlen( szNewWord ) ;
fclose( fp ) ;
Substitude( _InforSeaWord , isCounter , f_szName , szNewWord , szSeaword , iCount ) ;
printf( "\nhahahaha , success !!!\n\n" ) ;
while( 1 ) ;
}
/************************************************************************************
************************************************************************************/
void Substitude( _infor _inforSeaWord , UINT iCounter , char * f_szName , char * szNewWord , char * szSeaword , UINT iCount )
{
FILE * fp = NULL ;
UINT i = 0 , k = 0 , itCount = 0 , t = 0 , temp = iCount , iLengthNewWord = strlen( szNewWord ) , iLengthWord = strlen( szSeaword ) ;
char * pLess = NULL , * pMore = NULL , acc_file = '\0' ;
if( NULL == ( fp = fopen( f_szName , "rb+" ) ) ) //以可读写方式打开
{
printf( "can not open the file!!\n" ) ;
exit( 0 ) ;
}
if( iLengthWord == iLengthNewWord )
{
for( i = 0 ; i < iCounter ; i++ )
{
fseek( fp , ( _inforSeaWord + i ) -> iDistance , 0 ) ;
fwrite( szNewWord , iLengthNewWord , 1 , fp ) ;
}
}
else if( iLengthNewWord < iLengthWord )
{
pLess = ( char * )calloc( iLengthWord , sizeof( char ) ) ;
while( '\0' != * ( szNewWord + k ) )
{
* ( pLess + k ) = * ( szNewWord + k ) ;
k ++ ;
}
itCount = k ;
for( k = itCount ; k < iLengthWord ; k++ )
{
* ( pLess + k ) = ' ' ;
}
for( i = 0 ; i < iCounter ; i ++ )
{
fseek( fp , ( _inforSeaWord + i ) -> iDistance , 0 ) ;
fwrite( pLess , iLengthWord , 1 , fp ) ;
}
}
else
{
i = 0 ;
fclose( fp ) ;
if( NULL == ( fp = fopen( f_szName , "rb" ) ) )
{
printf( "can not open the file !!\n" ) ;
exit( 0 ) ;
}
iCount += ( iLengthNewWord - iLengthWord ) * iCounter ;
pMore = ( char * )calloc( iCount , sizeof( char ) ) ;
while( EOF != ( acc_file = fgetc( fp ) ) ) //这里可能要向上面一样考虑一下~号的使用
{
* ( pMore + i ) = acc_file ;
i ++ ;
}
for( i = 0 ; i < iCounter ; i ++ )
{
for( k = temp - 1 ; k >= ( _inforSeaWord + i ) -> iDistance ; k -- ) //这里又吸收了一点经验
{
* ( pMore + k + iLengthNewWord - iLengthWord ) = * ( pMore + k ) ;
}
for( k = ( _inforSeaWord + i ) -> iDistance , t = 0 ; k <= ( _inforSeaWord + i ) -> iDistance + iLengthNewWord ; k++ , t ++ )
{
* ( pMore + k ) = ' ' ;
if( k != ( _inforSeaWord + i ) -> iDistance + iLengthNewWord )
{
* ( pMore + k ) = * ( szNewWord + t ) ;
}
}
for( t = i + 1 ; t < iCounter ; t ++ )
{
( _inforSeaWord + t ) -> iDistance += ( iLengthNewWord - iLengthWord ) ;
}
}
fclose( fp ) ;
if( NULL == ( fp = fopen( f_szName , "wb" ) ) )
{
printf( "can not open the file !!\n" ) ;
exit( 0 ) ;
}
for( i = 0 ; i < iCount ; i++ )
{
if( EOF == ( acc_file = fputc( pMore[ i ] , fp ) ) )
{
printf( "some unexpected problems happens !!!\n" ) ;
exit( 0 ) ;
}
}
}
fclose( fp ) ;
}
/************************************************************************************
************************************************************************************/
UINT Count( FILE * fp )
{
UINT count = 0 ;
char acc_file = '\0' , szTemp[ 2 ] = { '\0' } ;
while( EOF != ( acc_file = fgetc( fp ) ) )
{
szTemp[ count % 2 ] = acc_file ;
count ++ ;
}
if( ' ' == szTemp[ ( count - 1 ) %2 ] )
{
return ( count - 1 ) ;
}
return count ;
}
/************************************************************************************
************************************************************************************/
void Memory( FILE * fp , char * p_Array , UINT iCount )
{
UINT i = 0 , iCount_word = 0 ;
char acc_file = '\0' ;
for( i = 0 ; i < iCount ; i ++ )
{
acc_file = fgetc( fp ) ;
if( 10 == acc_file || 13 == acc_file )
{
acc_file = '~' ;
}
if( ( 'a' <= acc_file && acc_file <='z' ) || ( 'A' <= acc_file && acc_file <= 'Z' ) )
{
iCount_word ++ ;
}
* ( p_Array + i ) = acc_file ;
}
printf( "\nthe Amount of the letter( 字母总数 ) is : %d\n" , iCount_word ) ;
}
/************************************************************************************
************************************************************************************/
UINT Record( char * p_szArray , UINT iCount , _infor pInfor , char * szSeaWord ) //最后的这里改一下参数 ,以另一种写法
{
UINT i = 0 , iLineLable = 0 , iDistsance = 0 , iCounter = 0 , iLength = strlen( szSeaWord ) , k = 0 , temp = 0 ;
for( i = 0 ; i <= iCount - iLength ; i ++ )
{
if( NULL != strstr( p_szArray + i , szSeaWord ) )
{
temp = ( UINT )( strstr( p_szArray + i , szSeaWord ) - p_szArray ) ;
( pInfor + iCounter) -> iDistance = temp ; //在且仅在里面进行判断 , 否则无意义
i = temp + iLength - 1 ; //i 后面会进行自加
for( k = 0 ; k < ( pInfor + iCounter) -> iDistance ; k ++ )
{
if( '~' == * ( p_szArray + k ) )
{
iLineLable ++ ;
}
k++ ;
}
( pInfor + iCounter) -> iLine = iLineLable ;
printf( "第 %d 个你查询的单词出现在 %d 行 !\n\n" , iCounter + 1 , iLineLable + 1 ) ;
iLineLable = 0 ;
iCounter ++ ; //还差行数的储存
}
}
return iCounter ;
}
/************************************************************************************
************************************************************************************/
void Conver( char * Array , UINT iCount )
{
UINT i = 0 , k = 0 , iCounter = 0 ;
char * temp_sz = ( char * )calloc( iCount , sizeof( char ) ) ;
char * t_Array = ( char * )calloc( iCount , sizeof( char ) ) ;
while( '\0' != * ( Array + i ) )
{
if( '~' == * ( Array + i ) || ',' == * ( Array + i ) || '.' == * ( Array + i ) || '!' == * ( Array + i ) )
{
* ( temp_sz + i ) = ' ' ;
}
else
{
* ( temp_sz + i ) = * ( Array + i ) ;
}
i ++ ;
}
* ( temp_sz + i ) = '\0' ;
i = 0 ;
while( ' ' == * ( temp_sz + i ) )
{
i ++ ;
}
* ( t_Array + k ) = * ( temp_sz + i ) ;
k++ ;
i ++ ;
while( '\0' != * ( temp_sz + i ) ) //去除多余的空格
{
if( ' ' != * ( temp_sz + i ) )
{
* ( t_Array + k ) = * ( temp_sz + i ) ;
i++ ;
k ++ ;
}
else
{
if( ' ' != * ( t_Array + k - 1 ) ) //这里可以尝试一下
{
* ( t_Array + k ) = * ( temp_sz + i ) ;
k ++ ;
i ++ ;
}
else
{
i ++ ;
}
}
}
* ( t_Array + k ) = '\0' ;
iCounter = k ;
iCount = 0 ;
for( i = 0 ; i < iCounter ; i ++ )
{
if( ' ' == * ( t_Array + i ) )
{
iCount ++ ;
}
}
printf( "\nthe Amount of word( 单词总数 ) is : %d\n\n" , iCount + 1 ) ;
}
/************************************************************************************
************************************************************************************/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
/************************************************************************************
************************************************************************************/
typedef unsigned int UINT ;
typedef struct Infor
{
UINT iLine ;
UINT iDistance ;
Infor * next ; //为以后用链表做准备,此程序中未使用
}_Infor , * _infor ;
/************************************************************************************
************************************************************************************/
extern UINT Count( FILE * fp ) ;
extern void Memory( FILE * fp , char * p_Array , UINT iCount ) ;
extern UINT Record( char * p_szArray , UINT iCount , _infor pInfor , char * szSeaWord ) ;
extern void Conver( char * Array , UINT iCount) ;
extern void Substitude( _infor _inforSeaWord , UINT iCounter , char * f_szName , char * szNewWord , char * szSeaword , UINT iCount ) ;
/************************************************************************************
************************************************************************************/
void main( void )
{
FILE * fp = NULL ;
char f_szName[ 20 ] = { '\0' } , szSeaword[ 20 ] = { '\0' } , szNewWord[ 20 ] = { '\0' } , * p_szArray = NULL ; //以后再改为链表 ,现在最多十个·
_Infor _InforSeaWord[ 10 ] = { 0 } ;
UINT iCount = 0 , i = 0 , iLineLable = 0 , isCounter = 0 , iCount_word = 0 , iLengthWord = 0 , iNewWordLength = 0 ;
printf( "please input your file name( 输入文件名 ) :\n" ) ;
scanf( "%s" , f_szName ) ;
if( NULL == ( fp = fopen( f_szName , "rb" ) ) )
{
printf( "can not open the file!!\n" ) ;
exit( 0 ) ;
}
printf( "please input the searching word( 输入你要查找的字符串 ) :\n" ) ;
scanf( "%s" , szSeaword ) ;
iCount = Count( fp ) ;
iLengthWord = strlen( szSeaword ) ;
if( iCount < iLengthWord )
{
printf( "your input is Error!!\n" ) ;
exit( 0 ) ;
}
rewind( fp ) ;
p_szArray = ( char * )calloc( iCount , sizeof( char ) ) ;
Memory( fp , p_szArray , iCount ) ;
Conver( p_szArray , iCount ) ;
isCounter = Record( p_szArray , iCount , _InforSeaWord , szSeaword ) ;
printf( "please input the new word that you want to substitude( 输入你要替换成的字符串 ) :\n\n" ) ;
scanf( "%s" , szNewWord ) ;
iNewWordLength = strlen( szNewWord ) ;
fclose( fp ) ;
Substitude( _InforSeaWord , isCounter , f_szName , szNewWord , szSeaword , iCount ) ;
printf( "\nhahahaha , success !!!\n\n" ) ;
while( 1 ) ;
}
/************************************************************************************
************************************************************************************/
void Substitude( _infor _inforSeaWord , UINT iCounter , char * f_szName , char * szNewWord , char * szSeaword , UINT iCount )
{
FILE * fp = NULL ;
UINT i = 0 , k = 0 , itCount = 0 , t = 0 , temp = iCount , iLengthNewWord = strlen( szNewWord ) , iLengthWord = strlen( szSeaword ) ;
char * pLess = NULL , * pMore = NULL , acc_file = '\0' ;
if( NULL == ( fp = fopen( f_szName , "rb+" ) ) ) //以可读写方式打开
{
printf( "can not open the file!!\n" ) ;
exit( 0 ) ;
}
if( iLengthWord == iLengthNewWord )
{
for( i = 0 ; i < iCounter ; i++ )
{
fseek( fp , ( _inforSeaWord + i ) -> iDistance , 0 ) ;
fwrite( szNewWord , iLengthNewWord , 1 , fp ) ;
}
}
else if( iLengthNewWord < iLengthWord )
{
pLess = ( char * )calloc( iLengthWord , sizeof( char ) ) ;
while( '\0' != * ( szNewWord + k ) )
{
* ( pLess + k ) = * ( szNewWord + k ) ;
k ++ ;
}
itCount = k ;
for( k = itCount ; k < iLengthWord ; k++ )
{
* ( pLess + k ) = ' ' ;
}
for( i = 0 ; i < iCounter ; i ++ )
{
fseek( fp , ( _inforSeaWord + i ) -> iDistance , 0 ) ;
fwrite( pLess , iLengthWord , 1 , fp ) ;
}
}
else
{
i = 0 ;
fclose( fp ) ;
if( NULL == ( fp = fopen( f_szName , "rb" ) ) )
{
printf( "can not open the file !!\n" ) ;
exit( 0 ) ;
}
iCount += ( iLengthNewWord - iLengthWord ) * iCounter ;
pMore = ( char * )calloc( iCount , sizeof( char ) ) ;
while( EOF != ( acc_file = fgetc( fp ) ) ) //这里可能要向上面一样考虑一下~号的使用
{
* ( pMore + i ) = acc_file ;
i ++ ;
}
for( i = 0 ; i < iCounter ; i ++ )
{
for( k = temp - 1 ; k >= ( _inforSeaWord + i ) -> iDistance ; k -- ) //这里又吸收了一点经验
{
* ( pMore + k + iLengthNewWord - iLengthWord ) = * ( pMore + k ) ;
}
for( k = ( _inforSeaWord + i ) -> iDistance , t = 0 ; k <= ( _inforSeaWord + i ) -> iDistance + iLengthNewWord ; k++ , t ++ )
{
* ( pMore + k ) = ' ' ;
if( k != ( _inforSeaWord + i ) -> iDistance + iLengthNewWord )
{
* ( pMore + k ) = * ( szNewWord + t ) ;
}
}
for( t = i + 1 ; t < iCounter ; t ++ )
{
( _inforSeaWord + t ) -> iDistance += ( iLengthNewWord - iLengthWord ) ;
}
}
fclose( fp ) ;
if( NULL == ( fp = fopen( f_szName , "wb" ) ) )
{
printf( "can not open the file !!\n" ) ;
exit( 0 ) ;
}
for( i = 0 ; i < iCount ; i++ )
{
if( EOF == ( acc_file = fputc( pMore[ i ] , fp ) ) )
{
printf( "some unexpected problems happens !!!\n" ) ;
exit( 0 ) ;
}
}
}
fclose( fp ) ;
}
/************************************************************************************
************************************************************************************/
UINT Count( FILE * fp )
{
UINT count = 0 ;
char acc_file = '\0' , szTemp[ 2 ] = { '\0' } ;
while( EOF != ( acc_file = fgetc( fp ) ) )
{
szTemp[ count % 2 ] = acc_file ;
count ++ ;
}
if( ' ' == szTemp[ ( count - 1 ) %2 ] )
{
return ( count - 1 ) ;
}
return count ;
}
/************************************************************************************
************************************************************************************/
void Memory( FILE * fp , char * p_Array , UINT iCount )
{
UINT i = 0 , iCount_word = 0 ;
char acc_file = '\0' ;
for( i = 0 ; i < iCount ; i ++ )
{
acc_file = fgetc( fp ) ;
if( 10 == acc_file || 13 == acc_file )
{
acc_file = '~' ;
}
if( ( 'a' <= acc_file && acc_file <='z' ) || ( 'A' <= acc_file && acc_file <= 'Z' ) )
{
iCount_word ++ ;
}
* ( p_Array + i ) = acc_file ;
}
printf( "\nthe Amount of the letter( 字母总数 ) is : %d\n" , iCount_word ) ;
}
/************************************************************************************
************************************************************************************/
UINT Record( char * p_szArray , UINT iCount , _infor pInfor , char * szSeaWord ) //最后的这里改一下参数 ,以另一种写法
{
UINT i = 0 , iLineLable = 0 , iDistsance = 0 , iCounter = 0 , iLength = strlen( szSeaWord ) , k = 0 , temp = 0 ;
for( i = 0 ; i <= iCount - iLength ; i ++ )
{
if( NULL != strstr( p_szArray + i , szSeaWord ) )
{
temp = ( UINT )( strstr( p_szArray + i , szSeaWord ) - p_szArray ) ;
( pInfor + iCounter) -> iDistance = temp ; //在且仅在里面进行判断 , 否则无意义
i = temp + iLength - 1 ; //i 后面会进行自加
for( k = 0 ; k < ( pInfor + iCounter) -> iDistance ; k ++ )
{
if( '~' == * ( p_szArray + k ) )
{
iLineLable ++ ;
}
k++ ;
}
( pInfor + iCounter) -> iLine = iLineLable ;
printf( "第 %d 个你查询的单词出现在 %d 行 !\n\n" , iCounter + 1 , iLineLable + 1 ) ;
iLineLable = 0 ;
iCounter ++ ; //还差行数的储存
}
}
return iCounter ;
}
/************************************************************************************
************************************************************************************/
void Conver( char * Array , UINT iCount )
{
UINT i = 0 , k = 0 , iCounter = 0 ;
char * temp_sz = ( char * )calloc( iCount , sizeof( char ) ) ;
char * t_Array = ( char * )calloc( iCount , sizeof( char ) ) ;
while( '\0' != * ( Array + i ) )
{
if( '~' == * ( Array + i ) || ',' == * ( Array + i ) || '.' == * ( Array + i ) || '!' == * ( Array + i ) )
{
* ( temp_sz + i ) = ' ' ;
}
else
{
* ( temp_sz + i ) = * ( Array + i ) ;
}
i ++ ;
}
* ( temp_sz + i ) = '\0' ;
i = 0 ;
while( ' ' == * ( temp_sz + i ) )
{
i ++ ;
}
* ( t_Array + k ) = * ( temp_sz + i ) ;
k++ ;
i ++ ;
while( '\0' != * ( temp_sz + i ) ) //去除多余的空格
{
if( ' ' != * ( temp_sz + i ) )
{
* ( t_Array + k ) = * ( temp_sz + i ) ;
i++ ;
k ++ ;
}
else
{
if( ' ' != * ( t_Array + k - 1 ) ) //这里可以尝试一下
{
* ( t_Array + k ) = * ( temp_sz + i ) ;
k ++ ;
i ++ ;
}
else
{
i ++ ;
}
}
}
* ( t_Array + k ) = '\0' ;
iCounter = k ;
iCount = 0 ;
for( i = 0 ; i < iCounter ; i ++ )
{
if( ' ' == * ( t_Array + i ) )
{
iCount ++ ;
}
}
printf( "\nthe Amount of word( 单词总数 ) is : %d\n\n" , iCount + 1 ) ;
}
/************************************************************************************
************************************************************************************/
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询