计算机等级考试三级 C语言上机试题总汇
1.已知在文件IN.DAT中存有100个产品销售记录, 每个产品销售
记录由产品代码dm(字符型4位), 产品名称mc(字符型10位), 单价
dj(整型), 数量sl(整型), 金额je(长整型)五部分组成。 其中:
金额=单价*数量计算得出。函数ReadDat( )读取这100个销售记录
并存入结构数组sell中。请编制函数SortDat( ), 其功能要求:
按金额从小到大进行排列, 若金额相等, 则按产品代码从小到大
进行排列, 最终排列结果仍存入结构数组sell中。最后main( )函
数调用函数WriteDat()把结果输出到文件OUT1.DAT中。
注意: 部分源程序存放在PROG1.C中。
请勿改动主函数main( )、读数据函数ReadDat()和输出数据函
数WriteDat()的内容。
*/
#include <stdio.h>
#include <mem.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
#define MAX 100
typedef struct {
char dm[5] ; /* 产品代码 */
char mc[11] ; /* 产品名称 */
int dj ; /* 单价 */
int sl ; /* 数量 */
long je ; /* 金额 */
} PRO ;
PRO sell[MAX] ;
void ReadDat() ;
void WriteDat() ;
void SortDat()
{/**/
int i,j,k;
PRO tt;
for(i=0; i < MAX-1; i++)
{ k=i;
for(j=i+1; j < MAX; j++)
if((sell[k].je>sell[j].je)||(sell[k].je==sell[j].je)&&(strcmp(sell[k].dm, sell[j].dm)>0))
k=j;
if(k!=i)
{ tt=sell[k]; sell[k]=sell[i]; sell[i]=tt;}
}
/**/
}
void main()
{
memset(sell, 0, sizeof(sell)) ;
ReadDat() ;
SortDat() ;
WriteDat() ;
}
void ReadDat()
{
FILE *fp ;
char str[80], ch[11] ;
int i ;
fp = fopen("IN.DAT", "r") ;
for(i = 0 ; i < 100 ; i++) {
fgets(str, 80, fp) ;
memcpy(sell[i].dm, str, 4) ;
memcpy(sell[i].mc, str + 4, 10) ;
memcpy(ch, str + 14, 4) ; ch[4] = 0 ;
sell[i].dj = atoi(ch) ;
memcpy(ch, str + 18, 5) ; ch[5] = 0 ;
sell[i].sl = atoi(ch) ;
sell[i].je = (long)sell[i].dj * sell[i].sl ;
}
fclose(fp) ;
}
void WriteDat()
{
FILE *fp ;
int i ;
fp = fopen("OUT1.DAT", "w") ;
for(i = 0 ; i < 100 ; i++) {
fprintf(fp, "%s %s %4d %5d %10ld\n", sell[i].dm, sell[i].mc, sell[i].dj, sell[i].sl, sell[i].je) ;
}
fclose(fp) ;
}
100个产品销售记录排序其余9题说明
1) in.dat 内容完全相同。
2) 程序也基本雷同,仅排序的要求不一样。
3) 考生应熟悉 strcmp() 函数,否则有的题相当不好处理。
2.排序要求:
按金额从小到大进行排列, 若金额相等, 则按产品代码从大到小
进行排列
相应语句:
if(sell[k].je>sell[j].je|| (sell[k].je==sell[j].je) &&
(strcmp(sell[k].dm, sell[j].dm) < 0))
3.排序要求:
按金额从大到小进行排列, 若金额相等, 则按产品代码从小到大
进行排列
相应语句:
if((sell[k].je <
sell[j].je)||(sell[k].je==sell[j].je)&&(strcmp(sell[k].dm,
sell[j].dm)>0))
4.排序要求:
按金额从大到小进行排列, 若金额相等, 则按产品代码从大到小
进行排列
相应语句:
if((sell[k].je <
sell[j].je)||(sell[k].je==sell[j].je)&&(strcmp(sell[k].dm,sell[j].dm)<0))
5.排序要求:
按产品名称从小到大进行排列, 若产品名称相同, 则按金额从小
到大进行排列
相应语句:
if((strcmp(sell[k].mc,
sell[j].mc)>0)||(strcmp(sell[k].mc,sell[j].mc)==0)&&(sell[k].je>sell[j].je))
6.排序要求:
按产品名称从小到大进行排列, 若产品名称相同, 则按金额从大
到小进行排列
相应语句:
if(strcmp(sell[i].mc, sell[j].mc)>0 || (strcmp(sell[i].mc,
sell[j].mc)==0)&&(sell[i].je < sell[j].je))
7.排序要求:
按产品名称从大到小进行排列, 若产品名称相同, 则按金额从小
到大进行排列
相应语句:
if((strcmp(sell[k].mc, sell[j].mc) < 0) ||
(strcmp(sell[k].mc,sell[j].mc)==0)&&(sell[k].je>sell[j].je))
8.排序要求:
按产品名称从大到小进行排列, 若产品名称相同, 则按金额从大
到小进行排列
相应语句:
if((strcmp(sell[k].mc, sell[j].mc) < 0)|| (strcmp(sell[k].mc,
sell[j].mc)==0)&&(sell[k].je < sell[j].je))
9.排序要求:
按产品代码从小到大进行排列, 若产品代码相同, 则按金额从小
到大进行排列
相应语句:
if((strcmp(sell[k].dm,
sell[j].dm)>0)||(strcmp(sell[k].dm,sell[j].dm)==0)&&(sell[k].je>sell[j].je
code:
/*
已知在文件IN.DAT中存有100个产品销售记录, 每个产品销售
记录由产品代码dm(字符型4位), 产品名称mc(字符型10位), 单价
dj(整型), 数量sl(整型), 金额je(长整型)五部分组成。 其中:
金额=单价*数量计算得出。函数ReadDat( )读取这100个销售记
录并存入结构数组sell中。请编制函数SortDat( ), 其功能要求:
按产品代码从大到小进行排列, 若产品代码相同, 则按金额从大
到小进行排列, 最终排列结果仍存入结构数组sell中。最后main()
函数调用函数WriteDat()把结果输出到文件OUT10.DAT中。
注意: 部分源程序存放在PROG1.C中。
请勿改动主函数main( )、读数据函数ReadDat()和输出数据函
数WriteDat()的内容。
*/
#include <stdio.h>
#include <mem.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
#define MAX 100
typedef struct {
char dm[5] ; /* 产品代码 */
char mc[11] ; /* 产品名称 */
int dj ; /* 单价 */
int sl ; /* 数量 */
long je ; /* 金额 */
} PRO ;
PRO sell[MAX] ;
void ReadDat() ;
void WriteDat() ;
void SortDat()
{/**/
int i,j;
PRO tt;
for(i=0; i < MAX-1; i++)
for(j=i+1; j < MAX; j++)
{ if(strcmp(sell[i].dm, sell[j].dm) < 0)
{ tt=sell[i]; sell[i]=sell[j]; sell[j]=tt;}
if((strcmp(sell[i].dm, sell[j].dm)==0)&&(sell[i].je < sell[j].je))
{ tt=sell[i]; sell[i]=sell[j]; sell[j]=tt;}
}
/**/
}
void main()
{
memset(sell, 0, sizeof(sell)) ;
ReadDat() ;
SortDat() ;
WriteDat() ;
}
void ReadDat()
{
FILE *fp ;
char str[80], ch[11] ;
int i ;
fp = fopen("IN.DAT", "r") ;
for(i = 0 ; i < 100 ; i++) {
fgets(str, 80, fp) ;
memcpy(sell[i].dm, str, 4) ;
memcpy(sell[i].mc, str + 4, 10) ;
memcpy(ch, str + 14, 4) ; ch[4] = 0 ;
sell[i].dj = atoi(ch) ;
memcpy(ch, str + 18, 5) ; ch[5] = 0 ;
sell[i].sl = atoi(ch) ;
sell[i].je = (long)sell[i].dj * sell[i].sl ;
}
fclose(fp) ;
}
void WriteDat()
{
FILE *fp ;
int i ;
fp = fopen("OUT10.DAT", "w") ;
for(i = 0 ; i < 100 ; i++) {
fprintf(fp, "%s %s %4d %5d %10ld\n", sell[i].dm, sell[i].mc, sell[i].dj, sell[i].sl, sell[i].je) ;
}
fclose(fp) ;
}
300个四位数问题(此类共10题)
本类10题中,五题产生数组B,并对B按一定要求排序;
其余五题是求平均值。我把它们分成两组五题来讨论。
以下为产生数组B之题一:
code:
/*
已知数据文件IN.DAT中存有300个四位数, 并已调用读函数
readDat()把这些数存入数组a中, 请编制一函数jsvalue(),其功
能是: 求出个位数上的数减千位数上的数减百位数上的数减十位
数上的数大于零的个数cnt, 再把所有满足此条件的四位数依次
存入数组b中, 然后对数组b的四位数按从大到小的顺序进行排序。
最后main( )函数调用写函数writeDat()把数组b中的数输出到文
件OUT.DAT。
例如: 1239, 9-1-2-3>0, 则该数满足条件存入数组b中, 且
个数cnt=cnt+1。
8129, 9-8-1-2<0, 则该数不满足条件忽略。
注意: 部分源程序存在文件PROG1.C文件中。
程序中已定义数组: a[300], b[300], 已定义变量: cnt
请勿改动数据文件IN.DAT中的任何数据、主函数main()、读
函数readDat()和写函数writeDat()的内容。
*/
#include <stdio.h>
int a[300], b[300], cnt=0 ;
jsvalue()
{/**/
int i,j,k,t;
for(i=0; i < 300; i++)
if(a[i]%10-a[i]/1000-a[i]/100%10-a[i]/10%10>0)
b[cnt++]=a[i];
for(i=0; i < cnt-1; i++)
{ k=i;
for(j=i+1; j < cnt; j++)
if(b[k] < b[j]) k=j;
if(k!=i) { t=b[k]; b[k]=b[i]; b[i]=t; }
}
/**/
}
main()
{
int i ;
readDat() ;
jsvalue() ;
writeDat() ;
printf("cnt=%d\n", cnt) ;
for(i = 0 ; i < cnt ; i++) printf("b[%d]=%d\n", i, b[i]) ;
}
readDat()
{
FILE *fp ;
int i ;
fp = fopen("in.dat","r") ;
for(i = 0 ; i < 300 ; i++) fscanf(fp, "%d,", &a[i]) ;
fclose(fp) ;
}
writeDat()
{
FILE *fp ;
int i ;
fp = fopen("out.dat","w") ;
fprintf(fp, "%d\n", cnt) ;
for(i = 0 ; i < cnt ; i++) fprintf(fp, "%d\n", b[i]) ;
fclose(fp) ;
}
要求:
求出所有这些四位数是素数的个数cnt, 再把所有满足此
条件的四位数依次存入数组b中, 然后对数组b的四位数按从小到
大的顺序进行排序。
out.dat 文件内容应当为:
30
1231
1277
1283
1319
2543
2609
3793
3911
4013
4549
4817
4933
5591
5843
5939
6733
6791
6841
6871
6917
7297
7369
7703
8101
8221
8941
9013
9461
9689
9887
之四
要求:
求出千位数上的数减百位数上的数减十位数上的数减个位
数上的数大于零的个数cnt, 再把所有满足此条件的四位数依次
存入数组b中, 然后对数组b的四位数按从小到大的顺序进行排序。
out.dat 文件内容应当为:
20
4002
4102
5111
5400
6014
6302
7050
7105
7113
8101
8130
8203
8221
8303
8700
9013
9016
9052
9053
9800
之五
要求:
求出千位数上的数加百位数上的数等于十位数上的数加个
位数上的数的个数cnt, 再把所有满足此条件的四位数依次存入
数组b中, 然后对数组b的四位数按从大到小的顺序进行排序。
out.dat 文件内容应当为:
22
7795
7429
7328
7153
6978
6767
6556
6226
5591
5555
5427
4509
4013
3672
3663
3205
3131
2855
2763
2543
2507
1625
求满足条件的数的个数、平均值 ……
求满足条件的数的个数、平均值及不满足条件的数的平均
值等,此类也是五题。本处仅给出一个全题,其余题只给出不同
之处。
code:
/*
已知数据文件IN.DAT中存有300个四位数, 并已调用读函数
readDat()把这些数存入数组a中, 请编制一函数jsvalue(),其功
能是: 求出千位数上的数减百位数上的数减十位数上的数减个位
数上的数大于零的个数cnt, 再求出所有满足此条件的四位数平
均值pjz1, 以及所有不满足此条件的四位数平均值pjz2。最后
main()函数调用写函数writeDat()把结果cnt,pjz1,pjz2输出到
OUT.DAT文件。
例如: 9123, 9-1-2-3>0, 则该数满足条件计算平均值pjz1,
且个数cnt=cnt+1。
9812, 9-8-1-2<0, 则该数不满足条件计算平均值pjz2。
注意: 部分源程序存在文件PROG1.C文件中。
程序中已定义数组: a[300], 已定义变量: cnt,pjz1,pjz2
请勿改动数据文件IN.DAT中的任何数据、主函数main()、读
函数readDat()和写函数writeDat()的内容。
*/
#include
int a[300], cnt=0 ;
double pjz1=0.0, pjz2=0.0 ;
jsvalue()
{/**/
int i;
for(i=0; i < 300; i++)
if(a[i]/1000-a[i]/100%10-a[i]/10%10-a[i]%10>0)
{ cnt++; pjz1+=a[i]; }
else pjz2+=a[i];
if(cnt) pjz1/=cnt;
if(cnt<300) pjz2/=300-cnt;
/**/
}
main()
{
int i ;
readDat() ;
jsvalue() ;
writeDat() ;
printf("cnt=%d\n满足条件的平均值pzj1=%7.2lf\n不满足条件的平均值pzj2=%7.2lf\n", cnt,pjz1,pjz2);
}
readDat()
{
FILE *fp ;
int i ;
fp = fopen("in.dat","r") ;
for(i = 0 ; i < 300 ; i++) fscanf(fp, "%d,", &a[i]) ;
fclose(fp) ;
}
writeDat()
{
FILE *fp ;
int i ;
fp = fopen("out.dat","w") ;
fprintf(fp, "%d\n%7.2lf\n%7.2lf\n", cnt, pjz1, pjz2) ;
fclose(fp) ;
}
in.dat 与前面给出的完全相同,out.dat 内容应当如下:
20
7389.55
5524.03
满足条件数的平均数 ……四题之说明
之二
要求:
求出千位数上的数加百位数上的数等于十位数上的数加个
位数上的数的个数cnt, 再求出所有满足此条件的四位数平均值
pjz1, 以及所有不满足此条件的四位数平均值pjz2。
正确程序生成的 out.dat 文件内容应当如下:
22
4876.86
5709.46
之三
要求:
求出个位数上的数减千位数上的数减百位数上的数减十位
数上的数大于零的个数cnt, 再求出所有满足此条件的四位数平
均值pjz1, 以及所有不满足此条件的四位数平均值pjz2。
正确程序生成的 out.dat 文件内容应当如下:
12
2926.08
5761.83
之四
要求:
求出所有这些四位数是素数的个数cnt, 再求出所有满足
此条件的四位数平均值pjz1, 以及所有不满足此条件的四位数平
均值pjz2。
正确程序生成的 out.dat 文件内容应当如下:
30
5782.87
5633.46
之五
要求:
求出千位数上的数加个位数上的数等于百位数上的数加十
位数上的数的个数cnt, 再求出所有满足此条件的四位数平均值
pjz1, 以及所有不满足此条件的四位数平均值pjz2。
正确程序生成的 out.dat 文件内容应当如下:
18
6681.22
5582.48
200个四位数(此类共10题)
此部分题与300个数的题有相似之处。
之一
code:
/*
已知数据文件IN.DAT中存有200个四位数, 并已调用读函数
readDat()把这些数存入数组a中,请考生编制一函数jsVal(),其
功能是: 把千位数字和十位数字重新组成一个新的十位数ab(新
十位数的十位数字是原四位数的千位数字,新十位数的个位数字
是原四位数的十位数字), 以及把个位数字和百位数字组成另一
个新的十位数cd (新十位数的十位数字是原四位数的个位数字,
新十位数的个位数字是原四位数的百位数字), 如果新组成的两
个十位数ab>cd, ab必须是偶数且能被5整除, cd必须是奇数,同
时两个新数的十位数字均不为零,则将满足此条件的四位数按从
大到小的顺序存入数组b中, 并要计算满足上述条件的四位数的
个数cnt。最后main()函数调用写函数writeDat( )把结果cnt以
及数组b中符合条件的四位数输出到OUT.DAT文件中。
注意: 部分源程序存在文件PROG1.C文件中。
程序中已定义数组: a[200], b[200], 已定义变量: cnt
请勿改动数据文件IN.DAT中的任何数据、主函数main()、读
函数readDat()和写函数writeDat()的内容。
*/
#include <stdio.h>
#define MAX 200
int a[MAX], b[MAX], cnt = 0 ;
void jsVal()
{/**/
int i,j,k,A,B,C,D;
for(i=0; i < MAX; i++)
{ A=a[i]/1000; B=a[i]/10%10; C=a[i]%10; D=a[i]/100%10;
if(A&&C&&(B==0)&&(D%2)&&(10*A>10*C+D))
b[cnt++]=a[i];
}
for(i=0; i < cnt-1; i++)
{ k=i;
for(j=i+1; j < cnt; j++)
if(b[k] < b[j]) k=j;
if(k!=i) {A=b[k]; b[k]=b[i]; b[i]=A;}
}
/**/
}
void readDat()
{
int i ;
FILE *fp ;
fp = fopen("in.dat", "r") ;
for(i = 0 ; i < MAX ; i++) fscanf(fp, "%d", &a[i]) ;
fclose(fp) ;
}
void main()
{
int i ;
readDat() ;
jsVal() ;
printf("满足条件的数=%d\n", cnt) ;
for(i = 0 ; i < cnt ; i++) printf("%d ", b[i]) ;
printf("\n") ;
writeDat() ;
}
writeDat()
{
FILE *fp ;
int i ;
fp = fopen("out.dat", "w") ;
fprintf(fp, "%d\n", cnt) ;
for(i = 0 ; i < cnt ; i++) fprintf(fp, "%d\n", b[i]) ;
fclose(fp) ;
200个四位数题之其余九题说明
之二
要求:
把千位数字和十位数字重新组成一个新的十位数(新十
位数的十位数字是原四位数的千位数字,新十位数的个位数字是
原四位数的十位数字), 以及把个位数字和百位数字组成另一个
新的十位数(新十位数的十位数字是原四位数的个位数字, 新十
位数的个位数字是原四位数的百位数字), 如果新组成的两个十
位数均是素数且新数的十位数字均不为零,则将满足此条件的四
位数按从大到小的顺序存入数组b中, 并要计算满足上述条件的
四位数的个数cnt。
out.dat 的内容应当为:
10
9971
8398
7711
6375
4719
4173
2736
2398
2397
1997
之三
要求:
把个位数字和千位数字重新组成一个新的十位数(新十
位数的十位数字是原四位数的个位数字,新十位数的个位数字是
原四位数的千位数字), 以及把百位数字和十位数字组成另一个
新的十位数(新十位数的十位数字是原四位数的百位数字, 新十
位数的个位数字是原四位数的十位数字), 如果新组成的两个十
位数必须是一个奇数,另一个为偶数并且两个十位数中至少有一
个数能被17整除,同时两个新数的十位数字均不为零, 则将满足
此条件的四位数按从大到小的顺序存入数组b中, 并要计算满足
上述条件的四位数的个数cnt。
out.dat 的内容应当为:
7
8656
8174
7781
7683
4313
4173
2513
之四
要求:
)把这些数存入数组a中,请考生编制一函数jsVal(),其
功能是: 把千位数字和十位数字重新组成一个新的十位数ab(新
十位数的十位数字是原四位数的千位数字,新十位数的个位数字
是原四位数的十位数字), 以及把个位数字和百位数字组成另一
个新的十位数cd (新十位数的十位数字是原四位数的个位数字,
新十位数的个位数字是原四位数的百位数字), 如果新组成的两
个十位数ab-cd>=10且ab-cd<=20且两个数均是偶数,同时两个新
数的十位数字均不为零,则将满足此条件的四位数按从大到小的
顺序存入数组b中, 并要计算满足上述条件的四位数的个数cnt。
out.dat 的内容应当为:
5
9068
5224
5024
3821
2281
之五
要求:
如果四位数各位上的数字均是0或2或4或6或8, 则统计
出满足此条件的个数cnt, 并把这些四位数按从大到小的顺序存
入数组b中。
out.dat 的内容应当为:
11
8448
6820
4488
4060
2888
2884
2686
2624
2484
2420
2042
之六
要求:
把千位数字和个位数字重新组成一个新的十位数(新十
位数的十位数字是原四位数的千位数字,新十位数的个位数字是
原四位数的个位数字), 以及把百位数字和十位数字组成另一个
新的十位数(新十位数的十位数字是原四位数的百位数字, 新十
位数的个位数字是原四位数的十位数字), 如果新组成的两个十
位数均是奇数并且两个十位数中至少有一个数能被5整除, 同时
两个新数的十位数字均不为零,则将满足此条件的四位数按从大
到小的顺序存入数组b中, 并要计算满足上述条件的四位数的个
数cnt。
out.dat 的内容应当为:
10
9851
8275
6375
5755
5359
4951
4595
4395
3575
3135
之七
要求:
把个位数字和千位数字重新组成一个新的十位数(新十
位数的十位数字是原四位数的个位数字,新十位数的个位数字是
原四位数的千位数字), 以及把百位数字和十位数字组成另一个
新的十位数(新十位数的十位数字是原四位数的百位数字, 新十
位数的个位数字是原四位数的十位数字), 如果新组成的两个十
位数均是偶数并且两个十位数中至少有一个数能被9整除, 同时
两个新数的十位数字均不为零,则将满足此条件的四位数按从大
到小的顺序存入数组b中, 并要计算满足上述条件的四位数的个
数cnt。
out.dat 的内容应当为:
13
8761
8724
8441
6722
6603
6545
6323
6181
4369
4285
4125
2724
2362
之八
要求:
把千位数字和十位数字重新组成一个新的十位数ab(新
十位数的十位数字是原四位数的千位数字,新十位数的个位数字
是原四位数的十位数字), 以及把个位数字和百位数字组成另一
个新的十位数cd (新十位数的十位数字是原四位数的个位数字,
新十位数的个位数字是原四位数的百位数字), 如果新组成的两
个十位数ab 同时两个新数的十位数字均不为零,则将满足此条件的四位数按
从大到小的顺序存入数组b中, 并要计算满足上述条件的四位数
的个数cnt。
out.dat 的内容应当为:
12
7878
5437
3897
2893
2877
2438
2039
2035
2033
1619
1494
1493
之九
要求:
如果四位数各位上的数字均是奇数,则统计出满足此条
件的个数cnt并把这些四位数按从大到小的顺序存入数组b中。
out.dat 的内容应当为:
13
9971
7973
7711
7511
5755
5359
5311
3575
3537
3135
1997
1979
1531
之十
要求:
把千位数字和十位数字重新组成一个新的十位数ab(新
十位数的十位数字是原四位数的千位数字,新十位数的个位数字
是原四位数的十位数字), 以及把个位数字和百位数字组成另一
个新的十位数cd (新十位数的十位数字是原四位数的个位数字,
新十位数的个位数字是原四位数的百位数字), 如果新组成的两
个十位数ab-cd>=0且ab-cd<=10且两个数均是奇数, 同时两个新
数的十位数字均不为零,则将满足此条件的四位数按从大到小的
顺序存入数组b中, 并要计算满足上述条件的四位数的个数cnt。
out.dat 的内容应当为:
5
8398
7996
5954
4313
2311
小于 200个四位数之一(共四题)
code:
/*
已知在文件IN.DAT中存有若干个(个数<200)四位数字的正整
数, 函数ReadDat( )读取这若干个正整数并存入数组xx中。请编
制函数Calvalue( ), 其功能要求: 1. 求出这文件中共有多少个
正整数totNum; 2.求出这些数中的各位数字之和是奇数的数的个
数totCnt, 以及满足此条件的这些数的算术平均值totPjz。最后
main( )函数调用函数WriteDat()把所求的结果输出到OUT.DAT文
件中。
注意: 部分源程序存放在PROG1.C中。
请勿改动数据文件IN.DAT中的任何数据,主函数main()、读
数据函数ReadDat()和输出数据函数WriteDat()的内容。
*/
#include <stdio.h>
#include <conio.h>
#define MAXNUM 200
int xx[MAXNUM] ;
int totNum = 0 ; /* 文件IN.DAT中共有多少个正整数 */
int totCnt = 0 ; /* 符合条件的正整数的个数 */
double totPjz = 0.0 ; /* 平均值 */
int ReadDat(void) ;
void WriteDat(void) ;
void Calvalue(void)
{/**/
while(xx[totNum])
{ if((xx[totNum]/1000+xx[totNum]/100%10+xx[totNum]/10%10+xx[totNum])%2)
{ totCnt++; totPjz+=xx[totNum]; }
totNum++;
}
if(totCnt) totPjz/=totCnt;
/**/
}
void main()
{
int i ;
clrscr() ;
for(i = 0 ; i < MAXNUM ; i++) xx[i] = 0 ;
if(ReadDat()) {
printf("数据文件IN.DAT不能打开!\007\n") ;
return ;
}
Calvalue() ;
printf("文件IN.DAT中共有正整数=%d个\n", totNum) ;
printf("符合条件的正整数的个数=%d个\n", totCnt) ;
printf("平均值=%.2lf\n", totPjz) ;
WriteDat() ;
}
int ReadDat(void)
{
FILE *fp ;
int i = 0 ;
if((fp = fopen("in.dat", "r")) == NULL) return 1 ;
while(!feof(fp)) {
fscanf(fp, "%d,", &xx[i++]) ;
}
fclose(fp) ;
return 0 ;
}
void WriteDat(void)
{
FILE *fp ;
fp = fopen("OUT.DAT", "w") ;
fprintf(fp, "%d\n%d\n%.2lf\n", totNum, totCnt, totPjz) ;
fclose(fp) ;
}
in.dat 文件内容如下:
6045,6192,1885,3580,8544,6826,5493,8415,3132,5841,
6561,3173,9157,2895,2851,6082,5510,9610,5398,5273,
3438,1800,6364,6892,9591,3120,8813,2106,5505,1085,
5835,7295,6131,9405,6756,2413,6274,9262,5728,2650,
6266,5285,7703,1353,1510,2350,4325,4392,7573,8204,
7358,6365,3135,9903,3055,3219,3955,7313,6206,1631,
5869,5893,4569,1251,2542,5740,2073,9805,1189,7550,
4362,6214,5680,8753,8443,3636,4495,9643,3782,5556,
1018,9729,8588,2797,4321,4714,9658,8997,2080,5912,
9968,5558,9311,7047,6138,7618,5448,1466,7075,2166,
4025,3572,9605,1291,6027,2358,1911,2747,7068,1716,
9661,5849,3210,2554,8604,8010,7947,3685,2945,4224,
7014,9058,6259,9503,1615,1060,7787,8983,3822,2471,
5146,7066,1029,1777,7788,2941,3538,2912,3096,7421,
9175,6099,2930,4685,8465,8633,2628,7155,4307,9535,
4274,2857,6829,6226,8268,9377,9415,9059,4872,6072,
out.dat 文件内容应当如下:
160
69
5460.51
小于 200个四位数之二(共四题)
code:
/*
已知在文件IN.DAT中存有若干个(个数<200)四位数字的正整
数, 函数ReadDat( )读取这若干个正整数并存入数组xx中。请编
制函数Calvalue( ), 其功能要求: 1. 求出这文件中共有多少个
正整数totNum; 2.求出这些数中的各位数字之和是偶数的数的个
数totCnt, 以及满足此条件的这些数的算术平均值totPjz。最后
main( )函数调用函数WriteDat()把所求的结果输出到OUT.DAT文
件中。
注意: 部分源程序存放在PROG1.C中。
请勿改动数据文件IN.DAT中的任何数据,主函数main()、读
数据函数ReadDat()和输出数据函数WriteDat()的内容。
*/
#include
#include
#define MAXNUM 200
int xx[MAXNUM] ;
int totNum = 0 ; /* 文件IN.DAT中共有多少个正整数 */
int totCnt = 0 ; /* 符合条件的正整数的个数 */
double totPjz = 0.0 ; /* 平均值 */
int ReadDat(void) ;
void WriteDat(void) ;
void Calvalue(void)
{/**/
for(; xx[totNum]; totNum++)
if((xx[totNum]/1000+xx[totNum]/100%10+xx[totNum]/10%10+xx[totNum]%10)%2==0)
{ totCnt++; totPjz+=xx[totNum];}
if(totCnt) totPjz/=totCnt;
/**/
}
void main()
{
int i ;
clrscr() ;
for(i = 0 ; i < MAXNUM ; i++) xx[i] = 0 ;
if(ReadDat()) {
printf("数据文件IN.DAT不能打开!\007\n") ;
return ;
}
Calvalue() ;
printf("文件IN.DAT中共有正整数=%d个\n", totNum) ;
printf("符合条件的正整数的个数=%d个\n", totCnt) ;
printf("平均值=%.2lf\n", totPjz) ;
WriteDat() ;
}
int ReadDat(void)
{
FILE *fp ;
int i = 0 ;
if((fp = fopen("in.dat", "r")) == NULL) return 1 ;
while(!feof(fp)) {
fscanf(fp, "%d,", &xx[i++]) ;
}
fclose(fp) ;
return 0 ;
}
void WriteDat(void)
{
FILE *fp ;
fp = fopen("OUT.DAT", "w") ;
fprintf(fp, "%d\n%d\n%.2lf\n", totNum, totCnt, totPjz) ;
fclose(fp) ;
}
out.dat 文件内容应当如下:
160
91
5517.16
200个四位数之三(共四题)
code:
/*
已知在文件IN.DAT中存有若干个(个数<200)四位数字的正整
数, 函数ReadDat( )读取这若干个正整数并存入数组xx中。请编
制函数Calvalue( ), 其功能要求: 1. 求出这文件中共有多少个
正整数totNum; 2. 求这些数右移1位后, 产生的新数是奇数的数
的个数totCnt, 以及满足此条件的这些数(右移前的值)的算术平
均值totPjz。最后main()函数调用函数WriteDat()把所求的结果
输出到文件OUT.DAT中。
注意: 部分源程序存放在PROG1.C中。
请勿改动数据文件IN.DAT中的任何数据,主函数main()、读
数据函数ReadDat()和输出数据函数WriteDat()的内容。
*/
#include
#include
#define MAXNUM 200
int xx[MAXNUM] ;
int totNum = 0 ; /* 文件IN.DAT中共有多少个正整数 */
int totCnt = 0 ; /* 符合条件的正整数的个数 */
double totPjz = 0.0 ; /* 平均值 */
int ReadDat(void) ;
void WriteDat(void) ;
void Calvalue(void)
{/**/
for(; xx[totNum]; totNum++)
if((xx[totNum]>>1)%2)
{ totCnt++; totPjz+=xx[totNum];}
if(totCnt) totPjz/=totCnt;
/**/
}
void main()
{
int i ;
clrscr() ;
for(i = 0 ; i < MAXNUM ; i++) xx[i] = 0 ;
if(ReadDat()) {
printf("数据文件IN.DAT不能打开!\007\n") ;
return ;
}
Calvalue() ;
printf("文件IN.DAT中共有正整数=%d个\n", totNum) ;
printf("符合条件的正整数的个数=%d个\n", totCnt) ;
printf("平均值=%.2lf\n", totPjz) ;
WriteDat() ;
}
int ReadDat(void)
{
FILE *fp ;
int i = 0 ;
if((fp = fopen("in.dat", "r")) == NULL) return 1 ;
while(!feof(fp)) {
fscanf(fp, "%d,", &xx[i++]) ;
}
fclose(fp) ;
return 0 ;
}
void WriteDat(void)
{
FILE *fp ;
fp = fopen("OUT.DAT", "w") ;
fprintf(fp, "%d\n%d\n%.2lf\n", totNum, totCnt, totPjz) ;
fclose(fp) ;
}
out.dat 文件内容应当如下:
160
80
5537.54
小于 200个四位数之四(共四题)
code:
/*
已知在文件IN.DAT中存有若干个(个数<200)四位数字的正整
数, 函数ReadDat( )读取这若干个正整数并存入数组xx中。请编
制函数Calvalue( ), 其功能要求: 1. 求出这文件中共有多少个
正整数totNum; 2. 求这些数右移1位后, 产生的新数是偶数的数
的个数totCnt, 以及满足此条件的这些数(右移前的值)的算术平
均值totPjz。最后main()函数调用函数WriteDat()把所求的结果
输出到文件OUT.DAT中。
注意: 部分源程序存放在PROG1.C中。
请勿改动数据文件IN.DAT中的任何数据,主函数main()、读
数据函数ReadDat()和输出数据函数WriteDat()的内容。
*/
#include
#include
#define MAXNUM 200
int xx[MAXNUM] ;
int totNum = 0 ; /* 文件IN.DAT中共有多少个正整数 */
int totCnt = 0 ; /* 符合条件的正整数的个数 */
double totPjz = 0.0 ; /* 平均值 */
int ReadDat(void) ;
void WriteDat(void) ;
void Calvalue(void)
{/**/
for(; xx[totNum]>0; totNum++)
if((xx[totNum]>>1)%2==0)
{ totCnt++; totPjz+=xx[totNum]; }
if(totCnt) totPjz/=totCnt;
/**/
}
void main()
{
int i ;
clrscr() ;
for(i = 0 ; i < MAXNUM ; i++) xx[i] = 0 ;
if(ReadDat()) {
printf("数据文件IN.DAT不能打开!\007\n") ;
return ;
}
Calvalue() ;
printf("文件IN.DAT中共有正整数=%d个\n", totNum) ;
printf("符合条件的正整数的个数=%d个\n", totCnt) ;
printf("平均值=%.2lf\n", totPjz) ;
WriteDat() ;
}
int ReadDat(void)
{
FILE *fp ;
int i = 0 ;
if((fp = fopen("in.dat", "r")) == NULL) return 1 ;
while(!feof(fp)) {
fscanf(fp, "%d,", &xx[i++]) ;
}
fclose(fp) ;
return 0 ;
}
void WriteDat(void)
{
FILE *fp ;
fp = fopen("OUT.DAT", "w") ;
fprintf(fp, "%d\n%d\n%.2lf\n", totNum, totCnt, totPjz) ;
fclose(fp) ;
}
out.dat 文件内容应当如下:
160
80
5447.93
英文文章 ——字符串处理(共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;
/****老王注:此题的关键是定义 char m 。记得往年的考试类似题可以不必定义,
如果要定义的话,则必须定义为 int 结果才能正确。看来理解出题
者的意图是机试的难点之一。 ****/
for(i=0; i < maxline; i++)
for(j=0; j < strlen(xx[i]); j++)
{ m=xx[i][j]<<4;
if((m>32)&&(m<=100))
xx[i][j]+=m;
}
/**/
}
void main()
{
clrscr() ;
if(ReadDat()) {
printf("数据文件IN.DAT不能打开!\n\007") ;
return ;
}
StrCharJL() ;
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("OUT7.DAT", "w") ;
for(i = 0 ; i < maxline ; i++) {
printf("%s\n", xx[i]) ;
fprintf(fp, "%s\n", xx[i]) ;
}
fclose(fp) ;
}
字符串处理之五
code:
/*
函数ReadDat( )实现从文件IN.DAT中读取一篇英文文章存入到
字符串数组xx中; 请编制函数StrCharJR( ), 其函数的功能是: 以
行为单位把字符串中的所有字符的ASCII值右移4位, 然后把右移后
的字符ASCII值再加上原字符的ASCII值, 得到新的字符仍存入原字
符串对应的位置上,之后把已处理的字符串仍按行重新存入字符串
数组xx中。最后main()函数调用函数WriteDat()把结果xx输出到文
件OUT8.DAT中。
原始数据文件存放的格式是: 每行的宽度均小于80个字符, 含
标点符号和空格。
注意: 部分源程序存放在PROG1.C中。
请勿改动主函数main( )、读数据函数ReadDat()和输出数据函
数WriteDat()的内容。
*/
#include
#include
#include
char xx[50][80] ;
int maxline = 0 ; /* 文章的总行数 */
int ReadDat(void) ;
void WriteDat(void) ;
void StrCharJR(void)
{/**/
int i,j;
for(i=0; i
for(j=0; j
xx[i][j]+=xx[i][j]>>4;
/**/
}
void main()
{
clrscr() ;
if(ReadDat()) {
printf("数据文件IN.DAT不能打开!\n\007") ;
return ;
}
StrCharJR() ;
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("OUT8.DAT", "w") ;
for(i = 0 ; i < maxline ; i++) {
printf("%s\n", xx[i]) ;
fprintf(fp, "%s\n", xx[i]) ;
}
fclose(fp) ;
}
out8.dat 文件内容应当如下:
^u|"igt"iykg{k"gt"otjk"ut"gt?"lokrj."ut"zk}kygr"lokrjz"{u"hk"|zkj
{umk{nky."uy"ut"wgy{z"{nkykul."{ng{"?u|"~gt{"{u"|zk"gz"g"qk?0"Ynk
qk?z"ot"otjkkz"grru~"?u|"x|oiq"giikzz"{u"zwkioloi"ykiuyjz"gtj"jklotk
uyjkyz"luy"zkx|kt{ogr"wyuikzzotm"ul"g"MXEQ"lork0"El{ky"?u|"tu"rutmky
tkkj"gt"otjk."?u|"igt"jkrk{k"o{0"Ejjo{out"gtj"otjkkz"ng}k"tu"kllki{
ut"{nk"jg{g"ykiuyjz"uy"ut"u{nky"otjkkz0
^u|"sg?"~gt{"g"lokrj"ot"lokrj"ot"kgin"ykiuyj"{u"|tox|kr?"ojkt{ol?"{ng{
ykiuyj"lyus"grr"u{nky"ykiuyjz"ot"{nk"lork0"Juy"kgswrk."{nk"Iswru?kk
R|shky"lokrj"oz"|tox|k"ol"?u|"ju"tu{"gzzomt"{nk"zgsk"t|shky"{u"{~u
jollkykt{"kswru?kkz."gtj"?u|"tk}ky"ykgzzomt"{nkzk"t|shkyz"{u"u{nky
kswru?kkz0"Ml"?u|"~ozn"{u"lotj"uy"sujol?"{nk"ykiuyj"hkrutmotm"{u"g
zwkioloi"kswru?kk."{noz"|tox|k"lokrj"zg}kz"{nk"{nu|hrk"ul"jk{kysototm
~nk{nky"?u|"ng}k"{nk"iuyyki{"ykiuyj0
Ml"?u|"ju"tu{"ng}k"g"|tox|k"lokrj."?u|"s|z{"lotj"{nk"loyz{"ykiuyj
{nk"sg{inkz"?u|y"qk?"gtj"jk{kysotk"~nk{nky"{nk"ykiuyj"oz"{nk"utk"?u|
~gt{0"Ml"o{"oz"tu{"{nk"iuyyki{"utk."?u|"s|z{"zkgyin"gmgot"{u"lotj"u{nkyz0
Ml"?u|"qtu~"{ng{"?u|"ng}k"g"|tox|k"lokrj"~o{not"?u|y"ykiuyjz."?u|
igt"otir|jk"{noz"lgi{"ot"{nk"qk?"jkziyow{out."gtj"MXEQ"~orr"grru~"utr?
|tox|k"qk?z0"Juy"kgswrk."ol"?u|"zwkiol?"{ng{"{nk"kswru?kk"t|shkyz"gyk
|tox|k."MXEQ"utr?"rk{z"?u|"gjj"ykiuyjz"{u"{nk"lork"luy."uy"ingtmk
t|shkyz"{u."kswru?kk"t|shkyz"{ng{"ju"tu{"grykgjr?"koz{"ot{"lork0
字符串处理之六
code:
/*
函数ReadDat( )实现从文件IN.DAT中读取一篇英文文章存入到
字符串数组xx中; 请编制函数StrOL( ), 其函数的功能是: 以行为
单位对行中以空格或标点符号为分隔的所有单词进行倒排,同时去
除标点符号,之后把已处理的字符串(应不含标点符号)仍按行重新
存入字符串数组xx中。最后main()函数调用函数WriteDat()把结果
xx输出到文件OUT6.DAT中。
例如: 原文: You He Me
I am a student.
结果: Me He You
student a am I
原始数据文件存放的格式是: 每行的宽度均小于80个字符, 含
标点符号和空格。
注意: 部分源程序存放在PROG1.C中。
请勿改动主函数main( )、读数据函数ReadDat()和输出数据函
数WriteDat()的内容。
**** 同1998年3B第六题 ****
*/
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>
char xx[50][80] ;
int maxline = 0 ; /* 文章的总行数 */
int ReadDat(void) ;
void WriteDat(void) ;
void StrOL(void)
{/**/
int i,j,k,m,n,ll;
char yy[80];
for(i=0; i < maxline; i++)
{ ll=strlen(xx[i]); k=n=0;
for(j=ll-1; j>=0; j--)
{ if(isalpha(xx[i][j])) k++;
else
{ for(m=1; m<=k; m++)
yy[n++]=xx[i][j+m];
k=0;
}
if(xx[i][j]==' ') yy[n++]=' ';
}
for(m=1; m<=k; m++)
yy[n++]=xx[i][j+m];
/* 上面两行处理每行的第一个单词。
如果漏写,结果显然不正确,但并不影响得分。 */
yy[n]=0;
strcpy(xx[i],yy);
}
/* 标准答案与此法结果相比,每行后面多一个空格。 */
}
void main()
{
clrscr() ;
if(ReadDat()) {
printf("数据文件IN.DAT不能打开!\n\007") ;
return ;
}
StrOL() ;
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("OUT6.DAT", "w") ;
for(i = 0 ; i < maxline ; i++) {
printf("%s\n", xx[i]) ;
fprintf(fp, "%s\n", xx[i]) ;
}
fclose(fp) ;
}
out6.dat 内容应当如下:
used be to fields several on field any on index an create can
You
The key a as use to want you that thereof parts on or together
define and records specific to access quick you allow indexes
in keys
longer no you After file ISAM a of processing sequential for
orders
effect no have indexes and Addition it delete can you index an
need
indexes other on or records data the on
that identify uniquely to record each in field in field a want
may You
Employee the example For file the in records other all from
record
two to number same the assign not do you if unique is field
Number
other to numbers these reassign never you and employees
different
a to belonging record the modify or find to wish you If
employees
determining of thouble the saves field unique this employee
specific
record correct the have you whether
record first the find must you field unique a have not do you
If
you one the is record the whether determine and key your
matches the
others find to again search must you one correct the not is it
If want
you records your within field unique a have you that know you
If
only allow will ISAM and description key the in fact this
include can
are numbers employee the that specify you if example For keys
unique
change or for file the to records add you lets only ISAM
unique
file int exist alreadly not do that numbers employee to
numbers
字符串处理之七
code:
/*
函数ReadDat( )实现从文件IN.DAT中读取一篇英文文章存入
到字符串数组xx中; 请编制函数ConvertCharD(), 其函数的功能
是: 以行为单位把字符串中的所有小写字母改写成该字母的上一
个字母, 如果是字母a, 则改写成字母z,大写字母和其它字符保
持不变。把已处理的字符串仍按行重新存入字符串数组xx中。最
后main()函数调用函数WriteDat()把结果xx输出到文件OUT4.DAT
中。
例: 原文: Adb.Bcdza
abck.LLhj
结果: Aca.Bbcyz
zabj.LLgi
原始数据文件存放的格式是: 每行的宽度均小于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 ConvertCharD(void)
{/**/
int i,j;
for(i=0; i < maxline; i++)
for(j=0; j < strlen(xx[i]); j++)
if(xx[i][j]=='a') xx[i][j]='z';
else if(islower(xx[i][j])) xx[i][j]-=1;
/**/
}
void main()
{
clrscr() ;
if(ReadDat()) {
printf("数据文件IN.DAT不能打开!\n\007") ;
return ;
}
ConvertCharD() ;
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("OUT4.DAT", "w") ;
for(i = 0 ; i < maxline ; i++) {
printf("%s\n", xx[i]) ;
fprintf(fp, "%s\n", xx[i]) ;
}
fclose(fp) ;
}
out4.dat 文件内容应当如下:
Ynt bzm bqdzsd zm hmcdw nm zmx ehdkc, nm rdudqzk ehdkcr sn ad
trdc
snfdsgdq, nq nm ozqsr sgdqdne, sgzs xnt vzms sn trd zr z jdx.
Tgd
jdxr hm hmcdwdr zkknv xnt pthbj zbbdrr sn rodbhehb qdbnqcr zmc
cdehmd
nqcdqr enq rdptdmshzk oqnbdrrhmf ne z ISAM ehkd. Aesdq xnt mn
knmfdq
mddc zm hmcdw, xnt bzm cdkdsd hs. Acchshnm zmc hmcdwdr gzud mn
deedbs
nm sgd czsz qdbnqcr nq nm nsgdq hmcdwdr.
Ynt lzx vzms z ehdkc hm ehdkc hm dzbg qdbnqc sn tmhptdkx
hcdmshex sgzs
qdbnqc eqnl zkk nsgdq qdbnqcr hm sgd ehkd. Fnq dwzlokd, sgd
Eloknxdd
Ntladq ehdkc hr tmhptd he xnt cn mns zrrhfm sgd rzld mtladq sn
svn
cheedqdms dloknxddr, zmc xnt mdudq qdzrrhfm sgdrd mtladqr sn
nsgdq
dloknxddr. Ie xnt vhrg sn ehmc nq lnchex sgd qdbnqc adknmfhmf
sn z
rodbhehb dloknxdd, sghr tmhptd ehdkc rzudr sgd sgntakd ne
cdsdqlhmhmf
vgdsgdq xnt gzud sgd bnqqdbs qdbnqc.
Ie xnt cn mns gzud z tmhptd ehdkc, xnt ltrs ehmc sgd ehqrs
qdbnqc
sgd lzsbgdr xntq jdx zmc cdsdqlhmd vgdsgdq sgd qdbnqc hr sgd
nmd xnt
vzms. Ie hs hr mns sgd bnqqdbs nmd, xnt ltrs rdzqbg zfzhm sn
ehmc nsgdqr.
Ie xnt jmnv sgzs xnt gzud z tmhptd ehdkc vhsghm xntq qdbnqcr,
xnt
bzm hmbktcd sghr ezbs hm sgd jdx cdrbqhoshnm, zmc ISAM vhkk
zkknv nmkx
tmhptd jdxr. Fnq dwzlokd, he xnt rodbhex sgzs sgd dloknxdd
mtladqr zqd
tmhptd, ISAM nmkx kdsr xnt zcc qdbnqcr sn sgd ehkd enq, nq
bgzmfd
mtladqr sn, dloknxdd mtladqr sgzs cn mns zkqdzckx dwhrs hms
ehkd.
字符串处理之八
code:
/*
函数ReadDat( )实现从文件IN.DAT中读取一篇英文文章存入到
字符串数组xx中; 请编制函数CharConvA( ), 其函数的功能是: 以
行为单位把字符串中的最后一个字符的ASCII值右移4位后加最后第
二个字符的ASCII值, 得到最后一个新的字符, 最后第二个字符的
ASCII值右移4位后加最后第三个字符的ASCII值,得到最后第二个新
的字符, 以此类推一直处理到第二个字符, 第一个字符的ASCII值
加原最后一个字符的ASCII值, 得到第一个新的字符, 得到的新字
符分别存放在原字符串对应的位置上,之后把已处理的字符串仍按
行重新存入字符串数组xx中。最后main()函数调用函数WriteDat()
把结果xx输出到文件OUT10.DAT中。
原始数据文件存放的格式是: 每行的宽度均小于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 CharConvA(void)
{/**/
int i,j,ll; char ch;
for(i=0; i < maxline; i++)
{ ll=strlen(xx[i]); ch=xx[i][ll-1];
for(j=ll-1; j; j--)
xx[i][j]=(xx[i][j]>>4)+xx[i][j-1];
xx[i][0]+=ch;
}
/**/
}
void main()
{
clrscr() ;
if(ReadDat()) {
printf("数据文件IN.DAT不能打开!\n\007") ;
return ;
}
CharConvA() ;
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("OUT10.DAT", "w") ;
for(i = 0 ; i < maxline ; i++) {
printf("%s\n", xx[i]) ;
fprintf(fp, "%s\n", xx[i]) ;
}
fclose(fp) ;
}
字符串处理之九
code:
/*
函数ReadDat( )实现从文件IN.DAT中读取一篇英文文章存入到
字符串数组xx中; 请编制函数StrOR( ), 其函数的功能是: 以行为
单位依次把字符串中所有小写字母o 左边的字符串内容移到该串的
右边存放, 然后并把小写字母o删除,余下的字符串内容移到已处理
字符串的左边存放,之后把已处理的字符串仍按行重新存入字符串
数组xx中。最后main()函数调用函数WriteDat()把结果xx输出到文
件OUT5.DAT中。
例如: 原文: You can create an index on any field.
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,j; char yy[80],*p;
for(i=0; i
for(j=0; j
if(xx[i][j]=='o')
{ p=&xx[i][j+1];
strcpy(yy,p);
strncat(yy,xx[i],j);
strcpy(xx[i],yy);
j=0;
}
/**/
}
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) ;
}
out5.dat 文件内容应当如下:
be usedYu can create an index n any field, n several fields t
use as a key. Thetgether, r n parts theref, that yu want t
rds and definekeys in indexes allw yu quick access t specific
rec
ngerrders fr sequential prcessing f a ISAM file. After yu n l
effectneed an index, yu can delete it. Additin and indexes
have n
ther indexes.n the data recrds r n
uniquely identify thatYu may want a field in field in each
recrd t
yeerecrd frm all ther recrds in the file. Fr example, the Empl
Number field is unique if yu d nt assign the same number t tw
therdifferent emplyees, and yu never reassign these numbers t
aemplyees. If yu wish t find r mdify the recrd belnging t
f determiningspecific emplyee, this unique field saves the
thuble
rd.whether yu have the crrect rec
rdIf yu d nt have a unique field, yu must find the first rec
uthe matches yur key and determine whether the recrd is the ne
y
thers.want. If it is nt the crrect ne, yu must search again t
find
uIf yu knw that yu have a unique field within yur recrds, y
nlycan include this fact in the key descriptin, and ISAM will
allw
yee numbers areunique keys. Fr example, if yu specify that the
empl
r changeunique, ISAM nly lets yu add recrds t the file fr,
t alreadly exist int file.numbers t, emplyee numbers that d n
字符串处理之 10
code:
/*
函数ReadDat( )实现从文件IN.DAT中读取一篇英文文章存入到
字符串数组xx中; 请编制函数ChA( ), 其函数的功能是: 以行为单
位把字符串中的第一个字符的ASCII值加第二个字符的ASCII值, 得
到第一个新的字符, 第二个字符的ASCII值加第三个字符的ASCII值,
得到第二个新的字符, 以此类推一直处理到最后第二个字符, 最后
一个字符的ASCII值加原第一个字符的ASCII值, 得到最后一个新的
字符, 得到的新字符分别存放在原字符串对应的位置上,之后把已
处理的字符串逆转后仍按行重新存入字符串数组xx中。最后main()
函数调用函数WriteDat()把结果xx输出到文件OUT9.DAT中。
原始数据文件存放的格式是: 每行的宽度均小于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 ChA(void)
{/**/
int i,j; char ch;
for(i=0; i < maxline; i++)
{ ch=xx[i][0];
for(j=0; j < strlen(xx[i])-1; j++)
xx[i][j]+=xx[i][j+1];
xx[i][j]+=ch;
strrev(xx[i]);
}
/**/
}
void main()
{
clrscr() ;
if(ReadDat()) {
printf("数据文件IN.DAT不能打开!\n\007") ;
return ;
}
ChA() ;
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("OUT9.DAT", "w") ;
for(i = 0 ; i < maxline ; i++) {
printf("%s\n", xx[i]) ;
fprintf(fp, "%s\n", xx[i]) ;
}
fclose(fp) ;
} |