翻译程序


 小明初学C++,已明白了四则运算、关系运算、逻辑运算、赋值运算、输入输出、简单选择和循环结构的用法,但他的英语不太好,记不住太多的保留字,于是他利用汉语拼音做保留字,山寨C++,发明了一种表达自己思想的算法描述规则。
    规则很简单:他将开始程序头部以一个拼音名字标记,C++程序中的"{,}"用拼音“kaishi,jieshu”直观表示;选择和循环只采用一种单一的结构,且保留字也分别用对应的拼音表示,不过在表示选择或循环条件时他去掉了多余的小括号;输入输出也用拼音表示;数据类型只保留了整型(int)和实型(float),并用拼音表示,且按他的习惯变量在前,类型在后。
    现在小明想请熟悉C++的你帮他写一个程序,将用他设计的算法描述规则写成的算法,翻译成C++源码。输入文件扩展名为.ming,输出文件扩展名为.cpp,如下例:

小明算法(test.ming):
chengxu1
kaishi
   i,j zhengxing;
   k   shixing;
   i=1;j=0;
   shuru k;  
   xunhuan i<10
   kaishi
      j=j+i;
      i++;
   jieshu
   ruguo j>10
   kaishi
     k=j*1.0/i;    
   jieshu  
   shuchu k,j;
jieshu

翻译成的C++源码(test.cpp):
#include <iostream>
using namespace std;
int main()
{
    int i,j;
    float k;
    i=1;j=0;
    cin>>k;
    while( i<10)
    {
       j=j+i;
       i++;
    } 
 if(j>10)
  k=j*1.0/i; 
 cout<<k<<j;
    return 0;
}

要求:字符串操作需自己实现。


