英语翻译小明初学C++,已明白了四则运算、关系运算、逻辑运算、赋值运算、输入输出、简单选择和循环结构的用法,但他的英语不太好,记不住太多的保留字,于是他利用汉语拼音做保留字,山
来源:学生作业帮助网 编辑:六六作业网 时间:2024/11/17 05:59:39
英语翻译小明初学C++,已明白了四则运算、关系运算、逻辑运算、赋值运算、输入输出、简单选择和循环结构的用法,但他的英语不太好,记不住太多的保留字,于是他利用汉语拼音做保留字,山
英语翻译
小明初学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 i10
kaishi
k=j*1.0/i;
jieshu
shuchu k,j;
jieshu
翻译成的C++源码(test.cpp):
#include
using namespace std;
int main()
{
int i,j;
float k;
i=1;j=0;
cin>>k;
while( i10)
k=j*1.0/i;
cout
英语翻译小明初学C++,已明白了四则运算、关系运算、逻辑运算、赋值运算、输入输出、简单选择和循环结构的用法,但他的英语不太好,记不住太多的保留字,于是他利用汉语拼音做保留字,山
呵呵,写了个简单的,又臭又长,希望对你有帮助.
#include <iostream>#include <fstream>
using namespace std;
const int line_len = 1000;
const int statement_len = 1000;
int my_strcmp(char *s1, char *s2);
int my_strlen(char *str);
int remove_blank_back(char *str);
int remove_blank_front(char *str);
void fill_blank(int n, ofstream &fout);
bool start_with(char *str, char *word);
bool end_with(char *str, char *word);
int get_statement(char *str, char *statement);
void handle_line(char *str, ofstream &fout);
void handle_statement(char *str, ofstream &fout);
int main()
{
ifstream fin("test.ming");
ofstream fout("test.cpp");
char line[line_len];
int len1, len2;
bool start_flag = false;
fout << "#include <iostream>\n";
fout << "using namesapce std;\n\n";
fout << "int main()\n";
while (fin.getline(line, line_len))
{
len1 = remove_blank_back(line);
len2 = remove_blank_front(line);
if (!start_flag && my_strcmp(line, "kaishi"))
continue;
else if (!start_flag && !my_strcmp(line, "kaishi"))
{
start_flag = true;
fill_blank(len1 - len2, fout);
fout << "{\n";
}
else
{
fill_blank(len1 - len2, fout);
handle_line(line, fout);
}
}
fin.clear();
fin.close();
fout.clear();
fout.close();
}
int my_strcmp(char *s1, char *s2)
{
int i = 0;
for (; s1[i] != '\0' && s2[i] != '\0'; i++)
if (s1[i] != s2[i])
return s1[i] - s2[i];
if (s1[i] != '\0')
return s1[i];
if (s2[i] != '\0')
return -s2[i];
return 0;
}
int my_strlen(char *str)
{
int i = 0;
while (str[i] != '\0')
i++;
return i;
}
int remove_blank_back(char *str)
{
int len = my_strlen(str);
while (str[len - 1] == ' ' || str[len - 1] == '\t')
len--;
str[len] = '\0';
return len;
}
int remove_blank_front(char *str)
{
int i = 0, j = 0;
while (str[i] != '\0' && (str[i] == ' ' || str[i] == '\t'))
i++;
for (; str[i] != '\0'; i++, j++)
str[j] = str[i];
str[j] = '\0';
return j;
}
void fill_blank(int n, ofstream &fout)
{
for (int i = 0; i < n; i++)
fout << ' ';
}
int get_statement(char *str, char *statement)
{
int i = 0, j;
remove_blank_front(str);
for (; str[i] != '\0' && str[i] != ';'; i++)
statement[i] = str[i];
if (str[i] == ';')
statement[i++] = ';';
statement[i] = '\0';
for (j = i; str[j] != '\0'; j++)
str[j - i] = str[j];
str[j - i] = '\0';
return i;
}
void handle_line(char *str, ofstream &fout)
{
char statement[statement_len];
int len1, len2;
len1 = remove_blank_back(str);
len2 = remove_blank_front(str);
while (len2)
{
fill_blank(len1 - len2, fout);
get_statement(str, statement);
handle_statement(statement, fout);
len1 = len2;
len2 = remove_blank_front(str);
}
fout << endl;
}
void handle_statement(char *str, ofstream &fout)
{
if (!my_strcmp(str, "kaishi"))
fout << "{";
else if (!my_strcmp(str, "jieshu"))
fout << "}";
else if (start_with(str, "shuru"))
{
fout << "cin >> ";
for (int i = 0; str[i] != '\0'; i++)
if (str[i] == ',')
fout << " >> ";
else if (str[i] != ' ')
fout << str[i];
}
else if (start_with(str, "xunhuan"))
{
remove_blank_front(str);
fout << "while (" << str << ")";
}
else if (start_with(str, "ruguo"))
{
remove_blank_front(str);
fout << "if (" << str << ")";
}
else if (start_with(str, "shuchu"))
{
fout << "cout << ";
for (int i = 0; str[i] != '\0'; i++)
if (str[i] == ',')
fout << " << ";
else if (str[i] != ' ')
fout << str[i];
}
else if (end_with(str, "zhengxing;"))
fout << "int " << str << ';';
else if (end_with(str, "shixing;"))
fout << "float " << str << ';';
else
fout << str;
}
bool start_with(char *str, char *word)
{
int i = 0, j = 0;
for (; str[i] != '\0' && word[i] != '\0'; i++)
if (str[i] != word[i])
return false;
if (word[i] != '\0')
return false;
for (j = i; str[j] != '\0'; j++)
str[j - i] = str[j];
str[j - i] = '\0';
return true;
}
bool end_with(char *str, char *word)
{
int len_str, len_word;
len_str = my_strlen(str);
len_word = my_strlen(word);
int i = len_str - 1;
int j = len_word - 1;
for (; i >= 0 && j >= 0; i--, j--)
if (str[i] != word[j])
return false;
if (j >= 0)
return false;
str[i + 1] = '\0';
return true;
}