【问题标题】:C program which accepts only integers 1-42 into array and they cannot repeat in function [closed]C程序仅接受整数1-42到数组中并且它们不能在函数中重复[关闭]
【发布时间】:2014-03-26 10:29:55
【问题描述】:

开发一个可以玩 Lotto 游戏的程序。该程序应允许用户 输入他们选择的 6 个数字并给他们一组选项,每个选项执行一个 具体要求。您必须将 6 个数字存储在一维数组中。

您的计划必须满足许多要求。你的程序 必须模块化(即使用功能)并且每个任务都应该在一个单独的 功能。程序应该向用户显示一个简单的菜单,并显示每个选项 菜单将通过调用单独的函数来实现。您必须使用指针 访问数组元素的符号 - 不是下标

要求如下(分别在单独的函数中实现):

  1. 从键盘读取 6 个选定的数字。执行任何必要的 需要验证(错误检查)(例如,所有数字必须不同,范围 1-42 等)。 这是我不能做的部分
  2. 显示包含您的乐透号码的一维数组的内容 进入。
  3. 按升序对数组的内容进行排序(即第一个元素 = 最小 数,第二个元素 = 第二个最小的数,等等)。您可以使用任何排序 您选择的算法。
  4. 将您在 1-D 数组中选择的乐透号码与以下号码进行比较 中奖号码:

1,3,5,7,9,11 - 42(奖金数)

5 显示用户每次输入新数字时输入数字的频率我不知道如何开始这个 屏幕上的一组数字(不退出程序)。例如,它 可能会显示:

数字 1 已被选中 x 次 数字 7 已被选中 x 次 28 号已被选中 x 次 等等,

6 退出程序

#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include <stdlib.h> 
#define NUMBERS 6

 // Declare Prototype
 void getnumbers(int*);
 void displaynumbers(int*);
 void sortnumbers(int*);
 void comparenumbers(int*,int*);
 void frequencynumbers(int*);
 void exit();
 main()
 {


     int choice=0;
     int lotto_no[NUMBERS];
     int winning_no[NUMBERS]={1,3,5,7,9,11};

    do
    {   
        system("cls");
         printf("\n======================MAGIC Lotto======================");
         printf("\n\n\n--------------Use 1-6 to navigate the menu-------------");
         printf("\n\n\n1.Pick your 6 lucky numbers");
         printf("\n\n2.Disply your numbers");
         printf("\n\n3.Display your lucky numbers in increasing order");
         printf("\n\n4.Compare your numbers to see your prize!");
         printf("\n\n5.Frequency of the numbers entered each time");
         printf("\n\n6.Exit");
         printf("\n\n\n========================================================");
         printf("\n");
        scanf("%d",&choice);
         system("cls");//Clears the menu from the screen while we selesc an option from the Menu 

        if (choice == 1)
        {
            getnumbers(lotto_no);
        }
        if (choice == 2)
        {
            displaynumbers(lotto_no);
        }
        if(choice == 3)
        {
            sortnumbers(lotto_no);
        }
        if(choice == 4)
        {
            comparenumbers(lotto_no,winning_no);
        }
        if(choice == 5)
        {
            frequencynumbers(lotto_no);
        }
        if(choice == 6)
        {
            exit();
        }


        flushall();
        getchar();

    }



    while(choice>1 || choice<7);
} 
void getnumbers(int *lotto_no)
{
    int i;
    printf("Enter your numbers\n");
        for(i=0;i<NUMBERS;i++)
        {
            scanf("%d", &*(lotto_no+i) );

            if ( *(lotto_no+i) >0 || *(lotto_no + i ) <= 43 )
            { 
                continue;
            }

            else
            {
                printf(" Please enter a value in between 1 and 42");
                scanf("%d", &(*(lotto_no+i)));
            }
        }

}

void displaynumbers(int *lotto_num)
{
    int i;
    printf("Your lucky numbers are as follow:\n");
    for(i=0;i<NUMBERS;i++)
    {
        printf("%d ",*(lotto_num+i));
    }
}

void sortnumbers(int *lotto_num)
{
    int i;
    int j;
    int temp;

    for(i=0;i<NUMBERS;i++)
    {
        for(j=i;j<NUMBERS;j++)
        {
            if(*(lotto_num+i) > *(lotto_num+j))
            {
                temp=*(lotto_num+i);
                *(lotto_num+i)=*(lotto_num+j);
                *(lotto_num+j)=temp;
            }
        }
    }
    printf("Your lucky numbers are as follow:\n");
    for(i=0;i<NUMBERS;i++)
    {
        printf("%d ",lotto_num[i]);
    }
}
void comparenumbers(int *lotto_num,int *winning_num)
{

    int i;
    int j;
    int c;
    int b;
    int g;
    c=0;
    b=42;
    g=0;

    for(i=0;i<NUMBERS;i++)
    {
        for(j=0;j<NUMBERS;j++)
        {
            if(*(lotto_num+i) == *(winning_num+j))
            {
                c++;
            }
        }
    }
    for(i=0;i<NUMBERS;i++)
    {
        if(*(lotto_num)==b)
        {
            g++;
        }
    }

    if(c==6)
    {
        printf("JACKPOT!!!");
    }
    if(c==5)
    {
        printf("HOLIDAY!!!");
    }
    if(c==4)
    {
        printf("NIGHT OUT!!!");
    }
    if(c==3&&g==1)
    {
        printf("CINEMA TICKET!!!");
    }
    if(c==4&&g==1)
    {
        printf("WEEKEND AWAY!!!");
    }
    if(c==5&&g==1)
    {
        printf("NEW CAR!!!");
    }
    if(c<3)
    {
        printf("MAYBE NEXT TIME QQ :(");
    }

}
void frequencynumbers(int *lotto_num)
{

}

