自己练习时手写,难免会有些疏忽遗漏等各种各样问题,错误之处还请指出
但这些代码确实已通过编译,实现了书上的输出结果,还希望能给抱有期待之人作为个小参考
6.1
#include
int main (void)
{
float num, max = 0.0f;
printf ("Enter a number: ");
scanf ("%f", &num);
for (; num > 0.0f; ) {
if (num > max)
max = num;
printf ("Enter a number: ");
scanf ("%f", &num);
}
printf ("The largest number entered was %.2f", max);
return 0;
}
6.2
#include
int main (void)
{
int m, n;
int temp = 0;
printf ("Enter two integers: ");
scanf ("%d%d", &m, &n);
while (n != 0) {
temp = n;
n = m % n;
m = temp;
}
printf ("Greatest common divisor: %d", m);
return 0;
}
6.3
#include
int main (void)
{
int denom, num;
int n, m;
int temp;
printf ("Enter a fraction: ");
scanf ("%d/%d", &num, &denom);
m = denom;
n = num;
while (n != 0) {
temp = n;
n = m % n;
m = temp;
}
denom = denom / m;
num = num / m;
printf ("In lowest terms: %d/%d", num, denom);
return 0;
}
6.4
#include
int main (void)
{
float value, commission = 0.0f;
printf ("Enter value of trade: ");
scanf ("%f", &value);
while (value != 0.0f) {
if (value < 2500.0f){
commission = 30.0f + 0.017f * value;
} else if (value < 6250.0f) {
commission = 56.0f + 0.0066f * value;
} else if (value < 20000.0f) {
commission = 76.0f + 0.0034f * value;
} else if (value < 50000.0f) {
commission = 100.0f + 0.0022f * value;
} else if (value < 500000.0f) {
commission = 155.0f + 0.0011f * value;
} else {
commission = 255.0f + 0.0009f * value;
}
if (commission < 39) commission = 39;
printf ("Commission: $%.2f\n\n", commission);
printf ("Enter value of trade: ");
scanf ("%f", &value);
}
return 0;
}
6.5
#include
int main (void)
{
int num;
int remainder;
printf ("Enter a number: ");
scanf ("%d", &num);
printf ("The reversal is: ");
while (num != 0) {
remainder = num % 10;
num = num / 10;
printf ("%d", remainder);
}
return 0;
}
6.6
#include
int main (void)
{
int num;
printf ("Enter a number: ");
scanf ("%d", &num);
for (int i = 2; i * i <= num; i = i + 2) {
printf ("%d\n", i * i);
}
return 0;
}
6.7
#include
int main (void)
{
int n, odd, square;
printf ("This program prints a table of squares.\n");
printf ("Enter number of entries in table: ");
scanf ("%d", &n);
odd = 3;
for (int i = 1, square = 1; i <= n; odd += 2, ++i) {
printf ("%10d%10d\n", i, square);
square += odd;
}
return 0;
}
6.8
#include
int main (void)
{
int day, week;
printf ("Enter number of days in month: ");
scanf ("%d", &day);
printf ("Enter starting day of the week (1=Sun, 7=Sat): ");
scanf ("%d", &week);
for (int i = week; i > 1; i--) printf (" ");
for (int i = 1; i <= day; i++, week++) {
printf ("%3d", i);
if (week == 7) {
week = 0;
printf ("\n");
}
}
return 0;
}
6.9
#include
int main (void)
{
float money, rate, payment;
printf ("Enter amount of loan:");
scanf ("%f", &money);
printf ("Enter interest rate:");
scanf ("%f", &rate);
printf ("Enter monthly payment:");
scanf ("%f", &payment);
int num;
printf ("Enter the number of repayments:(1~10) ");
scanf ("%d", &num);
for (int i = 1; i <= num; i++){
printf ("Balance remaining after ");
switch (i) {
case 1: printf ("1st"); break;
case 2: printf ("2nd"); break;
case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10:
printf ("%dth", i);
break;
}
printf (" payment:%.2f\n", money = ((money - payment) + (money * rate * 0.01/ 12)));
}
return 0;
}
6.10
#include
int main (void)
{
int m[10], d[10], y[10];
int mm = 99, md = 99, my = 99; //初始化最小的月、日、年
int num = 0;
for (int i = 0; ; i++) {
printf ("Enter a date (mm/dd/yy): ");
scanf ("%d/%d/%d", &m[i], &d[i], &y[i]);
if (m[i] == 0 && d[i] == 0 && y[i] == 0) break;
num++;
}
for (int i = 0; i < num; i++) { //选出最小的年份
if (my >= y[i]) {
my = y[i];
}
}
for (int i = 0; i < num; i++) { //再次扫描所有输入年份,在年份最小的日期中找到最小的月份
if (y[i] == my) {
if (mm >= m[i]) {
mm = m[i];
}
}
}
for (int i = 0; i < num; i++) { //同理扫描所有输入月份,在月份最小的日期中找到最小的日
if (m[i] == mm) {
if (md >= d[i]) {
md = d[i];
}
}
}
printf ("%d/%d/%.2d is the earliest date", mm, md, my);
return 0;
}
机智如我
6.11
#include
int main (void)
{
int n;
float sum = 1.0f;
int temp;
printf ("Enter the number: ");
scanf ("%d", &n);
for (int i = 1; i <= n; i++) {
temp = 1;
for (int j = i ; j >= 1; j--) {
temp *= j;
}
sum += 1.0f / temp;
}
printf ("The value of e is: %f", sum);
return 0;
}
6.12
#include
int main (void)
{
float m;
float sum = 1.0f;
int temp;
printf ("Enter the number: ");
scanf ("%f", &m);
for (int i = 1; ; i++) {
temp = 1;
for (int j = i ; j >= 1; j--) {
temp *= j;
}
if (1.0f / temp <= m) break;
sum += 1.0f / temp;
}
printf ("The value of e is: %f", sum);
return 0;
}