C language structure calculation

100 there is known sales IN.DAT recorded in the file, the record for each product sold by the DM product code (character 4), product name mc (character 10), monovalent dj (integer), the number of sl (int), the amount of je (long integer) of five parts.

Where: Amount = Price * Quantity calculated. ReadDat function () which is read and stored 100 sales records in the array of structures sell.

Please prepare function SortDat (), its functional requirements: by product name from small to large order, if the product name is equal to the amount of press from small to large order, the final result is still stored in the structure arranged in an array sell, after the function call WriteDat ( ) the output to a file OUT5.DAT in.
#include <stdio.h>

#include <mem.h>

#include <string.h>

#include <conio.h>

#include <stdlib.h>

#define MAX 100

typedef struct{

char dm [5]; / Product Code /

char mc [11]; / Name /

int dj; / Price /

int sl; / Number /

long je; / Amount /

}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);

}

Published 239 original articles · won praise 3 · Views 3151

Guess you like

Origin blog.csdn.net/it_xiangqiang/article/details/105176816