我是靠谱客的博主 负责缘分,最近开发中收集的这篇文章主要介绍《C Primer Plus》(第6版)编程练习——第6章,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

第1题

#include <stdio.h>
#define SIZE 26
int main(void)
{
char lcase[SIZE];
int i;
for (i = 0; i < SIZE; i++)
lcase[i] = 'a' + i;
for (i = 0; i < SIZE; i++)
printf("%c", lcase[i]);
printf("n");
return 0;
}

第2题

#include <stdio.h>
int main(void)
{
int i, j;
for (i = 1; i <= 5; i++)
{
for (j = 1; j <= i; j++)
printf("$");
printf("n");
}
return 0;
}

第3题

#include <stdio.h>
int main(void)
{
char let = 'F';
char start;
char end;
for (end = let; end >= 'A'; end--)
{
for (start = let; start >= end; start--)
printf("%c", start);
printf("n");
}
return 0;
}

第4题

#include <stdio.h>
int main(void)
{
const int n_let = 26;
char let = 'A';
int i, j;
int count = 0;
for (i = 1; n_let - count >= i; i++)
{
for (j = 0; j < i; j++, count++)
printf("%c", let + count);
printf("n");
}
return 0;
}

第5章

#include <stdio.h>
int main(void)
{
char let;
int row;
int i, j;
printf("Enter a uppercase letter: ");
scanf("%c", &let);
row = let - 'A' + 1;
// 需打印的行数
for (i = 1; i <= row; i++)
{
for (j = 1; j <= row - i; j++) // 打印空格
printf(" ");
for (j = 1; j <= i; j++)
// 升序打印字母
printf("%c", 'A' + j - 1);
for (j = i - 1; j >= 1; j--)
// 降序打印字母
printf("%c", 'A' + j - 1);
printf("n");
}
return 0;
}

第6题

#include <stdio.h>
int main(void)
{
int lower, upper, index;
int square, cube;
printf("Enter starting integer: ");
scanf("%d", &lower);
printf("Enter ending integer: ");
scanf("%d", &upper);
printf("%5s %10s %15sn", "num", "square", "cube");
for (index = lower; index <= upper; index++)
{
square = index * index;
cube = index * square;
printf("%5d %10d %15dn", index, square, cube);
}
return 0;
}

第7题

#include <stdio.h>
#include <string.h>
#define SIZE 20
int main(void)
{
char word[SIZE];
int index;
printf("Enter a word: ");
scanf("%s", word);
for (index = strlen(word) - 1; index >= 0; index--)
printf("%c", word[index]);
printf("n");
return 0;
}

第8题

#include <stdio.h>
int main(void)
{
double m, n;
double res;
printf("Enter a pair of numbers: ");
while (scanf("%lf %lf", &m, &n) == 2)
{
res = (m - n) / (m * n);
printf("(%.3g - %.3g) / (%.3g * %.3g) = %.5gn", m, n, m, n, res);
printf("Enter next pair (non-numeric to quit): ");
}
printf("Bye.n");
return 0;
}

第9题

#include <stdio.h>
double result(double, double);
int main(void)
{
double m, n;
printf("Enter a pair of numbers: ");
while (scanf("%lf %lf", &m, &n) == 2)
{
printf("(%.3g - %.3g) / (%.3g * %.3g) = %.5gn", m, n, m, n, result(m, n));
printf("Enter next pair (non-numeric to quit): ");
}
printf("Bye.n");
return 0;
}
double result(double a, double b)
{
return (a - b) / (a * b);
}

第10题

#include <stdio.h>
int main(void)
{
int lower, upper, sum;
int i;
printf("Enter lower and upper integer limits: ");
while (scanf("%d %d", &lower, &upper) == 2 && lower < upper)
{
for (sum = 0, i = lower; i <= upper; i++)
sum += i * i;
printf("The sums of the squares from %d to %d is %dn", lower * lower, upper * upper, sum);
printf("Enter next set of limits: ");
}
printf("Donen");
return 0;
}

第11题

#include <stdio.h>
#define SIZE 8
int main(void)
{
int vals[SIZE];
int i;
printf("Please enter %d integers.n", SIZE);
for (i = 0; i < SIZE; i++)
scanf("%d", &vals[i]);
printf("Here, in reverse order, are the values you entered:n");
for (i = SIZE - 1; i >= 0; i--)
printf("%d ", vals[i]);
printf("n");
return 0;
}

