C语言上机练习参考详细答案.doc
文本预览下载声明
PAGE
PAGE 120
第1章 C语言概述
编写程序,在屏幕上显示一个如下输出: Programming in C is fun! I love C language.
Program
#include stdio.h
main()
{ printf(\n);
printf(Programming in C is fun!\n);
printf(I love C language.\n);
printf(\n);
}
编写程序,在屏幕上显示一个如下图案: * * * * * * * * * *
Program (1)
#include stdio.h
main()
{ printf(* * * *\n);
printf( * * *\n);
printf( * *\n);
printf( *\n );
}
Program (2)
#include stdio.h
main()
{ printf(%c%4c%4c%4c\n, *, *, *, *);
printf(%3c%4c%4c\n, *, *, *);
printf(%5c%4c\n, *, *
printf(%7c\n , *);
}
已知某个圆的半径,编写一个程序,用来计算并显示面积。要求:将π定义为符号常量,并假设一个恰当的半径值。
Program
#include stdio.h
#define PI 3.14
main()
{ float r=5, s;
s = PI*r*r;
printf(The area of circle is: %.2f\n, s);
}
Output
The area of circle is: 78.50
已知两个整数20和10,编写程序,自定义函数add( )将这两个数相加,自定义函数sub( )计算这两个数的差,并按照下面形式显示计算结果: 20+10=30 20-10=10
Program
#include stdio.h
int add(int a, int b)
{ return (a+b);
}
int sub(int a, int b)
{ return (a-b);
}
main()
{ int a=20, b=10;
printf(%d + %d = %d\n, a, b, add(a, b));
printf(%d - %d = %d\n, a, b, sub(a, b));
}
Output
20 + 10 = 30
20 – 10 = 10
已知变量a、b和c的值,编写程序,用来计算并显示x的值,其中请分别用以下数值运行该程序(1)a=250,b=85,c=25(2)a=300,b=70,c=80
Program (1)
#include stdio.h
main()
{ int a=250, b=85, c=25;
float x;
x=1.0*a/(b-c);
printf(x = %.2f\n, x);
}
Output (1)
x = 4.17
Program (2)
#include stdio.h
main()
{ int a=300, b=70, c=80;
float x;
x=1.0*a/(b-c); /*试写成x=a/(b-c); 得到什么运行结果?为什么?*/
printf(x = %.2f\n, x);
}
Output (2)
x = -30.00
第2章 常量、变量及数据类型 第3章 运算符和表达式
编写程序,求华氏温度100oF对应的摄氏温度。计算公式如下: 式中:c表示摄氏温度,f表示华氏温度。(c定义为实型,f定义为整型)
Program
#include stdio.h
main()
{ int f=100;
float c;
c=5.0*(f-32)/9; /*如果是c=5*(f-32)/9; 会是什么结果?为什么?*/
printf(Celsius degree (corresponding to %d Fahrenheit) is: %.2f.\n, f, c);
}
Output
Celsius degree (corresponding to 100 Fahrenheit) is: 37.78.
一个物体从100m的高空自由落下,编写程序,求它在前3s内下落的垂直距离。设重力加速度为10m/s2。要求,将重力加速度定义为符号常量,尝试将其改为9.8 m/s2,看结果有何不同?
Program
显示全部