多次调用该函数

时间:2014-08-04 13:26:12

标签: function

这是我的代码。

#include <stdio.h>
#include <conio.h>

double fnCalculate(void);

    int main(void){

         float grosspay, netpay, tax, deduction, regular, overtime;
         char choice;


           printf("Enter:\n");
           printf("Regular:\t\n");
           scanf("%f", &regular);
           printf("Overtime:\t\n");
           scanf("%f", &overtime);
           printf("Deduction:\t\n");
           scanf("%f", &deduction);
           printf("Grosspay= %.2f\n", grosspay = (regular + overtime)-deduction);

           printf("Tax:\t\n");
           scanf("%f", &tax);
           printf("netpay= %.2f\n\n", grosspay - tax);
                double fnCalculate(){
                    printf("Enter:\n");
                    printf("Regular:\t\n");
                    scanf("%f", &regular);
                    printf("Overtime:\t\n");
                    scanf("%f", &overtime);
                    printf("Deduction:\t\n");
                    scanf("%f", &deduction);
                    printf("Grosspay= %.2f\n", grosspay = (regular + overtime)-deduction);

                    printf("Tax:\t\n");
                    scanf("%f", &tax);
                    printf("netpay= %.2f\n\n", grosspay - tax);
                    }
                      printf("Do you want to calculate again? 1-yes, 0-no\n");
                      scanf("%s", &choice);
                            if(choice == '1'){
                            return fnCalculate();
                                     }
                            else if(choice == '0'){
                            printf("Bye!");
                                     }
   getch();

}

我只想让这个过程重复无数次。但问题是,我只能用这个代码再次计算两次,所以,我应该怎么做?

我使用的是scanf,因为它是我们教授喜欢的。我很感激我的代码的其他更正,因为我只是c编程的初学者。

1 个答案:

答案 0 :(得分:0)

你会想要使用某种循环:

http://www.cprogramming.com/tutorial/c/lesson3.html

如果你想要处理的是一个不确定的乘法数,你将需要使用带有某种哨兵的'WHILE'循环。

我注意到你还声明了一个函数'fnCalculate',它在你声明之前完全按照你在主块中所做的那样。您可以在函数内移动变量声明,然后将主标题和fnCalculate声明之间的所有代码减少为对fnCalculate()的单个调用。请记住,函数的要点是封装您想要重用的代码。此外,您声明函数'fnCalculate'返回double,但它当前不返回任何内容。如果您真的希望它返回netpay值,请确保明确地这样做(例如,返回grosspay-tax;)。否则,将该函数声明为void。

最终产品看起来像:

#includes...
void fnCalculate(void) {
   float grosspay, netpay, tax, deduction, regular, overtime;
   printf("Enter:\n");
   printf("Regular:\t\n");
   scanf("%f", &regular);
   printf("Overtime:\t\n");
   scanf("%f", &overtime);
   printf("Deduction:\t\n");
   scanf("%f", &deduction);
   printf("Grosspay= %.2f\n", grosspay = (regular + overtime)-deduction);
   printf("Tax:\t\n");
   scanf("%f", &tax);
   printf("netpay= %.2f\n\n", grosspay - tax);
   // here is where you'd return. I've chosen void, so no return.
}

int main (void) {
   // declare sentinel
   int continue = 1;
   while (continue) { 
      // note, 1 will evaluate to true, 0 will evaluate to false
      // function does the bulk of our work
      fnCalculate();
      // modify our sentinel dependent on user input
      printf("Do you want to calculate again? 1-yes, 0-no\n");
      scanf("%s", &continue);
   }
   print("Bye!");
   return 0;
}

另外,请注意,当括号对齐时,代码更容易阅读。

希望这有帮助。

史蒂夫