第三次上机作业

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

#include<stdio.h>
int main()
{
 int a,b,c,u1,u2;
 a=1;
 b=2;
 c=3;
 u1=a?b:c;
 u2=(a=2)?b+a:c+a;
 printf("%d\n",u1);
 printf("%d\n",u2);
}


运行结果如下:

任务2:假设整型变量a 的值是1 ,b 的值是2 ,c 的值是0 ,请判断各语句的值,写出执行结果,并作简短分析.

#include<stdio.h>
int main()
{
 int a,b,c,u1,u2,u3,u4,u5,u6;
 a=1;
 b=2;
 c=0;
 u1=a&&c;
 u2=a||c;
 u3=a||b;
 u4=b&&c;
 u5=a&&!((b||c)&&!a);
 u6=!(a&&b)||c?a||b:a&&b&&c;
 printf("%d\n%d\n%d\n%d\n%d\n%d\n",u1,u2,u3,u4,u5,u6);
}


运行结果如下

 

任务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

 

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

#include<stdio.h>
double dmax (double x, double y)  
{   
  if (x > y)   
      return x;   
  else   
      return y;   
 }   
  
void main()  
{  
  double a,b;  
  printf("Input 2 number:\n");  
  scanf("%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("%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 tmax(double a,double b,double c)
{
 if(a>b,a>c)
  return a;
 if(b>c,b>a)
  return b;
 if(c>a,c>b)
  return c;
}

void main()
{
 double x,y,z;
 printf("Input 3 number:\n");
 scanf("%lf %lf %lf",&x,&y,&z);
 printf("The max is:%lf\n",tmax(x,y,z));
}


运行结果如下:

 

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

设计思路:用for语句循环,不用一个个打。

#include<stdio.h>
int main()
{
 int a;
 for(int i=1;i<11;i++)
 {
  a=i;
  printf("%d\n",a);
 }
 return 0;
}


运行结果如下:

 

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

#include<stdio.h>
int main()
{
 int a;
 for(int i=10;i>-11;i--)
 {
  a=i;
  printf("%d\n",a);
 }
 return 0;
}


运行结果如下:

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值