1.請編制函數(shù)readdat( )實(shí)現(xiàn)從文件hex.in中讀取100個(gè)十六進(jìn) 制數(shù)到字符串?dāng)?shù)組xx中; 請編制函數(shù)h16to10(), 將xx中的十六進(jìn) 制數(shù)轉(zhuǎn)換成十進(jìn)制數(shù)并把已轉(zhuǎn)換的十進(jìn)制數(shù)仍存放在字符串?dāng)?shù)組xx 中, 最后調(diào)用函數(shù)writedat()把結(jié)果輸出到dec.out文件中。 原始數(shù)據(jù)文件存放的格式是: 每行存放10個(gè)數(shù), 并用逗號隔 開。(每個(gè)數(shù)均大于0且小于等于2000)
注意: 部分源程序存放在prog1.c中。
請勿改動主函數(shù)main()和輸出數(shù)據(jù)函數(shù)writedat()的內(nèi)容。
/*參考答案*/
#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 ;
/***********讀入數(shù)據(jù)并存放到數(shù)組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))/*如果是字母,則轉(zhuǎn)換為大寫*/
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("數(shù)據(jù)文件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) ;
}
注意: 部分源程序存放在prog1.c中。
請勿改動主函數(shù)main()和輸出數(shù)據(jù)函數(shù)writedat()的內(nèi)容。
/*參考答案*/
#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 ;
/***********讀入數(shù)據(jù)并存放到數(shù)組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))/*如果是字母,則轉(zhuǎn)換為大寫*/
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("數(shù)據(jù)文件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) ;
}