编程珠玑(二):格式信函编程

来源:互联网 发布:hp1108打印机驱动mac 编辑:程序博客网 时间:2024/06/06 18:55

一,使用情况

当程序主要是输出且输出格式相同时,可以运用格式信函编程进行快速输出与解释。
需要两样东西:

  1. 格式信函模板
  2. 格式信函生成器

二,小的例子

1.格式信函模板

Hello Everyone!My name is $0.I am $1 years old.I am a $2 now.

2.格式信函生成器

#include <iostream>#include <fstream>#include <string>using namespace std;int main(){    string PrintString[3]={"Jack","21","student"};    ifstream file("form.txt");    string str;    while(getline(file,str))    {        for(int i=0;i<str.size();i++)        {            if(str[i]=='$')            {                i++;                if(str[i]=='$')                    cout<<"$";                else if(str[i]>='0'&&str[i]<='9')                    cout<<PrintString[str[i]-'0'];                else                    cout<<"error schema"<<endl;            }            else                cout<<str[i];        }        cout<<endl;    }    return 0;}
0 0
原创粉丝点击