📄 stu.c
字号:
/* file_name: init.c
* author: wangtiezhen miuliang @ Xidian University
* description:
*/
struct stu *inputstu(int num, int password,char name[], int age, char addr[]) ;
int readstudent(void) ;
int readstudent(void) {
FILE *fp;
int i = 0;
if( (fp = fopen("student", "r") ) == NULL) {
printf("It cann't opean file\n"); /* Cannot open the file */
return -1;
}
while( !feof(fp) ) {
int num, age,password;
char name[20], addr[20];
if(fscanf(fp, "%s%d%d%d%s", name, &num,&password, &age, addr) == 5){
inputstu(num, password,name, age, addr);
}
if(!feof(fp)){
i++;
}
}
fclose(fp);
return i;
}
struct stu *inputstu(int num, int password,char name[], int age, char addr[]) {
struct stu *p,*q;
p=(struct stu *)malloc(sizeof(struct stu));
p->num = num;
p->password=password;
strcpy(p->name,name);
strcpy(p->addr,addr);
p->age = age;
p->lchild=NULL;
p->rchild=NULL;
if((p->num)!=0) {
q=stubt;
if(q==NULL)
stubt=p;
else {
while((q->lchild!=p)&&(q->rchild)!=p) {
if((p->num)<(q->num)) {
if(q->lchild!=NULL)
q=q->lchild;
else
q->lchild=p;
} else {
if(q->rchild!=NULL) {
q=q->rchild;
} else
q->rchild=p;
}
}
}
}
return stubt;
}
/* Begin:
* stu travel;
*/
int print_stu(struct stu* root)
{
if(root == NULL){
printf("no find.\n");
return -1;
}
// putchar('p');
printf("num:%d\tpass:%d\tname:%s\tage:%d\taddr:%s\n", root->num, root->password,
root->name, root->age, root->addr);
return;
}
int tree_visit_stu(struct stu* root, int f(), int style)
{
// putchar('j');
if(root == NULL)
{
return 0;
}
if(style == 0)
(*f)(root);
tree_visit_stu (root->lchild, f, style );
if(style == 1)
(*f)(root);
tree_visit_stu (root->rchild, f, style );
if(style == 2)
(*f)(root);
}
/* End:
* stu travel;
*/
/* Begin:
* stu find;
*/
struct stu* find_stu_name(struct stu* root, char* find_name)
{
struct stu* t = NULL;
// putchar('j');
if(root == NULL){
return NULL;
}
if(!strcmp(find_name , root->name)){
// printf("find it!");
return root;
}
// putchar('J');
if((t = find_stu_name (root->lchild, find_name )) != NULL){
return t;
}
if((t = find_stu_name (root->rchild, find_name )) != NULL){
return t;
}
// printf("\t");
return NULL;
}
struct stu *find_stu_num(struct stu *stubt,int num) {
struct stu *p;
p=stubt;
if(p==NULL)
return NULL;
if(num==p->num)
return(p);
else if(num > p ->num) {
return find_stu_num(p->rchild, num );
} else if(num < p ->num) {
return find_stu_num(p->lchild, num );
}
}
/* End:
* stu find;
*/
/* Begin:
* student del
*/
extern int stu_del(stupointer *);
extern int stu_del_value(stupointer *, int );
int stu_del_value(stupointer *proot, int num){
stupointer root = *proot;
if(root == NULL){
return 0;
}
if(root ->num == num){
stu_del(proot);
}else if(num > root ->num){
if(root -> rchild == NULL){
return 0;
}
if(root ->rchild -> num == num){
return stu_del( &(root->rchild));
}else{
return stu_del_value(&(root->rchild), num );
}
}else if(num < root ->num){
if(root -> lchild == NULL){
return 0;
}
if(root ->lchild -> num == num){
return stu_del( &(root->lchild));
}else{
return stu_del_value(&(root->lchild), num );
}
}
}
int stu_del(stupointer *proot){
stupointer root = *proot;
if(root == NULL)
return 0;
if(root -> lchild == NULL){
*proot = root -> rchild;
free(root);
}else if(root -> rchild == NULL){
*proot = root -> lchild;
free(root);
}else{
stupointer temp = root -> lchild;
while (temp->rchild != NULL){
temp = temp -> rchild;
}
temp -> rchild = root -> rchild;
free(root);
}
}
int stu_destory(stupointer root){
if(root == NULL){
return 0;
}
stu_destory (root->lchild);
stu_destory (root->rchild);
// printf("Killing num:\t%ld\t\n", root->bnum);
free(root);
}
/* End:
* stu del
*/
//int stu_destory(stupointer root){
// if(root == NULL){
// return 0;
// }
// stu_destory (root->lchild);
// stu_destory (root->rchild);
//// printf("Killing num:\t%ld\t\n", root->num);
// free(root);
//
//}
/* Begin:
* stu update;
*/
struct stu * stu_update(int src_num,
char* pname, char* paddr,
int *page, int *ppassword,
int* pnum)
{
// putchar('a');
struct stu* p = find_stu_num(stubt, src_num);
// putchar('s');
char n_name[20], n_addr[20];
int n_age, n_password, n_num;
if(pname != NULL){
strcpy(n_name, pname);
}else{
strcpy(n_name, p->name);
}
if(paddr != NULL){
strcpy(n_addr, paddr);
}else{
strcpy(n_addr, p->addr);
}
if(pnum != NULL){
n_num = *pnum;
}else{
n_num = src_num;
}
if(page != NULL){
n_age = *page;
}else{
n_age = p->age;
}
if(ppassword != NULL){
n_password = *ppassword;
}else{
n_password = p->password;
}
stu_del_value(&stubt, src_num);
inputstu(n_num, n_password, n_name, n_age, n_addr);
return NULL;
}
/* End:
* book update;
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -