소스 파일명: testArgumentsCPP.cpp
  1. #include <iostream>  // cout 함수와 endl 문자의사용을 위해
  2. #include <cmath>     // atof 함수 사용을 위해
  3. using namespace std;
  4. // argc는 명령행 인자 개수, argv는 명령행 인자 문자열의 배열
  5. int main(int argc, char *argv[]) {
  6.     int i;
  7.     double sum = 0.0;    // 초기화
  8.     // 명령행 인자(command-line argument) 개수 출력
  9.     cout << "Count of arguments: " << argc << endl;
  10.     for (i = 0; i < argc; i++) {
  11.         // 스트링을 배정밀도 부동소수점수로 변환하여 누적
  12.         sum += atof(argv[i]);
  13.     }
  14.     // 배정밀도 부동소수점수 값을 cout로 출력
  15.     cout << "The sum of arguments is " << sum << endl;
  16.     return 0;
  17. }


컴파일> cl -GX testArgumentsCPP.cpp

실행> testArgumentsCPP 1 2 3 4
Count of arguments: 5
The sum of arguments is 10

실행> testArgumentsCPP 1 2 3 4.1
Count of arguments: 5
The sum of arguments is 10.1





Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.

Posted by Scripter
,