1) x = a ? b : c;
2) y = (a = 2) ? b + a : c + a;
我的程序:
#include<stdio.h>
void main ()
{
int a,b,c;
a=1,b=2,c=3;
int x,y;
x=a?b:c;
y=(a=2)?b+a:c+a;
printf("%d\n",x);
printf("%d\n",y);
}
运行截图:
------------------------------------任务分割线------------------------------------
任务2:假设整型变量a 的值是1 ,b 的值是2 ,c 的值是0 ,请判断各语句的值,写出执行结果,并作简短分析.
1) a && c
2) a || c &&b
3) a || c|| (a && b)
我的程序:
#include<stdio.h>
void main ()
{
int a,b,c;
a=1,b=2,c=0;
int p,q,r,s,t;
p=a&&c;
q=a||c&&b;
r=a||c||(a&&c);
s=b&&c&&!a;
t=a&&!((b||c)&&!a);
printf("%d\n%d\n%d\n%d\n%d\n",p,q,r,s,t);
}
运行截图:
------------------------------------任务分割线------------------------------------
任务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()
{
float x,y,z;
x=3*(2L+4.5f)-012+44;
y=3*(int)sqrt(144.0);
z=cos(2.5f+4)-6*27L+1526-2.4L;
printf(" %f\n %f\n %f\n",x,y,z);
}
运行截图:
------------------------------------任务分割线------------------------------------
任务4:以下两个程序都能实现了“取两个数最大值”算法,理解并分析两个程序的不同.
写法一:
double dmax (double x, double y)
{
if (x > y)
return x;
else
return y;
}
int main()
{
double a,b;
printf("Input 2 number:\n");
scanf_s("%lf %lf",&a,&b);
printf("The max is:%f \n",dmax(a,b));
}
写法二:
double dmax (double x, double y);
int main()
{
double a,b;
printf("Input 2 number:\n");
scanf_s("%lf %lf",&a,&b);
printf("The max is:%f \n",dmax(a,b));
}
double dmax (double x, double y)
{
if (x > y)
return x;
if (x < y)
return y;
}
写法二是先编写的主运算,且用 if (x < y) 代替了else
-----------------------------------任务分割线------------------------------------
任务5:参考任务4,编写“返回三个参数中最大的一个”的程序,要求函数名为 double tmax(double, double, double),详细说明设计思路.
我的程序:
#include<stdio.h>
double tmax(double x,double y,double z)
{
{
if(x>y&&x)
return x;
if(y>x&&y>z)
return y;
}
return z;
}
int main()
{
double a,b,c;
printf("Input 3 number:\n");
scanf_s("%Lf %Lf %Lf",&a,&b,&c);
printf("The max is:%f\n",tmax(a,b,c));
}
运行截图:
------------------------------------任务分割线------------------------------------
任务6:写一个简单程序,它输出从1 到10的整数,详细说明设计思路。
用一个循环和++来实现
我的程序:
#include<stdio.h>
void main()
{
for(int i=1;i<=10;i++)
printf("%d\n",i);
}
运行截图:
------------------------------------任务分割线------------------------------------
任务7:写一个简单程序,它输出从-10 到10的整数,详细说明设计思路。
用一个循环和--来实现
我的程序:
#include<stdio.h>
void main()
{
for(int i=10;i>=-10;i--)
printf("%d\n",i);
}
运行截图: