C程序,只接受数组中的整数1-42,不能在函数中重复
开发一个玩乐透游戏的程序。该程序应允许用户 输入他们选择的6个数字,并给他们一组选项,每个选项执行 具体要求。必须将6个数字存储在一维数组中 您的程序必须满足许多要求。你的程序 必须模块化(即使用函数),每个任务应在单独的 功能。程序应向用户显示一个简单的菜单,并在菜单中显示每个选项 菜单将通过调用单独的函数来实现。必须使用指针 访问数组元素的符号-非下标 要求如下(每个要求在单独的功能中实现):C程序,只接受数组中的整数1-42,不能在函数中重复,c,arrays,function,range,scanf,C,Arrays,Function,Range,Scanf,开发一个玩乐透游戏的程序。该程序应允许用户 输入他们选择的6个数字,并给他们一组选项,每个选项执行 具体要求。必须将6个数字存储在一维数组中 您的程序必须满足许多要求。你的程序 必须模块化(即使用函数),每个任务应在单独的 功能。程序应向用户显示一个简单的菜单,并在菜单中显示每个选项 菜单将通过调用单独的函数来实现。必须使用指针 访问数组元素的符号-非下标 要求如下(每个要求在单独的功能中实现): 从键盘上读取选定的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);
}
#包括
#包括
#包括
#包括
#定义数字6
//声明原型
无效数字(整数*);
无效数字(整数*);
无效排序编号(整数*);
空比较器枚举(int*,int*);
无效频率编号(int*);
无效退出();
main()
{
int-choice=0;
国际乐透号码;
int WINING_no[数字]={1,3,5,7,9,11};
做
{
系统(“cls”);
printf(“\n==============================================================================================”);
printf(“\n\n\n------------------使用1-6导航菜单----------------”);
printf(“\n\n\n1.选择您的6个幸运数字”);
printf(“\n\n2.显示您的号码”);
printf(“\n\n3.按递增顺序显示您的幸运数字”);
printf(“\n\n4.比较您的数字以查看您的奖品!”);
printf(“\n\n5.每次输入数字的频率”);
printf(“\n\n6.Exit”);
printf(“\n\n\n=================================================================================================”);
printf(“\n”);
scanf(“%d”,选择(&C);
system(“cls”);//当我们从菜单中选择一个选项时,从屏幕上清除菜单
如果(选项==1)
{
乐透号码(乐透号码);
}
如果(选项==2)
{
显示号码(乐透号码);
}
如果(选项==3)
{
索特号码(乐透号码);
}
如果(选项==4)
{
比较者(乐透号码、中奖号码);
}
如果(选项==5)
{
频率号码(乐透号码);
}
如果(选项==6)
{
退出();
}
flushall();
getchar();
}
而(choice>1 | | choice您的比较无效:
if ( *(lotto_no+i) >0 || *(lotto_no + i ) <= 43 )
if(*(lotto_no+i)>0 | |*(lotto_no+i)以下是回答第一个问题的一种方法:
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);
}
}
我在这里尝试任务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
}
您能否提供一个起点,例如读取文件中的代码并尝试验证输入?您的getnumbers
函数有什么问题?这是您的家庭作业吗?旁注:没有理由使用过于复杂的地址算术来解列*(lotto_no+i)
。只需使用数组索引运算符lotto_no[i]
。我尝试了if语句,但仍然对数字没有任何影响,并允许输入大于42的数字。是的,谢谢:)还有一件事。当我输入数字时,例如,我按下字母,我的程序就发疯了:/@user3463615:当你输入时发生了什么?程序被无限循环崩溃了
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]);
}
}
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
}
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;
}