C Language Beginner simple grammar comprehensive exercise

Source: Internet
Author: User

This exercise includes a one-dimensional array (with a character array) related knowledge, it is recommended to learn one-dimensional array after doing the following exercises, if you can independently complete the following exercise 80%, prove that you have a relatively skilled grasp of the relevant grammar, you can go to the function and the pointer part, if not successfully completed, please refer to the relevant procedures, Refer to the program I wrote, inevitably there are flaws, there are any questions please leave a message.

The list of topics is as follows:

Login Verification
User Mailbox Format Verification
Judging common year/leap years
Custom Pyramids
Calculator
Bubble Method Sort

1. Login Verification
Requirements: Impersonate the user login process, verify the user name, password and check code, if all correctly prompt the user to log on successfully, otherwise, prompt the user related input error.
Objective: To master the rules and principles of login check.

2. User Mailbox Format Verification
Requirement: The user enters the mailbox to verify that the mailbox is formatted correctly (verify that the user enters the email address that contains the character ' @ ').
Objective: To master the principle and method of the mailbox verification control.

3. Judge common year/Leap year
Requirement: After the user enters the year, the input is common year or leap years.
Objective: To master the method of if judgment.

4. Custom Pyramids
Requirements: Print in shape.
Graphic One
*
**
***
****
*****
Graphic Two
*******
*****
***
*

5. Calculator
Requirements: Can be added, minus, multiply, divide and take the remainder of the calculation.
Objective: To control the switch branch flexibly.

6. Sort by bubbling method
Requirements: Enter six student scores, then sort.
Objective: To master the basic methods of sorting, familiar with arrays.


Reference program:

1. Login Verification

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

int main (void)
{
Char username[20];
Char password[20];
Char checkcode[5];

printf ("Please enter user name:");
scanf ("%s", username);
printf ("Please enter password:");
scanf ("%s", password);
printf ("Please enter the Verification code: (1234)");
scanf ("%s", Checkcode);

if (!strcmp (Checkcode, "1234"))
{
if (!strcmp (username, "Yuanchunxu") &&!strcmp (password, "123456"))
{
printf ("Login successfully! \ n ");
}
Else
{
printf ("User name password is wrong!") \ n ");
}
}
Else
{
printf ("Captcha Error! \ n ");
}

System ("pause");
return 0;
}

2. User Mailbox Format Verification

Method One:

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

int main (void)
{
Char email[20];
int flag = 0;
int i = 0;

printf ("Please enter your email address:");

The main difference between scanf and get is that scanf the Space, tab, carriage return and so on as the default terminator, and gets the return as the default terminator.
scanf ("%s", email);
Gets (email);

Judge each, compare, see there is no ' @ ' sign
for (i = 0; i < strlen (email); i++)
{
if (' @ ' = = Email[i])
{
flag = 1;
Break
System ("pause");
return 0;
}
}

if (0 = = flag)
{
printf ("Illegal \ n");
}
Else
{
printf ("Legal \ n");
}

System ("pause");
return 0;
}

Method Two:

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

int main (void)
{
Char email[20];
int flag = 0; Flag bit method, available in many programs
int i = 0;

printf ("Please enter your email address:");
scanf ("%s", email);
Gets (email);

while (email[i]!= ')
{
if (' @ ' = = Email[i])
{
Find legal, set flag bit
flag = 1;
Break
}
i++;
}

Check the mark position and make different treatment according to the different mark
if (0 = = flag)
{
printf ("Illegal \ n");
}
Else
{
printf ("Legal \ n");
}

System ("pause");
return 0;
}

3. Judge common year/Leap year

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

int main (void)
{
int year = 0;

printf ("Please enter a year:");
scanf ("%d", &year);

if (0 = = year% 4)
{
if (0 = = year% 100)
{
if (0 = = year% 400)
{
printf ("%d is a leap year \ n", year);
}
Else
{
printf ("%d is common year \ n", year);
}
}
Else
{
printf ("%d is a leap year \ n", year);
}
}
Else
{
printf ("%d is common year \ n", year);
}

System ("pause");
return 0;
}

4. Custom Pyramids

Pattern One:
#include <stdio.h>
#include <stdlib.h>

int main (void)
{
int i = 1;
int j = 1;

Number of cycles determines rows
for (i = 0; i < 5; i++)
{
Number of cycles determined columns
for (j=0; J <= I; j + +)
{
printf ("*");
}
printf ("\ n");
}

System ("pause");
return 0;
}

Pattern Two:

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

int main (void)
{
int i = 0;
int k = 0;

Number of cycles determines rows
for (i = 0; i < 4; i++)
{
Number of cycles determined columns
for (k = 0; k <= 7-2 * i; k++)
{
printf ("*");

}
printf ("\ n");
}

System ("pause");
return 0;
}

5. Calculator

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

int main (void)
{
int NUM1 = 0;
int num2 = 0;
int result = 0;
Char OPR;
int flag=0;

printf ("Input operator:");
scanf ("%c", &AMP;OPR);

The user only has the correct input program to Exit while loop, this method can force the user to enter according to the rule of law
Do
{
printf ("Please enter two values:");
Flag = scanf ("%d,%d", &num1,&num2);
Fflush (stdin); Emptying the input cache
Rewind (stdin); This is another efficient way to empty the input cache, with different principles, but with the same effect
}while (0 = = flag);

Switch (OPR)
{
Case ' + ': Result=num1 + num2; Break
Case '-': result=num1-num2; Break
Case ' * ': RESULT=NUM1 * NUM2; Break
Case '/':
if (0 = = num2)
{
printf ("divisor cannot be zero \ n");
System ("pause");
return 0;
}
Else
{
result=num1/num2;
}
Break
Case '% ':
if (0 = = num2)
{
printf ("divisor cannot be zero \ n");
System ("pause");
return 0;
}
Else
{
Result=num1% Num2;
}
Break
default:printf ("Input wrong!");

}
printf ("%d%c%d=%d\n", Num1,opr,num2,result);

System ("pause");
return 0;
}

6. Sort by bubbling method

#include <stdio.h>
#include <stdlib.h>
#define COUNT 6

int main (void)
{
int Score[count] = {0};
int i = 0;
int k = 0;
int flag = 0;

for (i = 0; i < COUNT; i++)
{
User input is not valid and must be re-entered
Do
{
printf ("Current entry%d score", I);

FLAG=SCANF ("%d", &score[i]);
Fflush (stdin);
}while (0 = = flag);


}

Iterate over all elements before the array is sorted
for (i = 0; i < COUNT; i++)
{
printf ("score[%d] =%d\n", I, score[i]);
}

Kernal
The first cycle is the total number of bubbles
The second cycle is the number of times each bubble needs to be compared
for (i = 0; i < COUNT-1; i++)
{
for (k = 0; k < COUNT-1-I; k++)
{
if (Score[k] < Score[k + 1])
{
Swap Typical swap program
int temp = 0;
temp = Score[k];
SCORE[K] = score[k+1];
SCORE[K+1] = temp;
}
}
}

printf ("\ n------------------------------------\ n");

After sorting, iterating through all the elements in the array is advantageous to the comparison of the program
for (i = 0; i < COUNT; i++)
{
printf ("score[%d] =%d\n", I, score[i]);
}

System ("pause");
return 0;
}

This article is from the "8403723" blog, please be sure to keep this source http://8413723.blog.51cto.com/8403723/1716115

C Language Beginner simple grammar comprehensive exercise

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.