问题 问答题

试题要求如图10.5所示。

部分源程序内容如下:

#include<stdio.h>

#include<mem.h>

#include<string.h>

#include<stdlib.h>

#define MAX 100

typedef struct

char dm[5]; /*产品代码*/

char mc[11]; /*产品名称*/

int dj; /*单价*/

int s1; /*数量*/

long je; /*金额*/

PRO;

PRO sell[MAX];

void ReadDat();

void WriteDat();

void SortDat()

void main()

mem$et(sell, 0, siZeOf(Sell));

ReadDat();

SortDat();

WriteDat();

void ReadDat()

FTLE *fp;

char str[80], ch[11];

int i;

fp=fopen("K:\\k06\\35010006\\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=Eopen("K:\\k06\\35010006\\OUT.DA2", "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);

答案

参考答案:

void SortDat()

int i, j;

PRO temp;

for(i=0; i<II; i++)

for(j=i+A; j<A00; j++){

if(sell[i].je<sell[j].je)

{

temp=s[i];

s[i]=s[j];

s[j]=temp;

eise if(sell[i].je==sell[j].je)

if(strcmp(sell[i].dm, Sell[j].dm)>0)

{

temp=s[i];

s[i]=s[j];

s[j]=temp;

}

}

}

解析:

此题的解题中的排序方法是冒泡排序法,是先按金额的大小进行排序,如果遇到金额相等时就按代码从小到大进行排序。注意strcmp(x,y)函数的值是x>y时,为正数,x=y时为零,x<y时为负数。

单项选择题
阅读理解

Writer/ Time
Topic: Who’s a better singer, Andy Lau (刘德华) or Jacky Cheung (张学友)?
Jim
8-15-2006
5:25 pm.
Some people say Jacky is the best Chinese singer. I don’t think so! Andy is the best. I went to his concert last year. It was so wonderful, I almost cried! OK, I did cry, but that’s because Andy is so handsome!
Tom
9-3-2006
4:38 pm.
I disagree with Jim. Andy sounds like every other singer. His voice is nothing special. On the other hand, Jacky’s voice is unique, and his songs are so romantic!
Linda
9-10-2006
8:54 pm.
I think they’re about the same. But Andy is a better actor. I’ve seen almost all of his movies. Remember the one where he was an airplane pilot, and his plane crashed near a farm? I love that movie!
John
9-16-2006
10:25 pm.
I also think Jacky is a better singer. I prefer his Cantonese (粤语) songs. Maybe they sound better because Cantonese is his first language.
Tom
10-2-2006
10:12 am.
I agree. Jacky’s Cantonese songs are better than his Mandarin (华语) songs. (Though, I still like all his songs!)
小题1:Who likes Andy Lau the best?      .
A.JimB.TomC.LindaD.John
小题2:Why did Jim cry? Because      .
A.she was sad at a concert.B.she had a broken heart.
C.she was angry at Jacky CheungD.she thought Andy Lau was very good looking
小题3:What’s Linda’s opinion?      .
A.Andy is a better singer.B.Jacky is a better singer.
C.They both sing equally well.D.Jacky is a better actor.
小题4:Why does John think Jacky’s Cantonese songs sound the best? Because        .
A.Jacky has no experience singing in Mandarin.
B.it’s his native language
C.his mandarin pronunciation is bad
D.Jacky’s Mandarin songs are boring.