输入后敲回车, 按ctrl+z, 再回车即可显示翻译后的程序

  1 #include <iostream>
  2 #include <string>
  3 #define max_size 30
  4 #define MaxNum 9
  5 using namespace std;
  6 string XiaoMing[MaxNum]=
  7 {
  8     "chengxu1",
  9     "kaishi",
 10     "jieshu",
 11     "xunhuan",
 12     "shuru",
 13     "shuchu",
 14     "ruguo",
 15     "zhengxing",
 16     "shixing"
 17 };
 18 string Cpp[MaxNum]=
 19 {
 20     "#include <iostream>
#include <string>
using namespace std;
int main()",
 21     "{",
 22     "}",
 23     "while(",
 24     "cin>>",
 25     "cout<<",
 26     "if(",
 27     "int",
 28     "float"
 29 };
 30 void GetNextval(string &line,int nextval[])//对模式串求next[]值
 31 {
 32     int j=0,k=-1;
 33     nextval[0]=-1;
 34     while(j<line.length())
 35     {
 36         if(k==-1 || line[j]==line[k])//k为-1或字符相等时
 37         {
 38             j++;
 39             k++;
 40             if(line[j]!=line[k])
 41                 nextval[j]=k;
 42             else
 43                 nextval[j]=nextval[k];
 44         }
 45         else k=nextval[k];
 46     }
 47 }
 48 int KMP(string &s,string &line)//kmp算法实现
 49 {
 50     int sl=s.length();
 51     int ll=line.length();
 52     int nextval[max_size],i=0,j=0;
 53     GetNextval(line,nextval);
 54     while(i< sl&& j<ll)
 55     {
 56         if(j==-1 || s[i]==line[j])
 57         {
 58             i++;
 59             j++;
 60         }
 61         else
 62         {
 63             j=nextval[j];
 64         }
 65         sl=s.length();
 66         ll=line.length();
 67     }
 68     if(j>=line.length())
 69         return (i-line.length());//返回匹配模式串的首字符下标
 70     else
 71         return -1;//返回不匹配标志
 72 }
 73 int main()
 74 {
 75     string line;
 76     string str = "";
 77     int next[max_size];
 78     while(getline(cin,line))
 79     {
 80         bool fenhao;
 81         if(line[line.length()-1]==';')
 82             fenhao = true;
 83         else
 84             fenhao = false;
 85         bool flag = false;
 86         for(int m=0; m<9; m++)
 87         {
 88             int i;
 89             int x = KMP(line,XiaoMing[m]);
 90             if(x!=-1)
 91             {
 92                 flag = true;
 93                 if(m==0)//chengxu1
 94                 {
 95                     for(i=0; i<x; i++) //kongge
 96                     {
 97                         //cout<<line[i];
 98                         str += line[i];
 99                     }
100                     //cout<<Cpp[0];
101                     str += Cpp[0];
102                 }
103                 else if(m==1)//kaishi
104                 {
105                     for(i=0; i<x; i++) //kongge
106                     {
107                         //cout<<line[i];
108                         str += line[i];
109                     }
110                     //cout<<Cpp[1];
111                     str += Cpp[1];
112                 }
113                 else if(m==2)//jieshu
114                 {
115                     for(i=0; i<x; i++)
116                     {
117                         //cout<<line[i];
118                         str += line[i];
119                     }
120                     //cout<<Cpp[2];
121                     str += Cpp[2];
122                 }
123                 else if(m==3)//xunhuan
124                 {
125                     for(i=0; i<x; i++) //kongge
126                     {
127                         //cout<<line[i];
128                         str += line[i];
129                     }
130                     //cout<<Cpp[3];
131                     str += Cpp[3];
132                     for(i=x+7; i<line.length(); i++)
133                     {
134                         //cout<<line[i];
135                         str += line[i];
136                     }
137                     //cout<<")";
138                     str += ")";
139                 }
140                 else if(m==4)//shuru
141                 {
142                     for(i=0; i<x; i++) //kongge
143                     {
144                         //cout<<line[i];
145                         str += line[i];
146                     }
147                     //cout<<Cpp[4];
148                     str += Cpp[4];
149                     for(i=x+5; i<line.length(); i++)
150                     {
151                         if(line[i]==',')
152                             //cout<<">>";
153                             str += ">>";
154                         else if(line[i]==';')
155                         {
156                             //cout << ";";
157                             str += ";";
158                             break;
159                         }
160                         else
161                             //cout<<line[i];
162                             str += line[i];
163                     }
164                 }
165                 else if(m==5)//shuchu
166                 {
167                     for(i=0; i<x; i++) //kongge
168                     {
169                         //cout<<line[i];
170                         str += line[i];
171                     }
172                     //cout<<Cpp[5];
173                     str += Cpp[5];
174                     for(int i=x+6; i<line.length(); i++)
175                     {
176                         if(line[i]==',')
177                             //cout<<"<<";
178                             str += "<<";
179                         else if(line[i]==';')
180                             break;
181                         else
182                             //cout<<line[i];
183                             str += line[i];
184                     }
185                 }
186                 else if(m==6)//ruguo
187                 {
188                     for(i=0; i<x; i++)
189                     {
190                         //cout<<line[i];
191                         str += line[i];
192                     }
193                     //cout<<Cpp[6];
194                     str += Cpp[6];
195                     for(i=x+5; i<line.length(); i++)
196                     {
197                         //cout<<line[i];
198                         str += line[i];
199                     }
200                     //cout<<")";
201                     str += ")";
202                 }
203                 else if(m==7)//zhengxing
204                 {
205                     for(i=0; i<x-4; i++) //kongge
206                     {
207                         //cout<<line[i];
208                         str += line[i];
209                     }
210                     //cout<<Cpp[7];
211                     str += Cpp[7];
212                     for(i=0; i<x; i++)
213                     {
214                         //cout<<line[i];
215                         str += line[i];
216                     }
217                 }
218                 else if(m==8)//shixing
219                 {
220                     for(i=0; i<x-4; i++) //kongge
221                     {
222                         //cout<<line[i];
223                         str += line[i];
224                     }
225                     //cout<<Cpp[8];
226                     str += Cpp[8];
227                     for(i=0; i<x; i++)
228                     {
229                         //cout<<line[i];
230                         str += line[i];
231                     }
232                 }
233             }
234         }
235         if(!flag)
236             //cout << line;
237             str += line;
238         if(fenhao && flag)
239             //cout <<";";
240             str += ";";
241         //cout <<endl;
242         str += "
";
243     }
244     cout << str;
245     return 0;
246 }
247 
248 /*
249 chengxu1
250 kaishi
251    i,j zhengxing;
252    k   shixing;
253    i=1;j=0;
254    shuru k;
255    xunhuan i<10
256    kaishi
257       j=j+i;
258       i++;
259    jieshu
260    ruguo j>10
261    kaishi
262      k=j*1.0/i;
263    jieshu
264    shuchu k,j;
265 jieshu
266 */
原文地址:https://www.cnblogs.com/mjn1/p/11278318.html