c primer plus 第六版 第六章编程练习

(编译环境 Microsoft Visual Studio 2019)

 

1.

/*1.*/
#include<stdio.h>
int main(void)
{
    char alphabet[26];
    char letter;
    int i;

    for (i = 0, letter='a'; i < 26; i++ , letter++)
    {
        alphabet[i] = letter;
        printf("%c", alphabet[i]);
    }
    printf("\n");

    return 0;
}

 

 

2.

/*2.*/
#include<stdio.h>
int main(void)
{
    int row, column;

    for (row = 1; row <= 5; row++)
    {
        for (column = 0; column < row; column++)
        {
            printf("$");
        }
        printf("\n");
    }

    return 0;
}

 

 

3.

/*3.*/
#include<stdio.h>
int main(void)
{
    int row, column;
    char letter;

    for (row = 1; row <= 6; row++)
    {
        for (column = 0, letter = 'F'; column < row; column++, letter--)
        {
            printf("%c", letter);
        }
        printf("\n");
    }

    return 0;
}

 

 

4.

/*4.*/
#include<stdio.h>
int main(void)
{
    int row, column;
    char letter;

    for (row = 1, letter = 'A'; row <= 6; row++)
    {
        for (column = 0; column < row; column++, letter++)
        {
            printf("%c", letter);
        }
        printf("\n");
    }

    return 0;
}

 

 

5.

/*5.*/
#include<stdio.h>
int main(void)
{
    int row, column;
    int blank;
    char letter;
    char object;

    printf("Please enter a capital letter: ");
    scanf_s("%c", &object, 1);
    for (row = 0; row < (object - 'A' + 1); row++)
    {
        for (blank = 0; blank < (object - 'A' - row); blank++)
            printf(" ");
        for (column = 0, letter = 'A'; column < (row + 1); column++, letter++)
            printf("%c", letter);
        for (letter -= 2; letter >= 'A'; letter--)
            printf("%c", letter);
        printf("\n");
    }

    return 0;
}

 

 

6.

/*6.*/
#include<stdio.h>
int main(void)
{
    int row;
    int upper, lower;
    int square, cube;

    printf("Please enter the lower limit of integer: ");
    scanf_s("%d", &lower);
    printf("Please enter the upper limit of integer: ");
    scanf_s("%d", &upper);

    printf("\ninteger      square        cube\n");
    for (row = lower; row <= upper; row++)
    {
        square = row * row;
        cube = row * row * row;
        printf("%7d  %10d  %10d\n", row, square, cube);
    }

    return 0;
}

 

 

7.

/*7.*/
#include<stdio.h>
#include<string.h>
int main(void)
{
    char word[30];
    int length;
    int i;

    printf("Please enter a word.\n");
    scanf_s("%s", word, 30);
    length = strlen(word);
    for (i = length - 1; i >= 0; i--)
        printf("%c", word[i]);
    printf("\n");

    return 0;
}

 

 

8.

/*8.*/
#include<stdio.h>
#include<math.h>
int main(void)
{
    float value_1, value_2;

    printf("Please enter two decimals.(q to quit)\n");
    while (2 == scanf_s("%f %f", &value_1, &value_2))
    {
        printf("result = %.3f\n", 
            fabs(value_1 - value_2) / (value_1 * value_2));
        printf("\nPlease enter two decimals.(q to quit)\n");
    }
    printf("bye\n");

    return 0;
}

 

 

9.

/*9.*/
#include<stdio.h>
#include<math.h>
float result(float v1, float v2);

int main(void)
{
    float value_1, value_2;

    printf("Please enter two decimals.(q to quit)\n");
    while (2 == scanf_s("%f %f", &value_1, &value_2))
    {
        printf("result = %.3f\n",result(value_1,value_2));
        printf("\nPlease enter two decimals.(q to quit)\n");
    }
    printf("bye\n");

    return 0;
}

float result(float v1, float v2)
{
    float result;
    result = (float)fabs(v1 - v2) / (v1 * v2);

    return result;
}

 

 

10.

/*10.*/
#include<stdio.h>
int main(void)
{
    int upper, lower;
    int i;
    int s_squares = 0;
    int enter;

    printf("Enter lower and upper integer limits(lower >= upper to quit): ");
    enter = scanf_s("%d %d", &lower, &upper);
    while ((upper > lower) && (enter == 2))
    {
        for (i = lower; i <= upper; i++)
            s_squares += i * i;
        printf("The sums of the squares from %d to %d is %d\n",
            lower * lower, upper * upper, s_squares);
        printf("Enter next set of limits: ");
        enter = scanf_s("%d %d", &lower, &upper);
    }
    printf("Done!\n");

    return 0;
}

 

 

11.

