您的位置:首页 > 其它

第3次实验任务

2013-03-31 13:25 134 查看
任务1:假设整型变量 a 的值是 1,b 的值是 2,c
的值是 3,请判断各语句的值,写出执行结果,并作简短分析.
  1)  x = a ? b : c; 
  2)  y = (a = 2) ? b + a : c + a;  

我的程序:

# include<stdio.h>
void main()
{
int a=1,b=2,c=3;
int x,y;
x= a ? b : c;
y=(a = 2) ? b + a : c + a;
printf("x=%d\ny=%d\n",x,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=1,b=2,c=0;
int x,y,z;
x=a && c;
y=a || c &&b ;
z=a || c|| (a && b) ;
printf("%d\n%d\n%d\n",x,y,z);
}


运行结果:



任务3. 
写程序计算以下各个表达式的值。

我的程序:

# include<stdio.h>
#include<math.h>
#include <conio.h>
void main()
{
float x=3*(2L+4.5f)-012+44;
float y=3*(int)sqrt(144.0);
float z=cos(2.5f+4)-6*27L+1526-2.4L;
printf("%f\n%f\n%f\n",x,y,z);
}


运行结果:



任务4:以下两个程序都能实现了“取两个数最大值”算法,理解并分析两个程序的不同.
写法一:

#include <stdio.h>
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));
}

写法二:

#include <stdio.h>
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;
}


运行结果:



任务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 > x && y > z)
return y;
if (z > y && z > x)
return z;
return 0;
}
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",dmax(a,b,c));
}


运行结果:



任务6:写一个简单程序,它输出从1
到10的整数,详细说明设计思路。

我的程序:
#include <stdio.h>
void main ()
{
int i;
i=1;
while (i<=10)
{printf ("%d\n",i);
i++;
}
}


运行结果:



参考5.2的while语句

任务7: 写一个简单程序,它输出从10到-10的整数,详细说明设计思路。

# include<stdio.h>
void main()
{
int i;
i=10;
while(i>=-10)
{printf("%d\n",i);
i--;
}
}


运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: