《C语言编程学习课件 (10).pdf》由会员分享,可在线阅读,更多相关《C语言编程学习课件 (10).pdf(20页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、Programming In CProgramming In C Programming In CProgramming In C The movie“Roaring Across the Horizon”Programming In CProgramming In C in mathematics you use x for multiplication,in C you use*.The*is used rather than to avoid any possible confusion with a variable called x.Note also that/,rather th
2、an,is used for division.Programming In CProgramming In C#include stdio.h void main()char s=7;int n=4;printf(7/4=%dn,s/n);printf(7.0/4=%fn,(float)s/n);outputoutput:7/4=1 7/4=1 not 1.75not 1.75 7.0/4=1.750000 7.0/4=1.750000 Programming In CProgramming In C The remainder operation is very interesting i
3、n C.The remainder operation is very interesting in C.The remainder is also called the modulus.Each time the hour hand reaches the 12 oclock,and it indicates that it has rotated once and the modulus becomes 0.Starting from 0,the modulus will increase by 1 in turn.Therefore,the modulus of the hour han
4、d ranges from 0 to 11;the modulus of the minute hand ranges from 0 to 59.Programming In CProgramming In C#include stdio.h#include math.h void main()char s=A,t;/generate the random capital letter t=s+rand()%26;printf(s=%c,t=%cn,s,t);Programming In CProgramming In C#include stdio.h#include math.h void
5、 main()char s=a,t;/generate the random lowercase letter t=s+rand()%26;printf(s=%c,t=%cn,s,t);Programming In CProgramming In C#include stdio.h#include math.h void main()char s=0,t;/generate the numeric symbols t=s+rand()%10;printf(s=%c,t=%cn,s,t);Programming In CProgramming In C How to simulate and g
6、enerate a license plate number in How to simulate and generate a license plate number in Kunming?Kunming?The modulus operation has not only the special operators,but also the restrictions on the two operands involved in the operation.It can only operate on integers,that is,integer and data of charac
7、ter type can find the modulus.The floating numbers cannot find the modulus.Find the modulus for a negative number,and the sign of the result is the same as the number before the percent.Programming In CProgramming In C -7%2 :-1 15%-4:3 With the operations of division and modulus,how to take out the
8、With the operations of division and modulus,how to take out the number of each digit of an integer?number of each digit of an integer?518/100 5 518/10%10 1 518%10 8 Type conversions and casts When doing calculations involving mixed data types,C ranks the data types in this order:promotion or widenin
9、g of the data For calculations involving mixed data types,C automatically converts the value in the lower data type to a higher type.Promotion will cause no loss of data,because the higher data types occupy more memory than the lower types and can therefore hold the data precisely.Type conversions a
10、nd casts Sometimes the automatic type conversion does not conform to our calculation intention.It requires the use of cast operations.(dataType)expression (float)(s/4)(float)s/4 1.000000 1.750000 Note:The operand of the cast operation is the value of the expression,not the variable itself.Operator p
11、recedence Consider the following statement:2+7 *8=72 var 2+7 *8=58 var Operator precedence 1)*,/have a higher priority than+、-2)Expressions containing operators of the same precedence are evaluated according to their associativity 3)use()()to change the order of evaluation Programming In CProgrammin
12、g In C#include stdio.h void main()char d=0;int a=9,b=-5,c=23;float x;x=(a*a+c/b)%d-3.5f/(d-20);printf(x=%fn,x);x=(a*a+c/b)%d-3.5f/(d-20);(9*9+23/-5)%48-3.5/(48-20)(81+23/-5)%48-3.5/(48-20)(81-4)%48-3.5/(48-20)77%48-3.5/(48-20)29-3.5/(48-20)29-3.5/28 29-0.125 28.875 Programming In CProgramming In C T
13、he common mathematical operations,such as square root,exponent,logarithm and trigonometric function,there are standard library functions in C to complete the corresponding calculations.We only need to call library functions correctly in expressions during programming.When calling a library function,
14、the function name must be correct,and the data type,number,and order of the arguments must be consistent with the system regulations.The function name is provided by the system.Programming In CProgramming In C Before calling a standard library function,you usually use precompiled commands to include
15、 the header file of the function in the source codes.sqrt(n)sqrt(n)pow(a,b)pow(a,b)fabs(x)fabs(x)sin(x)sin(x)rand()rand()#include “math.h”Programming In CProgramming In C Function Description Example Returned value Comments sqrt(x)Square root of x sqrt(16.000)4.000000 An integer values of x results
16、in a complier error pow(x,y)x raised to the y power(xy)pow(2,3)pow(81,.5)8.000000 9.000000 Integer values of x and y are permitted exp(x)e raised to the x power(ex)exp(-3.2)0.040762 An integer value of x results in a complier error Programming In CProgramming In C Function Description Example Return
17、ed value Comments log(x)Natural log of x base e log(18.697)2.928363 An integer value of x results in a complier error log10(x)Common log of x base 10 log(18.697)1.271772 An integer value of x results in a complier error fabs(x)Absolute value of x fabs(-3.5)3.5000000 An integer value of x results in
18、a complier error abs(x)Absolute value of x fabs(-2)2 An floating-point value of x return a value of 0 Programming In CProgramming In C#define PI 3.14159 ps=2*cos(2*PI/3*(a+b)*sin(PI/2*(a-b);x=(-b+sqrt(b*b-4*a*c)/(2*a);The symbols in the C expressions are written on one line,without subscripts,exponents,fractional line and other forms.The multiplication sign in algebra can be omitted,but*must be written in C expressions.Only parentheses can be used to change the precedence,not brackets or braces.Programming In CProgramming In C