-3

1 学期の c 課題の関数を作成する必要があります。誰かが私を助けることができれば、私はすべての課題を書きました。ユーザー入力を検証するだけです。これは先生が私にくれたものです...これが十分な情報でない場合は、もっと投稿できます

double get_double(void) 関数を使用して、各費用と収入の入力を検証する必要があります。条件が満たされるまで、ユーザーに数値の入力を求め続けます。値は数値で、0 以上である必要があります。数字の前後に文字入力がある場合、その入力は無効と見なされます。

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

double get_expenses(double* pSchool, double* pLiving, double* pTransp, double* pOther); /* function prototype */
double get_income(double* pEmploy, double* pOther);                                     /* function prototype */
double display_report(double School, double Living, double Transp, double OtherExp, double Employ, double OtherInc);  /* function prototype */
char get_char(char* pRecalculate);   /* function prototype */
void clear_buffer(void);   /* function prototype */
double get_double(void);   /* function prototype */

main() {

double school, living, transp, otherExp;
double employ, otherInc;
char recalculate;
int counter = 1;



while (counter > 0){  
    printf("Student Budget Planner\n");
    printf("~~~~~~~~~~~~~~~~~~~\n");
    printf("Input Set: %d\n\n", counter);             
    get_expenses(&school, &living, &transp, &otherExp);
    get_income(&employ, &otherInc);
    display_report(school, living, transp, otherExp, employ, otherInc);
    clear_buffer();
    get_char(&recalculate);
     if (recalculate == 'Y' || recalculate == 'y'){
     counter = counter + 1;
        } else {
               counter = 0;
               }

}                                      
return 0;
}


double get_expenses(double* pSchool, double* pLiving, double* pTransp, double* pOther) {
*pSchool = *pLiving = *pTransp = *pOther = 0;

double tuition, textbooks, supplies, rent, utilities, phone, groceries;
double entertainment, transportation, car, insurance, gas, other;

printf("School Expenses:\n");
printf("~~~~~~~~~~~~~~\n");
printf("Tuition (per semester): ");
scanf("%lf", &tuition);
printf("\nTextbooks(per semester): ");
scanf("%lf", &textbooks);
printf("\nSupplies: ");
scanf("%lf", &supplies);

*pSchool = (tuition / 4) + (textbooks / 4) + supplies;

printf("\n\nLiving Expenses:\n");
printf("~~~~~~~~~~~~~\n");
printf("Residence/Rent/Mortgage: ");
scanf("%lf", &rent);
printf("\nUtilities: ");  
scanf("%lf", &utilities);           
printf("\nPhone/Internet: "); 
scanf("%lf", &phone);                
printf("\nGroceries/Eating out: ");
scanf("%lf", &groceries);                 
printf("\nEntertainment: "); 
scanf("%lf", &entertainment);

*pLiving = rent + utilities + phone + groceries + entertainment;

printf("\n\nTransportation: \n");              
printf("~~~~~~~~~~~~~\n");
printf("Public Transportation: "); 
scanf("%lf", &transportation);
printf("\nCar: ");
scanf("%lf", &car);
printf("\nAuto Insurance: ");
scanf("%lf", &insurance);
printf("\nGas/Maintenance: ");
scanf("%lf", &gas);

*pTransp = transportation + car + insurance + gas;

printf("\n\nOther: \n");
printf("~~~~~\n");
printf("Any other expenses: ");
scanf("%lf", &other);

*pOther = other;

return *pSchool, *pLiving, *pTransp, *pOther;
}

double get_income(double* pEmploy, double* pOther){
*pEmploy = *pOther = 0;

double wages, family, scholarship, other;

printf("\n\nEmployment income:\n");
printf("~~~~~~~~~~~~~~~~~\n");
printf("Expected Wages/Tips: ");
scanf("%lf", &wages);

*pEmploy = wages;

printf("\n\nOther income:\n");
printf("~~~~~~~~~~~\n");
printf("Family Support: ");
scanf("%lf", &family);
printf("\nScholarship/Bursaries (per semester): ");
scanf("%lf", &scholarship);
printf("\nOther: ");
scanf("%lf", &other);

*pOther = family + scholarship + other;

return *pEmploy, *pOther;
}

