成绩排行,从文件读取学生信息,学号、题目数、分数。按题目数从大到小排,题目数相同时,分数小的在前。//成绩排行 //输入 //CS00000001 4 110 //CS00000002 4 120 /

//成绩排行
//输入
//CS00000001 4 110
//CS00000002 4 120
//CS00000003 5 150
//输出
//CS00000003 5 150
//CS00000001 4 110
//CS00000002 4 120

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;

const int N=100;

struct student{
char bianhao[N];
 int number;
 int score;
}st[N];

bool comp1( student a, student b)
{
	if(a.number>b.number)
		return true;
	else 
		if(a.number==b.number)
		{
			if(a.score<b.score)
				return true;
			else
				return false;
		}
		else  return false;
}

int main()
{
    FILE *fp;
    char str[N];	
	int i=0,j=0,count=0;
    if((fp=fopen("123.txt","r"))==NULL)
        printf("file cannot open \n");
    else
        printf("file opened for writing \n");

	 while (!feof(fp))
    {
		fscanf(fp,"%s %d %d",st[i].bianhao,&st[i].number,&st[i].score);
          i++;  
    }
	i=i-1;	
    sort(st,st+i,comp1);
    for(int j=0;j<i;j++){		
	    cout<<st[j].bianhao<<" ";
	    cout<<st[j].number<<" ";
	    cout<<st[j].score<<" "<<endl;
		}

	if(fclose(fp)!=0)	
        printf("file cannot be closed \n");
	else
	{
		
        printf("file is now closed \n");
	}
	    
		system("pause");
    return 0;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值