| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 938 人关注过本帖
标题:排序有问题,说是访问冲突,麻烦大家看下
只看楼主 加入收藏
o0慢节奏0o
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2010-3-28
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:11 
排序有问题,说是访问冲突,麻烦大家看下
昨晚写的个学生成绩管理系统,错误一大堆。。自己没找出来,好像全是访问冲突方面的,麻烦大家帮忙看下吧
#include<iostream>
#include <stdlib.h>
using namespace std;
struct student
{
    int num;         
    char name[20];   
    float score[3];  
    float average;   
    struct student *next;
};
student *head;
 student *set()
{
    static student *p=head;
    student *l;
    l=new student;
    l->next=NULL;
    p->next=l;
    p=p->next;
    return p;
}
 ostream &operator <<(ostream &out,const student *Q)
 {
     out<<"\n学号:"<<Q->num<<"\n姓名:"<<Q->name;
     int i;
     for(i=0;i<3;i++)
     {
         out<<"第"<<i+1<<"门成绩"<<Q->score[i];
     }
     if(Q->average!=0)
     out<<"平均成绩:"<<Q->average<<endl;
     else
         out<<endl;
     return out;
 }
 void display()
 {
     student *h;
     h=head;
     do{
         cout<<h<<endl;
         if(h->next==NULL) break;
         h=h->next;
     }while(1);
 }

 void show()
 {
     student *h;
     h=head;
     int m=0;
     do{
         int j=0;
         for(int i=0;i<3;i++)
         {
             if(h->score[i]<60)  { j=1;m=1; break;}
         }
         if(j)
         {
             cout<<h<<endl;
         }
         if((h->next)==NULL)
         {break;}
         h=h->next;
     }while(1);
     if(!m)
         {
             cout<<"没有不及格的同学"<<endl;
         }
 }
 void avae()
 {
     student *h;
     h=head;
     do{
         float sum=0;
         for(int i=0;i<3;i++)
         {
             sum+=h->score[i];
         }
         h->average=sum/3;
         if((h->next)==NULL)
         { break;}
         h=h->next;
     }while(1);
 }
 int lon()
 {
     student *h;
     h=head;
     int i;
     for(i=1;1;i++)
     {
         if(h->next==NULL)
         { break;}
         h=h->next;
     }
     return i;
 }

 void turn()
 {
     student *h,*h_0,*h_1;
     int m=lon();
     if(m<3)
     {
         h=head;
         h_0=head->next;
         if(h->average>h_0->average)
         {
             head=h_0;
             h_0->next=h;
             h->next=NULL;
         }
     }
     else if(m>2)
     {
         for(;m>0;m--)
         {
             h=head;
             h_0=h->next;
             h_1=h_0->next;
             int i;
             for(i=0;i<m;i++)
             {
                 if(h_0->average>h_1->average)
                 {
                     h->next=h_1;
                     h_0->next=h_1->next;
                     h_1->next=h_0;
                 }
                 if(h_1->next==NULL)
                 { break; }
                 h=h->next;
                 h_0=h_0->next;
                 h_1=h_1->next;            
             }
         }
     }
 }
 void dele(int num1)
 {
     student *h;
     student *h_0;
     h=head->next;
     h_0=head;
     if((h_0->num)==num1)
     {
         head=h;
         delete h_0;
     }
     else
         do{
         if((h->num)==num1)
         {
             (h_0->next)=(h->next);
             delete h;
             break;
         }
         if((h->next)==NULL)
         { break; }
         h_0->next=h;
         h=h->next;
     }while(1);
 }

 int main()
 {     
     student *h;
loop_0:
     cout<<"学生成绩管理系统\n 1、成绩录入\n2、查看成绩\n3、成绩查询\n";
     cout<<"4、成绩统计\n5、增加成绩\n6、删除成绩\n7、退出\n请选择:"<<endl;
      int change;
      int change_0;
         cin>>change;
         system("cls");
         switch(change)
         {
         case 1:
             {
                 head=h=new student;
                 h->next=NULL;        
                 do{
                     cout<<"请输入学号";
                     cin>>h->num;
                     system("cls");
                     cout<<"请输入姓名";
                     cin>>h->name;
                     system("cls");
                     for(int i=0;i<3;i++)
                     {
                         cout<<"请输入第"<<i+1<<"门课成绩";
                         cin>>h->score[i];
                         system("cls");
                     }
                     cout<<"返回主菜单请输入0,继续输入请输入1:"<<endl;
                     cin>>change_0;
                     system("cls");
                     if(change_0==0)
                     {
                         goto loop_0;
                     }
                     h=set();
                 }while(change_0);
             }
         case 2:
             {
                 display();
                  system("pause");
                  system("cls");
                     goto loop_0;
                  break;
             }
         case 3:
             {
loop_1:
             cout<<"1、按学号查询\n2、查询不及格学生记录";
             int change_1;
             void inquire(int nums);
                 cin>>change_1;
                 system("cls");
                 if(change_1==1)
                 {
                     cout<<"请输入学号:"<<endl;
                     int num;
                     cin>>num;
                     system("cls");
                     inquire(num);
                     cout<<"返回主菜单请输入0,重新查询请输入1:";
                     cin>>change_0;
                     system("cls");
                     if(change_0==0)
                     {
                         goto loop_0;
                     }
                     else if(change_0==1)
                     {
                         goto loop_1;
                     }
                 }
                 else if(change_1==2)
                 {
                     show();
                     cout<<"返回主菜单请输入0,重新查询请输入1:";
                     cin>>change_0;
                     system("cls");
                     if(change_0==0)
                     {
                         goto loop_0;
                     }
                     else if(change_0==1)
                     {
                         goto loop_1;
                     }
                 }
                 else
                 {
                     cout<<"输入错误请重新输入:"<<endl;
                     system("cls");
                     goto loop_1;
                 }
                 break;
             }
         case 4:
             {
                 avae();
                 turn();
                 display();
                 system("pause");
                 system("cls");
                 goto loop_0;
                 break;
             }
         case 5:
             {
                  do{
                     h=set();
                     cout<<"请输入学号";
                     cin>>h->num;
                     system("cls");
                     cout<<"请输入姓名";
                     cin>>h->name;
                     system("cls");
                     for(int i=0;i<3;i++)
                     {
                         cout<<"请输入第"<<i+1<<"门课成绩";
                         cin>>h->score[i];
                         system("cls");
                     }
                     cout<<"返回主菜单请输入0,继续输入请输入1:"<<endl;
                     cin>>change_0;
                     system("cls");
                     if(change_0==0)
                     {
                         goto loop_0;
                     }
                  }while(change_0);
                 }
         case 6:
             {
                 int num1;
                 do{
                     cout<<"请输入您要删除数据的学号:"<<endl;
                     cin>>num1;
                     dele(num1);
                     system("cls");
                     cout<<"返回主菜单请输入0,继续输入请输入1:"<<endl;
                     cin>>change_0;
                     system("cls");
                     if(change_0==0)
                     {
                         goto loop_0;
                     }
                  }while(change_0);
             }
         case 7:
             break;
         default:
             {
                 cout<<"输入错误,请重新输入";
                 system("cls");
                 goto loop_0;
             }
         }
         cout<<"谢谢使用"<<endl;
         student *h_0=head;
         h=head->next;
         student *h_1;
         if(lon()>=3)
             {
                 do{
                     h_1=h_0;
                     h_0=(h_0->next);
                     h=h->next;
                     delete h_0;
                     if((h->next)==NULL)
                     {
                         delete h_1;
                         delete h;
                         break;
                     }
                 }while(1);
         }
         else
             {
                 delete h;
                 delete h_0;
         }
         return 0;
 }
  void inquire(int nums)
 {
     student *h;
     h=head;
     do{
         if(nums==(h->num))
         {
             cout<<h<<endl;
             break;
         }
         if((h->next)==NULL)
         {
             cout<<"您输入的学号有误"<<endl;
             break;
         }
         h=(h->next);
     }while(1);
 }
