Output result string after numbers addition and subtraction

Source: Internet
Author: User

Title Description:By typing the formula for adding and reducing the number of positive integers within 100, write a program output result string. The input string is in the following format: operand 1 operator operand 2, operand and operator separated by a space.
Additional notes:1. The operand is a positive integer, and there is no need to consider the case of a calculated result overflow. 2. If the input calculation format is wrong, the output is "0".
Required Implementation functions:void arithmetic (const char *PINPUTSTR, long Linputlen, Char *poutputstr);
input pinputstr:Input String Linputlen:Input string length Outputpoutputstr: Output string, space has been opened up, and input string and so long;
<span style= "font-size:10px;" > #include <stdio.h> #include <stdlib.h> #include <string.h> #define Maxchar 10void Arithmetic (    const char *PINPUTSTR, long Linputlen, char *poutputstr) {int I, CNT = 0, A, b, result;    Char ch[1] = {' 0 '};    Char Op1[maxchar], Op[maxchar], Op2[maxchar], buffer[4];        for (i = 0; i < Linputlen; i++) if (pinputstr[i] = = ") cnt++;        if (cnt! = 2)//space number is not equal to 2 {strcat (POUTPUTSTR, ch);    Return    } sscanf (Pinputstr, "%s%s%s", OP1, OP, OP2); if (strlen (OP) > 1 | |            (op[0]! = ' + ' && op[0]! = '-'))        {strcat (POUTPUTSTR, ch);    Return } for (i = 0; i < strlen (OP1); i++) {if (Op1[i] < ' 0 ' | | op1[i] > ' 9 '            ) {strcat (POUTPUTSTR, ch);        Return }} for (i = 0; i < strlen (OP2); i++) {if (Op2[i] < ' 0 ' | |            Op2[i] > ' 9 ') {strcat (POUTPUTSTR, ch);        Return    }} A = Atoi (OP1);        b = atoi (OP2);            Switch (op[0]) {case ' + ': result = a + B;            Itoa (result, buffer, 10);            strcat (poutputstr, buffer);        Break            Case '-': result = a-B;            Itoa (result, buffer, 10);            strcat (poutputstr, buffer);        Break    Default:break;      }}int Main () {char pinputstr3[] = {"3 + 4"};    Char Poutputstr3[maxchar] = {0};      Arithmetic (PINPUTSTR3, strlen (PINPUTSTR3), POUTPUTSTR3);     printf (POUTPUTSTR3); return;} </span>

Knowledge points1.sscanf ()-Reads data in a string that matches the specified format. function Prototypes:
int sscanf (String str, String fmt, mixed var1, mixed var2 ...);
int scanf (const char *format [, argument] ...);
Description:
sscanf is similar to scanf, which is used for input, except that the latter takes a screen (stdin) as the input source, the former with a fixed string as the input source.
Reference:http://www.cnblogs.com/lyq105/archive/2009/11/28/1612677.html Reference:2.itoa ()-function Prototype: char *itoa (int value, char *string,int radix);
    Description:          Value: The data that you want to convert.
< Span style= "FONT-FAMILY:ARIAL,SANS-SERIF; line-height:28px ">       string: The address of the target string.
< Span style= "White-space:pre" >        radix: converted binary number, It can be 10 binary, 16 binary and so on.
    Example:

converts an integer to string usage itoa (i,num,10);

I----numbers that need to be converted into strings

num----A variable that saves a string after conversion

----Converts the cardinality of the number (that is, the binary). 10 means that the conversion is done by 10 binary . It could be 2,8,16 and so on. Your preferred type of production

Return value: A pointer to the string num

3.atoi () Use the opposite of iota ()

Reference Program: http://blog.csdn.net/poinsettia/article/details/9569987 Program:

Output result string after numbers addition and subtraction

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.