C与指针第十五章编程练习

 

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char ch;
    while ((ch = getchar()) != EOF) // Ctrl + z 结束
    {
        putchar(ch);
    }
    
    system("pause");
    return EXIT_SUCCESS;
}

 

#include <stdio.h>
#include <stdlib.h>

#define MAX_LINE_LENGTH 81
/*这个程序会使用fgets函数从标准输入读取一行(最多读取BUFFER_SIZE - 1个字符,然后在末尾添加一个空字符作为字符串的结束),然后fputs函数将这些字符写入到标准输出。这个循环会一直持续到读取到文件结束符(EOF)为止。
这个程序可以处理任意长度的行。对于那些长于BUFFER_SIZE - 1个字符的行,fgets函数会分多次读取。每次读取到的字符都会立即被输出,所以长行的处理并不会需要额外的内存
2、3题用同样的代码
*/
int main()
{
    char chLine[MAX_LINE_LENGTH];
    while (fgets(chLine, MAX_LINE_LENGTH, stdin)) 
    {
        fputs(chLine, stdout);
    }
    
    system("pause");
    return EXIT_SUCCESS;
}

#include <stdio.h>
#include <stdlib.h>

#define BUFFER_SIZE 81

int main()
{
    char buffer[BUFFER_SIZE];
    char inputFileName[BUFFER_SIZE], outputFileName[BUFFER_SIZE];

    // 输入的文件带上路径,如: D:\123.txt
    printf("Please enter the name of the input file: ");
    fgets(inputFileName, BUFFER_SIZE, stdin);
    // strcspn函数会返回从 inputFileName 开始到第一个换行符的长度。然后我们就可以将这个换行符替换为字符串结束符(’\0’),以此来移除文件名中的换行符。
    inputFileName[strcspn(inputFileName, "\n")] = 0;

    printf("Please enter the name of the output file: ");
    fgets(outputFileName, BUFFER_SIZE, stdin);

    outputFileName[strcspn(outputFileName, "\n")] = 0;
    /*这个函数需要两个参数:第一个参数是要打开的文件的名称,第二个参数是打开文件的模式。模式可以是 “r”(只读),“w”(只写),“a”(追加),“r+”(读写),“w+”(读写,如果文件不存在则创建,如果存在则清空),“a+”(读写,如果文件不存在则创建,如果存在则在文件尾部追加) 等。如果文件成功打开,fopen 会返回一个 FILE 指针,这个指针可以用来后续的读写操作。如果文件打开失败,fopen 会返回 NULL。*/
    FILE *inputFile = fopen(inputFileName, "r");
    if (inputFile == NULL)
    {
        printf("Failed to open the input file\n");
        return EXIT_FAILURE;
    }

    FILE *outputFile = fopen(outputFileName, "w");
    if (outputFile == NULL)
    {
        printf("Failed to open the output file\n");
        return EXIT_FAILURE;
    }

    while (fgets(buffer, BUFFER_SIZE, inputFile))
    {
        fputs(buffer, outputFile);
    }

    fclose(inputFile);
    fclose(outputFile);
    system("pause");
    return EXIT_SUCCESS;
}

 

 

#include <stdio.h>
#include <stdlib.h>

#define BUFFER_SIZE 81

int main()
{
    char buffer[BUFFER_SIZE];
    char inputFileName[BUFFER_SIZE], outputFileName[BUFFER_SIZE];

    // 输入的文件带上路径,如: D:\123.txt
    printf("Please enter the name of the input file: ");
    fgets(inputFileName, BUFFER_SIZE, stdin);
    // strcspn函数会返回从 inputFileName 开始到第一个换行符的长度。然后我们就可以将这个换行符替换为字符串结束符(’\0’),以此来移除文件名中的换行符。
    inputFileName[strcspn(inputFileName, "\n")] = 0;

    printf("Please enter the name of the output file: ");
    fgets(outputFileName, BUFFER_SIZE, stdin);

    outputFileName[strcspn(outputFileName, "\n")] = 0;
    /*这个函数需要两个参数:第一个参数是要打开的文件的名称,第二个参数是打开文件的模式。模式可以是 “r”(只读),“w”(只写),“a”(追加),“r+”(读写),“w+”(读写,如果文件不存在则创建,如果存在则清空),“a+”(读写,如果文件不存在则创建,如果存在则在文件尾部追加) 等。如果文件成功打开,fopen 会返回一个 FILE 指针,这个指针可以用来后续的读写操作。如果文件打开失败,fopen 会返回 NULL。*/
    FILE *inputFile = fopen(inputFileName, "r");
    if (inputFile == NULL)
    {
        printf("Failed to open the input file\n");
        return 1;
    }

    FILE *outputFile = fopen(outputFileName, "w");
    if (outputFile == NULL)
    {
        printf("Failed to open the output file\n");
        return 1;
    }

    // 只负责整数开始的行的整数,即第一行是123,那么就取出1
    int iCnt = 0;
    while (fgets(buffer, BUFFER_SIZE, inputFile))
    {
        int iVal = buffer[0] - '0';
        if (iVal >= 0 && iVal <= 9)
            iCnt = iCnt + iVal;
        fputs(buffer, outputFile);
    }
    fprintf(outputFile, "\n%d\n", iCnt);

    fclose(inputFile);
    fclose(outputFile);
    system("pause");
    return EXIT_SUCCESS;
}

 

