struct

上一篇 / 下一篇  2014-08-05 14:43:56 / 个人分类:mark

// struct.cpp : Defines the entry point for the console application.
// 从文件中读取学生信息,并按照输入的科目的分数进行排序,然后打印出该科第一名学生信息
//结构体赋值问题

#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<iomanip>
#include<fstream>
using namespace std;
#define N 4
struct Subject
{
 string sub_name;
 int score;
};
struct  Student
{
 char name[20],number[13];
 Subject sub[4];
 double average;
};
Subject sub[4];
Student stu[N];
void taxis(int idx)  //排序并打印
{
 for(int i=0;i<=N;i++)  
 {
  for(int j=i+1;j<=N-1;j++)
  {
   Student Sa=stu[i];
   Student Sb=stu[j];
   int a=stu[i].sub[idx].score;
   int b=stu[j].sub[idx].score;
   if(a<b)
   {
    stu[i]=Sb;
    stu[j]=Sa;
   }
  }
 }
 printf("第一名:%s\t%d\n",stu[0].name,stu[0].sub[idx].score);
}

void readFile() //读文件并赋值
 {
  ifstream myFile("E:\\study\\class\\struct\\struct\\stuscore_in.txt",ios_base::in);
  char line[1000];
  int idx=0;
  while(myFile.getline(line,1000))
  {
   char number[13],name[20];
   int s1,s2,s3,s4;
   sscanf(line,"%s\t%s\t%d\t%d\t%d\t%d",&number,&name,&s1,&s2,&s3,&s4);
   //printf("%s\t%s\t%d\t%d\t%d\t%d\n",number,name,s1,s2,s3,s4);
  
   //stu[idx].number=number;
   strcpy(stu[idx].number,number);
   strcpy(stu[idx].name,name);
   int subIdx=0;
   stu[idx].sub[subIdx++].score=s1;
   stu[idx].sub[subIdx++].score=s2;
   stu[idx].sub[subIdx++].score=s3;
   stu[idx].sub[subIdx++].score=s4;
   printf("%s\t%s\t",stu[idx].number,stu[idx].name);
   for (int i=0;i<4;++i)
   {
    printf("%d\t",stu[idx].sub[i].score);
   }
   printf("\n");
   idx++;
  }
 }
 
int _tmain(int argc, _TCHAR* argv[])
{
 Subject sub[4];
 Student stu[N];
 int subIdx=0;
 readFile();
 printf("input subject number:(数学=0,物理=1,英语=2,计算机=3)");
 scanf("%d",&subIdx);
 taxis(subIdx);
 
return 0;
}

TAG:

 

评分:0

我来说两句

我的栏目

日历

« 2024-05-02  
   1234
567891011
12131415161718
19202122232425
262728293031 

数据统计

  • 访问量: 3273
  • 日志数: 3
  • 文件数: 4
  • 建立时间: 2012-05-01
  • 更新时间: 2014-09-01

RSS订阅

Open Toolbar