#include<stdio.h>
#define TREEMAX 100
typedef struct BT
{
char data;
BT *lchild;
BT *rchild;
}BT;
BT *CreateTree();
void Preorder(BT *T);
void Postorder(BT *T);
void Inorder(BT *T);
void Leafnum(BT *T);
void Nodenum(BT *T);
int TreeDepth(BT *T);
int count=0;
void main()
{
BT *T=NULL;
char ch1,ch2,a;
ch1='y';
while(ch1=='y'||ch1=='y')
{
printf("\n");
printf("\n\t\t 二叉樹子系統");
printf("\n\t\t*****************************************");
printf("\n\t\t 1---------建二叉樹 ");
printf("\n\t\t 2---------先序遍歷 ");
printf("\n\t\t 3---------中序遍歷 ");
printf("\n\t\t 4---------后序遍歷 ");
printf("\n\t\t 5---------求葉子數 ");
printf("\n\t\t 6---------求結點數 ");
printf("\n\t\t 7---------求樹深度 ");
printf("\n\t\t 0---------返 回 ");
printf("\n\t\t*****************************************");
printf("\n\t\t 請選擇菜單號 (0--7)");
scanf("%c",&ch2);
getchar();
printf("\n");
switch(ch2)
{
case'1':
printf("\n\t\t請按先序序列輸入二叉樹的結點:\n");
printf("\n\t\t說明:輸入結點(‘0’代表后繼結點為空)后按回車。\n");
printf("\n\t\t請輸入根結點:");
T=CreateTree();
printf("\n\t\t二叉樹成功建立!\n");break;
case'2':
printf("\n\t\t該二叉樹的先序遍歷序列為:");
Preorder(T);break;
case'3':
printf("\n\t\t該二叉樹的中序遍歷序列為:");
Inorder(T);break;
case'4':
printf("\n\t\t該二叉樹的后序遍歷序列為:");
Postorder(T);break;
case'5':
count=0;Leafnum(T);
printf("\n\t\t該二叉樹有%d個葉子。\n",count);break;
case'6':
count=0;Nodenum(T);
printf("\n\t\t該二叉樹總共有%d個結點。\n",count);break;
case'7':
printf("\n\t\t該樹的深度為:%d",TreeDepth(T));
break;
case'0':
ch1='n';break;
default:
printf("\n\t\t***請注意:輸入有誤!***");
}
if(ch2!='0')
{
printf("\n\n\t\t按【enter】鍵繼續,按任意鍵返回主菜單!\n");
a=getchar();
if(a!='\xA')
{
getchar();
ch1='n';
}
}
}
}
BT *CreateTree()
{
BT *t;
char x;
scanf("%c",&x);
getchar();
if(x=='0')
t=NULL;
else
{
t=new BT;
t->data=x;
printf("\n\t\t請輸入%c結點的左子結點:",t->data);
t->lchild=CreateTree();
printf("\n\t\t請輸入%c結點的右子結點:",t->data);
t->rchild=CreateTree();
}
return t;
}
void Preorder(BT *T)
{
if(T)
{
printf("%3c",T->data);
Preorder(T->lchild);
Preorder(T->rchild);
}
}
void Inorder(BT *T)
{
if(T)
{
Inorder(T->lchild);
printf("%3c",T->data);
Inorder(T->rchild);
}
}
void Postorder(BT *T)
{
if(T)
{
Postorder(T->lchild);
Postorder(T->rchild);
printf("%3c",T->data);
}
}
void Leafnum(BT *T)
{
if(T)
{
if(T->lchild==NULL&&T->rchild==NULL)
count++;
Leafnum(T->lchild);
Leafnum(T->rchild);
}
}
void Nodenum(BT *T)
{
if(T)
{
count++;
Nodenum(T->lchild);
Nodenum(T->rchild);
}
}
int TreeDepth(BT *T)
{
int ldep,rdep;
if(T==NULL)
return 0;
else
{
ldep=TreeDepth(T->lchild);
rdep=TreeDepth(T->rchild);
if(ldep>rdep)
return ldep+1;
else
return rdep+1;
}
}
標簽:
二叉樹
子系統
上傳時間:
2020-06-11
上傳用戶:ccccy
數字示波器功能強大,使用方便,但是價格相對昂貴。本文以Ti的MSP430F5529為主控器,以Altera公司的EP2C5T144C8 FPGA器件為邏輯控制部件設計數字示波器。模擬信號經程控放大、整形電路后形成方波信號送至FPGA測頻,根據頻率值選擇采用片上及片外高速AD分段采樣。FPGA控制片外AD采樣并將數據輸入到FIFO模塊中緩存,由單片機進行頻譜分析。測試表明:簡易示波器可以實現自動選檔、多采樣率采樣、高精度測頻及頻譜分析等功能。Digital oscilloscope is powerful and easy to use, but also expensive. The research group designed a low-cost digital oscilloscope, the chip of MSP430F5529 of TI is chosen as the main controller and the device of EP2C5T144C8 of Altera company is used as the logic control unit. Analog signal enter the programmable amplifier circuit, shaping circuit and other pre-processing circuit. The shaped rectangular wave signal is sent to FPGA for measure the frequency. According to the frequency value to select AD on-chip or off-chip high-speed AD for sampling. FPGA controls the off-chip AD sampling and buffers AD data by FIFO module. The single chip microcomputer receives the data, and do FFT for spectrum analysis. The test shows that the simple oscilloscope can realize automatic gain selection, sampling at different sampling rates, high precision frequency measurement and spectrum analysis.
標簽:
msp430
單片機
fpga
數字示波器
上傳時間:
2022-03-27
上傳用戶: