android-nuc / 18-C-Train

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

兰森考核作业 #37

Closed 9Dy8um closed 5 years ago

9Dy8um commented 5 years ago

矩阵乘法

#include<stdio.h> 
#include<stdlib.h>
const int ORDER = 1000;
struct matrix
{
    int a; 
    struct matrix *RLink,*DLink; 
};
typedef struct matrix MAT;
MAT *creat(void)
{
    int m=2,n=2;
    MAT *x_1,*x_2,*y_1,*y_2,*z;
    MAT *head_1=NULL;
    MAT *head_2=NULL;
    y_1=y_2=x_1=x_2=(MAT *)malloc(sizeof(MAT)); 
    scanf("%d",&y_1->a); 
    while(m<=ORDER)
     {
        if(m==2)
        {    
            head_1=y_1;
        }
        else
        {
            y_2->DLink=y_1;
        }
        y_2=y_1;
        y_1=(MAT *)malloc(sizeof(MAT));
        scanf("%d",&y_1->a);
        m=m+1;
     }
     y_2->DLink=y_1;
     y_1->DLink=NULL;
    while(m<=ORDER)
     {
        if(m==2)
        {    
            head_2=x_1;
        }
        else
        {
            x_2->DLink=x_1;
        }
        x_2=x_1;
        x_1=(MAT *)malloc(sizeof(MAT));
        scanf("%d",&x_1->a);
        m=m+1;
     }
     x_2->DLink=x_1;
     x_1->DLink=NULL;

    while(m<=ORDER)
    {
        z=head_2;
        x_1=x_2=head_2=head_2->DLink;
        while(n<=ORDER)
        {
            x_1=(MAT *)malloc(sizeof(MAT));
            z=z->RLink;
            scanf("%d",&x_1->a);
            z->DLink=x_2->RLink=x_1;
            x_2=x_1;
            n=n+1;
        }
        x_2->RLink=x_1;
        x_1->RLink=NULL;
        m=m+1;

    }
    x_2=head_2;
    while(m<ORDER)
    {
        x_2=x_2->RLink;
        x_2->DLink=NULL;
        m=m+1;
    }
    return head_1;
}
MAT *creatk(void)
{
    int m=2,n=2;
    MAT *x_1,*x_2,*y_1,*y_2,*z;
    MAT *head_1=NULL;
    MAT *head_2=NULL;
    y_1=y_2=x_1=x_2=(MAT *)malloc(sizeof(MAT)); 
    y_1->a=0; 
    while(m<=ORDER)
     {
        if(m==2)
        {    
            head_1=y_1;
        }
        else
        {
            y_2->DLink=y_1;
        }
        y_2=y_1;
        y_1=(MAT *)malloc(sizeof(MAT));
        y_1->a=0;
        m=m+1;
     }
     y_2->DLink=y_1;
     y_1->DLink=NULL;
    while(m<=ORDER)
     {
        if(m==2)
        {    
            head_2=x_1;
        }
        else
        {
            x_2->DLink=x_1;
        }
        x_2=x_1;
        x_1=(MAT *)malloc(sizeof(MAT));
        y_1->a=0;
        m=m+1;
     }
     x_2->DLink=x_1;
     x_1->DLink=NULL;

    while(m<=ORDER)
    {
        z=head_2;
        x_1=x_2=head_2=head_2->DLink;
        while(n<=ORDER)
        {
            x_1=(MAT *)malloc(sizeof(MAT));
            z=z->RLink;
            x_1->a=0;
            z->DLink=x_2->RLink=x_1;
            x_2=x_1;
            n=n+1;
        }
        x_2->RLink=x_1;
        x_1->RLink=NULL;
        m=m+1;

    }
    x_2=head_2;
    while(m<ORDER)
    {
        x_2=x_2->RLink;
        x_2->DLink=NULL;
        m=m+1;
    }
    return head_1;
}

void print(MAT *head_1) 
{
    int t=0;
    MAT *pointer,*head_2;
    head_2=pointer=head_1;

    printf("\n");
    if(head_1!=NULL)
    {
     while(head_2!=NULL)
     {
        while(pointer!=NULL)
        {
            printf("%d\t",pointer->a);
            pointer=pointer->RLink;
            t++;
            if(t%ORDER==0) printf("\n");
        }
        pointer=head_2=head_2->DLink;
     }

    }
}

MAT *mul(MAT *X1,MAT *X2,MAT *X3) 
{
    int o,m,n,s;
    MAT *head_2,*x_1,*ch,*y_1,*K1,*K2,*KH;
    head_2=x_1=X1;
    ch=y_1=X2;
    K1=K2=KH=X3;
    while(o<ORDER)
    {
        while(m<ORDER)
        {
            s=0;
            while(n<ORDER)  
            {
                s=s+x_1->a*y_1->a;
                x_1=x_1->RLink;
                y_1=y_1->DLink;
                n=n+1;
            }
            K2->a=s;
            K2=K2->RLink;
            y_1=ch=ch->RLink;
            x_1=head_2;
            m=m+1;
        }
        x_1=head_2=head_2->DLink;
        K2=K1=K1->DLink;
        y_1=ch=X2;
        o=o+1;
    }
    return KH;
}
int main(void)
{
    MAT *P1,*P2,*P3;
    printf("矩阵1\n");
    print(P1=creat());
    printf("矩阵2\n");
    print(P2=creat());
    P3=creatk();
    printf("乘积\n");
    print(mul(P1,P2,P3));
    getchar();
    return 0;
}

链表倒序

#include <stdio.h>
#include <stdlib.h>
#define N 10

struct Data{
int num;
struct Data *next;
};

int main ( int argc , char * argv[])
{
int data,i;
struct Data *head,*p;
head = (struct Data *)malloc (sizeof(struct Data));
head->next = NULL;
printf("输入10个数字\n"); 
for ( i = 0; i < N; i++)
{

 scanf ("%d",&data);
p = ( struct Data *)malloc(sizeof(struct Data));
p->num = data;
p->next = head->next;
head->next = p;

}

printf("倒序:");
while (p!=NULL)
{
printf("%d\t",p->num);
p=p->next;
}
printf("\n");
return 0;
}
wmpscc commented 5 years ago