搜索更多相关主题的帖子: 麻烦 访问 
2010-06-29 16:07
qiangyongshe
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-5-14
收藏
得分:0 
你的主函数呢?怎么只有函数体
2010-06-29 16:47
o0慢节奏0o
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2010-3-28
收藏
得分:0 
回复 楼主 o0慢节奏0o
倒数第二个函数是主函数
int main()
{     
     student *h;
loop_0:
     cout<<"学生成绩管理系统\n 1、成绩录入\n2、查看成绩\n3、成绩查询\n";
     cout<<"4、成绩统计\n5、增加成绩\n6、删除成绩\n7、退出\n请选择:"<<endl;
      int change;
      int change_0;
         cin>>change;
         system("cls");
         switch(change)
         {
         case 1:
             {
                 head=h=new student;
                 h->next=NULL;        
                 do{
                     cout<<"请输入学号";
                     cin>>h->num;
                     system("cls");
                     cout<<"请输入姓名";
                     cin>>h->name;
                     system("cls");
                     for(int i=0;i<3;i++)
                     {
                         cout<<"请输入第"<<i+1<<"门课成绩";
                         cin>>h->score[i];
                         system("cls");
                     }
                     cout<<"返回主菜单请输入0,继续输入请输入1:"<<endl;
                     cin>>change_0;
                     system("cls");
                     if(change_0==0)
                     {
                         goto loop_0;
                     }
                     h=set();
                 }while(change_0);
             }
         case 2:
             {
                 display();
                  system("pause");
                  system("cls");
                     goto loop_0;
                  break;
             }
         case 3:
             {
loop_1:
             cout<<"1、按学号查询\n2、查询不及格学生记录";
             int change_1;
             void inquire(int nums);
                 cin>>change_1;
                 system("cls");
                 if(change_1==1)
                 {
                     cout<<"请输入学号:"<<endl;
                     int num;
                     cin>>num;
                     system("cls");
                     inquire(num);
                     cout<<"返回主菜单请输入0,重新查询请输入1:";
                     cin>>change_0;
                     system("cls");
                     if(change_0==0)
                     {
                         goto loop_0;
                     }
                     else if(change_0==1)
                     {
                         goto loop_1;
                     }
                 }
                 else if(change_1==2)
                 {
                     show();
                     cout<<"返回主菜单请输入0,重新查询请输入1:";
                     cin>>change_0;
                     system("cls");
                     if(change_0==0)
                     {
                         goto loop_0;
                     }
                     else if(change_0==1)
                     {
                         goto loop_1;
                     }
                 }
                 else
                 {
                     cout<<"输入错误请重新输入:"<<endl;
                     system("cls");
                     goto loop_1;
                 }
                 break;
             }
         case 4:
             {
                 avae();
                 turn();
                 display();
                 system("pause");
                 system("cls");
                 goto loop_0;
                 break;
             }
         case 5:
             {
                  do{
                     h=set();
                     cout<<"请输入学号";
                     cin>>h->num;
                     system("cls");
                     cout<<"请输入姓名";
                     cin>>h->name;
                     system("cls");
                     for(int i=0;i<3;i++)
                     {
                         cout<<"请输入第"<<i+1<<"门课成绩";
                         cin>>h->score[i];
                         system("cls");
                     }
                     cout<<"返回主菜单请输入0,继续输入请输入1:"<<endl;
                     cin>>change_0;
                     system("cls");
                     if(change_0==0)
                     {
                         goto loop_0;
                     }
                  }while(change_0);
                 }
         case 6:
             {
                 int num1;
                 do{
                     cout<<"请输入您要删除数据的学号:"<<endl;
                     cin>>num1;
                     dele(num1);
                     system("cls");
                     cout<<"返回主菜单请输入0,继续输入请输入1:"<<endl;
                     cin>>change_0;
                     system("cls");
                     if(change_0==0)
                     {
                         goto loop_0;
                     }
                  }while(change_0);
             }
         case 7:
             break;
         default:
             {
                 cout<<"输入错误,请重新输入";
                 system("cls");
                 goto loop_0;
             }
         }
         cout<<"谢谢使用"<<endl;
         student *h_0=head;
         h=head->next;
         student *h_1;
         if(lon()>=3)
             {
                 do{
                     h_1=h_0;
                     h_0=(h_0->next);
                     h=h->next;
                     delete h_0;
                     if((h->next)==NULL)
                     {
                         delete h_1;
                         delete h;
                         break;
                     }
                 }while(1);
         }
         else
             {
                 delete h;
                 delete h_0;
         }
         return 0;
}
2010-06-29 16:54
qiangyongshe
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-5-14
收藏
得分:0 
先定义
2010-06-29 17:12
o0慢节奏0o
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2010-3-28
收藏
得分:0 
回复 4楼 qiangyongshe
定义什么
2010-06-29 17:14
o0慢节奏0o
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2010-3-28
收藏
得分:0 
有人会没 麻烦大家帮个忙 谢谢
2010-06-29 23:51
lintaoyn
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:4
帖 子:606
专家分:2499
注 册:2009-4-8
收藏
得分:0 
先检查指针有没有指向正确的位置,没有的话提示用户。
比如说一步步来的话你的程序还行,但是我没先输入成绩的话,就选择下面的选项你的程序就会出现错误,有些操作的前提是有了某些资源后才能进行的,然后你在进行这些操作时就应该先检查获得了这些资源没有。