#include <stdio.h>
#include <stdlib.h>
// 将数字翻转,只要翻转后的数字等于原始数据,就能判断数字是回文
int numeric_palindrome( int value )
{
    if (value < 0) 
        return 0; // 负数不是回文
    int reversed = 0; // 储存翻转后的数字
    int original = value; // 储存原始数字
    while (value != 0) 
    {
        reversed = reversed * 10 + value % 10;
        value /= 10;
    }
    return original == reversed;
}

int main()
{
    system("pause");
    return EXIT_SUCCESS;
}

 

#include <stdio.h>
#include <stdlib.h>

#define BUFFER_SIZE 81


int main()
{
    // 文件地址,通过外部输入
    char buffer[BUFFER_SIZE];
    char inputFileName[BUFFER_SIZE];
    // 输入的文件带上路径,如: D:\123.txt
    printf("Please enter the name of the input file: ");
    fgets(inputFileName, BUFFER_SIZE, stdin);
    inputFileName[strcspn(inputFileName, "\n")] = 0;
    FILE *inputFile = fopen(inputFileName, "r");
    if (inputFile == NULL) 
    {
        printf("Failed to open the input file\n");
        return EXIT_FAILURE;
    }

    while (fgets(buffer, BUFFER_SIZE, inputFile)) 
    {
        int iCnt = 0; // 求和的
        int iNum = 0; // 记录数字的个数
        int iVal = 0; // 记录单个数字的值
        printf("\nValue is");
        for (int i = 0; i < BUFFER_SIZE; ++i)
        {
            if (buffer[i] >= '0' && buffer[i] <= '9')
            {
                iVal = iVal * 10 + (buffer[i] - '0');
            }
            else
            {
                iCnt = iCnt + iVal;
                ++iNum;
                printf(" %d", iVal);
                iVal = 0;
                if (buffer[i] == '\n' || buffer[i] == EOF || buffer[i] == '\000')
                {
                    double iAverage = (double)iCnt / (double)iNum;
                    printf("\nAverage is %5.2f\n", iAverage);
                    iCnt = 0;
                    iNum = 0;
                    break;
                }
            }
        }
        memset(buffer, 0, sizeof(buffer)); // 数组清零
    }
    // fprintf(outputFile, "\n%d\n", iCnt);
    fclose(inputFile);
    system("pause");
    return EXIT_SUCCESS;
}

 

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#define BYTES_PER_LINE 16

void dump(FILE *fp)
{
    unsigned char buffer[BYTES_PER_LINE];
    size_t n;
    long offset = 0;

    while ((n = fread(buffer, 1, BYTES_PER_LINE, fp)) > 0)
    {
        printf("%06lx ", offset);
        for (size_t i = 0; i < n; i++)
        {
            printf("%02x ", buffer[i]);
            if (i == 7)
                putchar(' ');
        }
        if (n < BYTES_PER_LINE)
        {
            for (size_t i = n; i < BYTES_PER_LINE; i++)
            {
                printf("   ");
                if (i == 7)
                    putchar(' ');
            }
        }
        printf(" *");
        for (size_t i = 0; i < n; i++)
        {
            if (isprint(buffer[i]) && !isspace(buffer[i]))
            {
                putchar(buffer[i]);
            }
            else
            {
                putchar('.');
            }
        }
        printf("*\n");
        offset += n;
    }
}

int main(int argc, char *argv[])
{
    FILE *fp;
    if (argc < 2)
    {
        dump(stdin);
    }
    else
    {
        if ((fp = fopen(argv[1], "rb")) == NULL)
        {
            fprintf(stderr, "Can't open %s\n", argv[1]);
            exit(EXIT_FAILURE);
        }
        dump(fp);
        fclose(fp);
    }
    system("pause");
    return EXIT_SUCCESS;
}

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_LINE_LENGTH 510
/*
fgrep函数是执行查找操作的函数。
它逐行读取文件内容,使用strstr函数检查每一行是否包含给定的字符串,如果包含则打印该行。
*/
void fgrep(FILE *fp, const char *filename, const char *str)
{
    char line[MAX_LINE_LENGTH + 1];
    while (fgets(line, sizeof(line), fp) != NULL)
    {
        if (strstr(line, str) != NULL)
        {
            if (filename != NULL)
            {
                printf("%s: ", filename);
            }
            printf("%s", line);
        }
    }
}

int main(int argc, char *argv[])
{
    FILE *fp;
    if (argc < 2)
    {
        fprintf(stderr, "Usage: %s <string> [<filename>...]\n", argv[0]);
        exit(EXIT_FAILURE);
    }
    if (argc == 2)
    {
        fgrep(stdin, NULL, argv[1]);
    }
    else
    {
        for (int i = 2; i < argc; i++)
        {
            if ((fp = fopen(argv[i], "r")) == NULL)
            {
                fprintf(stderr, "Can't open %s\n", argv[i]);
                continue;
            }
            fgrep(fp, argv[i], argv[1]);
            fclose(fp);
        }
    }
    system("pause");
    return EXIT_SUCCESS;
}

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>

