2 回答

TA貢獻1752條經驗 獲得超4個贊
完整程序
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<stdlib.h>
int n,m,i,t,q1=1;
int e=1,counter=0,counter1=0,counter2=0;
struct student
{
char name[20];
int num;
int classname;
int math;
int english;
int chinese;
};
typedef student DataType;
/**********鏈表定義**********/
typedef struct Node
{
DataType data;
struct Node *next;
}SLNode;
SLNode *head,*q,s1;
/**********數學二叉樹定義***********/
typedef struct node
{
DataType data;
struct node*leftChild;
struct node*rightChild;
}BiTreenode;
BiTreenode *root,*v,*u,*o,*q4,*q2,*q3;
void Initiate(BiTreenode**root)
{*root=(BiTreenode*)malloc(sizeof(BiTreenode));
(*root)->leftChild=NULL;
(*root)->rightChild=NULL;
}
/**************英語初始化函數*****************/
void initiate(BiTreenode**eoot)
{*eoot=(BiTreenode*)malloc(sizeof(BiTreenode));
(*eoot)->leftChild=NULL;
(*eoot)->rightChild=NULL;
}
BiTreenode *eoot,*a,*b,*c;
char s[]=;
/*****************初始化**********************/
void ListInitiate(SLNode **head )
{
if((*head=(SLNode*)malloc(sizeof(SLNode)))==NULL) exit(1);
(*head)->next=NULL;
}
/****************求當前數據元素個數***********/
int ListLength(SLNode *head)
{
SLNode *p=head;
int size=0;
while(p->next!=NULL)
{
p=p->next;
size++;
}
return size;
}
/**************插入數據元素****************/
int ListInsert(SLNode *head,int i,DataType x)
{
SLNode *p,*q;
int j;
p=head;
j=-1;
while(p->next!=NULL&&j<i-1)
{
p=p->next;
j++;
}
if(j!=i-1)
{
printf("插入位置參數錯誤!\n");
return 0;
}
if((q=(SLNode *)malloc(sizeof(SLNode)))==NULL) exit(1);
q->data=x;
q->next=p->next;
p->next=q;
return 1;
}
/****************刪除數據元素****************/
int ListDelete(SLNode *head,int i,DataType *x)
{
SLNode *p,*s;
int j;
p=head;
j=-1;
while(p->next!=NULL&&j<i-1&&p->next->next!=NULL)
{
p=p->next;
j++;
}
if(j!=i-1)
{
printf("刪除位置參數錯誤!\n");
return 0;
}
s=p->next;
*x=s->data;
p->next=p->next->next;
free(s);
return 1;
}
/***************取數據元素*****************/
int ListGet(SLNode *head,int i,DataType *x)
{
SLNode *p;
int j;
p=head;
j=-1;
while(p->next!=NULL&&j<i)
{
p=p->next;
j++;
}
if(j!=i)
{
printf("取元素位置參數錯誤!\n");
return 0;
}
*x=p->data;
return 1;
}
/************撤消單鏈表**************/
void Destroy(SLNode * *head)
{
SLNode *p,*p1;
p=*head;
while(p!=NULL)
{
p1=p;
p=p->next;
free(p1);
}
*head=NULL;
}
/***********數學尋樹函數****************/
void Sxunshu(BiTreenode *v,SLNode *p)
{
if(p->next->data.math>v->leftChild->data.math)
{
if(v->leftChild->leftChild!=NULL)
{v=v->leftChild;
Sxunshu(v,p);}
else
{v->leftChild->leftChild=o;
v->leftChild->leftChild->rightChild=NULL;
v->leftChild->leftChild->leftChild=NULL;
}
}
else
{
if(v->leftChild->rightChild!=NULL)
{
v=v->rightChild;
Sxunshu(v,p);
}
else
{
v->leftChild->rightChild=o;
v->leftChild->rightChild->rightChild=NULL;
v->leftChild->rightChild->leftChild=NULL;
}
}
}
/*************英語尋樹函數**************/
void Exunshu(BiTreenode *a,SLNode *p)
{
if(p->next->data.english>a->leftChild->data.english)
{
if(a->leftChild->leftChild!=NULL)
{a=a->leftChild;
Exunshu(a,p);}
else
{a->leftChild->leftChild=c;
a->leftChild->leftChild->rightChild=NULL;
a->leftChild->leftChild->leftChild=NULL;
}
}
else
{
if(a->leftChild->rightChild!=NULL)
{
a=a->rightChild;
Exunshu(a,p);
}
else
{
a->leftChild->rightChild=c;
a->leftChild->rightChild->rightChild=NULL;
a->leftChild->rightChild->leftChild=NULL;
}
}
}
/*************英語成績錄入**************/
void englishy()
{
SLNode *p;
initiate(&eoot);
b=eoot;
p=head;
printf("共有%d位同學成績輸入",counter2);
do
{
c=(BiTreenode*)malloc(sizeof(BiTreenode));
strcpy(s,p->next->data.name);
puts(s);
scanf("%d",&t);
p->next->data.english=t;
strcpy(c->data.name,p->next->data.name);
c->data.english=p->next->data.english;
a=eoot;
if(b==eoot)
{b->leftChild=c;
b=b->leftChild;
b->leftChild=NULL;
b->rightChild=NULL;
}
else Exunshu(a,p);
p=p->next;
counter2--;
}while(p->next!=NULL);
printf("英語成績錄入完畢");
}
/**********數學成績錄入**********/
void mathy()
{SLNode *p;
Initiate(&root);
u=root;
p=head;
printf("共有%d位同學成績輸入",counter1);
do
{
o=(BiTreenode*)malloc(sizeof(BiTreenode));
strcpy(s,p->next->data.name);
puts(s);
scanf("%d",&t);
p->next->data.math=t;
strcpy(o->data.name,p->next->data.name);
o->data.math=p->next->data.math;
v=root;
if(u==root)
{u->leftChild=o;
u=u->leftChild;
u->leftChild=NULL;
u->rightChild=NULL;
}
else Sxunshu(v,p);
p=p->next;
counter1--;
}while(p->next!=NULL);
printf("數學成績錄入完畢");
}
/*****************注冊函數********************/
int zhuce()
{ SLNode *p;
q=head;
int j,z=0;
j=-1;
while(q->next!=NULL&&j<z-1)
{
q=q->next;
j++;
}
if(j!=z-1)
{
printf("插入位置參數錯誤!\n");
return 0;
}
printf("請輸入注冊學生數目");
scanf("%d",&m);
for(i=0;i<m;i++)
{
if((p=(SLNode *)malloc(sizeof(SLNode)))==NULL) exit(1);
q->next=p;
q=q->next;
q->next=NULL;
printf("請輸入第%d學生姓名\n",i+1);
scanf("%s",s);
strcpy(q->data.name,s);
printf("請輸入該學生班級\n");
scanf("%d",&q->data.classname);
printf("請輸入該學生學號\n");
scanf("%d",&q->data.num);
printf("注冊成功!\n");
counter++;
counter1++;
counter2++;
}
printf("\t\n注冊已完畢!\n");
if(counter1==0)counter1=counter;
if(counter2==0)counter2=counter;
return 1;
}
/***********學生刪除函數****************/
void zengjia()
{
while(e==1)
{
printf("請輸入您要刪除的學生姓名\n");
scanf("%s",s);
SLNode *p,*r;
p=head;
r=p;
while(p->next!=NULL&&strcmp(p->next->data.name,s))
{
r=p;
p=p->next;
}
if(p->next==NULL)printf("刪除失敗");
else{
r->next=p->next->next;
p=r->next;
printf("已刪除!\n\t1.繼續(xù)刪除\t\t2.返回\n");
printf("請選擇[ ]\b\b");
scanf("%d",&e);
}
}
e=1;
counter--;
counter1--;
counter2--;
}
/*******************成績錄入函數********************/
int chengjiluru()
{
printf("請選擇需要錄入的學科\n");
printf("1.math\t2.english\n\t[ ]\b\b");
scanf("%d",&t);
switch(t)
{
case 1:mathy();break;
case 2:englishy();break;
}
return 1;
}
/************數學后續(xù)遍歷函數*******************/
void postorder(BiTreenode*q2)
{
if(q2!=NULL)
{ postorder(q2->leftChild);
postorder(q2->rightChild);
if(!strcmp(q2->data.name,s))q2->data.math=n;
}
}
/************數學成績修改****************/
void chengjixiugai()
{
SLNode *h,*f;
q2=root->leftChild;
h=head;
f=h;
printf("輸入成績錯誤的的科目是:\n1.math\t2.english\n []\b\b");
scanf("%d",&t);
printf("成績需要修改的學生名字是\n");
scanf("%s",s);
while(strcmp(h->next->data.name,s))
{
if(h->next->next==NULL)break;
else h=h->next;
}
if(h->next->next==NULL)printf("沒有此學生");
else{
if(t==1)
{
printf("請再次輸入成績");
scanf("%d",&n);
h->next->data.math=n;
postorder(q2);
printf("成績修改完畢");
}
}
}
int shuru()
{
printf("\t\t\t歡迎光臨學生成績管理系統\n\n\n");
printf("功能如下:");
printf("\t1。學生注冊登記\n\t2。刪除學生\n\t3。成績錄入\n\t4。成績修改\n\t5。統計分析\n\t6。查找\n\t7。打印\n");
printf("請選擇您需要的功能[ ]\b\b");
scanf("%d",&n);
switch(n)
{
case 1:zhuce();break;
case 2:zengjia();break;
case 3:chengjiluru();break;
case 4:chengjixiugai();break;
//se 5:tongjifenxi();
//se 6:chazhao();
//se 7:dayin();
}
return 1;
}
void main()
{ ListInitiate(&head);
q=head;
while(1)
{
shuru();
}
}

TA貢獻1803條經驗 獲得超6個贊
#include "iostream"
#include "string"
#include "list"
using namespace std;
class clin
{
list<int> li;
list<int>::iterator head;
list<int>::iterator next;
public :
void out();
void in(int data);
bool isnull();
};
void clin::in(int data)
{
li.push_back(data);
}
void clin::out()
{
cout<<*li.begin()<<endl;
li.remove(*li.begin());
}
bool clin::isnull()
{
return li.empty();
}
int main()
{
clin l;
int a,data;
while(1)
{
cout<<"1.into"<<endl<<"2.out"<<endl;
cin>>a;
switch(a)
{
case 1:
cin>>data;
l.in(data);
break;
case 2:
if(l.isnull())
{
cout<<"is null"<<endl;
break;
}
l.out();
break;
default :
break;
}
}
}
添加回答
舉報