第三次上机实验

目标:
1. 掌握C语言基本运算符和表达式用法;
2. 预习选择和重复控制语句的用法.
任务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,b,c;
	int x,y;
	a=1,b=2,c=3;
	x=a?b:c;
	y=(a=2)?b+a:c+a;
	printf("%d\n%d\n",x,y);
}


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

 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 d,e,f;

d=a&&c;

e=a||c&&b;

f=a||c||(a&&b);

printf("%d\n%d\n%d\n",d,e,f);

}


 

 

------------------------------------任务分割线------------------------------------
任务3.  写程序计算以下各个表达式的值。
 说明: 程序头文件要添加 #include<math.h> 和  #include <conio.h>
   13 * (2L + 4.5f) - 012 + 44 
   23 * (int)sqrt(144.0) 
   3cos(2.5f + 4) - 6 *27L + 1526 - 2.4L 

#include<stdio.h>
#include<math.h>
#include<conio.h>
int main()
{
	double a,b,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("%lf\n%lf\n%lf",a,b,c);
}


------------------------------------任务分割线------------------------------------
任务4:以下两个程序都能实现了“取两个数最大值”算法,理解并分析两个程序的“主要不同”(注:有两个主要不同点).
写法一:
[cpp]view plaincopyprint?

double dmax (double x, double y)  

{   

  if (x > y)   

      return x;   

  else   

      return y;   

 }   

  

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 }  

.
写法二:

[cpp]view plaincopyprint?

16 double dmax (double x, double y);  

17 int main()  

18 {  

19  double a,b;  

20  printf("Input 2 number:\n");  

21  scanf_s("%lf %lf",&a,&b);  

22  printf("The max is:%f \n",dmax(a,b));  

23 }   

24 double dmax (double x, double y)  

25 {   

26   if (x > y)   

27       return x;   

28   if (x < y)   

29       return y;   

30  }  

写法一先定义函数,后直接调用;写法二先定义函数名,后定义函数,在定义的过程中调用函数。

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

#include<stdio.h>
#include<math.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>x&&z>y)
		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写一个简单程序,它输出从10的整数,详细说明设计思路。

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


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

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


 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值