c语言程序带注释50个,c语言程序示例带注释

发布时间:2023-01-08

本文目录一览:

  1. 求50行简单C语言程序代码,基础的就好
  2. c语言程序,请大佬详细一点,最好能有注释orz
  3. 求30行以上简单的C语言程序,要每行都有注释,明天用
  4. 给C语言程序加上注释
  5. 带注释的c语言程序
  6. C语言程序注释

求50行简单C语言程序代码,基础的就好

#include stdio.h
#include stdlib.h
#define NUM 10
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
//冒泡排序算法
//基本思想:比较相邻的两个数,如果前者比后者大,则进行交换。每一轮排序结束,选出一个未排序中最大的数放到数组后面。
void bubbleSort(int *arr, int n) {
    int i,j;
    for (i = 0; i < n - 1; i++)
        for (j = 0; j < n - i - 1; j++) {
            //如果前面的数比后面大,进行交换
            if (arr[j] > arr[j + 1]) {
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
}
//最差时间复杂度为O(n^2),平均时间复杂度为O(n^2)。稳定性:稳定。辅助空间O(1)。
//升级版冒泡排序法:通过从低到高选出最大的数放到后面,再从高到低选出最小的数放到前面,
//如此反复,直到左边界和右边界重合。当数组中有已排序好的数时,这种排序比传统冒泡排序性能稍好。
//升级版冒泡排序算法
void bubbleSort_1(int *arr, int n) {
    //设置数组左右边界
    int left = 0, right = n - 1;
    //当左右边界未重合时,进行排序
    while (left <= right) {
        int i,j;
        //从左到右遍历选出最大的数放到数组右边
        for (i = left; i < right; i++) {
            if (arr[i] > arr[i + 1]) {
                int temp = arr[i];
                arr[i] = arr[i + 1];
                arr[i + 1] = temp;
            }
        }
        right--;
        //从右到左遍历选出最小的数放到数组左边
        for (j = right; j > left; j--) {
            if (arr[j + 1] < arr[j]) {
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
        left++;
    }
}
int main(int argc, char *argv[]) {
    int arr[NUM],i,j,temp;
    printf("请输入10个数:\n");
    for(i=0; i < NUM; i++) {
        printf("请输入第(%d)个数:",i+1);
        scanf("%d",&arr[i]);
    }
    printf("\n输入如下排列:\n");
    for(i=0; i < NUM; i++) {
        printf("%4d",arr[i]);
    }
    bubbleSort_1(arr,NUM);
    printf("\n从大到小如下排列:\n");
    for(i=NUM-1; i >= 0; i--) {
        printf("%4d",arr[i]);
    }
    return 0;
}

c语言程序,请大佬详细一点,最好能有注释orz

#include stdio.h
int main()
{
    int Year, WeightClass;
    float Weight, Fee;
    //可以进行多组测试,直到输入的Year为负数为止
    while (1)
    {
        printf("Please input the model year registration :\n");
        scanf_s("%d", &Year); //输入Year
        if (Year < 0)
        {
            printf("Test End\n");
            break;
        }
        printf("Please input the weight :\n");
        scanf_s("%f", &Weight); //输入Weight
        if (Year <= 1970) //第一个条件,1970年以前的(包括1970)
        {
            if (Weight < 2700)      //第二个条件,小于2700磅
            {
                printf("\nWeight Class |Fee\n");
                printf("%-16d|$%-7.2f\n\n", 1, 16.5);
            }
            else if (Weight >= 2700 && Weight <= 3800)
            {
                printf("\nWeight Class |Fee\n");
                printf("%-16d|$%-7.2f\n\n", 2, 25.5);
            }
            else
            {
                printf("\nWeight Class |Fee\n");
                printf("%-16d|$%-7.2f\n\n", 3, 46.5);
            }
        }
        else if (Year >= 1971 && Year <= 1979)
        {
            if (Weight < 2700)
            {
                printf("\nWeight Class |Fee\n");
                printf("%-16d|$%-7.2f\n\n", 4, 27.0);
            }
            else if (Weight >= 2700 && Weight <= 3800)
            {
                printf("\nWeight Class |Fee\n");
                printf("%-16d|$%-7.2f\n\n", 5, 30.5);
            }
            else
            {
                printf("\nWeight Class |Fee\n");
                printf("%-16d|$%-7.2f\n\n", 6, 52.5);
            }
        }
        else
        {
            if (Weight < 3500)
            {
                printf("\nWeight Class |Fee\n");
                printf("%-16d|$%-7.2f\n\n", 7, 35.5);
            }
            else
            {
                printf("\nWeight Class |Fee\n");
                printf("%-16d|$%-7.2f\n\n", 8, 65.5);
            }
        }
    }
    return 0;
}

//测试输出: //Please input the model year registration : //1965 //Please input the weight : //3500 // //Weight Class | Fee //2 | $25.50 // //Please input the model year registration : //1975 //Please input the weight : //2500 // //Weight Class | Fee //4 | $27.00 // //Please input the model year registration : //1981 //Please input the weight : //3600 // //Weight Class | Fee //8 | $65.50 // //Please input the model year registration :

求30行以上简单的C语言程序,要每行都有注释,明天用

C语言程序设计(第7章结构体与共用体) 插入的节点可以在表头、表中或表尾。假定我们按照以学号为顺序建立链表,则插入的节点依次与表中节点相比较,找到插入位置。由于插入的节点可能在链表的头,会对链表的头指针造成修改,所以定义插入节点的函数的返回值定义为返回结构体类型的指针。节点的插入函数如下:

struct node *insert(head,pstr,n)
/*插入学号为n、姓名为pstr的节点*/
struct node *head; /*链表的头指针*/
char *pstr;
int n;
{
    struct node *p1,*p2,*p3;
    p1=(struct node*)malloc(sizeof(struct node)); /*分配一个新节点*/
    strcpy(p1->str,pstr); /*写入节点的姓名字串*/
    p1->num=n; /*学号*/
    p2=head;
    if(head==NULL) /*空表*/
    {
        head=p1;
        p1->next=NULL; /*新节点插入表头*/
    }
    else
    {
        /*非空表*/
        while(n > p2->num && p2->next!=NULL)
        {
            p3=p2;
            p2=p2->next; /*跟踪链表增长*/
        }
        if(n <= p2->num) /*找到插入位置*/
        {
            if (head == p2) /*插入位置在表头*/
            {
                head=p1;
                p1->next=p2;
            }
            else
            {
                /*插入位置在表中*/
                p3->next=p1;
                p1->next=p2;
            }
        }
        else
        {
            /*插入位置在表尾*/
            p2->next=p1;
            p1->next=NULL;
        }
    }
    return(head); /*返回链表的头指针*/
}

给C语言程序加上注释

第一个程序 (/* */内为注释)

#define X 10 /* 定义X为10 */
#define Y 30 /* 定义Y为30 */
#define N 20 /* 定义N为20 */
int A[N]={2,5,15,30,1,40,17,50,9,21,32,8,41,22,49,31,33,18,80,5}; /* 定义一个20位的数组A[N]并赋值 */
#include stdio.h /* 引用头文件stdio.h */
void del(int *A, int *n, int x, int y) /* 定义一个函数del,输入参数为int型的指针A,n和int型的x,y,作用是删去数组中大于x或小于y的数 */
{
    int i,j; /* 定义整数型变量i,j */
    for(i=j=0; i<*n; i++) /*循环,初始条件i=j=0,终止条件i指针n的值,循环步长为1 */
        if(A[i]>y || A[i]<x) /* 判断条件如果数组元素A[i]的值大于y或者小于x */
            A[j++]=A[i]; /* 将数组元素A[i]赋值给数组元素A[j],然后j加1 */
    *n=j; /* 给指针n赋值为j */
}
void output(int *A, int n) /* 定义函数output,输入参数为整型指针A和整型变量n,作用为输出数组前n个数 */
{
    int i; /* 定义整型变量i */
    printf("\n数组有%d个元素:\n",n); /* 在屏幕输出“数组有n个元素:”,(n为输入量或所赋值的量)*/
    for(i=0; i < n; i++){
        printf("%7d",A[i]); /* 在屏幕输出数组A[i]的各个值 */
        if((i+1)%10==0) /* 判断,条件为如果(i+1)除以10取余数等于0 */
            printf("\n"); /* 在屏幕上输出换行 */
    }
    printf("\n"); /* 在屏幕上输出换行 */
}
void main() /* 主程序 */
{
    int n; /* 定义一个整型变量n */
    n=N; /* 给n赋值为N,之前有定义N为20,所以实际n=20 */
    output(A,n); /* 以数组A和n为参数,执行函数output */
    del(A,&n,X,Y); /* 执行函数del */
    output(A,n); /* 执行函数output */
}

带注释的c语言程序

#include reg52.h
#define uchar unsigned char
#define uint unsigned int
bit write=0; //写24C02的标志;
sbit sda=P2^0;
sbit scl=P2^1;
sbit dula=P2^6;
sbit wela=P2^7;
uchar sec,tcnt;
uchar code table[]={
    0x3f,0x06,0x5b,0x4f,
    0x66,0x6d,0x7d,0x07,
    0x7f,0x6f,0x77,0x7c,
    0x39,0x5e,0x79,0x71};
void delay()
{ ;; }
void delay1ms(uint z)
{
    uint x,y;
    for(x=z;x>0;x--)
        for(y=110;y>0;y--);
}
void start() //开始信号
{
    sda=1;
    delay();
    scl=1;
    delay();
    sda=0;
    delay();
}
void stop() //停止
{
    sda=0;
    delay();
    scl=1;
    delay();
    sda=1;
    delay();
}
void respons() //应答
{
    uchar i;
    scl=1;
    delay();
    while((sda==1)&&(i<250))i++;
    scl=0;
    delay();
}
void init()
{
    sda=1;
    delay();
    scl=1;
    delay();
}
void write_byte(uchar date)
{
    uchar i,temp;
    temp=date;
    for(i=0;i<8;i++)
    {
        temp=temp<<1;
        scl=0;
        delay();
        sda=CY;
        delay();
        scl=1;
        delay();
    }
    scl=0;
    delay();
    sda=1;
    delay();
}
uchar read_byte()
{
    uchar i,k;
    scl=0;
    delay();
    sda=1;
    delay();
    for(i=0;i<8;i++)
    {
        scl=1;
        delay();
        k=(k<<1)|sda;
        scl=0;
        delay();
    }
    return k;
}
void write_add(uchar address,uchar date)
{
    start();
    write_byte(0xa0);
    respons();
    write_byte(address);
    respons();
    write_byte(date);
    respons();
    stop();
}
uchar read_add(uchar address)
{
    uchar date;
    start();
    write_byte(0xa0);
    respons();
    write_byte(address);
    respons();
    start();
    write_byte(0xa1);
    respons();
    date=read_byte();
    stop();
    return date;
}
void display(uchar bai_c,uchar sh_c) //显示程序
{
    dula=0;
    P0=table[bai_c]; //显示第一位
    dula=1;
    dula=0;
    wela=0;
    P0=0x7e;
    wela=1;
    wela=0;
    delay1ms(5);
    dula=0;
    P0=table[sh_c]; //显示第二位
    dula=1;
    dula=0;
    wela=0;
    P0=0x7d;
    wela=1;
    wela=0;
    delay1ms(5);
}
void main()
{
    init();
    sec=read_add(2); //读出保存的数据赋于sec
    if(sec>100) //防止首次读取出错误数据
        sec=0;
    TMOD=0x01; //定时器工作在方式1
    ET0=1;
    EA=1;
    TH0=(65536-50000)/256; //对TH0 TL0赋值
    TL0=(65536-50000)%256; //使定时器0.05秒中断一次
    TR0=1; //开始计时
    while(1)
    {
        display(sec/10,sec%10);
        if(write==1) //判断计时器是否计时一秒
        {
            write=0; //清零
            write_add(2,sec); //在24c02的地址2中写入数据sec
        }
    }
}
void t0() interrupt 1 //定时中断服务函数
{
    TH0=(65536-50000)/256; //对TH0 TL0赋值
    TL0=(65536-50000)%256; //重装计数初值
    tcnt++; //每过50ms tcnt加一
    if(tcnt==20) //计满20次(1秒)时
    {
        tcnt=0; //重新再计
        sec++;
        write=1; //1秒写一次24C02
        if(sec==100) //定时100秒,再从零开始计时
            sec=0;
    }
}

C语言程序注释

C语言编程规范-注释 规则:

  1. 一般情况下,源程序有效注释量必须在20%以上。 说明:注释的原则是有助于对程序的阅读理解,在该加的地方都加了,注释不宜太多也不能太少,注释语言必须准确、易懂、简洁。
  2. 说明性文件(如头文件.h文件、.inc文件、.def文件、编译说明文件.cfg等)头部应进行注释,注释必须列出:版权说明、版本号、生成日期、作者、内容、功能、与其它文件的关系、修改日志等,头文件的注释中还应有函数功能简要说明。 示例:下面这段头文件的头注释比较标准,当然,并不局限于此格式,但上述信息建议要包含在内。
/*************************************************
Copyright (C), 1988-1999, Tech. Co., Ltd.
File name: // 文件名
Author:
Version:
Date: // 作者、版本及完成日期
Description: // 用于详细说明此程序文件完成的主要功能,与其他模块
             // 或函数的接口,输出值、取值范围、含义及参数间的控
             // 制、顺序、独立或依赖等关系
Others: // 其它内容的说明
Function List: // 主要函数列表,每条记录应包括函数名及功能简要说明
1. ....
History: // 修改历史记录列表,每条修改记录应包括修改日期、修改
         // 者及修改内容简述
1. Date:
   Author:
   Modification:
2. ...
*************************************************/
  1. 源文件头部应进行注释,列出:版权说明、版本号、生成日期、作者、模块目的/功能、主要函数及其功能、修改日志等。 示例:下面这段源文件的头注释比较标准,当然,并不局限于此格式,但上述信息建议要包含在内。
/************************************************************
Copyright (C), 1988-1999, Tech. Co., Ltd.
FileName: test.cpp
Author:
Version :
Date:
Description: // 模块描述
Version: // 版本信息
Function List: // 主要函数及其功能
1. -------
History: // 历史修改记录
author time version desc
David 96/10/12 1.0 build this moudle
***********************************************************/

说明:Description一项描述本文件的内容、功能、内部各部分之间的关系及本文件与其它文件关系等。History是修改历史记录列表,每条修改记录应包括修改日期、修改者及修改内容简述。 4. 函数头部应进行注释,列出:函数的目的/功能、输入参数、输出参数、返回值、调用关系(函数、表)等。 示例:下面这段函数的注释比较标准,当然,并不局限于此格式,但上述信息建议要包含在内。

/*************************************************
Function: // 函数名称
Description: // 函数功能、性能等的描述
Calls: // 被本函数调用的函数清单
Called By: // 调用本函数的函数清单
Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
Input: // 输入参数说明,包括每个参数的作
       // 用、取值说明及参数间关系。
Output: // 对输出参数的说明。
Return: // 函数返回值的说明
Others: // 其它说明
*************************************************/
  1. 边写代码边注释,修改代码同时修改相应的注释,以保证注释与代码的一致性。不再有用的注释要删除。
  2. 注释的内容要清楚、明了,含义准确,防止注释二义性。 说明:错误的注释不但无益反而有害。
  3. 避免在注释中使用缩写,特别是非常用缩写。 说明:在使用缩写时或之前,应对缩写进行必要的说明。
  4. 注释应与其描述的代码相近,对代码的注释应放在其上方或右方(对单条语句的注释)相邻位置,不可放在下面,如放于上方则需与其上面的代码用空行隔开。 示例:如下例子不符合规范。 例1:
/* get replicate sub system index and net indicator */
repssn_ind = ssn_data[index].repssn_index;
repssn_ni = ssn_data[index].ni;

例2:

repssn_ind = ssn_data[index].repssn_index;
repssn_ni = ssn_data[index].ni;
/* get replicate sub system index and net indicator */

应如下书写

/* get replicate sub system index and net indicator */
repssn_ind = ssn_data[index].repssn_index;
repssn_ni = ssn_data[index].ni;
  1. 对于所有有物理含义的变量、常量,如果其命名不是充分自注释的,在声明时都必须加以注释,说明其物理含义。变量、常量、宏的注释应放在其上方相邻位置或右方。 示例:
/* active statistic task number */
#define MAX_ACT_TASK_NUMBER 1000
#define MAX_ACT_TASK_NUMBER 1000 /* active statistic task number */
  1. 数据结构声明(包括数组、结构、类、枚举等),如果其命名不是充分自注释的,必须加以注释。对数据结构的注释应放在其上方相邻位置,不可放在下面;对结构中的每个域的注释放在此域的右方。 示例:可按如下形式说明枚举/数据/联合结构。
/* sccp interface with sccp user primitive message name */
enum SCCP_USER_PRIMITIVE
{
    N_UNITDATA_IND, /* sccp notify sccp user unit data come */
    N_NOTICE_IND, /* sccp notify user the No.7 network can not */
    /* transmission this message */
    N_UNITDATA_REQ, /* sccp user's unit data transmission request*/
};
  1. 全局变量要有较详细的注释,包括对其功能、取值范围、哪些函数或过程存取它以及存取时注意事项等的说明。 示例:
/* The ErrorCode when SCCP translate */
/* Global Title failure, as follows */ // 变量作用、含义
/* 0 - SUCCESS 1 - GT Table error */
/* 2 - GT error Others - no use */ // 变量取值范围
/* only function SCCPTranslate() in */
/* this modual can modify it, and other */
/* module can visit it through call */
/* the function GetGTTransErrorCode() */ // 使用方法
BYTE g_GTTranErrorCode;
  1. 注释与所描述内容进行同样的缩排。 说明:可使程序排版整齐,并方便注释的阅读与理解。 示例:如下例子,排版不整齐,阅读稍感不方便。
void example_fun( void )
{
    /* code one comments */
CodeBlock One
    /* code two comments */
CodeBlock Two
}

应改为如下布局。

void example_fun( void )
{
    /* code one comments */
    CodeBlock One
    /* code two comments */
    CodeBlock Two
}
  1. 将注释与其上面的代码用空行隔开。 示例:如下例子,显得代码过于紧凑。
/* code one comments */
program code one
/* code two comments */
program code two

应如下书写

/* code one comments */
program code one
/* code two comments */
program code two
  1. 对变量的定义和分支语句(条件分支、循环语句等)必须编写注释。 说明:这些语句往往是程序实现某一特定功能的关键,对于维护人员来说,良好的注释帮助更好的理解程序,有时甚至优于看设计文档。
  2. 对于switch语句下的case语句,如果因为特殊情况需要处理完一个case后进入下一个case处理,必须在该case语句处理完、下一个case语句前加上明确的注释。 说明:这样比较清楚程序编写者的意图,有效防止无故遗漏break语句。 示例(注意斜体加粗部分):
case CMD_UP:
    ProcessUp();
    break;
case CMD_DOWN:
    ProcessDown();
    break;
case CMD_FWD:
    ProcessFwd();
    if (...)
    {
        ...
        break;
    }
    else
    {
        ProcessCFW_B(); // now jump into case CMD_A
    }
case CMD_A:
    ProcessA();
    break;
case CMD_B:
    ProcessB();
    break;
case CMD_C:
    ProcessC();
    break;
case CMD_D:
    ProcessD();
    break;

建议:

  1. 避免在一行代码或表达式的中间插入注释。 说明:除非必要,不应在代码或表达中间插入注释,否则容易使代码可理解性变差。
  2. 通过对函数或过程、变量、结构等正确的命名以及合理地组织代码的结构,使代码成为自注释的。 说明:清晰准确的函数、变量等的命名,可增加代码可读性,并减少不必要的注释。
  3. 在代码的功能、意图层次上进行注释,提供有用、额外的信息。 说明:注释的目的是解释代码的目的、功能和采用的方法,提供代码以外的信息,帮助读者理解代码,防止没必要的重复注释信息。 示例:如下注释意义不大。
/* if receive_flag is TRUE */
if (receive_flag)

而如下的注释则给出了额外有用的信息。

/* if mtp receive a message from links */
if (receive_flag)
  1. 在程序块的结束行右方加注释标记,以表明某程序块的结束。 说明:当代码段较长,特别是多重嵌套时,这样做可以使代码更清晰,更便于阅读。 示例:参见如下例子。
if (...)
{
    // program code
    while (index < MAX_INDEX)
    {
        // program code
    } /* end of while (index < MAX_INDEX) */ // 指明该条while语句结束
} /* end of if (...)*/ // 指明是哪条if语句结束
  1. 注释格式尽量统一,建议使用"/* …… */"。
  2. 注释应考虑程序易读及外观排版的因素,使用的语言若是中、英兼有的,建议多使用中文,除非能用非常流利准确的英文表达。 说明:注释语言不统一,影响程序易读性和外观排版,出于对维护人员的考虑,建议使用中文。