建议一个文件程序要求最大的数字 [英] Advice with a file program asking for the largest number

查看:165
本文介绍了建议一个文件程序要求最大的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这次的分配处理使用文件。 假定一个包含一系列整数的文件名为numbers.dat,并存在于计算机的磁盘上,设计一个程序来确定存储在文件中的最大编号,教师告诉我们不要使用基于数组的实现,并且包含一个变量计算从文件中读取的项目数,并在显示文件中找到的最大值之后输出这个计数我不知道如何获得最大值而不使用基于数组的实现这是我到目前为止: / b>

  def main():
n = 1
最大= None

num_input = int(input(有多少数字+你必须输入?))

numbers_file = open('numbers.dat','w')
$ b $ (1,num_input + 1):
number = float(input('Enter the number#'+ str(count)+':'))

if是无或n>最大:
最大= n
print('输入的最大值是',最大)
numbers_file.close()
main()


解决方案

使用一个变量来存储当前的数字,并假定它是最大的。当你通过文件比较存储的号码与文件中的当前号码,如果来自文件的号码更大,则将其存储在变量中,否则继续读取文件。重复这个,直到到达文件的结尾。

  largest = 0; ((num = input.readline())!= EOF){
count ++;
count = 0

if(最大< num){
largest = num;
}
}


the assignment this time around deals with using files. "Assume that a file containing a series of integers is named numbers.dat and exists on the computer's disk. Design a program that determines the largest number stored in the file. The instructor told us not to use array based implementations, and include a variable to count the number of items read from the file and output this count after displaying the largest value found in the file. I am having trouble on how to get the largest value without using array based implementation. Here is what I have so far:

    def main():
        n = 1
        largest = None

        num_input = int(input("How many numbers do " + "you have to input? "))

        numbers_file = open('numbers.dat', 'w')

        for count in range(1, num_input + 1):
            number = float(input('Enter the number #' + str(count) + ': '))

        if largest is None or n > largest:
            largest = n
        print('The largest value inputted is: ', largest)
        numbers_file.close()   
    main()

解决方案

use a variable to store the current number and assume that it is largest. As you go through the file compare the stored number to current number from file, if number from file is greater store it in the variable else keep on reading the file. Repeat this until you reach end of the file.

 largest = 0;
 count = 0
 while ((num =input.readline()) != EOF) {
      count++;
      if (largest < num) {
           largest = num;
      }
   }

这篇关于建议一个文件程序要求最大的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