Ⅰ c語言的家譜圖。。想求一個運用結構鏈表的源程序
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<string.h>
#define OK 1
#define ERROR -1
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef int Status;
struct BiNode{
//用結構體定義結點類型。
//數據域信息包含家譜中該對的男、女名字,其雙親名字,以及這對夫婦位於家譜中的輩分
//指針域指向他們的第一個孩子以及其他兄弟
char man[10],woman[10],father[10],mother[10];
int level;
struct BiNode *firstchild,*nextsibling;
};
struct SqStack{
//對棧類型進行定義
BiNode *base;
BiNode *top;
int stacksize;
};
//函數聲明
Status InitStack (SqStack &S);
Status Push (SqStack &S,BiNode e);
Status CreateBiTree(BiNode *s);
Status Pop(SqStack &S,BiNode e);
Status EmptyStack(SqStack &S);
Status Preorder(BiNode *T,char name[10],BiNode *p);
//Status CreateParent(BiNode *s);
void findchildren(BiNode *p);
void putoutchildren(BiNode *q,int n);
void findparents(BiNode *p);
void levelhome(BiNode *T);
void leveling(SqStack S1,SqStack S2,int n);
void print(BiNode *p);
//主函數
void main()
{
BiNode *home=NULL,*p=NULL;
char name[10];
printf("請按先序遍歷的順序根據提示輸入家譜信息,不存在則輸入「#」\n");
CreateBiTree(home);
printf("層次化的家譜信息為\n");
levelhome(home);
printf("請輸入要查找的人的名字");
gets(name);
Preorder(home,name,p);
if(!p)printf("家譜中無此人");
else{
printf("輩分:%d\n",p->level);
printf("孩子信息");
findchildren(p);
printf("父母信息:");
findparents(p);
}
}
//函數定義
Status InitStack (SqStack &S){
//初始化函數
S.base=(BiNode*)malloc(STACK_INIT_SIZE * sizeof(BiNode));
if(!S.base) return ERROR;
S.top=S.base;
S.stacksize =STACK_INIT_SIZE;
return OK;
}
Status Push (SqStack &S,BiNode e){
//將e結點推入棧,作為棧頂元素
if(S.top-S.base >=S.stacksize ){
S.base=(BiNode *)realloc(S.base ,(S.stacksize +STACKINCREMENT)*sizeof(BiNode));
if(!S.base ) return ERROR;
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}
*S.top++=e;
return OK;
}
Status Pop(SqStack &S,BiNode e){
//取出棧頂元素
if(S.base==S.top) return ERROR;
e=*--S.top;
return OK;
}
Status EmptyStack(SqStack &S){
//若棧空,則返回0
return (S.top == NULL);
}
Status CreateBiTree(BiNode *root){
//創建家譜二叉樹
char man1[10],woman1[10],father[10],mother[10];
printf("man:");//男方名字,不存在則輸入「#」
gets(man1);
printf("woman:");//女方名字,不存在則輸入「#」
gets(man1);
if(strcmp(man1,"#")==0&&strcmp(woman1,"#")==0)//若該結點男女都不存在,則說明該結點為空,即該子樹的根結點不存在
{
root=NULL;
}
else{
root=(BiNode *)malloc(sizeof(BiNode));
strcpy(root->man,man1); //將男女的名字賦給該結點的數據域
strcpy(root->woman,woman1);
printf("father:"); //輸入該結點的雙親名字
gets(father);
printf("mother:");
gets(mother);
CreateBiTree(root->firstchild); //遞歸創建該結點的左、右子樹
CreateBiTree(root->nextsibling);
root->level=0;//將改結點的層數暫時定義為0
}
return OK;
}
Status Preorder(BiNode *T,char name[10],BiNode *p){
//先序遍歷家譜樹,查找與name名字相同的結點位置
if(T){
if(strcmp(T->man,name)==0||strcmp(T->woman,name)==0){
p=T;
return OK;
}
else{
if(Preorder(T->firstchild,name,p))return OK;
else return(Preorder(T->nextsibling,name,p));
}
}
else return ERROR;
}
void findchildren(BiNode *p){
//查找所得名字的孩子信息,輸出他們的名字,若無孩子,則輸出無孩子
int n=1;
BiNode *q;
if(p->firstchild){//該結點的firstchild指針指向的為他的第一個孩子
q=p->firstchild;
putoutchildren(q,n);//輸出
}
while(q->nextsibling){
//第一個孩子的nextsibling指針指向的為孩子的兄弟,即第二個孩子
//如此繼續,知道結點的右指針為空
q=q->nextsibling;
putoutchildren(q,n);//輸出
}
if(n==1)printf("無孩子");
}
void putoutchildren(BiNode *q,int n){
//輸出其孩子的結點的信息,並把孩子數加一
printf("第%d個孩子,男方名字%s,女方名字%s\n",n++,q->man,q->woman);
}
void findparents(BiNode *s){
//查詢該結點的雙親名字
if(s->father=="#"&&s->mother=="#")
printf("沒有父母信息");
else{
if((s->father)!="#")printf("father:%s\n",s->father);
if((s->mother)!="#")printf("mother:%s\n",s->mother);
}
}
void levelhome(BiNode *T){
//按層輸出該家譜
SqStack S,N; //定義兩個棧並初始化
InitStack(S);
InitStack(N);
BiNode *p;
p=T;
int n=1;
printf("第%d層的信息為:\n");
if(p){
print(p);
p->level=n;//修改p所指向的結點的輩分信息
Push(S,*p);//將該結點推進棧S
}
while(p=p->nextsibling){
//用循環來查找該層的所有信息,只要其nextsibling指向的結點不空,均為同一層
print(p);
p->level=n;
Push(S,*p);
}
while(!EmptyStack(S)||!EmptyStack(N)){
//循環,知道棧S和N都為空,說明按輩分遍歷完成
leveling(S,N,n++);
leveling(N,S,n++);
}
printf("\n");
}
void leveling(SqStack S1,SqStack S2,int n){
//將S1棧保存的信息一一取出,查找他孩子的結點,輸出其名字,並推入棧S2.
//即S2棧保存的結點是S1的下一輩
BiNode *p,*q;
printf("第%d層的信息為:\n");
while(!EmptyStack(S1)){
Pop(S1,*p);
q=p->firstchild;
if(q){
print(q);
q->level=n;
Push(S2,*q);
while(q=q->nextsibling){
print(q);
q->level=n;
Push(S2,*q);
}
}
}
}
void print(BiNode *p){
//輸出該結點處的夫婦名字
printf("%s,%s\\",p->man,p->woman);
}
Ⅱ c語言編寫家譜
c語言編寫家譜搞定了沒 我粗線了
Ⅲ 家譜管理系統設計與實現。編制一個家譜資料管理軟體,實現對一個家族所有的資料進行收集整理。
這玩意都到網路來,來錯地方了,去專門代編程序設計網站吧,花點錢,會讓你很滿意的,不捨得花錢的話,就去網路貼吧吧,那裡人多
Ⅳ C語言 家譜問題
#include<stdio.h>
#include<string.h>
charw[5][20]={{"child"},{"parent"},{"sibling"},{"descendant"},{"ancestor"}};
intkong(chara[]){
intn=0,i;
for(i=0;a[i]!='