Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

闫晋梁(矩阵相乘) #24

Open
mythologicalwarrior opened this issue Nov 28, 2018 · 1 comment
Open

闫晋梁(矩阵相乘) #24

mythologicalwarrior opened this issue Nov 28, 2018 · 1 comment
Labels
修改 代码出现问题,请修改后重新提交代码

Comments

@mythologicalwarrior
Copy link

#include<stdio.h>
#include<stdlib.h>
#include<memory.h>
struct lian
{
	int num;
	struct lian *next;
};

struct lian *hcreat(int n)
{
	int i=1;
	struct lian*hang=NULL,*cut,*end;
	cut=end=(struct lian*)malloc(sizeof(struct lian));
	scanf_s("%d",&cut->num);
	while(cut->num!=999)
	{
		i++;
		if(hang==NULL)
			hang=cut;
		else
		{
			end->next=cut;
			end=cut;
			end=cut=(struct lian*)malloc(sizeof(struct lian));
			scanf_s("%d",&cut->num);
		}
	}
	end->next=NULL;
	return(hang);
}

struct lian *lcreat(int n)
{
	struct lian *lie=NULL,*cut,*end;
	int i=1;
	cut=end=(struct lian*)malloc(sizeof(struct lian));
	scanf_s("%d",&cut->num);
	while(cut->num!=999)
	{
		i++;
		if(lie==NULL)
			lie=cut;
		else
		{
			end->next=cut;
			end=cut;
			end=cut=(struct lian*)malloc(sizeof(struct lian));
			scanf_s("%d",&cut->num);
		}
	}
	return(lie);
}

struct lian *chen(struct lian*hang,struct lian*lie,int n)
{
	int i=0,s=0;
	struct lian*zui=NULL,*cut,*end;
	while(hang!=NULL)
	{
		i++;
		s+=hang->num*lie->num;
		if(i%n==0)
		{
			end=cut=(struct lian*)malloc(sizeof(struct lian));
			memset(cut,0,sizeof(struct lian));
			cut->num=s;
			if(zui==NULL)
				zui=cut;
			else
			{
				end->next=cut;
				end=cut;
			}
			s=0;
		}
	}
	end->next=NULL;
	return(zui);
}

void print(struct lian *zui,int n)
{
	struct lian *a;
	int i=1;
	a=zui;
	if(a==NULL)
	{
		printf("这是空链表");
		exit(1);
	}
	else
		while(a!=NULL)
		{
			printf("%d",a->num);
			a=a->next;
			if(i%n==0)
				printf("\n");
			i++;
		}
}

int main()
{
	struct lian *hang,*lie,*zui;
	hang=lie=zui=(struct lian*)malloc(sizeof(struct lian));
	int n;
	printf("请输入矩阵的阶数:\n");
	scanf_s("%d",&n);
	printf("请输入第一个矩阵:\n");
	hang=hcreat(n);
	hang->next=NULL;
	printf("请输入第二个矩阵:\n");
	lie=lcreat(n);
	lie->next=NULL;
	zui=chen(hang,lie,n);
	zui->next=NULL;
	printf("最后结果为:\n");
	print(zui,n);
	return 0;
}
@wmpscc
Copy link
Member

wmpscc commented Dec 7, 2018

点评:

  • 代码计算乘法操作时出现异常,未能输出答案,请修改(计算3阶矩阵时出现异常)
0x0F6D5290 (vcruntime140d.dll) (Project1.exe 中)处有未经处理的异常: 0xC0000005: 写入位置 0x00000000 时发生访问冲突。

输入:

请输入矩阵的阶数:
3
请输入第一个矩阵:
1 2 3
1 2 3
1 2 3
999
请输入第二个矩阵:
1 2 3
4 5 6
7 8 9
999

修改!

@wmpscc wmpscc added the 修改 代码出现问题,请修改后重新提交代码 label Dec 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
修改 代码出现问题,请修改后重新提交代码
Projects
None yet
Development

No branches or pull requests

2 participants