⏹第1章 C语言概述
1-1编写程序,在屏幕上显示一个如下输出:
---------------------------------
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");
}
1-2编写程序,在屏幕上显示一个如下图案:
* * * *
* * *
* *
*
Program (1)
不给力的奥特曼#include <stdio.h>
main()
{ printf("* * * *\n");
printf(" * * *\n");
printf(" * *\n");
printf(" *\n ");
}
rca
Program (2)
#include <stdio.h>
main()
{ printf("%c%4c%4c%4c\n", '*', '*', '*', '*');
printf("%3c%4c%4c\n", '*', '*', '*');最经典的英文歌
printf("%5c%4c\n", '*', '*');
printf("%7c\n ", '*');
}
1-3已知某个圆的半径,编写一个程序,用来计算并显示面积。
要求:将π定义为符号常量,并假设一个恰当的半径值。
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
1-4已知两个整数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
1-5已知变量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章 运算符和表达式
3-1编写程序,求华氏温度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.
3-2一个物体从100m的高空自由落下,编写程序,求它在前3s内下落的垂直距离。设重力加速度为10m/s2。
要求,将重力加速度定义为符号常量,尝试将其改为9.8 m/s2,看结果有何不同?
Program
#include <stdio.h>
#define G 10
main()
{ int t=3;
float s;
s=1.0/2*G*t*t; /*如果是s=1/2*G*t*t; 会是什么结果?为什么?*/
printf("The falling vertical distance (in %d conds) is: %.2f.\n"beloved是什么意思, t, s);
}
Output
The faccompany用法alling vertical distance (in 3 conds) is:45.00.
3-3将球的半径R定义为符号常量,计算球的表面积(4πR2)和体积(4/3*πR3)。
Program
#include <stdio.h>
#define R 5.2
#define PI 3.14
main()
{ float s, v;
s=4*PI*R*R;
v=4.0/3*PI*R*R*R;
printf("The surface area of the ball (radius is %.2f) is: %.2f, and the volume is: %.2f.\n", R, s, v);
}
Output
The surface area of the ball (radius is 5.20) is: 339.62, and the volume 588.68.
3-4给定x、y和z的值,编写程序,使x等于y的值,y等于z的值,z等于x的值。
Program
#include <stdio.h>
main()
{ int x=1, y=2, z=3, t;
printf("Before swap: x=%d, y=%d, z=%d.\n", x, y, z);
t=x;
x=y;
liberal
y=z;
z=t; /*变量t的作用是什么?*/
printf("After swap: x=%d, y=%d, z=%d.\n", x, y, z);
}
Output
Before swap: x=1, y=2, z=3.
After swap: x=2, y=3, z=1.
3-5编写一个程序,给定一个浮点数(例如456.78),显示该数的十位数字与个位数字之和(例如5+6=11)。
Program (1)
#include <stdio.h>
main()
{ float f=456.78;
int n, a, b;manabe
n=f;一直英文
a = n%10; /*赋值后,a是什么值?*/
b = n/10%10; /*赋值后,b是什么值?*/
printf("The sum of the tens digit and units digit of %.2f is: %d + %d = %d.\n", f, b, a, a+b);
}
Program (2)
#include <stdio.h>
main()
{ float f=456.78;
int n, a, b;
n=f;
a = n%10;
b = n%100/10; /*该语句与上面程序不同,看看有何区别?*/
printf("The sum of the tens digit and units digit of %.2f is: %d + %d = %d.\n", f, b, a, a+b);