本文目錄一覽:
- 1、c語言表示列表的問題
- 2、一個有關用C語言生成數字列表的問題
- 3、C語言動態列表排序
- 4、C語言如何建立一個列表,編程怎麼編,舉例說一下,謝謝啦
- 5、C語言宏定義——預處理指令列表
- 6、C語言printf下各種%號列表
c語言表示列表的問題
/*
抱歉,又是我
*/
//#define debug
struct dimension
{
int num_row ;
int num_col ;
} ;
void table_set_entry (const int t[] ,const struct dimension *dim ,const int row ,const int col ,const int v)
{
if (t == 0 || dim == 0 || row 0 || col 0 || row = dim-num_row || col = dim-num_col)
return ;
t[col * dim-num_row + row] = v ;
return ;
}
int table_get_entry (const int t[] ,const struct dimension *dim ,const int row ,const int col)
{
if (t == 0 || dim == 0 || row 0 || col 0 || row = dim-num_row || col = dim-num_col)
return 0 ;
return t[col * dim-num_row + row] ;
}
void table_clear (const int t[] ,struct dimension *dim)
{
if (t == 0 || dim == 0)
return ;
const int* pb = t ;
const int* pe = t + dim-num_row * dim-num_col ;
int* p ;
for (p = pb ; p pe ; p++)
*p = 0 ;
return ;
}
void table_copy (const int a[] ,const int b[] ,struct dimension *dim)
{
if (a == 0 || b == 0 || dim == 0)
return ;
const int* pab = a ;
const int* pae = a + dim-num_row * dim-num_col ;
int* pa ;
const int* pbb = b ;
int* pb ;
for (pa = pab ,pb = pbb ; pa pae ; pa++ ,pb++)
*pb = *pa ;
return ;
}
#ifdef debug
int main ()
{
void table_set_entry (const int t[] ,const struct dimension *dim ,const int row ,const int col ,const int v) ;
int table_get_entry (const int t[] ,const struct dimension *dim ,const int row ,const int col) ;
void table_clear (const int t[] ,struct dimension *dim) ;
void table_copy (const int a[] ,const int b[] ,struct dimension *dim) ;
int t[10][10] ;
int f[100] ;
const struct dimension dim = {10 ,10} ;
const int row = 4 ;
const int col = 7 ;
const int v = 250 ;
table_set_entry (t ,dim ,row ,col ,v) ;
printf (“isn’t %d equal to %d ?\n” ,t[col][row] ,table_get_entry (t ,dim ,row ,col)) ;
table_copy (t ,f ,dim) ;
table_clear (t ,dim) ;
printf (“t : %d while f : %d\n” ,t[col][row] ,f[col * dim.num_row + row]) ;
return 0 ;
}
#endif
一個有關用C語言生成數字列表的問題
如果生成的列表是用來列印的:
int i,n; char fmt[80]; scanf(“%d,%d”,n,k);
sprintf(fmt,”%%0%dd,”,n); for ( i=0;ik;i++ ) printf(fmt,i+1);
如果生成的列表需要保存:
int i,n,k; char fmt[80],mlist[1024][10];
scanf(“%d,%d”,n,k); if ( k1024 ) k=1024; if ( n9 ) n=9;
sprintf(fmt,”%%0%dd,”,n); for ( i=0;ik;i++ ) sprintf(mlist[i],fmt,i+1);
C語言動態列表排序
鏈表嗎?以前練習的時候做過一個,你參考下
#includestdio.h
#includestdlib.h
#includeiostream.h
#define OK 1;
#define ERROR 0;
typedef int ElemType;
typedef int Status;
typedef struct LNode{
ElemType data;
struct LNode *next;
}LNode,*LinkList;
void CreateList(LinkList L,int n) //創建表
{
int i;
LNode *p;
L=(LinkList)malloc(sizeof(LNode));
L-next=NULL;
for(i=n;i0;i–)
{
p=(LinkList)malloc(sizeof(LNode));
printf(“輸入第%d個元素的值\n”,i);
scanf(“%d”,p-data);
p-next=L-next;
L-next=p;
}
printf(“創建成功!\n”);
}
Status GetElem(LinkList L,int i) //得到第i個元素
{
ElemType j=1,e;
LNode *p;
p=L-next;
while(jip)
{
p=p-next;
j++;
}
if(!p||ji)
{
printf(“第%d個元素不存在!\n”,i);
return ERROR;
}
e=p-data;
printf(“第%d個元素是%d\n”,i,e);
return OK;
}
Status ListInsert(LinkList L,int i,ElemType e) //插入元素
{
ElemType j;
LNode *p,*s;
p=L;
j=1;
while(pji)
{
p=p-next;
++j;
}
if(!p||ji)
{
printf(“不能在第%d中插入\n”,i);
}
s=(LinkList)malloc(sizeof(LNode));
s-data=e;
s-next=p-next;
p-next=s;
return OK;
}
Status ListDelete(LinkList L,int i) //刪除元素
{
LNode *p,*q;
p=L;
int j=0;
while(p-nextji-1)
{
p=p-next;
j++;
}
if(!(p-next)||ji-1)
{
printf(“查找失敗!\n”);
return ERROR;
}
q=p-next;
p-next=q-next;
free(q);
printf(“刪除成功!\n”);
return OK;
}
void MergeList(LinkList La,LinkList Lb,LinkList Lc) //歸併
{
LNode *pa,*pb,*pc;
Lc=pc=La;
pa=La-next;
pb=Lb-next;
while(papb)
{
if(pa-data=pb-data)
{
pc-next=pa;
pc=pa;
pa=pa-next;
}
else
{
pc-next=pb;
pc=pb;
pb=pb-next;
}
}
pc-next=pa?pa:pb;
printf(“歸併成功!\n”);
}
void PList(LinkList L) //列印
{
LNode *p;
p=L-next;
while(p)
{
printf(“%d “,p-data);
p=p-next;
}
printf(“\n”);
}
Status CList(LinkList L) //排序
{
LNode *p;
int flag,e;
p=L;
while(1)
{
flag=0;
for(p=L;p-next-next!=NULL;p=p-next)
{
if(p-next-datap-next-next-data)
{
e=p-next-data;
p-next-data=p-next-next-data;
p-next-next-data=e;
flag=1;
}
}
if(flag==0)
{
printf(“排序成功!\n”);
return OK;
}
}
}
int main()
{
int count=1,m,n,k,sum,i,j,g;
LinkList list[10];
printf(“輸入創建表的個數\n”);
scanf(“%d”,m);
for(;count=m;count++)
{
printf(“輸入第%d個表的元素個數\n”,count);
scanf(“%d”,n);
printf(“逆序輸入n個元素\n”);
CreateList(list[count],n);
printf(“第%d個表創建成功\n”,count);
}
sum=m+1;
while(1)
{
printf(“功能:\n1.查找某位置元素的值\n2.插入元素\n3.刪除元素\n4.元素排序\n5.兩表合併\n6.顯示錶內元素\n7.退出\n”);
scanf(“%d”,k);
switch(k)
{
case 1:
printf(“輸入查找的表\n”);
scanf(“%d”,i);
if(im)
{
printf(“不存在表%d\n”,i);
break;
}
printf(“輸入查找位置\n”);
scanf(“%d”,j);
GetElem(list[i],j);
break;
case 2:
printf(“輸入要插入的表\n”);
scanf(“%d”,i);
if(im)
{
printf(“不存在表%d\n”,i);
break;
}
printf(“輸入要插入的位置\n”);
scanf(“%d”,j);
printf(“輸入要插入的值\n”);
scanf(“%d”,g);
ListInsert(list[i],j,g);
break;
case 3:
printf(“輸入要刪除的表\n”);
scanf(“%d”,i);
if(im)
{
printf(“不存在表%d\n”,i);
break;
}
printf(“輸入要刪除的位置\n”);
scanf(“%d”,j);
ListDelete(list[i],j);
break;
case 4:
printf(“輸入要排序的表\n”);
scanf(“%d”,i);
if(im)
{
printf(“不存在表%d\n”,i);
break;
}
CList(list[i]);
break;
case 5:
printf(“輸入表1\n”);
scanf(“%d”,i);
if(im)
{
printf(“不存在表%d\n”,i);
break;
}
printf(“輸入表2\n”);
scanf(“%d”,j);
if(im)
{
printf(“不存在表%d\n”,j);
break;
}
MergeList(list[i],list[j],list[sum]);
printf(“已經將合併的標放入第%d個表中”,sum);
sum++;
m++;
break;
case 6:
printf(“輸入要顯示的表\n”);
scanf(“%d”,i);
if(im)
{
printf(“不存在表%d\n”,i);
break;
}
PList(list[i]);
break;
case 7:
return 0;
default:
printf(“錯誤的指令!/n”);
break;
}
}
}
C語言如何建立一個列表,編程怎麼編,舉例說一下,謝謝啦
首先定義一個鏈表。
struct node
{
int id;
struct node * next;
};
接下來寫一些操作的函數(插入,刪除等等)。
插入函數:
struct node* insert(struct node* pNode, int iTemp)
{
//判斷 pNode 是否為空。
if(pNode==NULL)
{
//這個節點是空,返回錯誤。
return NULL;
}
// 創建一個新的節點。
struct node* tempNode = (struct node*)malloc(sizeof(struct node));
tempNode-id= iTemp;
if(pNode-next == NULL)
{
pNode-next = tempNode;
tempNode-next = NULL;
}else
{
struct node * pNext = pNode-next;
pNode-next = tempNode;
tempNode-next = pNext;
}
return tempNode;
}
int main()
{
struct node* head = (struct node*)malloc(sizeof(struct node));
head-id = 0;
head-next = NULL;
struct node * ptemp;
ptemp = head;
for( int i=1; i10; i++)
{
struct node* temp = insert(ptemp,i);
ptemp = temp;
}
return 0;
}
C語言宏定義——預處理指令列表
#:空指令,無任何效果 #include:包含一個源代碼文件 #define:定義宏 #undef:取消已定義的宏 #if:如果給定條件為真,則編譯下面代碼 #ifdef:如果宏已經定義,則編譯下面代碼 #ifndef:如果宏沒有定義,則編譯下面代碼 #elif:如果前面的#if給定條件不為真,當前條件為真,則編譯下面代碼 #endif:結束一個#if……#else條件編譯塊 #error:停止編譯並顯示錯誤信息 預處理指令是以#號開頭的代碼行。#號必須是該行除了任何空白字元外的第一個字元。#後是指令關鍵字,在關鍵字和#號之間允許存在任意個數的空白字元。整行語句構成了一條預處理指令,該指令將在編譯器進行編譯之前對源代碼做某些轉換
C語言printf下各種%號列表
%d 按整型數據的實際長度輸出
%md m是指輸出欄位的寬度。如果數據的位數小於m,則在左端補空格;若大於m,則按數據的實際位數輸出。
%ld 輸出長整型數據
%o 以八進位輸出整型數
%x 以十六進位輸出整型數
%u 以十進位輸出unsigned型數據
%c 輸出字元
%s 輸出字元串
%ms 輸出字元佔m列,如果串長度小於m則左邊補空格,若大於m則按原長度輸出
%-ms 如果串長度小於m,則字元串相左靠,右邊補空格
%m.ns 輸出佔m列,但只取字元串的左端n個字元。這n個字元在m的右側,左邊補空格
%-m.ns 其中m,n含義同上,n個字元輸出在m列的左側,右端補空格。如果nm,則m自動取n值,即保證n個字元正常輸出
%f 不指定字元寬度,由系統自動指定,使整數部分全部輸出,並輸出6位小數。應當注意輸出的數字並非全部都是有效數字。單精度數的有效位數一般是7位,而雙精度數的有效位數一般是16位。
%m.nf 指定輸出的數列佔m列,其中有n位小數。若數值長度小於m,左端補空格。
%-m.nf與%m.nf 的區別僅在於使輸出的數值向左端靠,右端補空格。
%e 不指定輸出數據所佔的寬度和數字部分的小數位數
%m.ne和%-m.ne含義與前面相同
%g 假設一個數要用%f和%e輸出,用%g格式時自動從這兩種格式中選出最短者
原創文章,作者:PFIQY,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/330407.html