简体   繁体   English

C开奖程序

[英]C lottery program

This is my assignment. 这是我的任务。

  1. If random 3 digit number matches user three digit in exact order then some awards. 如果随机的3位数字与用户的3位数字按确切顺序匹配,则将奖励一些。
  2. If user matches 3 digit number but not in order, then some other awards. 如果用户匹配3位数字,但顺序不匹配,则其他奖励。
  3. If user matches 2 digit, then some other prize 如果用户匹配2位数字,则其他一些奖品
  4. If user input matches just 1 digit, then some other prize 如果用户输入仅匹配一位数字,那么其他奖品

This is what I came up with: 这是我想出的:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(){
    int r,i,num[0];
  srand(time(NULL));
  r = rand()%(999-100+1)+100;

  printf("Here is the wininng number%d\n",r);

  printf("Enter three digit number to win lottery:\n");
  scanf("%d",num);

  for(i=0;i<3;i++){

    if(r==num[0]){
        printf("For three exact match you get $100,000\n");
    }else if((r/10)==(num[1]/10)){
        printf("For two number match you get $50,000\n");
    }else if((r%10)==(num[]%10)){
        printf("For one number match you get $10,000\n");
    }else{
        printf("You get nothing!\n");
    }}

}

I get like three digit match, and some time three digit and two digit match after compiling.Show me what's wrong. 我得到了三位数的匹配,有时编译后得到三位数和两位的匹配,告诉我怎么了。 Thanks you guys in advance. 谢谢你们提前。

For some reason, you have declared num[0] which makes num an array of zero length. 由于某种原因,您已经声明了num[0] ,这使num成为零长度的数组。

Note that the "winning number" is not really a number but a digit string. 请注意,“中奖号码”实际上不是数字,而是数字字符串。

You should declare num to be a character array: 您应该将num声明为字符数组:

char num[4];

You can read this with: 您可以阅读以下内容:

 scanf("%3s", num)

You then need to convert the ASCII characters read as num[0] , num[1] , num[2] so that you can compare them with the digits of the random number. 然后,您需要将ASCII字符转换为num[0]num[1]num[2]以便可以将它们与随机数的数字进行比较。

Your logic is wrong. 你的逻辑是错误的。

A 3-digit exact match is found by comparing what they entered with what you generated (the winning number). 通过将输入的内容与生成的内容(获胜数字)进行比较,可以找到3位数字的完全匹配项。

A 3-digit permutation is trickier, but you also need need to account for 2-digit and 1-digit permutations. 3位排列比较麻烦,但是您还需要考虑2位和1位排列。 I'd probably convert the generated (winning) number to a string, and also the user's number. 我可能会将生成的(获胜的)数字转换为字符串,以及用户的数字。 You can then step through the winning number, counting the number of digits from the winning number that match unused digits from the user's number. 然后,您可以逐步浏览中奖号码,计算中奖号码中与用户号码中未使用的数字相匹配的位数。 With the count, you know what, if anything, the user won. 通过计数,您知道用户赢得了什么(如果有的话)。 Note that when a digit has been matched, you need to delete it, so that when the winning number is 666 and the user enters 456, you don't count three matches with the 6. 请注意,当一个数字已匹配时,您需要将其删除,以便当中奖号码为666并且用户输入456时,您不会在数字6的情况下计入三个匹配项。

Like this: 像这样:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
    int win;
    int num;

    srand(time(NULL));
    win = rand() % (999 - 100 + 1) + 100;

    printf("Here is the winning number: %d\n", win);

    printf("Enter three digit number to win lottery:\n");
    if (scanf("%d", &num) != 1)
        return 1;

    if (num == win)
        printf("For exact match you get $100,000\n");
    else if (num < 0 || num > 999)
        printf("Your number is out of range - you win nothing\n");
    else
    {
        char win_str[4];
        char try_str[4];
        sprintf(win_str, "%d", win);
        sprintf(try_str, "%d", num);
        int match = 0;
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                if (win_str[i] == try_str[j])
                {
                    try_str[j] = 'x';
                    match++;
                    break;
                }
            }
        }

        switch (match)
        {
        case 0:
            printf("No digits in %.3d match %3d - you win nothing\n", num, win);
            break;
        case 1:
            printf("One digit of %.3d matches %3d - you win $10,000\n", num, win);
            break;
        case 2:
            printf("Two digits of %.3d match %3d - you win $20,000\n", num, win);
            break;
        case 3:
            printf("Three digits of %.3d match %3d - you win $50,000\n", num, win);
            break;
        default:
            printf("The impossible happened (%.3d vs %3d gives %d matches)\n", num, win, match);
            break;
        }
    }
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM