首页 > 其他分享 >软件设计-Tutorial18

软件设计-Tutorial18

时间:2024-11-18 08:50:31浏览次数:1  
标签:studentId 21 软件设计 Tutorial18 students add Student new

package Tutorial18;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

// 定义学生类
class Student {
    private String name;
    private String studentId;
    private int age;

    public Student(String name, String studentId, int age) {
        this.name = name;
        this.studentId = studentId;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public String getStudentId() {
        return studentId;
    }

    public int getAge() {
        return age;
    }
}

public class StudentInfoTraversal {
    public static void main(String[] args) {
        // 创建学生列表
        List<Student> students = new ArrayList<>();
        students.add(new Student("张三", "130501", 20));
        students.add(new Student("李四", "130502", 21));
        students.add(new Student("王五", "130503", 21));
        students.add(new Student("赵六", "130504", 21));
        students.add(new Student("mzd", "130505", 21));
        students.add(new Student("xxk", "130506", 21));
        students.add(new Student("qfy", "130507", 21));
        students.add(new Student("gyg", "130508", 21));
        students.add(new Student("czf", "130509", 21));
        // 可以继续添加更多学生信息

        // 按照学号从小到大排序并输出
        Collections.sort(students, new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                return s1.getStudentId().compareTo(s2.getStudentId());
            }
        });
        System.out.println("按照学号从小到大输出学生信息:");
        Iterator<Student> ascendingIterator = students.iterator();
        while (ascendingIterator.hasNext()) {
            Student student = ascendingIterator.next();
            System.out.println("姓名:" + student.getName() + ",学号:" + student.getStudentId() + ",年龄:" + student.getAge());
        }

        // 按照学号从大到小排序并输出
        Collections.sort(students, new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                return s2.getStudentId().compareTo(s1.getStudentId());
            }
        });
        System.out.println("按照学号从大到小输出学生信息:");
        Iterator<Student> descendingIterator = students.iterator();
        while (descendingIterator.hasNext()) {
            Student student = descendingIterator.next();
            System.out.println("姓名:" + student.getName() + ",学号:" + student.getStudentId() + ",年龄:" + student.getAge());
        }
    }
}
Cpp:
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

// 定义学生结构体
struct Student {
    std::string name;
    std::string studentId;
    int age;

    Student(const std::string& name, const std::string& studentId, int age) :
        name(name), studentId(studentId), age(age) {}
};

// 比较函数,用于按照学号从小到大排序
bool compareAscending(const Student& s1, const Student& s2) {
    return s1.studentId < s2.studentId;
}

// 比较函数,用于按照学号从大到小排序
bool compareDescending(const Student& s1, const Student& s2) {
    return s1.studentId > s2.studentId;
}

int main() {
    // 创建学生向量
    std::vector<Student> students;
    students.push_back(Student("张三", "130501", 20));
    students.push_back(Student("李四", "130502", 21));
    students.push_back(Student("王五", "130503", 21));
    students.push_back(Student("赵六", "130504", 21));
    students.push_back(Student("mzd", "130505", 21));
    students.push_back(Student("xxk", "130506", 21));
    students.push_back(Student("qfy", "130507", 21));
    students.push_back(Student("gyg", "130508", 21));
    students.push_back(Student("czf", "130509", 21));
    // 可以继续添加更多学生信息
    // 按照学号从小到大排序并输出
    std::sort(students.begin(), students.end(), compareAscending);
    std::cout << "按照学号从小到大输出学生信息:" << std::endl;
    for (const auto& student : students) {
        std::cout << "姓名:" << student.name << ",学号:" << student.studentId << ",年龄:" << student.age << std::endl;
    }

    // 按照学号从大到小排序并输出
    std::sort(students.begin(), students.end(), compareDescending);
    std::cout << "按照学号从大到小输出学生信息:" << std::endl;
    for (const auto& student : students) {
        std::cout << "姓名:" << student.name << ",学号:" << student.studentId << ",年龄:" << student.age << std::endl;
    }

    return 0;
}

 

标签:studentId,21,软件设计,Tutorial18,students,add,Student,new
From: https://www.cnblogs.com/muzhaodi/p/18551603

相关文章

  • 软件设计-Tutorial19
    packageTutorial19;//中介者接口publicinterfaceMediator{voidsendMessage(AbstractChatRoomchatRoom,AbstractMembermember,Stringmessage);voidsendPicture(AbstractChatRoomchatRoom,AbstractMembermember,byte[]picture);}packageTutori......
  • 软件设计师易错题汇总
    计算机组成原理原码、反码、补码1 2345小阶向大阶对齐,尾数右移6 CPU12 34 5 678 9DMA适合于对大量数据的处理1011校验码12 3 寄存器1234 56数据标识1 存储器1......
  • 软件设计师学习笔记整理
    以下笔记都是基于电子版教材和图书,存于百度网盘中需要自取,仅限于学习,禁止商用,如有侵权,请及时联系:电子图书提取链接:https://pan.baidu.com/s/149F0iwj7DmAVIOQl47dPig提取码:auyn学习视频提取链接:https://pan.baidu.com/s/1RJlM7vk2PZX3X8cw3Z-2zQ提取码:gdju计算机组成与体系......
  • 远光天鹿:重塑数字化时代软件设计新体验
    在当今数字化时代,用户体验已成为衡量软件产品成功与否的关键指标。然而,传统的软件设计模式存在流程繁琐、效率低下、资源浪费等弊端,严重制约了产品创新的步伐。为此,远光软件创新推出高效智能的用户体验创新平台——远光天鹿。 传统设计模式的挑战传统软件设计模式涵盖需求分......
  • 软件设计-Tutorial18
    ```mermaidclassDiagramclassExpression{<<interface>>+interpret()String}classDirection{-Stringdirection+interpret()String}classAction{-Stringaction+inter......
  • 软件设计-Tutorial14
    ```mermaidclassDiagramMatchmakingService<|..RealMatchmakingServiceMatchmakingService<|..MatchmakingProxyclassMatchmakingService{+findPartner()}classRealMatchmakingService{+findPartner()}......
  • 软件设计-Tutorial13
    ```mermaidclassDiagramclassChessPiece{<<abstract>>+Stringcolor+display(intx,inty)}classWhiteChessPiece{+display(intx,inty)}classBlackChessPiece{+display(int......
  • 软件设计-Tutorial12
    packageTutorial12;//定义各个硬件设备和软件的类classMemory{publicbooleancheck(){System.out.println("Memoryself-checking...");//假设返回true表示自检成功returntrue;}}classCPU{publicbooleanrun(){......
  • 4.3软件设计:面对对象的设计
    面对对象设计1、面对对象的架构设计1.1第一步:构造系统的物理模型1.2第二步:设计子系统划分各个子系统的方式定义子系统之间的关系定义子系统的接口1.3第三步:非功能需求设计2、面对对象的用例设计与类设计2.1类2.2类间关系2.3细化用例第一步:定义类的属性第二步:定义......
  • 幼儿早教小程序软件设计与实现毕业设计源码
    博主介绍:✌专注于VUE,小程序,安卓,Java,python,物联网专业,有17年开发经验,长年从事毕业指导,项目实战✌选取一个适合的毕业设计题目很重要。✌关注✌私信我✌具体的问题,我会尽力帮助你。研究的背景:随着科技的发展和教育理念的进步,幼儿教育越来越受到重视。然而,传统的幼儿教育模......