리스트를 이용한 덧셈 연산....
파도
질문 제목 : 실행은 되는데 에러가 나네요 ㅜ기본 행열 의 행열값 입력후에 a행렬의 항수를 입력받구 행 ,렬 , 값을 입력후에 b행렬 또한 같은방식으로 입력받습니다 . 아직 출력함수는 없습니다 ㅜ질문 내용 :
#include stdio.h
#include stdlib.h
typedef int element;// 요소 정의
typedef struct matrixnode{ // 행열의 정보를 담을 노드 구조체 정의
element value;
int row;
int col;
struct matrixnode * link;
}matrixnode;
typedef struct baseinfor{ // 행열의 행과 열을 저장할 구조체 정의
int rows;
int cols;
}baseinfor;
typedef struct headnode{ // 헤드노드 구조체 정의
int terms;
matrixnode * head;
matrixnode * tail;
}headnode;
baseinfor* inputrowscols() // 행열의 행과 열을 입력 받을 구조체생성과 입력 함수
{
baseinfor * p;
p=(baseinfor *)malloc(sizeof(baseinfor));
scanf(%d%d, &(p-rows), &(p-cols));
return p;
}
void init(headnode * p) // 헤드노드 초기화 함수
{
p-head=null;
p-tail=null;
p-terms=0;
}
void addnode(headnode * p) // 행열노드 추가 함수
{
matrixnode * tmp;
if((p-head==null)||(p-tail==null))
{
tmp = (matrixnode *)malloc(sizeof(matrixnode));
p-head=tmp; p-tail=tmp;
p-terms=1;
tmp-link=null;
}
else
{
tmp = (matrixnode *)malloc(sizeof(matrixnode));
p-tail-link=tmp;
p-tail=tmp;
p-terms++;
tmp-link=null;
}
}
void inputvalue(headnode * p, int terms) // 미리 항의수를 입력 받아 노드생성과 정보들을 입력하는 함수
{
int i;
for(i=0;iterms;i++)
{
addnode(p);
scanf(%d %d %d,&(p-tail-row),&(p-tail-col),&(p-tail-value));
}
}
int diccterms(headnode * p1, headnode * p2) // c 행열의 항수를 결정하기 위한 함수
{
int cterms;
matrixnode * a;
matrixnode * b;
cterms = p1-terms + p2-terms;
a=(p1-head);
b=(p2-head);
for(;a;a=a-link)
while(b)
{
if((a-row)==(b-row)&&(a-col)==(b-col))
{
cterms--;
b=b-link;
break;
}
else
b=b-link;
}
return cterms;
}
void inputcmatrix(headnode * p1, headnode * p2, headnode * p3) // c 노드에 a+b 의 행열값을 입력하는 함수
{
matrixnode * a;
matrixnode * b;
matrixnode * c;
a=(p1-head);
b=(p2-head);
c=(p3-head);
for(;a;a=a-link)
{
while(1)
{
if((a-row==b-row)&&(a-col)==(b-col))
{
c-value=a-value+b-value;
c-row=a-row; c-col=a-col;
c=c-link;
break;
}
else
{
c-value=a-value;
c-row=a-row; c-col=a-col;
c=c-link;
break;
}
}
}
for(;b;b=b-link)
{
while(1)
{
if((a-row==b-row)&&(a-col)==(b-col))
break;
else
{
c-value=b-value;
c-row=b-row; c-col=b-col;
c=c-link;
break;
}
}
}
}
void clearnode(headnode *p) // 노드들의 동적할당 해제를 위한 함수
{
matrixnode * a;
matrixnode * b;
while(b)
{
b=a=p-head;
/ p-head=p-head-link;
free(a);
b=b-link;
}
free(p);
}
int main()
{
int i;
headnode * a;
headnode * b;
headnode * c;
baseinfor * base;
a=(headnode *)malloc(sizeof(headnode));
b=(headnode *)malloc(sizeof(headnode));
c=(headnode *)malloc(sizeof(headnode));
init(a); init(b); init(c); // 헤드노드 a,b,c 초기화
base=inputrowscols(); // 기본 정보 입력
scanf(%d,&(a-terms)); // a행열의 항수 입력
inputvalue(a,a-terms); // b행열의 정보 입력
scanf(%d,&(b-terms)); // a행열의 항수 입력
inputvalue(b,b-terms); // b행열의 정보 입력
c-terms=diccterms(a, b); // c행열의 항수 결정
for(i=0;ic-terms;i++) // c행열의 노드 추가
addnode(c);
inputcmatrix(a, b, c); // 연산결과를 c행열에 저장
clearnode(a);
clearnode(b);
clearnode(c);
// 동적할당 해제
free(base);
// 동적할당 해제
return 0;
}
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2695766 | 달팽이 배열 어디서 틀렸는지 모르겠습니다ㅠㅠ | 연분홍 | 2025-05-23 |
2695738 | fopen과fclose질문~~ (5) | 희선 | 2025-05-23 |
2695707 | 3의 배수 나타내기. (2) | 수리 | 2025-05-23 |
2695626 | 피보나치수열 과제 때문에 질문 드립니다. (6) | 옆집언니 | 2025-05-22 |
2695595 | 포인트공부중입니다 int형에서 4=1 인가요? (3) | 족장 | 2025-05-22 |
2695567 | 드라이브 고유번호를 가져오는 함수 (2) | 초코맛사탕 | 2025-05-21 |
2695533 | 음수의 산술변환! 질문이요 ㅠㅠ... (4) | 꽃여름 | 2025-05-21 |
2695506 | 구조체 배열 이용 도서목록 출력 프로그램 (1) | 가을귀 | 2025-05-21 |
2695450 | c언어 함수 질문이요.... | 이슬비 | 2025-05-20 |
2695403 | VirtualAlloc함수 및 메모리 질문 | 크리에이터 | 2025-05-20 |
2695355 | c언어 for함수 | 미쿡 | 2025-05-19 |
2695327 | 안녕하세요 제가 이번에 좀 큰 프로그램을.. | 악당 | 2025-05-19 |
2695295 | mutex동기화의 thread기반 채팅 서버소스 질문입니다 | 그루터기 | 2025-05-19 |
2695270 | 질문이요..swap 관한겁니다..ㅠㅠ (3) | 콩알녀 | 2025-05-19 |
2695244 | 노땅초보궁금한게 하나 있는데요..반복문(while문)초보자질문 (6) | 큰꽃늘 | 2025-05-18 |
2695166 | do while 문 어떤것이잘못된건지 모르겠어요 (2) | 아이폰 | 2025-05-18 |
2695122 | 구조체에 대해 물어보고 싶은게 있습니다 ^^^.. (7) | 수련 | 2025-05-17 |
2695091 | txt 파일 입출력 후 2차 배열에 저장하기입니다. (3) | 헛장사 | 2025-05-17 |
2695063 | 수도요금 프로그램좀 짜주세요. | 시내 | 2025-05-17 |
2695033 | 답변좀요ㅠㅠ (1) | 비사벌 | 2025-05-16 |