我把它们用于切换语句时代码不能正常工作(Turbo C)

时间:2015-10-10 15:32:24

标签: c switch-statement

案例1,2,3和4是来自不同程序的代码,我将它们组合在一起以创建这一个转换程序。它们可以单独工作,但是当我使用开关将它们放在一起时,情况2和4不能按照它们的方式工作。请帮忙!

这是整个代码:
注意:我只包括案例2和4.

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#define MAX 1000

main() {
    double z, doub, r, n, count = 0, i = 0, frac;
    int num, p, ar[100], ch, fract = 0, bits = 0, whole = 0;
    long int j = 0;
    char hexaDecimal[MAX];
    int choice;
    clrscr();

    printf("\nType [1]: Decimal - Binary");
    printf("\nType [2]: Binary - Octal");
    printf("\nType [3]: Hexadecimal - Binary");
    printf("\nType [4]: Binary - Hexadecimal\n\n");

    printf("\nChoice: ");
    scanf("%d", &choice);
    printf("====================================\n");

    switch (choice) {

                case 2:
            /*BINARY TO OCTAL*/

            printf("Enter any binary number: ");
            scanf("%d", &ch);
            while ((ch = getchar()) != EOF && ch != '.' && ch != '\n') {
                switch (ch) {
                    case '0':
                        whole = whole * 2;
                        break;
                    case '1':
                        whole = whole * 2 + 1;
                        break;
                    default:
                        printf("Bad input. Try again.\n");
                        return 1;
                }
            }
            printf("\tOctaldecimal value: %lo", whole);

            /*fractional part is more tricky to align correctly*/
            if (ch == '.') {
                printf(".");
                while ((ch = getchar()) != EOF && ch != '\n') {
                    switch (ch) {
                        case '0':
                            fract = fract * 2;
                            bits++;
                            break;
                        case '1':
                            fract = fract * 2 + 1;
                            bits++;
                            break;
                        default:
                            printf("Bad input\n");
                            return 1;
                    }
                    if (bits == 3) {
                        printf("%o", fract);
                        fract = 0;
                        bits = 0;
                    }
                }
                if (bits) {
                    printf("%o\n", fract << (4 - bits));
                }
            }
            printf("\n");

            break;


                case 4:
            /*BINARY TO HEXADECIMAL*/

            fract = 0;
            bits = 0;
            printf("Enter any binary number: ");
            while ((ch = getchar()) != EOF && ch != '.' && ch != '\n') {
                switch (ch) {
                    case '0':
                        whole = whole * 2;
                        break;
                    case '1':
                        whole = whole * 2 + 1;
                        break;
                    default:
                        printf("Bad input\n");
                        return 1;
                }
            }
            printf("Hexadecimal value: %lX", whole);


            if (ch == '.') {
                printf(".");
                while ((ch = getchar()) != EOF && ch != '\n') {
                    switch (ch) {
                        case '0':
                            fract = fract * 2;
                            bits++;
                            break;
                        case '1':
                            fract = fract * 2 + 1;
                            bits++;
                            break;
                        default:
                            printf("Bad input\n");
                            return 1;
                    }
                    if (bits == 4) {
                        printf("%X", fract);
                        fract = 0;
                        bits = 0;
                    }
                }
                if (bits) {
                    printf("%X\n", fract << (4 - bits));
                }
            }
            printf("\n");

            break;
    }


    getch();
    return 0;
}

我不明白为什么输出变得不同。请帮忙!
提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

这里似乎有两个问题:

首先:您需要将scanf从%d更改为%c

case 2:
            /*BINARY TO OCTAL*/

            printf("Enter any binary number: ");
            //scanf("%d", &ch);
            //Made change here from %d to %c
            scanf("%c", &ch);

第二:你甚至没有任何scanf陈述来接受用户输入

case 4:
            /*BINARY TO HEXADECIMAL*/

            fract = 0;
            bits = 0;
            printf("Enter any binary number: ");

在为我更改scanf声明后,CASE2为我工作。使用gcc编译器。它应该适用于您的turbo C

相关问题