C 程序仅接受整数 1-42 到数组中,并且它们不能在函数中重复

标签 c arrays function range scanf

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

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

需求如下(每个需求在单独的函数中实现):

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

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);
}

最佳答案

您的比较无效:

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

这意味着“如果数字大于 0 数字小于或等于 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");
        }
    }

关于C 程序仅接受整数 1-42 到数组中,并且它们不能在函数中重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22658143/

相关文章:

c++ - 变量变化时自动调用函数

c - HMAC_Init_ex 损坏堆栈空间

关于 PCA9555 扩展器上#interrupt-cells 配置的混淆

python - 如何从Python中的二进制文件解析序列化的C结构?

c - 最长公共(public)子序列 : why is this wrong?

python - 在给定条件的情况下在 numpy 数组中填充值

javascript - 如何延迟循环?或者如何让循环变慢?

php - 如何在 php 中找到计数为零的最小值?

c - C 中的按位移位

python - 在 Mako 模板中将 def 作为函数调用