?? c語言難點(diǎn)分析整理.txt
字號(hào):
文件:
函數(shù)調(diào)用形式 說明
fopen("路徑","打開方式") 打開文件
fclose(FILE *) 防止之后被誤用
fgetc(FILE *) 從文件中讀取一個(gè)字符
fputc(ch,FILE *) 把ch代表的字符寫入這個(gè)文件里
fgets(FILE *) 從文件中讀取一行
fputs(FILE *) 把一行寫入文件中
fprintf(FILE *,"格式字符串",輸出表列) 把數(shù)據(jù)寫入文件
fscanf(FILE *,"格式字符串",輸入表列) 從文件中讀取
fwrite(地址,sizeof(),n,F(xiàn)ILE *) 把地址中n個(gè)sizeof大的數(shù)據(jù)寫入文件里
fread(地址,sizeof(),n,F(xiàn)ILE *) 把文件中n個(gè)sizeof大的數(shù)據(jù)讀到地址里
rewind(FILE *) 把文件指針撥回到文件頭
fseek(FILE *,x,0/1/2) 移動(dòng)文件指針。第二個(gè)參數(shù)是位移量,0代表從頭移,1代表從當(dāng)前位置移,2代表從文件尾移。
feof(FILE *) 判斷是否到了文件末尾
文件打開方式 說明
r 打開只能讀的文件
w 建立供寫入的文件,如果已存在就抹去原有數(shù)據(jù)
a 打開或建立一個(gè)把數(shù)據(jù)追加到文件尾的文件
r+ 打開用于更新數(shù)據(jù)的文件
w+ 建立用于更新數(shù)據(jù)的文件,如果已存在就抹去原有數(shù)據(jù)
a+ 打開或建立用于更新數(shù)據(jù)的文件,數(shù)據(jù)追加到文件尾
注:以上用于文本文件的操作,如果是二進(jìn)制文件就在上述字母后加“b”。
我們用文件最大的目的就是能讓數(shù)據(jù)保存下來。因此在要用文件中數(shù)據(jù)的時(shí)候,就是要把數(shù)據(jù)讀到一個(gè)結(jié)構(gòu)(一般保存數(shù)據(jù)多用結(jié)構(gòu),便于管理)中去,再對(duì)結(jié)構(gòu)進(jìn)行操作即可。例如,文件aa.data中存儲(chǔ)的是30個(gè)學(xué)生的成績(jī)等信息,要遍歷這些信息,對(duì)其進(jìn)行成績(jī)輸出、排序、查找等工作時(shí),我們就把這些信息先讀入到一個(gè)結(jié)構(gòu)數(shù)組中,再對(duì)這個(gè)數(shù)組進(jìn)行操作。如下例:
#include<stdio.h>
#include<stdlib.h>
#define N 30
typedef struct student /*定義儲(chǔ)存學(xué)生成績(jī)信息的數(shù)組*/
{
char *name;
int chinese;
int maths;
int phy;
int total;
}ST;
main()
{
ST a[N]; /*存儲(chǔ)N個(gè)學(xué)生信息的數(shù)組*/
FILE *fp;
void (*process[3])(ST *)={Output,Bubble,Find}; /*實(shí)現(xiàn)相關(guān)功能的三個(gè)函數(shù)*/
int choice,i=0;
Show();
printf("\nChoose:\n?");
scanf("%d",&choice);
while(choice>=0&&choice<=2)
{
fp=fopen("aa.dat","rb");
for(i=0;i<N;i++)
fread(&a[i],sizeof(ST),1,fp); /*把文件中儲(chǔ)存的信息逐個(gè)讀到數(shù)組中去*/
fclose(fp);
(*process[choice])(a); /*前面提到的指向函數(shù)的指針,選擇操作*/
printf("\n");
Show();
printf("\n?");
scanf("%d",&choice);
}
}
void Show()
{
printf("\n****Choices:****\n0.Display the data form\n1.Bubble it according to the total score\n2.Search\n3.Quit!\n");
}
void Output(ST *a) /*將文件中存儲(chǔ)的信息逐個(gè)輸出*/
{
int i,t=0;
printf("Name Chinese Maths Physics Total\n");
for(i=0;i<N;i++)
{
t=a[i].chinese+a[i].maths+a[i].phy;
a[i].total=t;
printf("%4s%8d%8d%8d%8d\n",a[i].name,a[i].chinese,a[i].maths,a[i].phy,a[i].total);
}
}
void Bubble(ST *a) /*對(duì)數(shù)組進(jìn)行排序,并輸出結(jié)果*/
{
int i,pass;
ST m;
for(pass=0;pass<N-1;pass++)
for(i=0;i<N-1;i++)
if(a[i].total<a[i+1].total)
{
m=a[i]; /*結(jié)構(gòu)互換*/
a[i]=a[i+1];
a[i+1]=m;
}
Output(a);
}
void Find(ST *a)
{
int i,t=1;
char m[20];
printf("\nEnter the name you want:");
scanf("%s",m);
for(i=0;i<N;i++)
if(!strcmp(m,a[i].name)) /*根據(jù)姓名匹配情況輸出查找結(jié)果*/
{
printf("\nThe result is:\n%s, Chinese:%d, Maths:%d, Physics:%d,Total:%d\n",m,a[i].chinese,a[i].maths,a[i].phy,a[i].total);
t=0;
}
if(t)
printf("\nThe name is not in the list!\n");
}
鏈表:
鏈表是C語言中另外一個(gè)難點(diǎn)。牽扯到結(jié)點(diǎn)、動(dòng)態(tài)分配空間等等。用結(jié)構(gòu)作為鏈表的結(jié)點(diǎn)是非常適合的,例如:
struct node
{
int data;
struct node *next;
};
其中next是指向自身所在結(jié)構(gòu)類型的指針,這樣就可以把一個(gè)個(gè)結(jié)點(diǎn)相連,構(gòu)成鏈表。
鏈表結(jié)構(gòu)的一大優(yōu)勢(shì)就是動(dòng)態(tài)分配存儲(chǔ),不會(huì)像數(shù)組一樣必須在定義時(shí)確定大小,造成不必要的浪費(fèi)。用malloc和free函數(shù)即可實(shí)現(xiàn)開辟和釋放存儲(chǔ)單元。其中,malloc的參數(shù)多用sizeof運(yùn)算符計(jì)算得到。
鏈表的基本操作有:正、反向建立鏈表;輸出鏈表;刪除鏈表中結(jié)點(diǎn);在鏈表中插入結(jié)點(diǎn)等等,都是要熟練掌握的,初學(xué)者通過畫圖的方式能比較形象地理解建立、插入等實(shí)現(xiàn)的過程。
typedef struct node
{
char data;
struct node *next;
}NODE; /*結(jié)點(diǎn)*/
正向建立鏈表:
NODE *create()
{
char ch='a';
NODE *p,*h=NULL,*q=NULL;
while(ch<'z')
{
p=(NODE *)malloc(sizeof(NODE)); /*強(qiáng)制類型轉(zhuǎn)換為指針*/
p->data=ch;
if(h==NULL) h=p;
else q->next=p;
ch++;
q=p;
}
q->next=NULL; /*鏈表結(jié)束*/
return h;
}
逆向建立:
NODE *create()
{
char ch='a';
NODE *p,*h=NULL;
while(ch<='z')
{
p=(NODE *)malloc(sizeof(NODE));
p->data=ch;
p->next=h; /*不斷地把head往前挪*/
h=p;
ch++;
}
return h;
}
用遞歸實(shí)現(xiàn)鏈表逆序輸出:
void output(NODE *h)
{
if(h!=NULL)
{
output(h->next);
printf("%c",h->data);
}
}
插入結(jié)點(diǎn)(已有升序的鏈表):
NODE *insert(NODE *h,int x)
{
NODE *new,*front,*current=h;
while(current!=NULL&&(current->data<x)) /*查找插入的位置*/
{
front=current;
current=current->next;
}
new=(NODE *)malloc(sizeof(NODE));
new->data=x;
new->next=current;
if(current==h) /*判斷是否是要插在表頭*/
h=new;
else front->next=new;
return h;
}
刪除結(jié)點(diǎn):
NODE *delete(NODE *h,int x)
{
NODE *q,*p=h;
while(p!=NULL&&(p->data!=x))
{
q=p;
p=p->next;
}
if(p->data==x) /*找到了要?jiǎng)h的結(jié)點(diǎn)*/
{
if(p==h) /*判斷是否要?jiǎng)h表頭*/
h=h->next;
else q->next=p->next;
free(p); /*釋放掉已刪掉的結(jié)點(diǎn)*/
}
return h;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -