android-nuc / 18-C-Train

18级Android实验室(人工智能+移动互联) C语言培训
16 stars 10 forks source link

张宇(宿舍管理系统不完善) #21

Open MeFH opened 5 years ago

MeFH commented 5 years ago
#include<stdio.h>
#include<malloc.h>
struct stu//定义结构体 
{
    int line;
    int colunm;
    int num;
    struct stu *next;
};
struct stu *Create1(){//初始化矩阵 1 
    int i=0,j=1;
    struct stu *head,*p1,*p2;
    p2=p1=(struct stu*)malloc(sizeof(struct stu));
    p1->line=0;
    p1->colunm=0;
    p1->num=1;
    head=p1;
    p1->next=NULL;
    for(;i<1000;i++)    
    {
        for(;j<1000;j++){
            p1=(struct stu*)malloc(sizeof(struct stu));
            p1->line=i;
            p1->colunm=j;
            p1->num=1;
            p2->next=p1;
            p1->next=NULL;
            p2=p1;
        }
        j=0;
    }
    return head;
}
struct stu *Create2(){//初始化矩阵2 
    int i=0,j=1;
    struct stu *head,*p3,*p4;
    p4=p3=(struct stu*)malloc(sizeof(struct stu));
    p3->line=0;
    p3->colunm=0;
    p3->num= 2;
    head=p3;
    p3->next=NULL;
    for(;i<1000;i++)    
    {
        for(;j<1000;j++){
            p3=(struct stu*)malloc(sizeof(struct stu));
            p3->line=i;
            p3->colunm=j;
            p3->num= 2;
            p4->next=p3;
            p3->next=NULL;
            p4=p3;
        }
        j=0;
    }
    return head;
}
struct stu *CreateNull(){//初始化矩阵3,用于存放矩阵1和矩阵2乘后的矩阵 
    int i=0,j=1;
    struct stu *head,*p5,*p6;
    p6=p5=(struct stu*)malloc(sizeof(struct stu));
    p5->line=0;
    p5->colunm=0;
    p5->num= 0;
    head=p5;
    p5->next=NULL;  
    for(;i<1000;i++)    
    {
        for(;j<1000;j++){
            p5=(struct stu*)malloc(sizeof(struct stu));
            p5->line=i;
            p5->colunm=j;
            p5->num= 0;
            p6->next=p5;
            p5->next=NULL;
            p6=p5;
        }
        j=0;
    }
    return head;
}
void multiplication(struct stu *p1,struct stu *p2,struct stu *p3)//矩阵1和矩阵2相乘 
{
    int i=0;
    struct stu *q1=p1,*q2=p2,*q3=p3;
    int n=0;
    for(;p1->next!=NULL;){
        for(q1=p1;q2;q2=q2->next,q1=q1->next){
            if(n==1000){
                q1=p1;
                n=0;
                q3=q3->next;
            }
            q3->num+=q1->num*q2->num;
            n++;
        }
        q3=q3->next;
        n=0;
        q2=p2;
        if(q1->next!=NULL){
            p1=q1->next;
        }
        else p1=q1;
    }

    for(;p3!=q3;p3=p3->next,i++){//输出相乘后的矩阵 
        if(i==1000){
            printf("\n");
            i=0;
        }
        printf("%d",p3->num);
    }
}
int main ()//主函数 
{
    struct stu *stu1,*stu2,*stu3;
    stu1 = Create1();
    stu2 = Create2();
    stu3 = CreateNull();
    multiplication(stu1,stu2,stu3);
    return 0;
}
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct stu)
int n,j;
struct stu {//学生信息数据结构体
    int study_num;
    char name[20];
    int age;
    char sex[10];
    int dorm_room;
    int bed_room;
    struct stu *next;
};
struct stu *create() { //创建宿舍链表函数
    struct stu *p1,*p2,*head;
    int i = 1;
    printf("请输入要输入学生的个数n=");
    scanf("%d",&n);
    head=NULL;
    p1=p2=(struct stu *)malloc(LEN);
    printf("请输入要录入学生的信息:(按顺序输入:学号,姓名,年龄,性别,宿舍号,床号(用回车键隔开))\n");
    scanf("%d",&p1->study_num);
    scanf("%s",p1->name);
    scanf("%d",&p1->age);
    scanf("%s",p1->sex);
    scanf("%d",&p1->dorm_room);
    scanf("%d",&p1->bed_room);
    head = p1;
    p1->next = NULL;
    while(i<n) {
        p1=(struct stu*)malloc(sizeof(struct stu));
        scanf("%d",&p1->study_num);
        scanf("%s",p1->name);
        scanf("%d",&p1->age);
        scanf("%s",p1->sex);
        scanf("%d",&p1->dorm_room);
        scanf("%d",&p1->bed_room);
        p2->next=p1;
        p1->next=NULL;
        p2=p1;
        i++;
    }
    getchar();
    return head;
}
void print(struct stu *head) {//输出学生信息
    struct stu *p;
    p=head;
    while(p) {
        printf("学号:%d\n",p->study_num);
        printf("姓名:%s\n",p->name);
        printf("年龄:%d\n",p->age);
        printf("性别:%s\n",p->sex);
        printf("宿舍号:%d\n",p->dorm_room);
        printf("床号:%d\n",p->bed_room);
        printf("\n");
        p=p->next;
    }
}
struct stu *find(struct stu *head,int i) { //寻找指定学生信息
    int j=0;
    struct stu *p=head;
    for(; p; j++) {
        if(p->study_num == i) break;
        p=p->next;
    }
    return p;
}
struct stu *insert (struct stu *head) { //插入学生信息
    struct stu *p,*q;
    char r;
    printf("请输入插入学生的信息:\n");
    p=(struct stu*)malloc(sizeof(struct stu));
    scanf("%d",&p->study_num);
    scanf("%s",p->name);
    scanf("%d",&p->age);
    scanf("%s",p->sex);
    scanf("%d",&p->dorm_room);
    scanf("%d",&p->bed_room);
    getchar();
    printf("请输入插入的位置:(插入开头请输入M,插入中间请输入N)\n");
    scanf("%c",&r);
    getchar();
    int i,x=0;
    if(r=='M') {
        p->next = head->next;
        p->next = head;
        head = p;
    } else if (r=='N') {
        printf("请输入插入的位置:(即前一位学生的学号)\n");
        scanf("%d",&i);
        q=find(head,i);
        if(j > n ) {
            printf("\n找不到第%d个数据,不能插入%d!",i,x);
        } else {
            p->next=q->next;
            q->next=p;
        }
    }
    return head;
}
struct stu *dele(struct stu *head,int x) { //删除指定学生信息
    struct stu *pre = NULL, *p;
    if(!head) {
        printf("数据为空。");
    }
    p=head;
    while(p&&p->study_num!=x) {
        pre=p;
        p=p->next;
    }
    if(p) {
        if(!pre) head=head->next;
        else pre->next=p->next;
        free(p);
    }
    return head;
}
void enter(struct stu *head,int n) {
    FILE *fp;
    fwrite(head,sizeof(LEN),n,fp);
    printf("成功录入!");
}
int main () {
    char a;
    int x,n=0;
    struct stu *pt,*p,*p1;
    pt=create();
    for(;;) {
        printf("请输入想要执行的工作:(输出:A 查询:B 插入:C 删除:D 结束: E)\n");
        scanf("%c",&a);
        getchar();
        if(a == 'A') {
            printf("输出结果为:\n");
            print(pt);
        } else if(a == 'B') {
            printf("请输入要查询学生的学号:");
            scanf("%d",&x);
            getchar();
            p=find(pt,x);
            printf("学号:%d\n",p->study_num);
            printf("姓名:%s\n",p->name);
            printf("年龄:%d\n",p->age);
            printf("性别:%s\n",p->sex);
            printf("宿舍号:%d\n",p->dorm_room);
            printf("床号:%d\n",p->bed_room);
        } else if (a == 'C') {
            p1=insert(pt);
            print(p1);
        } else if(a == 'D') {
            printf("请输入需要删除学生的学号:");
            scanf("%d",&x);
            getchar();
            p1=dele(pt,x);
            print(p1);
        } else if(a == 'E') break;
        else printf("输入错误!");
    }
    enter(pt,n);
    return 0;
}
wmpscc commented 5 years ago

点评:

修改!