UVa490

490 - Rotating Sentences

Time limit: 3.000 seconds

Rotating Sentences

In ``Rotating Sentences,'' you are asked to rotate a series of input sentences 90 degrees clockwise. So instead of displaying the input sentences from left to right and top to bottom, your program will display them from top to bottom and right to left.

Input and Output

As input to your program, you will be given a maximum of 100 sentences, each not exceeding 100 characters long. Legal characters include: newline, space, any punctuation characters, digits, and lower case or upper case English letters. (NOTE: Tabs are not legal characters.)

The output of the program should have the last sentence printed out vertically in the leftmost column; the first sentence of the input would subsequently end up at the rightmost column.

Sample Input

Rene Decartes once said,
"I think, therefore I am."

Sample Output

"R
Ie
 n
te
h 
iD
ne
kc
,a
 r
tt
he
es
r
eo
fn
oc
re
e
 s
Ia
 i
ad
m,
.
"
 

#include "iostream"
#include "cstdio"
#include "string"

using namespace std;

int const MAX= 110;

int main()
{
string a[MAX];
int i=0, j,max=0, cont;
while(getline(cin,a[i]))
{
  if(max < a[i].length())
   max = a[i].length();
  i++;
}
cont = i;
for(i=0; i<max; i++)
{
  for(j=cont-1; j>=0; j--)
   cout<<(i<a[j].length() ? a[j][i] : ' ');
  cout<<endl;
}
return 0;
}

------------------------------------------------------------------------------------------------------------------------------------------

定义长度不能等于100, 要大于100。

-------------------------------------------------------------------------------------------------------------------------------------------

//类似于矩阵转换, 可惜的是, 由于getline运用不好, N次WA,

getline(cin, str)

cin   输入流

str   string对象

getline函数在输入流中读取字符, 但不读取换行符。

getline功能

  getline是string类对象的成员函数,属于string IO 操作。用于读取整行文本

编辑本段

getline参数

  getline(cin,str);

  cin:输入流

  str:string对象

编辑本段

getline说明

  getline函数从输入流的下一行读取,并保存读取的内容到str中,但不包括换行符。getline 函数将 istream 参数作为返回值,和输入操作符一样也把它用作判断条件。 

  范例:

  #include <string>

  #include <iostream>

  using namespace std;

  int main()

  {

  string line;

  while(getline(cin,line))

  cout<<line<<endl;

  return 0;

  }

  结果:

  输入 line1 ----回车换行

  显示为空

  输入 line2 ----回车换行

  显示 line1

  输入 line3 ----回车换行

  显示 line2

  ----(以此类推)

getline的原型如下:

getline(char*c,int i,char c); 表示读入i个字符,或者遇到结束符c为止的字符数,保存到c中。

getline(char*,int);  表示读入i个字符到c中。注意读入的字符数应比实际的大1,因为读入的是字符串,字符串将会以'\0'作为结束,如果你要读入3个字符,那么i的值应该为4。

注意getline会读取并丢弃分界符。

后面的自已搞定,创建一个文件流类对象,然后用这个文件流来调用getline函数,比如

ifstreamhy1(“hyong1.txt”)//创建hy1流,并打开文件以便读取内容。

charc[333];

hy1.getline(c,3,'0');  //表示,把hy1流关联的hyong1中3个字符或者遇到'0'的字符数读入到c中。

假设有一个叫 data.txt 的文件, 它包含以下内容:

Fry: One Jilliondollars.
[Everyone gasps.]
                              Auctioneer: Sir, that's not a number.
数据读取, 测试 。


以下就是基于 data.txt 的数据读取操作:

#include <iostream>
include <fstream>
#include <string>

using namespace std;

//输出空行
void OutPutAnEmptyLine()
  {
      cout<<"\n";
}

//读取方式: 逐词读取, 词之间用空格区分
//read data from the file, Word By Word
//when used in this manner, we'll getspace-delimited bits of text from the file
//but all of the whitespace thatseparated words (including newlines) was lost.
void ReadDataFromFileWBW()
   {
      ifstream fin("data.txt"); 
     string s; 
     while( fin >> s )
       {   
         cout << "Read fromfile: " << s<< endl; 
      }
}

//读取方式: 逐行读取, 将行读入字符数组, 行之间用回车换行区分
//If we were interested in preservingwhitespace,
//we could read the file in Line-By-Line using the I/O getline() function.
void ReadDataFromFileLBLIntoCharArray()
   {
      ifstream fin("data.txt");
     const int LINE_LENGTH = 100;
     char str[LINE_LENGTH]; 
     while( fin.getline(str,LINE_LENGTH) )
       {   
         cout << "Read fromfile: " << str<< endl;
      }
}

//读取方式: 逐行读取, 将行读入字符串, 行之间用回车换行区分
//If you want to avoid reading intocharacter arrays,
//you can use the C++ string getline()function to read lines into strings
void ReadDataFromFileLBLIntoString()
   {
      ifstream fin("data.txt"); 
     string s; 
     while( getline(fin,s) )
       {   
         cout << "Read fromfile: " << s<< endl;
      }
}

//带错误检测的读取方式
//Simply evaluating an I/O object in aboolean context will return false
//if any errors have occurred
void ReadDataWithErrChecking()
   {
     string filename = "dataFUNNY.txt"; 
      ifstream fin(filename.c_str()); 
     if( !fin )
       {  
         cout << "Erroropening " << filename<< " forinput" <<endl;  
         exit(-1); 
      }
}

int main()
   {
     ReadDataFromFileWBW(); //逐词读入字符串
     OutPutAnEmptyLine(); //输出空行

     ReadDataFromFileLBLIntoCharArray(); //逐词读入字符数组
     OutPutAnEmptyLine(); //输出空行

     ReadDataFromFileLBLIntoString(); //逐词读入字符串
     OutPutAnEmptyLine(); //输出空行

     ReadDataWithErrChecking(); //带检测的读取
     return 0;
}

输出结果为:
Read from file: Fry:
Read from file: One
Read from file: Jillion
Read from file: dollars.
Read from file: [Everyone
Read from file: gasps.]
Read from file: Auctioneer:
Read from file: Sir,
Read from file: that's
Read from file: not
Read from file: a
Read from file: number.
Read from file: 数据读取,
Read from file: 测试
Read from file: 。

Read from file: Fry: One Jillion dollars.
Read from file: [Everyone gasps.]
Read from file: Auctioneer: Sir, that's not a number.
Read from file: 数据读取, 测试 。

Read from file: Fry: One Jillion dollars.
Read from file: [Everyone gasps.]
Read from file: Auctioneer: Sir, that's not a number.
Read from file: 数据读取, 测试 。

Error opening dataFUNNY.txt for input
Press any key to continue


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值