中文字幕精品无码一区二区,成全视频在线播放观看方法,大伊人青草狠狠久久,亚洲一区影音先锋色资源

100道南開c語言題

資源下載
  1. 二一教育資源

100道南開c語言題

資源簡介

南開100題【終極無錯2.0版】
題目前帶★號的是在2004年4月份上機考試中被考生們所確定了的上機題。&&題目前帶☆號的是在2004年9月份上機考試中被考生們所確定了的上機題。&&題目中所提示的“無憂id xx”指的是2004年上半年版無憂模擬系統(tǒng)中的固定抽題序號&&題目中所提示的“捷成id xx”指的是2004年上半年版捷成模擬系統(tǒng)中的固定抽題序號
題目1(無憂 id 14、id 27(提供isP()函數(shù);捷成id 23題)
請編寫一個函數(shù)jsValue(int m,int k,int xx[]),該函數(shù)的功能是:將大于整數(shù)m且緊靠m的k個素數(shù)存入數(shù)組xx傳回。
最后調(diào)用函數(shù)writeDat()讀取10組數(shù)據(jù),分別得出結果且把結果輸出到文件out.dat中。
部分源程序存在文件prog1.c中。
例如:若輸入17 5 則應輸出:19,23,29,31,37。
請勿改動主函數(shù)main()和寫函數(shù)writeDat()的內(nèi)容。
#include
#include
void readwriteDat();
int isP(int m)
{
int i;
for(i=2;iif(m % i==0)return 0;
return 1;
}
void num(int m,int k,int xx[])
{ int s=0;
for(m=m+1;k>0;m++)
if(isP(m)) { xx[s++]=m; k--;}
}
main()
{
int m,n,xx[1000];
clrscr();
printf("\nPlease enter two integers:");
scanf("%d%d",&m,&n);
num(m,n,xx);
for(m=0;mprintf("%d ",xx[m]);
printf("\n");
readwriteDat();
}
void readwriteDat()
{
int m,n,xx[1000], i;
FILE *rf,*wf;
rf=fopen("in.dat","r");
wf=fopen("out.dat","w");
for(i=0;i<10;i++){
fscanf(rf,"%d %d",&m,&n);
num(m,n,xx);
for(m=0;mfprintf(wf,"\n");
}
fclose(rf);
fclose(wf);
}
無憂id 14題(無isP()函數(shù))
#include
void jsValue(int m,int k,int xx[])
{ int i,j,s=0;
for(i=m+1;k>0;i++)
{ for(j=2;jif(i%j==0) break;
if(i==j) { xx[s++]=i; k--;}
}
}
main()
{
int m,n,zz[100];
printf("\n請輸入兩個整數(shù):");
scanf("%d%d",&m,&n);
jsValue(m,n,zz);
for(m=0;mprintf("\n");
writeDat();
}
writeDat()
{
int m,n,zz[100],i;
FILE *in,*out;
in=fopen("in.dat","r");
out=fopen("out.dat","w");
for(i=0;i<10;i++){
fscanf(in,"%d%d",&m,&n);
jsValue(m,n,zz);
for(m=0;mfprintf(out,"\n");
}
fclose(in);
fclose(out);
}
另一解法:
void num(int m,int k,int xx[])
{
int n=0,data=m+1;
while(nif(isP(data)) xx[n++]=data;
data++;}
}
★題目2(無憂 id 143 整數(shù)排序題)
已知數(shù)據(jù)文件IN.DAT中存有200個四位數(shù),并已調(diào)用讀函數(shù)readDat()把這些數(shù)存入數(shù)組a中,請考生編制一函數(shù)jsVal(),其功能是:如果四位數(shù)各位上的數(shù)字均是0或2或4或6或8,則統(tǒng)計出滿足此條件的個數(shù)cnt,并把這些四位數(shù)按從大到小的順序存入數(shù)組b中。最后main( )函數(shù)調(diào)用寫函數(shù)writeDat()把結果cnt以及數(shù)組b中符合條件的四位數(shù)輸出到OUT.DAT文件中。
注意:部分源程序存在文件prog1.c中。
程序中已定義數(shù)組:a[200],b[200],已定義變量:cnt
請勿改動數(shù)據(jù)文件IN.DAT中的任何數(shù)據(jù)、主函數(shù)main()、讀函數(shù)readDat()和寫函數(shù)writeDat()的內(nèi)容。
#include
#define MAX 200
int a[MAX], b[MAX], cnt = 0 ;
void jsVal()
{ int i,j,qw,bw,sw,gw;
for(i=0;i{ qw=a[i]/1000; bw=a[i]/100%10;
sw=a[i]%100/10; gw=a[i]%10;
if(qw&&qw%2==0&&bw%2==0&&sw%2==0&&gw%2==0) b[cnt++]=a[i];
}
for(i=0;ifor(j=i+1;jif(b[i]}
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("滿足條件的數(shù)=%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) ;
}
★☆題目3(無憂 id 133題;捷成id 59、99字符串位置倒置題)
函數(shù)ReadDat( )實現(xiàn)從文件IN.DAT中讀取一篇英文文章存入到字符串數(shù)組xx中;請編制函數(shù)StrOR( ),其函數(shù)的功能是:以行為單位依次把字符串中所有小寫字母o左邊的字符串內(nèi)容移到該串的右邊存放,然后把小寫字母o刪除,余下的字符串內(nèi)容移到已處理字符串的左邊存放,之后把已處理的字符串仍按行重新存入字符串數(shù)組xx中。最后main()函數(shù)調(diào)用函數(shù)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
原始數(shù)據(jù)文件存放的格式是:每行的寬度均小于80個字符,含標點符號和空格。
注意:部分源程序存放在文件prog1.c中。
請勿改動主函數(shù)main()、讀數(shù)據(jù)函數(shù)ReadDat()和輸出數(shù)據(jù)函數(shù)WriteDat()的內(nèi)容。
#include
#include
#include
char xx[50][80] ;
int maxline = 0 ; /* 文章的總行數(shù) */
int ReadDat(void) ;
void WriteDat(void) ;
void StrOR(void)
{int i,righto,j,s,k;
char tem[80];
for(i=0;ifor(j=strlen(xx[i])-1;j>=0;j--)
{ k=0;
memset(tem,0,80); /*初始化字符串數(shù)組tem*/
if(xx[i][j]=='o') /*如果當前字符為'o',進入以下語句*/
{righto=j; /*則將此字符中位置j的值賦給righto*/
for(s=righto+1;stem[k++]=xx[i][s]; /*從righto的下一跳開始將其后所有的字符都存入到tem中*/
for(s=0;sif(xx[i][s]!='o') tem[k++]=xx[i][s]; /*將不是字符'o'的字符全存入到tem中*/
strcpy(xx[i],tem); /*將當前已處理的字符重新存入當前行xx*/
}
else continue;
}
}
void main()
{
clrscr() ;
if(ReadDat()) {
printf("數(shù)據(jù)文件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{ 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; ifor(j=0; jif(xx[i][j]=='o')
{ p=&xx[i][j+1];
strcpy(yy,p); /*將指針p所指向的字符串拷貝到字符串yy中去*/
strncat(yy,xx[i],j); /*將字符串xx[i]中前j個字符連接到y(tǒng)y中*/
strcpy(xx[i],yy); /*將字符串yy重新拷貝到字符串xx[i]中去*/
j=0; /* 開始下一次的掃描。*/
}
}
相關庫函數(shù)解釋:
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
★☆題目4(無憂id 24題 捷成id 9 字符串單詞倒置題)
函數(shù)ReadDat()實現(xiàn)從文件IN.DAT中讀取一篇英文文章存入到字符串數(shù)組xx中,請編制函數(shù)StrOL(),其函數(shù)的功能是:以行為單位對行中以空格或標點符號為分隔的所有單詞進行倒排。最后把已處理的字符串(應不含標點符號)仍按行重新存入字符串數(shù)組xx中,最后調(diào)用函數(shù)writeDat()把結果xx輸出到文件OUT6.DAT中。
例如:原文:You He Me
I am a student.
     結果:Me He You
student a am I
原始數(shù)據(jù)文件存放的格式是:每行的寬度均小于80個字符,含標點符號和空格。
部分源程序存在文件prog1.c中。
請勿改動主函數(shù)main()、讀數(shù)據(jù)函數(shù)ReadDat()和輸出數(shù)據(jù)函數(shù)writeDat()的內(nèi)容。
#include
#include
#include
#include
char xx[50][80];
int maxline=0;/*文章的總行數(shù)*/
int ReadDat(void);
void WriteDat(void);
/*在無憂及捷成版模擬系統(tǒng)中都通過測試(輸入文件句末有標點的在輸出文件中句前有空格*/
void StrOL(void)
{ int i,j,k,s,m,strl;
char str[80];
for(i=0;i{ strl=strlen(xx[i]);
memset(str,0,80); /*初始化這字符串數(shù)組str*/
s=k=0;
for(j=strl-1;j>=0;j--) /*從當前字符串尾部開始向前倒序循環(huán),實現(xiàn)題意要求的倒排*/
{ if(isalpha(xx[i][j])) k++; /*如果當前字符是字母a~z或A~Z,則k加一*/
else { for(m=1;m<=k;m++) /*否則將長度為k的單詞順序存入到字符串數(shù)組str中,s值加1*/
str[s++]=xx[i][j+m];
k=0; /*將k值清0,以方便下一個單詞的長度計數(shù)*/
}
if(!isalpha(xx[i][j])) str[s++]=' '; /*如果當前字符不是字母a~z或A~Z,則以空格代之存入到字符串數(shù)組str中,s值加一*/
}
for(m=1;m<=k;m++) /*此時的k值為當前字符串中第一個單詞的長度,但在上一個for循環(huán)中沒能存入到字符串數(shù)組str中,所以在這里將其存入到str中*/
str[s++]=xx[i][j+m];
str[s]='\0'; /*在當前行尾加0以標記此行的結束*/
strcpy(xx[i],str); /*將倒排好的當前字符串重新存回到當前行xx中*/
}
}
void main()
{
clrscr();
if(ReadDat()){
printf("數(shù)據(jù)文件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;iprintf("%s\n",xx[i]);
fprintf(fp,"%s\n",xx[i]);
}
fclose(fp);
}
捷成版模擬系統(tǒng)中的解法
/*在無憂模擬系統(tǒng)中沒通過測試(輸入文件句末有標點的在輸出文件中句前無空格*/
void StrOL(void)
{
int i, j ;
char word[21], yy[80], zz[80], *p ;
for(i = 0 ; i < maxline ; i++) {
p = xx[i] ;
j = 0 ;
memset(word, 0, 21) ;
memset(yy, 0, 80) ;
while(*p) {
if(isalpha(*p)) {
word[j++] = *p++ ;
if(*p) continue ;
}
strcpy(zz, yy) ;
sprintf(yy, "%s %s", word, zz) ;
j = 0 ;
memset(word, 0, 21) ;
while(*p && (!isalpha(*p))) p++ ;
}
strcpy(xx[i], yy) ;
}
}
另一解法():
/*在無憂及捷成版模擬系統(tǒng)中都通過測試(輸入文件句末有標點的在輸出文件中句前有空格*/
void StrOL(void)
{ int i,j,m;
char str[80];
for(i=0;i{ m=strlen(xx[i]);
memset(str,0,80);
for(j=m-1;j>=0;j--)
if(!isalpha(xx[i][j]))
{ strcat(str,xx[i]+j+1);
strcat(str," ");
xx[i][j]='\0';
}
strcat(str,xx[i]);
strcpy(xx[i],str);
}
}
實際上機考試時上面幾種解法都可以采用。
*****************************************
★☆題目5(無憂id 8整數(shù)排序題)
在文件in.dat中有200個正整數(shù),且每個數(shù)均在1000至9999之間。函數(shù)ReadDat()讀取這200個數(shù)存放到數(shù)組aa中。請編制函數(shù)jsSort(),其函數(shù)的功能是:要求按每個數(shù)的后三位的大小進行升序排列,然后取出滿足此條件的前10個數(shù)依次存入數(shù)組bb中,如果后三位的數(shù)值相等,則按原先的數(shù)值進行降序排列。最后調(diào)用函數(shù)WriteDat()把結果bb輸出到文件out.dat中。
例:處理前 6012 5099 9012 7025 8088
處理后 9012 6012 7025 8088 5099
部分源程序存在文件prog1.c中。
  請勿改動主函數(shù)main()、讀數(shù)據(jù)函數(shù)ReadDat()和輸出數(shù)據(jù)函數(shù)WriteDat()的內(nèi)容。
#include
#include
#include
int aa[200],bb[10];
void jsSort()
{
int i,j,data;
for(i=0;i<199;i++)
for(j=i+1;j<200;j++)
if(aa[i]%1000>aa[j]%1000||aa[i]%1000==aa[j]%1000&&aa[i]{data=aa[i];aa[i]=aa[j];aa[j]=data;}
for(i=0;i<10;i++)
bb[i]=aa[i];
}
void main()
{
readDat();
jsSort();
writeDat();
}
readDat()
{
FILE *in;
int i;
in=fopen("in.dat","r");
for(i=0; i<200; i++) fscanf(in,"%d,",&aa[i]);
fclose(in);
}
writeDat()
{
FILE *out;
int i;
clrscr();
out=fopen("out.dat","w");
for(i=0; i<10; i++){
printf(" %d",bb[i]);
fprintf(out,"%d\n",bb[i]);
}
fclose(out);
}
*****************************************
題目6 正整數(shù)排序
在文件in.dat中有200個正整數(shù),且每個數(shù)均在1000至9999之間。函數(shù)ReadDat()讀取這200個數(shù)存放到數(shù)組aa中。請編制函數(shù)jsSort(),其函數(shù)的功能是:要求按每個數(shù)的后三位的大小進行降序排列,然后取出滿足此條件的前10個數(shù)依次存入數(shù)組b中,如果后三位的數(shù)值相等,則按原先的數(shù)值進行升序排列。最后調(diào)用函數(shù)WriteDat()把結果bb輸出到文件out.dat中。
例:處理前 9012 5099 6012 7025 8088
處理后 5099 8088 7025 6012 9012
注意:部分源程序已給出。
  請勿改動主函數(shù)main()、讀數(shù)據(jù)函數(shù)ReadDat()和輸出數(shù)據(jù)函數(shù)WriteDat()的內(nèi)容。
#include
#include
#include
int aa[200],bb[10];
void jsSort()
{
int i,j,data;
for(i=0;i<199;i++)
for(j=i+1;j<200;j++)
if(aa[i]%1000aa[j])
{data=aa[i];aa[i]=aa[j];aa[j]=data;}
for(i=0;i<10;i++)
bb[i]=aa[i];
}
void main()
{
readDat();
jsSort();
writeDat();
system("pause");
}
readDat()
{
FILE *in;
int i;
in=fopen("in.dat","r");
for(i=0; i<200; i++) fscanf(in,"%d,",&aa[i]);
fclose(in);
}
writeDat()
{
FILE *out;
int i;
clrscr();
out=fopen("out.dat","w");
for(i=0; i<10; i++){
printf("i=%d,%d\n",i+1,bb[i]);
fprintf(out,"%d\n",bb[i]);
}
fclose(out);
}
*****************************************
★題目7(無憂id 15結構體操作題))
已知在文件IN.DAT中存有100個產(chǎn)品銷售記錄,每個產(chǎn)品銷售記錄由產(chǎn)品代碼dm(字符型4位),產(chǎn)品名稱mc(字符型10位),單價dj(整型),數(shù)量sl(整型),金額je(長整型)五部分組成。其中:金額=單價*數(shù)量計算得出。函數(shù)ReadDat()是讀取這100個銷售記錄并存入結構數(shù)組sell中。請編制函數(shù)SortDat(),其功能要求:按產(chǎn)品代碼從大到小進行排列,若產(chǎn)品代碼相同,則按金額從大到小進行排列,最終排列結果仍存入結構數(shù)組sell中,最后調(diào)用函數(shù)WriteDat()把結果輸出到文件OUT6.DAT中。
部分源程序存在文件prog1.c中。
  請勿改動主函數(shù)main()、讀數(shù)據(jù)函數(shù)ReadDat()和輸出數(shù)據(jù)函數(shù)WriteDat()的內(nèi)容。
#include
#include
#include
#include
#include
#define MAX 100
typedef struct{
char dm[5]; /*產(chǎn)品代碼*/
char mc[11]; /*產(chǎn)品名稱*/
int dj; /*單價*/
int sl; /*數(shù)量*/
long je; /*金額*/
}PRO;
PRO sell[MAX];
void ReadDat();
void WriteDat();
void SortDat()
{int i,j;
PRO xy;
for(i=0;i<99;i++)
for(j=i+1;j<100;j++)
if(strcmp(sell[i].dm,sell[j].dm)<0||strcmp(sell[i].dm,sell[j].dm)==0&&sell[i].je{xy=sell[i]; sell [i]=sell[j]; sell[j]=xy;}
}
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(void)
{
FILE *fp;
int i;
fp=fopen("OUT6.DAT","w");
for(i=0;i<100;i++){
printf("%s %s %4d %5d %5d\n", sell[i].dm,sell[i].mc,sell[i].dj,sell[i].sl,sell[i].je);
fprintf(fp,"%s %s %4d %5d %5d\n", sell[i].dm,sell[i].mc,sell[i].dj,sell[i].sl,sell[i].je);
}
fclose(fp);
}
*****************************************
★☆題目8(無憂id 83 字符替換題)
函數(shù)ReadDat()實現(xiàn)從文件ENG.IN中讀取一篇英文文章,存入到字符串數(shù)組xx中;請編制函數(shù)encryptChar(),按給定的替代關系對數(shù)組xx中的所有字符進行替代,仍存入數(shù)組xx的對應的位置上,最后調(diào)用函數(shù)WriteDat()把結果xx輸出到文件PS1.DAT中。
  替代關系:f(p)=p*11 mod 256(p是數(shù)組中某一個字符的ASCII值,f(p)是計算后新字符的ASCII值),如果計算后f(p)值小于等于32或大于130,則該字符不變,否則將f(p)所對應的字符進行替代。部分源程序存在文件prog1.c中。原始數(shù)據(jù)文件存放的格式是:每行的寬度均小于80個字符。
  請勿改動主函數(shù)main()、讀數(shù)據(jù)函數(shù)ReadDat()和輸出數(shù)據(jù)函數(shù)WriteDat()的內(nèi)容。
#include
#include
#include
#include
unsigned char xx[50][80];
int maxline=0;/*文章的總行數(shù)*/
int ReadDat(void);
void WriteDat(void);
void encryptChar()
{ int i,j;
for(i=0;ifor(j=0;jif(xx[i][j]*11%256<=32||xx[i][j]*11%256>130) continue;
else xx[i][j]=xx[i][j]*11%256;
}
void main()
{
clrscr();
if(ReadDat()){
printf("數(shù)據(jù)文件ENG.IN不能打開!\n\007");
return;
}
encryptChar();
WriteDat();
}
int ReadDat(void)
{
FILE *fp;
int i=0;
unsigned char *p;
if((fp=fopen("eng.in","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("ps1.dat","w");
for(i=0;iprintf("%s\n",xx[i]);
fprintf(fp,"%s\n",xx[i]);
}
fclose(fp);
}
解法二:
void encryptChar()
{ int i,j,k;
for(i=0;ifor(j=0;j{ k= xx[i][j]*11%256;
if(k<=32||k>130) continue;
else xx[i][j]=k;
}
}
解法三:
void encryptChar()
{ int i,j;
unsigned char ch;
for(i=0;ifor(j=0;j{ ch=xx[i][j]*11%256;
if(ch<=32||ch>130) continue;
else xx[i][j]=ch;
}
}
解法四:
void encryptChar()
{ int i,j;
char *p;
for(i=0;ifor(j=0;j{ p=xx[i][j]*11%256;
if(p<=32||p>130) continue;
else xx[i][j]=p;
}
}
解法五:
void encryptChar()
{ int i;
char *pf;
for(i=0;i{ pf=xx[i];
while(*pf!=0)
if(*pf*11%256<=32||*pf*11%256>130) pf++;
else *pf++=*pf*11%256;
}
}
***********************************
★☆題目9(無憂id 28;id 124;捷成id 16 字符串排序題)
函數(shù)ReadDat()實現(xiàn)從文件IN.DAT中讀取一篇英文文章存入到字符串數(shù)組xx中,請編制函數(shù)SortCharD(),其函數(shù)的功能是:以行為單位對字符按從大到小的順序進行排序,排序后的結果仍按行重新存入字符串數(shù)組xx中,最后調(diào)用函數(shù)writeDat()把結果xx輸出到文件OUT2.DAT中。
例:原文:dAe,BfC.
CCbbAA
結果:fedCBA.
bbCCAA
原始數(shù)據(jù)文件存放的格式是:每行的寬度均小于80個字符,含標點符號和空格。
部分源程序存在文件prog1.c中。
請勿改動主函數(shù)main()、讀數(shù)據(jù)函數(shù)ReadDat()和輸出數(shù)據(jù)函數(shù)writeDat()的內(nèi)容。
#include
#include
#include
char xx[50][80];
int maxline=0;/*文章的總行數(shù)*/
int ReadDat(void);
void WriteDat(void);
void SortCharD(void)
{int i,j,k,strl;
char ch;
for(i=0;i{strl=strlen(xx[i]);
for(j=0;jfor(k=j+1;kif(xx[i][j]{ch=xx[i][j]; xx[i][j]=xx[i][k]; xx[i][k]=ch;}
}
}
void main()
{
clrscr();
if(ReadDat()){
printf("數(shù)據(jù)文件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;
fp=fopen("OUT2.DAT","w");
for(i=0;iprintf("%s\n",xx[i]);
fprintf(fp,"%s\n",xx[i]);
}
fclose(fp);
}
*****************************************
題目10(無憂id 68 替換字符題)
函數(shù)ReadDat()實現(xiàn)從文件IN.DAT中讀取一篇英文文章存入到字符串數(shù)組xx中,請編制函數(shù)ConvertCharA(),其函數(shù)的功能是:以行為單位把字符串中的所有小寫字母改寫成該字母的下一個字母,如果是字母z,則改寫成字母a。大寫字母仍為大寫字母,小寫字母仍為小寫字母,其他字符不變。把已處理的字符串仍按行重新存入字符串數(shù)組xx中,最后調(diào)用函數(shù)writeDat()把結果xx輸出到文件OUT1.DAT中。
例:原文:Adb.Bcdza
abck.LLhj
結果:Aec.Bdeab
bcdl.LLik
原始數(shù)據(jù)文件存放的格式是:每行的寬度均小于80個字符,含標點符號和空格。
部分源程序存在文件prog1.c中。
請勿改動主函數(shù)main()、讀數(shù)據(jù)函數(shù)ReadDat()和輸出數(shù)據(jù)函數(shù)writeDat()的內(nèi)容。
#include
#include
#include
char xx[50][80];
int maxline=0;/*文章的總行數(shù)*/
int ReadDat(void);
void WriteDat(void);
void ConvertCharA(void)
{ int i,j;
for(i=0;i{ for(j=0;jif(xx[i][j]=='z') xx[i][j]='a';
else if(xx[i][j]>='a'&&xx[i][j]<='y') xx[i][j]+=1;
}
}
void main()
{
clrscr();
if(ReadDat()){
printf("數(shù)據(jù)文件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("OUT1.DAT","w");
for(i=0;iprintf("%s\n",xx[i]);
fprintf(fp,"%s\n",xx[i]);
}
fclose(fp);
}
*****************************************
☆題目11(無憂id 93 字符串字母移位題)
程序prog1.c的功能是:把 s 字符串中的所有字母改寫成該字母的下一個字母,字母z改寫成字母a。要求大寫字母仍為大寫字母,小寫字母仍為小寫字母,其它字符不做改變。
請考生編寫函數(shù)chg(char *s)實現(xiàn)程序要求,最后調(diào)用函數(shù)readwriteDAT( )把結果輸出到文件bc1.out中。
例如:s 字符串中原有的內(nèi)容為:Mn.123Zxy,則調(diào)用該函數(shù)后,結果為:No.123Ayz。
注意:部分源程序存在文件prog1.c文件中。
請勿改動主函數(shù)main( )和輸出數(shù)據(jù)函數(shù)readwriteDAT()的內(nèi)容。
#include
#include
#include
#include
#define N 81
void readwriteDAT();
void chg(char *s)
{while(*s)
if(*s=='z'||*s=='Z') {*s-=25; s++;}
else if(*s>='a'&&*s<='y') {*s+=1;s++;}
else if(*s>='A'&&*s<='Y') {*s+=1;s++;}
else s++;
}
main( )
{
char a[N];
clrscr();
printf("Enter a string : "); gets(a);
printf("The original string is : "); puts(a);
chg(a);
printf("The string after modified : ");
puts (a);
readwriteDAT() ;
}
void readwriteDAT()
{
int i ;
char a[N] ;
FILE *rf, *wf ;
rf = fopen("bc1.in", "r") ;
wf = fopen("bc1.out", "w") ;
for(i = 0 ; i < 50 ; i++) {
fscanf(rf, "%s", a) ;
chg(a) ;
fprintf(wf, "%s\n", a) ;
}
fclose(rf) ;
fclose(wf) ;
}
*****************************************
★題目12(無憂id 78結構體運算題題)
已知在文件IN.DAT中存有100個產(chǎn)品銷售記錄,每個產(chǎn)品銷售記錄由產(chǎn)品代碼dm(字符型4位),產(chǎn)品名稱mc(字符型10位),單價dj(整型),數(shù)量sl(整型),金額je(長整型)五部分組成。其中:金額=單價*數(shù)量計算得出。函數(shù)ReadDat()是讀取這100個銷售記錄并存入結構數(shù)組sell中。請編制函數(shù)SortDat(),其功能要求:
按產(chǎn)品名稱從小到大進行排列,若產(chǎn)品名稱相等,則按金額從小到大進行排列,最終排列結果仍存入結構數(shù)組sell中,最后調(diào)用函數(shù)WriteDat()把結果輸出到文件OUT5.DAT中。
部分源程序存在文件prog1.c中。
  請勿改動主函數(shù)main()、讀數(shù)據(jù)函數(shù)ReadDat()和輸出數(shù)據(jù)函數(shù)WriteDat()的內(nèi)容。
#include
#include
#include
#include
#include
#define MAX 100
typedef struct{
char dm[5]; /*產(chǎn)品代碼*/
char mc[11]; /*產(chǎn)品名稱*/
int dj; /*單價*/
int sl; /*數(shù)量*/
long je; /*金額*/
}PRO;
PRO sell[MAX];
void ReadDat();
void WriteDat();
void SortDat()
{int i,j;
PRO xy;
for(i=0;i<99;i++)
for(j=i+1;j<100;j++)
if(strcmp(sell[i].mc,sell[j].mc)>0||strcmp(sell[i].mc,sell[j].mc)==0&&sell[i].je>sell[j].je)
{xy=sell[i];sell[i]=sell[j];sell[j]=xy;}
}
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("OUT5.DAT","w");
for(i=0;i<100;i++){
printf("%s %s %4d %5d %5d\n",sell[i].dm,sell[i].mc,sell[i].dj,sell[i].sl,sell[i].je);
fprintf(fp,"%s %s %4d %5d %5d\n", sell[i].dm,sell[i].mc,sell[i].dj,sell[i].sl,sell[i].je);
}
fclose(fp);
}
*****************************************
題目13(無憂id 81結構體運算題)
無憂id 81題按金額從小到大進行排列
已知在文件IN.DAT中存有100個產(chǎn)品銷售記錄,每個產(chǎn)品銷售記錄由產(chǎn)品代碼dm(字符型4位),產(chǎn)品名稱mc(字符型10位),單價dj(整型),數(shù)量sl(整型),金額je(長整型)五部分組成。其中:金額=單價*數(shù)量計算得出。函數(shù)ReadDat()是讀取這100個銷售記錄并存入結構數(shù)組sell中。請編制函數(shù)SortDat(),其功能
要求:按產(chǎn)品代碼從小到大進行排列,若產(chǎn)品代碼相等,則按金額從大到小進行排列,最終排列結果仍存入結構數(shù)組sell中,最后調(diào)用函數(shù)WriteDat()把結果輸出到文件OUT9.DAT中。
部分源程序存在文件prog1.c中。
  請勿改動主函數(shù)main()、讀數(shù)據(jù)函數(shù)ReadDat()和輸出數(shù)據(jù)函數(shù)WriteDat()的內(nèi)容。
#include
#include
#include
#include
#include
#define MAX 100
typedef struct{
char dm[5]; /*產(chǎn)品代碼*/
char mc[11]; /*產(chǎn)品名稱*/
int dj; /*單價*/
int sl; /*數(shù)量*/
long je; /*金額*/
}PRO;
PRO sell[MAX];
void ReadDat();
void WriteDat();
void SortDat()
{int i,j;
PRO xy;
for(i=0;i<99;i++)
for(j=i+1;j<100;j++)
if(strcmp(sell[i].dm,sell[j].dm)>0||strcmp(sell[i].dm,sell[j].dm)==0&&sell[i].je{xy=sell[i];sell[i]=sell[j];sell[j]=xy;}
}
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("OUT9.DAT","w");
for(i=0;i<100;i++){
printf("%s %s %4d %5d %5d\n", sell[i].dm,sell[i].mc,sell[i].dj,sell[i].sl,sell[i].je);
fprintf(fp,"%s %s %4d %5d %5d\n", sell[i].dm,sell[i].mc,sell[i].dj,sell[i].sl,sell[i].je);
}
fclose(fp);
}
*****************************************
☆題目14(無憂id 151 整數(shù)統(tǒng)計排序題)
已知數(shù)據(jù)文件IN.DAT中存有200個四位數(shù),并已調(diào)用讀函數(shù)readDat()把這些數(shù)存入數(shù)組a中,請考生編制一函數(shù)jsVal(),其功能是:依次從數(shù)組a中取出一個四位數(shù),如果該四位數(shù)連續(xù)大于該四位數(shù)以后的五個數(shù)且該數(shù)是奇數(shù)(該四位數(shù)以后不滿五個數(shù),則不統(tǒng)計),則統(tǒng)計出滿足此條件的個數(shù)cnt并把這些四位數(shù)按從小到大的順序存入數(shù)組b中,最后調(diào)用寫函數(shù)writeDat( )把結果cnt以及數(shù)組b中符合條件的四位數(shù)輸出到OUT.DAT文件中。
注意:部分源程序存在文件prog1.c中。
程序中已定義數(shù)組:a[200],b[200],已定義變量:cnt
請勿改動數(shù)據(jù)文件IN.DAT中的任何數(shù)據(jù)、主函數(shù)main()、讀函數(shù)readDat()和寫函數(shù)writeDat()的內(nèi)容。
#include
#define MAX 200
int a[MAX], b[MAX], cnt = 0 ;
void jsVal()
{int i,j,flag;
for(i=0;i{for(j=i+1;j<=i+5;j++)
if(a[i]>a[j]&&a[i]%2) flag=1;
else {flag=0;break;}
if(flag==1) b[cnt++]=a[i];
}
for(i=0;ifor(j=i+1;jif(b[i]>b[j]) {flag=b[i];b[i]=b[j];b[j]=flag;}
}
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("滿足條件的數(shù)=%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) ;
}
*****************************************
題目15(無憂id 150 整數(shù)各位數(shù)字運算排序題)
已知數(shù)據(jù)文件IN.DAT中存有200個四位數(shù),并已調(diào)用讀函數(shù)readDat()把這些數(shù)存入數(shù)組a中,請考生編制一函數(shù)jsVal(),其功能是:若一個四位數(shù)的千位數(shù)字上的值小于等于百位數(shù)字上的值,百位數(shù)字上的值小于等于十位數(shù)字上的值,以及十位數(shù)字上的值小于等于個位數(shù)字上的值,并且原四位數(shù)是偶數(shù),則統(tǒng)計出滿足此條件的個數(shù)cnt并把這些四位數(shù)按從小到大的順序存入數(shù)組b中,最后調(diào)用寫函數(shù)writeDat()把結果cnt以及數(shù)組b中符合條件的四位數(shù)輸出到OUT.DAT文件中。
注意:部分源程序存在文件prog1.c中。
程序中已定義數(shù)組:a[200],b[200],已定義變量:cnt
請勿改動數(shù)據(jù)文件IN.DAT中的任何數(shù)據(jù)、主函數(shù)main()、讀函數(shù)readDat()和寫函數(shù)writeDat()的內(nèi)容。
#include
#define MAX 200
int a[MAX], b[MAX], cnt=0;
void jsVal()
{int i,j;
int qw,bw,sw,gw;
for (i=0;i{qw=a[i]/1000; bw=a[i]%1000/100;
sw=a[i]%100/10; gw=a[i]%10;
if((qw<=bw)&&(bw<=sw)&&(sw<=gw)&&(a[i]%2==0)) b[cnt++]=a[i];
}
for(i=0;ifor(j=i+1;jif (b[i]>b[j]) {qw=b[i];b[i]=b[j];b[j]=qw;}
}
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("滿足條件的數(shù)=%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) ;
}
*****************************************
★題目16(無憂id 23 字符替換題)
函數(shù)ReadDat()實現(xiàn)從文件IN.DAT中讀取一篇英文文章存入到字符串數(shù)組xx中,請編制函數(shù)StrCharJR(),其函數(shù)的功能是:以行為單位把字符串中所有字符的ASCII值右移4位,然后把右移后的字符ASCII值再加上原字符的ASCII值,得到新的字符仍存入原字符串對應的位置上。最后把已處理的字符串仍按行重新存入字符串數(shù)組xx中,最后調(diào)用函數(shù)writeDat()把結果xx輸出到文件OUT8.DAT中。
原始數(shù)據(jù)文件存放的格式是:每行的寬度均小于80個字符,含標點符號和空格。
部分源程序存在文件prog1.c中。
請勿改動主函數(shù)main()、讀數(shù)據(jù)函數(shù)ReadDat()和輸出數(shù)據(jù)函數(shù)writeDat()的內(nèi)容。
#include
#include
#include
char xx[50][80];
int maxline=0;/*文章的總行數(shù)*/
int ReadDat(void);
void WriteDat(void);
void StrCharJR()
{int i,j;
for(i=0;ifor(j=0;jxx[i][j]+=(xx[i][j]>>4);
}
void main()
{
clrscr();
if(ReadDat()){
printf("數(shù)據(jù)文件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;iprintf("%s\n",xx[i]);
fprintf(fp,"%s\n",xx[i]);
}
fclose(fp);
}
*****************************************
☆題目17 (親朋字符替換題)
函數(shù)READDAT()實現(xiàn)從文件IN.DAT中讀取一篇英文文章存入到字符串數(shù)組XX中;請編制函數(shù)CHA(),其函數(shù)功能是:以行為單位把字符串中的第一個字符的ASCII值加第二個字符的ASCII值,得到第一個親朋字符,第二個字符的ASCII值加第三個字符的ASCII值,得到第二個新字符,依此類推一直處理到最后第二個字符,最后一個字符的ASCII值加原第一個字符的ASCII值,得到最后一個新的字符,得到的新字符分別存放在原字符串對應的位置上。最后把已處理的字符串逆轉(zhuǎn)后按行重新存入字符串數(shù)組XX中,最后調(diào)用函數(shù)WRITEDAT()把結果XX輸出到文件OUT9.DAT中.原始數(shù)據(jù)文件存放的格式是:每行的寬度均小于80個字符,含標點符號和空格.
注意:部分源程序已給出。
請勿改動主函數(shù)main()、讀數(shù)據(jù)函數(shù)ReadDat()和輸出數(shù)據(jù)函數(shù)writeDat()的內(nèi)容。
#include
#include
#include
char xx[50][80] ;
int maxline = 0 ; /* 文章的總行數(shù) */
int ReadDat(void) ;
void WriteDat(void) ;
void ChA(void)
{ int i,j;
char ch;
for(i=0;i{ ch=xx[i][0];
for(j=0;jxx[i][j]+=xx[i][j+1];
xx[i][strlen(xx[i])-1]+=ch;
strrev(xx[i]);
}
}
void main()
{
clrscr() ;
if(ReadDat()) {
printf("數(shù)據(jù)文件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) ;
}
*****************************************
☆題目18(無憂id 1 題)
函數(shù)ReadDat()實現(xiàn)從文件ENG.IN中讀取一篇英文文章,存入到字符串數(shù)組xx中;請編制函數(shù)encryptChar(),按給定的替代關系對數(shù)組xx中的所有字符進行替代,仍存入數(shù)組xx的對應的位置上,最后調(diào)用函數(shù)WriteDat()把結果xx輸出到文件PS10.DAT中。
替代關系:f(p)=p*11 mod 256(p是數(shù)組中某一個字符的ASCII值,f(p)是計算后新字符的ASCII值),如果原字符的ASCII值是偶數(shù)或計算后f(p)值小于等于32,則該字符不變,否則將f(p)所對應的字符進行替代。
部分源程序存在文件prog1.c中。原始數(shù)據(jù)文件存放的格式是:每行的寬度均小于80個字符。
請勿改動主函數(shù)main()、讀數(shù)據(jù)函數(shù)ReadDat()和輸出數(shù)據(jù)函數(shù)WriteDat()的內(nèi)容。
#include
#include
#include
#include
unsigned char xx[50][80];
int maxline=0;/*文章的總行數(shù)*/
int ReadDat(void);
void WriteDat(void);
void encryptChar()
{ int i,j;
for(i=0;ifor(j=0;jif(xx[i][j]*11%256<=32||xx[i][j]%2==0) continue;
else xx[i][j]=xx[i][j]*11%256;
}
void main()
{
clrscr();
if(ReadDat()){
printf("數(shù)據(jù)文件ENG.IN不能打開!\n\007");
return;
}
encryptChar();
WriteDat();
}
int ReadDat(void)
{
FILE *fp;
int i=0;
unsigned char *p;
if((fp=fopen("eng.in","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("ps10.dat","w");
for(i=0;iprintf("%s\n",xx[i]);
fprintf(fp,"%s\n",xx[i]);
}
fclose(fp);
}此題還有許多解法,方法可看題8
*****************************************
題目19(無憂id 144 單詞個數(shù)統(tǒng)計題)
編寫一個函數(shù)findStr(char *str,char *substr),該函數(shù)統(tǒng)計一個長度為2的子字符串在另一個字符串中出現(xiàn)的次數(shù)。例如,假定輸入的字符串為"asd asasdfg asd as zx67 asd mklo",子字符串為"as",函數(shù)返回值是6。
函數(shù)ReadWrite()實現(xiàn)從文件in.dat中讀取兩個字符串,并調(diào)用函數(shù)findStr(),最后把結果輸出到文件out.dat中。
注意:部分源程序存在文件prog1.c中。
請勿改動主函數(shù)main()和其它函數(shù)中的任何內(nèi)容,僅在函數(shù)findStr()的花括號中填入你編寫的若干語句。
#include
#include
#include
int findStr(char *str,char *substr)
{ int n=0;
char *p , *r;
while ( *str )
{p=str;
r=substr;
while(*r)
if(*r==*p) { r++; p++; }
else break;
if(*r=='\0')
n++;
str++;
}
return n;
}
main()
{
char str[81], substr[3] ;
int n ;
clrscr() ;
printf("輸入原字符串:") ;
gets(str) ;
printf("輸入子字符串:") ;
gets(substr) ;
puts(str) ;
puts(substr) ;
n=findStr(str, substr) ;
printf("n=%d\n", n) ;
ReadWrite() ;
}
ReadWrite()
{
char str[81], substr[3], ch;
int n, len, i = 0;
FILE *rf, *wf ;
rf = fopen("in.dat", "r") ;
wf = fopen("out.dat", "w") ;
while(i < 25) {
fgets(str, 80, rf) ;
fgets(substr, 10, rf) ;
len = strlen(substr) - 1 ;
ch = substr[len] ;
if(ch == '\n' || ch == 0x1a) substr[len] = 0 ;
n=findStr(str, substr);
fprintf(wf, "%d\n", n) ;
i++ ;
}
fclose(rf) ;
fclose(wf) ;
}
解法二:
int findStr(char *str,char *substr)
{ int i,j,len1,len2,cnt=0,flag;
len1=strlen(str);
len2=strlen(substr);
for(i=0;i{ for(j=0;jif(str[i+j]==substr[j]) flag=1;
else {flag=0;break;}
if(flag==1) cnt++;
}
return cnt;
}
解法三:
int findStr(char *str,char *substr)
{ int i,cnt=0;
for(i=0;iif(str[i]==*substr&&str[i+1]==*(substr+1)) cnt++;
return cnt;
}
解法四:
int findStr(char *str,char *substr)
{int cnt=0;
while(*str)
if(*str==*substr&&*(str+1)==*(substr+1)) { cnt++; str++;}
else str++;
return cnt;
}
*****************************************
題目20(無憂id 80 Fibonacci數(shù)列題)
編寫函數(shù)jsValue,它的功能是:求Fibonacci數(shù)列中大于t的最小的一個數(shù),結果由函數(shù)返回。其中Fibonacci數(shù)列F(n)的定義為:
F(0)=0,F(1)=1
F(n)=F(n-1)+F(n-2)
最后調(diào)用函數(shù)writeDat()讀取50個數(shù)據(jù)t,分別得出結果且把結果輸出到文件out.dat中。
例如:當t=1000時,函數(shù)值為:1597。
部分源程序存在文件prog1.c中。
請勿改動主函數(shù)main()和寫函數(shù)writeDat()的內(nèi)容。
#include
int jsValue(int t)
{ int f0=0,f1=1,fn;
fn=f0+f1;
while(fn<=t)
{ f0=f1;
f1=fn;
fn=f0+f1;
}
return fn;
}
main()
{
int n;
n=1000;
printf("n=%d,f=%d\n",n,jsValue(n));
writeDat();
}
writeDat()
{
FILE *in,*out;
int i,n,s;
in=fopen("in.dat","r");
out=fopen("out.dat","w");
for(i=0;i<50;i++){
fscanf(in,"%d",&n);
s=jsValue(n);
printf("%d\n",s);
fprintf(out,"%d\n",s);
}
fclose(in);
fclose(out);
}
*****************************************
題目21(無憂id 53 迭代方法求方程題)
下列程序prog1.c的功能是:利用以下所示的簡單迭代方法求方程:cos(x)-x=0的一個實根。
Xn+1=cos(Xn)
迭代步驟如下:
(1)取X1初值為0.0;
(2)X0=X1,把X1的值賦給X0;
(3)X1=cos(X0),求出一個新的X1;
(4)若X0-X1的絕對值小于0.000001,執(zhí)行步驟(5),否則執(zhí)行步驟(2);
(5)所求X1就是方程cos(X)-X=0的一個實根,作為函數(shù)值返回。
請編寫函數(shù)countValue()實現(xiàn)程序的要求,最后調(diào)用函數(shù)writeDat()把結果輸出到文件OUT17.DAT中。
部分源程序已給出。
請勿改動主函數(shù)main()和輸出數(shù)據(jù)函數(shù)writeDat()的內(nèi)容。
#include
#include
#include
float countValue()
{ double x0,x1;
x1=0.0;
do{ x0=x1;
x1=cos(x0);
}while(fabs(x0-x1)>=0.000001);
return x1;
}
main()
{
clrscr();
printf("實根=%f\n",countValue());
printf("%f\n",cos(countValue())-countValue());
writeDat();
}
writeDat()
{
FILE *wf;
wf=fopen("OUT17.DAT","w");
fprintf(wf,"%f\n",countValue());
fclose(wf);
}
*****************************************
★題目22(無憂id 39 平方根問題)
請編寫函數(shù)countValue(),它的功能是:求n以內(nèi)(不包括n)同時能被3與7整除的所有自然數(shù)之和的平方根s,并作為函數(shù)值返回,最后結果s輸出到文件out.dat中。
例如若n為1000時,函數(shù)值應為:s=153.909064。
部分源程序存在文件prog1.c中。
請勿改動主函數(shù)main()和輸入輸出數(shù)據(jù)函數(shù)progReadWrite()的內(nèi)容。
#include
#include
#include
double countValue(int n)
{ int i;
double s=0.0;
for(i=1;iif(i%21==0) s+=i;
return sqrt(s);
}
main()
{
clrscr();
printf("自然數(shù)之和的平方根=%f\n",countValue(1000));
progReadWrite();
}
progReadWrite()
{
FILE *fp,*wf;
int i,n;
float s;
fp=fopen("in.dat","r");
if(fp==NULL){
printf("數(shù)據(jù)文件in.dat不存在!");
return;
}
wf=fopen("out.dat","w");
for(i=0;i<10;i++){
fscanf(fp,"%d\n",&n);
s=countValue(n);
fprintf(wf,"%f\n",s);
}
fclose(fp);
fclose(wf);
}
*****************************************
★題目23 (實數(shù)運算題)
已知在文件in.dat中存有N個(個數(shù)<200)實數(shù),函數(shù)readdat()讀取這N個實數(shù)并存入數(shù)組xx中。請編制函數(shù)calvalue(),其功能要求:
1、求出這N個實數(shù)的平均值aver;
2、分別求出這N個實數(shù)的整數(shù)部分之和sumint以及小數(shù)部分之和sumdec,最后調(diào)用函數(shù)writedat()把所求的結果輸出到文件out.dat中。
注意:部分源程序已給出。
請勿改動主函數(shù)main()、讀數(shù)據(jù)函數(shù)readdat()和輸出數(shù)據(jù)函數(shù)writedat()的內(nèi)容。
#include
#include
#define MAXNUM 200
float xx[MAXNUM] ;
int N= 0 ; /* 文件IN.DAT中共有多少個實數(shù) */
double aver=0.0;/*平均值*/
double sumint=0.0;/*整數(shù)部分之和*/
double sumdec=0.0;/*小數(shù)部分之和*/
int ReadDat(void) ;
void WriteDat(void) ;
void CalValue(void)
{int i;
for (i=0;i{sumint+=(long)xx[i];
sumdec+=xx[i]-(long)xx[i];
aver+=xx[i];
}
aver/=N;
}
void main()
{ int i ;
clrscr() ;
for(i = 0 ; i < MAXNUM ; i++) xx[i] = 0 ;
if(ReadDat()) {
printf("數(shù)據(jù)文件IN.DAT不能打開!\007\n") ;
return ;
}
Calvalue() ;
printf("文件IN.DAT中共有實數(shù)=%d個\n", N) ;
printf("平均值=%.2lf\n", aver) ;
printf("整數(shù)部分之和=%.2lf\n", sumint) ;
printf("小數(shù)部分之和=%.2lf\n", sumdec) ;
WriteDat() ;
system("pause");
}
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, "%.2lf\n%.2lf\n%.2lf\n", aver,sumint,sumdec) ;
fclose(fp) ;
}
*****************************************
☆題目24(無憂id 16 完全平方數(shù)問題)
下列程序prog1.c的功能是:在三位整數(shù)(100至999)中尋找符合條件的整數(shù)并依次從小到大存入數(shù)組中;它既是完全平方數(shù),又是兩位數(shù)字相同,例如144、676等。
請編制函數(shù)實現(xiàn)此功能,滿足該條件的整數(shù)的個數(shù)通過所編制的函數(shù)返回。
最后調(diào)用函數(shù)writeDat()把結果輸出到文件out.dat中。
請勿改動主函數(shù)main()和寫函數(shù)writeDat()的內(nèi)容。
#include
int jsValue(int bb[])
{int i,j,k=0;
int hun,ten,data;
for(i=100;i<=999;i++)
{j=10;
while(j*j<=i)
{if(i==j*j)
{ hun=i/100; data=i%100/10; ten=i%10;
if(hun==ten||hun==data||ten==data) bb[k++]=i;
}
j++;
}
}
return k;
}
main()
{
int b[20],num;
num=jsValue(b);
writeDat(num,b);
}
writeDat(int num,int b[])
{
FILE *out;
int i;
out=fopen("out.dat","w");
printf("%d\n",num);
fprintf(out,"%d\n",num);
for(i=0;ifclose(out);
}
另一解法:
int jsValue(int bb[])
{int i,j,cnt=0,bw,sw,gw;
for(i=100;i<=999;i++)
{ bw=i/100; sw=i%100/10; gw=i%10;
for(j=10;j*j<=i;j++)
if(i==j*j&&(bw==sw||sw==gw||gw==bw)) bb[cnt++]=i;
}
return cnt;
}
*****************************************
★☆題目25 (回文數(shù)問題)
下列程序的功能是:尋找并輸出11至999之間的數(shù)m,它滿足m,m2和m3均為回文數(shù)。所謂回文數(shù)是指其各位數(shù)字左右對稱的整數(shù),例如121,676,94249等。滿足上述條件的數(shù)如m=11,m2=121,m3=1331皆為回文數(shù)。請編制函數(shù)int svalue(long m)實現(xiàn)此功能,如果是回文數(shù),則函數(shù)返回1,反之則返回0。最后把結果輸出到文件out.dat中。
注意:部分源程序已給出。
請勿改動主函數(shù)main()的內(nèi)容。
#include
int jsValue(long n)
{int i,strl,half;
char xy[20];
ltoa(n,xy,10); /*注意這里不能使用itoa()函數(shù),因為n是long 型的*/
strl=strlen(xy);
half=strl/2;
for(i=0;iif(xy[i]!=xy[--strl]) break;
if(i>=half) return 1;
else return 0;
}
main()
{long m;
FILE *out;
out=fopen("out.dat","w");
for(m=11;m<1000;m++)
{ if(jsValue(m)&&jsValue(m*m)&&jsValue(m*m*m))
{ printf("m=%4ld,m*m=%6ld,m*m*m=%8ld \n",m,m*m,m*m*m);
fprintf(out,"m=%4ld,m*m=%6ld,m*m*m=%8ld \n",m,m*m,m*m*m);
}
}
fclose(out);
system("pause");
}
或者下面的解法:
int jsValue(long n)
{long int s=0,k;
k=n;
while(k)
{ s=s*10+k%10;
k/=10;
}
if(s==n) return 1;
if(s!=n) return 0;
}
輸出結果為:
m= 11,m*m= 121,m*m*m= 1331
m= 101,m*m= 10201,m*m*m= 1030301
m= 111,m*m= 12321,m*m*m= 1367631
*****************************************
★題目26(無憂id 37 整數(shù)統(tǒng)計運算題)
已知在文件IN.DAT中存有若干個(個數(shù)<200)四位數(shù)字的正整數(shù),函數(shù)ReadDat()讀取這若干個正整數(shù)并存入數(shù)組xx中。請編制函數(shù)CalValue(),其功能要求:1、求出這文件中共有多少個正整數(shù)totNum;2、求這些數(shù)右移1位后,產(chǎn)生的新數(shù)是偶數(shù)的數(shù)的個數(shù)totCnt,以及滿足此條件的這些數(shù)(右移前的值)的算術平均值totPjz,最后調(diào)用函數(shù)writeDat()把所求的結果輸出到文件OUT.DAT中。
部分源程序存在文件prog1.c中。
請勿改動主函數(shù)main()、讀函數(shù)ReadDat()和寫函數(shù)writeDat()的內(nèi)容。
#include
#include
#define MAXNUM 200
int xx[MAXNUM];
int totNum=0; /*文件IN.DAT中共有多少個正整數(shù)*/
int totCnt=0; /*符合條件的正整數(shù)的個數(shù)*/
double totPjz=0.0; /*平均值*/
int ReadDat(void);
void writeDat(void);
void CalValue(void)
{int i,data;
for(i=0;iif(xx[i]>0)
{ totNum++;
data=xx[i]>>1;
if(data%2==0){totCnt++;totPjz+=xx[i];}
}
if(totCnt==0) totPjz=0;
else totPjz/=totCnt;
}
void main()
{
int i;
clrscr();
for(i=0;iif(ReadDat()){
printf("數(shù)據(jù)文件IN.DAT不能打開!\007\n");
return;
}
CalValue();
printf("文件IN.DAT中共有正整數(shù)=%d個\n",totNum);
printf("符合條件的正整數(shù)的個數(shù)=%d個\n",totCnt);
printf("平均值=%.2f\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%6.2f\n",totNum,totCnt,totPjz);
fclose(fp);
}
*****************************************
★題目27(無憂id 57 整數(shù)各位數(shù)字運算題)
已知數(shù)據(jù)文件in.dat中存有300個四位數(shù),并已調(diào)用讀函數(shù)ReadDat()把這些數(shù)存入數(shù)組a中,請編制一函數(shù)jsValue(),其功能是:求出千位數(shù)上的數(shù)減百位數(shù)上的數(shù)減十位數(shù)上的數(shù)減個位數(shù)上的數(shù)大于零的個數(shù)cnt,再把所有滿足此條件的四位數(shù)依次存入數(shù)組b中,然后對數(shù)組b的四位數(shù)按從小到大的順序進行排序,最后調(diào)用寫函數(shù)writeDat()把結果輸出到out.dat文件。
例如:9123,9-1-2-3>0,則該數(shù)滿足條件存入數(shù)組b中,且個數(shù)cnt=cnt+1。
9812,9-8-1-2>0,則該數(shù)不滿足條件忽略。
部分源程序存在文件prog1.c中。
程序中已定義數(shù)組:a[300],b[300],已定義變量:cnt
請勿改動主函數(shù)main()、讀函數(shù)ReadDat()和寫函數(shù)writeDat()的內(nèi)容。
#include
int a[300],b[300],cnt=0;
jsValue()
{ int i,j,qw,bw,sw,gw;
for(i=0;i<300;i++)
{ qw=a[i]/1000; bw=a[i]/100%10;
sw=a[i]%100/10; gw=a[i]%10;
if(qw-bw-sw-gw>0) b[cnt++]=a[i];
}
for(i=0;ifor(j=i+1;jif( b[i]>b[j]) { qw=b[i]; b[i]=b[j]; b[j]=qw;}
}
main()
{
int i;
readDat();
jsValue();
writeDat();
printf("cnt=%d\n",cnt);
for(i=0;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;ifclose(fp);
}
*****************************************
★題目28(無憂id 138整數(shù)各位打散組合運算題)
已知數(shù)據(jù)文件IN.DAT中存有200個四位數(shù),并已調(diào)用讀函數(shù)readDat()把這些數(shù)存入數(shù)組a中,請考生編制一函數(shù)jsVal(),其功能是:把千位數(shù)字和十位數(shù)字重新組成一個新的十位數(shù)ab(新十位數(shù)的十位數(shù)字是原四位數(shù)的千位數(shù)字,新十位數(shù)的個位數(shù)字是原四位數(shù)的十位數(shù)字),以及把個位數(shù)字和百位數(shù)字組成另一個新的十位數(shù)cd(新十位數(shù)的十位數(shù)字是原四位數(shù)的個位數(shù)字,新十位數(shù)的個位數(shù)字是原四位數(shù)的百位數(shù)字),如果新組成的兩個十位數(shù)ab-cd>=0且ab-cd<=10且兩個數(shù)均是奇數(shù),同時兩個新數(shù)的十位數(shù)字均不為零,則將滿足此條件的四位數(shù)按從大到小的順序存入數(shù)組b中,并要計算滿足上述條件的四位數(shù)的個數(shù)cnt。最后main()函數(shù)調(diào)用寫函數(shù)writeDat( )把結果cnt以及數(shù)組b中符合條件的四位數(shù)輸出到OUT.DAT文件中。
注意:部分源程序存在文件prog1.c中。
程序中已定義數(shù)組:a[200],b[200],已定義變量:cnt
請勿改動數(shù)據(jù)文件IN.DAT中的任何數(shù)據(jù)、主函數(shù)main()、讀函數(shù)readDat()和寫函數(shù)writeDat()的內(nèi)容。
#include
#define MAX 200
int a[MAX], b[MAX], cnt = 0 ;
void jsVal()
{int i,j,thou,hun,ten,data,ab,cd;
for(i=0;i<200;i++)
{thou=a[i]/1000; hun=a[i]%1000/100;
ten=a[i]%100/10; data=a[i]%10;
ab=10*thou+ten; cd=10*data+hun;
if((ab-cd)>=0&&(ab-cd)<=10&&ab%2==1&&cd%2==1&&ab>=10&&cd>=10)
b[cnt++]=a[i];
}
for(i=0;ifor(j=i+1;jif(b[i]}
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("滿足條件的數(shù)=%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) ;
}
*****************************************
★題目29(無憂id 139整數(shù)各位打散組合運算題)
已知數(shù)據(jù)文件IN.DAT中存有200個四位數(shù),并已調(diào)用讀函數(shù)readDat()把這些數(shù)存入數(shù)組a中,請考生編制一函數(shù)jsVal(),其功能是:把千位數(shù)字和十位數(shù)字重新組成一個新的十位數(shù)ab(新十位數(shù)的十位數(shù)字是原四位數(shù)的千位數(shù)字,新十位數(shù)的個位數(shù)字是原四位數(shù)的十位數(shù)字),以及把個位數(shù)字和百位數(shù)字組成另一個新的十位數(shù)cd(新十位數(shù)的十位數(shù)字是原四位數(shù)的個位數(shù)字,新十位數(shù)的個位數(shù)字是原四位數(shù)的百位數(shù)字),如果新組成的兩個十位數(shù)ab-cd>=10且ab-cd<=20且兩個數(shù)均是偶數(shù),同時兩個新數(shù)的十位數(shù)字均不為零,則將滿足此條件的四位數(shù)按從大到小的順序存入數(shù)組b中,并要計算滿足上述條件的四位數(shù)的個數(shù)cnt。
最后main()函數(shù)調(diào)用寫函數(shù)writeDat( )把結果cnt以及數(shù)組b中符合條件的四位數(shù)輸出到OUT.DAT文件中。
注意:部分源程序存在文件prog1.c中。
程序中已定義數(shù)組:a[200],b[200],已定義變量:cnt
請勿改動數(shù)據(jù)文件IN.DAT中的任何數(shù)據(jù)、主函數(shù)main()、讀函數(shù)readDat()和寫函數(shù)writeDat()的內(nèi)容。
#include
#define MAX 200
int a[MAX], b[MAX], cnt = 0 ;
void jsVal()
{ int i,j,qw,bw,sw,gw,ab,cd;
for(i=0;i{ qw=a[i]/1000; bw=a[i]/100%10;
sw=a[i]%100/10; gw=a[i]%10;
ab=qw*10+sw; cd=gw*10+bw;
if(ab-cd>=10&&ab-cd<=20&&ab%2==0&&cd%2==0&&qw!=0&&gw!=0)
b[cnt++]=a[i];
}
for(i=0;ifor(j=i+1;jif(b[i]}
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("滿足條件的數(shù)=%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) ;
}
*****************************************
題目30(無憂id 152 整數(shù)統(tǒng)計排序題)
已知數(shù)據(jù)文件IN.DAT中存有200個四位數(shù),并已調(diào)用讀函數(shù)readDat()把這些數(shù)存入數(shù)組a中,請考生編制一函數(shù)jsVal(),其功能是:依次從數(shù)組a中取出一個四位數(shù),如果該四位數(shù)連續(xù)小于該四位數(shù)以后的五個數(shù)且該數(shù)是偶數(shù)(該四位數(shù)以后不滿五個數(shù),則不統(tǒng)計),則統(tǒng)計出滿足此條件的個數(shù)cnt并把這些四位數(shù)按從小到大的順序存入數(shù)組b中,最后調(diào)用寫函數(shù)writeDat( )把結果cnt以及數(shù)組b中符合條件的四位數(shù)輸出到OUT.DAT文件中。
注意:部分源程序存在文件prog1.c中。
程序中已定義數(shù)組:a[200],b[200],已定義變量:cnt
請勿改動數(shù)據(jù)文件IN.DAT中的任何數(shù)據(jù)、主函數(shù)main()、讀函數(shù)readDat()和寫函數(shù)writeDat()的內(nèi)容。
#include
#define MAX 200
int a[MAX], b[MAX], cnt = 0 ;
void jsVal()
{int i,j,flag;
for(i=0;i{for(j=i+1;j<=i+5;j++)
if(a[i]else { flag=0; break;}
if(flag==1) b[cnt++]=a[i];
}
for(i=0;ifor(j=i+1;jif(b[i]>b[j]) {flag=b[i];b[i]=b[j];b[j]=flag;}
}
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("滿足條件的數(shù)=%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) ;
}
*****************************************
★題目31(無憂id 17 結構體運算題)
已知在文件IN.DAT中存有100個產(chǎn)品銷售記錄,每個產(chǎn)品銷售記錄由產(chǎn)品代碼dm(字符型4位),產(chǎn)品名稱mc(字符型10位),單價dj(整型),數(shù)量sl(整型),金額je(長整型)五部分組成。其中:金額=單價*數(shù)量計算得出。函數(shù)ReadDat()是讀取這100個銷售記錄并存入結構數(shù)組sell中。請編制函數(shù)SortDat(),其功能要求:按金額從大到小進行排列,若金額相同,則按產(chǎn)品代碼從大到小進行排列, 最終排列結果仍存入結構數(shù)組sell中,最后調(diào)用函數(shù)WriteDat() 把結果輸出到文件OUT4.DAT中。
部分源程序存在文件prog1.c中。
  請勿改動主函數(shù)main()、讀數(shù)據(jù)函數(shù)ReadDat()和輸出數(shù)據(jù)函數(shù)WriteDat()的內(nèi)容。
#include
#include
#include
#include
#include
#define MAX 100
typedef struct{
char dm[5]; /*產(chǎn)品代碼*/
char mc[11]; /*產(chǎn)品名稱*/
int dj; /*單價*/
int sl; /*數(shù)量*/
long je; /*金額*/
}PRO;
PRO sell[MAX];
void ReadDat();
void WriteDat();
void SortDat()
{int i,j;
PRO xy;
for(i=0;i<99;i++)
for(j=i+1;j<100;j++)
if(sell[i].je{xy=sell[i];sell[i]=sell[j];sell[j]=xy;}
}
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;ifgets(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(void)
{
FILE *fp;
int i;
fp=fopen("OUT4.DAT","w");
for(i=0;iprintf("%s %s %4d %5d %5d\n", sell[i].dm,sell[i].mc,sell[i].dj,sell[i].sl,sell[i].je);
fprintf(fp,"%s %s %4d %5d %5d\n", sell[i].dm,sell[i].mc,sell[i].dj,sell[i].sl,sell[i].je);
}
fclose(fp);
}
*****************************************
☆題目32(無憂id 79 結構體運算題)
已知在文件IN.DAT中存有100個產(chǎn)品銷售記錄,每個產(chǎn)品銷售記錄由產(chǎn)品代碼dm(字符型4位),產(chǎn)品名稱mc(字符型10位),單價dj(整型),數(shù)量sl(整型),金額je(長整型)五部分組成。其中:金額=單價*數(shù)量計算得出。函數(shù)ReadDat()是讀取這100個銷售記錄并存入結構數(shù)組sell中。請編制函數(shù)SortDat(),其功能要求:按產(chǎn)品名稱從大到小進行排列,若產(chǎn)品名稱相等,則按金額從小到大進行排列,最終排列結果仍存入結構數(shù)組sell中
,最后調(diào)用函數(shù)WriteDat()把結果輸出到文件OUT7.DAT中。
部分源程序存在文件prog1.c中。
  請勿改動主函數(shù)main()、讀數(shù)據(jù)函數(shù)ReadDat()和輸出數(shù)據(jù)函數(shù)WriteDat()的內(nèi)容。
#include
#include
#include
#include
#include
#define MAX 100
typedef struct{
char dm[5]; /*產(chǎn)品代碼*/
char mc[11]; /*產(chǎn)品名稱*/
int dj; /*單價*/
int sl; /*數(shù)量*/
long je; /*金額*/
}PRO;
PRO sell[MAX];
void ReadDat();
void WriteDat();
void SortDat()
{int i,j;
PRO xy;
for(i=0;i<99;i++)
for(j=i+1;j<100;j++)
if(strcmp(sell[i].mc,sell[j].mc)<0||strcmp(sell[i].mc,sell[j].mc)==0&&sell[i].je>sell[j].je)
{xy=sell[i];sell[i]=sell[j];sell[j]=xy;}
}
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("OUT7.DAT","w");
for(i=0;i<100;i++){
printf("%s %s %4d %5d %5d\n", sell[i].dm,sell[i].mc,sell[i].dj,sell[i].sl,sell[i].je);
fprintf(fp,"%s %s %4d %5d %5d\n", sell[i].dm,sell[i].mc,sell[i].dj,sell[i].sl,sell[i].je);
}
fclose(fp);
}
*****************************************
★題目33(無憂id 61 方差運算題)
請編制函數(shù)ReadDat()實現(xiàn)從文件IN.DAT中讀取1000個十進制整數(shù)到數(shù)組xx中;請編制函數(shù)Compute()分別計算出xx中偶數(shù)的個數(shù)even,奇數(shù)的平均值ave1,偶數(shù)的平均值ave2以及方差totfc的值,最后調(diào)用函數(shù)WriteDat()把結果輸出到OUT.DAT文件中。
計算方差的公式如下:
N 2
totfc=1/N∑(xx[i]-ave2)
i=1
設N為偶數(shù)的個數(shù),xx[i]為偶數(shù),ave2為偶數(shù)的平均值。
原始數(shù)據(jù)文件存放的格式是:每行存放10個數(shù),并用逗號隔開。(每個數(shù)均大于0且小于等于2000)
部分源程序存在文件prog1.c中。
請勿改動主函數(shù)main()和輸出數(shù)據(jù)函數(shù)writeDat()的內(nèi)容。
#include
#include
#include
#define MAX 1000
int xx[MAX],odd=0,even=0;
double ave1=0.0,ave2=0.0,totfc=0.0;
void WriteDat(void);
int ReadDat(void)
{int i;
FILE *fp;
if((fp=fopen("IN.DAT","r"))==NULL) return 1;
/*********編制函數(shù)ReadDat()的部分************/
for(i=0;i{ fscanf(fp,"%d,",&xx[i]);
if((i+1)%10==0)
fscanf(fp,"\n"); }
/*******************************************/
fclose(fp);
return 0;
}
void Compute(void)
{ int i,yy[MAX];
for(i=0;iyy[i]=0;
for(i=0;iif(xx[i]%2==0) { yy[even++]=xx[i]; ave2+=xx[i];}
else { odd++; ave1+=xx[i];}
if(odd==0) ave1=0;
else ave1/=odd;
if(even==0) ave2=0;
else ave2/=even;
for(i=0;itotfc+=(yy[i]-ave2)*(yy[i]-ave2)/even;
}
void main()
{
int i;
for(i=0;iif(ReadDat()){
printf("數(shù)據(jù)文件IN.DAT不能打開!\007\n");
return;
}
Compute();
printf("OVEN=%d\nAVE1=%f\nAVER2=%f\nTOTFC=%f\n",even,ave1,ave2,totfc);
WriteDat();
}
void WriteDat(void)
{
FILE *fp;
int i;
fp=fopen("OUT.DAT","w");
fprintf(fp,"%d\n%f\n%f\n%f\n",even,ave1,ave2,totfc);
fclose(fp);
}
*****************************************
★☆題目34(無憂id 73,102 素數(shù)題)
無憂id 102 題提供了求素數(shù)isPrime()函數(shù)
程序prog1.c的功能是:選出100以上1000之內(nèi)所有個位數(shù)字與十位數(shù)字之和被10除所得余數(shù)恰是百位數(shù)字的素數(shù)(如293)。計算并輸出上述這些素數(shù)的個數(shù)cnt以及這些素數(shù)值的和sum。 請考生編寫函數(shù)countValue( )實現(xiàn)程序要求,最后調(diào)用函數(shù)writeDAT()把結果cnt和sum輸出到文件bc10.out中。
注意:部分源程序存放在文件prog1.c中。
請勿改動主函數(shù)main( )和輸出數(shù)據(jù)函數(shù)writeDAT()的內(nèi)容。
#include
int cnt, sum ;
void countValue()
{ int i,j,bw,sw,gw;
for(i=100;i<1000;i++)
{ bw=i/100; sw=i%100/10; gw=i%10;
for(j=2;jif(i%j==0) break;
if((i==j) &&(gw+sw)%10==bw) { cnt++; sum+=i;}
}
}
void main()
{
cnt=sum=0;
countValue() ;
printf("素數(shù)的個數(shù)=%d\n", cnt) ;
printf("滿足條件素數(shù)值的和=%d", sum) ;
writeDAT() ;
}
writeDAT()
{
FILE *fp ;
fp = fopen("bc10.out", "w") ;
fprintf(fp, "%d\n%d\n", cnt, sum) ;
fclose(fp) ;
}
輸出結果為:
素數(shù)的個數(shù)=15
滿足條件素數(shù)值的和=6825
*****************************************
★☆題目35(無憂id 20 級數(shù)運算題)
某級數(shù)的前兩項A1=1,A2=1,以后各項具有如下關系:
An=An-2+2An-1(注:n-2與n-1為下標)
下列程序prog1.c的功能是:要求依次對于整數(shù)M=100,1000和10000求出對應的n值,使其滿足:Sn=M,這里Sn=A1+A2+...+An,并依次把n值存入數(shù)組單元b[0],b[1]和b[2]中,請編制jsValue()函數(shù)來實現(xiàn)此功能, 最后調(diào)用函數(shù)writeDat()把數(shù)組b[]中的值輸出到out.dat文件中。
請勿改動主函數(shù)main()和寫函數(shù)writeDat()的內(nèi)容。
#include
int b[3];
jsValue()
{int a1=1,a2=1,a12,sn,k=2;
sn=a1+a2;
while(1)
{a12=a1+2*a2;
if(sn<100&&sn+a12>=100) b[0]=k;
if(sn<1000&&sn+a12>=1000) b[1]=k;
if(sn<10000&&sn+a12>=10000) {b[2]=k;break;}
sn=sn+a12;
a1=a2;a2=a12;
k++;
}
}
main()
{
jsValue();
printf("M=100,n=%d\nM=1000,n=%d\nM=10000,n=%d\n",b[0],b[1],b[2]);
writeDat();
}
writeDat()
{
FILE *fp;
fp=fopen("out.dat","w");
printf("%d\n%d\n%d\n",b[0],b[1],b[2]);
fprintf(fp,"%d\n%d\n%d\n",b[0],b[1],b[2]);
fclose(fp);
}
運行結果為:
M=100,n=6
M=1000,n=9
M=10000,n=11
*****************************************
★☆題目36(無憂id 5 字符替換題)
函數(shù)ReadDat()實現(xiàn)從文件ENG.IN中讀取一篇英文文章,存入到字符串數(shù)組xx中;請編制函數(shù)encryptChar(),按給定的替代關系對數(shù)組xx中的所有字符進行替代,仍存入數(shù)組xx的對應的位置上,最后調(diào)用函數(shù)WriteDat()把結果xx輸出到文件pS6.DAT中。
  替代關系:f(p)=p*11 mod 256(p是數(shù)組中某一個字符的ASCII值,f(p)是計算后新字符的ASCII值),如果計算后f(p)值小于等于32或f(p)對應的字符是數(shù)字0至9,則該字符不變,否則將f(p)所對應的字符進行替代。
  部分源程序存在文件prog1.c中。原始數(shù)據(jù)文件存放的格式是:每行的寬度均小于80個字符。
  請勿改動主函數(shù)main()、讀數(shù)據(jù)函數(shù)ReadDat()和輸出數(shù)據(jù)函數(shù)WriteDat()的內(nèi)容。
#include
#include
#include
#include
unsigned char xx[50][80];
int maxline=0;/*文章的總行數(shù)*/
int ReadDat(void);
void WriteDat(void);
void encryptChar()
{ int i,j;
for(i=0;ifor(j=0;jif(xx[i][j]*11%256<=32||xx[i][j]*11%256>='0'&&xx[i][j]*11%256<='9') continue;
else xx[i][j]=xx[i][j]*11%256;
}
void main()
{
clrscr();
if(ReadDat()){
printf("數(shù)據(jù)文件ENG.IN不能打開!\n\007");
return;
}
encryptChar();
WriteDat();
}
int ReadDat(void)
{
FILE *fp;
int i=0;
unsigned char *p;
if((fp=fopen("eng.in","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("ps6.dat","w");
for(i=0;iprintf("%s\n",xx[i]);
fprintf(fp,"%s\n",xx[i]);
}
fclose(fp);
}
或另一解法:
void encryptChar()
{ int i,j,val;
for(i=0;ifor(j=0;j{ val=xx[i][j]*11%256;
if(val<=32||val>='0'&&val<='9') continue;
else xx[i][j]=val;
}
}
此題還有許多解法,方法可看題8
*****************************************
☆題目37(無憂id 89 字符替換題)
函數(shù)ReadDat()實現(xiàn)從文件ENG.IN中讀取一篇英文文章,存入到字符串數(shù)組xx中;請編制函數(shù)encryptChar(),按給定的替代關系對數(shù)組xx中的所有字符進行替代,仍存入數(shù)組xx的對應的位置上,最后調(diào)用函數(shù)WriteDat()把結果xx輸出到文件PS7.DAT中。
  替代關系:f(p)=p*11 mod 256(p是數(shù)組中某一個字符的ASCII值,f(p)是計算后新字符的ASCII值),如果原字符是大寫字母或計算后f(p)值小于等于32,則該字符不變,否則將f(p)所對應的字符進行替代。
  部分源程序存在文件prog1.c中。原始數(shù)據(jù)文件存放的格式是:每行的寬度均小于80個字符。
  請勿改動主函數(shù)main()、讀數(shù)據(jù)函數(shù)ReadDat()和輸出數(shù)據(jù)函數(shù)WriteDat()的內(nèi)容。
#include
#include
#include
#include
unsigned char xx[50][80];
int maxline=0;/*文章的總行數(shù)*/
int ReadDat(void);
void WriteDat(void);
void encryptChar()
{ int i,j;
for(i=0;ifor(j=0;jif(xx[i][j]*11%256<=32||xx[i][j]>='A'&&xx[i][j]<='Z') continue;
else xx[i][j]=xx[i][j]*11%256;
}
void main()
{
clrscr();
if(ReadDat()){
printf("數(shù)據(jù)文件ENG.IN不能打開!\n\007");
return;
}
encryptChar();
WriteDat();
}
int ReadDat(void)
{
FILE *fp;
int i=0;
unsigned char *p;
if((fp=fopen("eng.in","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("ps7.dat","w");
for(i=0;iprintf("%s\n",xx[i]);
fprintf(fp,"%s\n",xx[i]);
}
fclose(fp);
}
此題還有許多解法,方法可看題8
*****************************************
★題目38(無憂id 81 結構體運算題)
已知在文件IN.DAT中存有100個產(chǎn)品銷售記錄,每個產(chǎn)品銷售記錄由產(chǎn)品代碼dm(字符型4位),產(chǎn)品名稱mc(字符型10位),單價dj(整型),數(shù)量sl(整型),金額je(長整型)五部分組成。其中:金額=單價*數(shù)量計算得出。函數(shù)ReadDat()是讀取這100個銷售記錄并存入結構數(shù)組sell中。請編制函數(shù)SortDat(),其功能要求:按產(chǎn)品代碼從小到大進行排列,若產(chǎn)品代碼相同,則按金額從小到大進行排列,最終排列結果仍存入結構數(shù)組sell中,最后調(diào)用函數(shù)WriteDat()把結果輸出到文件OUT6.DAT中。
部分源程序存在文件prog1.c中。
  請勿改動主函數(shù)main()、讀數(shù)據(jù)函數(shù)ReadDat()和輸出數(shù)據(jù)函數(shù)WriteDat()的內(nèi)容。
#include
#include
#include
#include
#include
#define MAX 100
typedef struct{
char dm[5]; /*產(chǎn)品代碼*/
char mc[11]; /*產(chǎn)品名稱*/
int dj; /*單價*/
int sl; /*數(shù)量*/
long je; /*金額*/
}PRO;
PRO sell[MAX];
void ReadDat();
void WriteDat();
void SortDat()
{int i,j;
PRO xy;
for(i=0;i<99;i++)
for(j=i+1;j<100;j++)
if(strcmp(sell[i].dm,sell[j].dm)>0||strcmp(sell[i].dm,sell[j].dm)==0&&sell[i].je>sell[j].je)
{xy=sell[i];sell[i]=sell[j];sell[j]=xy;}
}
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(void)
{
FILE *fp;
int i;
fp=fopen("OUT6.DAT","w");
for(i=0;i<100;i++){
printf("%s %s %4d %5d %5d\n", sell[i].dm,sell[i].mc,sell[i].dj,sell[i].sl,sell[i].je);
fprintf(fp,"%s %s %4d %5d %5d\n", sell[i].dm,sell[i].mc,sell[i].dj,sell[i].sl,sell[i].je);
}
fclose(fp);
}
*****************************************
★☆題目39(無憂id 63 選票問題)
現(xiàn)有一個10個人100行的選票數(shù)據(jù)文件IN.DAT,其數(shù)據(jù)存放的格式是每條記錄的長度均為10位,第一位表示第一個人的選中情況,第二位表示第二個人的選中情況,依此類推 :內(nèi)容均為字符0和1,1表示此人被選中,0表示此人未被選中,全選或不選均為無效的選票。給定函數(shù)ReadDat()的功能是把選票數(shù)據(jù)讀入到字符串數(shù)組xx中。請編制函數(shù)CountRs()來統(tǒng)計每個人的選票數(shù)并把得票數(shù)依次存入yy[0]到y(tǒng)y[9]中。把結果yy輸出到文件OUT.DAT中。
部分源程序存在文件prog1.c中。
請勿改動主函數(shù)main()、讀數(shù)據(jù)函數(shù)ReadDat()和輸出數(shù)據(jù)函數(shù)writeDat()的內(nèi)容。
#include
char xx[100][11];
int yy[10];
int ReadDat(void);
void WriteDat(void);
void CountRs(void)
{ int i,j,count;
for(i=0;i<100;i++)
{ count=0;
for(j=0;j<10;j++)
if(xx[i][j]=='1') count++;
if(count==0||count==10) continue;
for(j=0;j<10;j++)
if(xx[i][j]=='1') yy[j]++;
}
}
void main()
{
int i;
for(i=0;i<10;i++)yy[i]=0;
if(ReadDat()){
printf("選票數(shù)據(jù)文件IN.DAT不能打開!\n\007");
return;
}
CountRs();
WriteDat();
}
int ReadDat(void)
{
FILE *fp;
int i;
if((fp=fopen("IN.DAT","r")) == NULL) return 1;
for(i=0;i<100;i++){
if(fgets(xx[i],11,fp)==NULL)return 1;
xx[i][10]='\0';
}
fclose(fp);
return 0;
}
void WriteDat(void)
{
FILE *fp;
int i;
fp=fopen("OUT.DAT","w");
for(i=0;i<10;i++){
fprintf(fp,"%d\n",yy[i]);
printf("第%d個人的選票數(shù)=%d\n",i+1,yy[i]);
}
fclose(fp);
}
*****************************************
★題目40(無憂id 43 整數(shù)統(tǒng)計運算題)
已知在文件IN.DAT中存有若干個(個數(shù)<200)四位數(shù)字的正整數(shù),函數(shù)ReadDat()讀取這若干個正整數(shù)并存入數(shù)組xx中。請編制函數(shù)CalValue(),其功能要求:
1、求出這文件中共有多少個正整數(shù)totNum;
2、求這些數(shù)中的各位數(shù)字之和是偶數(shù)的數(shù)的個數(shù)totCnt,
以及滿足此條件的這些數(shù)的算術平均值totPjz,最后調(diào)用函數(shù)writeDat()把所求的結果輸出到文件OUT.DAT中。
部分源程序存在文件prog1.c中。
請勿改動主函數(shù)main()、讀函數(shù)ReadDat()和寫函數(shù)writeDat()的內(nèi)容。
#include
#include
#define MAXNUM 200
int xx[MAXNUM];
int totNum=0; /*文件IN.DAT中共有多少個正整數(shù)*/
int totCnt=0; /*符合條件的正整數(shù)的個數(shù)*/
double totPjz=0.0; /*平均值*/
int ReadDat(void);
void writeDat(void);
void CalValue(void)
{ int i,qw,bw,sw,gw;
for(i=0;iif(xx[i]>0)
{ totNum++;
qw=xx[i]/1000;
bw=xx[i]/100%10;
sw=xx[i]%100/10;
gw=xx[i]%10;
if((qw+bw+sw+gw)%2==0) { totCnt++; totPjz+=xx[i];}
}
if(totCnt==0) totPjz=0;
else totPjz/=totCnt;
}
void main()
{
int i;
clrscr();
for(i=0;iif(ReadDat()){
printf("數(shù)據(jù)文件IN.DAT不能打開!\007\n");
return;
}
CalValue();
printf("文件IN.DAT中共有正整數(shù)=%d個\n",totNum);
printf("符合條件的正整數(shù)的個數(shù)=%d個\n",totCnt);
printf("平均值=%.2f\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%6.2f\n",totNum,totCnt,totPjz);
fclose(fp);
}
*****************************************
★☆題目41(無憂id 51 SIX/NINE問題)
下列程序prog1.c的功能是:計算出自然數(shù)SIX和NINE,它們滿足的條件是SIX+SIX+SIX=NINE+NINE的個數(shù)cnt以及滿足此條件所有的SIX與NINE的和SUM。請編寫函數(shù)countValue()實現(xiàn)程序的要求,最后調(diào)用函數(shù)writeDat()把結果cnt和sum,輸出到文件OUT15.DAT中。
其中的S,I,X,N,E各代表一個十進制數(shù)字。
部分源程序存在文件prog1.c中。
請勿改動主函數(shù)main()和輸出數(shù)據(jù)函數(shù)writeDat()的內(nèi)容。
#include
int cnt,sum;
void countValue()
{ int s,i,x,n,e,six,nine;
for(s=1;s<10;s++)
for(i=0;i<10;i++)
for(x=0;x<10;x++)
for(n=1;n<10;n++)
for(e=0;e<10;e++)
{ six=s*100+i*10+x;
nine=n*1000+i*100+n*10+e;
if(3*six==2*nine)
{ cnt++;sum+=six+nine;}
}
}
void main()
{
cnt=sum=0;
countValue();
printf("滿足條件的個數(shù)=%d\n",cnt);
printf("滿足條件所有的SIX與NINE的和=%d\n",sum);
writeDat();
}
writeDat()
{
FILE *fp;
fp=fopen("OUT15.DAT","w");
fprintf(fp,"%d\n%d\n",cnt,sum);
fclose(fp);
}
另一種經(jīng)典解法:
void countValue()
{ int i;
for(i=666;i<=999;i=i+2)
if((i/10%10==(3*i/2)/100%10)&&((3*i/2)/1000==(3*i/2)%100/10))
{cnt++;sum+=i+3*i/2;}
}
運算結果為:
滿足條件的個數(shù)=4
滿足條件所有的SIX與NINE的和=9430
*****************************************
題目42 結構體運算題
跟南開12一樣,將“從小到大”改成了“從大到小”
已知在文件IN.DAT中存有100個產(chǎn)品銷售記錄,每個產(chǎn)品銷售記錄由產(chǎn)品代碼dm(字符型4位),產(chǎn)品名稱mc(字符型10位),單價dj(整型),數(shù)量sl(整型),金額je(長整型)五部分組成。其中:金額=單價*數(shù)量計算得出。函數(shù)ReadDat()是讀取這100個銷售記錄并存入結構數(shù)組sell中。請編制函數(shù)SortDat(),其功能要求:按產(chǎn)品名稱從大到小進行排列,若產(chǎn)品名稱相等,則按金額從大到小進行排列,最終排列結果仍存入結構數(shù)組sell中,最后調(diào)用函數(shù)WriteDat()把結果輸出到文件OUT5.DAT中。
部分源程序存在文件prog1.c中。
  請勿改動主函數(shù)main()、讀數(shù)據(jù)函數(shù)ReadDat()和輸出數(shù)據(jù)函數(shù)WriteDat()的內(nèi)容。
#include
#include
#include
#include
#include
#define MAX 100
typedef struct{
char dm[5]; /*產(chǎn)品代碼*/
char mc[11]; /*產(chǎn)品名稱*/
int dj; /*單價*/
int sl; /*數(shù)量*/
long je; /*金額*/
}PRO;
PRO sell[MAX];
void ReadDat();
void WriteDat();
void SortDat()
{int i,j;
PRO xy;
for(i=0;i<99;i++)
for(j=i+1;j<100;j++)
if(strcmp(sell[i].mc,sell[j].mc)<0||strcmp(sell[i].mc,sell[j].mc)==0&&sell[i].je{xy=sell[i];sell[i]=sell[j];sell[j]=xy;}
}
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("OUT5.DAT","w");
for(i=0;i<100;i++){
printf("%s %s %4d %5d %5d\n",sell[i].dm,sell[i].mc,sell[i].dj,sell[i].sl,sell[i].je);
fprintf(fp,"%s %s %4d %5d %5d\n", sell[i].dm,sell[i].mc,sell[i].dj,sell[i].sl,sell[i].je);
}
fclose(fp);
}
*****************************************
☆題目43(無憂id 82 字符排序題)
無憂id 82題(只是將結果按“從大到小”排序)
函數(shù)ReadDat()實現(xiàn)從文件in.dat中讀取20行數(shù)據(jù)存放到字符串數(shù)組xx中(每行字符串長度均小于80)。請編制函數(shù)jsSort(),其函數(shù)的功能是:以行為單位對字符串變量的下標為奇數(shù)的字符按其ASCII值從小到大的順序進行排序,排序后的結果仍按行重新存入字符串數(shù)組xx中,最后調(diào)用函數(shù)WriteDat()把結果xx輸出到文件out.dat中。
  例如:位置   0 1 2 3 4 5 6 7 
     源字符串 a b c d e f g h
則處理后字符串 a h c f e d g b
  部分源程序存在文件prog1.c中。
  請勿改動主函數(shù)main()、讀數(shù)據(jù)函數(shù)ReadDat()和輸出數(shù)據(jù)函數(shù)WriteDat()的內(nèi)容。
#include
#include
#include
char xx[20][80];
void jsSort()
{int i,j,k,strl;
char ch;
for(i=0;i<20;i++)
{ strl=strlen(xx[i]);
for(j=1;jfor(k=j+2;kif(xx[i][j]>xx[i][k]) { ch=xx[i][j];xx[i][j]=xx[i][k];xx[i][k]=ch;}
}
}
void main()
{
read

展開更多......

收起↑

資源預覽

<pre id="tfb94"><li id="tfb94"></li></pre>

<bdo id="tfb94"><rt id="tfb94"></rt></bdo>
  • <menu id="tfb94"><dl id="tfb94"></dl></menu><i id="tfb94"><acronym id="tfb94"><sub id="tfb94"></sub></acronym></i>

    1. 主站蜘蛛池模板: 乳山市| 梁山县| 平顺县| 敖汉旗| 临桂县| 时尚| 长宁县| 郁南县| 榆中县| 阿鲁科尔沁旗| 怀仁县| 磐安县| 抚远县| 巴林左旗| 大连市| 本溪| 康马县| 卢氏县| 嘉兴市| 乌拉特中旗| 都昌县| 盐池县| 五指山市| 海林市| 建昌县| 会昌县| 楚雄市| 富阳市| 遵义县| 丁青县| 濮阳市| 剑河县| 乌兰县| 高清| 和政县| 威远县| 临安市| 延长县| 绩溪县| 大关县| 泰兴市|