알려주세요 제발 인풋을 읽질못함
아리솔
질문 제목 : 알려주세요 제발 인풋을 읽질못함인풋파일에 빈줄이 없으면 되는데.... 빈줄이 잇으면 아웃풋이 에러가 나네요;;;
질문 내용 :
빈줄을 어떻게 읽을까요? ㄷㄷ
#include iostream
using std::cout;
using std::endl;
using std::cin;
#include fstream
using std::ifstream;
#include iomanip
using std::setprecision;
using std::ios;
#include cmath
#include cstring
#include cstdlib
const int max_chars_per_line = 200;
const int max_tokens_per_line = 4;
const char* const delimiter = ;
struct square
{
double side;
};
struct rectangle
{
double length;
double width;
};
struct circle
{
double radius;
};
struct cube
{
double side;
};
struct prism
{
double length;
double width;
double height;
};
struct cylinder
{
double radius;
double height;
};
void printsquare(square*);
void printrectangle(rectangle*);
void printcircle(circle*);
void printcube(cube*);
void printprism(prism*);
void printcylinder(cylinder*);
void printerror(char* str)
{
cout str invalid object endl;
}
int main()
{
void* shapes[100]={};
int shapeid[100]={};
int i;
ifstream fin;
fin.open(input.txt);
if (!fin.good())
return 1;
coutprogrammer: jaeyoung paik\n;
coutdescription: this program calculate shape \n;
cout(square,rectangle,circle,cube,prism,cylinder) \n;
coutusing the input data \n;
coutendl;
int n=0;
int count = 0;
while (!fin.eof())
{
char buf[max_chars_per_line];
fin.getline(buf, max_chars_per_line);
const char* token[max_tokens_per_line] = {0};
token[0] = strtok(buf, delimiter);
if (token[0])
{
for (n = 1; n max_tokens_per_line; n++)
{
token[n] = strtok(0, delimiter);
if (!token[n]) break;
}
}
if (!((strcmp(token[0],square) == 0) || (strcmp(token[0],rectangle) == 0) || (strcmp(token[0],cube) == 0) || (strcmp(token[0],circle) == 0) || (strcmp(token[0],prism) == 0) || (strcmp(token[0],cylinder) == 0)))
{
char str[20];
strcpy(str,token[0]);
shapes[count] = str;
shapeid[count] = 0;
}
else if(strcmp(token[0], square)==0)
{
if (n == 1)
{
token[1] = 0;
}
square* s = new square;
s-side = token[1] == 0? 0.0 : atof(token[1]);
shapes[count] = s;
shapeid[count] = 1;
}
else if(strcmp(token[0], rectangle) == 0)
{
if (n == 1)
{
token[1] = 0;
token[2] = 0;
}
if (n == 2)
{
token[2] = 0;
}
rectangle* r = new rectangle;
r-length = token[1] == 0? 0.0 : atof(token[1]);
r-width = token[2] == 0? 0.0 : atof(token[2]);
shapes[count] = r;
shapeid[count] = 2;
}
else if(strcmp(token[0], circle) == 0)
{
if (n == 1)
{
token[1] = 0;
}
circle* c = new circle;
c-radius = token[1] == 0? 0.0 : atof(token[1]);
shapes[count] = c;
shapeid[count] = 3;
}
else if(strcmp(token[0], cube) == 0)
{
if (n == 1)
{
token[1] = 0;
}
cube* c = new cube;
c-side = token[1] == 0? 0.0 : atof(token[1]);
shapes[count] = c;
shapeid[count] = 4;
}
else if(strcmp(token[0], prism) == 0)
{
if (n == 1)
{
token[1] = 0;
token[2] = 0;
token[3] = 0;
}
if (n == 2)
{
token[2] = 0;
token[3] = 0;
}
if (n == 3)
{
token[3] = 0;
}
prism* p = new prism;
p-length = token[1] == 0? 0.0 : atof(token[1]);
p-width = token[2] == 0? 0.0 : atof(token[2]);
p-height = token[3] == 0? 0.0 : atof(token[3]);
shapes[count] = p;
shapeid[count] = 5;
}
else if(strcmp(token[0], cylinder) == 0)
{
if (n == 1)
{
token[1] = 0;
token[2] = 0;
}
if (n == 2)
{
token[2] = 0;
}
cylinder* c = new cylinder;
c-radius = token[1] == 0? 0.0 : atof(token[1]);
c-height = token[2] == 0? 0.0 : atof(token[2]);
shapes[count] = c;
shapeid[count] = 6;
}
count++;
}
fin.close();
for (i = 0; i count; i++)
{
if (shapeid[i] == 1)
printsquare((square*)shapes[i]);
else if (shapeid[i] == 2)
printrectangle((rectangle*)shapes[i]);
else if (shapeid[i] == 3)
printcircle((circle*)shapes[i]);
else if (shapeid[i] == 4)
printcube((cube*)shapes[i]);
else if (shapeid[i] == 5)
printprism((prism*)shapes[i]);
else if (shapeid[i] == 6)
printcylinder((cylinder*)shapes[i]);
else
printerror((char*)shapes[i]);
}
system(pause);
return 0;
}
void printsquare(square* s)
{
double area, perimeter;
area=pow(s-side, 2);
perimeter=4*s-side;
cout.unsetf(ios::fixed|ios::showpoint);
cout setprecision(6);
coutsquare side=s-side;
cout.setf(ios::fixed|ios::showpoint);
coutsetprecision(2) area=area perimeter=perimeterendl;
}
void printrectangle(rectangle* r)
{
double area, perimeter;
area=(r-length)*(r-width);
perimeter=2*(r-length)+2*(r-width);
cout.unsetf(ios::fixed|ios::showpoint);
cout setprecision(6);
coutrectangle length=(r-length) width=(r-width);
cout.setf(ios::fixed|ios::showpoint);
coutsetprecision(2) area=area perimeter=perimeterendl;
}
void printcircle (circle* r)
{
double area, circumference;
area=4*atan(1.0)*pow((r-radius), 2);
circumference=2*4*atan(1.0)*(r-radius);
cout.unsetf(ios::fixed|ios::showpoint);
cout setprecision(6);
coutcircle radius=(r-radius);
cout.setf(ios::fixed|ios::showpoint);
coutsetprecision(2) area=area circumference=circumferenceendl;
}
void printcube(cube* s)
{
double surface,volume;
volume=pow((s-side), 3);
surface=6*pow((s-side), 2);
cout.unsetf(ios::fixed|ios::showpoint);
cout setprecision(6);
coutcube side=(s-side);
cout.setf(ios::fixed|ios::showpoint);
coutsetprecision(2) surface area=surface volume=volumeendl;
}
void printprism(prism* p)
{
double surface,volume;
volume=(p-length)*(p-width)*(p-height);
surface=2*(p-length)*(p-width)+2*(p-width)*(p-height)+2*(p-height)*(p-length);
cout.unsetf(ios::fixed|ios::showpoint);
cout setprecision(6);
coutprism length=(p-length) width=(p-width) height=(p-height);
cout.setf(ios::fixed|ios::showpoint);
coutsetprecision(2) surface area=surface volume=volumeendl;
}
void printcylinder(cylinder* c)
{
double surface,volume;
volume=4*atan(1.0)*pow((c-radius), 2)*(c-height);
surface=2*4*atan(1.0)*pow((c-radius), 2)+2*4*atan(1.0)*(c-radius)*(c-height);
cout.unsetf(ios::fixed|ios::showpoint);
cout setprecision(6);
coutcylinder radius=(c-radius) height=(c-height);
cout.setf(ios::fixed|ios::showpoint);
coutsetprecision(2) surface area=surface volume=volumeendl;
}
input
square 14.5
rectangle 14.5 4.65
circle 14.5
cube 13
prism 1 2 3
spheres 2.4
cylinder 1.23
cylinder 50 1.23
triangle 1.2 3.2
저빈줄도 포함입니다.;;
-
큰돛
왜 C++언어 질문을 C언어 Q&A 게시판에 올리는지 이해가 안 되네요.
-
의사양반
\tif (token[0])
\t{
\t\tfor (n = 1; n MAX_TOKENS_PER_LINE; n++)
\t\t{
\t\t\ttoken[n] = strtok(0, DELIMITER);
\t\t\tif (!token[n]) break;
\t\t}
\t}
\telse
\t\tcontinue;
빈줄 확인은 strtok 반환값이 NULL 이 들어 옵니다. NULL 일땐 다시 while 문으로 고고싱 -
놓아주세요
\tif (token[0])
\t{
\t\tfor (n = 1; n MAX_TOKENS_PER_LINE; n++)
\t\t{
\t\t\ttoken[n] = strtok(0, DELIMITER);
\t\t\tif (!token[n]) break;
\t\t}
\t}
\telse
\t continue; -
아라
빈줄만 있는 것은 어떻게 구분하시나요?
해당 줄이 빈줄인지 여부를 체크하는 부분이 안보이는 것 같아서...
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2670009 | #if #endif 질문입니다. (5) | 새솔 | 2024-09-29 |
2669952 | 구구단질문 | 독특한 | 2024-09-28 |
2669926 | 인텔CPU 명령어들...질문좀... (1) | 널위해 | 2024-09-28 |
2669899 | 회문검사 오류 | 꽃님이 | 2024-09-28 |
2669870 | 색인값 찾기를 만들었는데 봐주세여 ㅠ (1) | 아빠몬 | 2024-09-27 |
2669843 | 저기요 ㅠㅠ 이게 헤더파일 로써 갖추지 못 한게 뭐죠 ? ? ? (7) | AngelsTears | 2024-09-27 |
2669815 | 자료구조 덱에 대해 궁금한게있어요 | 흙이랑 | 2024-09-27 |
2669768 | 파일관련에 관한 질문인데요 ㅠㅠ (1) | 화가마 | 2024-09-26 |
2669722 | if문기초에요 ㅠ 누구든지오셔서 알려주세요 ㅠ | 황소자리 | 2024-09-26 |
2669696 | add(&imsi,lenth);는 안되는이유! | 겨울 | 2024-09-26 |
2669635 | 문제좀 ..풀어주세요 ㅠㅠ (4) | 볼매 | 2024-09-25 |
2669608 | C언어를 배우고 이쓴데 /n이 말을 안듣네요;; (2) | 희나리 | 2024-09-25 |
2669581 | 다차원문자열배열과 입출력 (1) | 볼수록매력 | 2024-09-25 |
2669555 | 최대값 최소값 구하는 방법 알려주세요. (2) | 아연 | 2024-09-25 |
2669449 | float 와 double의 차이점 해석하는것좀 도와주세요~~ | 초롬 | 2024-09-24 |
2669426 | 퀵 정렬 소스에 대해서.. | 키클 | 2024-09-24 |
2669399 | %s로 문자열 호출 안하면 코드가 종료되는 이유 | 아이뻐 | 2024-09-23 |
2669342 | 랜덤발생기인데요 (3) | 카제 | 2024-09-23 |
2669309 | 소켓 통신 recv 할때 질문합니다. | 미투리 | 2024-09-23 |
2669264 | fscanf 도와주세요,... (3) | 볼수록매력 | 2024-09-22 |