#8342: C 語言 問題 +-* 計算 救命


frank66 (frank)


http://acm.cs.nthu.edu.tw/problem.php?pid=1814

http://acm.cs.nthu.edu.tw/problem.php?pid=1815

 

有人會寫這兩題嗎 我下密善我自己寫的 錯了 但不知道怎麼改 又有點急著要 有人可以幫忙嗎

 

 /*

Input:

2

+ * 2 - 5 3 * 3 4

+ 5 - * - 9 3 2 1

 

Output:

CASE 1:

((2*(5-3))+(3*4))=16

CASE 2:

(5+(((9-3)*2)-1))=16

*/

#include <stdio.h>

#include <ctype.h>

/* Uncomment the following #define before you submit the code to OJ. */

// #define ONLINE_JUDGE

int calculate(void);

 

int main(void)

{

    int ncase, i;

 

    scanf("%d\n", &ncase);

    for (i=1; i<=ncase; i++) {

        printf("CASE %d:\n", i);

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

        while (getchar()!='\n');

    }

}

 

int calculate() {

    int C;

    int X, Y, Z;

 

    while (1) {

        C = getchar();

        if (isdigit(C) || C=='+' || C=='-' || C=='*' || C=='\n') break;

    }

 

    if (C=='\n') {

        return 0;

    }

    if (C=='+') {

X=calculate();

Y=calculate();

Z=X+Y;

return Z;

    }

    if (C=='-') {

      X=calculate();

Y=calculate();

Z=X-Y;

return Z;

    }

    if (C=='*') {

     X=calculate();

Y=calculate();

Z=X*Y;

return Z;

    }

    if (isdigit(C)) {

        ungetc(C, stdin);

        scanf("%d", &Z);

        printf("%d", Z);

        return Z;

    }

 

}


----------------- 

/*

Input:

**--**-*

-**-**

 

Output:

CASE 1:

**00**0*

**00**1*

**01**0*

**01**1*

**10**0*

**10**1*

**11**0*

**11**1*

CASE 2:

0**0*

0**1*

1**0*

1**1*ss

*/

#include <stdio.h>

#include <string.h>

/* Uncomment the following #define before you submit the code to OJ. */

// #define ONLINE_JUDGE

char bits[33];

char mask[33];

void showbit(int n);

int main(void)

{

    int ncase, i;

 

 

    ncase = 1;

    while (scanf("%32s", mask)!=EOF) {

        printf("CASE %d:\n", ncase);

        showbit(strlen(mask));

        ncase++;

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

    {

    bits[i]=0;

mask[i]=0;

    }

    }

    return 0;

}

 

void showbit(int n)

{

    int len;

    len = strlen(mask);

    if (n>0) {

        if(mask[n]=='*'){

bits[n]='*';

showbit(n-1);

        }

        else{

            bits[n]='0';

            showbit(n-1);

            bits[n]='1';

            showbit(n-1);

        }

 

    } else {

        printf("%s\n", bits);

    }

}

---------------------------------------------- 

#8343: Re:C 語言 問題 +-* 計算 救命


frank66 (frank)


 以解決 不知道怎麼刪掉問題@@.

 

 

http://acm.cs.nthu.edu.tw/problem.php?pid=1814

http://acm.cs.nthu.edu.tw/problem.php?pid=1815

 

有人會寫這兩題嗎 我下密善我自己寫的 錯了 但不知道怎麼改 又有點急著要 有人可以幫忙嗎

 

 /*

Input:

2

+ * 2 - 5 3 * 3 4

+ 5 - * - 9 3 2 1

 

Output:

CASE 1:

((2*(5-3))+(3*4))=16

CASE 2:

(5+(((9-3)*2)-1))=16

*/

#include

#include

/* Uncomment the following #define before you submit the code to OJ. */

// #define ONLINE_JUDGE

int calculate(void);

 

int main(void)

{

    int ncase, i;

 

    scanf("%d\n", &ncase);

    for (i=1; i<=ncase; i++) {

        printf("CASE %d:\n", i);

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

        while (getchar()!='\n');

    }

}

 

int calculate() {

    int C;

    int X, Y, Z;

 

    while (1) {

        C = getchar();

        if (isdigit(C) || C=='+' || C=='-' || C=='*' || C=='\n') break;

    }

 

    if (C=='\n') {

        return 0;

    }

    if (C=='+') {

X=calculate();

Y=calculate();

Z=X+Y;

return Z;

    }

    if (C=='-') {

      X=calculate();

Y=calculate();

Z=X-Y;

return Z;

    }

    if (C=='*') {

     X=calculate();

Y=calculate();

Z=X*Y;

return Z;

    }

    if (isdigit(C)) {

        ungetc(C, stdin);

        scanf("%d", &Z);

        printf("%d", Z);

        return Z;

    }

 

}


----------------- 

/*

Input:

**--**-*

-**-**

 

Output:

CASE 1:

**00**0*

**00**1*

**01**0*

**01**1*

**10**0*

**10**1*

**11**0*

**11**1*

CASE 2:

0**0*

0**1*

1**0*

1**1*ss

*/

#include

#include

/* Uncomment the following #define before you submit the code to OJ. */

// #define ONLINE_JUDGE

char bits[33];

char mask[33];

void showbit(int n);

int main(void)

{

    int ncase, i;

 

 

    ncase = 1;

    while (scanf("%32s", mask)!=EOF) {

        printf("CASE %d:\n", ncase);

        showbit(strlen(mask));

        ncase++;

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

    {

    bits[i]=0;

mask[i]=0;

    }

    }

    return 0;

}

 

void showbit(int n)

{

    int len;

    len = strlen(mask);

    if (n>0) {

        if(mask[n]=='*'){

bits[n]='*';

showbit(n-1);

        }

        else{

            bits[n]='0';

            showbit(n-1);

            bits[n]='1';

            showbit(n-1);

        }

 

    } else {

        printf("%s\n", bits);

    }

}

----------------------------------------------