<分区>
先手下留情,我是C++初学者
我为我的解释器编写了这段代码:从源代码中读取一行并将行拆分为单词。我使用 vector 对象来存储单词。这是代码,源是文件描述符(ifstream):
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
typedef unsigned int UIntegerP;
#define V(X, Y, Z) X##Y##Z
#define Version V(0, 0, 1)
#define Free 0x0
int main(int ACount, char *Arguments[]){
if(ACount < 2){
cout << "Venus Interpreter - Engine V: " << Version << " - Interprate: -I <Source> \n";
}else{
if(Arguments[1][0] == '-' && Arguments[1][1] == 'I'){
if(ACount < 3){
cout << "Error: No input files \n";
}else if(ACount > 3){
cout << "Error: Too much arguments \n";
}else{
ifstream Source(Arguments[2]);
if(Source.good()){
# define __TEST__ 1
string Line;
vector<string> Words;
string Word;
while(getline(Source, Line)){
for(unsigned long Index = 0; Index <= Line.length(); Index++){
if(Line[Index] == ' ' or Line[Index] == '\0'){
Words.push_back(Word); //Inject the Word to Words
Word.clear();
} else {
Word += Line[Index];
}
}
# if __TEST__
cout << Words[0] << "\n";
# endif
//Interpration starts here
Words.clear();
}
}else{
cout << "Error: File does not exist \n";
}
Source.close();
}
}else{
cout << "Error: Unknown operand \n";
}
}
return 0;
}
这是程序解释的文件:
10 * 20 / 5 * 10
Asparagas
这是输出:
10
10
正如您在此处看到的,值是重复的。有什么问题?