gpt4 book ai didi

C++ 变量增量停止程序

转载 作者:行者123 更新时间:2023-12-01 19:11:19 27 4
gpt4 key购买 nike

我正在尝试编写一个词法分析器程序。但每次我在第 152 行增加变量“varCount”时,我的程序就会崩溃。

更具体地说,只有当我将其增加超过 1 时,它才会崩溃。我已经尝试重命名它,但它仍然不起作用。我不知道我做错了什么。

这是我的代码

#include<bits/stdc++.h>
using namespace std;

string presentKeyWords[100], presentVariables[100], operators[100] ;
int pkcount=0, varCount=0, opcount=0;


int main()
{
ifstream inputFile("input.txt");
ofstream outputFile("output.txt");
string line;
bool s_cmt = 0;

if(inputFile.is_open())
{
while( getline(inputFile, line) )
{
char arrline[line.length()+1] ;
strcpy( arrline, line.c_str() );

/* Removing comments */
// traverse each character of a line
for(int i=0; i<line.length(); i++)
{
if(arrline[i]=='/' && arrline[i+1]=='/'){
s_cmt = 1;
break; // skip if single-line cmt
}
else if(arrline[i]=='/' && arrline[i+1]=='*'){

while( getline(inputFile, line) ){

char arrline[line.length()+1] ;
strcpy( arrline, line.c_str() );
int x = 0;
for(int i=line.length(); i>0; i--){
if(arrline[i]=='/' && arrline[i-1]=='*'){
x = 1;
break;
}
}
if(x==1){
break;
}
}
break;

}
else{
s_cmt = 0;
outputFile << arrline[i];
}
}




/* Detecting variables and keywords */
if(s_cmt==0)
{
string words[100], var;
// break the line into words
int c=0;
istringstream streamOfString(line);
while( getline(streamOfString, var, ' ') ){
words[c]=var;
c++;
}

// loop through the words
for(int i=0; i<c; i++)
{
// Checking for special characters
int specialCharFound = 0;
string listOfOperators[] = { "+", "Plus",
"-", "Minus",
"*", "Multiplication",
"/", "Division",
"=", "Equal",
"~", "Difference",
"!", "Exclamation",
".", "dot",
"@", "at",
"#", "Hash",
"$", "Dollar sign",
"%", "Modulus",
"^", "Carrot",
"&", "AND",
"?", "Question Mark",
"<", "Greater than",
">", "Less than",
",", "Comma",
":", "Colon",
";", "Semicolon"
};

int sn = sizeof(listOfOperators)/sizeof(listOfOperators[0]);

for(int iter=0; iter<sn; iter+=2)
{
if( listOfOperators[iter]==words[i] )
{
operators[opcount] = listOfOperators[iter] ;
operators[opcount+1] = listOfOperators[iter+1] ;
opcount+=2;
specialCharFound = 1;
}
}

if(specialCharFound==1){
continue;
}


/* check for keyWords */
int keyWordFound = 0;
string keyWords[] = {"bool", "int", "float", "char", "if", "for", "while", "string", "break", "continue"};

for(int j=0; j<10; j++){
if(words[i]==keyWords[j]){
presentKeyWords[pkcount] = words[i] ;
pkcount++;
keyWordFound = 1;
break;
}
}

if(keyWordFound==1){
continue;
}


/* check for variables */
if( isdigit(words[i][0]) ){
//cout << words[i] << endl ;
}
else{
string tempVar = words[i] ;
string dType ;
for(int j=0; j<varCount; j++){
if( presentVariables[j]==tempVar ){
dType = presentVariables[j+1];
}
}
if(dType.empty() && pkcount!=0){
dType = presentKeyWords[pkcount-1];
}
presentVariables[varCount] = tempVar ;
presentVariables[varCount+1] = dType ;
varCount+=2;
}



}
}


outputFile << endl;
}
}





outputFile << "\n\n" << "Keywords \n---------- \n";
for(int i=0; i<pkcount; i++){
outputFile << presentKeyWords[i] << endl;
}
outputFile << "\n\n\n" ;

outputFile << "\n\n" << "Variables \n---------- \n";
for(int i=0; i<varCount; i+=2){
outputFile << presentVariables[i] << " = " << presentVariables[i+1] << endl;
}

outputFile << "\n\n" << "Operators \n---------- \n";
for(int i=0; i<opcount; i+=2){
outputFile << operators[i] << " " << operators[i+1] << endl;
}


return 0;
}

请有人帮忙...

最佳答案

(第 2 行)presentVariables[100]最多可容纳 100 个元素;而varCount可以走得更远。我尝试presentVariables[1000]并且该程序在我的计算机上运行良好(输入很小)。

您应该使用 vector<string>

关于C++ 变量增量停止程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49640217/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com