One of you EXPERT C PROGRAMMERS rewrite this
1
Name:
Anonymous
2010-11-01 5:47
I'm reading K&R ; doing the opposite of exercise 1-16. This really shouldn't take that much code, should it?
// Write a program to print all input lines that are shorter than 80 characters.
#include <stdio.h>
#define LINEMAX 80
int main()
{
int c, i = 0, j, thisstr[LINEMAX];
while ((c = getchar()) != EOF) {
// end of a line?
if (c == '\n') {
// if line short enough, print the line
if (i < LINEMAX) {
for (j = 0; j < i; j++)
putchar(thisstr[j]);
putchar('\n');
}
// and clear the variables for the next one
for (i = 0; i < LINEMAX; i++)
thisstr[i] = 0;
i = 0;
} else if (i < LINEMAX) {
// store character in array
thisstr[i] = c;
i++;
}
}
return 0;
}
Feel free to correct the shit out of me.
P.S. ((c = getchar()) != EOF) is PIG DISGUSTING
2
Name:
Anonymous
2010-11-01 5:56
Here's some enterprise level code
#include <stdio.h>
int main(void)
{
char s[1024];
while(!feof(stdin))
{
t = fgets(s,1024,stdin);
if(t < 80)
puts(s);
else if(t==1024)
{
while(t==1024)
t = fgets(s,1024,stdin);
fgets(s,1024,stdin);
}
}
}
3
Name:
Anonymous
2010-11-01 6:17
I'm doing the opposite of all the exercises, too.
That is, I'm not doing any of them.
4
Name:
Anonymous
2010-11-01 6:55
>>2
Please, that's littered with magic numbers. And what is
t?
This is
ENTERPRISE level code:
[code]
#include <stdio.h>
#include <string.h>
#define MAXLINE 80
int main()
{
char line[MAXLINE + 2];
size_t len;
memset(line, 0, sizeof line);
while (fgets(line, MAXLINE + 1, stdin)) {
len = strlen(line);
if (len <= MAXLINE) {
fputs(line, stdout);
}
}
exit(0);
}
5
Name:
Anonymous
2010-11-01 6:58
main = interact $ unlines . filter ((< 80) . length) . lines
6
Name:
Anonymous
2010-11-01 8:18
>>5
First time Haskell actually amazes me.
7
Name:
Anonymous
2010-11-01 8:21
>>5-6
???????????????????????????? ???????????????????????????????????????? ???????????? ???????????????????????????????????????????????? ???????????????????????????? ???????? ???????????????????????????????????????????????? ???????????????????????????????????????????????? ????????????
????????????????????????????????????? ???????????????????????????????????????????????????? ???????? ???????????? ???????????????? ???????? ???????????????????????????????????????????? ???????????????????????????????? ????????????????????????????????,
???????????????????????????????????????? ???????????????????????? ???????????????????? ???????? ???????????? ????????????????????????????????, ???? ???????????????????????????? ???????????????????????????????????????????? ???????? ????????????????????????????, ????????????????????,
????????????????-???????????????????????????? ???????????????????????? ???????????????? ???????????????? ???????????????????????? ???????????????????????????????????????? ???????????????????????????????????????? ???????????????????????????????????????????????????? ???????? ????????????????
???????? ???????????????????????????? ???????????? ???????????????????????? ???????????? ???????????????????????????????????????????????? ???????????????????????????? ???????????? ???????????? ????????????.
???????? ???????? ???? ???????????????? ???????? ???????????????????????? ????????????, ???????????????????????????? ???????? ???????????????????????? ???????????????????? ???????????? ???????????????????????????? ???????????? ???????? ???????????????????????? ????????????
???????????????????????????????????? ???????????????????? ???????? ???????????? ????????????????????????????????.
8
Name:
Anonymous
2010-11-01 8:51
>>7
Shut up,
???????????????????? ???????????????????? ????????????????????????
.
9
Name:
Anonymous
2010-11-01 9:00
>>6
If you find that amazing, you should see what it can really do.
It smells terrible!
10
Name:
Anonymous
2010-11-01 9:42
#include <stdio.h>
int main(void)
{
char s[81];
while (s[0] = s[79] = 0, scanf("%80[^\n]%*[^\n]", s) >= 0) {
if (!s[79])
printf("%s\n", s);
getchar();
}
return 0;
}
11
Name:
Anonymous
2010-11-01 10:24
grep { .chars < 80 }, slurp();
12
Name:
Anonymous
2010-11-01 10:31
grep -vE .{80}
13
Name:
Anonymous
2010-11-01 16:10
ENTERPRISE C
// Write a program to print all input lines that are shorter than 80 characters.
#include <stdio.h>
#define LINEMAX 80
int main()
{
int c, i = 0, j, s[LINEMAX];
start:
// end of a line?
if (c == '\n') {
// if line short enough, print the line
if (i < LINEMAX) {
j=0;
loop1:
putchar(s[j]);
j++;
if(j<i)
goto loop1;
putchar('\n');
}
// and clear the variables for the next one
i=0;
loop2:
s[i] = 0;
i++;
if(i<LINEMAX)
goto loop2;
i = 0;
} else if (i < LINEMAX) {
// store character in array
s[i] = c;
i++;
}
if((c=getchar())!=EOF)
goto start;
end:
return 0;
}
14
Name:
Anonymous
2010-11-01 16:32
>>11
slurp() takes a param. Did you mean:
lines.grep: *.chars < 80?
15
Name:
Anonymous
2010-11-01 17:44
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
char *s;
size_t len;
int ret = 1;
if (argc != 2)
goto E1;
len = 4 + strlen(argv[1]) + 11 + 80 + 1 + 1;
if (!(s = malloc(len))) {
perror("malloc()");
goto E1;
}
if (snprintf(s, len, "cat %s| gr"
"ep -v \".."
".........."
".........."
".........."
".........."
".........."
".........."
".........."
"........\"", argv[1]) < 0) {
perror("snprintf()");
goto E2;
}
if (system(s) == -1) {
perror("system()");
goto E2;
}
ret = 0;
E2: free(s);
E1: return ret;
}
tuwien
16
Name:
Anonymous
2010-11-02 13:41
17
Name:
Anonymous
2010-11-02 14:19
>>2
else if(t==1024)
{
while(t==1024)
t = fgets(s,1024,stdin);
fgets(s,1024,stdin);
}
This is the most idiotic thing I've read in a while. It doesn't even work! Maybe you meant
while(t==1024)
t = fgets(s,1024,stdin);
?
18
Name:
Anonymous
2010-11-02 15:50
19
Name:
Anonymous
2010-11-02 17:30
20
Name:
Anonymous
2010-11-02 20:49
section .bss
b resb 80
section .text
global _start
puts:
xor rax, rax
inc rax
mov rdi, rax
mov rsi, b
mov rdx, rbx
syscall
ret
gets:
xor rax, rax
mov rdi, rax
mov rsi, b
mov rdx, 0x50
syscall
cmp rax, 0
je fin
jl bail
mov rbx, rax
ret
bail:
mov rdx, 1
jmp exit
fin:
mov rdx, 0
exit:
mov rax, 60
syscall
hlt
_start:
call gets
cmp rbx, 80
jl ok
mov rdi, b
mov rax, 10
mov rcx, 0x50
cld
repne scasb
cmp rcx, 0
jle nextnl
ok: call puts
jmp _start
nextnl: mov rsi, b
mov rdi, 0
mov rdx, 1
xor rax, rax
syscall
cmp rax, 0
je fin
jl bail
cmp byte [b], 0xA
je _start
jmp nextnl
Only really works with terminal i/o since that waits for a newline, with pipes it might just read a small bit, print it, and continue. I'd make it robust
cc -S >>13 .c , but i doubt anyone on
/gorp/ would care.
21
Name:
Anonymous
2010-11-02 21:21
22
Name:
Anonymous
2010-11-03 7:47
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
while(getline(cin, str)){
if(str.length() < 80)
cour << str;
}
}
Requesting ENTERPRISE Brainfuck, J and APL. I'm lazy for that shit.
23
Name:
Anonymous
2010-11-03 8:09
>>22
I have a boring course in the morning, I might bother to provide a brainfuck implementation.
24
Name:
Anonymous
2010-11-03 8:29
C/C++ faggots like to work hard
LINEMAX = 80
import sys
for i in sys.stdin:
if len(i) < LINEMAX:
print i,
25
Name:
Anonymous
2010-11-03 11:22
26
Name:
Anonymous
2010-11-03 12:41
>>25
Thou hast been trolleth.
27
Name:
Anonymous
2010-11-03 12:57
C/C#
28
Name:
Anonymous
2010-11-03 13:11
29
Name:
Anonymous
2010-11-03 13:25
PHP/Perl
30
Name:
Anonymous
2010-11-03 13:32
FIOC/Boo
31
Name:
Anonymous
2010-11-03 16:26
v
>$ v
| :<
>0>~:52*=!|\1+:852**`|
|! :<
>, ^
32
Name:
Anonymous
2010-11-04 15:50
while True:
s = raw_input("> ");
if len(s) < 80:
print s
33
Name:
Anonymous
2010-11-04 15:50
>>32
The semicolon is for
aesthetics
34
Name:
Anonymous
2010-11-04 16:54
import sys
filter(lambda s:len(s)<80,sys.read().split('\n'))
inb4 15-character Perl implementation
35
Name:
Anonymous
2010-11-04 17:05
36
Name:
Anonymous
2010-11-04 17:13
>>35
Right. Never mind, that should teach me about posting without reading the thread.
37
Name:
Anonymous
2010-11-04 18:33
n/{,80<},n*
38
Name:
Anonymous
2010-11-05 23:44
>>34
you forgot to print out the result of filter
Newer Posts