★☆題目3(上機題庫id 133題;上機題庫id 59、99字符串位置倒置題)
函數(shù)ReadDat( )實現(xiàn)從文件IN.DAT中讀取一篇英文文章存入到字符串數(shù)組xx中;請編制函數(shù)StrOR( ),其函數(shù)的功能是:以行為單位依次把字符串中所有小寫字母o左邊的字符串內(nèi)容移到該串的右邊存放,然后把小寫字母o刪除,余下的字符串內(nèi)容移到已處理字符串的左邊存放,之后把已處理的字符串仍按行重新存入字符串數(shù)組xx中。最后main()函數(shù)調(diào)用函數(shù)WriteDat()把結果xx輸出到文件OUT5.DAT中。
例如:原文:n any field.Yu can create an index
you have the correct record.
結果:n any field. Yu can create an index
rd. yu have the crrect rec
原始數(shù)據(jù)文件存放的格式是:每行的寬度均小于80個字符,含標點符號和空格。
注意:部分源程序存放在文件prog1.c中。
請勿改動主函數(shù)main()、讀數(shù)據(jù)函數(shù)ReadDat()和輸出數(shù)據(jù)函數(shù)WriteDat()的內(nèi)容。
#include
#include
#include
char xx[50][80] ;
int maxline = 0 ; /* 文章的總行數(shù) */
int ReadDat(void) ;
void WriteDat(void) ;
void StrOR(void)
{int i,righto,j,s,k;
char tem[80];
for(i=0;i for(j=strlen(xx[i])-1;j>=0;j--)
{ k=0;
memset(tem,0,80); /*初始化字符串數(shù)組tem*/
if(xx[i][j]=='o') /*如果當前字符為'o',進入以下語句*/
{righto=j; /*則將此字符中位置j的值賦給righto*/
for(s=righto+1;s tem[k++]=xx[i][s]; /*從righto的下一跳開始將其后所有的字符都存入到tem中*/
for(s=0;s if(xx[i][s]!='o') tem[k++]=xx[i][s]; /*將不是字符'o'的字符全存入到tem中*/
strcpy(xx[i],tem); /*將當前已處理的字符重新存入當前行xx*/
}
else continue;
}
}
void main()
{
clrscr() ;
if(ReadDat()) {
printf("數(shù)據(jù)文件IN.DAT不能打開!\n\007") ;
return ;
}
StrOR() ;
WriteDat() ;
}
int ReadDat(void)
{
FILE *fp ;
int i = 0 ;
char *p ;
if((fp = fopen("IN.DAT", "r")) == NULL) return 1 ;
while(fgets(xx[i], 80, fp) != NULL) {
p = strchr(xx[i], '\n') ;
if(p) *p = 0 ;
i++ ;
}
maxline = i ;
fclose(fp) ;
return 0 ;
}
void WriteDat(void)
{
FILE *fp ;
int i ;
clrscr() ;
fp = fopen("OUT5.DAT", "w") ;
for(i = 0 ; i < maxline ; i++) {
printf("%s\n", xx[i]) ;
fprintf(fp, "%s\n", xx[i]) ;
}
fclose(fp) ;
}
函數(shù)ReadDat( )實現(xiàn)從文件IN.DAT中讀取一篇英文文章存入到字符串數(shù)組xx中;請編制函數(shù)StrOR( ),其函數(shù)的功能是:以行為單位依次把字符串中所有小寫字母o左邊的字符串內(nèi)容移到該串的右邊存放,然后把小寫字母o刪除,余下的字符串內(nèi)容移到已處理字符串的左邊存放,之后把已處理的字符串仍按行重新存入字符串數(shù)組xx中。最后main()函數(shù)調(diào)用函數(shù)WriteDat()把結果xx輸出到文件OUT5.DAT中。
例如:原文:n any field.Yu can create an index
you have the correct record.
結果:n any field. Yu can create an index
rd. yu have the crrect rec
原始數(shù)據(jù)文件存放的格式是:每行的寬度均小于80個字符,含標點符號和空格。
注意:部分源程序存放在文件prog1.c中。
請勿改動主函數(shù)main()、讀數(shù)據(jù)函數(shù)ReadDat()和輸出數(shù)據(jù)函數(shù)WriteDat()的內(nèi)容。
#include
#include
#include
char xx[50][80] ;
int maxline = 0 ; /* 文章的總行數(shù) */
int ReadDat(void) ;
void WriteDat(void) ;
void StrOR(void)
{int i,righto,j,s,k;
char tem[80];
for(i=0;i
{ k=0;
memset(tem,0,80); /*初始化字符串數(shù)組tem*/
if(xx[i][j]=='o') /*如果當前字符為'o',進入以下語句*/
{righto=j; /*則將此字符中位置j的值賦給righto*/
for(s=righto+1;s
for(s=0;s
strcpy(xx[i],tem); /*將當前已處理的字符重新存入當前行xx*/
}
else continue;
}
}
void main()
{
clrscr() ;
if(ReadDat()) {
printf("數(shù)據(jù)文件IN.DAT不能打開!\n\007") ;
return ;
}
StrOR() ;
WriteDat() ;
}
int ReadDat(void)
{
FILE *fp ;
int i = 0 ;
char *p ;
if((fp = fopen("IN.DAT", "r")) == NULL) return 1 ;
while(fgets(xx[i], 80, fp) != NULL) {
p = strchr(xx[i], '\n') ;
if(p) *p = 0 ;
i++ ;
}
maxline = i ;
fclose(fp) ;
return 0 ;
}
void WriteDat(void)
{
FILE *fp ;
int i ;
clrscr() ;
fp = fopen("OUT5.DAT", "w") ;
for(i = 0 ; i < maxline ; i++) {
printf("%s\n", xx[i]) ;
fprintf(fp, "%s\n", xx[i]) ;
}
fclose(fp) ;
}