#define MAX_FILENAME 255

void print_checksum(const char *filename, int to_file)
{
    FILE *fp = stdin;
    FILE *out = stdout;
    char out_filename[MAX_FILENAME + 5]; // extra space for ".cks\0"

    if (filename != NULL)
    {
        fp = fopen(filename, "r");
        if (fp == NULL)
        {
            perror(filename);
            return;
        }

        if (to_file)
        {
            snprintf(out_filename, sizeof(out_filename), "%s.cks", filename);
            out = fopen(out_filename, "w");
            if (out == NULL)
            {
                perror(out_filename);
                return;
            }
        }
    }

    uint16_t checksum = 0;
    int c;

    while ((c = fgetc(fp)) != EOF)
    {
        checksum += (uint8_t)c;
    }

    fprintf(out, "%u\n", checksum);

    if (fp != stdin)
    {
        fclose(fp);
    }

    if (out != stdout)
    {
        fclose(out);
    }
}

int main(int argc, char *argv[])
{
    int to_file = 0;
    int start = 1;

    if (argc > 1 && strcmp(argv[1], "-f") == 0)
    {
        to_file = 1;
        start = 2;
    }

    if (start == argc)
    {
        if (to_file)
        {
            fprintf(stderr, "Can't write to file when reading from stdin\n");
            exit(EXIT_FAILURE);
        }
        print_checksum(NULL, to_file);
    }
    else
    {
        for (int i = start; i < argc; i++)
        {
            print_checksum(argv[i], to_file);
        }
    }

    return EXIT_SUCCESS;
}

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct
{
    int part_number;
    char description[21];
    int quantity;
    double cost_each;
} Part;
// 没有异常判断
int main(int argc, char *argv[])
{
    FILE *inventory;
    Part parts[100];
    int next_part_number = 1;

    if (argc != 2)
    {
        printf("Usage: %s inventory_file\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    // Open or create the inventory file
    if ((inventory = fopen(argv[1], "r+b")) == NULL)
    {
        if ((inventory = fopen(argv[1], "w+b")) == NULL)
        {
            printf("Cannot open or create file %s\n", argv[1]);
            exit(EXIT_FAILURE);
        }
    }

    // Read parts from the inventory file
    while (fread(&parts[next_part_number], sizeof(Part), 1, inventory) == 1)
        next_part_number++;

    char command[6];
    while (scanf("%s", command) != EOF)
    {
        if (strcmp(command, "new") == 0)
        {
            scanf("%s%d%lf", parts[next_part_number].description, &parts[next_part_number].quantity, &parts[next_part_number].cost_each);
            parts[next_part_number].part_number = next_part_number;
            next_part_number++;
        }
        else if (strcmp(command, "buy") == 0)
        {
            int part_number, quantity;
            double cost_each;
            scanf("%d%d%lf", &part_number, &quantity, &cost_each);
            parts[part_number].quantity += quantity;
            parts[part_number].cost_each = (parts[part_number].cost_each * parts[part_number].quantity + cost_each * quantity) / (parts[part_number].quantity);
        }
        else if (strcmp(command, "sell") == 0)
        {
            int part_number, quantity;
            double price_each;
            scanf("%d%d%lf", &part_number, &quantity, &price_each);
            parts[part_number].quantity -= quantity;
            printf("Profit: $%.2lf\n", (price_each - parts[part_number].cost_each) * quantity);
        }
        else if (strcmp(command, "delete") == 0)
        {
            int part_number;
            scanf("%d", &part_number);
            parts[part_number].description[0] = '\0';
        }
        else if (strcmp(command, "print") == 0)
        {
            int part_number;
            scanf("%d", &part_number);
            printf("Part number: %d\n", parts[part_number].part_number);
            printf("Description: %s\n", parts[part_number].description);
            printf("Quantity: %d\n", parts[part_number].quantity);
            printf("Cost each: $%.2lf\n", parts[part_number].cost_each);
        }
        else if (strcmp(command, "printall") == 0)
        {
            for (int i = 1; i < next_part_number; i++)
            {
                if (parts[i].description[0] != '\0')
                {
                    printf("Part number: %d\n", parts[i].part_number);
                    printf("Description: %s\n", parts[i].description);
                    printf("Quantity: %d\n", parts[i].quantity);
                    printf("Cost each: $%.2lf\n", parts[i].cost_each);
                }
            }
        }
        else if (strcmp(command, "total") == 0)
        {
            double total_value = 0.0;
            for (int i = 1; i < next_part_number; i++)
                total_value += parts[i].quantity * parts[i].cost_each;
            printf("Total value of inventory: $%.2lf\n", total_value);
        }
        else if (strcmp(command, "end") == 0)
        {
            break;
        }
    }

    // Write parts to the inventory file
    rewind(inventory);
    for (int i = 1; i < next_part_number; i++)
        fwrite(&parts[i], sizeof(Part), 1, inventory);

    fclose(inventory);

    return 0;
}

 

  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值