#include<stdio.h> #include<stdlib.h> #include<string.h> typedef struct GLNode //定义存储中缀表达式的结点类型 { char name[100]; char prof[100]; int type; struct { struct GLNode *hp,*tp; } ptr; /*hp指向同级的下一结点,tp指向下级的首结点*/ } GList; GList *GListCreate() //建立广义表 { GList *head,*p,*q,*s,*r; //简要介绍:head指向头结点;p指向导师结点;q指向研究生结点;r指向本科生节点; s指向新建立的节点 int i,j,b; char str[100]; b=1; head=p=q=r=s=NULL; while(b) { printf("请输入人员信息"); scanf("%s",str); s =(GList *)malloc(sizeof(GList)); if(!s) printf("申请空间失败!"); for(j=0,i=0; str[i] != '-'; j++,i++) //将字符串中的学生信息转化成学生结点 s->name[j] = str[i]; s->name[j] = '\0'; i=i+1; for(j=0; str[i] != '-'; j++,i++) s->prof[j] = str[i]; s->prof[j] = '\0'; i=i+1; s->type = str[i] - 48; s->ptr.hp=NULL; s->ptr.tp=NULL; switch(s->type) { case 0: if(head) p->ptr.hp=s; // 非首结点 else head=s; // 首结点的处理 p=s; r=q=s; // a在此等于m,主要是处理本科生直属于导师的情况 break; case 1: if(p->ptr.tp) q->ptr.hp=s; //非首结点的处理 else q->ptr.tp=s; // 首结点的处理 q=s; r=s; break; case 2: if(q->ptr.tp) r->ptr.hp=s; //非首结点的处理 else r->ptr.tp=s; //首结点的处理 r=s; break; default: printf("结点有误"); break; } printf("输入1:继续添加;输入0:录入结束"); scanf("%d",&b); } return head; } void Inquire(GList *head) //查询信息 { char n[100]; GList *p,*q,*r; int result; result=0; p = head; printf("\n请输入所查询人员的姓名:\n"); scanf("%s",n); while(p != NULL&&result==0) { q = p->ptr.tp; if(!strcmp(p->name,n)) { printf("\n查询结果:姓名:%s 职称:%s 类型:导师\n",p->name,p->prof); result=1; } else { if(q->type == 2) // 该导师带本科生 { r = q; while(r!= NULL) { if(!strcmp(r->name,n)) { printf("\n查询结果:姓名:%s 班级:%s 类型:本科生\n",r->name,r->prof); printf("所属导师:姓名:%s 职称:%s\n",p->name,p->prof); result=1; } r=r->ptr.hp; } } else { while(q!= NULL) { r = q->ptr.tp; if(!strcmp(q->name,n)) { printf("\n查询结果:姓名:%s 班级:%s 类型:研究生\n",q->name,q->prof); printf("所属导师:姓名:%s 职称:%s\n",p->name,p->prof); result=1; } while(r!= NULL) { if(!strcmp(r->name,n)) { printf("\n查询结果:姓名:%s 班级:%s 类型:本科生\n",r->name,r->prof); printf("所属导师:姓名:%s 职称:%s\n",p->name,p->prof); printf("所属导师生:姓名:%s 班级:%s\n",q->name,q->prof); result=1; } r=r->ptr.hp; } q = q->ptr.hp; } } p=p->ptr.hp; } } if(!result) printf("查无此人!\n"); printf("\n"); } GList *StudentInsert(GList *head) //插入学生 { char student[100],teacher[100],graduate[100]; GList *s,*p,*q,*r; int i,j; p = head; printf("请输入待插入学生信息,如:李刚-二班-1\n"); scanf("%s",student); s =(GList *)malloc(sizeof(GList)); if(!s) printf("申请空间失败!"); for(j=0,i=0; student[i] != '-'; j++,i++) //将字符串中的学生信息转化成学生结点 s->name[j] = student[i]; s->name[j] = '\0'; i=i+1; for(j=0; student[i] != '-'; j++,i++) s->prof[j] = student[i]; s->prof[j] = '\0'; i=i+1; s->type = student[i] - 48; s->ptr.hp=NULL; s->ptr.tp=NULL; printf("请输入所属导师姓名:\n"); scanf("%s",teacher); while(p&&strcmp(p->name,teacher)) p = p->ptr.hp; if(!p) printf("此导师不存在,不能插入!\n"); else { switch(s->type) { case 1: if(!p->ptr.tp) { p->ptr.tp=s; printf("插入成功!\n"); } else { if(p->ptr.tp->type==2) printf("该导师只能带本科生,因此不能将研究生插入!\n"); else { q=p->ptr.tp; while(q->ptr.hp) q=q->ptr.hp; q->ptr.hp=s; printf("插入成功!\n"); } } break; case 2: if(!p->ptr.tp) { p->ptr.tp=s; printf("插入成功!\n"); } else { switch(p->ptr.tp->type) { case 1: printf("请输入所属研究生姓名:\n"); //导师带研究生 scanf("%s",graduate); q=p->ptr.tp; while(q&&strcmp(q->name,graduate)) q=q->ptr.hp; if(!q) printf("该研究生不存在,不能插入!\n"); else { if(!q->ptr.tp) { q->ptr.tp=s; printf("插入成功!\n"); } else { r=q->ptr.tp; while(r->ptr.hp) r=r->ptr.hp; r->ptr.hp=s; printf("插入成功!\n"); } } break; case 2: if(!p->ptr.tp) //导师带本科生 { p->ptr.tp=s; printf("插入成功!\n"); } else { r=p->ptr.tp; while(r->ptr.hp) r=r->ptr.hp; r->ptr.hp=s; printf("插入成功!\n"); } break; default: printf("结点有误"); break; } } break; default: printf("结点有误"); break; } } return head; } GList *StudentDelete(GList *head) // 删除学生 { char student[100]; GList *s,*p,*q,*r,*t; int i,j,result; result=0; p=q=r=head; printf("请输入待删除学生信息,如:李刚-二班-1\n"); scanf("%s",student); s =(GList *)malloc(sizeof(GList)); if(!s) printf("申请空间失败!"); for(j=0,i=0; student[i] != '-'; j++,i++) //将字符串中的学生信息转化成学生结点 s->name[j] = student[i]; s->name[j] = '\0'; i=i+1; for(j=0; student[i] != '-'; j++,i++) s->prof[j] = student[i]; s->prof[j] = '\0'; i=i+1; s->type = student[i] - 48; s->ptr.hp=NULL; s->ptr.tp=NULL; switch(s->type) { case 1: do { if(p->ptr.tp->type==1) { q=p->ptr.tp; t=p; while(q&&strcmp(q->name,s->name)) { t=q; q=q->ptr.hp; } if(!strcmp(q->name,s->name)) { if(q->ptr.tp) { printf("此研究生下有本科生,请先将本科生移至别处,再删除此研究生"); result=2; break; } else { result=1; if(t->type==0) t->ptr.tp=q->ptr.hp; else t->ptr.hp=q->ptr.hp; free(q); } } } p=p->ptr.hp; } while(p != NULL&&result==0); break; case 2: do { if(!p->ptr.tp) { p=p->ptr.hp; continue; } else { switch(p->ptr.tp->type) { case 1: q=p->ptr.tp; //导师带研究生 while(q&&!result) { t=q; r=t->ptr.tp; while(r&&strcmp(r->name,s->name)) { t=r; r=r->ptr.hp; } if(r) { result=1; if(t->type==1) t->ptr.tp=r->ptr.hp; else t->ptr.hp=r->ptr.hp; free (r); } else q=q->ptr.hp; } break; case 2: r=p->ptr.tp; //导师带本科生 t=p; while(!result&&r) { while(r&&strcmp(r->name,s->name)) { t=r; r=r->ptr.hp; } if(r) { result=1; if(t->type==0) t->ptr.tp=r->ptr.hp; else t->ptr.hp=r->ptr.hp; free(r); } } break; default: break; } } p=p->ptr.hp; } while(p != NULL&&result==0); break; default: printf("结点有误"); break; } if(result==0) printf("查无此人"); return head; } void StudentCount(GList *head)//统计导师的研究生、本科生人数 { GList *p,*q,*r; int graduate=0,ungraduate=0; char teacher[100]; printf("请输入老师姓名:\n"); scanf("%s",teacher); p=head; while(p&&strcmp(p->name,teacher)) { p=p->ptr.hp; } if(!p) printf("查无此导师"); else { if(p->ptr.tp) { if(p->ptr.tp->type==1) { q=p->ptr.tp; while(q) { graduate++; r=q->ptr.tp; while(r) { ungraduate++; r=r->ptr.hp; } q=q->ptr.hp; } } else { r=p->ptr.tp; while(r) { ungraduate++; r=r->ptr.hp; } } } printf("该导师带的研究生数为%d",graduate); printf("该导师带的本科生数为%d",ungraduate); } } void GListPrint(GList *head)//输出广义表 { GList *p,*q,*r; p = head; while(p) { printf("%s-%s-%d ",p->name,p->prof,p->type); if(p->ptr.tp) //导师带学生 { switch(p->ptr.tp->type) { case 1: q=p->ptr.tp; //导师带研究生 while(q) { printf("%s-%s-%d ",q->name,q->prof,q->type); if(q->ptr.tp) //研究生带本科生 { r=q->ptr.tp; while(r) { printf("%s-%s-%d ",r->name,r->prof,r->type); r=r->ptr.hp; } } q=q->ptr.hp; } break; case 2: r=p->ptr.tp; //导师直接带本科生 while(r) { printf("%s-%s-%d ",r->name,r->prof,r->type); r=r->ptr.hp; } break; default: printf("结点有误"); break; } } p=p->ptr.hp; printf("\n"); } } int main() { int b; GList *Head; Head=NULL; printf("1.建立广义表\n"); Head = GListCreate(); do { printf("2.插入学生\n"); printf("3.删除学生\n"); printf("4.查询信息\n"); printf("5.统计导师的研究生、本科生人数\n"); printf("6.输出广义表\n"); printf("0.退出\n"); scanf("%d",&b); switch(b) { case 2: Head = StudentInsert(Head); break; case 3: Head = StudentDelete(Head); break; case 4: Inquire(Head); break; case 5: StudentCount(Head); break; case 6: GListPrint(Head); break; case 0: break; default: printf("选择有误"); break; } if(b==0) break; } while(1); return 0; }
#include<iostream> #include<cstdlib> #include<cstdio> #include<cstring> #include<string> using namespace std; typedef struct GLNode { string name; string prof; int type; struct { struct GLNode *hp,*tp; //hp指向下一节点,tp指向下一级的首节点 } ptr; } GList; GList *GListCreate(int typ) { GList *head,*p,*q,*s,*r; int cho=1; head=p=q=r=s=NULL; while(cho) { s =(GList *)malloc(sizeof(GList)); if(!s) cout << "申请空间失败" << endl; s->type = typ; if(typ==0) cout << "请输入导师的姓名与职称" << endl; else cout << "请输入学生的姓名及班级" << endl; string name,prof; cin >> name >> prof; s->name = name; s->prof = prof; s->ptr.hp=NULL; s->ptr.tp=NULL; if(head) p->ptr.hp=s; else head=s; p=s; r=q=s; cout << "1:继续添加" << endl; cout << "0:结束" << endl; cin >> cho; } return head; } GList *StudentInsert(GList *head, int typ) { GList *s,*p,*q,*r; p = head; s =(GList *)malloc(sizeof(GList)); if(!s) printf("申请空间失败!"); cout << "请输入学生的姓名及班级:" << endl; string name,prof; cin >> name >> prof; s->name = name; s->prof = prof; s->type = typ; s->ptr.hp=NULL; s->ptr.tp=NULL; cout << "请输入所属导师的姓名" << endl; string teacher,graduate; cin >> teacher; while(p&&p->name!=teacher) p = p->ptr.hp; if(!p) printf("此导师不存在,不能插入!\n"); else { switch(s->type) { case 1: if(!p->ptr.tp) { p->ptr.tp=s; cout << "插入成功" << endl; } else { if(p->ptr.tp->type==2) cout << "该导师只能带本科生,插入失败" << endl; else { q=p->ptr.tp; while(q->ptr.hp) q=q->ptr.hp; q->ptr.hp=s; cout << "插入成功" << endl; } } break; case 2: if(!p->ptr.tp) { p->ptr.tp=s; cout << "插入成功" << endl; } else { switch(p->ptr.tp->type) { case 1: cout << "请输入该本科生所属研究生姓名" << endl; cin >> graduate; q=p->ptr.tp; while(q&&q->name!=graduate) q=q->ptr.hp; if(!q) cout << "该研究生不存在,插入失败" << endl; else { if(!q->ptr.tp) { q->ptr.tp=s; cout << "插入成功" << endl; } else { r=q->ptr.tp; while(r->ptr.hp) r=r->ptr.hp; r->ptr.hp=s; cout << "插入成功" << endl; } } break; case 2: if(!p->ptr.tp) //导师带本科生 { p->ptr.tp=s; cout << "插入成功" << endl; } else { r=p->ptr.tp; while(r->ptr.hp) r=r->ptr.hp; r->ptr.hp=s; cout << "插入成功" << endl; } break; default: cout << "插入失败" << endl; break; } } break; default: break; } } return head; } GList *StudentDelete(GList *head,int type) { GList *p,*q,*r,*t; int result; result=0; p=q=r=head; cout << "请输入需删除学生的姓名" << endl; string name; cin >> name; switch(type) { case 1: do { if(p->ptr.tp->type==1) { q=p->ptr.tp; t=p; while(q&&q->name!=name) { t=q; q=q->ptr.hp; } if(q->name==name) { if(q->ptr.tp) { cout << "次研究生带有本科生,不可删除" <<endl; result=2; break; } else { result=1; if(t->type==0) t->ptr.tp=q->ptr.hp; else t->ptr.hp=q->ptr.hp; free(q); cout << "删除成功" << endl; } } } p=p->ptr.hp; } while(p != NULL&&result==0); break; case 2: do { if(!p->ptr.tp) { p=p->ptr.hp; continue; } else { switch(p->ptr.tp->type) { case 1: q=p->ptr.tp; //导师带研究生 while(q&&!result) { t=q; r=t->ptr.tp; while(r&&r->name!=name) { t=r; r=r->ptr.hp; } if(r) { result=1; if(t->type==1) t->ptr.tp=r->ptr.hp; else t->ptr.hp=r->ptr.hp; free (r); cout << "删除成功" << endl; } else q=q->ptr.hp; } break; case 2: r=p->ptr.tp; //导师带本科生 t=p; while(!result&&r) { while(r&&r->name!=name) { t=r; r=r->ptr.hp; } if(r) { result=1; if(t->type==0) t->ptr.tp=r->ptr.hp; else t->ptr.hp=r->ptr.hp; free(r); cout << "删除成功" << endl; } } break; default: break; } } p=p->ptr.hp; } while(p != NULL&&result==0); break; default: break; } if(result==0) cout << "输入错误" << endl; return head; } void Inquire(GList *head) { string name; GList *p,*q,*r; int result; result=0; p = head; cout << "请输入需查询的姓名" << endl; cin >> name; while(p != NULL&&result==0) { q = p->ptr.tp; if(p->name==name) { cout << "导师姓名,职称: "; cout << p->name << " " << p->prof << endl; result=1; } else { if(q->type == 2) // 该导师带本科生 { r = q; while(r!= NULL) { if(r->name==name) { cout << "该本科生姓名,班级: "; cout << r->name << " " << r->prof << endl; cout << "所属导师:姓名: 职称: "; cout << p->name << " " << p->prof << endl; result=1; } r=r->ptr.hp; } } else { while(q!= NULL) { r = q->ptr.tp; if(q->name==name) { cout << "该研究生姓名,班级: "; cout << r->name << " " << r->prof << endl; cout << "所属导师:姓名: 职称: "; cout << p->name << " " << p->prof << endl; result=1; } while(r!= NULL) { if(r->name==name) { cout << "该本科生姓名,班级: "; cout << r->name << " " << r->prof << endl; cout << "所属导师:姓名: 职称: "; cout << p->name << " " << p->prof << endl; cout << "所属研究生:姓名: 职称: "; cout << q->name << " " << q->prof << endl; result=1; } r=r->ptr.hp; } q = q->ptr.hp; } } p=p->ptr.hp; } } if(!result) printf("查无此人!\n"); printf("\n"); } void StudentCount(GList *head) { GList *p,*q,*r; int graNum=0,ungraNum=0; string teacher; cout << "请输入导师姓名" << endl; cin >> teacher; p=head; while(p&&p->name!=teacher) { p=p->ptr.hp; } if(!p) cout << "导师姓名输入错误" << endl; else { if(p->ptr.tp) { if(p->ptr.tp->type==1) { q=p->ptr.tp; while(q) { graNum++; r=q->ptr.tp; while(r) { ungraNum++; r=r->ptr.hp; } q=q->ptr.hp; } } else { r=p->ptr.tp; while(r) { ungraNum++; r=r->ptr.hp; } } } cout <<"导师" << teacher<< "带研究生数量:" << graNum << " 本科生" << ungraNum << endl; } } void GListPrint(GList *head) { GList *p,*q,*r; p = head; while(p) { cout << "导师: "; cout << p->name << "," << p->type <<endl; if(p->ptr.tp) { switch(p->ptr.tp->type) { case 1: q=p->ptr.tp; while(q) { cout << "研究生: "; cout << q->name << "," << q->type <<endl; if(q->ptr.tp) { r=q->ptr.tp; while(r) { cout << "本科生: "; cout << r->name << "," << r->type <<endl; r=r->ptr.hp; } } q=q->ptr.hp; } break; case 2: r=p->ptr.tp; while(r) { cout << "本科生: "; cout << r->name << "," << r->type <<endl; r=r->ptr.hp; } break; default: break; } } p=p->ptr.hp; cout << endl; cout << endl; } } int main() { do { cout << "-----------------------------------------" << endl; cout << "|***************************************|" << endl; cout << "|*1建立:建立导师广义表 *|" << endl; cout << "|*2插入:插入某位本科生或研究生 *|" << endl; cout << "|*3删除:删除某本科生或研究生 *|" << endl; cout << "|*4查询:查询导师、本科生或研究生 *|" << endl; cout << "|*5统计:某导师带了多少个研究生和本科生*|" << endl; cout << "|*6输出:输出全部信息 *|" << endl; cout << "|*0退出:程序结束 *|" << endl; cout << "|***************************************|" << endl; cout << "-----------------------------------------" << endl; int cho; cin >> cho; switch(cho) { case 1: GList *Head; Head=NULL; Head = GListCreate(0); break; case 2: cout << "1.插入研究生" << endl; cout << "2.插入本科生" << endl; cin >> cho; Head = StudentInsert(Head,cho); break; case 3: cout << "1.删除研究生" << endl; cout << "2.删除本科生" << endl; cin >> cho; Head = StudentDelete(Head,cho); break; case 4: Inquire(Head); break; case 5: StudentCount(Head); break; case 6: GListPrint(Head); break; case 0: break; default: printf("选择有误"); break; } if(cho==0) break; getchar(); } while(1); return 0; }
#include<iostream> #include<cstdio> #include<cstring> #include<iomanip> using namespace std; int num; int magicsqr[100][100]; void Input(); void Create(); void Output(); int main() { do{ cout << "-----------------------------------------" << endl; cout << "|***************************************|" << endl; cout << "|* *|" << endl; cout << "|* 1.生成魔方阵 *|" << endl; cout << "|* *|" << endl; cout << "|* 0.退出 *|" << endl; cout << "|* *|" << endl; cout << "|***************************************|" << endl; cout << "-----------------------------------------" << endl; int cho; cin >> cho; switch(cho){ case 1: Input(); Create(); Output(); break; case 2: continue; break; } }while(1); return 0; } void Input() { cout << "请输入魔方阵的阶数: " << endl; cin >> num; if(num>100){ cout << "输入数据过大,请重新输入" << endl; Input(); } if(num<1){ cout << "输入数据错误,请重新输入" << endl; Input(); } if(num%2==0) { cout << "请输入奇数" << endl; Input(); } } void Create() { memset(magicsqr, 0, sizeof(magicsqr)); int row=0; int col=num/2; magicsqr[row][col] = 1; for(int i=2;i<=num*num;i++) { int m = row; int n = col; row--; col++; if(row<0) row=num-1; if(col>num-1) col=0; if(magicsqr[row][col]!=0) { row=m+1; col=n; magicsqr[row][col]=i; continue; } magicsqr[row][col]=i; } } void Output() { cout << "生成的魔方阵为:" << endl; for(int i=0;i<num;i++) { cout<<setfill(' '); for(int j=0;j<num;j++) cout << setw(4) << magicsqr[i][j]; cout << endl; } }