2014-02-08 59 views
0

我正在爲一個類做代碼,一切看起來應該可行,但代碼似乎不接受字符輸入和數學的混亂。例如,如果我輸入第一個輸入1000並輸入'a'作爲輸入,它會重複詢問輸入,如果我確實將它自動設置爲'a',那麼就會使數學陷入混亂。不知何故,數學是離開的。我該如何解決?任何幫助,將不勝感激。C:不接受字符輸入+無功能數學

#include <stdio.h> 
#include <stdlib.h> 

void print_status_menu(void) 
{ 
printf("MONTHLY  1  2  3  4  5  6  7  8\n"); 
printf("Status\n"); 
printf("A. Table for employees without qualified dependent\n"); 
printf("1. Z   1  0  833 2,500 5,833 11,667 20,833 41,667\n"); 
printf("2. S/ME  1  4,167 5,000 6,667 10,000 15,833 25,000 45,833\n\n"); 

printf("B. Table for single/married employee with qualified dependent child(ren)\n"); 
printf("1. ME1/S1 1  6,250 7,083 8,750 12,083 17,917 27,083 47,917\n"); 
printf("2. ME2/S2 1  8,333 9,167 10,833 14,167 20,000 29,167 50,000\n"); 
printf("3. ME3/S3 1 10,417 11,250 12,917 16,250 22,083 31,250 52,083\n"); 
printf("4. ME4/S4 1 12,500 13,333 15,000 18,333 24,167 33,333 54,167\n"); 
} 



void entered_filing_status(char *px, int *py, int*pz, float *pinc, int *ploc) 
{ 
int def; 
while(*px!='a' || *px!='b') 
{ 
    printf("Table a or b?: "); 
    *px = getchar(); 
    if(*px == 'A') 
     *px = 'a'; 
    else if(*px == 'B') 
     *px = 'b'; 
} 

if(*px == 'b') 

{ 
    while(*py<1) 
    { 
    printf("Input amount of children: "); 
    scanf("%d", &*py); 
    } 
    while(*py>4) 
    { 
     *py-=1; 
    } 
    if(*pinc>(45833+(*py*2083))) 
     def = 8; 
    else if(*pinc>25000+(*py*2083)) 
     def = 7; 
    else if(*pinc>15833+(*py*2083)) 
     def = 6; 
    else if(*pinc>10000+(*py*2083)) 
     def = 5; 
    else if(*pinc>6667+(*py*2083)) 
     def = 4; 
    else if(*pinc>5000+(*py*2083)) 
     def = 3; 
    else if(*pinc>4167+(*py*2083)) 
     def = 2; 
    else 
     def = 1; 
} 


else{ 
printf("Are you under zero exemptions?(0 for yes, 1 for no): "); 
scanf("%d", &*pz); 
*pz+=1; 
if((*pinc)>(33333+(4167 * *pz))) 
     def = 8; 
    else if((*pinc)>12499+(4167 * *pz)) 
     def = 7; 
    else if((*pinc)>3333+(4167 * *pz)) 
     def = 6; 
    else if((*pinc)>(4167 * *pz)-2501) 
     def = 5; 
    else if((*pinc)>(4167 * *pz)-5834) 
     def = 4; 
    else if((*pinc)>(4167 * *pz)-7501) 
     def = 3; 
    else if((*pinc)>(4167 * *pz)-8334) 
     def = 2; 
} 

*ploc = def; 
} 



void computed_tax(float *px, float *py, int *pz) 
{ 
switch (*pz) 
{ 
case 8: 
    *py = (10416 + (*px * 0.32)); 
    break; 
    case 7: 
    *py = (4166.67 + (*px * 0.3)); 
    break; 
    case 6: 
    *py = (1875 + (*px * 0.25)); 
    break; 
    case 5: 
    *py = (708.33 + (*px * 0.2)); 
    break; 
    case 4: 
    *py = (208.33 + (*px * 0.15)); 
    break; 
    case 3: 
    *py = (41.67 + (*px * 0.1)); 
    break; 
    case 2: 
    *py = (*px * 0.05); 
    break; 
    case 1: 
    *py = 0; 
    break; 
}  
} 


int entered_gross_income(float *px) 

{ 
printf("What is your gross monthly income?: "); 
scanf("%f", &*px); 
if(*px<833) 
    return 1; 
else 
    return 0; 
} 

void output_results(float *px, float *py, int *pc, char *pd, int *pzex) 
{ 
printf("\nYour monthly income is %f", *px); 
if (*pd == 'a') 
{ 
    if(*pzex == 1) 
     printf("\nYou're filed under Zero Exemptions"); 
      else 
       printf("\nYou're filed under S/ME"); 
} 
if(*pd == 'b') 
    printf("\nYou're filed under ME%d/S%d", *pc, *pc); 
printf("\nYour tax is %f", *py); 
} 

int main (void) 
{ 
char c = 'd'; 
int children = 0, zex = 0, table = 0; 
float income = 0, tax = 0; 

if (entered_gross_income(&income)==1) 
{ 
    printf("You have no taxes to pay!"); 
    exit(0); 
} 

print_status_menu(); 
entered_filing_status(&c, &children, &zex, &income, &table); 
computed_tax(&income, &tax, &table); 
output_results(&income, &tax, &children, &c, &zex); 
} 
+0

'的scanf( 「%F」,&*px);'應該只是'的scanf( 「%F」,像素);' –

+0

你計算的稅收函數應該返回答案,不需要其他它應該是'double void calculate_tax(double px,int pz)'。所有的計算都應該使用'double',如果不是一個專門的小數類型,並且有足夠的準確性來處理財務工作,當數字開始達到10,000.00貨幣單位,當你在大多數系統上使用'float'時,你開始失去百分之一的貨幣單位的準確性,你應該使用數組而不是開關來存儲稅表。 –

回答

2

條件

while (*px!='a' || *px!='b') 

總是返回true。它應該是:

while(*px!='a' && *px!='b')