📄 xueshengxinxi.cpp
字号:
#include"StdAfx.h"
#include "kechengshejixueshengxinxi.h"
#include "CreateDlg.h"
#include "kechengshejixueshengxinxiDlg.h"
#include"windows.h"
#include"xueshengxinxi.h"
HWND hWnd;
int n=0;
char* print(struct student *head)//打印输出学生信息
{
struct student *p;
//printf("\nNow,these %d records are(结果数是%d,结果是):\n",n,n);
CKechengshejixueshengxinxiDlg dlg;
CString str;
str.Format("\nNow,these %d records are(结果数是%d,结果是):\r\n",n,n);
str+="学号: 姓名: 性别: 年龄: 成绩:";
p=head;
if(head!=NULL)
do
{
CString strr;
p=p->next;
if(p) //printf("%ld %s %s %ld %ld\n",p->num,p->name,p->sex,p->age,p->score);
strr.Format("\r\n %ld %s %6s %5ld %5ld",p->num,p->name,p->sex,p->age,p->score);
str+=strr;
}
while(p!=NULL);
//MessageBox(hWnd,dlg.m_mainstr,"提示",MB_OK);
char q[200];
strcpy(q,str.GetBuffer(0));
return q;
}
//构造学生链表
struct student *List(void)
{
struct student *head;
head=(struct student*)malloc(LEN);
head->next=NULL;
return(head);
}
//尾插法构造学生链表
struct student *creat(void)
{
struct student *head;
head=List();
struct student *p1,*p2;
n=0;
p1=p2=(struct student*)malloc(LEN);
//scanf("%ld%s%s%ld%ld",&p1->num,&p1->name,&p1->sex,&p1->age,&p1->score);
CCreateDlg dlg;
if(dlg.DoModal()==IDOK)
{
p1->num=dlg.m_num;
strcpy(p1->name,dlg.m_name.GetBuffer(0));
strcpy(p1->sex,dlg.m_sex.GetBuffer(0));
p1->score=dlg.m_score;
p1->age=dlg.m_age;
}
else return 0;
while(p1->num!=0)
{
n=n+1;
if(n==1)head->next=p1;
else
{
p2->next=p1;
p2=p1;
}
p1=(struct student*)malloc(LEN);
//scanf("%ld%s%s%ld%ld",&p1->num,&p1->name,&p1->sex,&p1->age,&p1->score);
CCreateDlg dlg;
if(dlg.DoModal()==IDOK)
{
p1->num=dlg.m_num;
strcpy(p1->name,dlg.m_name.GetBuffer(0));
strcpy(p1->sex,dlg.m_sex.GetBuffer(0));
p1->score=dlg.m_score;
p1->age=dlg.m_age;
}
else return 0;
}
p2->next=NULL;
//print(head);
return(head);
}
//对学生链表排序
void Insert_sort(struct student *head,struct student* s)
{
struct student* p=head;
while(p->next&&p->next->num<=s->num)
p=p->next;
s->next=p->next;
p->next=s;
}
struct student * paixu(struct student *head)
{
struct student *p;
struct student *h;
h=(struct student*)malloc(LEN);
h->next=NULL;
for(p=head->next;p;p=p->next)
{
struct student *s=(struct student*)malloc(sizeof(student));
s->num=p->num;
strcpy(s->name,p->name);
strcpy(s->sex,p->sex);
s->age=p->age;
s->score=p->score;
s->next=NULL;
Insert_sort(h,s);
}
//print(head);
return h;
}
//对学生链表进行数据的插入“尾插法
struct student *insert(struct student *head,struct student *stud){
struct student *p0,*p1,*p2;
p1=head;
p0=stud;
if(head==NULL)
{
head=p0;
p0->next=NULL;
}
else
while((p0->num>p1->num)&&(p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if(p0->num<=p1->num)
{
if(head==p1)head=p0;
else
p2->next=p0;
p0->next=p1;
}
else
{
p1->next=p0;
p0->next=NULL;
}
n=n+1;
return(head);
}
//查询学生链表中已有学生的信息
struct student *search(struct student *head,long num)
{
struct student *p;
CKechengshejixueshengxinxiDlg dlg;
if(head==NULL)
{
//printf("\nlist null! \n");
MessageBox(hWnd,"\nlist null! \n","提示",MB_OK);
}
p=head;
while((num!=p->num)&&(p->next!=NULL))
{
p=p->next;
}
if(num==p->num) {
//printf("%ld %s %s %ld %ld",p->num,p->name,p->sex,p->age,p->score);
CString str;
str.Format("要查询的学生信息如下:\r\n学号:%ld 姓名:%s 性别:%s 年龄:%ld 成绩:%ld",
p->num,p->name,p->sex,p->age,p->score);
MessageBox(hWnd,str,"查询结果",MB_OK);
}
else
//printf("num not been found(找不到)!\n");
MessageBox(hWnd,"num not been found(找不到)!\n","提示",MB_OK);
return(head);
}
//删除学生链表中的数据记录
struct student *del(struct student *head,long num)
{
struct student *p1,*p2;
if(head==NULL)
{
//printf("\nlist null(无数据)! \n");
MessageBox(hWnd,"\nlist null(无数据)! \n","提示",MB_OK);
}
p1=head;
while((num!=p1->num)&&(p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if(num==p1->num)
{
if(p1==head)
head=p1->next;
else
p2->next=p1->next;
//printf("delete(删除):%ld\n",num);
CString str;
str.Format("delete(删除):%ld\n",num);
MessageBox(hWnd,str,"提示",MB_OK);
n=n-1;
}
else
//printf("num not been found(无效的数字)!\n");
MessageBox(hWnd,"num not been found(无效的数字)!","提示",MB_OK);
return(head);
}
//对学生链表中的记录进行统计计数
void count(struct student *head)
{
int count=0;
struct student *p;
p=head->next;
while(p)
{
p=p->next;
++count;
}
//printf("该链表有%d条学生记录\n",count);
CString str;
str.Format("该链表有%d条学生记录\n",count);
MessageBox(hWnd,str,"计数结果",MB_OK);
}
/*
void main() //主函数
{
struct student *head,*stu;
long del_num;
long search_num;
int x;
printf(" ***********************************************************\n");
printf(" * *\n");
printf(" * 欢迎进入学生信息管理系统 *\n");
printf(" * *\n");
printf(" ***********************************************************\n");
do
{
printf("-----------------------\n1 create new chain(制作学生链表)\n
2 insert data(插入学生信息)\n
3 query data(查询学生信息)\n4 output data(打印学生信息)\n
5 delete data(删除学生信息)\n6 count data(计数)\n0 end(退出)\n-----------------------\n");
printf("Please input number 0-6 to test the program(请输入0-6选择测试):\n");
scanf("%d",&x);
switch(x)
{
case 0:break;
case 1:
printf("input records(Ex: num name sex age score)(输入数据如:学号 姓名 性别 年龄 分数):\n");
head=creat();
printf("制作学生链表如下(未排序):\n");
print(head);
printf("排序后学生链表如下:\n");
paixu(head);
break;
case 2:
printf("\ninput the inserted record(输入要插入的学生信息,输入数据如:学号 姓名 性别 年龄 分数):");
stu=(struct student*)malloc(LEN);
scanf("%ld%s%s%ld%ld",&stu->num,&stu->name,&stu->sex,&stu->age,&stu->score);
while(stu->num!=0)
{
head=insert(head,stu);
print(head);
printf("input the inserted record(输入要插入的学生信息):");
stu=(struct student*)malloc(LEN);
scanf("%ld%s%s%ld%ld",&stu->num,&stu->name,&stu->sex,&stu->age,&stu->score);
}
printf("插入数据后学生链表如下:\n");
print(head);
break;
case 3:
printf("\ninput the searched number(输入查询学号):");
scanf("%ld",&search_num);
printf("要查询的学生信息如下:\n");
while(search_num!=0)
{
head=search(head,search_num);
printf("\ninput the searched number(输入查询学号):");
scanf("%ld",&search_num);
}
break;
case 4:
printf("打印学生信息如下:\n");
paixu(head);
break;
case 5:
printf("\ninput the deleted number(输入删除学号):");
scanf("%ld",&del_num);
printf("删除后学生链表如下:\n");
while(del_num!=0)
{
head=del(head,del_num);
print(head);
printf("input the deleted number(输入删除学号):");
scanf("%ld",&del_num);
}
break;
case 6:count(head);break;
default:printf("输入有误,请重新输入!\n");
}
}while(x!=0);
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -