论坛首页 编程语言技术论坛

数据结构实训:学生信息系统应用

浏览 3783 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (4) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-01-04  
C
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>
#define size 25

typedef struct node
{
char name[10];
int no;
char sex[5];
int age;
float english;
float math;
float ds;
float os;
float total;
}NODE;

typedef struct node1
{
NODE data;
   struct node1 *next;
}student;

typedef struct node2
{
    char data[20];
float no1;
    struct node2*link ;
}ListNode;


student *p;
student *head;

float total(student *p)
{
float tot;
tot=p->data.ds+p->data.english+p->data.math+p->data.os;
printf("total=%5.0f\n",tot);
return tot;
}

student *crelist()
{
student *head,*p,*q;
int i;
float zf;
head=(student*)malloc(sizeof(student));
printf("xingming:");
scanf("%s",&head->data.name);
printf("xuehao:");
scanf("%d",&head->data.no);
printf("xingbie:");
scanf("%s",&head->data.sex);
printf("nianling:");
scanf("%5d",&head->data.age);
printf("shuruchengji\n");
    printf("english  math  ds  os\n");
scanf("%f%f%f%f",&head->data.english,&head->data.math,&head->data.ds,&head->data.os);
    zf=total(head);
head->data.total=zf;
p=head;
for (i=1;i<=size;i++)
{

q=(student*)malloc(sizeof(student));
printf("xingming:");
        scanf("%s",&q->data.name);
printf("xuehao:");
scanf("%d",&q->data.no);
printf("xingbie:");
scanf("%s",&q->data.sex);
printf("nianling:");
scanf("%d",&q->data.age);
printf("shuruchengji\n");
printf("english math ds os\n");
scanf("%f%f%f%f",&q->data.english,&q->data.math,&q->data.ds,&q->data.os);
zf=total(q);
q->data.total=zf;
p->next=q;
p=q;
}
p->next=NULL;
return head;
}


void prilist(student *head)
{
p=head;
    printf("xingming  xuehao  xingbie  \n");
printf("english math os ds\n");
while (p!=NULL)
{
printf("  %s       %d    %s\n",p->data.name,p->data.no,p->data.sex);
printf("%5.0f%5.0f%5.0f%5.0f\n",p->data.english,p->data.math,p->data.ds,p->data.os);
printf("%5.0f\n",p->data.total);
p=p->next;
}
}

void print(student *head)
{
p=head;
while (p!=NULL)
{
printf("%s\n",p->data.name);
p=p->next;
}
}


void tjstu(student *p,student *head)
{
student *p1,*p2;
p1=head;
p2=p1->next;
while (p2!=NULL &&p2->data.no<p->data.no)
{
p1=p2;
p2=p2->next;
}
p1->next=p;
p->next=p2;
}


student *chazhao(student *head)
{
student *p;
int xuan;
int no;
char name[10];
printf("1)  anxuehao\n2)  anxingming\n");
scanf("%d",&xuan);
if(xuan==1)
{
p=head;
printf("shuruxuehao:");
scanf("%d",&no);
while(p->data.no!=no)
p=p->next;
}
else if(xuan==2)
{
p=head;
printf("shuruxingming\n");
scanf("%s",&name);
while (strcmp(p->data.name,name)!=0)
p=p->next;
}
else
printf("error");
printf("%d,%s,%s\n",p->data.no,p->data.name,p->data.sex);
return p;

}

void shanchu(student *head)
{
int sc;
student *p,*q;
p=chazhao(head);
if (p==NULL)
{
printf("wu");
return;
}
printf("queding  1  fanhui  0\n");
scanf("%d",&sc);
if(sc==1)
{
q=head;
while(q!=NULL&&q->next!=p)
q=q->next;
q->next=p->next;
free(p);
printf("shanchujieshu\n");
}
}

void xiugai(student *head)
{
p=chazhao(head);
printf("shuruxuehao:");
scanf("%d",&p->data.no);
printf("shuruxingming:");
scanf("%s",&p->data.name);
printf("shuruxingbie:");
scanf("%s",&p->data.sex);
printf("shuruchengji\n");
printf("english math ds os\n");
scanf("%f%f%f%f",&p->data.english,&p->data.math,&p->data.ds,&p->data.os);
}

void ListInsert1(ListNode**L,float no)  
{
    ListNode *p, *q, *curr ;
    p = (ListNode*)malloc(sizeof(ListNode));
    p->no1=no;
    p->link = NULL;
    if((*L) == NULL)
    {
        (*L) = p;
        return;
    }
    else if(no <=(*L)->no1)  
    {
        p->link = (*L);
        (*L) = p;
        return;
    }
    else
    {
        curr = (*L);
        q = (*L);   
        while(q != NULL && (no>=q->no1))
        {
            curr = q;
            q = q->link;
        }      
        p->link = curr->link;
        curr->link = p;
        return;
    }
}

void Print1(ListNode*L)   
{
    ListNode *curr = L;
    while(curr!=NULL)
    {
        printf("%5.0f  \n",curr->no1);
        curr=curr->link ;
    }
}

