本文目錄一覽:
- 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; in – 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; iNUM; i++) {
printf(“請輸入第(%d)個數:”,i+1);
scanf(“%d”,arr[i]);
}
printf(“\n輸入如下排列:\n”);
for(i=0; iNUM; i++) {
printf(“%4d”,arr[i]);
}/*
for(i=0; iNUM; i++) {
for(j=i+1; jNUM; j++) {
if(arr[i]arr[j]) {
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}*/
bubbleSort_1(arr,NUM);
/*printf(“\n從小到大如下排列:\n”);
for(i=0; iNUM; i++) {
printf(“%4d”,arr[i]);
}*/
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(np2-nump2-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]並賦值 */
#includestdio.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; in; i++){ /* 循環,初始條件i=0,終止條件in,步長為1 */
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語言程序
#includereg52.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;x0;x–)
for(y=110;y0;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)(i250))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;i8;i++)
{
temp=temp1;
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;i8;i++)
{
scl=1;
delay();
k=(k1)|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(sec100) //防止首次讀取出錯誤數據
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. …
*************************************************/
3:源文件頭部應進行注釋,列出:版權說明、版本號、生成日期、作者、模塊目的/功能、主要函數及其功能、修改日誌等。
示例:下面這段源文件的頭注釋比較標準,當然,並不局限於此格式,但上述信息建議要包含在內。
/************************************************************
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: // 其它說明
*************************************************/
5:邊寫代碼邊注釋,修改代碼同時修改相應的注釋,以保證注釋與代碼的一致性。不再有用的注釋要刪除。
6:注釋的內容要清楚、明了,含義準確,防止注釋二義性。
說明:錯誤的注釋不但無益反而有害。
7:避免在注釋中使用縮寫,特別是非常用縮寫。
說明:在使用縮寫時或之前,應對縮寫進行必要的說明。
8:注釋應與其描述的代碼相近,對代碼的注釋應放在其上方或右方(對單條語句的注釋)相鄰位置,不可放在下面,如放於上方則需與其上面的代碼用空行隔開。
示例:如下例子不符合規範。
例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;
9:對於所有有物理含義的變數、常量,如果其命名不是充分自注釋的,在聲明時都必須加以注釋,說明其物理含義。變數、常量、宏的注釋應放在其上方相鄰位置或右方。
示例:
/* active statistic task number */
#define MAX_ACT_TASK_NUMBER 1000
#define MAX_ACT_TASK_NUMBER 1000 /* active statistic task number */
10:數據結構聲明(包括數組、結構、類、枚舉等),如果其命名不是充分自注釋的,必須加以注釋。對數據結構的注釋應放在其上方相鄰位置,不可放在下面;對結構中的每個域的注釋放在此域的右方。
示例:可按如下形式說明枚舉/數據/聯合結構。
/* 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*/
};
11:全局變數要有較詳細的注釋,包括對其功能、取值範圍、哪些函數或過程存取它以及存取時注意事項等的說明。
示例:
/* 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;
12:注釋與所描述內容進行同樣的縮排。
說明:可使程序排版整齊,並方便注釋的閱讀與理解。
示例:如下例子,排版不整齊,閱讀稍感不方便。
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
}
13:將注釋與其上面的代碼用空行隔開。
示例:如下例子,顯得代碼過於緊湊。
/* code one comments */
program code one
/* code two comments */
program code two
應如下書寫
/* code one comments */
program code one
/* code two comments */
program code two
14:對變數的定義和分支語句(條件分支、循環語句等)必須編寫注釋。
說明:這些語句往往是程序實現某一特定功能的關鍵,對於維護人員來說,良好的注釋幫助更好的理解程序,有時甚至優於看設計文檔。
15:對於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)
4:在程序塊的結束行右方加註釋標記,以表明某程序塊的結束。
說明:當代碼段較長,特別是多重嵌套時,這樣做可以使代碼更清晰,更便於閱讀。
示例:參見如下例子。
if (…)
{
// program code
while (index MAX_INDEX)
{
// program code
} /* end of while (index MAX_INDEX) */ // 指明該條while語句結束
} /* end of if (…)*/ // 指明是哪條if語句結束
5:注釋格式盡量統一,建議使用”/* …… */”。
6:注釋應考慮程序易讀及外觀排版的因素,使用的語言若是中、英兼有的,建議多使用中文,除非能用非常流利準確的英文表達。
說明:注釋語言不統一,影響程序易讀性和外觀排版,出於對維護人員的考慮,建議使用中文。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/238093.html