2014-09-06 48 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); 
} 
相关问题