2014-02-05 49 views
-3

我已經發布了這個程序在上一個問題,但代碼已經改變,因爲原來的帖子,不再是一個重複的,所以我發佈新的代碼作爲一個新的問題,因爲原來的解決方案沒有更長的申請和[重複]標籤不能被刪除。我希望這沒關係。C - 意外的輸出與strcmp

問題:我已更改函數使用strcmp而不是==,但問題仍然存在。這個問題的目的是要找出哪些問題與strcmp一起導致函數返回不正確的值。

謝謝!

編輯: 'main'將用戶的輸入發送給該函數,以便與顏色列表進行比較並將其轉換爲適當的整數。我遇到的問題是,儘管輸入了有效的顏色,但函數正在從「其他」返回「錯誤」值。編輯:我一直在使用紅色作爲我的輸入,並且返回的值始終爲100而不是2. 100僅僅是發送消息給主要說沒有找到匹配。

#include <stdio.h> 
#include <math.h> 
#include <string.h> //This is an edit 

int values123(char c[10]); 

int main(void) //Changed void to int 
{ 
    int bands = 0; 
    char band1[10]; 
    char band2[10]; 
    char band3[10]; 
    char band4[10]; 
    char band5[10]; 
    printf("Number of colour bands: "); 
    scanf_s("%i", &bands); 
    printf("\nBand 1: "); 
    scanf_s("%s", band1); 
    if (values123(band1) == 100) 
    { 
     printf("Colour is invalid!"); 
    } 
    fflush(stdin); 
    printf("\nBand 2: "); 
    scanf_s("%s", band2); 
    fflush(stdin); 
    printf("\nBand 3: "); 
    scanf_s("%s", band3); 
    fflush(stdin); 
    printf("\nBand 4: "); 
    scanf_s("%s", band4); 
    fflush(stdin); 
    if (bands == 5) 
    { 
     printf("\nBand 5: "); 
     scanf_s("%s", band5); 
     fflush(stdin); 
    } 

    getch(); 
} 
int values123(char c[10]) 
{ 
    if (strcmp(c, "black") == 0) 
     return (0); 
    else if (strcmp(c, "brown") == 0) 
     return (1); 
    else if (strcmp(c, "red") == 0) 
     return (2); 
    else if (strcmp(c, "orange") == 0) 
     return (3); 
    else if (strcmp(c, "yellow") == 0) 
     return (4); 
    else if (strcmp(c, "green") == 0) 
     return (5); 
    else if (strcmp(c, "blue") == 0) 
     return (6); 
    else if (strcmp(c, "violet") == 0) 
     return (7); 
    else if (strcmp(c, "grey") == 0) 
     return (8); 
    else if (strcmp(c, "white") == 0) 
     return (9); 
    else 
     return (100); 
} 
+1

評論*但是問題仍然存在*我們不能猜測是什麼問題,我們肯定不會去挖舊的問題找到原來的一個。你能描述一下嗎? –

+2

您的輸入和預期輸出是什麼?你得到什麼輸出? – haccks

+0

是的,對不起。 'main'將用戶的輸入發送給該函數,以便與顏色列表進行比較並將其轉換爲適當的整數。我遇到的問題是,儘管輸入了有效的顏色,但函數正在從「其他」返回「錯誤」值。 編輯:我一直使用紅色作爲我的輸入,並且返回的值始終爲100而不是2. 100只是發送消息給主要說沒有找到匹配。 –

回答

0

這是它的工作和完成! 謝謝大家,他們的輸入:)

/* 
Author: Sean Sciberras 
Last edited: Feb. 05/2014 
Description: Calculates resistances of 4 and 5 banded resistors. 
*/ 
#include <stdio.h> 
#include <math.h> 
#include <string.h> 

