http://acm.hdu.edu.cn/showproblem.php?pid=1088 這道題 讓我對 cin 和 scanf 有更多的了解 剛開始用gets 讀入 但是由于不好控制 讀入下個單詞時 這行是否 會超過80 個字符 包括空格;
而使用 scanf 這個對換行符 處理不是很好 ,于是就用了 cin 很好的以空格和換行符讀入單詞 ,這個題貢獻了很多次 表達錯誤 ,
#include<iostream>
using namespace std;

# include<stdio.h>
# include<stdlib.h>
# include<string.h>
int main()


{
//freopen("myout.txt","w",stdout);
char str[100],str1[20];
int i,j,len,count = 0,flag = 1;//flag 用來防止 兩個hr 而出現中間空行的現象
while (cin>>str)

{
if (strcmp(str,"")==0)
continue;
len = strlen(str);

if (strcmp(str,"<br>")==0)
printf("\n"),count =0;
else if (strcmp(str,"<hr>")==0)

{
if (count)printf("\n");
for (j = 0;j<80;j++)
printf("-");
printf("\n");
count = 0;
flag = 1;
}
else

{
if (count + len +1 <= 80) //因為要加一個空格

{
if (count )
printf(" "),count ++;
printf("%s",str);
count += len;
}
else

{
printf("\n"),count = len,printf("%s",str);
}
}
}
printf("\n");

}


posted on 2010-04-15 13:02
付翔 閱讀(268)
評論(0) 編輯 收藏 引用