C语言的初学者功能

C语言的初学者功能

我的程序计算用户提供的两个数字的总和,差,商和乘积。在我的代码中,我正在main函数中使用print语句将这些信息提供给用户,但是,我试图找出一种方法来使用我可以简单调用的函数以打印出所有的结果。过程,给用户。我是新手,所以将不胜感激。非常感谢。

//declare functions
int firstuserinput(void);
int seconduserinput(void);
int calcSum(int, int);
int calcProduct(int, int);
int calcDifference(int, int);
int calcQuotient(int, int);

#include <stdio.h>

int main()
{
    int sum, product, difference, quotient, num1, num2;
    num1=firstuserinput();
    num2=seconduserinput();

    sum = calcSum(num1, num2);
    product = calcProduct(num1, num2);
    difference = calcDifference(num1, num2);
    quotient = calcQuotient(num1, num2);

    printf("\nThe sum of the two numbers: %d\n", sum );
    printf("The product of the two numbers: %d\n", product);
    printf("The difference of the two numbers: %d\n", difference);
    printf("The quotient of the two numbers: %d", quotient);

}
//Input Functions
int firstuserinput()
{
    int num1;
    printf("Please enter a number: ");
    scanf("%d", &num1);
    return num1;
}

int seconduserinput()
{
    int num2;
    printf("Please enter another number: ");
    scanf("%d", &num2);
    return num2;
}
//Process functions
int calcSum(int x, int y)
{
    return x+y;


}

int calcProduct(int x, int y)
{
    return x*y;


}

int calcDifference(int x, int y)
{
    return x-y;


}

int calcQuotient(int x, int y)
{
    return x/y;


}

最佳答案

创建一个获取结果并打印的函数:

void printResults(int sum, int product, int difference, int quotient) {
    printf("\nThe sum of the two numbers: %d\n", sum );
    printf("The product of the two numbers: %d\n", product);
    printf("The difference of the two numbers: %d\n", difference);
    printf("The quotient of the two numbers: %d", quotient);
}


在主要方面:

   ...
    printResults(sum, product, difference, quotient);
}

关于c - C语言的初学者功能,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42235239/

10-11 04:17