Basic C language examination questions

Source: Internet
Author: User

Basic C language examination questions

//

// Main. m

// C Language Test Library

//

// Created by MRBean on 15/5/20.

// Copyright (c) 2015 MRBean. All rights reserved.

//

 

# Import

 

/** 5, 12 questions

* Determine whether a number is a prime number.

*

* @ Param x indicates the number to be determined.

*

* @ Return 0 indicates that the return value is not a prime number, and 1 indicates a prime number.

*/

Int isZhiShu (int x)

{

Int count = 0;

For (int I = 1; I <= x; I ++ ){

If (x % I = 0 ){

Count ++;

}

}

If (count = 2 ){

Return 1;

}

Return 0;

}

 

// ================================================ ======================================

 

/**

* 6.

* Write a function to calculate the Triangle Area.

*/

Typedef struct SanJiaoXing

{

Double a; // the length of an edge of a triangle

Double B;

Double c;

 

} SanJiaoXing; // defines a struct variable. The variable type is SanJiaoXing.

 

Double mianJi (SanJiaoXing s) // 6th calculate the area of the triangle

{

 

Double area = 0; // area of the triangle

If (s. a + s. b> s. c & s. a + s. c> s. B & s. B + s. c> s. a) // The condition for forming a triangle is that the sum of any two sides is greater than the third side.

{

Double r = (s. a + s. B + s. c)/2;

Area = sqrt (r * (r-s.a) * (r-s. B) * (r-s.c); // use the Helen formula to calculate the area

Printf ("the Triangle area is % g", area );

Return area;

}

Else

{

Printf ("cannot form a triangle! ");

Return area;

 

}

 

 

}

// ================================================ ==========

 

/**

* 7. Determine whether the circle overlaps

*

*/

 

// Define a circular struct

Typedef struct Circle

{

Double x; // x coordinate of the center

Double y; // y coordinate of the center

Double r; // radius of the circle

} Circle;

 

Int chongDie (Circle a, Circle B)

{

Double distance = sqrt (pow (a. x-b.x, 2) + pow (a. y-b.y, 2); // distance between Centers

Double radiusSum = a. r + B. r;

If (distance

{

Printf ("two circles overlap! \ N ");

Return 1; // overlap

}

Else {

Printf ("two circles do not overlap! \ N ");

Return 0; // No overlap

 

}

}

 

// ==============================================

/**

* 8. String Encryption

*

*/

 

Char * JiaMi (char str [])

{

For (int I = 0; str [I]; I ++ ){

If (str [I] = 'Z '){

Str [I] = 'a ';

}

Else if (str [I] = 'Z ')

{

Str [I] = 'Z ';

}

Else if (str [I]> = 'A' & str [I] <= 'y ') | (str [I]> = 'A' & str [I] <= 'y '))

{

Str [I] ++;

}

 

}

Printf ("the encrypted string is % s", str );

Return str;

}

 

// ================================================ ==========

/**

* 11. define a three vertex (using a struct) to determine whether the three points can form a triangle. If a triangle can be formed, calculate the area of the triangle (Helen formula ); otherwise, print "cannot constitute a triangle ".

*

*/

 

// Define a point struct type

Typedef struct

{

Double x;

Double y;

} MPoint;

 

// Calculate the area of a triangle based on the three vertices.

Double mianJi2 (MPoint a, MPoint B, MPoint c)

{

Double len1 = sqrt (pow (a. x-b.x, 2) + pow (a. y-b.y, 2); // The distance between the AB

Double len2 = sqrt (pow (a. x-c.x, 2) + pow (a. y-c.y, 2); // distance between the ac

Double len3 = sqrt (pow (c. x-b.x, 2) + pow (c. y-b.y, 2); // The distance between cb

Double area = 0; // area of the triangle

If (len1 + len2> len3 & len1 + len3> len2 & len2 + len3> len1)

{

Double r = (len1 + len2 + len3)/2;

Area = sqrt (r * (r-len1) * (r-len2) * (r-len3 ));

Printf ("the Triangle area is % g", area );

Return area;

}

 

Else

{

Printf ("these three points cannot constitute a triangle! ");

Return area;

}

 

}

 

 

// ================================================ ==============

 

 

 

 

 

 

 

 

 

Int main (int argc, const char * argv []) {

 

// ================================================ ====

/**

* 1. Enter two positive integers m and n. Output the maximum public approx. And the minimum public multiples.

* Calculate the maximum common divisor c of two numbers a and B using the moving phase division.

*/

 

// Int m;

// Int n;

// Printf ("enter two numbers: \ n ");

// Scanf ("% d", & m, & n );

// Int temp = m * n; // store the product of m * n to calculate the minimum public multiple.

// Int c = m % n;

// While (c! = 0 ){

// M = n; // Divisor

// N = c; // the remainder is used as the divisor.

// C = m % n;

//}

// Printf ("maximum common divisor: % d, minimum common multiple % d \ n", n, temp/n );

 

// ================================================ ==========

/**

* 2. Enter a string to count the numbers of uppercase and lowercase letters.

*/

 

// Char str [1000];

// Int count1 = 0; // number of lower case Cases

// Int count2 = 0; // uppercase

// Printf ("enter a string \ n ");

// Gets (str );

// For (int I = 0; str [I]! = '\ 0'; I ++ ){

// If (str [I]> = 'A' & str [I] <= 'Z '){

// Count1 ++;

//}

// Else if (str [I]> = 'A' & str [I] <= 'Z '){

// Count2 ++;

//}

//}

// Printf ("the number of uppercase letters is % d, and the number of lowercase letters is % d", count1, count2 );

 

// ================================================ ============

 

/**

* 3. Enter two positive integers. program a and program n output the values of a + aa + aaa +... (a... a) (n ).

*/

 

// Printf ("enter two integers \ n ");

// Int;

// Int n;

// Int fac = 0; // expression in the form of a, aa, aaa...

// Int sum = 0; // calculate and

// Scanf ("% d", & a, & n );

// For (int I = 0; I

//{

// Fac = fac * 10 + a; // aa = a * 10 + a, and the last one equals the previous number * 10 +

// Sum + = fac;

//

//}

// Printf ("a + aa + aaa... = % d \ n", sum );

 

// ================================================ ========================

 

 

/**

* 4. Enter a positive integer n, and return 1! + 2! +... N! Value (n! Represents the factorial of n)

*/

 

// Int sum = 0; // The sum of the recorded factorial

// Int fac = 1; // records the factorial of a number.

// Int n; // Number Limit

// Printf ("enter an integer not greater than 10 \ n ");

// Scanf ("% d", & n );

// For (int I = 1; I <= n; I ++ ){

// Fac * = I;

// Sum + = fac;

//}

//

// Printf ("1! + 2! +... + N! = % D ", sum );

 

 

// ================================================ ================

 

/**

* 5. Write a function to determine the prime number, input an integer in the main function, and output the information about whether it is a prime number.

*/

 

// Int n;

// Printf ("enter an integer ");

// Scanf ("% d", & n );

// If (isZhiShu (n) = 1 ){

// Printf ("is a prime number! \ N ");

//}

// Else

//{

// Printf ("not a prime number! ");

//}

 

// ================================================ ================

 

 

/**

* 6. Define a struct that contains three sides of a triangle. Write a function, input the struct variable, and return the Triangle Area (Note whether the triangle is valid)

*/

 

// SanJiaoXing s;

// Printf ("Enter the three sides of the triangle \ n ");

// Scanf ("% lf", & s. a, & s. B, & s. c );

// MianJi (s); // calculate the Triangle Area

//

 

 

// ================================================ ======================================

 

 

/**

* 7. Define a struct that contains the center and radius. Write a function, input two struct variables, and return whether the two circles overlap.

*/

 

 

// Circle a; // The First Circle

// Circle B; // The second Circle

// Printf ("Enter the center and radius of the first circle \ n ");

// Scanf ("% lf", & a. x, & a. y, & a. r );

// Printf ("Enter the center and radius of the second circle \ n ");

// Scanf ("% lf", & B. x, & B. y, & B. r );

// ChongDie (a, B );

 

// ================================================ ======================================

 

/**

* 8. Write a function to encrypt the string. The encryption rule is a-> B z-> a and so on.

*/

 

// Char s [1000];

// Gets (s );

// JiaMi (s );

 

// ================================================ ======================================

 

/**

* 9. If you enter 10 numbers, the maximum and minimum values are returned.

*/

 

// Int a [10];

// Int max = 0;

// Int min = 0;

// Printf ("input 10 numbers \ n ");

// For (int I = 0; I <10; I ++)

//{

// Scanf ("% d", & a [I]);

// If (I = 0)

//{

// Max = a [0];

// Min = a [0];

//}

//

// If (max

//{

// Max = a [I];

//}

//

// If (min> a [I])

//{

// Min = a [I];

//}

//

//}

// Printf ("maximum value: % d \ n minimum value: % d \ n", max, min );

//

// Printf ("Enter ten \ n ");

// Int a [10];

// For (int I = 0; I <10; I ++ ){

// Scanf ("% d", & a [I]);

//}

// Int max = a [0]; // maximum value

// Int min = a [0]; // minimum value

// For (int I = 1; I <10; I ++ ){

// If (max

// Max = a [I];

//}

// If (min> a [I]) {

// Min = a [I];

//}

//}

//

// Printf ("maximum value is % d, minimum value is % d", max, min );

//

// ================================================ ==================================

 

/**

*

 

10. The following code has problems. Please point out

 

Int * p;

{

Int a; // a is released after parentheses.

P = &;

}

* P = 3; // The problem is that the wild pointer is abnormal.

 

*/

// ================================================ ==================================

 

/**

* 11. define a three vertex (using a struct) to determine whether the three points can form a triangle. If a triangle can be formed, calculate the area of the triangle (Helen formula ); otherwise, print "cannot constitute a triangle ".

*/

 

// MPoint a; // vertex of the triangle

// MPoint B;

// MPoint c;

// Printf ("Enter three vertices in sequence! \ N ");

// Scanf ("% lf", &. x, &. y, & B. x, & B. y, & c. x, & c. y );

// MianJi2 (a, B, c );

 

 

// ================================================ ==================================

 

/**

* 12. Print up to 100 prime numbers

*/

 

// Int count = 0; // number of printed prime numbers

// For (int I = 1; count <= 100; I ++) // The cycle condition is in count <= 100, that is, if no 100 prime numbers are found, the loop goes on.

//{

// If (isZhiShu (I) = 1) {// a prime number is encountered.

// Printf ("% d", I );

// Count ++;

//}

//

//}

 

// ================================================ ==================================

 

/**

* 13. Enter 10 numbers and calculate the average value.

*/

 

// Double a [10]; // used to save 10 numbers

// Double avg; // Average Value

// Double sum = 0.0;

// Printf ("Enter ten \ n ");

// For (int I = 0; I <10; I ++)

//{

// Scanf ("% lf", & a [I]);

// Sum + = a [I];

//}

//

// Avg = sum/10;

// Printf ("average value: % g", avg );

 

// ================================================ ================

 

/**

* 14. Calculate the result of 1-2 + 3-4 + 5-6 +... 100

*/

 

// Int sum = 0;

// For (int I = 1; I <= 100; I ++)

//{

// If (I % 2 = 0)

//{

// Sum-= I;

//}

// Else

//{

// Sum + = I;

//}

//}

//

// Printf ("and % d", sum );

 

// ================================================ ========================

 

 

/**

* 15. The input positive integer n program outputs the value of 1/2 + 2/3 +... n/n + 1.

*/

 

 

// Int sum = 0;

// Int n;

// Printf ("enter a positive integer! \ N ");

// Scanf ("% d", & n );

// For (int I = 1; I <= n; I ++)

//{

// Sum + = I/(I + 1 );

//}

// Printf ("the sum of calculation is % d", sum );

 

// ================================================ ==================================

Return 0;

}

Related Article

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.