/*11.*/
#include<stdio.h>
int main(void)
{
    int eight_int[8];
    int i;

    printf("Please enter eight integers.\n");
    for (i = 0; i < 8; i++)
        scanf_s("%d", &eight_int[i]);
    printf("Inverted order.\n");
    for (i = 7; i >= 0; i--)
        printf("%d ", eight_int[i]);
    printf("\n");

    return 0;
}

 

 

12.

/*12.*/
#include<stdio.h>
#include<math.h>
int main(void)
{
    float number;
    float sum_1, sum_2;
    float deno;
    _Bool enter_is_right;
    printf("Please enter the total number of time(<=0 to quit): ");
    enter_is_right=scanf_s("%f", &number);
    while ((number > 0) && enter_is_right)
    {
        for (deno = 1.0, sum_1 = 0, sum_2 = 0; deno <= number; deno++)
        {
            sum_1 += 1.0f / deno;
            sum_2 += (1.0f / deno) * (float)pow(-1.0, deno + 1);
        }
        printf("sum_1= %f\nsum_2= %f\n", sum_1, sum_2);
        printf("\nPlease enter the total number of time(<=0 to quit): ");
        scanf_s("%f", &number);
    }
    printf("bye\n");

    return 0;
}

 

 

13.

/*13.*/
#include<stdio.h>
int pow_int(int a, int b);
int main(void)
{
    int two_power[8];
    int i;

    for (i = 0; i < 8; i++)
    {
        two_power[i] = pow_int(2, i+1);
    }
    i = 0;
    do
    {
        printf("%d ", two_power[i]);
    } while (++i < 8);
    printf("\n");

    return 0;
}

int pow_int(int a, int b)
{
    int i;
    int pow = 1;

    for (i = 0; i < b; i++)
        pow *= a;

    return pow;
}

 

 

14.

/*14.*/
#include<stdio.h>
int main(void)
{
    double array_1[8];
    double array_2[8];
    int i;

    printf("Please enter eight decimals.\n");
    for (i = 0; i < 8; i++)
        scanf_s("%lf", &array_1[i]);
    array_2[0] = array_1[0];
    for (i = 1; i < 8; i++)
    {
        array_2[i] = array_2[i - 1] + array_1[i];
    }
    printf("array:\n");
    for (i = 0; i < 8; i++)
        printf("%10.3f ", array_1[i]);
    printf("\n");
    for (i = 0; i < 8; i++)
        printf("%10.3f ", array_2[i]);
    printf("\n");

    return 0;
}

 

 

15.

/*15.*/
#include<stdio.h>
#include<string.h>
int main(void)
{
    char array[255];
    int i=0;

    printf("Please enter a line of characters.\n");
    scanf_s("%c", &array[i], 1);
    while (array[i] != '\n')//%c会读取每一个输入字符,包括空白(空格,回车,制表)
    {
        i++;
        scanf_s("%c", &array[i], 1);
    }
    for (i--; i >= 0; i--)//strlen()函数返回字段长度,需要'\0'作为结束标志,所以只能用于字符串,这里不能用
        printf("%c", array[i]);
    printf("\n");

    return 0;
}

 

 

16.

/*16.*/
#include<stdio.h>
#define DA_ORIGINAL 100
#define DE_ORIGINAL 100
#define DA_RATE 0.1f
#define DE_RATE 0.05f
int main(void)
{
    unsigned int years;
    float da_balance = DA_ORIGINAL;
    float de_balance = DE_ORIGINAL;

    for (years = 1; da_balance >= de_balance; years++)
    {
        da_balance += DA_ORIGINAL * DA_RATE;
        de_balance += de_balance * DE_RATE;
    }
    printf("years=%u, Deirdre's investment will exceed Daphne's\n", years);
    printf("Deirdre's investment is %.3f\n", de_balance);
    printf("Daphne's investment is %.3f\n", da_balance);

    return 0;
}

 

 

17.

/*17.*/
#include<stdio.h>
#define ORIGINAL 100
#define RATE 0.08f
int main(void)
{
    unsigned int years;
    float balance = ORIGINAL;

    for (years = 1; balance > 0; years++)
    {
        balance += balance * RATE;
        balance -= 10;
    }
    printf("years = %d\n", years);

    return 0;
}

 

 

18.

/*18.*/
#include<stdio.h>
#define ORIGINAL 5
#define RATE 2
#define DUNBER 150
int main(void)
{
    int weeks;
    int friends = ORIGINAL;

    for (weeks = 1; friends <= DUNBER; weeks++)
    {
        friends -= weeks;
        friends *= 2;
        printf("week= %d   friend= %d\n", weeks, friends);
    }

    return 0;
}

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 黑客帝国 设计师:上身试试 返回首页