开发者

Reading variables from command line

开发者 https://www.devze.com 2023-01-29 03:31 出处:网络
Hello(again :) ) I want to know how to read something from command line like: OpsComp= executable resultados=file.txt

Hello(again :) ) I want to know how to read something from command line like:

OpsComp= executable resultados=file.txt

$ OpsComp (23+45i)+(12+23i) resultados The answer is: 35+68i

The program is supposed to read as following: a=23 b=45 c=12 d=23 operand=+ 开发者_开发问答make the operation and then print the operation on a file.txt(resultados)... I would like to know, how(when im reading b and d) could I not scan the 'i'... Thanks in advance,i know Im such a nuisance with my questions haha

EDIT So what Im trying to do is to read (23+45i) from the command line, what im trying to ask is..how can I read the 23 as 1 variable, + as another and 45 as another(excluding the 'i and the parenthesis') from the command line


Reading arguments from the command line is done in different ways in different languages.

In C/C++/Java/C# and probably some others, you get them from your main-function:

int main(int argc, char *argv[]) {
   // do something (argc in the number of arguments, argv is a list of the arguments themselves)
}

In Ruby you do it with the ARGV-object:

ARGV.each do |a|
  # Do something for every argument "a"
end

Etc for other languages.

For the second part of your question; splitting the arguments into logical parts (numbers, operators and so on) you'll have to write a little parser. If it's just going to be simple math, a regular expression will probably do. Once again, how you use one of those depends on your language, but the idea is the same in all of them. There are plenty of tutorials for all languages if you google it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号