📄 manage1.cpp
字号:
#include <iostream.h>
#include <string.h>
#include <iomanip.h>
#include <stdlib.h>
class ListNode
{
public:
int number;
float mark;
char name[20];
ListNode *link;
ListNode() { //不带参数的构造函数
}
ListNode(int x,char y[],char z,ListNode *p) //带参数的构造函数
{
number=x;
strcpy(name,y);
mark=z;
link=p;
}
~ListNode() //析构函数
{
}
void del(ListNode *head, int delVal);
};
//void definit();
ListNode *create()
{
ListNode *tmp;
ListNode *head;
ListNode value;
cout<<"输入学生信息,格式:学号 姓名 分数,以数据 '0 0 0'结束:"<<endl;
cin>>value.number>>value.name>>value.mark;
head = new ListNode;
head->link=NULL;
while(value.number!=0)
{
tmp=new ListNode;
tmp->number = value.number;
for(int j=0;j<20;j++) {
tmp->name[j] = value.name[j];
}
tmp->mark = value.mark;
tmp->link = head->link;
head->link=tmp;
cin>>value.number>>value.name>>value.mark;
}
return head;
}
/* 删除运算,删除学号为 delVal 的值 */
void del(ListNode *head, int delVal)
{
ListNode *front, *current;
front = head;
current = head->link;
while((current != NULL) && (current->number != delVal))
{
front = current;
current = current->link;
}
if(current == NULL)
{
cout <<"要删除的结点不存在!\n";
}
else
{
front->link = current->link;
}
// cout<<"删除编号为"<<delVal<<"的纪录后,表中内容为:\n";
}
void print(ListNode *head)
{
ListNode *current = new ListNode;
current->link = head->link;
cout<<setw(5)<<"学号"<<setw(10)<<"姓名"<<setw(8)<<"分数"<<endl;
while(current->link != NULL)
{
cout <<setw(5)<<current->link->number<<setw(10)<<current->link->name<<setw(5)<<current->link->mark<<endl;
current = current->link;
}
//cout <<setw(5)<<current->number<<setw(10)<<current->name<<setw(5)<<current->mark<<endl;
}
// 查找,返回编号为x的数据(所在的位置)
ListNode *find(ListNode *head,int x)
{
ListNode *current = new ListNode;
current = head->link;
while((current != NULL) && (current->number != x))
{
current = current->link;
}
if(current==NULL)
{
cout<<"没找到!学号有误!"<<endl;
}
else
{
//find flag
cout<<setw(5)<<"学号"<<setw(10)<<"姓名"<<setw(8)<<"分数"<<endl;
cout <<setw(5)<<current->number<<setw(10)<<current->name<<setw(5)<<current->mark<<endl;
}
return current;
}
/*void definit()
{
int chars[]={67,111,112,121,114,105,103,104,116,32,66,73,71,67,32,70,69,78,71,76,85,32,50,48,
48,52};
for(int i=0;i<26;i++)
cout<<(char)chars[i];
}*/
void insert(ListNode *head, int inVal, ListNode findVal)
{
ListNode *current, *tmp;
//current = new ListNode;
tmp = new ListNode;
tmp->number = findVal.number;
tmp->mark = findVal.mark;
for(int z=0;z<20;z++)
{
tmp->name[z]=findVal.name[z];
}
if(head->link == NULL)
{
tmp->link = NULL;
head->link = tmp;
}
cout<<"插入位置:"<<endl;
current = find(head,inVal);
if(current == NULL)
{
cout <<"插入位置非法!\n";
}
else
{
tmp->link = current->link;
current->link = tmp;
}
}
//对单链表进行有序输出
void OrderOutputList(ListNode* link, int mark)
{
if(link==NULL)
{
cout<<"链表为空!"<<endl;
return;
}
//建立新的单链有序表的表头结点
ListNode* head=new ListNode;//head为新建有序表的表头指针
head->number=link->number;
head->mark=link->mark;
for(int x=0;x<20;x++)
{
head->name[x]=link->name[x];
}
head->link=NULL;
//根据HL单链表生成head单链有序表
for(ListNode* p=link->link;p;p=p->link)
{
ListNode* q=new ListNode;
q->number=p->number;
q->mark=p->mark;
for(int u=0;u<20;u++)
{
q->name[u]=p->name[u];
}
ListNode *cp=head;
ListNode *ap=NULL;
//为向head单链表插入q结点而顺序查找合适位置
while(cp)
{
if(mark==1)
{
if(q->mark < cp->mark)
break;
else
{
ap=cp;
cp=cp->link;
}
}
else
{
if(q->mark > cp->mark)
break;
else
{
ap=cp;
cp=cp->link;
}
}
}
//把q结点插入head有序单链表中
if(ap==NULL){
q->link=head;
head=q;
}
else
{
q->link=cp;
ap->link=q;
}
}
//output
print(head);
}
void main()
{
//var
int whatfind;
ListNode valinsert;
int numinlink;
int num;
int orderby;
int j;
//banner style
int w=0;
/* while(w<60)
{
cout<<"-";
w++;
}
w=0;
cout<<"\n\n";
while(w<20)
{
cout<<" ";
w++;
}*/
cout<<"学生成绩管理\n";
//cout<<" 9421 liguo 021820\n ";
cout<<endl;
/* w=0;
while(w<60)
{
cout<<"-";
w++;
}
cout<<endl;*/
cout<<"1.创建数据表\n";
ListNode *mylink=new ListNode;
mylink=create();
cout <<"当前单链表中内容为: \n";
print(mylink);
while(1)
{
cout<<"请选择功能项:\n2.查找学生信息\n3.插入学生信息\n4.删除记录\n5.按成绩排序\n6.退出"<<endl;
cin>>j;
switch (j)
{
case 2:
//int whatfind;
cout<<"\n2.查找数据";
cout<<"请输入要查找目标的学号:";
cin>>whatfind;
find(mylink,whatfind);
break;
case 3:
cout<<"\n3.插入数据\n";
cout<<"输入要插入数据的前一个数据的学号:";
//ListNode valinsert;
//int numinlink;
cin>>numinlink;
cout<<"输入要插入的信息:\n";
cin>>valinsert.number>>valinsert.name>>valinsert.mark;
insert(mylink,numinlink,valinsert);
cout <<"\n在编号为 "<<numinlink<<" 的记录之后插入数据后,链表为:\n"<<endl;
print(mylink);
break;
case 4:
cout<<"\n4.删除纪录"<<endl;
cout<<"输入要删除记录的编号:";
//int num;
cin>>num;
del(mylink,num);
print(mylink);
break;
case 5:
cout<<"\n5.按分数排序";
cout<<"请选择排序方式,1为从低到高,0为从高到低:\n";
//int orderby;
cin>>orderby;
OrderOutputList(mylink,orderby);
break;
case 6:
cout<<"谢谢使用!"<<endl;
exit(1);
default:
cout<<"输入错误,请重新输入:"<<endl;
break;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -