[2006年全国房价]2006年9月全国等级考试三级c语言上机题库(三)

试题集锦 2023-06-15 网络整理 可可

【shitiku.jxxyjl.com--试题集锦】

★☆题目3(上机题库id 133题;上机题库id 59、99字符串位置倒置题)

函数readdat( )实现从文件in.dat中读取一篇英文文章存入到字符串数组xx中;请编制函数stror( ),其函数的功能是:以行为单位依次把字符串中所有小写字母o左边的字符串内容移到该串的右边存放,然后把小写字母o删除,余下的字符串内容移到已处理字符串的左边存放,之后把已处理的字符串仍按行重新存入字符串数组xx中。最后main()函数调用函数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
原始数据文件存放的格式是:每行的宽度均小于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 stror(void)
{int i,righto,j,s,k;
char tem[80];
for(i=0;i<maxline;i++) /*倒序循环*/
for(j=strlen(xx[i])-1;j>=0;j--)
{ k=0;
memset(tem,0,80); /*初始化字符串数组tem*/
if(xx[i][j]=="o") /*如果当前字符为"o",进入以下语句*/
{righto=j; /*则将此字符中位置j的值赋给righto*/
for(s=righto+1;s<strlen(xx[i]);s++)
tem[k++]=xx[i][s]; /*从righto的下一跳开始将其后所有的字符都存入到tem中*/
for(s=0;s<righto;s++) /*从当前行首部开始到出现字符"o"的位置(righoto)之前开始循环*/
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("数据文件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) ;
}

解法二:
void stror(void)
{ int i;
char a[80],*p;
for(i=0;i<maxline;i++)
{ p=strchr(xx[i],"o");
while(p)
{ memset(a,0,80);
memcpy(a,xx[i],p-xx[i]);
strcpy(xx[i],p+1);
strcat(xx[i],a);
p=strchr(xx[i],"o");
}
}
}

解法三:
void stror(void)
{ int i,j; char yy[80],*p;
for(i=0; i<maxline;i++)
for(j=0; j<strlen(xx[i]); j++)
if(xx[i][j]=="o")
{ p=&xx[i][j+1];
strcpy(yy,p); /*将指针p所指向的字符串拷贝到字符串yy中去*/
strncat(yy,xx[i],j); /*将字符串xx[i]中前j个字符连接到yy中*/
strcpy(xx[i],yy); /*将字符串yy重新拷贝到字符串xx[i]中去*/
j=0; /* 开始下一次的扫描。*/
}
}

相关库函数解释:
char *strncat(char *dest, const char *src, size_t maxlen)
功能:将字符串src中前maxlen个字符连接到dest中
相关头文件:string.h
char *strcpy(char *dest, const char *src)
功能:将字符串src拷贝到字符串dest中去
相关头文件:string.h

本文来源:https://shitiku.jxxyjl.com/shitijijin2/27200.html

Copyright @ 2011- 考试题库网 All Rights Reserved. 版权所有

免责声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。

 站长统计