1、2022-11-112/862022-11-113/86并列的两个函数并列的两个函数其中主函数是其中主函数是程序的入口程序的入口程序注释程序注释编译预处理命令编译预处理命令2022-11-114/862022-11-115/862022-11-116/862022-11-117/86长整型长整型无符号整型无符号整型八进制八进制十六进制十六进制单精度实型单精度实型长双精度实型长双精度实型十进制小十进制小数数指数形式指数形式十进制十进制2022-11-118/862022-11-119/862022-11-1110/86b,98B,66b,98B,662022-11-1111/862022-11-
2、1112/862022-11-1113/862022-11-1114/86结果会是什么?结果会是什么?2022-11-1115/86Algorithm Syntax Rules:类型一致类型一致Expressions type must be the same as variables typeint x;int y;x=12;y=5.75;2022-11-1116/86int number1,number2;number1=25;number2=23;number1=number2;number1?number2?2523232022-11-1117/86Example:Calculate
3、and display the price of a number of apples if the quantity in kg and price per kg are given.Input:quantity and pricePerkgOutput:priceProcess:price=quantity*pricePerkg2022-11-1118/86Example:int quantity;float pricePerkg,price;quantity=5;pricePerkg=4.50;price=quantity*pricePerkg;2022-11-1119/86Exampl
4、e:int quantity;float pricePerkg,price;quantity=2;pricePerkg=4.50;price=quantity*pricePerkg;quantity?pricePerkg?price?4.509.0022022-11-1120/86Example:2022-11-1121/862022-11-1122/86数据类型数据类型基本类型基本类型构造类型构造类型指针类型指针类型空类型空类型整型整型实型(浮点型)实型(浮点型)字符型字符型枚举类型枚举类型数组类型数组类型结构体类型结构体类型共用体类型共用体类型单精度实型双精度实型基本整型长整型短整型无符号
5、整型2022-11-1123/862022-11-1124/862022-11-1125/862022-11-1126/862022-11-1127/862022-11-1128/862022-11-1129/862022-11-1130/862022-11-1131/86#include void main()printf(Data type Number of bytesn);printf(-n);printf(char%dn,sizeof(char);printf(int%dn,sizeof(int);printf(short int%dn,sizeof(short);printf(lo
6、ng int%dn,sizeof(long);printf(float%dn,sizeof(float);printf(double%dn,sizeof(double);2022-11-1132/862022-11-1133/86#include main()float price,discount,total;printf(“Buying price:“);scanf(“%f”,&price);printf(“nDiscount rate:“);scanf(“%f”,&discount);total=price*discount;printf(“The total price is%.2fn
7、”,total);Buying price:_discount?price?total?Buying price:10.00_10.00Buying price:10.00Discount rate:_Buying price:10.00Discount rate:0.25_0.252.50Buying price:10.00Discount rate:0.25_Buying price:10.00Discount rate:0.25The total price is 2.50_Example:2022-11-1134/86 a=123 type(a)a=abc type(a)变量是一个对象
8、的变量是一个对象的别名、指针或引用别名、指针或引用赋值是重新绑定赋值是重新绑定int a;a=123;a=abc;变量是数据的内变量是数据的内存地址名,存地址名,赋值是改写赋值是改写PythonC2022-11-1135/862022-11-1136/86Example:int a,b;float x,y;.a=b=0;x=y=100.0;b?a?x?y?00100.0100.02022-11-1137/862022-11-1138/86Example:2022-11-1139/862022-11-1140/86Example:浮点数除法浮点数除法(Floating Division)or o
9、r both are floats整数除法整数除法(Integer Division)and are integers2022-11-1141/86Example:Python/float division/integer division对Python 2.x版本 当a/b同为整数时,a/b等价于a/b来处理以避免出错2022-11-1142/86Example:2022-11-1143/862022-11-1144/862022-11-1145/86Example:-115-2-10-1remainderresult2022-11-1146/86Example:11-5-210 1rema
10、inderresult2022-11-1147/86Example:2022-11-1148/862022-11-1149/862022-11-1150/86Example:2022-11-1151/86Example:2022-11-1152/86Example:2022-11-1153/862022-11-1154/862022-11-1155/862022-11-1156/862022-11-1157/86Example:num=num+5;num1520Example:num+=5;2022-11-1158/86OperationExamples of expressionDescri
11、ption+=num+=5;num=num+5;-=num-=5;num=num 5;*=num*=5;num=num*5;/=num/=5;num=num/5;%=num%=5;num=num%5;2022-11-1159/862022-11-1160/86执行执行 a+=a-=a*=a 后,变量后,变量a的值?的值?a+=a-=a*=a a+=a-=9 a+=0 a=02022-11-1161/862022-11-1162/862022-11-1163/86Example:j=+i-2i5similar to j?462022-11-1164/86Example:j=i+-2i5simil
12、ar to j?362022-11-1165/86a3similar to 42022-11-1166/862022-11-1167/86circumference=33.300854area=88.2472632022-11-1168/86如何从键盘读取半径值?如何从键盘读取半径值?Input r:5.3 circumference=33.300854 area=88.247263在程序中直接使用的常数称在程序中直接使用的常数称为幻数为幻数(Magic Number)2022-11-1169/862022-11-1170/862022-11-1171/862022-11-1172/86#in
13、clude#define PI 3.14159;#define R 5.3;main()printf(area=%fn,PI*R*R);printf(circumference=%fn,2*PI*R);相当于执行相当于执行#include main()printf(area=%fn,3.14159;*5.3;*5.3;);printf(circumference=%fn,2*3.14159;*5.3;);2022-11-1173/862022-11-1174/86doublefloatlongunsignedintchar,short低高2022-11-1175/862022-11-1176/
14、862022-11-1177/86Example:int x=10;float y;y=(float)x;x10y?10.0000002022-11-1178/86Example:int total,number;float average;average=total/number;total15number2average?7.000000两个整数运算的结果两个整数运算的结果还是整数,不是浮点数还是整数,不是浮点数2022-11-1179/86Example:int total,number;float average;average=(float)total/number;total15number2average?7.5000002022-11-1180/86m/2=2(float)(m/2)=2.000000(float)m/2=2.500000m=52022-11-1181/862022-11-1182/862022-11-1183/86Input a,b,c:3,4,5 area=6.0000002022-11-1184/862022-11-1185/86