2.举例
例1:函数ReadDat()实现从文件IN.DAT中读取一篇英文文章, 存入到字符串数组xx中; 请编制函数encryptChar(), 按给定的替代关系对数组xx中的所有字符进行替代, 仍存入数组xx的对应的位置上, 最后调用函数WriteDat()把结果xx输出到文件OUT.DAT中。
替代关系:f(p)=p*11 mod 256 (p是数组xx中某一个字符的ASCII值, f(p)是计算后新字符的ASCII值),如果计算后f(p)值小于等于32或大于130,则该字符不变, 否则将f(p)所对应的字符进行替代。
注意:部分源程序存放在PROG1.C中, 原始数据文件存放的格式是:每行的宽度均小于80个字符。
请勿改动主函数main()、读数据函数ReadDat()和输出数据函数WriteDat()的内容。部分源程序如下:
#include
#include
#include
#include
unsigned char xx[50][80];
int maxline=0 ; /* 文章的总行数 */
int ReadDat(void) ;
void WriteDat(void) ;
void encryptChar()
{
}
void main()
{
clrscr() ;
if(ReadDat()) {
printf(“数据文件ENG.IN不能打开!\n\007”) ;
return ; }
encryptChar() ;
WriteDat() ;
}
int ReadDat(void)
{
FILE *fp ;
int i=0 ;
unsigned char *p ;
if((fp=fopen(“n.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 ;
fp=fopen(“out.dat“, w) ;
for(i=0 ; i 《 maxline ; i+[ +) {
printf(“%s\n“, xx[i]) ;
fprintf(fp, “%s\n“, xx[i]) ;
}
fclose(fp) ;
}
分析:根据题意分析得出:本题主要考查考生的字符串指针或字符串数组以及根据公式进字符的ASCII值运算,再根据运算结果和条件进行相应的替代操作,要求考生仅编写函数encryptChar()实现转换功能。其中输入及输出函数给出且已调用,考生不必自己编写。
编写函数encryptChar()的程序内容如下:
void encryptChar()
{ int i, j, val ;
for(i=0 ; i
for(j=0 ; j
val=(xx[i][j]*11) % 256 ;
if(! (val《=32 || val》130)) xx[i][j]=val ;
}
}
数据文件IN.DAT内容如下:
You may WANT A FIELD in field in each record to uniquely identify that1234
record from all other records IN THE FILE. For example, the Employee123456
Number field is unique if you DO NOT ASSIGN the same number to two12345678
different employees, and you never reassign THESE NUMBERS to other12345678
employees. If you wish to FIND OR MODIFY the record belonging to a11111111
specific employee, this unique FIELD SAVES the thouble of determining22222
whether you have the correct record.12345678901234567
If you do not have a unique field, YOU MUST FIND THE first record123456787
the matches your key and determine whether THEN RECORD is the one you33333
want. If it is not the CORRECT ONE, you must search again to find others.4
结果文件OUT.DAT内容如下:
Youm+3WAZTAF#EDDinbiWlLinW+AxrWAorLtouniquWl3iLWntib3tx+t1&1《rWAorLbrom+llotxWrrWAorLs#ZTHEF#DE.ForW(+mplW,txWEmplo3WW1&1
例2: 文件in.dat中有200个正整数,且每个数均在1000至9999之间。函数readDat()是读取这200个数存放到数组aa中。请编制函数jsSort(),其函数的功能是:要求按每个数的后三位的大小进行升序排列,将排序后的前10个数存入数组b中,如果数组b中出现后三位相等的数值,则对这些数值按原始4位数据进行降序排列。最后调用函数writeDat()把结果bb输出到文件out.dat中。
例:处理前 6012 5099 9012 7025 8088
处理后 9012 6012 7025 8088 5099
注意:部分源程序存在文件PROG1.C文件中。
请勿改动数据文件in.dat中的任何数据、主函数main()、读函数readDat()和写函数writeDat()的内容。
部分源程序如下:
#include
www.lexue88.com
#include
#include
int aa[200], bb[10] ;
void jsSort()
{
}
void main()
{
readDat() ;
jsSort() ;
writeDat() ;
}
readDat()
{
FILE *in ;
int i ;
in=fopen(“in.dat“, r) ;
for(i=0 ; i《200 ; i+[KG-*3]+) fscanf(in, “%d,“, &aa[i]) ;
fclose(in) ;
}
writeDat()
{
FILE *out ;
int i ;
clrscr() ;
out=fopen(“out.dat“, w) ;
for(i=0 ; i《10 ; i+[KG-*3]+) {
printf(“i=%d,%d\n“, i + 1,bb[i]) ;
fprintf(out, “%d\n“, bb[i]) ;
}
fclose(out) ;
}
分析:根据题意分析得出:本题主要考查考生的运用数组和排序的能力,要求考生仅编写函数jsSort实现此功能。其中输出函数给出且已调用,考生不必自己编写。
编写函数jsSort的程序如下:
void jsSort()
{
int i, j, m ;
for(i=0 ; i《199 ; i++)
for(j=i + 1 ; j《200 ; j++) {
if((aa[i] % 1000)》(aa[j] % 1000)) {
m=aa[i] ;
aa[i]=aa[j] ;
aa[j]=m ;
}
else if((aa[i] % 1000)= =(aa[j] % 1000)) {
if(aa[i]
m=aa[i] ;
aa[i]=aa[j] ;
aa[j]=m ;
}
}
}
for(i=0 ; i《10 ; i++) bb[i]=aa[i] ;
}
数据文件IN.DAT内容如下:
1862,8281,6183,5180,1217
8215,8865,7110,1870,4373
7447,8420,8252,8809,7968
7331,3472,4126,8101,5576
7397,2066,5940,9779,7947
2912,1226,2238,4122,6210
2020,3721,1646,8324,2351
2345,1862,5186,7573,8949
3211,2103,6695,7968,6157
9956,2998,4063,6096,9432
6452,2422,1382,5813,8138
7486,1178,7650,1687,5012
4845,1428,3197,6077,4650
6641,2695,5758,8855,5321
1337,5323,9154,6419,8044
3654,2246,1890,3250,5542
1976,5986,5051,8053,7377
3726,6189,9483,5874,9246
2522,1020,3492,4032,4367
9784,4746,3130,8521,7837
6521,7917,6240,1225,4092
8022,6893,2075,8043,7409
3316,5688,4600,6016,2953
6206,4100,8947,6264,1986
4933,6105,7054,9121,5318
3164,9609,5784,4178,4906
3731,2996,4218,3498,3849
3835,5491,3890,3531,1475
8219,3720,8585,9616,6539
4961,8850,5822,2769,1269
2171,2104,3681,9291,5768
3582,3035,7212,8313,5669
4782,7545,7917,8561,5447
4912,7624,1343,8579,5639
5745,1833,7948,4321,3090
9389,2796,8073,7806,4910
7970,2808,8109,8664,5963
3660,1396,1380,5705,7269
7417,5590,2823,6308,1754
5467,8126,9822,6737,1023
结果文件OUT.DAT的内容如下:
5012
6016
2020
1020
8022
1023
4032
3035
8043
8044
,全国计算机三级考试数据库技术上机指导tag: 计算机等级考试,计算机等级考试试题,全国计算机等级考试试题,计算机等级考试