目标:
1. 掌握C语言基本运算符和表达式用法;
2. 预习选择和重复控制语句的用法.
任务1:假设整型变量 a 的值是 1,b 的值是 2,c 的值是 3,请判断各语句的值,写出执行结果,并作简短分析.
1) x = a ? b : c;
2) y = (a = 2) ? b + a : c + a;
1) 2 2) 4
------------------------------------任务分割线------------------------------------
任务2:假设整型变量a 的值是1 ,b 的值是2 ,c 的值是0 ,请判断各语句的值,写出执行结果,并作简短分析.
1) a && c
2) a || c &&b
3) a || c|| (a && b)
1) 0 2)1 3) 1
任务3. 写程序计算以下各个表达式的值。
说明: 程序头文件要添加 #include<math.h> 和 #include <conio.h>
1)3 * (2L + 4.5f) - 012 + 44
2)3 * (int)sqrt(144.0)
3)cos(2.5f + 4) - 6 *27L + 1526 - 2.4L
#include "stdafx.h"
#include<math.h>
#include <conio.h>
void main()
{
float a;
float b;
float c;
a=3 * (2L + 4.5f) - 012 + 44;
b=3 * (int)sqrt(144.0);
c=cos(2.5f + 4) - 6 *27L + 1526 - 2.4L;
printf("%f\n",a);
printf("%f\n",b);
printf("%f\n",c);
}
任务4:以下两个程序都能实现了“取两个数最大值”算法,理解并分析两个程序的不同.
一个是用了两个比较,一个是用了if else语句。
任务5:参考任务4,编写“返回三个参数中最大的一个”的程序,要求函数名为 double tmax(double, double, double),详细说明设计思路.
通过两次比较(if)确定最大的一个值。
#include "stdafx.h"
double tmax (double a, double b,double c)
{
if(a>b&&a>c)
return a;
if(a<b&&c<b)
return b;
if(a<c&&b<c)
return c;
}
int main()
{
double x,y,z,f;
printf("Input 3 number:\n");
scanf_s("%lf %lf %lf",&x,&y,&z);
f=tmax(x,y,z);
printf("The max is: %lf \n",f);
}
任务6:写一个简单程序,它输出从1 到10的整数,详细说明设计思路。
用while循环,在a<11的时候执行自增长代码并打印。
#include"stdafx.h"
void main()
{
int a;
int b;
a=1
while(a<11)
{ b=a;
a++;
printf("b=%d\n",b);
}
}
任务7: 写一个简单程序,它输出从10到-10的整数,详细说明设计思路。
用for循环,从a=-10开始,每当a<11,执行a的自增长命令,然后定义b=a,打印b
#include"stdafx.h"
void main()
{
int a=-10;
int b;
for(a=-10;a<11;a++)
{
b=a;
printf("b=%d\n",b);
}
}