栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

poj 3218 Alignments

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

poj 3218 Alignments

#include<iostream>#include<string>#include<queue>using namespace std;typedef queue<string> TEXT;const int LINEWIDTH=75;int LineWordLength(TEXT line){int length=0;while(!line.empty()){length+=line.front().length();line.pop();}return length;}TEXT GetLine(TEXT& paragraph){TEXT line;int totalLength=0,wordNum=0;while(!paragraph.empty() &&totalLength+paragraph.front().length()+wordNum<=LINEWIDTH){line.push(paragraph.front());totalLength+=paragraph.front().length();wordNum++;paragraph.pop();}return line;}void PrintLine(TEXT line){while(true){cout<<line.front();line.pop();if(line.empty())break;cout<<' ';}cout<<endl;}void AlignLeft(TEXT paragraph){while(!paragraph.empty()){TEXT line=GetLine(paragraph);PrintLine(line);}}void AlignRight(TEXT paragraph){while(!paragraph.empty()){TEXT line=GetLine(paragraph);for(int i=line.size()+LineWordLength(line)-1;i<LINEWIDTH;i++)cout<<' ';PrintLine(line);}}void AlignCenter(TEXT paragraph){while(!paragraph.empty()){TEXT line=GetLine(paragraph);for(int i=line.size()+LineWordLength(line)-1;i<LINEWIDTH-1;i+=2)cout<<' ';PrintLine(line);}}void AlignJustify(TEXT paragraph){while(!paragraph.empty()){TEXT line=GetLine(paragraph);if(paragraph.empty())PrintLine(line);else{int totalSpaceNum,averageSpaceNum,anotherSpaceNum;totalSpaceNum=LINEWIDTH-LineWordLength(line);averageSpaceNum=totalSpaceNum/(line.size()-1);anotherSpaceNum=totalSpaceNum%(line.size()-1);int outputCount=0;while(!line.empty()){cout<<line.front();line.pop();if(line.empty())break;for(int i=0;i<averageSpaceNum;i++)cout<<' ';if(outputCount<anotherSpaceNum)cout<<' ';outputCount++;}cout<<endl;}}}int main(){char alignMode;cin>>alignMode;string word;TEXT paragraph;while(cin>>word)paragraph.push(word);switch(alignMode){case 'L':AlignLeft(paragraph);break;case 'R':AlignRight(paragraph);break;case 'C':AlignCenter(paragraph);break;case 'J':AlignJustify(paragraph);break;default:break;}}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/378706.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号