C语言 读文件 并对数据排序输出
4个回答
展开全部
#include <stdio.h>
#include <stdlib.h>
struct node
{
char id[100];
char name[20];
int year;
int month;
int day;
double ton;
char city[80];
}log[1005];
int count;
int cmp(const void *a , const void *b)
{
node * c = (node *) a;
node * d = (node *) b;
if(c -> year != d -> year)
return (c->year) - (d->year);
if(c -> month != d -> month)
return (c->month) - (d->month);
return (c->day) - (d->day);
}
int loadfile()
{
FILE *fp = NULL;
fp = fopen("file15.txt","r");
if(fp == NULL)
{
printf("load file15.txt error!\n");
return -1;
}
count = 0;
while(!feof(fp))
{
fscanf(fp , "%s %s %d-%d-%d %lf %s",log[count].id,log[count].name,log[count].year,log[count].month,log[count].day,log[count].ton,log[count].city);
count ++;
}
return 0;
}
void output()
{
int i;
for(i=0;i<count;i++)
{
printf("%s\t%s\t%d-%d-%d\t%lf\t%s\n",log[i].id,log[i].name,log[i].year,log[i].month,log[i].day,log[i].ton,log[i].city);
}
}
int main()
{
loadfile();
qsort(log,count,sizeof(node),cmp);
output();
return 0;
}
注释和菜单栏你自己加吧,主要的部分给你写了,菜单栏就直接printf 和 scanf完成就可以
更多追问追答
追问
代码运行闪一下就退出了。output里printf打不出来啊
追答
你.c文件的同级目录下有没有file15.txt文件?
2014-09-17
展开全部
struct INFO {
CTime time;
char id[100];
char name[100];
char strtime[100];
char val[100];
char city[100];
};
void CDlgtestDlg::OnOK()
{
// TODO: Add extra validation here
//CDialog::OnOK();
int i,j;
int y=0,m=0,d=0;
int count=0;
CString str;
CStdioFile file;
INFO temp;
INFO *info=NULL;
file.Open("file15.txt",CFile::modeRead);
while (file.ReadString(str)){
count++; //统计有多少行
}
info=new INFO [count];
file.SeekToBegin();
for (i=0;i<count;i++){
file.ReadString(str);
sscanf(str,"%s %s %s %s %s",info[i].id,info[i].name,info[i].strtime,info[i].val,info[i].city);
sscanf(info[i].strtime,"%d-%d-%d",&y,&m,&d);
info[i].time=CTime(y,m,d,0,0,0);
}
file.Close();
// 按日期排序
for (i=0;i<count-1;i++){
for (j=i+1;j<count;j++){
if (info[i].time>info[j].time){
temp=info[i];
info[i]=info[j];
info[j]=temp;
}
}
}
// 显示
// List Control Report模式
m_list.SetExtendedStyle( m_list.GetExtendedStyle() | LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
LV_COLUMN lvcol;
int nCount = m_list.GetHeaderCtrl ()->GetItemCount ();
for(i=0; i<nCount; i++){
m_list.DeleteColumn (0);
}
m_list.DeleteAllItems ();
lvcol.mask = LVCF_FMT|LVCF_TEXT|LVCF_WIDTH;
lvcol.fmt = LVCFMT_LEFT;
CRect rect;
m_list.GetWindowRect(rect);
int width=rect.Width()/5;
lvcol.cx = width;
lvcol.pszText = "编号";
lvcol.iSubItem = 0;
m_list.InsertColumn (0, &lvcol);
lvcol.pszText = "姓名";
lvcol.iSubItem = 1;
m_list.InsertColumn (1, &lvcol);
lvcol.pszText = "日期";
lvcol.iSubItem = 2;
m_list.InsertColumn (2, &lvcol);
lvcol.pszText = "重量";
lvcol.iSubItem = 3;
m_list.InsertColumn (3, &lvcol);
lvcol.pszText = "城市";
lvcol.iSubItem = 4;
m_list.InsertColumn (4, &lvcol);
for (i=0;i<count;i++){
m_list.InsertItem(LVIF_TEXT|LVIF_STATE, i, info[i].id, 0, LVIS_SELECTED,0,0);
m_list.SetItemText(i,1,info[i].name);
m_list.SetItemText(i,2,info[i].strtime);
m_list.SetItemText(i,3,info[i].val);
m_list.SetItemText(i,4,info[i].city);
}
m_list.Invalidate();
delete [] info;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 10000
int main(){
FILE *pf;
char filename[]="file15.txt";
char bh[N][10], xm[N][15], rq[N][11], dz[N][20], ct[20];
float sl[N], ft;
int i, j, k, n=0;
pf=fopen(filename,"r");
if(!pf){
printf("打开文件时出错!\n");
return 1;
}
fscanf(pf,"%s",bh[n]);
while(!feof(pf)){
fscanf(pf,"%s%s%f%s",xm[n],rq[n],&sl[n],dz[n]);
n++;
fscanf(pf,"%s",bh[n]);
}
fclose(pf);
for(i=0; i<n-1; i++){
k=i;
for(j=i+1; j<n; j++)
if(strcmp(rq[j],rq[k])<0)
k=j;
if(k!=i){
strcpy(ct,bh[k]); strcpy(bh[k],bh[i]); strcpy(bh[i],ct);
strcpy(ct,xm[k]); strcpy(xm[k],xm[i]); strcpy(xm[i],ct);
strcpy(ct,rq[k]); strcpy(rq[k],rq[i]); strcpy(rq[i],ct);
strcpy(ct,dz[k]); strcpy(dz[k],dz[i]); strcpy(dz[i],ct);
ft=sl[k]; sl[k]=sl[i]; sl[i]=ft;
}
}
for(i=0; i<n; i++)
printf("%s\t%s\t%s\t%.2f\t%s\n",bh[i],xm[i],rq[i],sl[i],dz[i]);
return 0;
}
#include<stdlib.h>
#include<string.h>
#define N 10000
int main(){
FILE *pf;
char filename[]="file15.txt";
char bh[N][10], xm[N][15], rq[N][11], dz[N][20], ct[20];
float sl[N], ft;
int i, j, k, n=0;
pf=fopen(filename,"r");
if(!pf){
printf("打开文件时出错!\n");
return 1;
}
fscanf(pf,"%s",bh[n]);
while(!feof(pf)){
fscanf(pf,"%s%s%f%s",xm[n],rq[n],&sl[n],dz[n]);
n++;
fscanf(pf,"%s",bh[n]);
}
fclose(pf);
for(i=0; i<n-1; i++){
k=i;
for(j=i+1; j<n; j++)
if(strcmp(rq[j],rq[k])<0)
k=j;
if(k!=i){
strcpy(ct,bh[k]); strcpy(bh[k],bh[i]); strcpy(bh[i],ct);
strcpy(ct,xm[k]); strcpy(xm[k],xm[i]); strcpy(xm[i],ct);
strcpy(ct,rq[k]); strcpy(rq[k],rq[i]); strcpy(rq[i],ct);
strcpy(ct,dz[k]); strcpy(dz[k],dz[i]); strcpy(dz[i],ct);
ft=sl[k]; sl[k]=sl[i]; sl[i]=ft;
}
}
for(i=0; i<n; i++)
printf("%s\t%s\t%s\t%.2f\t%s\n",bh[i],xm[i],rq[i],sl[i],dz[i]);
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用C还是C++?回答一下我帮你弄
追问
C语言谢谢
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询