C Language Processing command line input

Source: Internet
Author: User
Tags mul strcmp

In the C language, we often encounter situations where we want to handle the command line. When the C language string is often difficult to deal with, eventually there are a variety of strange errors. An example of an algorithm instruction is presented, which illustrates the framework of the C language command-line processing. The algorithm directives are as follows:

algorithm instruction
Addition ADD OP1 OP2
Subtraction SUB OP1 OP2
Multiplication MUL OP1 OP2
Division DIV OP1 OP2

expected effect : The program waits for the user to enter a user instruction, outputs the correct result when the program has typed the algorithm directive, and outputs an error message for the response if an illegal operation occurs.
In the main function, the code framework for the handler do_line processing a row of data data obtained by the command line:

1  while (Fgets (buf, MAXLINE, stdin)! = NULL) 2 {3     if (Do_line (BUF)) 4          Break ;   5 }

When Do_line returns a value other than 0, the program exits, but returns 1 o'clock, and the program continues to run. Note that the Fgets function gets the data for the current row will contain ' \ n ', so the BUF string ends with ' \ n ', ' two characters '. The processing of each row is defined in the Do_line_core function:

1Flag = Get_token (cmd, line, &POS);2 if(flag) {return 0;}//illegal command, re-entry3 if(strcmp ("Q", cmd) = =0)return 1;//Exit Program4     5 if(strcmp ("Add", cmd) = =0) ...6 Else if(strcmp ("Sub", cmd) = =0) ...7 Else if(strcmp ("Mul", cmd) = =0) ...8 Else if(strcmp ("div", cmd) = =0) ...9 Else return 0;//unknown command, re-enter

Get_token gets the next command or operand, where POS represents the current address of the read. When the command obtained is illegal (empty), 1 is returned, otherwise 0 is returned.

1 intGet_token (Char*token,Char*line,int*POS)2 {3     /*Save the current token pointer*/4     Char*token_saved =token;5     /*Remove the opening space symbol*/6      while(Isspace (Line[*pos]) && Line[*pos]) (*pos) + +;7     /*gets the next command or operator at the beginning of the POS position*/8      while(!isspace (Line[*pos]) && line[*POS])9     {Ten*token = line[*POS]; Onetoken++; A(*pos) + +; -     } -*token = ' \0'; the     /*string is empty, return 1*/ -     if(strcmp (Token_saved, "") = =0)return 1; -     return 0; -}

Simplified :
* If you use global variables to describe the data for each row, you can reduce the parameters of the function.
* If you are in a *nix environment, you can use setjmp, longjmp to simplify processing logic 1.

    1. Advanced Programming for the UNIX environment, chapter 7th, "setjmp functions and LONGJMP functions".?

C Language Processing command line input

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.