题目:编写一个"banner"函数,该函数的输入为大写字母,输出为一个字符数组,该数组以图像化的方式表示该字母。
编程 珠玑》上提到当要 输入 数据 很多,且没有 规律 时, 可以 考虑编写一个格式信函发生器( for m letter generator)用于 解析 格式信函 模板 (form letter schema)。将数据从 控制 层分离的好处在于: 避免每次针对不同的数据编写不同的代码;当需要改变一些公用文本的输出方式时,直接编辑模板即可,并不需要对数据进行修改
        题目要求:输入一个字母,输出一个字符数组,该数组要以图像的方式将该字母的大写打印出来。
        对于26个字母,每个字母的外形并没有必然规律可循,最直接 方法 是编写26个函数,针对特定的字母编写特定的打印程序,这是个体力活,代码数量将非常巨大。 联想 上面的格式信函编程,可以考虑为字母的外形 设计 一个定制模板,自己规定一套模板编写的格式,然后写一个解析程序,每次打印字母时,只需解析字母对应的模板即可,这样主要的工作量就花在每个字母模板的编写上,当然模板的编写是相当简单的,将字母图形转化为相应的模板格式即可。例如: 一个字母可以利用length = 12, width = 9的矩阵来表示


 
xxxxxxxxx
xxxxxxxxx
xxxxxxxxx
   xxx   
   xxx   
   xxx   
   xxx   
   xxx   
   xxx   
xxxxxxxxx
xxxxxxxxx
xxxxxxxxx
 
 
任何 母都可以在这张表示出来,每个点就像一个像素点。下面就对字母I和L进行模板编码,编码要求
 
(1)用尽可能简单的方式表示上面的图像;(2)方便程序解析;(3) 必须 适用于所有的情况
 
根据书上给出的编码结构,上图可表示为:
3 9 x
6 3 b 3 x 3 b
3 9 x
编码规则: 第一列表示要打印的行数,,后面的数字代表每行要打印的字符个数,个数后面紧跟要打印的字符,并用空格隔开。这里字母b表示空格。根据上述规则,字母L编码如下
9 3 x 6 b
3 9 x
xxx      
xxx      
xxx      
xxx      
xxx      
xxx      
xxx      
xxx      
xxx      
xxxxxxxxx
xxxxxxxxx
xxxxxxxx
x
那么下一步就是按照规则编写能解析该编码模板的程序,为方便 执行下面解码程序直接写在主函数里面。
InBlock.gif#include <iostream>
InBlock.gif#include < string>
InBlock.gif#include <algorithm>
InBlock.gif using namespace std;
InBlock.gif
InBlock.gif int main()
InBlock.gif{
InBlock.gif   string line;
InBlock.gif   const string tag( " ");
InBlock.gif   int numRow = 0;
InBlock.gif   int rowIdx = 0;
InBlock.gif   char cMatrix[12][9];
InBlock.gif   char cArry[9];
InBlock.gif        
InBlock.gif   while(getline(cin, line)){
InBlock.gif    
InBlock.gif     int numCln = 0;
InBlock.gif     int clnIdx = 0;
InBlock.gif    
InBlock.gif     string::size_type begIdx, endIdx;
InBlock.gif    begIdx = line.find_first_of(tag);
InBlock.gif    numRow = atoi((line.substr(0,begIdx)).c_str());
InBlock.gif    begIdx = line.find_first_not_of(tag, begIdx);
InBlock.gif    
InBlock.gif     while(begIdx < line.length() - 1)
InBlock.gif    {
InBlock.gif      endIdx = line.find_first_of(tag, begIdx);
InBlock.gif       if(endIdx == string::npos)
InBlock.gif        endIdx = line.length();
InBlock.gif      numCln = atoi((line.substr(begIdx,endIdx - begIdx)).c_str());
InBlock.gif        
InBlock.gif      begIdx = line.find_first_not_of(tag, endIdx);
InBlock.gif       char val = line[begIdx];
InBlock.gif        
InBlock.gif       for( int j = clnIdx; j < clnIdx + numCln; j++){
InBlock.gif        cArry[j] = val;
InBlock.gif      }
InBlock.gif      clnIdx += numCln;
InBlock.gif       if(begIdx < line.length() - 1)
InBlock.gif        begIdx = line.find_first_not_of(tag, ++begIdx);
InBlock.gif    }
InBlock.gif    
InBlock.gif     for( int i = rowIdx; i < rowIdx + numRow; i++){
InBlock.gif       for( int j = 0; j < 9; j++)
InBlock.gif        cMatrix[i][j] = cArry[j];
InBlock.gif    }
InBlock.gif    rowIdx += numRow;
InBlock.gif  }
InBlock.gif        
InBlock.gif   for( int i = 0; i < 12; i++){
InBlock.gif     for( int j = 0; j < 9; j++){
InBlock.gif       if(cMatrix[i][j] == 'b')
InBlock.gif        cout << " " ;
InBlock.gif       else
InBlock.gif        cout << cMatrix[i][j];
InBlock.gif    }
InBlock.gif    cout << endl;
InBlock.gif  }
InBlock.gif}
 
程序读入上面的编码模板,就可以将打印出来。 由于是while方式读入,要用F6加回车来结束输入
 
《编程珠玑》中的习题3.7.5不知道是什么意思,木有思路,求救啊