void ListInsert2(ListNode**L,char x[]) 
{
    ListNode *p, *q, *curr ;
    p = (ListNode*)malloc(sizeof(ListNode));   
    strcpy(p->data,x);
    p->link = NULL;  
    if((*L) == NULL)
    {
        (*L) = p;
        return;
    }
    else if(strcmp(x,(*L)->data) <= 0)  
    {
        p->link = (*L);
        (*L) = p;
        return;
    }
    else
    {
        curr = (*L);
        q = (*L);   
        while(q != NULL && strcmp(x,q->data) >= 0)
        {
            curr = q;
            q = q->link;
        }       
        p->link = curr->link;
        curr->link = p;
        return;
    }
}

void Print2(ListNode*L)   
{
    ListNode *curr = L;
    while(curr!=NULL)
    {
        printf("%s  \n",curr->data);
        curr=curr->link ;
    }
}

void save()
{
FILE *fp;
int i;
if((fp=fopen("ds.dat","wb"))==NULL)
{
printf("cannot open file\n");
return;
}
for (i=0;i<size;i++)
if(fwrite(head,sizeof(student),1,fp)!=1)
printf("file write error\n");
fclose(fp);
}

void save1()
{

    FILE *fp;
int i;
if((fp=fopen("new1.dat","wb"))==NULL)
{
printf("cannot open file\n");
return;
}
for (i=0;i<size;i++)
if(fwrite(head,sizeof(student),1,fp)!=1)
printf("file write error\n");
fclose(fp);
}

void save2()
{
    FILE *fp;
int i;
if((fp=fopen("new2.dat","wb"))==NULL)
{
printf("cannot open file\n");
return;
}
for (i=0;i<size;i++)
if(fwrite(head,sizeof(student),1,fp)!=1)
printf("file write error\n");
fclose(fp);
}




   



void showstumenu()
{
printf("**************************");
printf("      xueshengxinxi       ");
printf("**************************\n");

printf("0)         jianbiao\n");
printf("1)         tianjia\n");
printf("2)         zhanshi\n");
printf("3)         shanchu\n");
printf("4)         xiugai\n");
printf("5)         chazhao\n");
printf("6)         paixu1(zhongchengji)\n");
printf("7)         paixu2(xingming)\n");
printf("8)         beifen\n");
printf("9)         dayin\n");
printf("10)        over\n");
printf("\n\n");
}


void main()
{
int xx;
showstumenu();
printf("shurushuzi:");
scanf("%d",&xx);
if (xx==0)
head=crelist();
save();
for(;;)
{
int a;
showstumenu();
printf("shurushuzi:");
scanf("%d",&a);
switch (a)
{

       case 1:
   {
        float tota;
    printf("tianjia\n");
        p=(student*)malloc(sizeof (student));
printf("xingming:");
        scanf("%s",&p->data.name);
printf("xuehao:");
scanf("%d",&p->data.no);
printf("xingbie:");
scanf("%s",&p->data.sex);
printf("english math ds os\n");
scanf("%f%f%f%f",&p->data.english,&p->data.math,&p->data.ds,&p->data.os);
tota=total(head);
p->data.total=tota;
        tjstu(p,head);
        break;

   }

      case 2:
  {
        printf("zhanshi\n");
        prilist(head);
        break;
  }

      case 3:
  {
        printf("shanchu\n");
                shanchu(head);
        break;
  }

  case 4:
  {
  printf("xiugai\n");
  xiugai(head);
  break;
  }

  case 5:
  {
  p=chazhao(head);
  break;
  }

  case 6:
  {
int i ;
             ListNode *head1 ;
             p=head;
             head1=NULL ;
             for(i = 0;i < 3;i++)
{
               ListInsert1(&head1,p->data.total);
       p=p->next;
}
             printf("链表中数据输出如下:\n");
             Print1(head1);
save1();
  break;
  }

  case 7:
  {
             int i ;
             ListNode *head1 ;
           p=head;
             head1=NULL ;
             for(i = 0;i < 3;i++)
{
              ListInsert2(&head1,p->data.name);
      p=p->next;
}
             printf("链表中数据输出如下:\n");
             Print2(head1);
save2();
          break;
  }

  case 8:
  {
  FILE *in,*out;
          if((in=fopen("ds.dat","r"))==NULL)
  {
        printf("cannot open infile\n");
         exit(0);
  }
        if((out=fopen("ds1.dat","w"))==NULL)
{
        printf("cannot open outfile\n");
             exit (0);
}
          while(!feof(in))fputc(fgetc(in),out);
             fclose(in);
             fclose(out);
          return;
  }
  case 9:
  {
  printf("daying");
  print(head);
  }

  case 10:
  {
  printf("jieshu");
  return;
  }    
}
}
}
   发表时间:2011-05-29  
你把代码贴上来,想说明什么问题呢?
0 请登录后投票
   发表时间:2011-06-11  
可以看看~
0 请登录后投票
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics