C语言添加注释

#include <stdio.h>
#include<string.h>
#include<stdlib.h>
int main( )
{
    int m,k=1,n;
    char line1[256],line[256];//将文件中的数据读入到字符数组中
    FILE *fpin = fopen("sourcefile.c","r");
    if(fpin==NULL)
    {
        printf("source code read error!\n");
        exit(1);
    }
    FILE *fpout = fopen("targetfile.c","w+");
    if(fpout==NULL)
    {
        printf("source code write error!\n");
        exit(1);
    }
    printf("请输入m和n:");
    scanf("%d %d", &m, &n);
    int n1=0;
    fgets(line1,255,fpin);
    do
    {
        n1++;
        if(n1>=m&&n1<m+k)
            fputs("//",fpout);
        fputs(line1, fpout);
        fgets(line1,255,fpin);
    }
    while(!feof(fpin));
    fseek(fpout,0,SEEK_SET);
    fgets(line,255,fpout);
    do
    {
        printf("%d\t%s", k, line);
        k++;
        fgets(line,255,fpout);
    }
    while (!feof(fpout));
    fclose(fpout);
    fclose(fpin);
    return 0;
}

好的,下面是添加注释后的 C 语言程序: ```c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #define MAXSIZE 100 // 栈结构体 typedef struct { int data[MAXSIZE]; // 存储元素的数组 int top; // 栈顶元素的位置 } Stack; // 初始化栈 void init_stack(Stack *s) { s->top = -1; // 栈顶指针初始化为-1,表示栈为空 } // 判断栈是否为空 bool is_empty(Stack *s) { return s->top == -1; // 栈为空的条件是栈顶指针为-1 } // 判断栈是否已满 bool is_full(Stack *s, int max) { return s->top == max - 1; // 栈已满的条件是栈顶指针等于最大容量减1 } // 入栈 bool push(Stack *s, int x, int max) { if (is_full(s, max)) { // 如果栈已满,无法入栈 return false; } s->data[++s->top] = x; // 栈顶指针先加1,再将元素x入栈 return true; } // 出栈 int pop(Stack *s) { if (is_empty(s)) { // 如果栈为空,无法出栈 return -1; } return s->data[s->top--]; // 先取出栈顶元素,再将栈顶指针减1 } // 队列结构体 typedef struct { Stack s1; // 入队栈 Stack s2; // 出队栈 int max1; // 入队栈的最大容量 int max2; // 出队栈的最大容量 } Queue; // 初始化队列 void init_queue(Queue *q, int max1, int max2) { q->max1 = max1; // 设置入队栈的最大容量 q->max2 = max2; // 设置出队栈的最大容量 init_stack(&q->s1); // 初始化入队栈 init_stack(&q->s2); // 初始化出队栈 } // 入队 bool enqueue(Queue *q, int x) { if (is_full(&q->s1, q->max1)) { // 如果入队栈已满,无法入队 return false; } push(&q->s1, x, q->max1); // 将元素x入栈s1 return true; } // 出队 int dequeue(Queue *q) { if (is_empty(&q->s2)) { // 如果出队栈为空 while (!is_empty(&q->s1)) { // 将入队栈中所有元素出栈并压入出队栈 push(&q->s2, pop(&q->s1), q->max2); } } return pop(&q->s2); // 直接出栈出队栈的栈顶元素 } // 判断队列是否为空 bool is_queue_empty(Queue *q) { return is_empty(&q->s1) && is_empty(&q->s2); // 队列为空的条件是入队栈和出队栈均为空 } int main() { Queue q; init_queue(&q, MAXSIZE, MAXSIZE); // 初始化队列,设置最大容量为100 enqueue(&q, 1); // 入队1 enqueue(&q, 2); // 入队2 enqueue(&q, 3); // 入队3 printf("%d\n", dequeue(&q)); // 出队1,输出1 printf("%d\n", dequeue(&q)); // 出队2,输出2 enqueue(&q, 4); // 入队4 printf("%d\n", dequeue(&q)); // 出队3,输出3 printf("%d\n", dequeue(&q)); // 出队4,输出4 printf("%d\n", dequeue(&q)); // 出队-1,表示队列已空 return 0; } ``` 在注释中,我们解释了每个函数的作用和实现细节,让读者更容易理解代码的意义和功能。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值