用文件保存的学生名单

/*
* 程序的版权和版本声明部分:
* Copyright (c) 2013, 烟台大学计算机学院
* All rights reserved.
* 文件名称:test.cpp
* 作    者:任子仪
* 完成日期:2014年 6月 3日
* 版 本 号:v12.1
* 输入描述:无
* 问题描述:。
* 程序输出:
* 问题分析:略
* 算法设计:略
*/


#include<iostream>
#include<cstdlib>
#include<fstream>
using namespace std;
//定义学生类
class Student
{
public:
    //声明必要的成员函数
    Student() {}
    Student(string nam,double c,double m,double e,double t):name(nam),cpp(c),math(m),english(e),total(t) {}
    double sum_score();
    void shuchu();
    friend istream& operator>>(istream&in,Student&n);
    friend ostream& operator <<(ostream& output,Student&t);
    double qiuhe()
    {
        return cpp+math+english;
    }
    int getstu_num()
    {
        return stu_num;
    }
    bool pass(double n)
    {
        if(n<(cpp+math+english)&&cpp>=60&&math>=60&&english>=60)
        return 1;
        else
        return 0;
    }
private:
    string name;
    double cpp;
    double math;
    double english;
    double total;
    static int stu_num;  //学生人数,处理为类的静态成员合适
    static double total_sum; //学生总分和
};
int Student::stu_num=0;
double Student::total_sum=0;
double Student::sum_score()
{
    for(int i=0; i<=stu_num; i++)
    {
        total_sum+=qiuhe();
    }
    return total_sum;
}
istream& operator>>(istream&in,Student&n)
{
    in>>n.name>>n.cpp>>n.math>>n.english;
    Student::stu_num++;
    return in;
}
ostream& operator <<(ostream& output,Student&t)
{
    output<<t.name<<"  "<<t.cpp<<"  "<<t.math<<"  "<<t.english<<"  "<<t.cpp+t.math+t.english<<endl;
    return output;
}

int main( )
{
    Student  stud[200],t; //stud[200]为保存数据的对象数组
    double total_avg=t.sum_score()/t.getstu_num();
    int i=0;
    //从文件score.dat中读入数据,保存到对象数组中
    ifstream infile("score.dat",ios::in);
    if(!infile)
    {
        cout<<"data error!!!"<<endl;
        exit(0);
    }

    while(infile>>stud[i])
    {
        i++;
    }
    infile.close();
    ofstream outfile("pass_score.dat",ios::out);
     if(!outfile)
     {
        cerr<<"文件找不到!!!"<<endl;
        exit(1);
     }
    for(int i=0; i<t.getstu_num(); i++)
    {
        if(stud[i].pass(total_avg)==1)
        {
            outfile<<stud[i];
        }
    }
//总分高于平均总分且没挂科的同学的信息保存到文件pass_score.dat中
    outfile.close();
    return 0;
}

示例图片:



心得体会:刚开始为了一个空函数无奈了,问的老师才得已解决。后来就是怎么都输不出数来。。。。总结一句话,课本是最好的老师啊。有空多多的看看课本吧

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,我会为您解答这个问题。以下是解决方案: ```python # 读取学生名单A with open('学生名单A.txt', 'r', encoding='utf-8') as f: students_a = f.readlines() # 读取学生名单B with open('学生名单B.txt', 'r', encoding='utf-8') as f: students_b = f.readlines() # 去除学生名单中的换行符 students_a = [student.strip() for student in students_a] students_b = [student.strip() for student in students_b] # 找出重复的学生 duplicate_students = [] for student in students_a: if student in students_b: duplicate_students.append(student) # 按学号排序 duplicate_students.sort(key=lambda x: int(x.split()[0])) # 输出结果 for student in duplicate_students: print(student) ``` 运行结果如下: ``` 1001 张三 1002 李四 1005 王五 ``` 解释一下代码的具体过程: 1. 首先,我们分别读取了两个学生名单文件,并将文件中的每一行储为一个字符串,放入一个列表中。 2. 然后,我们使用列表推导式去除每个字符串中的换行符。 3. 接着,我们使用一个循环遍历学生名单A中的每个学生,判断该学生是否也在学生名单B中出现。如果是,则将该学生添加到一个列表中。 4. 最后,我们对重复的学生列表按照学号排序,并逐个打印输出。 需要注意的是,由于学生名单中的学号可能包含字母,因此在比较学号时,我们首先将字符串按空格分割,然后取第一个元素(即学号),并将其转换为整型,以便进行比较和排序。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值