double display_report(double School, double Living, double Transp, double OtherExp, double Employ, double OtherInc) {  

printf("\n\nStudent Name Budget Report\n");  
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("Expenses:\n");
printf("~~~~~~~~\n");
printf("                                              MONTHLY  ANNUALY\n");
printf("School Expenses:                              $%.2lf   $%.2lf\n", School, School * 12);
printf("Living Expenses:                              $%.2lf   $%.2lf\n", Living, Living * 12);
printf("Transportation Expenses:                      $%.2lf   $%.2lf\n", Transp, Transp * 12);
printf("Other Expenses:                               $%.2lf   $%.2lf\n", OtherExp, OtherExp * 12);
printf("total Expenses:                               $%.2lf   $%.2lf\n", School + Living + Transp + OtherExp, School * 12 + Living * 12 + Transp * 12 + OtherExp * 12); 
printf("Income:\n");
printf("~~~~~~~\n");
printf("Employment Income:                            $%.2lf   $%.2lf\n", Employ, Employ * 12);
printf("Other Income:                                 $%.2lf   $%.2lf\n", OtherInc, OtherInc * 12);
printf("Total Income:                                 $%.2lf   $%.2lf\n", Employ + OtherInc, Employ * 12 + OtherInc * 12);
printf("Total Savings Available for your Goals:       $%.2lf   $%.2lf\n", (Employ + OtherInc) - (School + Living + Transp + OtherExp), (Employ * 12 + OtherInc * 12)- (School * 12 + Living * 12 + Transp * 12 + OtherExp * 12));


return 0;
}

char get_char(char* pRecalculate){

     int valid_input;

     valid_input = 0;

     while (valid_input == 0){
     printf("\nWould you like to recalculate your budget? (Y/N): ");
     scanf("%c", &*pRecalculate);
     *pRecalculate = toupper(*pRecalculate);
     if (*pRecalculate == 'Y' || *pRecalculate == 'y' || *pRecalculate == 'N' || *pRecalculate == 'n') valid_input = 1;
         else printf("Error Invalid choice\n");


     printf("\n");
     }

     return *pRecalculate;
     }


void clear_buffer(void) {

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

double get_double()
{

 // What to put here?
}
4

2 に答える 2

2

一連のプロセスについて考え、プログラムまたは関数がたどるフローチャート/意思決定ツリーについて考えます。

有効な入力が入力されるまで、ユーザーに入力を求めるのをやめたくありません。ユーザーに少なくとも 1 回尋ねることが保証されていますが、複数回尋ねることもあります。入力は有効または無効です。

ここで、いくつかの疑似コードの下書きを開始します。これが私がそれを行う方法です:

double get_double(void)
{
    double d;
    int ask_again = 1; /* continue if 1 (true), exit if 0 (false) */

    do {
        /* display message to user to let them know to type a number */
        /* read user input */
        if (/* user input didn't input a valid double */)
        {
            /* display appropriate error message */
        }
        else if (/* user entered a double, but it wasn't greater than or equal to zero */)
        {
            /* display appropriate error message */
        }
        else    /* user entered valid input, so ...  */
        {
            /* ... do something to make sure the loop exists */
        }
    } while (/* we need to ask again */);
}

これは私には do-while と叫ぶので、do-while ループで書きました。私はint ask_again変数を提供しましたが、それが有用であるとすれば、それはあなたに任せます。

すべてのコメントがある場所に実際のコードを配置する必要があることを考慮する必要があります。
コースノートを確認して、ここで適用するために教師が知っておくべきテクニックを確認してください。

ユーザー入力の読み取りとサニタイズは、すべてのコンピューター言語、特に C で悪名高い問題です。StackOverflow を使用すると、特定のアクションを実行する方法のアイデアを得ることができます。

于 2012-07-15T22:46:07.207 に答える
1

これを試して:

double get_double()
{
    double d;

    if (scanf("%lf", &d) != 1)
        /* handle error */
        reurn -1;

    if (d < 0)
        /* handle error */
        return -1;

    return d;
}

または、Keith Thompson が指摘したように、次の方法でも安全です。

double get_double()
{
    double d;
    char *line = NULL;
    size_t len;

    getline(&line, &len, stdin);
    if (line == NULL)
        return -1;

    d = strtod(line, NULL);
    free(line);

    if (d < 0)
        /* handle error */
        return -1;

    return d;
}

次のように使用できます (入力されたすべての double を 1 つの double で返す方法はないため、配列が必要な場合もありますが、質問ではターゲット署名が double (*)() として指定されています)。

double d;

while ((d = get_double()) >= 0)
{
    /* do what you want */
}
于 2012-07-15T21:41:48.270 に答える