1.基础回顾
sizeof 是C语言的关键字,计算指定数据类型的字节:sizeof(int) == 4
C语言不允许: int a = b = 0
单目运算符(取反、自增、逻辑运算符非!;从右往左)>双目(算术、移位、关系、逻辑{&&,||})>三目(条件运算符)> 赋值> ,(逗号)
逻辑 与 的优先级比逻辑 或 高
复合赋值: x op= m; ==> x = x op m; n*=m+1 n=n*(m+1)
--/++ 优先级大于 - ; m=-n++ ==> m=-n;n=n+1
(-n)++ 不合法,因为表达式不能自增
自动类型转换(优先级):
long double > float/double > unsigned long > long > unsigned int >int.char.short
简单例子:
#include<stdio.h>
main()
{
char ch;
printf("press a key :");
ch=getchar();
ch=ch+32;
putchar(ch);
putchar('y');
putchar('\n');
return 0;
}
简单例子2:
#include<stdio.h>
main()
{
char ch;
printf("press a key:");
ch=getchar();
ch=ch+32;
printf("%c,%d,%c\n",ch,ch,88);
}
/*
注:
scanf("%2d%2d",&a,&b); 此时输入1234 ,则a=12,b=13
%*2d 的 * 为忽略输入修饰符,表示输入项在读入后不赋予给相应变量
补充: 对于*c的使用,若前面有空格,回车,则可以加getchar(); 或者 scanf(" *c",&x);
*/
2.算法
关系运算符:>= 、>、 <、 <= 比 == 、 != 的优先级 高
如:
#include<stdio.h>
main(){
int a=0,b=5;
//(a>=1)&&(++b==5); 结果为b=5
(++b==5); //结果为b=6
printf("%d,%d",a,b);
}
条件运算符: 唯一的三目运算符, 表达式1 ? 表达式2 : 表达式3,结合性从右向左
例:
int x=15; y=x>10 ? x/10 : x>0 ? x : -x
先看后面 x>0?x:-x , y=x>10?x/10:15
再看x>10, y=1.5
#include<stdio.h>
main(){
int x=15;
float y= x>10 ? (float)(x)/10 : x>0 ? x : -x ;
printf("%f",y);
}
教材课本例子(苏小红教材)
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define eps 1e-6
main(){
float a,b,c,disc,p,q;
printf("input a,b,c:");
scanf("%f,%f,%f",&a,&b,&c);
if(fabs(a)<=eps){
printf("it is not a 二次方程");
exit(0);
}
disc=b*b-4*a*c;
p=(-b)/(2*a);
q=sqrt(fabs(disc))/(2*a);
if(fabs(disc)<=eps){
printf("x1=x2=%.2f\n",p);
}
else{
if(disc>eps){
printf("x1=%.2f,x2=%.2f",p+q,p-q);
}
else
{
printf("x1=%.2f+%.2fi",p,q);
printf("x2=%.2f-%.2fi\n",p,q);
}
}
return 0;
}
#include<stdio.h>
#include<math.h>
#define eps 1e-6
main(){
float a,b,c;
int flag = 1;
printf("input a,b,c:");
scanf("%f%f%f",&a,&b,&c);
if(a+b>c&&b+c>a&&a+b>b){
if(fabs(a-b)<=eps&&fabs(b-c)<=eps&&fabs(c-a)<=eps){
printf("等边");
flag = 0;
}
else if(fabs(a-b)<=eps||fabs(b-c)<=eps||fabs(c-a)<=eps){
printf("等腰");
flag = 0;
}
if(fabs(a*a+b*b-c*c)<=eps||fabs(a*a+c*c-b*b)<=eps||fabs(b*b+c*c-a*a)<=eps){
printf("直角");
flag = 0;
}
if(flag){
printf("一般");
}
printf("三角形");
}
else{
printf("不是三角形");
}
return 0;
}
------------------------------------------------------------------------下一段 循环