标题:帮我解决一下删除操作
取消只看楼主
陈ren
Rank: 2
等 级:论坛游民
帖 子:42
专家分:33
注 册:2012-2-29
结帖率:100%
已结贴  问题点数:20 回复次数:4 
帮我解决一下删除操作
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
#define RIGHT 1
class Student
{
public:
    Student();
    void show();
    void write_in();
    friend void shownum(Student &);
    static int number;
    friend string returnname(Student &);
    friend string returnno(Student &);
private:
    string Name;
    string No;
    int Age;
    string Sex;
};
Student::Student()
{
}
void Student::show()//打印学生信息
{
    cout<<Name<<"\t"<<No<<"\t"<<Age<<"\t"<<Sex<<endl;
}
void Student::write_in()//输入学生信息
{
    cout<<"please write in:"<<endl;
    cout<<"name:";
    cin>>Name;
    cout<<"no:";
    cin>>No;
    cout<<"age:";
    cin>>Age;
    cout<<"sex:";
    cin>>Sex;
    number++;
}
void shownum(Student &s)//打印目前学生人数
{

    cout<<"目前学生总人数为:"<<s.number<<endl;
}
int Student::number=0;
string returnname (Student &s)
{
    return s.Name;
}
string returnno (Student &s)
{
    return s.No;
}
class Score
{
public:
    Score();
    void show(Score &);
    void write_in();
    friend double ave(Score &);
    friend void Change(Score &);
private:
    int Math;
    int English;
    int C;
    int Chinese;
};
Score::Score()
{
    Math=0;English=0;C=0;Chinese=0;
}
void Score::show(Score &c)//打印学生成绩
{
    double ave=(Math+English+C+Chinese)/4.0;
    cout<<Math<<"\t"<<English<<"\t"<<C<<"\t"<<Chinese<<"\t"<<ave<<endl;
}
void Score::write_in()//输入学生成绩
{
    cout<<"please write in:"<<endl;
    cout<<"Math:";
    cin>>Math;
    cout<<"English:";
    cin>>English;
    cout<<"C:";
    cin>>C;
    cout<<"Chinese:";
    cin>>Chinese;
}
//double ave(Score &c)//该学生的平均成绩
//{
//    c.Ave=(c.Math+c.English+c.C+c.Chinese)/4;
//    return 0;
//}
void Change(Score &s)
{
    cout<<"please write in:"<<endl;
    cout<<"Math:";
    cin>>s.Math;
    cout<<"English:";
    cin>>s.English;
    cout<<"C:";
    cin>>s.C;
    cout<<"Chinese:";
    cin>>s.Chinese;
}
class Operate//操作(对学生各门课程成绩的增、删、改、查)
{

public:
    Operate()
    {}
    void add(Student &stu,Score &sco);
    void deletesco(Student &stu,Score &sco,int );
    void change(Score &sco);
    void check(Student stu,Score sco);
private:
    Student stu;
    Score sco;
};
void Operate::add(Student &stu,Score &sco)//添加学生
{
    stu.write_in();
    sco.write_in();
}
void Operate::deletesco(Student &stu,Score &sco,int i)//删除学生
{
    Student *s;
    Score *sc;
    s=new Student[stu.number];
    sc=new Score[stu.number];
    cout<<"删除学生信息为:"<<endl;
    cout<<"Name"<<"\t"<<"No"<<"\t"<<"Age"<<"\t"<<"Sex"<<endl;
    stu.show();
    cout<<"Math"<<"\t"<<"English"<<"\t"<<"C"<<"\t"<<"Chinese"<<"\t"<<"Ave"<<endl;
    sco.show(sco);
    for (i;i<stu.number;i++)
    {
        s[i]=s[i+1];
        sc[i]=sc[i+1];
    }
    s[i-1].number--;
}
void Operate::change(Score &sco)//修改学生成绩。
{
    cout<<"当前学生成绩为:"<<endl;
    cout<<"Math"<<"\t"<<"English"<<"\t"<<"C"<<"\t"<<"Chinese"<<"\t"<<"Ave"<<endl;
    sco.show(sco);
    Change(sco);
}
void Operate::check(Student stu,Score sco)//查找学生
{
    cout<<"Name"<<"\t"<<"No"<<"\t"<<"Age"<<"\t"<<"Sex"<<endl;
    stu.show();
    cout<<"Math"<<"\t"<<"English"<<"\t"<<"C"<<"\t"<<"Chinese"<<"\t"<<"Ave"<<endl;
    sco.show(sco);
}
int main ()
{
    int n=0;//n用来做数组下标。
    Student stu[10];
    Score sco[10];
    int i=1;
    int j,k=0;
    string c;
    string ch;
    Operate ope;
    while (i)
    {
        system("cls");
        cout <<"******************欢迎进入学生成绩管理系统******************"<<endl;
        cout <<"-----------------------------------------------------------"<<endl;
        cout<<"******************  录入学生信息和成绩:1   ******************"<<endl;
        cout<<"******************  按学号查询学生的信息:2 ******************"<<endl;
        cout<<"******************  按名字查询学生的信息:3 ******************"<<endl;
        cout<<"******************  按学号删除学生的信息:4 ******************"<<endl;
        cout<<"******************  按名字删除学生的信息:5 ******************"<<endl;
        cout<<"******************  按学号修改学生的信息:6 ******************"<<endl;
        cout<<"******************  按名字修改学生的信息:7 ******************"<<endl;
        cout<<"******************  查询当前总学生的情况:8 ******************"<<endl;
        cout<<"******************  退出该系统请按:      0 ******************"<<endl;
        cout <<"-----------------------------------------------------------"<<endl;
        cin>>j;
        //getchar();
        switch (j)
        {
        case 1:    //录入学生信息和成绩:1   
            ope.add(stu[n],sco[n]);
            n++;
            break;
        case 2://按学号查询学生的信息:2
            cout<<"请输入学生学号:"<<endl;
            cin >>c;
            for (k=0;k<n;k++)
            {
                ch=returnno(stu[k]);
                if (c==ch)
                {
                    cout<<"该学生的信息为:"<<endl;
                    ope.check(stu[k],sco[k]);
                    goto tt;
                }
                if (k==n-1&&c!=returnno(stu[k]))
                {
                    cout<<"输入该学号错误"<<endl;
                }
            }
            getchar();
            break;
        case 3://按名字查询学生的信息:3
            cout<<"请输入学生名字:"<<endl;
            cin >>c;
            for (k=0;k<n;k++)
            {
                ch=returnname(stu[k]);
                if (c==ch){
                    cout<<"该学生的信息为:"<<endl;
                    ope.check(stu[k],sco[k]);
                    goto tt;}
                if (k==n-1&&c!=ch)
                {
                    cout<<"输入该名字错误"<<endl;
                }
            }
            getchar();
            break;
        case 4:// 按学号删除学生的信息:4
            cout<<"请输入学生学号:"<<endl;
            cin >>c;
            for (k=0;k<n;k++)
            {
                ch=returnno( stu[k]);
                if (c==ch){
                    ope.deletesco(stu[k],sco[k],k);
                    goto tt;}
                if (k==n-1&&c!=ch)
                {
                    cout<<"输入该学号错误"<<endl;
                }
            }
            getchar();
            break;
        case 5://按名字删除学生的信息:5
            cout<<"请输入学生名字:"<<endl;
            cin >>c;
            for (k=0;k<n;k++)
            {
                ch=returnname(stu[k]);
                if (c==ch){
                    ope.deletesco(stu[k],sco[k],k);
                    goto tt;}
                if (k==n-1&&c!=ch)
                {
                    cout<<"输入该名字错误"<<endl;
                }
            }
            getchar();
            break;
        case 6://按学号修改学生的信息:6
            cout<<"请输入学生学号:"<<endl;
            cin >>c;
            for (k=0;k<n;k++)
            {
                ch=returnno( stu[k]);
                if (c==ch){
                    ope.change(sco[k]);
                    goto tt ;}
                if (k==n-1&&c!=ch)
                {
                    cout<<"输入该学号错误"<<endl;
                }
            }
            getchar();
            break;
        case 7://按名字修改学生的信息:7
            cout<<"请输入学生名字:"<<endl;
            cin >>c;
            for (k=0;k<n;k++)
            {
                ch=returnname(stu[k]);
                if (c==ch){
                    ope.change(sco[k]);
                    goto tt;}
                if (k==n-1&&c!=ch)
                {
                    cout<<"输入该名字错误"<<endl;   
                }
            }
            getchar();
            break;
        case 8://查询当前总学生情况8。
            shownum(stu[0]);
            for (k=0;k<n;k++)
            {
                ope.check(stu[k],sco[k]);
            }
            getchar();
            break;
        case 0:
            return -1;
        }
tt:getchar();
   getchar();
    }
    return 0;
}