第12题

#include <stdio.h>
int main(void)
{
double item, sum1, sum2;
int sign;
int num, i;
printf("Enter the number of calculation (<=0 to quit): ");
while (scanf("%d", &num) == 1 && num > 0)
{
sum1 = sum2 = 0;
sign = -1;
for (i = 1; i <= num; i++)
{
sign *= -1;
item = 1.0 / i;
sum1 += item;
sum2 += sign * item;
}
printf("Result after running %d items:n", num);
printf("1.0 + 1.0/2.0 + 1.0/3.0 + 1.0/4.0 + ... = %en", sum1);
printf("1.0 - 1.0/2.0 + 1.0/3.0 - 1.0/4.0 + ... = %en", sum2);
printf("Enter next number (<=0 to quit): ");
}
printf("Byen");
return 0;
}

第13题

#include <stdio.h>
#define SIZE 8
int main(void)
{
int twopows[SIZE];
int i;
int value = 1; /* 2 to the 0 */
for (i = 0; i < SIZE; i++)
{
twopows[i] = value;
value *= 2;
}
i = 0;
do
{
printf("%d ", twopows[i]);
i++;
} while (i < SIZE);
printf("n");
return 0;
}

第14题

#include <stdio.h>
#define SIZE 8
int main(void)
{
double arr[SIZE];
double arr_cumul[SIZE];
int i;
printf("Enter %d numbers:n", SIZE);
for (i = 0; i < SIZE; i++)
{
printf("value #%d: ", i + 1);
scanf("%lf", &arr[i]);
}
arr_cumul[0] = arr[0];
for (i = 1; i < SIZE; i++)
arr_cumul[i] = arr_cumul[i - 1] + arr[i];
for (i = 0; i < SIZE; i++)
printf("%8g ", arr[i]);
printf("n");
for (i = 0; i < SIZE; i++)
printf("%8g ", arr_cumul[i]);
printf("n");
return 0;
}

第15题

#include <stdio.h>
#define SIZE 255
int main(void)
{
char c[SIZE];
int i = 0;
int count = 0;
printf("Enter something (less than 255 characters):n");
scanf("%c", &c[i]);
while (i < SIZE && c[i] != 'n')
{
count++;
i++;
scanf("%c", &c[i]);
}
printf("nHere, in reverse order, are the characters you entered:n");
for (i = count - 1; i >= 0; i--)
printf("%c", c[i]);
printf("n");
return 0;
}

第16题

#include <stdio.h>
#define RATE_SIMP 0.10
#define RATE_COMP 0.05
#define INIT_AMT 100.0
int main(void)
{
double daphne = INIT_AMT;
double deirdre = INIT_AMT;
int years = 0;
while (deirdre <= daphne)
{
daphne += INIT_AMT * RATE_SIMP;
deirdre += deirdre * RATE_COMP;
years++;
}
printf("Investment values after %d years:n", years);
printf("Daphne: $%.2fn", daphne);
printf("Deirdre: $%.2fn", deirdre);
return 0;
}

第17题

#include <stdio.h>
#define RATE 0.08
#define INIT_AMT 100.0
int main(void)
{
double amt, res;
int year = 0;
amt = res = INIT_AMT;
do
{
amt = res * (1 + RATE);
res = amt - 10;
year++;
} while (res > 0);
printf("After %d years, Chuckie's account is empty.n", year);
return 0;
}

第18题

#include <stdio.h>
#define INIT_FRI 5
#define DUNBAR 150
int main(void)
{
int fri;
int weeks = 0;
fri = INIT_FRI;
while (fri < DUNBAR)
{
weeks++;
fri -= weeks;
fri *= 2;
printf("After %d weeks,Doctor Rabnud has %d friends.n", weeks, fri);
}
return 0;
}

最后

以上就是负责缘分为你收集整理的《C Primer Plus》(第6版)编程练习——第6章的全部内容,希望文章能够帮你解决《C Primer Plus》(第6版)编程练习——第6章所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(69)

评论列表共有 0 条评论

立即
投稿
返回
顶部