迭代的是人,递归的是神。
2010-06-30 08:44
o0慢节奏0o
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2010-3-28
收藏
得分:0 
回复 7楼 lintaoyn
应该怎么检查 能不能举个例子 刚学C++没多久 这方面不太了解
2010-06-30 10:03
lintaoyn
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:4
帖 子:606
专家分:2499
注 册:2009-4-8
收藏
得分:20 
#include<iostream>
#include <stdlib.h>
using namespace std;
struct student
{
    int num;         
    char name[20];   
    float score[3];  
    float average;   
    struct student *next;
};
student *head = 0;//先赋值,养成习惯
student *set()
{
    static student *p=head;
    student *l;
    l=new student;
    l->next=NULL;
    p->next=l;
    p=p->next;
    return p;
}
ostream &operator <<(ostream &out,const student *Q)
{
     out<<"\n学号:"<<Q->num<<"\n姓名:"<<Q->name;
     int i;
     for(i=0;i<3;i++)
     {
         out<<"第"<<i+1<<"门成绩"<<Q->score[i];
     }
     if(Q->average!=0)
     out<<"平均成绩:"<<Q->average<<endl;
     else
         out<<endl;
     return out;
}
void display()
{
     student *h;
     if (head) h=head; //做一个判断,是否存在学生信息
     else
     {
         cout << "有错误发生" << endl;//这里你也可以跳转到让用户先输入学生信息的部分
         return;
     }
     do{
         cout<<h<<endl;
         if(h->next==NULL) break;
         h=h->next;
     }while(1);
}

