经典实验--结构体和文件结合操作


·一、实验目的

  实验目的:熟练文件和结构体数组的综合应用

·二、实验原理

   已知多组产品记录,要求对每组销售记录进行排序,排序规则如下:

(1)产品代码按ASCII码从小到大排序

(2)如果产品代码相同,则按金额从大到小排序。同时找出该销售记录中金额总合最大的产品代码。

  

·三、主要数据结构和算法

1、数据结构的设计

struct SaleRecord                              //定义结构体 SaleRecord

    char id[5];                                //产品代码+'\0'

    char name[11];                             //产品名称+'\0'

    int price;                                 //单价

    int num;                                   //数量

    long total;                                //金额

};

2、算法分析

·四、实验结果及分析

1、测试结果截图

(1)

输入文件

输出文件

(2)

输入文件

输出文件

(3)      

输入文件

输出文件

·五、心得体会

  首先,要看清题目要求,不能盲目去写程序,然后仔细分析题目内容;其次,学会在文件中输入数据,并按要求对元素的数据进行排序,例如冒泡法排序,总数最大值的比较;最后,通过测试用例的结果来检测程序的正确与否。

 

·六、源文件

#include 
#include 
#include <string.h>
#define N 1500
struct SaleRecord                               //定义结构体 SaleRecord
{
    char id[5];                                //产品代码+'\0'
    char name[11];                             //产品名称+'\0'
    int price;                                 //单价
    int num;                                   //数量
    long total;                                //金额
};
char max_id[5]="";
void write(struct SaleRecord *sr,int count);   //写一组数据记录
void sort(struct SaleRecord *sr,int count);    //对一组数据记录排序
int main()
{
    struct SaleRecord *sr = NULL;               // 声明 SaleRecord *sr 为空 
    FILE *fp;                                  //定义一个指向文件的指针fp 
    int count;
    //读文件
    
    fp = fopen("C:\\A_01_in.dat","r");         //打开文件
    if(fp==NULL)                               //如果文件为空,输出"file open error"
    {
        printf("file open error");
        return 0;
    }

    while(1)                                   //循环操作 
    {
        if(feof(fp))                           //直到文件结束,然后停止 
            break;
        fscanf(fp,"%d\n",&count);               //从文件输入组数count 
        if(count>0)                            //组数大于0 
        {
            sr = (struct SaleRecord *)malloc(count * sizeof(struct SaleRecord));//分配空间 
            if(sr==NULL)                       //如果文件为空,则返回 
                return 0;
            for(int i=0;i//从文件中输入每组中的各项数据 
                fscanf(fp,"%s %s %d %d %ld\n",sr[i].id,sr[i].name,&sr[i].price,&sr[i].num,&sr[i].total);

            sort(sr,count);                       //冒泡法排序 
get_max(sr,count);                       //寻找金额总和最大值
            write(sr,count);                   //写一组数据记录
            free(sr);                           //释放空间 
        }
    }
    
    fclose(fp);//关闭文件

}
void get_max(struct SaleRecord *sr,int count)
{
    char temp_id[5];
    long max = 0;
    long totalsum=0;
    //取第一条记录的产品代码给temp_id,金额给totalsum 
    //读下一条记录,如果和上一条记录产品代码相同  totalsum+=本条记录的金额
    //如果和上一条产品代码不同,把max的值和totalsum比较,max取大值,然后将totalsum=0;
    int i;
    strcpy(temp_id,sr[0].id);
    strcpy(max_id,temp_id);
    for(i=0;i)
    {
        if(strcmp(temp_id,sr[i].id)==0)
        {
            totalsum+=sr[i].total;            
        }
        else
        {
            if(max<totalsum)
            {
                max = totalsum;
                strcpy(max_id,temp_id);
            }
        
            totalsum = 0;
            strcpy(temp_id ,sr[i].id);
            totalsum+=sr[i].total;                
        }
            
    }
    if(max<totalsum)
    {
        max = totalsum;
        strcpy(max_id,temp_id);
    }     
}
void sort(struct SaleRecord *sr,int count)//冒泡法排序排
{
    
    int i,j;
    int t;
    struct SaleRecord temp;
    long s;
    int k;
    k = 0;
    for(j=0;j1;j++)
    {
        for(i=0;i1;i++)
        {
            t = strcmp(sr[i].id,sr[i+1].id);
            if(t>=0)
            {
                if(t==0)                //如果ASCII相同,则按金额排序 
                {
                    if(sr[i].total1].total)
                    {
                        temp = sr[i];
                        sr[i] = sr[i+1];
                        sr[i+1] = temp;

                    }

                }
                else                   //ASCII不同则按ASCII排序 

                {
                        temp = sr[i];
                        sr[i] = sr[i+1];
                        sr[i+1] = temp;


                }

            }
            
        }

        if(sr[j].total>sr[k].total)        //寻找金额最大的产品 
            k = j;
    }
    strcpy(max_id,sr[k].id);            //并金额最大的产品的代码复制到max_id中 


}


void write(struct SaleRecord *sr,int count) //写一组数据记录
{
    FILE *fp;
    
    fp = fopen("C:\\A_01_out.dat","a");        //追加打开文件
    fprintf(fp,"%d\n",count);                //从文件输入 count 

    for(int i=0;i//向文件中输出count组数据 
        fprintf(fp,"%s,%s,%d,%d,%ld\n",sr[i].id,sr[i].name,sr[i].price,sr[i].num,sr[i].total); 
    fprintf(fp,"%s\n",max_id);              //向文件中输出max_id 
    fclose(fp);                             //关闭文件
    
}

相关