练习、C# 结构体、冒泡排序

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 复习CS_冒泡排序
{
    class Program
    {
         /// <summary>
         /// 题目:
         /// 添加5个学生的信息到集合中,
         /// 每个学生都有:学号,姓名,成绩,3个内容,
         /// 添加完毕后将学生的分数从高到低排列并打印出来
         /// </summary>
        struct Student
        {
            public int num;
            public string Code;
            public string Name;
            public decimal Score;
        }

        static void Main(string[] args)
        {
            //1、循环添加学生信息
            ArrayList list = new ArrayList();

            for (int i = 1; i < 4; i++)
            {
                Student s = new Student(); //实例化

                Console.Write("请输入第" + i + "个学生的学号:");
                s.Code = Console.ReadLine();
                Console.Write("请输入第" + i + "个学生的姓名:");
                s.Name = Console.ReadLine();
                Console.Write("请输入第" + i + "个学生的成绩:");
                s.Score = Convert.ToDecimal(Console.ReadLine());
                s.num = i;

                list.Add(s);
                Console.WriteLine("===============================");
            }

            Console.WriteLine("-----------------------学生数据展示--------------------------");

            //2、排序

            for (int i = 0; i < list.Count - 1; i++)
            {
                for (int j = i + 1; j < list.Count; j++)
                {
                    Student s1 = (Student)list[i];
                    Student s2 = (Student)list[j];

                    if (s1.Score < s2.Score)
                    {
                        Object ob = list[i];
                        list[i] = list[j];
                        list[j] = ob;
                    }
                }
            }

            //3、打印
            foreach (object o in list)
            {
                Student ss = (Student)o;
                Console.WriteLine
                    ("第" + ss.num + "个学生的学号:" +
                            ss.Code + ",姓名:" +
                            ss.Name + ",分数:" +
                            ss.Score + "。");
            }

            Console.ReadKey();

        }
    }
}

时间: 2024-08-09 20:28:50

练习、C# 结构体、冒泡排序的相关文章

结构体&amp;冒泡排序——1147最高分数

求分数最高学生名字 本题我使用了结构体 结构体变量为数组a,每个元素为student类型 在进行过对结构体成员赋值后 之后进行排序 我参考了一下课本 也使用了冒泡排序 但冒泡排序的时间复杂度较高,可能数据过大后需要对程序进行优化 #include<iostream> #include<cstdio> #include<cstring> #include<string> using namespace std; struct student { string

结构体数组排序

using System;using System.Collections;using System.Collections.Generic;using System.Text; namespace 结构体冒泡排序{    class Program    {        struct student        {            public string name;            public int age;        }        static void Ma

例题:输入学生的各项资料,然后根据学生的分数,重新排序。重新复习结构体,集合,数组,for循环,冒泡排序,水平符的使用。

class Program { struct student     //定义一个student的结构体 { public string name;   //基本格式 public int code; public int age; public int fenshu; } static void Main(string[] args) { while (true) { ArrayList al = new ArrayList();  //定义一个新的集合 Console.Write("请输入人

代码案例(结构体,函数指针,指针函数,冒泡排序) 修改

#import <Foundation/Foundation.h> typedef struct {     char name[20];     int age;     float score; }Stu; //建立字符串和函数之间的一一对应关系. typedef BOOL (*PStu) (Stu stu1,Stu stu2) ; typedef struct nameFunctionPair{     char name[20]; //存储函数对应的字符串     PStu funct

代码案例(结构体,函数指针,指针函数,冒泡排序)

typedef struct {     char name[20];     int age;     float score; }Stu; #import <Foundation/Foundation.h> //姓名升序 void sortByName(Stu *p , int count ) {     for (int i = 0 ; i < count -1; i ++) {         for (int j= 0 ; j < count -1-i; j ++) {

29._结构体

结构体  为什么需要结构体   为了表示一些复杂的事物,而普通的的基本   类型无法满足实际要求 什么叫结构体   把一些基本数据类型组合在一起形成的一个   新的复合数据类型,这个叫做结构体 如何定义结构体   三种方式,推荐使用第一种 1 /* 2 2015年04月26日 14:21:45 3 目的: 4 结构体的定义 5 */ 6 7 # include <stdio.h> 8 9 //第一种方式 10 struct Student1 11 { 12 int age; 13 float

结构体排序

经常碰到结构体排序的问题,在此总结一下.以一个简单的例题开始: 例1.有三个人(Person结构体),每个人都有name(string型)和age(int型)两个属性,现在需要按照下面的规则排序:先以姓名按从小到大排序(如abc<abd),如果姓名相同,则按照年龄从大到小排序. #include<iostream> #include<string> using namespace std; struct Person{ string name; int age; }; voi

c# 结构体 集合 复习

添加5个学生的信息到集合中,每个学生都有:学号,姓名,成绩,3个内容,添加完毕后将学生的分数从高到低排列并打印出来,使用结构体 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace ConsoleApplication1 { class Progra

C# 结构体 枚举类型

结构体主要用于创建小型对象,例如   对象:学生  他包含:学号 姓名  性别  生日 班级   成绩等多个再小的对象 当我们有成千上万个对象组合起来   容易会自己造成混乱 而且占一定的内存 结构体就是把一个对象分支多个对象  组合起来进行计算 运行  并且不会重复运用同一个程序  把内存节省 定义: 它一般定义在Main函数外面,类Class program里面 格式: struct+结构体名称 { public   int 变量名; public   string   变量名; publi

用结构体实现一个电话本

结构体是c语言又一个存储数据的类型,那么用结构体怎么实现一个简单的可以存储1000个人信息的电话本呢?     一.首先需要定义一个结构体,包含一个人的信息(比如姓名,性别,年龄,电话,住址等等).我们先来定义一个结构体.     typedef struct Pdhb-info     {       char name[5];       char sex[3];       int age;       char tele[12];       char addr[30];     }Pd