void show()
{
     student *h;
     h=head; // 这里也是,你也先做个判断
     int m=0;
     do{
         int j=0;
         for(int i=0;i<3;i++)
         {
             if(h->score[i]<60)  { j=1;m=1; break;}
         }
         if(j)
         {
             cout<<h<<endl;
         }
         if((h->next)==NULL)
         {break;}
         h=h->next;
     }while(1);
     if(!m)
         {
             cout<<"没有不及格的同学"<<endl;
         }
}
void avae()
{
     student *h;
     h=head;//还有这里
     do{
         float sum=0;
         for(int i=0;i<3;i++)
         {
             sum+=h->score[i];
         }
         h->average=sum/3;
         if((h->next)==NULL)
         { break;}
         h=h->next;
     }while(1);
}
int lon()
{
     student *h;
     h=head;//这里,反正你自己看,哪里需要判断的就做下判断
     int i;
     for(i=1;1;i++)
     {
         if(h->next==NULL)
         { break;}
         h=h->next;
     }
     return i;
}

void turn()
{
     student *h,*h_0,*h_1;
     int m=lon();
     if(m<3)
     {
         h=head;
         h_0=head->next;
         if(h->average>h_0->average)
         {
             head=h_0;
             h_0->next=h;
             h->next=NULL;
         }
     }
     else if(m>2)
     {
         for(;m>0;m--)
         {
             h=head;
             h_0=h->next;
             h_1=h_0->next;
             int i;
             for(i=0;i<m;i++)
             {
                 if(h_0->average>h_1->average)
                 {
                     h->next=h_1;
                     h_0->next=h_1->next;
                     h_1->next=h_0;
                 }
                 if(h_1->next==NULL)
                 { break; }
                 h=h->next;
                 h_0=h_0->next;
                 h_1=h_1->next;            
             }
         }
     }
}
void dele(int num1)
{
     student *h;
     student *h_0;
     h=head->next;
     h_0=head;
     if((h_0->num)==num1)
     {
         head=h;
         delete h_0;
     }
     else
         do{
         if((h->num)==num1)
         {
             (h_0->next)=(h->next);
             delete h;
             break;
         }
         if((h->next)==NULL)
         { break; }
         h_0->next=h;
         h=h->next;
     }while(1);
}

