数据结构-串-test.ming



主题: 4、串    设置标记    

作者: 匿名
张贴日期: 2016年8月31日 星期三 上午11时15分23秒 CST
最后修改日期: 2016年8月31日 星期三 上午11时15分23秒 CST
查看总数: 11  您的查看次数: 1
 
 
 

 
翻译程序
 小明初学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;
}

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

-----------------------------------------------------main.cpp----------------------------------------------

#include <iostream>
#include <string>
#include <fstream>
#include<stdlib.h>
#include "KMP.h"
using namespace std;


int main()
{
    ifstream input("test.ming");
    ofstream output("test.cpp");
    string sourceFile;///存放读取到的源文件
    while(getline(input,sourceFile))
    {
        bool fenhao = false;
        if(sourceFile[sourceFile.length()-1]==';')
            fenhao=true;
        bool go=false;
        for(int matchTime=0; matchTime<9; matchTime++) ///test.ming里面有9个关键字,每次匹配一个关键字,通过matchTime的值来判断匹配到的是哪个keyWords
        {
            int i;
            int x=KMPIndex(sourceFile,keyWords[matchTime]);///KMP算法进行串的匹配,失败返回-1,成功返回匹配到的首个下标
            if(x!=-1)///匹配成功,进入if语句
            {
                go=true;///go设置为true,用于这个for循环后面的分号是否输出判断
                if(matchTime==0)///匹配到  关键字  chengxu1 ,输出相应的翻译出的字符
                {
                    output<<printWords[matchTime];
                }
                else if(matchTime==1)///匹配到 关键字  kaishi ,输出相应的翻译出的字符
                {
                    output<<printWords[1];
                }
                else if(matchTime==2)///匹配到关键字 jieshu ,输出相应的翻译出的字符
                {
                    output<<" "<<printWords[2];
                }


                else if(matchTime==3) ///匹配到 关键字  xunhuan
                {
                    output<<" "<<printWords[3];
                    for(i=x+7; i<sourceFile.length(); i++) ///x是匹配到的首个下标,+7是把xunhuan(7个位子)去掉之后,把后面的输出来
                    {
                        output<<sourceFile[i];
                    }
                    output<<")";
                }
                else if(matchTime==4)///匹配到 关键字 shuru
                {
                    output<<"   "<<printWords[4];
                    for(int i=x+5; i<sourceFile.length(); i++)
                    {
                        if(sourceFile[i]==',') output<<">>";///输入是多个值的时候中间有逗号隔开,所以会在俩个值中间再加一个符号>>
                        else  if(sourceFile[i]==';')///最后符号为分号输出分号
                        {
                            fenhao=true;
                            break;
                        }
                        else output<<sourceFile[i];//输出输入的值
                    }


                }
                else if(matchTime==5)///匹配到 关键字 shuchu
                {
                    output<<"   "<<printWords[5];
                    for(int i=x+6; i<sourceFile.length(); i++)
                    {
                        if(sourceFile[i]==',')
                        {
                            output<<"<<";   ///输出的值有多个的时候 中间用逗号隔开,换成<<
                        }
                        else if(sourceFile[i]==';')
                        {
                            fenhao=true;
                            break;
                        }
                        else
                        {
                            output<<sourceFile[i];
                        }
                    }
                }
                else if(matchTime==6)///关键字  ruguo
                {
                    output<<"   "<<printWords[6];
                    for(i=x+5; i<sourceFile.length(); i++)
                    {
                        output<<sourceFile[i];
                    }
                    output<<")";
                }
                else if(matchTime==7)///关键字 zhengxing
                {
                    output<<"   "<<printWords[7];
                    for(i=0; i<x; i++) ///源文件中格式原因(i,j zhengxing;),从i=0开始输出到文件,直到小于匹配后返回的下标结束
                    {
                        output<<sourceFile[i];
                    }
                }
                else if(matchTime==8)
                {
                    output<<"   "<<printWords[8];
                    for(i=0; i<x; i++)
                    {
                        output<<sourceFile[i];
                    }
                }
                else  cout<<"file error!"<<endl;
            }
        }
        if(!go) output<<sourceFile;///一行中没有keyWords,直接输出到文件中。
        if(fenhao&&go)
            output<<";";
        output<<endl;//结束
    }
    input.close();
    FILE *file= fopen("test.cpp", "r");

    readFile(file);
    return 0;
}



--------------------------------------------------KMP.h------------------------------------------------------

#ifndef KMP_H_INCLUDED
#define KMP_H_INCLUDED

#include<iostream>
using namespace std;
string keyWords[9]=
{
    "chengxu1",//0
    "kaishi",//1
    "jieshu",//2
    "xunhuan",//3
    "shuru",//4
    "shuchu",//5
    "ruguo",//6
    "zhengxing",//7
    "shixing"//8
};
string printWords[9]=
{
    "#include <iostream>\nusing namespace std;\nint main()",//0
    "{",//1
    "}",//2/
    "while(",//3
    "cin>>",//4
    "cout<<",//5
    "if(",//6
    "int",//7
    "float"//8
};
void GetNextval(string &line,int nextval[])
{
    int j=0,k=-1;
    nextval[0]=-1;
    while(j<line.length())
    {
        if(k==-1||line[j]==line[k])//k为-1或字符相等时
        {
            j++;
            k++;
            if(line[j]!=line[k])
                nextval[j]=k;
            else
                nextval[j]=nextval[k];
        }
        else k=nextval[k];
    }
}

int KMPIndex(string &substr,string &line)//kmp算法进行串的匹配
{
    int sl=substr.length();
    int ll=line.length();
    int nextval[30],i=0,j=0;
    GetNextval(line,nextval);
    while(i< sl&& j<ll)
    {
        if(j==-1 || substr[i]==line[j])
        {
            i++;
            j++;
        }
        else
        {
            j=nextval[j];
        }
        sl=substr.length();
        ll=line.length();
    }
    if(j>=line.length())
        return (i-line.length());//返回匹配模式串的首字符下标
    else return -1;//返回不匹配标志
}

void readFile(FILE *pf)
{
    FILE *pf2 = fopen("test.txt", "w");
    if(pf2 == NULL)
    {
        printf("file error!\n");
        fclose(pf);
        exit(0);
    }
    char ch;
    while(!feof(pf))
    {
        ch = fgetc(pf);
        putchar(ch);
        fputc(ch, pf2);
    }
    fclose(pf2);
    fclose(pf);
    cout<<"\n\nfile success!"<<endl;
}
#endif // KMP_H_INCLUDED


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值