类C语 类C语言语法示例 #define true 1 define false o 1、预定义常量和 #define ok 1 RROR O 类型 #define infeasible-1 #define overFlow-2 typedef in Status;/ Status是函数的类型,其值是函数结果状态代码 2、数据结构的存 typedef ElemType first 储结构 函数类型函数名(函数参数表){ 3、基本*作的算 ∥/算法说明 语句序列 /函数名 简单赋值 变量名≡表达式 串联赋值: 变量名1=变量名2=…=变量名k=表达式 (变量名1,…,变量名k)=(表达式1,…,表达式k); 结构名=结构名 4、赋值语句 成组赋值 结构名=(值1,…,值k) 变量名[]=表达式 变量名[起始下标终止下标]=变量名[起始下标终止下标] 交换赋值 变量名<>变量名 条件赋值 变量名=条件表达式?表达式?表达式T:表达式F 1、f(表达式)语句 else语句 3、 switch(表达式){ case值1:语句序列1: break; 5、选择语句 case值n:语句序列n: break; defau:语句序列n+1: break
ListDelete(&L,i,e) 元素。 ... 二、类 C 语言语法 类 C 语言语法示例 1、预定义常量和 类型 #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2 typedef in Status; //Status 是函数的类型,其值是函数结果状态代码。 2、数据结构的存 储结构 typedef ElemType first; 3、基本*作的算 法 函数类型 函数名(函数参数表){ //算法说明 语句序列 }//函数名 4、赋值语句 简单赋值: 变量名=表达式; 串联赋值: 变量名 1=变量名 2=...=变量名 k=表达式; 成组赋值: (变量名 1,...,变量名 k)=(表达式 1,...,表达式 k); 结构名=结构名; 结构名=(值 1,...,值 k); 变量名[]=表达式; 变量名[起始下标..终止下标]=变量名[起始下标..终止下标]; 交换赋值: 变量名<-->变量名; 条件赋值: 变量名=条件表达式?表达式?表达式 T:表达式 F 5、选择语句 1、if(表达式) 语句; 2、if(表达式) 语句; else 语句; 3、switch(表达式){ case 值 1:语句序列 1;break; ... case 值 n:语句序列 n;break; default:语句序列 n+1;break; } 4、switch{
case条件1:语句序列1: break; 条件n:语句序列n ult:语句序列n+1 for(赋初值表达式:条件;修改表达式序列)语句 6、循环语句 Whle(条件)语句 do{语句序列} while(条件) retum[表达式]: 7、结束语句 um;//函数结束语句 break;//case结束语句 exit(异常代码);∥/异常结束语句 8、输入和输出语 scanf([格式串],变量1,…,变量n) 9、注释 ∥/文字序列 10、基本函数 max(表达式1 min, abs, floor, ceil, eof, eoln 11、逻辑运算 &&与运算:|运算 例:线性表的实现 数据对象:D={ ail ai(( Elem set.i=1,2…,nn>=0 数据关系:R1={<a1,a>|a-1,a(-Di=2…,n} 基本*作 InitList(&L) ListInsert(&L, i, e) ListDelete(&L,i, &e) ListInsert(List &L, int i, Elem Type e) [if(i<1li>Length+)return ERROR n1])
case 条件 1:语句序列 1;break; ... case 条件 n:语句序列 n;break; default:语句序列 n+1;break; } 6、循环语句 for(赋初值表达式;条件;修改表达式序列)语句; while(条件)语句; do{ 语句序列}while(条件); 7、结束语句 return [表达式]; return; //函数结束语句 break; //case 结束语句 exit(异常代码); //异常结束语句 8、输入和输出语 句 scanf([格式串],变量 1,...,变量 n); 9、注释 //文字序列 10、基本函数 max(表达式 1,...,表达式 n) min,abs,floor,ceil,eof,eoln 11、逻辑运算 &&与运算;||或运算 例:线性表的实现: ADT List{ 数据对象: D={ai| ai(-ElemSet,i=1,2,...,n,n>=0} 数据关系: R1={<ai-1,ai>| ai-1,ai(- D,i=2,...,n} 基本*作: InitList(&L) DestroyList(&L) ListInsert(&L,i,e) ListDelete(&L,i,&e) }ADT List ListInsert(List &L,int i,ElemType e) {if(i<1||i>L.length+) return ERROR; q=&(L.elem[i-1]);
for(p=&(L elem[L length -1D: p>=g-p)*(p+1=*p q=e ++Length tum OK: 下面是C语言编译通过
for(p=&(L.elem[L.length-1]);p>=q;--p) *(p+1)=*p; *q=e; ++L.length; return OK; } 下面是 C 语言编译通过的示例:
define error o define OK 1 struct STU I char name[20]: char stun[10] int age; int score stu[50] struct LIST I struct STU stu[50]; nt length JL: int printlist(struct LIST L) I int i; printf( name stun age scoreIn"); for(i=0; i<L length; i++ printf("%s%sIt%dt%dIn",L.stu[ name, L. stu[].stun, L. stu[i] age, L. stu[. score); printf( In); int listinsert(struct LIST*L, int i, struct STU e) struct STU*p,’q if (i<1i>L->length+1) return Error: &(L→>stu[i-1]) for(p=&L→stu[L→> length-1];p>=q;-p) (p+1)=*; *q=e; ++L->length; return OK: y/Listinsert Before i*/ maino I struct STU e L length=0; strcpy(e. name, zmofun"); strcpy(e. stun, 100001 ); eage=80 e score=1000: listinsert(&L,1, e) printlist(L); printf("List length now is %d InIn", L length); strcpy(e. name, bobin") strcpy(e. stun, 100002 );
#define ERROR 0 #define OK 1 struct STU { char name[20]; char stuno[10]; int age; int score; }stu[50]; struct LIST { struct STU stu[50]; int length; }L; int printlist(struct LIST L) { int i; printf("name stuno age score\n"); for(i=0;i<L.length;i++) printf("%s %s\t%d\t%d\n", L.stu[i].name, L.stu[i].stuno, L.stu[i].age, L.stu[i].score); printf("\n"); } int listinsert(struct LIST *L,int i,struct STU e) { struct STU *p,*q; if (i<1||i>L->length+1) return ERROR; q=&(L->stu[i-1]); for(p=&L->stu[L->length-1];p>=q;--p) *(p+1)=*p; *q=e; ++L->length; return OK; }/*ListInsert Before i */ main() { struct STU e; L.length=0; strcpy(e.name,"zmofun"); strcpy(e.stuno,"100001"); e.age=80; e.score=1000; listinsert(&L,1,e); printlist(L); printf("List length now is %d.\n\n",L.length); strcpy(e.name,"bobjin"); strcpy(e.stuno,"100002");
eage=80 e score=1000 listinsert(&L, 1, e) printlist(L) printf("List length now is %dInIn, Llength);
e.age=80; e.score=1000; listinsert(&L,1,e); printlist(L); printf("List length now is %d.\n\n",L.length); }