출력된 화면을 파일저장
남은
질문 제목 : 출력된 화면을 파일저장
#include stdio.h
#include stdlib.h
#define max_vertex 50
#define false 0
#define true 1
typedef struct node *node_pointer;
typedef struct node {
int vertex;
node_pointer link;
}node;
typedef struct queue *queue_pointer;
typedef struct queue{
int vertex;
queue_pointer link;
}queue;
//graph정보
node_pointer graph[max_vertex];
int numofvertexes;
int numofedges;
int v=0;
//struct node **graph;
short int visited_dfs[max_vertex];
short int visited_bfs[max_vertex];void addq(queue_pointer *front, queue_pointer *rear, int); //큐 입력
int deleteq(); // 큐 삭제
void initgraph(int); //그래프의 초기화
void makegraph(); //삽입 삭제 선택
void insertedge(int vertex1, int vertex2); //노드 연결
void getedge(); //파일로부터 입력
void printgraph(); //그래프 출력
void deletevertexes(); //노드 삭제
void initvisited(); //visited 초기화
void dfs(int v); //깊이 우선 탐색
void bfs(int v); //너비 우선 탐색
//void visit(int);
void main()
{
initgraph(v);
makegraph();
}
//큐 삽입
void addq(queue_pointer *front, queue_pointer *rear, int item)
{
queue_pointer temp = (queue_pointer)malloc(sizeof (queue));
temp-vertex = item;
temp-link = null;
if(*front)
(*rear)-link = temp;
else
*front = temp;
*rear = temp;
}
//큐 삭제
int deleteq(queue_pointer *front)
{
queue_pointer temp = *front;
int item;
if(!(*front))
{
fprintf(stderr, the queue is empty\n);
exit(1);
}
item = temp-vertex;
*front = temp-link;
free(temp);
return item;
}
//노드의 연결
void insertedge(int vertex1, int vertex2)
{
node_pointer ptr = (node_pointer)malloc(sizeof(node));
ptr-vertex = vertex2;
ptr-link = null;
if (graph[vertex1] != null)
{
node_pointer next_ptr = null;
next_ptr = graph[vertex1];
while(next_ptr-link != null)
{
next_ptr = next_ptr-link;
}
if(next_ptr-vertex == ptr-vertex)
printf(중복된 값입니다!!\n);
else
next_ptr-link=ptr;
}
else graph[vertex1] = ptr;
}
//너비우선탐색
void bfs(int v)
{
queue_pointer front, rear;
node_pointer w;
front = rear = null;
printf(%5d, v);
visited_bfs[v] = true;
addq(&front, &rear,v);
while(front)
{
v = deleteq(&front);
for(w=graph[v]; w; w=w-link)
if(!visited_bfs[w-vertex])
{
printf(%5d, w-vertex);
addq(&front, &rear, w-vertex);
visited_bfs[w-vertex] = true;
}
}
}
//깊이우선탐색
void dfs(int v)
{
node_pointer w;
visited_dfs[v] = true;
printf(%5d, v);
for (w = graph[v]; w; w = w-link)
if (!visited_dfs[w-vertex])
dfs(w-vertex);
}
//그래프의 초기화
void initgraph(int v)
{
int i;
graph[max_vertex]= malloc(v*sizeof(node_pointer*));
for(i=0;iv;i++){
graph[i]=null;
}
}
//visited 초기화
void initvisited()
{
int i;
for (i=0; inumofvertexes; i++)
{
visited_bfs[i] = false;
visited_dfs[i] = false;
}
}
//그래프의 출력
void printgraph(int v)
{
int i=0;
node_pointer w;
printf(\n\n);
printf(## adjacency lists ## \n\n);
for(i=0; iv; i++)
{
if(graph[i])
{
printf(graph[%d] - , i);
for(w=graph[i]; w; w=w-link)
{
if(w-link)
printf(%d → , w-vertex);
else printf(%d \n, w-vertex);
}
}
}
printf(num of vertexes : %d, numofvertexes);
printf(num of edges : %d, numofedges/2);
}
//삽입, 출력, bfs, dfs 선택
void makegraph()
{
int n;
int state;
file *fp;
fp = fopen(result.txt, wt);
while(1)
{
printf(enter your choice\n);
fprintf(fp, enter your choice\n);
printf(1: insert, 2:delete, 3:print, 4:bsf, 5:dsf, 6:quit);
fprintf(fp, 1: insert, 2:delete, 3:print, 4:bsf, 5:dsf, 6:quit);
scanf(%d, &n);
switch(n)
{
case 1:
getedge();
printf(\n);
break;
case 2:
deletevertexes();
printf(\n);
break;
case 3:
printgraph(numofvertexes);
printf(\n);
break;
case 4:
initvisited();
bfs(v);
printf(\n);
break;
case 5:
initvisited();
dfs(v);
printf(\n);
&);
break;
case 6:
printf(\n);
exit(1);
default:
break;
}
}
state=fclose(fp);
if(state!=0)
printf(file close error\n);
}
//파일로부터 입력
void getedge()
{
int vertex1, vertex2;
file *f;
f=fopen(input.txt,rt);
fscanf(f, %d, &numofvertexes);
for(numofedges=0;;numofedges++)
{
if(feof(f))
break;
fscanf(f, %d, %d, &vertex1);
fscanf(f, %d, %d, &vertex2);
insertedge(vertex1, vertex2);
}
fclose(f);
}
void deletevertexes(node* start)
{
int i, delnum, vertex;
node* p;
node* pre;
pre = start;
p = p-link;
scanf(%d, &vertex);
while(pre!=null)
{
if((p-vertex)==delnum)
{
pre-link = p-link;
free(p);
p=p-link;
break;
}
else
{
pre = p;
p = p-link;
}
}
}
이런 그래프의 bfs dfs입니다만, 출력된 것은 모두 파일로 저장하고 싶습니다.
fprintf(fp, 1: insert, 2:delete, 3:print, 4:bsf, 5:dsf, 6:quit); 이렇게 하나씩 출력하는 방법은 아는데요.
혹시 일일히 printf 함수 찾아서써주는 것 말고 다른 방법이 있으면 알려주세요..
ps 혹시 이 소스자체가 잘못된게 있으면 지적 부탁드립니다.질문 내용 :
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2700530 | 전처리문 질문입니다. (1) | 아놀드 | 2025-07-05 |
2700510 | c언어를 어케하면 잘할수 있을까요.. | 연연두 | 2025-07-05 |
2700484 | 두 개가 차이가 뭔지 알려주세요...(소수 찾는 프로그램) (2) | 날위해 | 2025-07-05 |
2700426 | 인터넷 창 띄우는 질문이요 (1) | 정훈 | 2025-07-04 |
2700400 | 원넓이를 계산이요 ㅜㅜ | 천칭자리 | 2025-07-04 |
2700368 | if에 관해서 질문이요... | Orange | 2025-07-04 |
2700339 | 이거 결과값이 왜이런건지.. (4) | 그댸와나 | 2025-07-04 |
2700313 | 파일 읽어서 저장하는데 빈파일일 경우 문재가 발생하네요.. (2) | 크나 | 2025-07-03 |
2700287 | 구조체 동적할당 연습을 하는데 오류가 뜹니다...(해결) (3) | 아련나래 | 2025-07-03 |
2700264 | 문자와 숫자 동시에 입력??? | 글고운 | 2025-07-03 |
2700236 | txt파일로만 쓰고 읽게 하려면 어떻게 해야 하나요..?? (8) | 미국녀 | 2025-07-03 |
2700211 | 전위 연산자 (2) | 어른처럼 | 2025-07-02 |
2700183 | C에서 파일이름을 받고, 그 파일의 사이즈를 출력해줘야하는데 내용이 출력이 안되네요 ;ㅅ; | 피스케스 | 2025-07-02 |
2700150 | 꼭좀 도와주세요ㅠㅠㅠ | 호습다 | 2025-07-02 |
2700095 | 연산문제...질문... | 오빤테앵겨 | 2025-07-01 |
2700070 | while문 , 3의배수 출력하는 프로그램좀 짜주세욤. | 횃불 | 2025-07-01 |
2700041 | 초보인데요 ㅎ 배열안에 배열을 집어넣을수 있나요?? | 헛장사 | 2025-07-01 |
2700012 | 배열// (1) | 전갈자리 | 2025-07-01 |
2699895 | 무한루프에 빠집니다.!! 해결좀부탁드려요 (10) | 선아 | 2025-06-30 |
2699842 | 질문을 너무 많이 하네여.....죄송.... (2) | 해님꽃 | 2025-06-29 |