나는 파일을 읽는 데 argc, argv []를 사용하지는 않았지만 프로그램 요구 사항을 사용하기를 바랍니다. 내 문제는 헤더 파일의 파일을 읽고 대신 메인 파일을 처리하는 것입니다. main.cpp 외부에서 argc, argv [] 입력을 사용하는 방법이 있습니까?arg.c, argv를 내 main.cpp 파일 외부로 전달할 수 있습니까?
헤더 파일 "input.txt"등을 argv로 바꾸고 싶습니다.
하는 대신 경우 output.txt
void expression::convertToPostFix()
{
{
ofstream fout("output.txt");
stack<char> stack;
stringstream postfix;
while (!obj.empty()) {
stack.push('(');
fout << "InFix is:\t";
while (1) {
if ((obj.front() != ';') && (obj.front() != '.'))
{
const char current = obj.front();
cout << current;
fout << current;
obj.pop();
if (isspace(current)) {
}
else if (isalnum(current)) {
postfix << current;
}
else if ('(' == current) {
stack.push(current);
}
else if (isOperator(current)) {
char rightOperator = current;
while (!stack.empty() && isOperator(stack.top()) && precedence(stack.top(), rightOperator)) {
postfix << ' ' << stack.top();
stack.pop();
}
postfix << ' ';
stack.push(rightOperator);
}
else if (')' == current) {
while (!stack.empty() && '(' != stack.top()) {
postfix << ' ' << stack.top();
stack.pop();
}
stack.pop();
postfix << ' ';
}
}
else
{
obj.pop();
break;
}
}
while (!stack.empty() && '(' != stack.top()) {
postfix << ' ' << stack.top();
stack.pop();
}
stack.pop();
pfix = postfix.str();
postfix.str("");
cout << "\nPost fix is:\t" << pfix << endl << endl;
fout << "\nPost fix is:\t" << pfix << endl << endl;
}
}
}의 여기에 사용 하시겠습니까
(그것뿐만 아니라 여기에 사용 하시겠습니까)
expression::expression()
{
ifix = "";
pfix = "";
last = 0;
char chr;
ifstream fin;
fin.open("input.txt");
while (!fin.eof())
{
fin >> chr;
if (!fin.eof())
{
if (chr != ' ')
{
obj.push(chr);
}
}
}
}
그냥 통과하십시오. 'argv [1]'을 첫 번째 인수가 필요한 함수에 추가합니까? 물론 'argc> = 2'인지 확인한 후. –
BTW, 일반적인 코딩 지침은 소스 파일과 헤더 파일의 선언에 코드를 배치하는 것입니다. –