英文文章——字符串处理(共10题)之一
code:
/*
函数ReadDat( )实现从文件IN.DAT中读取一篇英文文章存入到字符串数组xx中; 请编制函数SortCharD( ), 其函数的功能是: 以行为单位对字符按从大到小的顺序进行排序, 排序后的结果仍按行重新存入字符串数组xx中。最后main()函数调用函数WriteDat()把结果xx输出到文件OUT2.DAT中。
例: 原文: dAe,BfC.
CCbbAA
结果: fedCBA.,
bbCCAA
原始数据文件存放的格式是: 每行的宽度均小于80个字符, 含标点符号和空格。
注意: 部分源程序存放在PROG1.C中。
请勿改动主函数main( )、读数据函数ReadDat()和输出数据函数WriteDat()的内容。
*/
#include <stdio.h>
#include <string.h>
#include <conio.h>
char xx[50][80] ;
int maxline = 0 ; /* 文章的总行数 */
int ReadDat(void) ;
void WriteDat(void) ;
void SortCharD(void)
{/**/
int i,j,k,m,n; char ch;
for(i=0; i < maxline; i++)
{ j=strlen(xx[i]);
for(m=0; m < j-1; m++)
{ k=m;
for(n=m+1; n < j; n++)
if(xx[i][k] < xx[i][n]) k=n;
if(k!=m)
{ ch=xx[i][k]; xx[i][k]=xx[i][m]; xx[i][m]=ch; }
}
}
/**/
}
void main()
{
clrscr() ;
if(ReadDat()) {
printf("数据文件IN.DAT不能打开!\n\007") ;
return ;
}
SortCharD() ;
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("OUT2.DAT", "w") ;
for(i = 0 ; i < maxline ; i++) {
printf("%s\n", xx[i]) ;
fprintf(fp, "%s\n", xx[i]) ;
}
fclose(fp) ;
}
in.dat 文件内容为:
You can create an index on any field, on several fields to be
used
together, or on parts thereof, that you want to use as a key.
The
keys in indexes allow you quick access to specific records and
define
orders for sequential processing of a ISAM file. After you no
longer
need an index, you can delete it. Addition and indexes have no
effect
on the data records or on other indexes.
You may want a field in field in each record to uniquely
identify that
record from all other records in the file. For example, the
Employee
Number field is unique if you do not assign the same number to
two
different employees, and you never reassign these numbers to
other
employees. If you wish to find or modify the record belonging
to a
specific employee, this unique field saves the thouble of
determining
whether you have the correct record.
If you do not have a unique field, you must find the first
record
the matches your key and determine whether the record is the
one you
want. If it is not the correct one, you must search again to
find others.
If you know that you have a unique field within your records,
you
can include this fact in the key description, and ISAM will
allow only
unique keys. For example, if you specify that the employee
numbers are
unique, ISAM only lets you add records to the file for, or
change
numbers to, employee numbers that do not alreadly exist int
file.
out2.dat 文件内容应当为:
yxvuuttsssrroooonnnnnnllliiiffeeeeeeeeeddddccbaaaaaY,
yywuuttttttttsssrrrrpoooooonnkhhhhgfeeeeeeeaaaaaT.,,
yyxwuutssssssrrqpoooonnnnllkkiiiiiiffeeeeeeeeddddccccccaaa
yuuttssssrrrrrrqpooooooonnnnllliiiggffffeeeeeeedcaaSMIAA.
yxxvuttttsooonnnnnnnnliiiiihffeeeeeeeeeeedddddddccaaaaA.,
xtttssrrrrooooonnnihheeeeedddcaa.
yyywuuutttttrrqooonnnnnmllliiiiiiihhfffeeeeeeddddccaaaaaY
yxtttsrrrrrrrppoooooonmmmllllliihhhffeeeeeeeeeeddccaaFE.,
ywuuuuuttttssssrrqooooonnnnmmmliiiiihgffeeeeeeddbbaaN
yyvuuttttsssssrrrrrpoooonnnnnmmliihhgffeeeeeeeeeeeeddbaa,
yyywutttssrrrpoooooooonnnmmlliiiihhggfffeeeeeedddcbaI.
yvuuuttttssssrqppooonnnmmllliiiiiiihhhgfffeeeeeeeeeeeddccba,
ywvutttrrrrrooohhhheeeeeedccca.
yyvuuuuuttttssrrrqooooonnnmliiiihhffffeeeeeddddcaaI,
yyywuuttttttssrrrrroooonnnmmkiihhhhhheeeeeeeeeeeedddccaa
ywuuttttttttssssrrrroooooonnnnnmiiiihhhgffeeeeedcccaaaaI..,
yyyywwvuuuuuutttsrrrqoooooonnnlkiiiihhhffeeeeddcaaaI,
yywwuttttssrpooonnnnnnllllllkiiiiiihhfeeeedddccccaaaaSMIA,
yyyyxuuuutttsssrrrqpppooonnmmmllkiiihhffeeeeeeeeeeecbaaaF.,
yyuuutttssrrrrqoooooonnnllliihhgffeeeeeedddccaaSMIA,,
yyxuuttttttsssrrrpoooonnnnmmmlllliiihfeeeeeeeeddbbaaa.,
字符串处理之二
code:
/*
函数ReadDat( )实现从文件IN.DAT中读取一篇英文文章存入到字符串数组xx中; 请编制函数ConvertCharA(), 其函数的功能是: 以行为单位把字符串中的所有小写字母改写成该字母的下一个字母, 如果是字母z, 则改写成字母a,大写字母和其它字符保持不变。把已处理的字符串仍按行重新存入字符串数组xx中。最后main()函数调用函数WriteDat()把结果xx输出到文件OUT3.DAT中。
例: 原文: Adb.Bcdza
abck.LLhj
结果: Aec.Bdeab
bcdl.LLik
原始数据文件存放的格式是: 每行的宽度均小于80个字符, 含标点符号和空格。
注意: 部分源程序存放在PROG1.C中。
请勿改动主函数main( )、读数据函数ReadDat()和输出数据函数WriteDat()的内容。
*/
#include
#include
#include
char xx[50][80] ;
int maxline = 0 ; /* 文章的总行数 */
int ReadDat(void) ;
void WriteDat(void) ;
void ConvertCharA(void)
{/**/
int i,j;
for(i=0; i < maxline; i++)
for(j=0; j < strlen(xx[i]); j++)
if(xx[i][j]=='z') xx[i][j]='a';
else if((xx[i][j]>='a')&&(xx[i][j]<'z')) xx[i][j]++;
/**/
}
void main()
{
clrscr() ;
if(ReadDat()) {
printf("数据文件IN.DAT不能打开!\n\007") ;
return ;
}
ConvertCharA() ;
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("OUT3.DAT", "w") ;
for(i = 0 ; i < maxline ; i++) {
printf("%s\n", xx[i]) ;
fprintf(fp, "%s\n", xx[i]) ;
}
fclose(fp) ;
}
out3.dat文件内容应当如下:
Ypv dbo dsfbuf bo joefy po boz gjfme, po tfwfsbm gjfmet up cf
vtfe
uphfuifs, ps po qbsut uifsfpg, uibu zpv xbou up vtf bt b lfz.
Tif
lfzt jo joefyft bmmpx zpv rvjdl bddftt up tqfdjgjd sfdpset boe
efgjof
psefst gps tfrvfoujbm qspdfttjoh pg b ISAM gjmf. Agufs zpv op
mpohfs
offe bo joefy, zpv dbo efmfuf ju. Aeejujpo boe joefyft ibwf op
fggfdu
po uif ebub sfdpset ps po puifs joefyft.
Ypv nbz xbou b gjfme jo gjfme jo fbdi sfdpse up vojrvfmz
jefoujgz uibu
sfdpse gspn bmm puifs sfdpset jo uif gjmf. Fps fybnqmf, uif
Enqmpzff
Nvncfs gjfme jt vojrvf jg zpv ep opu bttjho uif tbnf ovncfs up
uxp
ejggfsfou fnqmpzfft, boe zpv ofwfs sfbttjho uiftf ovncfst up
puifs
fnqmpzfft. Ig zpv xjti up gjoe ps npejgz uif sfdpse cfmpohjoh
up b
tqfdjgjd fnqmpzff, uijt vojrvf gjfme tbwft uif uipvcmf pg
efufsnjojoh
xifuifs zpv ibwf uif dpssfdu sfdpse.
Ig zpv ep opu ibwf b vojrvf gjfme, zpv nvtu gjoe uif gjstu
sfdpse
uif nbudift zpvs lfz boe efufsnjof xifuifs uif sfdpse jt uif
pof zpv
xbou. Ig ju jt opu uif dpssfdu pof, zpv nvtu tfbsdi bhbjo up
gjoe puifst.
Ig zpv lopx uibu zpv ibwf b vojrvf gjfme xjuijo zpvs sfdpset,
zpv
dbo jodmvef uijt gbdu jo uif lfz eftdsjqujpo, boe ISAM xjmm
bmmpx pomz
vojrvf lfzt. Fps fybnqmf, jg zpv tqfdjgz uibu uif fnqmpzff
ovncfst bsf
vojrvf, ISAM pomz mfut zpv bee sfdpset up uif gjmf gps, ps
dibohf
ovncfst up, fnqmpzff ovncfst uibu ep opu bmsfbemz fyjtu jou
gjmf.
字符串处理之三
code:
/*
函数ReadDat( )实现从文件IN.DAT中读取一篇英文文章存入到字符串数组xx中; 请编制函数SortCharA( ), 其函数的功能是: 以行为单位对字符按从小到大的顺序进行排序, 排序后的结果仍按行重新存入字符串数组xx中。最后main()函数调用函数WriteDat()把结果xx输出到文件OUT1.DAT中。
例: 原文: dAe,BfC.
CCbbAA
结果: ,.ABCdef
AACCbb
原始数据文件存放的格式是: 每行的宽度均小于80个字符, 含标点符号和空格。
注意: 部分源程序存放在PROG1.C中。
请勿改动主函数main( )、读数据函数ReadDat()和输出数据函数WriteDat()的内容。
*/
#include
#include
#include
char xx[50][80] ;
int maxline = 0 ; /* 文章的总行数 */
int ReadDat(void) ;
void WriteDat(void) ;
void SortCharA(void)
{/**/
int i,j,k,m,n; char ch;
for(i=0; i < maxline; i++)
{ j=strlen(xx[i]);
for(m=0; m < j-1; m++)
{ k=m;
for(n=m+1; n < j; n++)
if(xx[i][k] > xx[i][n]) k=n;
if(k!=m)
{ ch=xx[i][k]; xx[i][k]=xx[i][m]; xx[i][m]=ch; }
}
}
/**/
}
void main()
{
clrscr() ;
if(ReadDat()) {
printf("数据文件IN.DAT不能打开!\n\007") ;
return ;
}
SortCharA() ;
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("OUT1.DAT", "w") ;
for(i = 0 ; i < maxline ; i++) {
printf("%s\n", xx[i]) ;
fprintf(fp, "%s\n", xx[i]) ;
}
fclose(fp) ;
}
out1.dat 文件内容如下(注意每行的前面有若干空格):
,Yaaaaabccddddeeeeeeeeeffiiilllnnnnnnoooorrsssttuuvxy
,,.Taaaaaeeeeeeefghhhhknnooooooprrrrsssttttttttuuwyy
aaaccccccddddeeeeeeeeffiiiiiikkllnnnnoooopqrrsssssstuuwxyy
.AAIMSaacdeeeeeeeffffggiiilllnnnnooooooopqrrrrrrssssttuuy
,.Aaaaaccdddddddeeeeeeeeeeeffhiiiiilnnnnnnnnooosttttuvxxy
.aacdddeeeeehhinnnooooorrrrsstttx
Yaaaaaccddddeeeeeefffhhiiiiiiilllmnnnnnoooqrrtttttuuuwyyy
,.EFaaccddeeeeeeeeeeffhhhiilllllmmmnoooooopprrrrrrrstttxy
Naabbddeeeeeeffghiiiiilmmmnnnnoooooqrrssssttttuuuuuwy
,aabddeeeeeeeeeeeeffghhiilmmnnnnnooooprrrrrsssssttttuuvyy
.Iabcdddeeeeeefffgghhiiiillmmnnnooooooooprrrsstttuwyyy
,abccddeeeeeeeeeeefffghhhiiiiiiilllmmnnnoooppqrssssttttuuuvy
.acccdeeeeeehhhhooorrrrrtttuvwy
,Iaacddddeeeeeffffhhiiiilmnnnoooooqrrrssttttuuuuuvyy
aaccdddeeeeeeeeeeeehhhhhhiikmmnnnoooorrrrrssttttttuuwyyy
,..Iaaaacccdeeeeeffghhhiiiimnnnnnoooooorrrrssssttttttttuuwy
,Iaaacddeeeeffhhhiiiiklnnnooooooqrrrstttuuuuuuvwwyyyy
,AIMSaaaaccccdddeeeefhhiiiiiikllllllnnnnnnoooprssttttuwwyy
,.Faaabceeeeeeeeeeeffhhiiikllmmmnnooopppqrrrssstttuuuuxyyyy
,,AIMSaaccdddeeeeeeffghhiilllnnnooooooqrrrrsstttuuuyy
,.aaabbddeeeeeeeefhiiillllmmmnnnnooooprrrsssttttttuuxyy
字符串处理之四
code:
/*
函数ReadDat( )实现从文件IN.DAT中读取一篇英文文章存入到字符串数组xx中; 请编制函数StrCharJL( ), 其函数的功能是: 以行为单位把字符串中的所有字符的ASCII值左移4位, 如果左移后,其字符的ASCII值小于等于32或大于100, 则原字符保持不变, 否则就把左移后的字符ASCII值再加上原字符的ASCII值, 得到新的字符仍存入原字符串对应的位置上,之后把已处理的字符串仍按行重新存入字符串数组xx中。最后main()函数调用函数WriteDat()把结果xx输出到OUT7.DAT文件中。
原始数据文件存放的格式是: 每行的宽度均小于80个字符, 含标点符号和空格。
注意: 部分源程序存放在PROG1.C中。
请勿改动主函数main( )、读数据函数ReadDat()和输出数据函数WriteDat()的内容。
*/
#include
#include
#include
char xx[50][80] ;
int maxline = 0 ; /* 文章的总行数 */
int ReadDat(void) ;
void WriteDat(void) ;
void StrCharJL(void)
{/**/
int i,j; char m;
上一页 [1] [2] [3] [4] 下一页