C language application: marketing means for businessmen

Topic: A fruit supermarket launched an anniversary store celebration event and launched a number guessing game. Depending on the number of correct guesses, different rewards will be given.

This game is designed using C language.

Requirements: Output the price list, and the money the user needs to spend (a purchase of 100 or more unlocks the number guessing game), and a 50% discount for purchases of 30 to 100.

Numbers Game: Guess it once and the order is free. Get a 50% discount within three times (including three times), a 25% discount within five times (including five times), and a 15% discount if you fail to guess correctly five times.

Menu: Operated by our store
[1] apple price:3.00
[2] pear price:2.50
[3] orange price:4.10
[4] grape price:10.20
[0] exit

Note: The user inputs 0, and 0 exits the menu; if the user outputs a number other than the menu, price=0.0 is output;

First we need to learn two functions:

1:srand

The srand function is a seed function in C language. Its function is to generate a random number and use it as the seed parameter used in the next random number. It is mainly used to generate random number sequences that are not exactly the same. In C language, the srand function mainly sets the seed of the random number generator by using specified parameters, thereby setting the generated random numbers to a completely different sequence. srand uses 1 as the time seed. It is implemented through rand, but rand is a random number (the same sets of numbers are generated every time). In order to avoid duplication, the time function is introduced to generate random numbers. Its return value is the number of seconds from 00:00:00, 1970 to the present. Commonly used usage stand((unsigned)time(NULL) to achieve.

2SetConsoleTextAttribute

Use the windows.h librarySetConsoleTextAttribute

Function to change the color of a line of output.

The code is implemented as follows:

#include<stdio.h>
#include<windows.h>
int main() {
// SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN); // Make the next line output green font //
printf("Output this line green");
// SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED); // Make the next line output red font //
printf("Output this line in red");
return 0;
}

Now we can implement the code:

Enter use case (1):

Output use case (1):

Enter use case (2.1)

Output use case (2.1)

Enter the numbers game:

Enter the use case (2.2):

Output use case (2.2):

The total code is as follows:

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

int main() {
\t
//Corresponding price list//
printf("Our store operates \
");
printf("*************[1] apple *************\
");
printf("*************[2] pear *************\
");
printf("******************[3] orange ************\
");
printf("*************[4] grape *************\
");
printf("*************[0] exit *************\
");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN); // Make the next line output green font //
    printf("********* Anniversary Store Celebration 5% discount *********\
" when spending over 30 yuan.
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED); // Make the next line output red font //
printf("Warm reminder; If your purchase needs are met, press 0,0 to end\
");
printf("\
");
int i = 0;
float sum1 = 0;
float sum = 0;
int fu;
float jin;//Corresponding serial number input, total price//
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);
while (i < 5) {
printf("Please enter the query number weight (jin):\
");
scanf("%d %f", & amp;fu, & amp;jin);
if (fu == 1) {

sum = 3.00 * jin*1.0;
printf("price = 3.00\
The total price is: %f\
", sum);
}
else if (fu == 2) {
\t\t\t
sum = 2.50* jin;
printf("price = 2.50\
The total price is: %f\
",sum);

}
else if (fu == 3) {
\t\t\t
sum = 4.10 * jin*1.0;
printf("price = 4.10\
The total price is: %f\
", sum);

}
else if (fu == 4) {
\t\t\t
sum = 10.2 * jin*1.0;
printf("price = 10.20\
The total price is: %f\
", sum);

}
else if (fu == 0) {
break;
}
else {
\t
sum = 0.0 * jin;
printf("price = 0\
The total price is: %f\
", sum);

}
i + + ;
sum1 + =sum ;
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);
if (sum1 >= 30 & amp; & amp;sum1<100) {
float sum2 =sum1 * 0.95;
Sleep(1000);//Delay output//
printf("You need to pay in total: %.2f", sum2);
}
else if (sum >= 100) {//Introducing a lottery game (guessing numbers)//
printf("Get 1 game chance\
");
printf("Game Rules; The system randomly generates a number (positive integer) within 100. You have five chances to guess it. If you guess it right once (free of charge), you will get a 50% discount within three times (including three times). (Including five times) 25% off discount, 15% off if you didn’t guess correctly\
");
srand((unsigned int)time(NULL));
int r = rand() % 100 + 1;
int j = 0;
int b = 0;
int h = 0;
while (j < 5) {
printf("Please enter the number you guessed:\
");
scanf("%d", & amp;b);
h + + ;
if (r == b) {
if (h == 1) {
printf("Get the chance to get a free order");
float sum3 = sum1 * 1.0 * 0.0;
Sleep(1000);
printf("You need to pay a total of: %.2f yuan", sum3); break;
}
else if (h <= 3 & amp; & amp; h > 1) {
printf("Get 50% off\
");
float sum4 = sum1 * 1.0 * 0.5;
Sleep(1000);
printf("You need to pay a total of: %.2f yuan", sum4); break;
}
else if (h <= 5 & amp; & amp; h > 3) {
printf("Get 25% off\
");
float sum5 = sum1 * 1.0 * 0.75;
Sleep(1000);
printf("You need to pay a total of: %.2f yuan", sum5);
break;

}
}
else if (r > b) {
printf("Guess it's too small\
");
}
else if (r < b) {
printf("guessed it\
");
}
j + + ;
}

if (j == 5) {
printf("You did not guess correctly. The correct answer is %d\
", r);
float sum6 = sum1 * 1.0 * 0.85;
Sleep(1000);
printf("You need to pay a total of: %.2f yuan", sum6);

}
}
if (sum1<30.0){
Sleep(1000); //Delay output results//
printf("You need to pay a total of: %.2f yuan", sum1);
}


return 0;


}

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. C Skill Tree Home Page Overview 194088 people are learning the system