第三次实验任务

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

分析:取u=b,C语言中,“0”为非真,其余数字全为真,在程序中,“u=a=1”,为真,输出b。

    2)执行结果:4 

分析:取u=c+a,C语言中,“0”为非真,其余数字全为真,在程序中,a=1而不是=2,所以为假,输出c+a。


        ------------------------------------任务分割线------------------------------------

任务2:

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

1)  a && c
2)  a || c &&b 
3)  a || c|| (a && b) 

1) 执行结果:0,  

分析:没有交集,为假,输出0; 

2)执行结果:1,

分析:有交集,为真,输出1;

3)执行结果:1,

分析:有交集,为真,输出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 


我编辑的程序如下:

  1. #include<stdio.h>   
  2. #include<math.h>   
  3. #include<conio.h>   
  4.   
  5.   
  6. void main()  
  7. {  
  8.     float a;  
  9.     int b;  
  10.     double c;  
  11.     a=3*(2L+4.5f)-012+44;  
  12.     b=3*(int)sqrt(144.0);  
  13.     c=cos(2.5f+4)-6*27L+1526-2.4L;  
  14.     printf("%f\n%d\n%lf\n",a,b,c);  
  15. }  
#include<stdio.h>
#include<math.h>
#include<conio.h>


void main()
{
	float a;
	int b;
	double 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%d\n%lf\n",a,b,c);
}

执行结果如图:

                                         

------------------------------------任务分割线------------------------------------


任务4:

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

写法一

  1. double dmax (double x, double y)    
  2. {     
  3.   if (x > y)     
  4.       return x;     
  5.   else     
  6.       return y;     
  7.  }     
  8.     
  9. int main()    
  10. {    
  11.   double a,b;    
  12.   printf("Input 2 number:\n");    
  13.   scanf_s("%lf %lf",&a,&b);    
  14.   printf("The max is:%f \n",dmax(a,b));    
  15. }    
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));  
}  


写法二:

  1. double dmax (double x, double y);    
  2. int main()    
  3. {    
  4.  double a,b;    
  5.  printf("Input 2 number:\n");    
  6.  scanf_s("%lf %lf",&a,&b);    
  7.  printf("The max is:%f \n",dmax(a,b));    
  8. }     
  9. double dmax (double x, double y)    
  10. {     
  11.   if (x > y)     
  12.       return x;     
  13.   if (x < y)     
  14.       return y;     
  15.  }    
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;   
 }  

用else取代了if(x<y)从而使程序有所简化。

                ------------------------------------任务分割线------------------------------------

                  

任务5:

参考任务4,编写“返回三个参数中最大的一个”的程序,要求函数名为 double tmax(double, double, double),详细说明设计思路.

思路如下:根据任务四的步骤,先写出我们所需的条件函数,再编写程序,把之前的条件函数运用进程序的判断中,使程序能输出我们想要的结果。

  1. #include<stdio.h>     
  2. double dmax(double x, double y, double z)    
  3. {    
  4.     if(x>y&&x>z)    
  5.         return x;    
  6.     if(y>x&&y>z)    
  7.         return y;    
  8.     else    
  9.         return z;    
  10. }    
  11.     
  12. void main()    
  13. {    
  14.      double a,b,c;      
  15.      printf("Input 3 number:\n");      
  16.      scanf("%lf %lf %lf",&a,&b,&c);      
  17.      printf("The max is:%f \n",dmax(a,b,c));      
  18. }      
#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;  
    else  
        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的整数,详细说明设计思路。

设计思路如下:整数从1开始,把变量先赋值为1,每次增加1(a++)直到a=10,则运用循环。

我编辑的程序如下:

  1. #include<stdio.h>     
  2.     
  3. void main()    
  4. {    
  5.     int a=1;    
  6.         while(a<=10)    
  7.         {    
  8.             printf("%d\n",a) ;    
  9.             a++;    
  10.         }    
  11. }      
#include<stdio.h>  
  
void main()  
{  
    int a=1;  
        while(a<=10)  
        {  
            printf("%d\n",a) ;  
            a++;  
        }  
}    


执行结果如图:



         ------------------------------------任务分割线------------------------------------

任务7:
 写一个简单程序,它输出从10到-10的整数,详细说明设计思路。
设计思路:与任务6设计思路同理。
我编辑的程序如下:
  1. #include<stdio.h>     
  2.     
  3. void main()    
  4. {    
  5.     int a=10;    
  6.         while(a>=-10)    
  7.         {    
  8.             printf("%d\n",a) ;    
  9.             a--;    
  10.         }    
  11. }      
#include<stdio.h>  
  
void main()  
{  
    int a=10;  
        while(a>=-10)  
        {  
            printf("%d\n",a) ;  
            a--;  
        }  
}    

执行结果如图:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值