首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用气泡排序比较类对象

使用气泡排序比较类对象
EN

Stack Overflow用户
提问于 2016-04-19 10:35:11
回答 1查看 126关注 0票数 1

任务:在OOP中模拟一个名为“学生”的班级,里面有名字、姓氏和冬季考试的分数。显示有拖欠考试的学生的名字和小组中的前三名学生的名字。

我在比较分数方面有困难(前三名学生)。

我的代码:

代码语言:javascript
复制
#define _CRT_SECURE_NO_WARNINGS

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

class Student {
private:
    char nume[10];                                                                      
    char prenume[20];
    double matematica;
    double fizica;
    double programare;

public:
    Student(char num[10], char pren[20], double mate, double fiz, double progra);
    void afis();            
    void citire_stud();
    int restanta();
    double media();


};

Student::Student(char num[10] = "", char pren[20] = "", double mate = 0, double fiz = 0, double progra = 0)
{
    strcpy(nume, num);
    strcpy(prenume, pren);
    matematica = mate;
    fizica = fiz;
    programare = progra;
}


void Student::afis()
{
    cout << "Nume: " << nume << endl;
    cout << "Prenume: " << prenume << endl;
    cout << "Nota la matematica: " << matematica << endl;
    cout << "Nota fizica: " << fizica << endl;
    cout << "Nota programare: " << programare << endl;
    cout << endl;

}

void Student::citire_stud()
{
    char num[10];
    char pren[20];
    double mate, fiz, progra;

    cout << "Introduceti numele studentului: " << endl;
    cin >> num;
    strcpy(nume, num);

    cout << "Introduceti preumele studentului: " << endl;
    cin >> pren;
    strcpy(prenume, pren);

    cout << "Introduceti nota la mate studentului: " << endl;
    cin >> mate;
    matematica = mate;

    cout << "Introduceti nota la fizica studentului: " << endl;
    cin >> fiz;
    fizica = fiz;

    cout << "Introduceti nota la programare studentului: " << endl;
    cin >> progra;
    programare = progra;

}

int Student::restanta() //arrears
{
    if (matematica < 5 || programare <5 || fizica < 5)
        return 1;
    else return 0;
}

double Student::media()
{
    double med;
    med = (matematica + fizica + programare) / 3;
        return med;

}

void main()
{
    int cont = 0;
    int res[10];
    int  nr;

    cout << "Cati studenti sunt(max 10): "; //How many students
    cin >> nr;

    Student ob2[10], temp;
    for (int i = 0;i < nr;i++)
    {
        ob2[i].citire_stud();
        if (ob2[i].restanta())
        {

            res[cont++] = i;        
        }

    }
    if (cont == 0)
    {
        cout << "Nu sunt studenti restanti! " << endl;
        goto jmp;
    }
    else
    {
        cout << "\nStundetii restanti sunt: " << endl; 
        int i = 0;
        int k = 0;
        do
        {
            k = res[i];
            ob2[k].afis();
            i++;

        } while (i != cont);    
    }

    jmp:
    cout << "\n\n\n\nStudentii in ordinea medilor sunt: " << endl;

    for (int i = 0;i < nr;i++)
    {

        if (ob2[i].media() < ob2[i + 1].media()) //Not working
        {
            temp = ob2[i];
            ob2[i] = ob2[i + 1];
            ob2[i + 1] = temp;



        }
    }
        for (int i = 0;i < nr;i++)
            ob2[i].afis();


    system("pause");

}

输出:输出

应该是:9-7-5

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-19 11:15:39

你的泡泡被打破了。( A)你需要2个循环。B)你出界了。将其改为类似于:

代码语言:javascript
复制
for (int j = 0; j < nr; j++)
{
    for (int i = 0;i < nr - 1 ; i++)
    {

        if (ob2[i].media() < ob2[i + 1].media())
        {
            temp = ob2[i];
            ob2[i] = ob2[i + 1];
            ob2[i + 1] = temp;
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36715840

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档