【发布时间】:2014-03-26 10:29:55
【问题描述】:
开发一个可以玩 Lotto 游戏的程序。该程序应允许用户 输入他们选择的 6 个数字并给他们一组选项,每个选项执行一个 具体要求。您必须将 6 个数字存储在一维数组中。
您的计划必须满足许多要求。你的程序 必须模块化(即使用功能)并且每个任务都应该在一个单独的 功能。程序应该向用户显示一个简单的菜单,并显示每个选项 菜单将通过调用单独的函数来实现。您必须使用指针 访问数组元素的符号 - 不是下标
要求如下(分别在单独的函数中实现):
- 从键盘读取 6 个选定的数字。执行任何必要的 需要验证(错误检查)(例如,所有数字必须不同,范围 1-42 等)。 这是我不能做的部分
- 显示包含您的乐透号码的一维数组的内容 进入。
- 按升序对数组的内容进行排序(即第一个元素 = 最小 数,第二个元素 = 第二个最小的数,等等)。您可以使用任何排序 您选择的算法。
- 将您在 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