任务一
目标:
1. 掌握C语言基本运算符和表达式用法;
2. 预习选择和重复控制语句的用法.
任务1:假设整型变量 a 的值是 1,b 的值是 2,c 的值是 3,在这种情况下分别执行下
面各个语句,写出执行对应语句后整型变量u 的值,做简短分析.
1) u = a ? b : c;
2) u = (a = 2) ? b + a : c + a;
#include<stdio.h>
void main()
{
int a,b,c;
int u;
a = 1;
b = 2;
c = 3;
u = a?b:c;
printf("%d\n",u);
int x;
x =(a=2)?b+a:c+a;
printf("%d\n",x);
}
输出结果2 、4
任务2:假设整型变量a 的值是1 ,b 的值是2 ,c 的值是0 ,写出下面各个表达式的值。做简短分析.
1) a && c
2) a || c
3) a || b
4) b && c
5) a && !((b || c) && !a)
6) !(a && b) || c ? a || b : a && b && c
输出结果011010
任务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<stdio.h>
#include<math.h>
#include<conio.h>
void main()
{
int u,x,y;
u = 3 * (2L + 4.5f) - 012 + 44 ;
x = 3 * (int)sqrt(144.0);
y = cos(2.5f + 4) - 6 *27L + 1526 - 2.4L ;
printf("%d %d %d\n",u,x,y);
}
输出结果53 36 1362
------------------------------------任务分割线------------------------------------
任务4:以下是两种“返回二个参数中最大的一个”的程序写法,理解并运行这两种写法,做简短分析
两种程序运行结果相同都如图所示:
说明一题有多种解,并不是唯一的。
-----------------------------------任务分割线------------------------------------
任务5:参考任务4,编写“返回三个参数中最大的一个”的程序,要求函数名为 double tmax(double, double, double),详细说明设计思路.
#include<stdio.h>
double dmax (double x, double y,double z)
{
if ((x > y)&&(x > z))
return x;
if ((y > z)&&(y > x))
return y;
if ((z > x)&&(z > y))
return z;
}
void main()
{
double a,b,c;
printf("Input 3 number:\n");
scanf("%lf %lf %lf",&a,&b,&c);
printf("The max is:%f \n",dmax(a,b,c));
}
截图
------------------------------------任务分割线------------------------------------
任务6: 写一个简单程序,它输出从1 到10的整数, 详细说明设计思路 。
#include<stdio.h>
void main()
{
int a;
int i;
for( i=1;i<=10;i++)
{
a=i;
printf("%d\n",a);
}
}
------------------------------------任务分割线------------------------------------
任务7: 写一个简单程序,它输出从10到-10的整数, 详细说明设计思路 。
#include<stdio.h>
void main()
{
int a;
int i;
for( i=-10;i<=10;i++)
{
a=i;
printf("%d\n",a);
}
}