int sigfigs(char band[10]); 
float mult(char band[10]); 
float tol(char band[10]); 
//-------------------------------------------------------------------------------------------------------------- 
int main(void) 
{ 
    int bands = 0; 
    char band1[10]; 
    char band2[10]; 
    char band3[10]; 
    char band4[10]; 
    char band5[10]; 
    float resistance; 
    float resistance_min; 
    float resistance_max; 
    printf("Number of colour bands: "); 
    scanf_s("%i", &bands); 
    printf("\nBand 1: "); 
    scanf_s("%s", band1, 10); 
    if (sigfigs(band1) == 100) 
    { 
     printf("Colour is invalid!"); 
    } 
    fflush(stdin); 
    printf("\nBand 2: "); 
    scanf_s("%s", band2, 10); 
    if (sigfigs(band2) == 100) 
    { 
     printf("Colour is invalid!"); 
    } 
    fflush(stdin); 
    printf("\nBand 3: "); 
    scanf_s("%s", band3, 10); 
    if (bands == 5) 
    { 
     if (sigfigs(band3) == 100) 
     { 
      printf("Colour is invalid!"); 
     } 
    } 
    else if (bands == 4) 
    { 
     if (mult(band3) == 9) 
     { 
      printf("Colour is invalid!"); 
     } 
    } 
    fflush(stdin); 
    printf("\nBand 4: "); 
    scanf_s("%s", band4, 10); 
    if (mult(band4) == 9) 
    { 
     printf("Colour is invalid!"); 
    } 
    fflush(stdin); 
    if (bands == 5) 
    { 
     printf("\nBand 5: "); 
     scanf_s("%s", band5, 10); 
     if (tol(band5) == 9) 
     { 
      printf("Colour is invalid!"); 
     } 
     fflush(stdin); 
    } 

    if (bands == 4) 
    { 
     resistance = ((sigfigs(band1) * 10) + (sigfigs(band2) * 1)) * mult(band3); 
     resistance_min = resistance - (resistance * tol(band4)); 
     resistance_max = resistance + (resistance * tol(band4)); 
     system("cls"); 
     printf("Band 1: %10s -> %i\n", band1, sigfigs(band1)*10); 
     printf("Band 2: %10s -> %i\n", band2, sigfigs(band2)*1); 
     printf("Band 3: %10s -> x%.2f\n", band3, mult(band3)); 
     printf("Band 4: %10s -> +/-%.4f%%\n", band4, tol(band4)*100); 
     printf("Resistance = (%i x 10 + %i x 1) x %.2f = %.2fohms\n", sigfigs(band1), sigfigs(band2), mult(band3), resistance); 
     printf("Max. Resistance = %.2f + %.2f x %.4f = %.2fohms\n", resistance, resistance, tol(band4), resistance_max); 
     printf("Min. Resistance = %.2f - %.2f x %.4f = %.2fohms\n", resistance, resistance, tol(band4), resistance_min); 
    } 
    else if (bands == 5) 
    { 
     resistance = ((sigfigs(band1) * 100) + (sigfigs(band2) * 10) + (sigfigs(band3) * 1)) * mult(band4); 
     resistance_min = resistance - (resistance * tol(band5)); 
     resistance_max = resistance + (resistance * tol(band5)); 
     system("cls"); 
     printf("Band 1: %10s -> %i\n", band1, sigfigs(band1) * 100); 
     printf("Band 2: %10s -> %i\n", band2, sigfigs(band2) * 10); 
     printf("Band 3: %10s -> %i\n", band3, sigfigs(band3) * 1); 
     printf("Band 4: %10s -> x%.2f\n", band4, mult(band4)); 
     printf("Band 5: %10s -> +/-%.4f%%\n", band5, tol(band5) * 100); 
     printf("Resistance = (%i x 100 + %i x 10 + %i x 1) x %.2f = %.2fohms\n", sigfigs(band1), sigfigs(band2), sigfigs(band3), mult(band4), resistance); 
     printf("Max. Resistance = %.2f + %.2f x %.4f = %.2fohms\n", resistance, resistance, tol(band5), resistance_max); 
     printf("Min. Resistance = %.2f - %.2f x %.4f = %.2fohms\n", resistance, resistance, tol(band5), resistance_min); 
    } 
    printf("Press any key to continue..."); 
    getch(); 
} 
//---------------------------------------------------------------------------------------------------- 
int sigfigs(char band[10]) 
{ 
    if (strcmp(band, "black") == 0) 
     return (0); 
    else if (strcmp(band, "brown") == 0) 
     return (1); 
    else if (strcmp(band, "red") == 0) 
     return (2); 
    else if (strcmp(band, "orange") == 0) 
     return (3); 
    else if (strcmp(band, "yellow") == 0) 
     return (4); 
    else if (strcmp(band, "green") == 0) 
     return (5); 
    else if (strcmp(band, "blue") == 0) 
     return (6); 
    else if (strcmp(band, "violet") == 0) 
     return (7); 
    else if (strcmp(band, "grey") == 0) 
     return (8); 
    else if (strcmp(band, "white") == 0) 
     return (9); 
    else 
     return (100); 
} 

float mult(char band[10]) 
{ 
    if (strcmp(band, "black") == 0) 
     return (1); 
    else if (strcmp(band, "brown") == 0) 
     return (10); 
    else if (strcmp(band, "red") == 0) 
     return (100); 
    else if (strcmp(band, "orange") == 0) 
     return (1000); 
    else if (strcmp(band, "yellow") == 0) 
     return (10000); 
    else if (strcmp(band, "green") == 0) 
     return (100000); 
    else if (strcmp(band, "blue") == 0) 
     return (1000000); 
    else if (strcmp(band, "violet") == 0) 
     return (10000000); 
    else if (strcmp(band, "gold") == 0) 
     return (0.1); 
    else if (strcmp(band, "silver") == 0) 
     return (0.01); 
    else 
     return (9); 
} 

float tol(char band[10]) 
{ 
    if (strcmp(band, "brown") == 0) 
     return (0.01); 
    else if (strcmp(band, "red") == 0) 
     return (0.02); 
    else if (strcmp(band, "green") == 0) 
     return (0.005); 
    else if (strcmp(band, "blue") == 0) 
     return (0.0025); 
    else if (strcmp(band, "violet") == 0) 
     return (0.001); 
    else if (strcmp(band, "grey") == 0) 
     return (0.0005); 
    else if (strcmp(band, "gold") == 0) 
     return (0.05); 
    else if (strcmp(band, "silver") == 0) 
     return (0.1); 
    else 
     return (9); 
}