void exit()
{
    exit(0);
}

【问题讨论】:

  • 您能否提供一个起点,例如您要读取文件的代码并尝试验证输入?
  • 你的getnumbers函数有什么问题?
  • 这是你的作业吗?
  • 旁注:没有理由将过于复杂的地址算术与 derefenrecing *(lotto_no+i) 一起使用。只需使用数组索引运算符lotto_no[i]
  • 我尝试了 if 语句,但仍然对数字没有任何影响,并允许输入大于 42 的数字。是的

标签: c arrays function range scanf


【解决方案1】:

您的比较无效:

if ( *(lotto_no+i) >0 || *(lotto_no + i ) <= 43 )

意思是“如果数字大于0OR数字小于或等于到43。

即使你输入了错误的号码,你也会继续下一个号码。

    for(i=0;i<NUMBERS;)
    {
        int number = 0;
        scanf("%d", &number);
        // Replace OR with AND, and fix the upper comparison operator
        if(number > 0 && number < 43 )
        { 
            lotto_no[i] = number;
            i++; // Only increase when number was correct
        }
        else
        {
            printf(" Please enter a value in between 1 and 42");
        }
    }

【讨论】:

    【解决方案2】:

    我在这里尝试 1 号和 5 号任务。

    让我们看看第一个任务

    void getnumbers(int *lotto_no)
    {
    int i,j,temp;
    printf("Enter 6 different numbers in the range 1-42\n");
    for(i=0;i<6;i++)
    {
         printf("Enter number %d ",i+1);
         scanf("%d",&temp);
         for(j=0;j<i;j++)
         {
              while( (temp == *(lotto_no+j)) || (temp<1 || temp>42)) //Error checking
              {
                   printf("Wrong input. Please enter a unique number in the range 1-42");
                   scanf("%d",&temp);
                   j=0;
              }
         }
         *(lotto_no+i)=temp;
    }
    return
    }
    

    现在让我们执行第 5 项任务。我正在获取另一个数组 int frequency[43]={0}。我正在创建另一个函数void UpdateFrequency(int *, int *)。在这个频率将被更新,并且每次用户输入正确的输入组合时都会调用它,即我们应该在任务 1 中调用这个函数。

    void UpdateFrequency(int *lotto_no, int *frequency)
    {
       int i;
       for(i=0;i<6;i++)
       {
           (*(frequency+(*(lotto_no+i))))+=1; //frequency[lotto_no[i]]+=1;
       }
    return;
    }
    
    void frequencynumbers(int *lotto_no, int *frequency)
    {
       int i;
       for(i=0;i<6;i++)
       {
           printf("The frequency of number %d is %d\n",*(lotto_no+i),*(frequency+(*(lotto_no+i))));
       }
    return;
    }
    

    【讨论】:

    • 谢谢 :) 还有一件事。当我输入数字和例如我按字母时,我的程序就会变得疯狂:/
    • @user3463615:输入输入时发生了什么?
    • 程序因无限循环而崩溃
    【解决方案3】:

    这是回答第一个问题的一种方法:

    void getnumbers(int *lotto_no)
    {
        int i;
        printf("Enter your numbers\n");
        for(i=0;i<NUMBERS;i++)
        {
            do 
            {
                printf("Please enter number %d, between 1 and 42\n", i+1);
                scanf("%d", lotto_no+i); // or &lotto_no[i] if you prefer
            } 
            while ( *(lotto_no + i) < 1 || *(lotto_no + i ) > 42 ); // or lotto_no[i]
            printf("Number %d accepted\n", i+1);
        }
    }
    

    我已经从您的scanf 中删除了不必要的&amp;*,因为它们是多余的。我颠倒了不等式,以便内部 do while 循环将继续为每个数字,直到给出有效的输入。

    计算输入给定数字的次数的一种简单方法是创建一个bins 数组,每个数字一个。然后你可以把scanf变成一个临时数,并增加bins的对应元素:

    void getnumbers(int *lotto_no)
    {
        int i, n, bins[42] = {}; // initialise zeroed array
        printf("Enter your numbers\n");
        for(i=0;i<NUMBERS;i++)
        {
            do {
                printf("Please enter number %d, between 1 and 42\n", i+1);
                scanf("%d", &n); // read into temporary variable
            } 
            while ( n < 1 || n > 42 );
            lotto_no[i] = n; // assign to lotto_no
            ++bins[n-1];     // increment corresponding counter
            printf("Number %d accepted\n", i+1);     
        }
        for(i=0; i<42; ++i)
        {
            if (bins[i] > 0) printf("number %d entered %d times\n", i+1, bins[i]);
        } 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-14
      • 2016-01-10
      • 1970-01-01
      • 2021-12-24
      • 1970-01-01
      • 2023-02-09
      • 2014-03-05
      • 1970-01-01
      相关资源
      最近更新 更多