第三次C程序上机报告

来源:互联网 发布:邓肯全部季后赛数据 编辑:程序博客网 时间:2024/05/18 00:23

任务一:

1)

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


程序运行结果:2

u=a?b:c表示:若a=0则是“非”,取c的数值;若a<>0则是“真”,取b的数值。其中,a=1,是“真”,则最后结果b=2。

2)

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


程序运行结果:4

u = (a = 2) ? b + a : c + a;表示:(a=2)<>1所以为“非”,则最后结果取c+a=4。

*******************************************************************************************************************************************

任务二:

1):a&&c,运行结果为:0

2):a||c,运行结果为:1

3):a||b,运行结果为:1

4):b&&c,运行结果为:0

5):a && !((b || c) && !a),运行结果为:1

6):!(a && b) || c ? a || b : a && b && c运行结果为:0

分析:“&&”表示:逻辑与、“||”表示:逻辑或、“!”表示:逻辑非。

*********************************************************************************************************************************************

任务三:

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


*************************************************************************************************************************************************

任务四:

#include<stdio.h>double dmax(double x,double y){if (x>y)return x;elsereturn 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;}


的区别:分析问题时的思考思路的先后顺序不同,但最后运行的结果一样的。 

*************************************************************************************************************************************************

任务五:

#include<stdio.h>double tmax (double x,double y,double z){if(x>y&&x>z)return x;if(y>x&&y>z)return y;elsereturn z;}int main(){double a,b,c;printf("Input 3 number:\n");scanf("%lf,%lf,%lf",&a,&b,&c);printf("The max is:%lf\n",tmax(a,b,c));}


运行结果:

设计思路:参照例四,将赋予的两个值改成三个值,主函数用double tmax,同时,采用if语句,在取y>x,y>z时中间用&&表并集。

***********************************************************************************************************************************************************************************************

任务六:

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


运行结果:

设计思路:这是一个循环增加问题,使用循环语句while。

**************************************************************************************************************************************************

任务七:

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


运行结果:

设计思路:同任务六使用循环语句while.

 

原创粉丝点击