int main()
{     
     student *h;
loop_0:
     cout<<"学生成绩管理系统\n 1、成绩录入\n2、查看成绩\n3、成绩查询\n";
     cout<<"4、成绩统计\n5、增加成绩\n6、删除成绩\n7、退出\n请选择:"<<endl;
      int change;
      int change_0;
         cin>>change;
         system("cls");
         switch(change)
         {
         case 1:
             {
                 head=h=new student;
                 h->next=NULL;        
                 do{
                     cout<<"请输入学号";
                     cin>>h->num;
                     system("cls");
                     cout<<"请输入姓名";
                     cin>>h->name;
                     system("cls");
                     for(int i=0;i<3;i++)
                     {
                         cout<<"请输入第"<<i+1<<"门课成绩";
                         cin>>h->score[i];
                         system("cls");
                     }
                     cout<<"返回主菜单请输入0,继续输入请输入1:"<<endl;
                     cin>>change_0;
                     system("cls");
                     if(change_0==0)
                     {
                         goto loop_0;
                     }
                     h=set();
                 }while(change_0);
             }
         case 2:
             {
                 display();
                  system("pause");
                  system("cls");
                     goto loop_0;
                  break;
             }
         case 3:
             {
loop_1:
             cout<<"1、按学号查询\n2、查询不及格学生记录";
             int change_1;
             void inquire(int nums);
                 cin>>change_1;
                 system("cls");
                 if(change_1==1)
                 {
                     cout<<"请输入学号:"<<endl;
                     int num;
                     cin>>num;
                     system("cls");
                     inquire(num);
                     cout<<"返回主菜单请输入0,重新查询请输入1:";
                     cin>>change_0;
                     system("cls");
                     if(change_0==0)
                     {
                         goto loop_0;
                     }
                     else if(change_0==1)
                     {
                         goto loop_1;
                     }
                 }
                 else if(change_1==2)
                 {
                     show();
                     cout<<"返回主菜单请输入0,重新查询请输入1:";
                     cin>>change_0;
                     system("cls");
                     if(change_0==0)
                     {
                         goto loop_0;
                     }
                     else if(change_0==1)
                     {
                         goto loop_1;
                     }
                 }
                 else
                 {
                     cout<<"输入错误请重新输入:"<<endl;
                     system("cls");
                     goto loop_1;
                 }
                 break;
             }
         case 4:
             {
                 avae();
                 turn();
                 display();
                 system("pause");
                 system("cls");
                 goto loop_0;
                 break;
             }
         case 5:
             {
                  do{
                     h=set();
                     cout<<"请输入学号";
                     cin>>h->num;
                     system("cls");
                     cout<<"请输入姓名";
                     cin>>h->name;
                     system("cls");
                     for(int i=0;i<3;i++)
                     {
                         cout<<"请输入第"<<i+1<<"门课成绩";
                         cin>>h->score[i];
                         system("cls");
                     }
                     cout<<"返回主菜单请输入0,继续输入请输入1:"<<endl;
                     cin>>change_0;
                     system("cls");
                     if(change_0==0)
                     {
                         goto loop_0;
                     }
                  }while(change_0);
                 }
         case 6:
             {
                 int num1;
                 do{
                     cout<<"请输入您要删除数据的学号:"<<endl;
                     cin>>num1;
                     dele(num1);
                     system("cls");
                     cout<<"返回主菜单请输入0,继续输入请输入1:"<<endl;
                     cin>>change_0;
                     system("cls");
                     if(change_0==0)
                     {
                         goto loop_0;
                     }
                  }while(change_0);
             }
         case 7:
             break;
         default:
             {
                 cout<<"输入错误,请重新输入";
                 system("cls");
                 goto loop_0;
             }
         }
         cout<<"谢谢使用"<<endl;
         student *h_0=head;
         h=head->next;
         student *h_1;
         if(lon()>=3)
             {
                 do{
                     h_1=h_0;
                     h_0=(h_0->next);
                     h=h->next;
                     delete h_0;
                     if((h->next)==NULL)
                     {
                         delete h_1;
                         delete h;
                         break;
                     }
                 }while(1);
         }
         else
             {
                 delete h;
                 delete h_0;
         }
         return 0;
}
  void inquire(int nums)
{
     student *h;
     h=head;
     do{
         if(nums==(h->num))
         {
             cout<<h<<endl;
             break;
         }
         if((h->next)==NULL)
         {
             cout<<"您输入的学号有误"<<endl;
             break;
         }
         h=(h->next);
     }while(1);
}

[ 本帖最后由 lintaoyn 于 2010-6-30 10:37 编辑 ]

迭代的是人,递归的是神。
2010-06-30 10:36
lintaoyn
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:4
帖 子:606
专家分:2499
注 册:2009-4-8
收藏
得分:0 
指针指向的对象不存在了你就把该指针赋值为0。
int *p = new int;
delete p;
p = 0;//让p不指向任何地方

迭代的是人,递归的是神。
2010-06-30 10:40
快速回复:排序有问题,说是访问冲突,麻烦大家看下
数据加载中...
 
   



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

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