android-nuc / 18-C-Train

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

陈抒菡 #33

Open notwiioe opened 5 years ago

notwiioe commented 5 years ago

#include"stdio.h"
#include"stdlib.h"
typedef struct Matrix{
    int i,j,a;
    struct Matrix *right,*down;
}Mat,*Link;
typedef struct{
    Link *rhead,*chead;
    int m,n,t;
}CrossList;
void initialize(CrossList *M)
{
    M->chead = NULL;
    M->rhead = NULL;
    M->m = M->n = M->t = 0;
}
void creat(CrossList *M)
{
    int i,j,a;
    int k;
    Mat *p,*q;
    M->m = 1000;
    M->n = 1000;
    M->t = 1e6;
    FILE *fp = fopen("E://test//矩阵.txt","r");
    M->rhead = (Link *)malloc(1001*sizeof(Link));
    M->chead = (Link *)malloc(1001*sizeof(Link));
    for(k = 1;k <= 1000;k ++)
    {
        M->rhead[k] = NULL;
        M->chead[k] = NULL;
    }
    while(fp)
    {
        fscanf(fp,"%d%d%d",&i,&j,&a);
        p = (Mat *)malloc(sizeof(Mat));
        p->i = i;
        p->j = j;
        p->a = a;
        //修改行的指针域
        if(M->rhead[i] == NULL || M->rhead[i]->j > j)
        {
            p->right = M->rhead[i];
            M->rhead[i] = p;
        }
        else
        {
            for(q = M->rhead[i]; q->right && q->j < j;p = q->right);
                p->right = q->right;
                q->right = p;
        }
        if(M->chead[i] == NULL || M->chead[i]->j > j)
        {
            p->down = M->chead[i];
            M->chead[i] = p;
        }
        else
        {
            for(q = M->chead[i]; q->down && q->j < j;p = q->down);
                p->down = q->down;
                q->down = p;
        }
    }
    fclose(fp);
}
void multiply(CrossList X,CrossList Y, CrossList *Q)
{
    int i,j,a;
    int k;
    Link q,q0,p0,q1,q2;//新节点,n rhead,m chead,q rhead,q chead;
    initialize(Q);
    Q->m = 1000;
    Q->n = 1000;
    Q->t = 0;
    Q->rhead = (Link *)malloc(1001*sizeof(Link));
    Q->chead = (Link *)malloc(1001*sizeof(Link));
    for(k=1;k <= 1000; k++)
    {
        Q->chead[k] = NULL;
        Q->rhead[k] = NULL;
    }
    for(i=1;i <= 1000;i++)
        for(j=1;j <= 1000;j++)
    {
        p0 = X.rhead[i];
        q0 = Y.chead[i];
        a=0;
        while(p0 && q0)
        {
            if(p0->i == q0->j)
            {
                a += p0->a * q0->a;
                p0 = p0->right;
                q0 = q0->down;
            }
            else if(p0->i < q0 ->j)
                p0 = p0->right;
            else
                q0 = q0->down;
        }
        while(a)
        {
            (Q->t)++;
            q=(Link)malloc(sizeof(Mat));
            q->i = i;
            q->j = j;
            q->a = a;
            q->right=NULL;
            q->down=NULL;
            if(!Q->rhead[i])
                Q->rhead[i] = q1 = q;
            else
                while(q1->right)
                q1 = q1->right;
                q1->right = q;
            if(!Q->chead[j])
                Q->chead[j] = q2 = q;
            else
                while(q2->down)
                q2 = q2->down;
                q2->down = q;
        }
    }
}
void save(CrossList D)
{
    int k;
    Link p;
    FILE *fp = fopen("E://test//矩阵.txt","a+");
    for(k=1;k<=1000;k++)
    {
        p = D.rhead[k];
        printf("%d行%d列值为%d\n",p->i,p->j,p->a);
        fprintf(fp,"%d\t%d\t%d\n",p->i,p->j,p->a);
        p=p->right;
    }
    fclose(fp);
}
int main()
{
    CrossList A,B,C;
    initialize(&A);
    initialize(&B);
    creat(&A);
    creat(&B);
    multiply(A,B,&C);
    save(C);
    return 0;
}

#include"stdio.h"
#include"stdlib.h"
struct node{
int data;
struct node *next;
}*p,*n,*s,*head,*current;
void creat()
{
    head = NULL;
    int x;
    scanf("%d",&x);
    while(x != -1)
    {
        current = (struct node*)malloc(sizeof(struct node));
        current->data = x;
        current->next = head;
        head = current;
        scanf("%d",&x);
    }
}
void print()
{
    while(current != NULL)
    {
        printf("%d ",current->data);
        current = current->next;
    }
}
void reverse()
{
    p = NULL;
    while(head != NULL)
    {
        n = head->next;
        head->next = p;
        p = head;
        head = n;
    }
}
void print_reverse()
{
    s = p;
   while(s != NULL)
    {
        printf("%d ",s->data);
        s = s->next;
    }
 }
 int main()
{
    creat();
    print();
    reverse();
    printf("\n");
    print_reverse();
   return 0;
}

