http://acm.hdu.edu.cn/showproblem.php?pid=1088 這道題 讓我對(duì) cin 和 scanf 有更多的了解 剛開(kāi)始用gets 讀入 但是由于不好控制 讀入下個(gè)單詞時(shí) 這行是否 會(huì)超過(guò)80 個(gè)字符 包括空格;
而使用 scanf 這個(gè)對(duì)換行符 處理不是很好 ,于是就用了 cin 很好的以空格和換行符讀入單詞 ,這個(gè)題貢獻(xiàn)了很多次 表達(dá)錯(cuò)誤 ,
#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 用來(lái)防止 兩個(gè)hr 而出現(xiàn)中間空行的現(xiàn)象
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) //因?yàn)橐右粋€(gè)空格

{
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)
評(píng)論(0) 編輯 收藏 引用