帮我这只菜鸟看看这个程序错在那里,应如何改正

a39400 2005-03-19 11:57:19
////////////////////////////////////////////////////////////////
//===============written ========================//
//////////////////////////////////////////////////////////////

#include<iomanip.h>//学生成绩管理系统的链表实现
#include<string.h>


//===============declaration================================//

class node{//节点类

friend class list;
public:
void getdata();
// ~node(){delete next;}

private:
char number[10];
char name[10];
bool sex;
float score[4];
node * next;
};
void node::getdata(){//赋值函数
char rtemp[10];
float rscore;
int temp;

cout<<"Name:";
cin>>rtemp;
strcpy(name,rtemp);
cout<<"Number:";
cin>>rtemp;
strcpy(number,rtemp);
cout<<"Sex(0-female,1-male):";
cin>>temp;
while(!(temp==0||temp==1)){
cout<<"\n对不起,输入错误,请重输!\nSex(0-female,1-male):";
cin>>temp;
}
if(temp==1) sex=true;
else sex=false;
for(int j=0;j<4;j++){
cout<<"Score"<<j<<":";
cin>>rscore ;
score[j]=rscore;
}
cout<<endl;
}

//----------------------------------------------------------------------//
class list{//ADT表类

public:

list(){first=NULL;}
~list();
bool search(const char *x);
bool Insert(int k,node &x);
bool Delete(int k);
void PrintList(ostream & out);

private:
node* first;

};
list::~list()//析构函数
{
node *p;

while(first){
p=first->next;
delete first;
first=p;
}
}

bool list::search(const char *x )//按学号查找
{

bool success=false;
node * p=first;
int k=1;

while(strcmp(p->number,x)!=0 && p){

p=p->next;
k++;

}

if(p) {

success=true;

cout<<"该记录为第"<<k<<"条"<<":";
cout<<"name:"<<
p->name<<";number:"<<p->number
<<";sex:";

if(p->sex=true) cout<<"male";
else cout<<"female";

for(int j=0;j<4;j++){
cout<<";score"<<j<<":"<<p->score[j]
<<"."<<endl<<endl; }
}
return success;
}

void list::PrintList(ostream & out)//输出ADT表
{
node *p=first;
int i=1;

while(p){

out<<"No"<<i<<":"<<endl<<"name:"<<
p->name<<";number:"<<p->number
<<";sex:";

if(p->sex=true) out<<"male";
else out<<"female";

for(int j=0;j<4;j++){
out<<";score:"<<j<<p->score[j]
<<"."<<endl<<endl;}


p=p->next;
}
}

bool list::Insert(int k,node &x)//在第k个元素后插入
{
bool success=false;

node * p=first;
int s=1;
if(k==0) {

if(!first){
first=new node;
first->next=NULL;
}
x.next=first->next;
first=&x;

success=true;
}

else if(k>0){
if(p && s!=k){
s++;
p=p->next;
}
if(p){
x.next=p->next;
p=&x;

success=true;
}
}

return success;
}

bool list::Delete(int k)//删除第k个元素后的元素
{
bool success=false;

node * p=first;
int s=1;

if(k==0 && p) {
first=first->next;
success=true;
}

else if(k>0){
if(s!=k && p->next){
p=p->next;
s++;
}
if(p->next){
p=p->next->next;
success=true;
}
}

return success;
}
//=================end of declration============================//

//=================main function===============================//
void createadt(int n,list & x);
void insert(list &x);
void Delete(list &x);
void search(list &x);
void print(list &x);

void main()
{
int n;
list x;

cout<<"********************************"<<endl;
cout<<"********************************"<<endl;
cout<<"* ◎欢迎进入学生成绩管理系统◎ *"<<endl;
cout<<"********************************"<<endl;
cout<<"********************************"<<endl<<endl;
cout<<"请先建立一张表!"<<endl<<endl;
cout<<"输入表中的记录数:";

cin>>n;
while(n<0){
cout<<"对不起,输入错误!\n请重新输入:";
cin>>n;
}
cout<<"\n开始建表......"<<endl;


createadt(n,x);

cout<<"......建表结束\n";
do{
cout<<"请选择操作\n1.插入 2.删除 3.查询 4.打印 5.退出"<<endl;

cin>>n;


while(n<1||n>5){
cout<<"对不起,输入错误!\n请重新输入:";
cin>>n;
}
cout<<"\n执行操作......"<<endl;

switch(n){

case(1):insert(x);break;
case(2):Delete(x);break;
case(3):search(x);break;
case(4):print(x);break;

}

} while(n!=5);
cout<<"感谢使用本管理系统!\n请真诚提出建议,谢谢!\n再见!!";
// char m;
// cin>>m;
}