#include"stdio.h"
#include"stdlib.h"
# include"string.h"
#include"windows.h"
typedef struct Student{
    char name[20];
    int id;
    int age;
    int gender; //0male1female
    int dorm;
    int bed;
    struct Student *next;
}Stu;
int n,in,del,stu_id;
Stu *head,*p,*r,*c,*new,*ne;
int sw;
void input()
{
    if(n <= 0)
        return NULL;
    else
    {
        for(int i = 0;i < n;i ++)
        {
            int n,a,g,d,b;
            printf("Please input student information:\n name:\n id:\n age:\n gender:\n dorm:\n bed:\n");
            c = (Stu *)malloc(sizeof(Stu));
            scanf("%s%d%d%d%d%d",&c->name,&c->id,&c->age,&c->gender,&c->dorm,&c->bed);
            c->next = NULL;
            if(i == 0)
            {
                head = c;
                r = c;
            }
            else
            {
                r->next = c;
                r = c;
            }
        }
    }
}
//输出
void output()
{
    p = head;
    if(head == NULL)
        {
            printf("No student information\n");
            exit(0);
        }
    else
    {
        printf("student information:\n");
        while(p != NULL)
        {

            printf("   name:%s\n     id:%d\n    age:%d\n gender:%c\n   dorm:%d\n    bed:%d\n",p->name,p->id,p->age,p->gender,p->dorm,p->bed);
            p = p->next;
        }
    }
}
//查询
void query()
{
    system("cls");
    p = head;
    if(p = NULL)
        {
            printf("No student information\n");
            exit(0);
        }
    else
    {
        printf("please enter student id:\n");
        scanf("%d",stu_id);
        while(p->id != stu_id)
        {
            p = p->next;
        }
        printf("%s\n %d\n %d\n %c\n %d\n %d\n",p->name,p->id,p->age,p->gender,p->dorm,p->bed);
    }
}
//插入
void insert()
{
    p = head;
    new = (Stu *)malloc(sizeof(Stu));
    printf("Please input the new student information:\n name:\n id:\n age:\n gender:\n dorm:\n bed:\n");
    scanf("%s%d%d%d%d%d",&c->name,&c->id,&c->age,&c->gender,&c->dorm,&c->bed);
    printf("Please enter the location you want to insert:");
    scanf("%d",&in);
    if(in == 1)
    {
        new ->next = p;
        head = new;
    }
    else
    {
        for(int j = 1;j < in - 1 && p;j ++)
            p = p->next;
        new->next = p->next;
        p->next = new;
    }
}
//删除
void delete()
{
    printf("Please enter the location you want to delete:");
    scanf("%d",&del);
    p = head;
    if(del == 1)
        {
            ne = p;
            head = p->next;
            free(ne);
        }
    else
    {
        for(int j = 1;j < del -1 && p;j ++)
            p = p->next;
        ne = p->next;
        p->next = ne->next;
        free(ne);
    }
}
//存储
void read_and_write()
{
    p = head;
    FILE *fp;
    fp=fopen("数据.txt","a+");
    if(fp == NULL)
    {
        while(p != NULL)
        {
            fprintf("name:%s\n id:%d\n age:%d\n gender:%c\n dorm:%d\n bed:%d\n",p->name,p->id,p->age,p->gender,p->dorm,p->bed);
            p = p->next;
        }
        fflush(fp);
        fclose(fp);
    }
    else
    {
        printf("student information:\n name:%d\n id:%d\n age:%d\n gender:%d\n dorm:%d\n bed:%d\n");
        char name[20];
        int id;
        int age;
        int gender;
        int dorm;
        int bed;
        fp=fopen("数据.txt","r+");
        while(fscanf("student information:\n name:%d\n id:%d\n age:%d\n gender:%d\n dorm:%d\n bed:%d\n",&name,&id,&age,&gender,&dorm,&bed))
        {
            printf("   name:%s\n     id:%d\n    age:%d\n gender:%c\n   dorm:%d\n    bed:%d\n",name,id,age,gender,dorm,bed);
        }
    }
}
int main()
{
    while(1)
    {
        printf("1添加2显示3查询4插入5删除6存储7退出");
        scanf("%d",&sw);
        if(sw != 0)
        switch(sw)
        {
        case 1:
            system("cls");
            printf("please enter the number you want\n");
            scanf("%d",&n);
            if(n <= 0)
            {
                 printf("please enter right number");
                Sleep(600);
                break;
            }
            else
            {
                input();
                break;
            }
        case 2:
            system("cls");
            output();
            system("pause");
            break;
        case 3:
            system("cls");
            query();
            break;
        case 4:
            system("cls");
            insert();
            break;
        case 5:
            system("cls");
            delete();
            break;
        case 6:
            system("cls");
            read_and_write();
            break;
        case 7:
            system("cls");
            printf("seeya\n\n");
            exit(0);
        }
    }
    return 0;
}
wmpscc commented 5 years ago

点评: