![](/img/trans.png)
[英]In C++ any other way for a function to change a variable in main()?
[英]C++: Easiest way to access main variable from function?
我目前正在处理在main()中初始化的字符串,如果我尝试使其全局化,出于某种原因,该字符串会变得怪异(它变成非字符字符串)。 我想知道我是否可以在程序中声明一个函数来访问该变量...该函数本身只能在main或从main调用的函数中执行,但是编译器在main()之前看不到变量名到达了。
这是相关代码的简明版本:
string getCommand(int input_pos,string inputstring)
{
int temp_paren=0;
int begin_pos = stdinstring.rfind("(",input_pos);
int len = 0;
while (temp_paren>0 && len < 10)
{
if (stdinstring.substr(begin_pos+len,1)=="(") {temp_paren++;}
if (stdinstring.substr(begin_pos+len,1)==")") {temp_paren--;}
len++;
}
return stdinstring.substr(begin_pos,len);
}
int main(void) {
string stdinstring = "";
}
我一直在考虑前向声明,每次使用它时都将变量手动传递给函数,甚至创建一个类来保存该变量...解决这个问题的最简单方法是什么? 理想情况下,我将有足够的时间将其设置为全局变量,并找出出现问题的原因/原因,但是我只需要完成此操作即可。 感谢您提供的所有帮助。
编辑 :对于任何有兴趣的人:如果我尝试将字符串设置为全局字符串,这将会发生。 所有字符都变成“”(即使可见,也不知道)
编辑 :好的,这是我的代码的更完整版本。 另外,我在上面尝试粘贴的符号是带有“ 0 0 0 1”(ASCII字符代码?)的框。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<stdint.h>
//#include<regex>
#define PI M_PI
#define VERBOSE 1
using namespace std;
string stdinstring;
template <class dataclass>
struct linkm {
dataclass value;
linkm *next;
};
template <class dataclass>
class linklist
{
public:
linklist()
{top = NULL;}
~linklist()
{}
void push(dataclass num)
{
linkm<dataclass> *temp = new linkm<dataclass>;
temp->value = num;
temp->next = top;
top = temp;
}
dataclass pop()
{
if (top == NULL)
return 0;
linkm<dataclass> * temp;
temp = top;
dataclass value;
value = temp->value;
top = temp->next;
delete temp;
return value;
}
bool isEmpty()
{
if (top == NULL)
return 1;
return 0;
}
private:
linkm<dataclass> *top;
};
double evaluateExpression(string expression)
{
// sample expression : (* (/ a 1) (+ b 2))
if (expression.substr(0,2)=="(+") {cout << "add found"<<endl;}
else if (expression.substr(0,2)=="(-") {cout << "sub found"<<endl;}
else if (expression.substr(0,2)=="(*") {cout << "mult found"<<endl;}
else if (expression.substr(0,2)=="(/") {cout << "div found"<<endl;}
else if (expression.substr(0,2)=="(sin") {cout << "sin found"<<endl;}
else if (expression.substr(0,2)=="(cos") {cout << "cos found"<<endl;}
else {cout << "Error: invalid operation";} // or is it just a number?
}
string getCommand(int input_pos,string inputstring)
{
int temp_paren=0;
int begin_pos = stdinstring.rfind("(",input_pos);
int len = 0;
while (temp_paren>0 && len < 10)
{
if (stdinstring.substr(begin_pos+len,1)=="(") {temp_paren++;}
if (stdinstring.substr(begin_pos+len,1)==")") {temp_paren--;}
len++;
}
return stdinstring.substr(begin_pos,len);
}
class symContainer
{
public:
string index[500];
float value[500];
int currindex;
symContainer () { currindex = 0 ; }
void add(string id,float invalue)
{
index[currindex] = id;
value[currindex] = invalue;
currindex++;
}
float get(string id)
{
int i=0;
while (i<currindex)
{
if(id==index[i]) {return value[i];}
i++;
}
cout << "Invalid input - an unassigned symbol was requested:" << id << endl;
exit(2);
}
};
struct transform { int type; double arguments[4]; } ;
struct point { double x; double y; } ;
struct drawing { linklist<point> points; linklist<transform> transforms; void applyTransforms(linklist<transform> * trsptr) {} } ;
struct group : public drawing { linklist<int> drawings; void transform(linklist<transform> * trsptr) {} } ;
struct symbol {string index; double value;};
char getNext() { //for the lookaround function. has hard-filters, like replacing newlines/tabs with spaces
char temp = getchar();
if (temp=='\n') {temp=' ';}
if (temp=='\t') {temp=' ';}
return temp;
}
int main(void) {
stdinstring="a";
char command[20], args[2048];
int commandindex=0; //'i' for what command we're on
int stdinsize=2;
double argsArray[8];
bool filled=0;
int parenlevel = 0;
symContainer symbol;
//1 is the character that will be written at the end of each loop. 0 and 2 are 1 char ahead/behind, respectively
char c_lookaround[2];
c_lookaround[0]=NULL;
c_lookaround[1]=getNext();
c_lookaround[2]=getNext();
unsigned long i=0;
bool write=0;
while( c_lookaround[2] != EOF )
{
write=1;
// Lookaround logic goes here. (Clearing duplicate whitespaces, newlines, and the like)
while ( c_lookaround[1]==' ' && c_lookaround[2]==' ' ) {c_lookaround[2]=getNext();}
while (c_lookaround[0]=='(' && c_lookaround[1]==' ' ) {c_lookaround[1]=c_lookaround[2]; c_lookaround[2]=getNext();}
while ( c_lookaround[1]==' ' && c_lookaround[2]==')' ) {c_lookaround[1]=c_lookaround[2]; c_lookaround[2]=getNext();}
while (c_lookaround[0]==NULL && c_lookaround[1]==' ' ) {c_lookaround[1]=c_lookaround[2]; c_lookaround[2]=getNext();}
//while (c_lookaround[0]==')' && c_lookaround[1]==' ' && c_lookaround[2]=='(' ) {c_lookaround[1]=c_lookaround[2]; c_lookaround[2]=getNext();}
//while (c_lookaround[0]==')' && c_lookaround[1]==' ' && c_lookaround[2]=='\0' ) {cout<<"aa";c_lookaround[1]=c_lookaround[2]; c_lookaround[2]=getNext();}
if (c_lookaround[0]=='(' && c_lookaround[1]==':' && c_lookaround[2]=='=')
{
getCommand(i,stdinstring);
}
//Determine current parentheses level
if (c_lookaround[1] == '(') { parenlevel++;}
if (parenlevel==0) {write=0;}
if (c_lookaround[1] == ')') { parenlevel--;}
//Write the character
if (write) {stdinstring.push_back(c_lookaround[1]);}
cout << stdinstring<< endl;
//Advance the tape!
i++;
c_lookaround[0]=c_lookaround[1];
c_lookaround[1]=c_lookaround[2];
c_lookaround[2]=getNext();
}
stdinsize = i;
}
如果要从main()
调用getCommand()
,则应该可以传递变量。
int main(void) {
string stdinstring = "";
string answer = getCommand(0, stdinstring);
}
如果要从其他地方调用getCommand()
,则必须将变量从main()
传递给该函数,然后再传递给getCommand()
。 同样,您应该能够使它成为全局的,但是如果没有代码,我不知道为什么您不能这样做。
//stdinstring = "a" in the case (how is this set to anything but it?
string getCommand(int input_pos,string inputstring)
{
int temp_paren=0;
int begin_pos = stdinstring.rfind("(",input_pos); //This equals -1
int len = 0;
while (temp_paren>0 && len < 10) //There is no ( so the loop terminates with len=10
{
if (stdinstring.substr(begin_pos+len,1)=="(") {temp_paren++;}
if (stdinstring.substr(begin_pos+len,1)==")") {temp_paren--;}
len++;
}
return stdinstring.substr(begin_pos,len); //returns the first 10 chars of stdinstring which would be "a"
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.