删除操作出什么问题呢??s[i]=s[i+1]有问题??帮我修改一下。还有,如果考虑到删除的时候为最后一个时,应该怎么操作呢?
搜索更多相关主题的帖子: void 打印 private include public 
2012-05-22 18:11
陈ren
Rank: 2
等 级:论坛游民
帖 子:42
专家分:33
注 册:2012-2-29
得分:0 
,,大神帮帮。

老师说:死记硬背是一种很靠智商的方法,重要的还是努力
2012-05-22 19:28
陈ren
Rank: 2
等 级:论坛游民
帖 子:42
专家分:33
注 册:2012-2-29
得分:0 
.

老师说:死记硬背是一种很靠智商的方法,重要的还是努力
2012-05-22 21:44
陈ren
Rank: 2
等 级:论坛游民
帖 子:42
专家分:33
注 册:2012-2-29
得分:0 
回复 4楼 凌傲
没事没事。还要求大神帮忙呢。

老师说:死记硬背是一种很靠智商的方法,重要的还是努力
2012-05-23 18:24
陈ren
Rank: 2
等 级:论坛游民
帖 子:42
专家分:33
注 册:2012-2-29
得分:0 
回复 6楼 jianyuling00
用vector,直接用erase()。这些还没有学到用法。要去充电了。对的。用指针效果好很多。谢谢提醒。

老师说:死记硬背是一种很靠智商的方法,重要的还是努力
2012-05-26 00:24



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-367884-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.127343 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved