C language to achieve a simplified version of the red envelope program code

  • 2020-06-07 04:57:17
  • OfStack

This article shares the specific code of Android9 grid pictures for your reference. The specific content is as follows

In fact this is a very simple code, as to why put it on the blog, mainly for the beginner to learn programming students (or is interested in the program classmate), an intuitive program (game) may be more to cultivate their interest, or more said they are enthusiastic about programming. First of all, I myself from that period, for the teacher speak knowledge is very confused, don't know operator, familiar with 1 to statements, what's the use of the structure, how to use, so oneself groped for several instances headphones, in the process of practice is gradually mastered the teacher speak knowledge, more interested in programming and it himself. The following will give you a simplified version of the red envelope grab procedure, take your own to run 1.


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

This is the header file of the program, and I don't think I have to tell you too much;


int hongbao()
{
 int num=100;
 float total=100;
 float a[101];
 float min=0.01;
 int i;
 float t;
 float safe_total=0;
 float sum=0;
 srand(time(0));
 for(i=1;i<num;i++)
 {
 t=total/(num-i+1);
 safe_total=t*2;
 a[i]=(rand()%(int)(safe_total*total)+(int)min*total)/total+min;
 total=total-a[i];
 printf(" The first %d A red envelope with %0.2f yuan \n",i,a[i]);
 
 }
 a[i]=total;
 printf(" The first %d A red envelope with %0.2f yuan \n\n The total amount you receive %0.2f yuan \n",i,a[i],sum+a[i]);
 return 0;
 
 } 

This is the whole program, I defined it as a function, in order to make the game more interesting (call in the main function can set the loop). In the code I set the default 100 yuan, 100 red envelopes, and then grab their own, the amount of each red envelope is randomly allocated, here with a 1 dimensional array to store; Iterating through the output of these statements is trivial and not redundant.


int main()
 {
 loop1:
  hongbao();
  printf("\n\n");
  while(1)
  {
  printf(" Whether to continue to grab the red envelope (Y\\N)\n");
  int a;
  scanf("%c",&a);
  getchar();
  a=toupper(a);
  if(a=='Y')
   {
   goto loop1;
   }
  else
  if(a=='N')
   {
   printf("Congratulation !!!!!!!!! ");
   break;
   }
   else
   {
    printf("your input is wrong!please input again\n\n");
    continue;
    } 
  }
 return0; 
 }

The main function is straightforward, it calls the hongbao() function, and there is the while loop, as mentioned above, in order to test the game in practice you have to choose whether to continue or not. The others are 1 print statements, which are easy to understand. The goto statement is used here purely for convenience, as the loop of the program is not complicated, but for simplicity and intuition. (To be honest, this is my code from a long time ago)

Take the trouble to paste the entire code in 1 time


#include<stdio.h> 
#include<stdlib.h> 
#include<time.h> 
#include<ctype.h>  
int hongbao() 
{ 
  int num=100; 
  float total=100; 
  float a[101]; 
  float min=0.01; 
  int i; 
  float t; 
  float safe_total=0; 
  float sum=0; 
  srand(time(0));  
  for(i=1;i<num;i++) 
  { 
    t=total/(num-i+1); 
    safe_total=t*2; 
    a[i]=(rand()%(int)(safe_total*total)+(int)min*total)/total+min; 
    total=total-a[i]; 
    printf(" The first %d A red envelope with %0.2f yuan \n",i,a[i]); 
     
  } 
  a[i]=total; 
  printf(" The first %d A red envelope with %0.2f yuan \n\n The total amount you receive %0.2f yuan \n",i,a[i],sum+a[i]); 
  return 0; 
   
 }  
  
int main() 
 { 
  loop1: 
    hongbao(); 
    printf("\n\n"); 
    while(1) 
    { 
      printf(" Whether to continue to grab the red envelope (Y\\N)\n"); 
      int a; 
      scanf("%c",&a); 
      getchar(); 
      a=toupper(a); 
      if(a=='Y') 
        { 
          goto loop1; 
        } 
      else 
        if(a=='N') 
          { 
          printf("Congratulation !!!!!!!!! "); 
          break; 
          } 
        else 
          { 
            printf("your input is wrong!please input again\n\n"); 
            continue; 
          }   
    } 
  return0;   
 } 

Above is a simplified version of the red envelope code, I hope to help you.

In fact, programming is a very interesting thing, you can according to a piece of simple code, oneself constantly add 1 thing, constantly to improve it, in this process you will slowly progress, as the saying goes, start from small things, over time, slowly you will find yourself completed a great job.


Related articles: