Last active
May 8, 2020 07:23
-
-
Save t-nissie/ddb0f5360ca147469c0e8deb7735b97d to your computer and use it in GitHub Desktop.
コマンドライン引数に与えられた数組のコンマで区切られた整数 (csv) を読み取るプログラムをC++11, Fortran, Rubyで
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// csvcxx.cpp | |
//// | |
#include <iostream> | |
#include <sstream> | |
#include <string> | |
#include <vector> | |
int main(int argc, char **argv) | |
{ | |
for (int i=1;i<argc;++i) { | |
std::istringstream iss(argv[i]); | |
std::string field; | |
std::vector<int> values; | |
while (getline(iss,field,',')) values.push_back(std::stoi(field)); | |
for (auto itr=values.begin(); itr!=values.end(); ++itr) std::cout << " " << *itr; | |
std::cout << std::endl; | |
} | |
return 0; | |
} | |
//Local variables: | |
// compile-command: "g++ -Wall -std=c++11 -o csvcxx csvcxx.cpp && ./csvcxx 0,2,4 1,3,5,7,9" | |
//End: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
! csvfortran.f | |
! Author: Takeshi NISHIMATSU | |
!! | |
program csvfortran | |
implicit none | |
integer,allocatable :: values(:) | |
integer,parameter :: STRLEN=100 | |
integer :: i,j,n,argn | |
character(len=STRLEN) :: str | |
argn = command_argument_count() | |
do j=1,argn | |
call get_command_argument(j,str) | |
!write(6,*) str | |
n=0 | |
do i=1,STRLEN | |
!write(6,*) str(i:i) | |
if (str(i:i)==',') n=n+1 | |
end do | |
n=n+1 | |
!write(6,*) n | |
allocate(values(n)) | |
read(str,*) values(:) | |
write(6,*) values(:) | |
deallocate(values) | |
end do | |
end program csvfortran | |
!Local variables: | |
! compile-command: "gfortran -ffree-form -Wall -o csvfortran csvfortran.f && ./csvfortran 0,2,4 1,3,5,7,9" | |
!End: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
## | |
ARGV.each{|a| | |
values=a.split(",").map(&:to_i) | |
p values | |
} | |
#Local variables: | |
# compile-command: "./csvruby.rb 0,2,4 1,3,5,7,9" | |
#End: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment