일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- .cu
- GPU
- Multithreading
- vi 여러 창
- gpgpu sim
- Process
- vi
- 소비자 생산자
- C
- ubuntu
- CUDA
- Producer consumer
- 운영체제
- gpgpu-sim
- tabedit
- Unified Memory
- nvidia
- OS
- parallel programming
- unix
- cdua
- CSAPP
- multiprocessor
- vim
- thread
- peer-to-peer access
- GPGPU
- Virtual Address
- Linux
- ostep
Archives
- Today
- Total
하루의 대학원 도전기
[OSTEP] wzip.c 본문
728x90
ostep-projects중 initial-utilities/wzip의 해답 코드입니다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#define SIZE 15000
typedef struct _value
{
int num;
char alpha;
}value;
int main(int argc, char** argv)
{
if(argc == 1)
{
printf("wzip: file1 [file2 ...]\n");
return 1;
}
FILE* fp;
char input[SIZE];
char buffer[SIZE];
size_t len=0;
ssize_t nread;
value arr[SIZE];
int f=0; // for arr
for(int t=0;t<argc-1;t++)
{
char * line = NULL;
fp=fopen(argv[t+1],"r");
while((nread=getline(&line,&len,fp)!=-1 ))
{
fclose(fp);
//strcpy(input,line);
//free(line);
int cnt=0;
int j=0;
memset(buffer,0,sizeof(buffer));
int temp=0;
while(1)
{
if(line[j] == '\n')
{
arr[f].num = temp;
arr[f].alpha = buffer[0];
f++;
arr[f].num = 1;
arr[f].alpha = '\n';
f ++; j=0; cnt=0; temp=0;
break;
}
if(j==strlen(line)-1)
{
if(line[j]==buffer[cnt-1])
{
buffer[cnt] = line[j];
cnt++; j++; temp++;
}
arr[f].num = temp;
arr[f].alpha = buffer[0];
f++;
cnt=0; j=0; temp=0;
break;
}
if(cnt==0)
{
buffer[cnt] = line[j];
cnt++; j++; temp++;
}
else
{
if(line[j]==buffer[cnt-1])
{
buffer[cnt] = line[j];
cnt++; j++; temp++;
}
else
{
cnt=0;
arr[f].num = temp;
arr[f].alpha = buffer[0];
f++;
memset(buffer,0,sizeof(buffer));
temp=0;
}
}
} // end of while
} // end of while (nread)
free(line);
}//end of for
int sum=0;
int i=0;
while(1)
{
if(sum==0)
{
if(i==f-1)
{
sum+= arr[i].num;
fwrite(&sum,sizeof(int),1,stdout);
fwrite(&arr[i].alpha,sizeof(char),1,stdout);
break;
}
else
{
sum += arr[i].num;
i++;
}
}
else // if sum is not zero
{
if(i==f-1)
{
if ( arr[i].alpha == arr[i-1].alpha )
{
sum += arr[i].num;
fwrite(&sum,sizeof(int),1,stdout);
fwrite(&arr[i].alpha,sizeof(char),1,stdout);
break;
}
else
{
fwrite(&sum,sizeof(int),1,stdout);
fwrite(&arr[i-1].alpha,sizeof(char),1,stdout);
fwrite(&arr[i].num,sizeof(int),1,stdout);
fwrite(&arr[i].alpha,sizeof(char),1,stdout);
break;
}
}
if(arr[i].alpha == arr[i-1].alpha)
{
sum += arr[i].num;
i++;
}
else if(arr[i].alpha != arr[i-1].alpha)
{
fwrite(&sum, sizeof(int),1,stdout);
fwrite(&(arr[i-1].alpha),sizeof(char),1,stdout);
sum=0;
}
}
} // end of while
return 0;
}
|
cs |
<wzip.c>
728x90
'프로그래밍 > C' 카테고리의 다른 글
Makefile 기초 (0) | 2021.11.25 |
---|---|
[C] goto. (0) | 2021.10.06 |