윈도우 환경에 MinGW(http://www.mingw.org/) 와 MSYS 설치하기
무료로 쓰는 C/C++ 컴파일러를 설치하기 위해서
윈도우에 cygwin을 설치한다는 것은 좀 무겁다는 생각이 든다.
이럴 때는 MinGW와 MSYS를 설치하면 간단한 C/C++ 개발 환경이 구축된다.
MinGW는 윈도우용 GCC Compiler Toolchain이고,
MSYS(Minal SYStem)로 윈도우에서 리눅스 쉘(Linux Shell)과 Binutils 환경을 제공한다.
1. MinGW 다운로드
- 자세한 설치 방법은 HOWTO Install the MinGW (GCC) Compiler를 참조
3. MinGW/bin, MSYS/1.0/bin 디렉토리를 환경변수의 PATH에 등록한다.
- 만약 PATH를 잡지 않고 MSYS를 먼저 실행하였다면,
$ export PATH=/c/MinGW/bin:/c/msys/1.0/bin:$PATH
하면 된다.
설치 후 컴파일 테스트
$ gcc -o hello hello.c
$ g++ -o hello2 hello2.cpp
$ g++ rpnSTL rpnSTL.cpp
[ STL을 이용하는 C++ 예제 소스: rpnSTL.cpp ]
- 만약 PATH를 잡지 않고 MSYS를 먼저 실행하였다면,
$ export PATH=/c/MinGW/bin:/c/msys/1.0/bin:$PATH
하면 된다.
설치 후 컴파일 테스트
$ gcc -o hello hello.c
$ g++ -o hello2 hello2.cpp
$ g++ rpnSTL rpnSTL.cpp
[ STL을 이용하는 C++ 예제 소스: rpnSTL.cpp ]
// stl/rpnSTL.cpp -- RPN (Reverse Polish Notation) Calculator
//
// Modified by PH Kim, 2009-02-08
//
// Which are different from the original source rpn.cpp ?
//
// 1. double instead of int
// 2. negative value possible
// 3. support the power operator ^
// 4. etc
//
// Compile:
// [Borland C++ 5.5.1] bcc32 rpnSTL.cpp
// [Visual C++ 2008] cl /EHsc rpnSTL.cpp
// [GNU C++] g++ rpnSTL.cpp
//
// Execute: rpnSTL
// ./rpnSTL
//
// Original from:
// http://www.fredosaurus.com/notes-cpp/examples/rpn/rpn.html
// ---------------------------------------------------------
// stl/rpn.cpp -- RPN (Reverse Polish Notation) Calculator
// Fred Swartz 2001-12-05, 2002-09-27
// ---------------------------------------------------------
//
// Modified by PH Kim, 2009-02-08
//
// Which are different from the original source rpn.cpp ?
//
// 1. double instead of int
// 2. negative value possible
// 3. support the power operator ^
// 4. etc
//
// Compile:
// [Borland C++ 5.5.1] bcc32 rpnSTL.cpp
// [Visual C++ 2008] cl /EHsc rpnSTL.cpp
// [GNU C++] g++ rpnSTL.cpp
//
// Execute: rpnSTL
// ./rpnSTL
//
// Original from:
// http://www.fredosaurus.com/notes-cpp/examples/rpn/rpn.html
// ---------------------------------------------------------
// stl/rpn.cpp -- RPN (Reverse Polish Notation) Calculator
// Fred Swartz 2001-12-05, 2002-09-27
// ---------------------------------------------------------
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <stdexcept>
#include <vector>
#include <string>
#include <cmath>
#include <stdexcept>
using namespace std;
//--- prototypes
double pop(vector<double>& stk);
void printVector(vector<double>& stk);
void clearVector(vector<double>& stk);
void printUsage();
double pop(vector<double>& stk);
void printVector(vector<double>& stk);
void clearVector(vector<double>& stk);
void printUsage();
//============================================================= main
int main() {
vector<double> opndStack; // vector used as operand stack
string token; // to read number or operator
int main() {
vector<double> opndStack; // vector used as operand stack
string token; // to read number or operator
cout << "RPN Calculator" << endl;
printUsage();
printUsage();
while (cin >> token) {
if (isdigit(token[0]) || (token.length() > 1 && (token[0] == '-' || token[0] == '+') && isdigit(token[1]))) { // if first is digit, it's number.
opndStack.push_back(atof(token.c_str())); // convert, push
printVector(opndStack);
} else { // If it's not a number, assume it's an operator
double left, right; // used by some operators as temps
if (isdigit(token[0]) || (token.length() > 1 && (token[0] == '-' || token[0] == '+') && isdigit(token[1]))) { // if first is digit, it's number.
opndStack.push_back(atof(token.c_str())); // convert, push
printVector(opndStack);
} else { // If it's not a number, assume it's an operator
double left, right; // used by some operators as temps
switch (token[0]) { // assume operators are one char
case '+': opndStack.push_back(pop(opndStack) + pop(opndStack));
break;
case '-': right = pop(opndStack); // get right operand
left = pop(opndStack); // get left operand
opndStack.push_back(left - right);
break;
case '*': opndStack.push_back(pop(opndStack) * pop(opndStack));
break;
case '/': right = pop(opndStack); // get right operand
left = pop(opndStack); // get left operand
opndStack.push_back(left / right);
break;
case '^': right = pop(opndStack); // get right operand
left = pop(opndStack); // get left operand
opndStack.push_back(pow(left, right));
break;
case '=':
case 'p':
break;
case 'c':
clearVector(opndStack);
break;
case 'q':
cout << "Quit" << endl;
exit(0);
break;
case 'h':
printUsage();
break;
default: throw domain_error("Undefined operator");
}
case '+': opndStack.push_back(pop(opndStack) + pop(opndStack));
break;
case '-': right = pop(opndStack); // get right operand
left = pop(opndStack); // get left operand
opndStack.push_back(left - right);
break;
case '*': opndStack.push_back(pop(opndStack) * pop(opndStack));
break;
case '/': right = pop(opndStack); // get right operand
left = pop(opndStack); // get left operand
opndStack.push_back(left / right);
break;
case '^': right = pop(opndStack); // get right operand
left = pop(opndStack); // get left operand
opndStack.push_back(pow(left, right));
break;
case '=':
case 'p':
break;
case 'c':
clearVector(opndStack);
break;
case 'q':
cout << "Quit" << endl;
exit(0);
break;
case 'h':
printUsage();
break;
default: throw domain_error("Undefined operator");
}
printVector(opndStack);
if (!opndStack.empty())
cout << " (" << token[0] << ") Top value: " << opndStack.back() << endl;
}
}
return 0;
}//end main
if (!opndStack.empty())
cout << " (" << token[0] << ") Top value: " << opndStack.back() << endl;
}
}
return 0;
}//end main
//============================================================== pop
// This utility function checks stack for underflow
// and pops (removes and returns) last element.
double pop(vector<double>& stk) {
if (stk.empty()) {
throw underflow_error("Stack underflow.");
}
double result = stk.back();
stk.pop_back();
return result;
}//end pop
// This utility function checks stack for underflow
// and pops (removes and returns) last element.
double pop(vector<double>& stk) {
if (stk.empty()) {
throw underflow_error("Stack underflow.");
}
double result = stk.back();
stk.pop_back();
return result;
}//end pop
double peek(vector<double>& stk) {
if (stk.empty()) {
throw underflow_error("Stack underflow.");
}
return stk.back();
}//end pop
if (stk.empty()) {
throw underflow_error("Stack underflow.");
}
return stk.back();
}//end pop
void printVector(vector<double>& stk) {
int n = stk.size();
cout << "[";
for (int i = 0; i < n; i++) {
cout << stk[i];
if (i < n - 1)
cout <<", ";
}
cout << "]" << endl;
}//end printVector
int n = stk.size();
cout << "[";
for (int i = 0; i < n; i++) {
cout << stk[i];
if (i < n - 1)
cout <<", ";
}
cout << "]" << endl;
}//end printVector
void clearVector(vector<double>& stk) {
while (!stk.empty()) {
stk.erase(stk.begin());
// stk.pop_back();
}
}//end clearVector
while (!stk.empty()) {
stk.erase(stk.begin());
// stk.pop_back();
}
}//end clearVector
void printUsage() {
cout << " +,-,*,/,^: opr, =,p: show top, c: clear stack, q: quit, h: help" << endl;
}//end printVector
cout << " +,-,*,/,^: opr, =,p: show top, c: clear stack, q: quit, h: help" << endl;
}//end printVector
/*
// stl/rpn.cpp -- RPN (Reverse Polish Notation) Calculator
// Fred Swartz 2001-12-05, 2002-09-27
#include <iostream>
#include <vector>
#include <string>
#include <stdexcept>
using namespace std;
// stl/rpn.cpp -- RPN (Reverse Polish Notation) Calculator
// Fred Swartz 2001-12-05, 2002-09-27
#include <iostream>
#include <vector>
#include <string>
#include <stdexcept>
using namespace std;
int pop(vector<int>& stk); //--- prototype to pop stack
//============================================================= main
int main() {
vector<int> opndStack; // vector used as operand stack
string token; // to read number or operator
int main() {
vector<int> opndStack; // vector used as operand stack
string token; // to read number or operator
while (cin >> token) {
if (isdigit(token[0])) { // if first is digit, it's number.
opndStack.push_back(atoi(token.c_str())); // convert, push
} else { // If it's not a number, assume it's an operator
int left, right; // used by some operators as temps
if (isdigit(token[0])) { // if first is digit, it's number.
opndStack.push_back(atoi(token.c_str())); // convert, push
} else { // If it's not a number, assume it's an operator
int left, right; // used by some operators as temps
switch (token[0]) { // assume operators are one char
case '+': opndStack.push_back(pop(opndStack) + pop(opndStack));
break;
case '-': right = pop(opndStack); // get right operand
left = pop(opndStack); // get left operand
opndStack.push_back(left - right);
break;
case '*': opndStack.push_back(pop(opndStack) * pop(opndStack));
break;
case '/': right = pop(opndStack); // get right operand
left = pop(opndStack); // get left operand
opndStack.push_back(left / right);
break;
default: throw domain_error("Undefined operator");
}
cout << "Result: " << opndStack.back() << endl;
}
}
return 0;
}//end main
case '+': opndStack.push_back(pop(opndStack) + pop(opndStack));
break;
case '-': right = pop(opndStack); // get right operand
left = pop(opndStack); // get left operand
opndStack.push_back(left - right);
break;
case '*': opndStack.push_back(pop(opndStack) * pop(opndStack));
break;
case '/': right = pop(opndStack); // get right operand
left = pop(opndStack); // get left operand
opndStack.push_back(left / right);
break;
default: throw domain_error("Undefined operator");
}
cout << "Result: " << opndStack.back() << endl;
}
}
return 0;
}//end main
//============================================================== pop
// This utility function checks stack for underflow
// and pops (removes and returns) last element.
int pop(vector<int>& stk) {
if (stk.empty()) {
throw underflow_error("Stack underflow.");
}
int result = stk.back();
stk.pop_back();
return result;
}//end pop
*/
// This utility function checks stack for underflow
// and pops (removes and returns) last element.
int pop(vector<int>& stk) {
if (stk.empty()) {
throw underflow_error("Stack underflow.");
}
int result = stk.back();
stk.pop_back();
return result;
}//end pop
*/
'소개와 설치 > C++' 카테고리의 다른 글
무료로 쓰는 Borland C/C++ 컴파일러 (0) | 2009.02.07 |
---|---|
ANSI/ISO 표준 C++ 라이브러리와 STL (0) | 2008.03.21 |