C语言 家谱问题
c语言编写家谱搞定了没 我粗线了
给你一个二叉树,你自己再想想。
/* ****************二叉树有关算法*************** */
/*********设计目标:教学演示**********************/
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
#define stackinitsize 100
#define OK 1
#define ERROR 0
#define OVERFLOW -1
typedef int TElemType ;
typedef int Status;
//一一一一一二叉树的二叉链表存储表示一一一一一
typedef struct BiTNode{
TElemType data;
struct BiTNode *lchild,*rchild; //左右孩子指针
}BiTnode,*SElemType,*BiTree;
typedef struct{
//该堆栈的元素是指针类型的
//base和top均是指向指针的指针
SElemType *base;
SElemType *top;
int stacksize;
int tag;
}sqstack;//堆栈结构
//一一一一一基本操作的函数原型说明(部分)一一一一一
Status CreateBitree(BiTree &T);
//按先序次序输入二叉树中结点的值(一个字符),空格字符表示空树。
//构造二叉链表表示的二叉树T.
void PreOrder(BiTree T);
//采用二叉链表存储结构,先序遍历二叉树T,对每个结点的访问就是输出它的值
void Inorder(BiTree T);
//采用二叉链表存储结构,中序遍历二叉树T,对每个结点的访问就是输出它的值
void Postorder(BiTree T);
//采用二叉链表存储结构,后序遍历二叉树T,对每个结点的访问就是输出它的值。
void InitStack(sqstack &s)//初始化堆栈
{
s.base=(SElemType*)malloc(100*sizeof(SElemType));
if(!s.base) return ;
s.top=s.base;
s.stacksize=100;
return ;
}
int StackEmpty(sqstack s) //栈空判别
{return(s.top==s.base);
}
void Pop(sqstack &s,SElemType &e)//弹栈
{
e=*--s.top;
}
Status GetTop(sqstack s,SElemType &e)
{
if(s.top==s.base) return ERROR;
e=*(s.top-1);
return OK;
}
void Push(sqstack &s,SElemType e)//将元素压入堆栈
{
*s.top++=e;
}
Status CreateBiTree(BiTree &T){
//按先序次序输入二叉树中结点的值(一个字符),空格字符表示空树。
//构造二叉链表表示的二叉树T.
char ch;
ch=getche();
if(ch==' ') T=NULL;
else{
if(!(T=(BiTNode *)malloc(sizeof(BiTNode)))) return(OVERFLOW);
T->data=ch; //生成根结点
CreateBiTree(T->lchild);//构造左子树
CreateBiTree(T->rchild);//构造右子树
}
return OK;
}//CreateBiTree
void PreOrder(BiTree T)
//采用二叉链表存储结构,先序遍历二叉树T,对每个结点的访问就是输出它的值
{
if(T)
{
printf("%c",T->data);
PreOrder(T->lchild);
PreOrder(T->rchild);
}
}//preOrderTraVerse
void InOrder(BiTree T)
//采用二叉链表存储结构,中序遍历二叉树T,对每个结点的访问就是输出它的值
{
if(T)
{
InOrder(T->lchild);
printf("%c",T->data);
InOrder(T->rchild);
}
}//inOrderTraVerse
void PostOrder(BiTree T)
//采用二叉链表存储结构,后序遍历二叉树T,对每个结点的访问就是输出它的值
{
if(T)
{
PostOrder(T->lchild);
PostOrder(T->rchild);
printf("%c",T->data);
}
}//posOrderTraVerse
void preord1(BiTree T)
//采用二叉链表存储结构,先序遍历二叉树T的非递归算法。
{sqstack s;
BiTree p;
InitStack(s);
if(T)
{
Push(s,T); //根结点入栈
while(!StackEmpty(s)) //栈不为空时循环
{Pop(s,p);
printf("%c",p->data); //退栈并访问该结点
if(p->rchild) Push(s,p->rchild );//右孩子入栈
if(p->lchild) Push(s,p->lchild );//左孩子入栈
}
}
}
void inord1(BiTree T)
//采用二叉链表存储结构,中序遍历二叉树T的非递归算法。
{
return ;
}//Inord1
void inord2(BiTree T)
//采用二叉链表存储结构,visit是对数据元素操作的应用函数。
//中序遍历二叉树T的非递归算法,对每个数据元素调用函数visit。
{
}//Inord1
void posord1(BiTree T)
//采用二叉链表存储结构,后序遍历二叉树T的非递归算法。
{
BiTree p;
BiTree stack[100];
int tag[100],top=0;
p=T;
do
{
while(p)//扫描左子树
{
top++;
stack[top]=p;
tag[top]=0;
p=p->lchild ;
}
while(top>0&&tag[top]==1)
{ //p所指结点为无左子树的结点或其左子树已经遍历过
p=stack[top];
top--;
printf("%c",p->data );//访问结点
}
if(top>0)
{
p=stack[top];
p=p->rchild ;//扫描右子树
tag[top]=1; //表示当前的右子树已访问过
}
else break;
}while(p||top!=0);
}
void main()
{
BiTree t;
printf("
请按先序遍历输入二叉树(当左右子树为空时用空格输入)
");
CreateBiTree(t);
printf("
该二叉树的先序遍历为:
");
PreOrder(t);
printf("
该二叉树的先序遍历为:(用非递归调用)
");
preord1(t);
printf("
该二叉树的中序遍历为:
");
InOrder(t);
printf("
该二叉树的中序遍历为:(用非递归调用1)
");
inord1(t);
printf("
该二叉树的中序遍历为:(用非递归调用2)
");
inord2(t);
printf("
该二叉树的后序遍历为:
");
PostOrder(t);
printf("
该二叉树的递归后序遍历为:
");
posord1(t);
}
#include<stdio.h>
#include<string.h>
char w[5][20]={{"child"},{"parent"},{"sibling"},{"descendant"},{"ancestor"}};
int kong(char a[]) {
int n=0,i;
for(i=0;a[i]!='\0';i++) {
if(a[i]==' ')
n++;
else
break;
}
return n;
}
char fam[200][20];
int num[200]={0};
int search(char a[],int n) {
int i=1;
while(1) {
if(!strcmp(a,fam[i]))
return i;
i++;
}
return 0;
}
int search1(int n,int x) {
int i=1;
while(i<x) {
if(num[i]==n)
return i;
i++;
}
return 0;
}
char name[1000],word[1000],n1[100],n2[100],d[100];
int main() {
int n,m,i,j,a,b,t,x,k,h;
while(scanf("%d%d",&n,&m)!=EOF) {
memset(fam,'\0',sizeof(fam));
memset(num,'\0',sizeof(num));
scanf("%s",fam[1]);
getchar();
a=b=2;
t=1;
num[1]=1;
for(i=2;i<=n;i++) {
memset(name,'\0',sizeof(name));
gets(name);
b=kong(name);
if(b>a) {
if(fam[search1(t*2+1,i)][0]!='\0')
t=t*2+1;
else
t=t*2;
}
else { if(b<a) {
h=(a-b)/2;
while(h>=1) {
t=t/2;
h--;
}
}}
if(fam[search1(t*2,i)][0]!='\0')
x=t*2+1;
else x=t*2;
num[i]=x;
for(j=b;name[j]!='\0';j++)
fam[i][j-b]=name[j];
a=b;
}
for(i=0;i<m;i++) {
gets(word);
k=0;h=0;
memset(n1,'\0',sizeof(n1));
memset(n2,'\0',sizeof(n2));
memset(d,'\0',sizeof(d));
for(j=0;word[j]!='\0';j++) {
if(word[j]==' ') {
k++;
h=0;
continue;
}
if(k==0)
n1[j]=word[j];
if(k==5) {
n2[h]=word[j];
h++;
}
if(k==3) {
d[h]=word[j];
h++;
}
}
if(!strcmp(d,w[0])) {
a=search(n1,n);a=num[a];
b=search(n2,n);b=num[b];
if(a==b*2||a==(b*2+1))
printf("True
");
else printf("False
");
continue;
}
if(!strcmp(d,w[1])) {
a=search(n1,n);a=num[a];
b=search(n2,n);b=num[b];
if(b==a*2||b==(a*2+1))
printf("True
");
else printf("False
");
continue;
}
if(!strcmp(d,w[2])) {
a=search(n1,n);a=num[a];
b=search(n2,n);b=num[b];
if((a==b+1&&a==b/2*2+1)||(a==b-1&&b==a/2*2+1))
printf("True
");
else printf("False
");
continue;
}
if(!strcmp(d,w[3])) {
a=search(n1,n);a=num[a];
b=search(n2,n);b=num[b];
while(a>0) {
a=a/2;
if(a==b) {
printf("True
");
break;
}
if(a==0)
printf("False
");
}
continue;
}
if(!strcmp(d,w[4])) {
a=search(n1,n);a=num[a];
b=search(n2,n);b=num[b];
while(b>0) {
b=b/2;
if(a==b) {
printf("True
");
break;
}
if(b==0)
printf("False
");
}
continue;
}
}
}
return 0;
}
听说回答的够长能够自动采纳
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define R_CHILD "child"
#define R_ANCESTOR "ancestor"
#define R_SIBLING "sibling"
#define R_PARENT "parent"
#define R_DESC "descendant"
typedef unsigned int uint32;
typedef struct _member
{
//当前代数编号,首代为1(其实等于缩进格数/2+1 )
uint32 cur_generation;
uint32 cur_no; //自己编号
uint32 parent_no; //父编号
char name[32];
}Member;
typedef enum bool
{
false = 0,
true = 1
}BOOL;
#define LEN_NAME 64
#define LEN_SENTENCE 128
typedef struct stru_sentence
{
char sentence[LEN_SENTENCE];
char x[LEN_NAME];
char y[LEN_NAME];
char relation[LEN_NAME];
}StruSentence;
int parse_sentence(StruSentence *psentence);
BOOL is_true_sentence(const Member * pmember, uint32 nmember, StruSentence *psentence);
void init_member(Member **ppmember, uint32 nmember)
{
uint32 len = sizeof(Member)*nmember;
*ppmember = (Member*)malloc(len);
memset(*ppmember, 0, len);
}
void init_sentence(StruSentence **ppsentence, uint32 nsentence)
{
uint32 len = sizeof(StruSentence)*nsentence;
*ppsentence = (StruSentence*)malloc(len);
memset(*ppsentence, 0, len);
}
int read_all(Member **ppmember, uint32 *pnmember, StruSentence **ppsentence, uint32 *pnsentence)
{
uint32 ind = 0;
FILE *pfile = fopen("family.txt", "r");
if (pfile == NULL)
return -1;
#define MAX_LINE 256
#define MAX_LEN_NUMBER 8
char buf[MAX_LINE] = {0};
//读取数字
char cmember[MAX_LEN_NUMBER] = {0};
char csentence[MAX_LEN_NUMBER] = {0};
if (fgets(buf, MAX_LINE, pfile) != NULL)
{
uint32 i = 0;
for (; i < strlen(buf); ++i)
{
if (buf[i] != ' ')
{
cmember[i] = buf[i];
}
else
break;
}
*pnmember = atoi(cmember);
uint32 temp = 0;
for (; i < strlen(buf); ++i)
{
if (buf[i] != ' ')
{
csentence[temp++] = buf[i];
}
}
*pnsentence = atoi(csentence);
}
Member *pmember = NULL;
init_member(&pmember, *pnmember);
*ppmember = pmember;
StruSentence *psentence = NULL;
init_sentence(&psentence, *pnsentence);
*ppsentence = psentence;
//读取人物
while (ind < *pnmember && fgets(buf, MAX_LINE, pfile) != NULL)
{
uint32 nspace = 0;
uint32 i = 0;
for (; i < strlen(buf); ++i)
{
if (buf[i] != 13 && buf[i] != 10)
{
if (buf[i] == ' ')
++nspace;
else
pmember[ind].name[i-nspace] = buf[i];
}
else
break;
}
pmember[ind].cur_generation = nspace/2 + 1;
pmember[ind].cur_no = ind;
++ind;
}
//读取句子
ind = 0;
while (ind < *pnsentence && fgets(psentence[ind].sentence, MAX_LINE, pfile) != NULL)
{
++ind;
}
fclose(pfile);
return 0;
}
void parse_relation(Member *pmember, uint32 nmember)
{
pmember[0].parent_no = 0;
uint32 i = nmember - 1;
for (; i > 0; --i)
{
uint32 j = i - 1;
for (; j >= 0; --j)
{
if (pmember[i].cur_generation - 1 == pmember[j].cur_generation)
{
pmember[i].parent_no = pmember[j].cur_no;
break;
}
}
}
for (i = 0; i < nmember; ++i)
printf("name=[%s], generation[%d], cur_no[%d], p[%d]
",
pmember[i].name, pmember[i].cur_generation, pmember[i].cur_no, pmember[i].parent_no);
}
uint32 get_no_by_name(const Member *pmember, uint32 nmember, const char* pname)
{
uint32 i = 0;
for (i = 0; i < nmember; ++i)
{
if (strcmp(pmember[i].name, pname) == 0)
return pmember[i].cur_no;
}
return 0;
}
BOOL is_true_sentence(const Member * pmember, uint32 nmember, StruSentence *psentence)
{
uint32 xid = get_no_by_name(pmember, nmember, psentence->x);
uint32 yid = get_no_by_name(pmember, nmember, psentence->y);
if (strstr(psentence->relation, R_CHILD) != NULL)
{
if (pmember[xid].parent_no == pmember[yid].cur_no)
return true;
}
else if (strstr(psentence->relation, R_ANCESTOR) != NULL)
{
if (pmember[xid].cur_generation == 0)
return true;
uint32 temp = pmember[yid].parent_no;
while (temp > 0)
{
if (temp == pmember[xid].cur_no)
return true;
temp = pmember[temp].parent_no;
}
}
else if (strstr(psentence->relation, R_DESC) != NULL)
{
if (pmember[yid].cur_generation == 0)
return true;
uint32 temp = pmember[xid].parent_no;
while (temp > 0)
{
if (temp == pmember[yid].cur_no)
return true;
temp = pmember[temp].parent_no;
}
}
else if (strstr(psentence->relation, R_PARENT) != NULL)
{
if (pmember[yid].parent_no == pmember[xid].cur_no)
return true;
}
else if (strstr(psentence->relation, R_SIBLING) != NULL)
{
if (pmember[xid].parent_no == pmember[yid].parent_no)
return true;
}
return false;
}
int parse_sentence(StruSentence *psentence)
{
//assert(psentence->sentence[0] != '\0');
uint32 i = 0;
for (; i < strlen(psentence->sentence); ++i)
{
if (psentence->sentence[i] != ' ')
psentence->x[i] = psentence->sentence[i];
else
break;
}
for (; i < strlen(psentence->sentence); ++i)
{
if (psentence->sentence[i-2] == 'o'
&& psentence->sentence[i-1] == 'f'
&& psentence->sentence[i] == ' ')
break;
}
++i;
uint32 ntemp = 0;
for (; i < strlen(psentence->sentence); ++i)
{
if (psentence->sentence[i] != 10 && psentence->sentence[i] != 13)
{
psentence->y[ntemp++] = psentence->sentence[i];
}
else
break;
}
if (strstr(psentence->sentence, R_CHILD) != NULL)
{
strcpy(psentence->relation, R_CHILD);
}
else if (strstr(psentence->sentence, R_ANCESTOR) != NULL)
{
strcpy(psentence->relation, R_ANCESTOR);
}
else if (strstr(psentence->sentence, R_DESC) != NULL)
{
strcpy(psentence->relation, R_DESC);
}
else if (strstr(psentence->sentence, R_PARENT) != NULL)
{
strcpy(psentence->relation, R_PARENT);
}
else if (strstr(psentence->sentence, R_SIBLING) != NULL)
{
strcpy(psentence->relation, R_SIBLING);
}
//printf("x[%s],y[%s], relation[%s]
", psentence->x, psentence->y, psentence->relation);
return 0;
}
void test_family(int argc, char *argv[])
{
Member *pmember = NULL;
uint32 nmember = 0;
uint32 nsentence = 0;
StruSentence *psentence = NULL;
if (read_all(&pmember, &nmember, &psentence, &nsentence) == -1)
return;
parse_relation(pmember, nmember);
////测试---begin
uint32 ind = 0;
for (; ind < nsentence; ++ind)
{
printf("[%s]
", psentence[ind].sentence);
parse_sentence(&psentence[ind]);
BOOL ret = is_true_sentence(pmember, nmember, &psentence[ind]);
if (ret)
printf("is true..
");
else
printf("is false..
");
}
////测试---end
}
int main(int argc, char *argv[])
{
test_family(argc, argv);
return 0;
}
测试时没有使用输入方式,采用读取文件的方式。工程发你邮箱,若有问题,请继续。望采纳
帮做20微信红包
曾婷诺通: 如果能确定家族成员个数,以及成员最大信息,可以用二维字符数组存储 char a[100][21];//代表最多可以存放100个成员,每个成员信息长度最多20,字符串结尾\0算一个如果不能确定,要用动态创建数组 a = (char **)malloc(sizeof(char *) * M);//分配指针数组,M是实际运行是确定的成员个数 for(i=0; i<M; i++) { a[i] = (char *)malloc(sizeof(char) * (slen+1));//分配每个指针所指向的数组,slen是每个成员信息长度 }
南谯区17010049101: C语言画家谱图 - ?
曾婷诺通: 用双向指针,定位方便一点
南谯区17010049101: C语言的家谱图..想求一个运用结构链表的源程序 - ?
曾婷诺通: #include #define OK 1 #define ERROR -1 #define STACK_INIT_SIZE 100 #define STACKINCREMENT 10 typedef int Status; struct BiNode{ //用结构体定义结点类型. //数据域信息包含家谱中该对的男、女名字,其双亲名字,以及这对夫妇位于...
南谯区17010049101: 大神求助,建立一个家谱,并实现查找某人的c语言程序 - ?
曾婷诺通: 任务占坑
南谯区17010049101: C语言家谱,有人提议用双向链表或者2维数组,求高手指教 - ?
曾婷诺通: #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 10typedef int Status;struct BiNode{ //用结构体定义结点类型. //数据域信息包...
南谯区17010049101: 用c语言实现家谱管理系统 如何建一个二叉树从而将数据导入二叉树中? - ?
曾婷诺通: read.table() 可先设置工作空间,也可在file选项填写全路径!
南谯区17010049101: 定义一个求阶乘的函数,然后计算1! - 2!+3! - 4!+5! - 6!+7! - 8!+9!.C语言问题 - ?
曾婷诺通: int fact(int n) {int m = 1;while(n>0){m*=n;n--;}return m; } int main() {int i, sum=0;for(i=1;i<=9;i++){if(i%2)sum+=fact(i);elsesum-=fact(i);}printf("1!容-2!+3!-4!+5!-6!+7!-8!+9!=%d\n", sum); }
南谯区17010049101: C语言中的strlen函数 - ?
曾婷诺通: strlen()只是统计字符串的长度,统计到'\0'结束,但不包括'\0',这个与sizeof()不一样.对于用双引号引起来的字符串,系统默认最后一位字符为'\0',如你自己定义的:char a[]="china"; char b[]={"china"}; 另外,数组初始化还可以是一...
南谯区17010049101: 急!数据结构二叉树建立家谱 - ?
曾婷诺通: 家谱 是一个树结构,实际情况要用多叉树来表示,如果是子女不超过两个的话,可以用二叉树来表示
南谯区17010049101: C语言鸡兔同笼问题 - ?
曾婷诺通: 数组定义错误.C语言中不能像这样定义变量a,然后定义数组m[a].这个错误导致了你程序运算的错误.修改方法:int m[a],n[a];改为int m[100],n[100]; 即不要用变量a,而用一个常数.只要保证输入的a不大于100就可以了. 如果想用变量作为数组的界限长度,要用到动态数组,不能用你现在的这种方法.