게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
[The C Programming Language] Ex 7-2
게시물ID : computer_44149짧은주소 복사하기
작성자 : Lazer
추천 : 2
조회수 : 247회
댓글수 : 0개
등록시간 : 2012/04/17 22:52:28
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

struct {
unsigned int is_hex : 1;
unsigned int is_oct : 1;
} flags;

#define STRSIZE 100

int getch();

main(int argc, char *argv[])
{
char *otp, str[STRSIZE], format[STRSIZE];
int c, strp = 0;

if ((otp = argv[1]) != NULL) {
if (*otp++ == '-') {
switch (*otp) {
case 'h':
case 'H':
flags.is_hex = 1;
break;
case 'o':
case 'O':
flags.is_oct = 1;
break;
}
}
}

while ((c = str[strp++] = getch()) != EOF)
if (c == '\n') {
str[strp] = '\0';
c = atoi(str);

if (flags.is_hex)
strcpy(format, "%0x\n");
else if (flags.is_oct)
strcpy(format, "%o\n");
else
strcpy(format, "%d\n");

printf(format, c);
str[strp = 0] = '\0';
}
}

#define BUFSIZE 100

char buf[BUFSIZE];
int bufp;

int getch()
{
return (bufp > 0) ? buf[--bufp] : getchar();
}

void ungetch(int c)
{
if (bufp >= BUFSIZE)
printf("%s", "buf is full.\n");
else
buf[bufp++] = c;
}
전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호