C language multiple operation string, output (not easy Ah, adjusted for 1 hours, just adjust it out ....) I hope the gods have a better way of pointing twos.

Source: Internet
Author: User
Tags sprintf

Enter a multi-operand string, output the result, support only arithmetic, and support only integers.

Parsing string operations (without parentheses)

int calculatorfinal (char *str)

{

JIEGUO[100][10] for storing parsed arrays

Char *p[100], jieguo[100][10];

int result, secondnumber, i = 0, j = 0;

Char tempnumber[20], op;

for (i = 0; i <; i++) {

P[i] = Jieguo[i];

}

i = 0;

Parse the string and put it into a two-dimensional array

while (*STR) {

if (*str >= ' 0 ' && *str <= ' 9 ') {

TEMPNUMBER[J] = *str;

j + +;

}else{

TEMPNUMBER[J] = ' + ';

strcpy (P[i], tempnumber);

i++;

for (int j = 0; J <; J + +) {

TEMPNUMBER[J] = ' + ';

}

Tempnumber[0] = *str;

strcpy (P[i], tempnumber);

i++;

Tempnumber[0] = ' + ';

j = 0;

}

str++;

}

strcpy (P[i], tempnumber);

i++;

for (int j = 0; J <; J + +) {

TEMPNUMBER[J] = ' + ';

}

Tempnumber[0] = *str;

strcpy (P[i], tempnumber);

printf ("* * * * parsing string result ********\n");

for (j = 0; J < i; J + +) {

Puts (P[j]);

//    }

printf ("* * * * * multiplication law ********\n");

for (int n = 0; n < i; n++) {

if (n% 2 = = 1) {

op = p[n][0];

if (op = = ' * ') {

result = Atoi (p[n-1]) * ATOI (p[n + 1]);

printf ("result =%d\n", result);

for (int m = n; m < i; m++) {

if (M = = N) {

printf ("result =%d\n", result);

sprintf (P[m-1], "%d", result);

Puts (p[m-1]);

}

To determine if this calculation is the last group, the words stop moving the array, not the words move

if (M + 2! = i) {

strcpy (P[m], p[m + 2]);

strcpy (p[m + 1], P[m + 3]);

}else if (m + 2 = = i) {

Each time an operation is performed, the array moves up and starts to detect ' * ', '/'

and reduce the array size by 2

n = 0;

I-= 2;

Break

}

}

}else if (op = = '/') {

result = Atoi (p[n-1])/atoi (P[n + 1]);

for (int m = n; m < i; m++) {

if (M = = N) {

printf ("result =%d\n", result);

sprintf (P[m-1], "%d", result);

Puts (p[m-1]);

}

To determine if this calculation is the last group, the words stop moving the array, not the words move

if (M + 2! = i) {

strcpy (P[m], p[m + 2]);

strcpy (p[m + 1], P[m + 3]);

}else if (m + 2 = = i) {

Each time an operation is performed, the array moves up and starts to detect ' * ', '/'

and reduce the array size by 2

n = 0;

I-= 2;

Break

}

}

}

}

}

printf ("* * * * * * multiplication----results ********\n");

for (j = 0; J < i; J + +) {

Puts (P[j]);

//    }

printf ("* * * * Add subtraction----results ********\n");

for (int n = 0; n < i; n++) {

if (n% 2 = = 1) {

op = p[n][0];

if (op = = ' + ') {

result = Atoi (p[n-1]) + atoi (p[n + 1]);

printf ("result =%d\n", result);

for (int m = n; m < i; m++) {

if (M = = N) {

printf ("result =%d\n", result);

sprintf (P[m-1], "%d", result);

Puts (p[m-1]);

}

To determine if this calculation is the last group, the words stop moving the array, not the words move

if (M + 2! = i) {

strcpy (P[m], p[m + 2]);

strcpy (p[m + 1], P[m + 3]);

}else if (m + 2 = = i) {

Each time an operation is performed, the array moves up and starts to detect ' * ', '/'

and reduce the array size by 2

n = 0;

I-= 2;

Break

}

}

}else if (op = = '-') {

result = Atoi (p[n-1])-atoi (P[n + 1]);

for (int m = n; m < i; m++) {

if (M = = N) {

printf ("result =%d\n", result);

sprintf (P[m-1], "%d", result);

Puts (p[m-1]);

}

To determine if this calculation is the last group, the words stop moving the array, not the words move

if (M + 2! = i) {

strcpy (P[m], p[m + 2]);

strcpy (p[m + 1], P[m + 3]);

}else if (m + 2 = = i) {

Each time an operation is performed, the array moves up and starts to detect ' * ', '/'

and reduce the array size by 2

n = 0;

I-= 2;

Break

}

}

}

}

}

return result;

}

C language multiple operation string, output (not easy Ah, adjusted for 1 hours, just adjust it out ....) I hope the gods have a better way of pointing twos.

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.