三级B上级模拟试题及答案(1) |
|
www.nanhushi.com 佚名 不详 |
1.请编制函数readdat( )实现从文件hex.in中读取100个十六进 制数到字符串数组xx中; 请编制函数h16to10(), 将xx中的十六进 制数转换成十进制数并把已转换的十进制数仍存放在字符串数组xx 中, 最后调用函数writedat()把结果输出到dec.out文件中。 原始数据文件存放的格式是: 每行存放10个数, 并用逗号隔 开。(每个数均大于0且小于等于2000) 注意: 部分源程序存放在prog1.c中。 请勿改动主函数main()和输出数据函数writedat()的内容。
/*参考答案*/ #include #include #include #include #define max 100 char xx[max][20] ; void writedat(void) ; int readdat(void) { file *fp ; int i,j; char c; if((fp = fopen('hex.in', 'r')) == null) return 1 ; /***********读入数据并存放到数组xx中*************/ for(i = 0; i < max; i++) { j = 0; while((c = (char) fgetc(fp)) != eof) { if(c == ',') { xx[j] = '/0'; break; } else if(c != '/n' && c != '/r')/*去掉回车换行符*/ { if(!isdigit(c))/*如果是字母,则转换为大写*/ c &= 0xdf; xx[j] = c; ++j; } } if(c == eof) break; } fclose(fp) ; return 0 ; }void h16to10(void) { char str[20]; int i,j,len,val; for(i = 0; i < max; i++) { strcpy(str,xx); len = strlen(str); val = 0; for(j = 0; j < len; j++) { val *= 16; val += isdigit(str[j]) ? (str[j] - 48) : (str[j] - 55); } itoa(val,xx,10); }} void main() { int i ; for(i = 0 ; i < max ; i++) memset(xx, 0, 20) ; if(readdat()) { printf('数据文件hex.in不能打开!/007/n') ; return ; } h16to10() ; writedat() ; }void writedat(void) { file *fp ; int i ; fp = fopen('dec.out', 'w') ; for(i = 0 ; i < max ; i++) fprintf(fp, '%s/n', xx) ; fclose(fp) ; }
|
|
|
文章录入:杜斌 责任编辑:杜斌 |
|
上一篇文章: 2001年4月全国计算机等级考试三级-B-笔试试卷 下一篇文章: 三级B上级模拟试题及答案(2) |
【字体:小 大】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口】 |
|
|