首页 > 其他 > 详细

结构体和文件结合操作

时间:2021-06-07 09:22:09      阅读:33      评论:0      收藏:0      [点我收藏+]

一、实验目的

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

二、实验原理

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

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

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

(3)输入输出文件见附件。

三、主要数据结构和算法

1、数据结构的设计(写出构造的数据类型,即自己定义的结构体)

struct SaleRecord

{             

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

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

    int price;//单价

    int num;//数量

    long total;//金额

}

2、算法分析(算法用流程图或自然语言描述)

1.打开in文件。判断是否能打开。

2.将文件内容输入到屏幕。

3.对数据进行排序。

       (1)先对ASKII码值进行排序。

       (2)对ASKII码值相同的结构体 进行total值从大到小排序。

       (3)找出total最大值的id。

4.打开out文件。判断是否能打开。

5.将排完序的内容写进文件。

四、实验结果及分析

1、源程序(见附录)

2、测试结果截图 写出测试程序的数据,至少写三组数据,并把每组测试结果截图)

 

技术分享图片

 

技术分享图片

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct SaleRecord
{
    char id[5];//产品代码+‘\0‘
    char name[11];//产品名称+‘\0‘
    int price;//单价
    int num;//数量
    long total;//金额
};

char max_id[5]="";
int max1;
void write(struct SaleRecord *sr,int count);//写一组数据记录
void sort(struct SaleRecord *sr,int count);//对一组数据记录排序

int main()
{
    FILE *fp;
    struct SaleRecord *sr;
    int count;
    int i;
    fp = fopen("D:\\A_01_in.dat","r");
    if(!fp)
    {
        printf("文件打开失败.");
        return 0;
    }
    while(1)
    {

        fscanf(fp,"%d\n",&count);
        if(count>0)
        {
            sr = (struct SaleRecord *)malloc (count*sizeof(struct SaleRecord));
            for(i=0;i<count;i++)
            {
                fscanf(fp,"%s %s %d %d %ld\n",sr[i].id,sr[i].name,&sr[i].price,&sr[i].num,&sr[i].total);
                
                if(feof(fp))
                    break;
            }
            sort(sr,count);//对结构体数组元素按照字段名排序
            write(sr,count);
            free(sr);
            if(feof(fp))
                break;
        }
    }
    fclose(fp);
    
    return 0;
}
void sort(struct SaleRecord *sr,int count)//排序
{
    int max;
    int i,j;
    int t;
    int k;
    struct SaleRecord temp;
    for(i=0;i<count-1;i++)//对ASKII码值进行排序。
    {
        t=i;
        for(j=i+1;j<count;j++)
        {
            if(strcmp(sr[t].id,sr[j].id)>0)
            {
                t=j;
        
            }
        }
        if(t!=i) 
        {
            temp=sr[t];
            sr[t]=sr[i];
            sr[i]=temp;
        }    
    }
    for(i=0;i<count-1;i++)//对ASKII码值相同的结构体 进行total值从大到小排序。
    {
        for(j=i+1;j<count;j++)
        {
            if(strcmp(sr[i].id,sr[j].id)==0)
            {
                if(sr[i].total<sr[j].total)
                {
                    temp=sr[i];
                    sr[i]=sr[j];
                    sr[j]=temp;
                }
            }
        }
    }
    
        strcpy(max_id,sr[0].id);// 找出total最大值的id。
        
        k=0;
        for(j=1;j<count;j++)
        {
            if(sr[k].total<sr[j].total)
            {
                k=j;
                strcpy(max_id,sr[k].id);
            }
        }

}
void write(struct SaleRecord *sr,int count)//将排完后的数据写进文件
{
    int n=1;
    FILE * fp;
    fp = fopen("D:\\A_01_out.dat","a");
    if(!fp)
    {
        printf("文件打开失败.");
        
    }
    int i,j; 
    fprintf(fp,"%d\n",count);                                                     
    for(i=0;i<count;i++)
    {
        
        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);
    
    fclose(fp);

        
}

 

结构体和文件结合操作

原文:https://www.cnblogs.com/1472683788wan/p/14830268.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!