【C prime plus】 程序清单—第八章

本文档详细介绍了C++编程语言第八章的各个程序清单,包括8.1至8.8共八个示例,涵盖了重要的编程概念和技术。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

程序清单

8.1

/* echo.c -- 重复输入 */ 
// 该程序获取从键盘输入的字符,并把这些字符发送到屏幕上。
//输入: Hello, there. I would 
#include <stdio.h>
int main()
{
    char ch;
    
    while (( ch = getchar() ) != '#')
        putchar (ch);    
    return 0;
 } 

8.2

/* echo_epf.c -- 重复输入,直到文件结尾 */
#include <stdio.h>
int main()
{
    int ch;
    
    while((ch = getchar()) != EOF)
        putchar(ch);
            
    return 0;
 } 

8.3

/* file_eof.c -- 打开一个文件并显示该文件 */
#include <stdio.h>
#include <stdlib.h>        //为了使用exit()
int main()
{
    int ch;
    FILE * fp;
    char fname[50];        //储存文件名
    
    printf("Enter the name of the file: ");
    scanf("%s", fname);
    fp = fopen (fname, "r");    // 打开带读取文件
    if ( fp == NULL )            // 如果打开失败 
    {
        printf("Failed to open the file: ");
        exit (1);                // 退出程序 
     } 
    // get(fp) 从打开的文件获取一个字符 
    while ((ch = getc(fp)) != EOF)
        putchar(ch);
    fclose(fp);                    // 关闭程序
     
    return 0;    
} 

8.4

/* guess.c -- 一个拖沓且错误的猜数字程序 */
#include <stdio.h>
int main()
{
    int guess = 1;
    char response; 
    
    printf("Pick an integer from 1 to 100. I will try to your guess it.\n");
    printf("Respond with a 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值