C Prime Plus书中代码整理

发布于:2022-10-28 ⋅ 阅读:(271) ⋅ 点赞:(0)

第二章:

程序清单

2.1

/* first.c */
#include <stdio.h>
int main(void)      // a simple program                          
{
    int num;        // define a variable called num              

    num = 1;        // aasign a value to num                     
    printf("I am a simple ");       // use the printf() function
    printf("computer.\n");
    printf("My favorite number is %d because it is first.\n", num);

    return 0;
}

2.2

// fathom_ft.c -- convert 2 fathoms into feet                   
#include <stdio.h>
int main(void)
{
    int fathoms, feet;

    fathoms = 2;
    feet = fathoms * 6;

    printf("There are %d feet in %d fathoms!\n", feet, fathoms);
    printf("Yes, I said %d feet!\n", 6 * fathoms);

    return 0;
}

2.3

// two_func.c -- a program using two functions in a file         
#include <stdio.h>
void bulter(void);   /* ANSI/ISO C function prototyping */

int main (void)
{
    printf("I will summon the bulter function.\n");
    bulter();
    printf("Yes. Bring me some tea and writeable"
            " DVDs.\n");

    return 0;
}

void bulter(void)   // start a function of definition            
{
    printf("You rang, sir?\n");
}

2.4

/* nogood.c -- a program with errors */
#include <stdio.h>
int main(void)
(
    int n, int n2, int n3;

    /* this program have several errors */                         
    n = 5;                                                       
    n2 = n * n;                                                  
    n3 = n2 * n2;                                                
    printf("n = %d, n squared = %d, n cubed = %d\n", n, n2, n3)  
                                                                 
    return 0;                                                    
)                  

2.5

/* stillbad.c --  a program with its syntax errors fixed */
#include <stdio.h>
int main(void)
{
    int n, n2, n3;

    /* this program with a semantic error */
    n = 5;
    n2 = n * n;
    n3 = n2 * n2;
    printf ("n = %d, n squared = %d, n cubed = %d\n",
            n, n2, n3);

    return 0;
}

2.6

/* good.c -- a program with its syntax and semantic errors fixed */
#include <stdio.h>
int main(void)
{
    int n, n2, n3;

    /* this program with a semantic error */
    n = 5;
    n2 = n * n;
    n3 = n2 * n;
    printf ("n = %d, n squared = %d, n cubed = %d\n",
            n, n2, n3);

    return 0;
}

复习题答案:

1. C语言的基本模块是什么?
answer: function

2. 什么是语法错误?写出一个英语例子和C语言例子.
answer:
A syntax error is a violation of the rules governing how sentences or programs are put together.
Here’s an example in English: “Me speak English good.”
Here’s an example in C: printf"Where are the parentheses?"

3. 什么是语义错误, 写出一个英语例子和C语言例子。
answer:
 A semantic error is one of meaning.
 Here’s an example in English: “This sentence is excellent Czech.”
 Here’s a C example: thrice_n = 3 + n;

4.

// after correct aq2_4.c                                                       
#include <stdio.h>
int main(void)      // this program prints the number of weeks in a year
{
    int s;

    s = 56;
    printf ("There are %d weeks in a year.\n", s);

    return 0;
}
~                               

5.

#include <stdio.h>
int main(void)
{
    // a
    printf("Baa Baa Black Sheep.");
    printf("Have you any wool?\n");
    // b
    printf("Begone!\nO creature of lard!\n");
    // c
    printf("What?\nNo/nfish?\n");
    // d
    int num;
    num = 2;
    printf("%d + %d = %d", num, num, num + num);

    return 0;
}

6. Which of the following are C keywords? main, int, function, char, =
answer: int and char
(main is a function name, function is a technical term for describing C,
and = is an operator).

7

#include <stdio.h>
int main(void)
{
    int words = 3020, lines = 350;

    printf("There were %d words and %d lines.\n", words, lines);

    return 0;
}

8

include <stdio.h>                                               
int main(void)
{
    int a, b;

    a = 5;
    b = 2;  /* line 7 */
    printf("a = %d, b = %d\n", a, b); 
    b = a;  /* line 8 */
    printf("a = %d, b = %d\n", a, b); 
    a = b;  /* line 9 */
    printf("a = %d, b = %d\n", a, b); 
    printf("%d %d\n", b, a); 

    return 0;
}

9.

#include <stdio.h>                                               
int main(void)
{
    int x, y;

    x = 10; 
    y = 5;       /* line 7 */
    printf("x=%-3d   y=%d\n", x, y); 
    y = x + y;   /* line 8 */
    printf("x=%-3d   y=%d\n", x, y); 
    x = x*y;     /* line 9 */
    printf("x=%-3d   y=%d\n", x, y); 
    printf("%d %d\n", x, y); 

    return 0;
}

编程练习:

1.

#include <stdio.h>
int main(void)
{
    printf("Luyu Pan\n");
    printf("Luyu \nPan\n");
    printf("Luyu ");
    printf("Pan\n");

    return 0;
}

2.

* 2. Write a program to print your name and address. */         
#include <stdio.h>
#define NAME    "Luyu Pan"
#define ADDRESS "FuShun"
int main(void)
{
    printf("My name is %s, My address is %s.\n", NAME, ADDRESS);

    return 0;
}

3.

#include <stdio.h>
#define YEAR    365
int main(void)
{
    int age = 20; 
    int days = age * YEAR;

    printf("My age is %d, equals %d days.\n", age, days);

    return 0;
}

4.

#include <stdio.h>
void jolly(void);       // ASNI prototype
void deny(void);
int main(void)                                                 
{
    for(int i = 0; i < 3; i++)
        jolly();    // call function jolly()
    deny();

    return 0;
}

void jolly(void)    // function definition
{
    puts("For he's a jolly good fellow!");
}

void deny(void)
{
    puts("Which nobody can deny!");
}

5.

#include <stdio.h>
void br(void);
void ic(void);

int main(void)
{
    br();
    putchar(',');
    putchar(' ');
    ic();
    putchar('\n');
    ic();
    putchar(',');
    putchar('\n');
    br();
    putchar('\n');

    return 0;
}

void br(void)
{
    printf("Brazil, Russia");
}

void ic(void)
{
    printf("India, China");
}                          

6.

#include <stdio.h>
int main(void)
{
    int toes = 10;

    printf("toes = %d, twice toes = %d, toes squared = %d\n",
            toes, toes * 2, toes * toes);

    return 0;
}

7.

#include <stdio.h>
#define TIMES   3
void smile(void);

int main(void)
{
    for (int i = 0; i < TIMES; i++)
    {
        for (int j = TIMES; j > i; j--)
            smile();
        putchar('\n');
    }

    return 0;
}

void smile(void)
{
    printf("Smile!");
}                          

8.

#include <stdio.h>                                               
void one_three(void);
void two(void);

int main(void)
{
    puts("Starting now:");
    one_three();
    puts("done!");

    return 0;
}

void one_three(void)
{
    puts("one");
    two();      // one function call another function
    puts("three");
}

void two(void)
{
    puts("two");
}


网站公告

今日签到

点亮在社区的每一天
去签到