filestream.h:
#ifndef FILESTREAM_H
#define FILESTREAM_H
//#endif // FILESTREAM_H
//#define FileStream_H
//#ifndef StrBufferStack_H
//#endif
#include "strbufferstack.h"
#include <fstream>
using namespace std;
class FileStream
{
public:
string myFileName;
fstream myFile;
public:
FileStream(const char* fileName);
~FileStream()
{
myFile.close();
}
void createNew()
{
myFile.close();
myFile.open((const char*)&myFileName[0],ios::out);
refresh();
}
void refresh()
{
myFile.close();
myFile.open((const char*)&myFileName[0],ios::in|ios::out);
}
StrBufferStack* toString();
void write(StrBufferStack *sbs);
};
#endif
mainwindows.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "filestream.h"
#include "wordanalyse.h"
#include <QString>
namespace Ui
{
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
// void on_output_anchorClicked();
// void on_input_anchorClicked();
// void on_show_clicked();
void doexpor();
private:
Ui::MainWindow *ui;
void MyWriteFile(string fileName,const char *bf);//,const char *bf
char* MyWordAnalyse(string fileName);
};
#endif // MAINWINDOW_H
strbufferstack.h:
#ifndef STRBUFFERSTACK_H
#define STRBUFFERSTACK_H
#ifndef MAXSIZE
#define MAXSIZE 20000
#endif
#include <iostream>
using namespace std;
class StrBufferStack{
public:
StrBufferStack(int sz);
~StrBufferStack();
void push(char ch);
void push(char *bf);
void push(string str);
char pop();
void eat(StrBufferStack *sbs);
char getA(int indx);
void clear();
char* getStr();
int getLength();
private:
char *buffer;
int size;
int index;
};
#endif
wordanalyse.h:
#ifndef WORDANALYSE_H
#define WORDANALYSE_H
#include "wordbuffer.h"
#include "strbufferstack.h"
#define RESERVEDWORD '1' //保留字
#define IDENTIFIER '2' //标识符
#define CONSTANT '3' //常数
#define OPERATOR '4' //运算符
#define SEPARATOR '5' //分隔符
#define STRINGTYPE '6' //字符串
#define CHARTYPE '7' //字符
class WordAnalyse
{
private:
StrBufferStack *code;
WordBuffer *result;
void deleteNotes();
bool isString(char ch);
bool isChar(char ch);
bool isBlank(char ch);
bool isSeparator(char ch);
bool isOperator(char ch);
bool isConstant(StrBufferStack *sbs);
bool isIdentifier(StrBufferStack *sbs);
bool isReservedWord(StrBufferStack *sbs);
char isWhat(StrBufferStack *sbs);
public:
WordAnalyse(StrBufferStack *sbs);
~WordAnalyse()
{
delete code;
delete result;
}
void analyze();
char* getResult();
};
#endif // WORDANALYSE_H
//#ifndef WordBuffer_H
//#include "WordBuffer.h"
//#endif
//#define WordAnalyze_H
wordbuffer.h
#ifndef WORDBUFFER_H
#define WORDBUFFER_H
#define WORDLEN 50
#include "strbufferstack.h"
class WordBuffer{
public:
WordBuffer(int sz){
size = sz;
wordBuffer = new char*[size];
typeBuffer = new char[size];
index = 0;
}
~WordBuffer(){
for(int i=0;i<index;i++){
delete [] wordBuffer[i];
}
delete []wordBuffer;
delete []typeBuffer;
}
void push(char type,StrBufferStack *word);
void push(char type,char word);
char* get();
private:
char **wordBuffer;
char *typeBuffer;
int index;
int size;
};
#endif
filestream.cpp:
#include "filestream.h"
#include <QDebug>
FileStream::FileStream(const char* fileName)
{
fileName = "source.txt";
myFileName = fileName;
myFile.open(fileName,ios::in|ios::out);
if(!myFile)
{
qDebug()<<"will create a new file !"<< endl;
myFile.close();
myFile.open((const char*)&myFileName[0],ios::out);
refresh();
}
else
{
qDebug()<< "open the file called :" << fileName << endl;
}
}
StrBufferStack* FileStream::toString(){
StrBufferStack *sbs = new StrBufferStack(MAXSIZE);
char ch;
refresh();
myFile.unsetf(ios::skipws);
while(!myFile.eof())
{
myFile>>ch;
sbs->push(ch);
}
sbs->pop();
return sbs;
}
void FileStream::write(StrBufferStack *sbs)
{
int len = sbs->getLength();
for(int i = 0;i<len;i++)
{
myFile<<sbs->getA(i);
}
refresh();
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindows.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "wordanalyse.h"
#include <QString>
#include <iostream>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(this->ui->show,SIGNAL(clicked(bool)),this,SLOT(doexpor()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::MyWriteFile(string fileName,const char *bf)
{
FileStream *fs = new FileStream(&fileName[0]);
StrBufferStack *sbs = new StrBufferStack(MAXSIZE);
sbs->push(bf);
fs->createNew();
fs->write(sbs);
delete fs;
delete sbs;
}
//Analyze code in file and save result into new file called "result"+fileName
char* MainWindow::MyWordAnalyse(string fileName)
{
char* result;
FileStream *fs = new FileStream("source.txt");
WordAnalyse *ana = new WordAnalyse(fs->toString());
ana->analyze();
result = ana->getResult();
this->ui->output->setText(result);
fs->myFile << result;
delete fs;
delete ana;
return result;
}
void MainWindow::doexpor()
{
ofstream ffile;
QString temp;
ffile.open( "out.txt",ios::in );
temp = this->ui->input->toPlainText();
string c = temp.toStdString();
ffile << c ;
const char* ch = c.c_str();
// qDebug()<< " have input the string to source.txt" << endl;
// this->ui->output->setText(temp);
this->MyWriteFile("source.txt",ch);
this->MyWordAnalyse("source.txt");
}
strbufferstack.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "wordanalyse.h"
#include <QString>
#include <iostream>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(this->ui->show,SIGNAL(clicked(bool)),this,SLOT(doexpor()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::MyWriteFile(string fileName,const char *bf)
{
FileStream *fs = new FileStream(&fileName[0]);
StrBufferStack *sbs = new StrBufferStack(MAXSIZE);
sbs->push(bf);
fs->createNew();
fs->write(sbs);
delete fs;
delete sbs;
}
//Analyze code in file and save result into new file called "result"+fileName
char* MainWindow::MyWordAnalyse(string fileName)
{
char* result;
FileStream *fs = new FileStream("source.txt");
WordAnalyse *ana = new WordAnalyse(fs->toString());
ana->analyze();
result = ana->getResult();
this->ui->output->setText(result);
fs->myFile << result;
delete fs;
delete ana;
return result;
}
void MainWindow::doexpor()
{
ofstream ffile;
QString temp;
ffile.open( "out.txt",ios::in );
temp = this->ui->input->toPlainText();
string c = temp.toStdString();
ffile << c ;
const char* ch = c.c_str();
// qDebug()<< " have input the string to source.txt" << endl;
// this->ui->output->setText(temp);
this->MyWriteFile("source.txt",ch);
this->MyWordAnalyse("source.txt");
}
wordanalyse.cpp:
#include "wordanalyse.h"
WordAnalyse::WordAnalyse(StrBufferStack *sbs)
{
code = sbs;
result = new WordBuffer(MAXSIZE);
}
void WordAnalyse::deleteNotes(){
StrBufferStack *newCode = new StrBufferStack(MAXSIZE);
int len = code->getLength();
char ch,nxtch;
for(int i=0;i<len;i++){
if((ch = code->getA(i)) == '/' && (nxtch = code->getA(i+1)) == '*'){
ch = code->getA((++i)+1);
while((++i)<len){
cout<<i<<endl;
if((nxtch = code->getA(i+1)) == '/' && ch == '*'){
i++;
break;
}
else{
ch = nxtch;
}
}
}
else if(ch == '/' && nxtch == '/'){
while((++i)<len){
if((ch = code->getA(i)) == '\n'||ch == '\r')
break;
}
}
else{
newCode->push(ch);
}
}
delete code;
code = newCode;
}
char* WordAnalyse::getResult(){
return result->get();
}
bool WordAnalyse::isString(char ch){
if(ch == '\"')
return true;
else
return false;
}
bool WordAnalyse::isChar(char ch){
if(ch == '\'')
return true;
else
return false;
}
bool WordAnalyse::isBlank(char ch){
string s = " \t\v\n\r\0";
for(int i=s.length()-1;i>=0;i--){
if(ch == s[i])
return true;
}
return false;
}
bool WordAnalyse::isSeparator(char ch){
string s = ",;{}[]()";
for(int i=s.length()-1;i>=0;i--){
if(ch == s[i])
return true;
}
return false;
}
bool WordAnalyse::isOperator(char ch){
string s = "+-*/=!<>#~&|";
for(int i=s.length()-1;i>=0;i--){
if(ch == s[i])
return true;
}
return false;
}
bool WordAnalyse::isConstant(StrBufferStack *sbs){
int len = sbs->getLength();
char ch;
for(int i=0;i<len;i++){
ch = sbs->getA(i);
if(( (int)(ch)>57 || (int)(ch)<48) && ch!='.'){
return false;
}
}
return true;
}
bool WordAnalyse::isIdentifier(StrBufferStack *sbs){
if(isReservedWord(sbs) || isConstant(sbs)){
return false;
}
else
return true;
}
bool WordAnalyse::isReservedWord(StrBufferStack *sbs){
string s = "if#int#char#for#while#do#return#break#continue#bool#struct#switch#";
int lenM = sbs->getLength();
int lenT = s.length();
for(int i=0,j=0;j<lenM&&i<lenT;i++){
if(sbs->getA(j) != s[i]){
j = 0;
for(;i<lenT;i++){
if(s[i] == '#'){
break;
}
}
}
else{
j++;
}
if(j == lenM && s[i+1] == '#'){
return true;
}
}
return false;
}
char WordAnalyse::isWhat(StrBufferStack *sbs)
{
if(isConstant(sbs))
{
return CONSTANT;
}
else if(isReservedWord(sbs))
{
return RESERVEDWORD;
}
else{
return IDENTIFIER;
}
}
void WordAnalyse::analyze(){
StrBufferStack *bf = new StrBufferStack(WORDLEN);
int len = code->getLength();
char ch;
//删除注释
deleteNotes();
for(int i=0;i<len;i++){
ch = code->getA(i);
if(isString(ch)){
if(bf->getLength()>0){
result->push(isWhat(bf),bf);
bf->clear();
}
while(i<len-1){
if((ch = code->getA(++i)) == '\\'){
bf->push(ch);
ch = code->getA(++i);
bf->push(ch);
}
else if(ch == '\"'){
break;
}
else{
bf->push(ch);
}
}
result->push(STRINGTYPE,bf);
bf->clear();
}
else if(isChar(ch)){
if(bf->getLength()>0){
result->push(isWhat(bf),bf);
bf->clear();
}
while(i<len-1){
ch = code->getA(++i);
if(ch == '\\'){
bf->push(ch);
ch = code->getA(++i);
bf->push(ch);
}
else if(ch == '\''){
break;
}
else{
bf->push(ch);
}
}
result->push(CHARTYPE,bf);
bf->clear();
}
else if(isBlank(ch)){
if(bf->getLength()>0){
result->push(isWhat(bf),bf);
bf->clear();
}
}
else if(isSeparator(ch)){
if(bf->getLength()>0){
result->push(isWhat(bf),bf);
bf->clear();
}
result->push(SEPARATOR,ch);
}
else if(isOperator(ch)){
if(bf->getLength()>0){
result->push(isWhat(bf),bf);
bf->clear();
}
bf->push(ch);
if(i<len-1)
ch = code->getA(++i);
while(isOperator(ch)&&i<len-1){
bf->push(ch);
ch = code->getA(++i);
}
i--;
result->push(OPERATOR,bf);
bf->clear();
}
else{
bf->push(ch);
}
}
delete bf;
}
wordbuffer.cpp:
#include "wordbuffer.h"
void WordBuffer::push(char type,StrBufferStack *word){
if(index < size){
wordBuffer[index] = word->getStr();
typeBuffer[index++] = type;
}
}
void WordBuffer::push(char type,char word){
char *w = new char[2];
w[0] = word;
w[1] = '\0';
if(index < size){
wordBuffer[index] = w;
typeBuffer[index++] = type;
}
}
char* WordBuffer::get(){
StrBufferStack *sbs = new StrBufferStack(MAXSIZE);
for(int i = 0,j,len;i<index;i++){
sbs->push('(');
sbs->push(typeBuffer[i]);
sbs->push(',');
sbs->push('"');
for(j=0,len = ((string)wordBuffer[i]).length();j<len;j++){
sbs->push(wordBuffer[i][j]);
}
sbs->push('"');
sbs->push(')');
sbs->push('\r');
sbs->push('\n');
}
char *r = sbs->getStr();
delete sbs;
return r;
}
界面:
运行结果: