0

我正在编写一个简单的计算程序,但我无法获得任何有效的输出。我得到的只是一个颠倒的问号。此外,我在程序末尾有一个提示,询问用户是否要输入另一个计算。但是,当我输入计算时,控制台中会出现两次提示。有谁知道为什么会发生这些事情?最后,我只能使用 getchar 和 putchar 来处理输入和输出。在此先感谢您的帮助。

int addFunction( int, int);
int subtractFunction(int, int);
int multiplyFunction(int, int);
int modulusFunction(int, int);
float divideFunction(float, float);

int main(int argc, const char * argv[])
{

int num1 = 0, num2 = 0, result = 0;
char  continuePrompt,  iochar = 0, operator = 0;

do {
    iochar = getchar();
    getchar();


    if ((iochar >= 0) && (iochar <= 20000)) {
        num1 = iochar; 
    }

    if ((iochar == '+') || (iochar == '-') || (iochar == '*') || (iochar == '/') || (iochar == '%')) {
        operator = iochar; 
    }

    if ((num1 >= 0) || ((iochar >= 0) && (iochar <= 20000))){
        num2 = iochar;
    }

    switch (operator) {

        case '+':
           iochar  = addFunction(num1, num2);
            break;

        case '-':
            iochar = subtractFunction(num1, num2);
            break;

        case '*':
            iochar = multiplyFunction(num1, num2);
            break;

        case '%':
            iochar = modulusFunction(num1, num2);
            break;

        case '/':
            iochar = divideFunction(num1, num2);
            break;

    }

    putchar(iochar);



    printf("Would you like to make another calulation? (y or n)");
    scanf("%c", &continuePrompt);

} while (continuePrompt != 'n');
return 0;
}

int addFunction(int x, int y){
    return x + y;
}

int subtractFunction(int x, int y){
     return x - y;
}

int multiplyFunction(int x, int y){
    return x * y;
}

int modulusFunction(int x, int y){
    return x % y;
}

float divideFunction(float x, float y){
    return x / y;
}
4

2 回答 2

0

The function getchar and gets a character from the console and putchar puts a character to the console. The are not general input/output functions. Your code reads like you expect getchar to read in decimal representations of integers and putchar to print decimal representation of integers, but they don't work that way. Since you can only use getchar and putchar, you are going to have to write your own input/output methods with them to parse your inputs and display them correctly. So, first, figure out how to parse and output integers. Then use those methods where you're expecting to read or write integers (and floats if you have to display the estimated values for division). It may help to have a helper method that actually grabs the "current" numerical string from wherever you are in the expression.

于 2012-10-06T02:11:33.117 回答
0

一些基础...

字符值 '0' 不等于ascii中的整数值 0,它的整数值是 48。如果每个数字只有 1 位,则类似于:

char c = getchar();
// Assuming the user only will input a number...
int number = c - '0';

对于仅使用 getchar() 读取整数,我会执行以下操作:

#include <stdio.h>
#include <math.h> // for pow

int getint()
{
    char c;
    char buffer[255]; // way larger than an integer will ever be I think...
    int numlen = 0;
    int number = 0;
    int x;
    int multfornegative = 1;

    while ((c = getchar()) != '\n') {
        buffer[numlen++] = c;
    }

    for (x = numlen - 1; x >= 0; x--) {
        c = buffer[(numlen - 1) - x];
        if (c == '-') {
            multfornegative *= -1;
        } else {
            number += (c - '0') * (int)pow(10, x);
        }
    }

    return number * multfornegative;
 }

对于输出,你会做类似...

void putint(int number)
{
    char digit;
    int x;
    int start;

    if (number < 0) {
        putchar('-');
        number *= -1;
    }

    start = log(number) / log(10);

    for (x = start; x >= 0; x--) {
        digit = ((number / (int)pow(10, x)) % 10) + '0';
        putchar(digit);
    }
}

此外,尝试分解您的输入,您在循环中的输入方式最终会令人困惑,这会扰乱您的逻辑。

int num1;
char op;
int num2;
int ans;

do {
    num1 = getint();
    op = getchar(); getchar();
    num2 = getint();

    switch(op) {
        case '+': ans = num1 + num2; break;
        case '-': ans = num1 - num2; break;
        // And so on...
    }
    putint(ans);
while (1);

C Programming Language是一本非常适合学习 C 的书,由 C 的发明者自己编写。

于 2012-10-06T02:19:36.970 回答