//========================end of main function=========================================//

void createadt(int n,list & x)
{

for(int i=1;i<=n;i++)
{ cout<<"No"<<i<<":"<<endl;
node a;
a.getdata();
x.Insert(i-1,a);
}
}
//-----------------------------------------------------------------------------//
void insert(list &x)
{

cout<<"请输入插入的记录有关信息:"<<endl;
node a;
a.getdata();
cout<<"需要插入在第几条的后面:";
int j;
cin>>j;
if(x.Insert(j,a)) cout<<"\n插入成功"<<endl;
else cout<<"\n对不起,插入失败"<<endl;
}
//----------------------------------------------------------------------------//
void Delete(list &x)
{
cout<<"需要在第几条的后面删除:";
int j;
cin>>j;
if(x.Delete (j)) cout<<"\n删除成功"<<endl;
else cout<<"\n对不起,输入有误,删除失败"<<endl;
}
//---------------------------------------------------------------------------//
void search(list& x)
{
cout<<"输入要查询的学生学号:";
char *a;
cin>>a;

if(!x.search(a)) cout<<"您要查询的记录不存在!\n";
}
//---------------------------------------------------------------------------//
void print(list& x)
{
x.PrintList(cout);
}

...全文
287 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
xtutzh 2005-03-20
  • 打赏
  • 举报
回复
楼主,你编程咋一气编完啊?
出了问题那肯定是连锁反应的啦
设置几个断点吧自己调试一下罗,-------------太长啦
llf_hust 2005-03-20
  • 打赏
  • 举报
回复
太长了
lzg_1127 2005-03-20
  • 打赏
  • 举报
回复
是啊,你先说说那里有错了!!```~~~~~~~~
我好想办法啊!!
qqfig 2005-03-19
  • 打赏
  • 举报
回复
22.obj - 81 error(s), 9 warning(s)
太多了,自己先把简单的搞定!!!
等我有时间再帮你看看!!!!
lw1a2 2005-03-19
  • 打赏
  • 举报
回复
#include<iostream.h>
llf_hust 2005-03-19
  • 打赏
  • 举报
回复
好长哦
你说说在哪儿报的错呀
ulwxf2 2005-03-19
  • 打赏
  • 举报
回复
太长了

先说哪里有问题吧,


Salam2001 2005-03-19
  • 打赏
  • 举报
回复
逻辑错误多
Mc_king 2005-03-19
  • 打赏
  • 举报
回复
奇怪啊````我运行居然没有错误啊```````````
morningeveryone 2005-03-19
  • 打赏
  • 举报
