2014-09-06 46 views
0

我該如何去製作一個C程序,該程序需要用戶輸入(像-232或14這樣的整數)並輸出用戶輸入的最大值的整數?如何製作一個輸入整數的C程序,並打印輸入數量最多的輸入?

到目前爲止,我所知道的是(我的僞代碼):

int main(void) 
{ 

    int variable; 

    printf("Enter an integer to check if that is the greatest integer you inputted.") 

    if %d > variable; 
     printf("The greatest value you entered is %d") 
    elif 
     printf("The greatest value you entered is 'variable'") 

    scanf("%d", &variable) /Will this command help? IDK 
} 

我不想實際的代碼,但步驟/命令,就必須這樣做。 對不起,我似乎讓別人爲我做我的工作。 我剛開始C和我不是很熟悉:(

感謝。

PS程序應該儲存和保持最大的整數的記錄輸入。

+0

使用'while'循環。 – McLovin 2014-09-06 00:28:56

+0

可能重複[MIN和MAX在C](http://stackoverflow.com/questions/3437404/min-and-max-in-c) – simonzack 2014-09-06 00:32:42

+2

你有什麼是不是真的僞代碼,但非常破碎的C代碼。如果你對C不熟悉,我會建議通過一個在線教程或者獲得一本介紹性的C書。正如@McLovin所建議的,你將需要一個循環(在C中,'while'在這裏可以工作)。你需要首先將'variable'初始化爲可能的最小值,或者有一個單獨的標誌指示你是否已經讀取了第一個值。在循環中,每當變量大於'variable'時,將'variable'替換爲下一個讀取的整數。當沒有更多輸入時,循環結束。然後你打印'變量'。 – lurker 2014-09-06 00:32:49

回答

0

你需要兩樣東西,一個是你要找的東西,一個是你最終的情況(你何時會停止尋找)

你正在尋找最大的數字,但是你什麼時候停止尋找?10個值之後?文件結束之後?一條新線後?

因此,在僞代碼它像

int i = 0; 
int variable = 0; //Good practice to initialize your variables. 
while(When will you stop? i < 10 eg 10 inputs?){ 
    if(your input is > variable){ 
     variable = input; 
    } 
i++; //or whatever your end case is. Have to get closer to the end case. 
return variable; 
0
#include <limits.h> 
#include <stdio.h> 

int main(void) 
{ 
    int greatest = INT_MIN, variable; 
    FILE *fp; 
    (fp = fopen("record.txt", "a")) && 
    (fp = freopen("record.txt", "r+", fp)); 
    if (!fp) return perror("record.txt"), 1; 

    fscanf(fp, "%d", &greatest); 
    printf("Enter an integer to check if that is" 
      " the greatest integer you inputted. "); 
    if (scanf("%d", &variable) == 1) 
     if (variable > greatest) 
      rewind(fp), fprintf(fp, "%d\n", greatest = variable); 
    printf("The greatest value you entered is %d\n", greatest); 
} 
相關問題