回复
太长了,自己找找吧 :-(
Salam2001 2005-03-19
  • 打赏
  • 举报
回复
这是在你的程序的基础上改进的, 顺便"复习"了一下链表的知识 -_-!!
自己对比着看看吧...
CSDN 的分现在是越来越难抢了...楼主表太吝啬哦,该出手时就出手哇!

//学生成绩管理系统的链表实现

#include "stdafx.h"
#include <cstring>
#include <iostream>
using namespace std;


//===============declaration================================//

//节点类
class Node
{
char number[10];
char name[10];
bool sex;
double score[4];
Node* next;

friend class List;

public:
Node() { next = 0; }
void Get();
};

//赋值函数
void Node::Get()
{
char rtemp[10];
double rscore;
int temp;

cout << "Name: ";
cin >> rtemp;
strncpy( name, rtemp, 10 );
cout << "ID: ";
cin >> rtemp;
strncpy( number, rtemp, 10 );
cout << "Gender( 0 - Female, 1 - Male): ";
cin >> temp;

while( !( temp == 0 || temp == 1 ))
{
cout << "\n输入错误, 请重新输入.\nSex( 0 - Female, 1 - Male): ";
cin >> temp;
}
if( temp == 1 )
sex = true;
else
sex = false;

for( int j = 0; j < 4; j++ )
{
cout << "Score " << j + 1 << ": ";
cin >> rscore ;
score[j] = rscore;
}
}

//ADT表类--------------------------------------------------------------------//
class List
{
Node* _Head;

public:

List(){ _Head = 0; }
~List();

bool Search( const char* x );
bool Insert( int k, Node* x);
bool Delete( int k );
void Print( ostream& out );
};

List::~List()//析构函数
{
Node* p;

while( _Head )
{
p = _Head->next;
delete _Head;
_Head = p;
}
}

//按学号查找
bool List::Search( const char* x )
{

bool Done = false;
Node* p = _Head;
int k = 1;

while( strcmp( p->number, x ) && p )
{
p = p->next;
k++;
}

if( p )
{

Done = true;

cout << "\n该记录为第 " << k << " 条" << ":\n";
cout << "\nName: " << p->name
<< "\tID: " << p->number
<< "\tSex: ";

if( p->sex )
cout << "Male";
else
cout << "Female";

for(int j = 0; j < 4; j++ )
cout << "\nScore " << j + 1<< ": " << p->score[j];
cout << endl;
}
return Done;
}

//输出ADT表
void List::Print( ostream& out )
{
Node* p = _Head;
int i = 1;

while( p )
{

out << "\nRecord " << i++ << " ---\n"
<< "Name: " << p->name
<< "\tID: " << p->number
<< "\tSex: ";

if( p->sex )
out << "Male";
else
out << "Female";

for( int j = 0; j < 4; j++ )
out << "\nScore " << j + 1 << ": " << p->score[j];

cout << endl;
p = p->next;
}
}

//在第k个元素后插入
bool List::Insert( int k, Node* x )
{
bool Done = false;

if( k == 0 )
{
if( !_Head )
{
_Head = x;
_Head->next = NULL;
}
else
{
x->next = _Head->next;
_Head->next = x;
}
Done = true;
}
else
if( k > 0 )
{
Node* p = _Head;
int s = 0;

if( p && s != k )
{
++s;
p = p->next;
}
if( p )
{
x->next = p->next;
p->next = x;

Done = true;
}
}

return Done;
}

//删除第k个元素后的元素
bool List::Delete( int k )
{
bool Done = false;

if( k == 0 && _Head->next )
{
Node* temp = _Head->next;
_Head->next = _Head->next->next;
delete temp;

Done = true;
}
else
if( k > 0 )
{
Node* p = _Head;
int s = 1;

if( p->next && s != k )
{
p = p->next;
s++;
}
if( p->next )
{
Node* temp = p->next;
p = p->next->next;
delete temp;

Done = true;
}
}

return Done;
}
//=================end of declration============================//

//=================main function===============================//

void CreateDB( int, List& );
void Insert( List& );
void Delete( List& );
void Search( List& );
void Print( List& );

int main()
{
int n;
List x;

cout << "********************************\n";
cout << "********************************\n";
cout << "* ◎欢迎进入学生成绩管理系统◎ *\n";
cout << "********************************\n";
cout << "********************************\n\n";
cout << "请先建立一张表, 输入表中的记录数: ";

cin >> n;
while( n < 0 )
{
cout << "输入错误, 请重新输入: ";
cin >> n;
}
cout << "\n开始建表...\n";
CreateDB( n, x );

cout << "\n建表结束.\n";
do
{
cout << "\n请选择操作:\n1.插入\t2.删除\t3.查询\t4.打印\t5.退出\n";

cin >> n;

while( n < 1 || n > 5 )
{
cout<<"\n输入错误, 请重新输入: ";
cin >> n;
}
cout << "\n执行操作...\n";

switch( n )
{
case 1:
Insert( x );
break;
case 2:
Delete( x );
break;
case 3:
Search(x);
break;
case 4:
Print( x );
break;

}

}while( n != 5 );

return 0;
}

//========================end of main function===============================//

void CreateDB( int n, List& x )
{

for( int i = 0; i < n; i++ )
{
cout << "\nRecord " << i + 1 << " ---\n\n";
Node* a = new Node;
a->Get();
x.Insert( i, a );
}
}

//-----------------------------------------------------------------------------//

void Insert( List& x )
{

cout << "\n请输入记录信息:\n";
Node* a = new Node;
a->Get();
cout << "\n需要插入在第几条的后面: ";
int j;
cin >> j;
if( x.Insert( j - 1, a ) )
cout << "\n插入成功.\n";
else
cout << "\n插入失败.\n";
}
//----------------------------------------------------------------------------//
void Delete(List &x)
{
cout << "\n需要在第几条的后面删除: ";
int j;
cin >> j;
if( x.Delete( j - 1 ) )
cout << "\n删除成功.\n";
else
cout << "\n删除失败.\n";
}
//---------------------------------------------------------------------------//
void Search( List& x )
{
cout << "\n输入要查询的学生学号: ";
char ID[10];
cin >> ID;

if( !x.Search( ID ) )
cout << "\n您要查询的记录不存在!\n";
}
//---------------------------------------------------------------------------//
void Print( List& x )
{
x.Print( cout );
}
zhousqy 2005-03-19
  • 打赏
  • 举报
回复
up

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