100行的c語言程序-ag真人国际官网
① c語言例子 100行以上
#include "stdafx.h"
#include "iostream.h"
#define m 2000
#define n 8
void magic(int a[m][m], int);
int main(int argc, char* argv[])
{
static int a[m][m];
int d = 12;
int n = 8;
while(1){
while(1)
{
cout<<"請輸入方陣的階數, 階數必須能被4整除:";
cin>>n;
if(n%4 != 0 )cout<<"笨蛋,看清楚題目!\n"<
}
//init
for(int i=1; i<=n; i )
{
for(int j=1; j<=n; j )
{
a[i][j]=d;
d ;
// d ;
}
}
magic(a, n);
//print dimension and sum for rows
for( i=1; i<=n; i )
{
int sum=0;
for(int j=1; j<=n; j )
{
cout< sum =a[i][j];
}
cout<<" | "<
//print sum of columns
for(i = 1; i<=n; i )cout<<"--\t";
cout<<"\n";
for(i=1; i<=n; i )
{
int sum = 0;
for(int j = 1; j<=n; j ) sum = a[j][i];
cout<
cout<<"\n\n";
char c;
cout<<"continue?(y/n)";
cin>> c;
if(c=='n'|| c=='n')break;
}
return 0;
}
void exchg(int &a, int &b)
{
int t;
t = a;
a = b;
b = t;
}
void magic(int a[m][m], int n) // a:= 矩陣 n:= 實際階數
{
int baseblock_x=0;
int baseblock_y=0;
int maxblock = n/4;
if(maxblock%2==0)
{
for(int bx = 0; bx
for(int c = 1; c <= 4; c )
{
exchg(a[bx*4 c][by*4 c], a[n 1-bx*4-c][n 1-by*4-c]);
exchg(a[bx*4 c][by*4 5-c], a[n 1-bx*4-c][n 1-by*4-5 c]);
}
}
}
else
{
for(int bx = 0; bx
for(int by=0; by
for(int c = 1; c <= 4; c )
{
exchg(a[bx*4 c][by*4 c], a[n 1-bx*4-c][n 1-by*4-c]);
exchg(a[bx*4 c][by*4 5-c], a[n 1-bx*4-c][n 1-by*4-5 c]);
}
}
}
bx = maxblock/2;
for(int by=0; by
for(int c = 1; c <= 2; c )
{
exchg(a[bx*4 c][by*4 c], a[n 1-bx*4-c][n 1-by*4-c]);
exchg(a[bx*4 c][by*4 5-c], a[n 1-bx*4-c][n-by*4-4 c]);
}
}
}
}
② c語言100行的程序,要自己寫的 謝謝. 所有分全給了.
這是我寫的貪吃蛇,樓主看行不行?不行換一個
#include
#include
#include
#include
int length=1;//蛇的當前長度,初始值為1
int line[100][2];//蛇的走的路線
int head[2]={40,12};//蛇頭
int food[2];//食物的位置
char direction;//蛇運動方向
int x_min=1;x_max=77; y_min=2; y_max=23;//設置蛇的運動區域
int tail_before[2]={40,12};//上一個狀態的蛇尾
char direction_before='s';//上一個狀態蛇的運動方向
int live_death=1;//死活狀態,0死,1活
int eat_flag=0;//吃食物與否的狀態。0沒吃 1吃了
int max=0;
int delay;//移動延遲時間
void gotoxy(int x, int y)//x為列坐標,y為行坐標
{
coord pos = {x,y};
handle hout = getstdhandle(std_output_handle);
setconsolecursorposition(hout, pos);
}
void hidden()//隱藏游標
{
handle hout = getstdhandle(std_output_handle);
console_cursor_info cci;
getconsolecursorinfo(hout,&cci);
cci.bvisible=0;//賦1為顯示,賦0為隱藏
setconsolecursorinfo(hout,&cci);
}
void update_score()
{
gotoxy(2,1);
printf("我的分數:%d",length);
gotoxy(42,1);
printf("最高記錄:%d",max);
}
void create_window()
{
gotoxy(0,0);
printf("╔══════════════════╦═══════════════════╗");
printf("║ ║ ║");
printf("╠══════════════════╩═══════════════════╣");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("╚══════════════════════════════════════╝");
}
void update_line()
{
int i;
if(eat_flag==0)//吃了食物就不用記住上一個狀態的蛇尾,否則會被消掉
{
tail_before[0]=line[0][0];//記住上一個狀態的蛇尾
tail_before[1]=line[0][1];
for(i=0;i
line[i][0]=line[i 1][0];
line[i][1]=line[i 1][1];
}
line[length-1][0]=head[0];//更新蛇頭
line[length-1][1]=head[1];
}
}
void initial()
{
file *fp;
gotoxy(head[0],head[1]);
printf("蛇");
line[0][0]=head[0];//把蛇頭裝入路線
line[0][1]=head[1];
if((fp=fopen("highest","r"))==null)
{
fp=fopen("highest","w");
fprintf(fp,"%d",0);
max=0;
fclose(fp);
}//第一次使用時,初始化獎最高分為0
else
{
fp=fopen("highest","r");
fscanf(fp,"%d",&max);
}
update_score();
}
void createfood()
{
int flag,i;
srand((unsigned)time(null));
for(;;)
{
for(;;)
{
food[0]=rand()%(x_max 1);
if(food[0]%2==0 && food[0]>x_min)
break;
}//產生一個偶數橫坐標
for(;;)
{
food[1]=rand()%(y_max);
if(food[1]>y_min)
break;
}
for(i=0,flag=0;i
{ flag=1; break; }
if(flag==0)// 食物不在蛇身上 結束循環
break;
}
gotoxy(food[0],food[1]);
printf("蛇");
}
void show_snake()
{
gotoxy(head[0],head[1]);
printf("蛇");
if(eat_flag==0)//沒吃食物時消去蛇尾
{
gotoxy(tail_before[0],tail_before[1]);
printf(" ");//消除蛇尾
}
else
eat_flag=0;//吃了食物就回到沒吃狀態
}
char different_direction(char dir)
{
switch(dir)
{
case 'a': return 'd';
case 'd': return 'a';
case 'w': return 's';
case 's': return 'w';
}
}
void get_direction()
{
direction_before=direction;//記住蛇上一個狀態的運動方向
while(kbhit()!=0) //調試
direction=getch();
if( direction_before == different_direction(direction) || (direction!='a' && direction!='s' && direction!='d' && direction!='w') ) //新方向和原方向相反,或獲得的方向不是wasd時,保持原方向
direction=direction_before;
switch(direction)
{
case 'a': head[0]-=2; break;
case 'd': head[0] =2; break;
case 'w': head[1]--; break;
case 's': head[1] ; break;
}
}
void live_state()//判斷蛇的生存狀態
{
file *fp;
int i,flag;
for(i=0,flag=0;i
{
flag=1;
break;
}
if(head[0]<=x_min || head[0]>=x_max || head[1]<=y_min || head[1]>=y_max || flag==1)
{
system("cls");
create_window();
update_score();
gotoxy(35,12);
printf("游戲結束!\n");
sleep(500);
live_death=0;
fp=fopen("highest","w");
fprintf(fp,"%d",max);//保存最高分
}
}
void eat()
{
if(head[0]==food[0]&&head[1]==food[1])
{
length ;
line[length-1][0]=head[0];//更新蛇頭
line[length-1][1]=head[1];
eat_flag=1;
createfood();
if(length>max)
max=length;
update_score();
if(delay>100)
delay-=30;//加速
}
}
main()
{
int x=0,y=0;
int i;
hidden();//隱藏游標
create_window();
initial();
createfood();
for(direction='s',delay=600;;)
{
get_direction();
eat();
update_line();
live_state();//判斷生死狀態
if(live_death==1)
{
show_snake();
}
else
break;
sleep(delay);
}
}
這是在vc6.0環境下運行的,如果你用的tc,我可以幫你改成tc環境下運行的
③ 求100行左右的代碼(c語言,c ,數據結構編寫的均可)
#include
#include
#include
#define n 10
struct library {
int num;
char book_name[30];
char writer[30];
char sort_num[3];
char pub_company[30];
char pub_time[30];
char prise[30];
};
typedef struct library lib; /*結構體的定義用於存放書籍及借書的信息*/
lib lib[n];
file *fp; int all=0;
int menu(void);
void input(void);
void output(void);
void save(void);
void del(void);
void search(void);
void xiugai(void);
main()
{
for(;;)
{
switch(menu()) {
case 1:input();break;
case 2:output();break;
case 3:save();break;
case 4:search();break;
case 5:xiugai();break;
case 6:del();break;
case 7:sort();break;
case 0:exit(1);break;
} /*switch定義函數輸出*/
}
}
int menu(void)
{
char m[3];
int n; printf(" *********************welcome**********************\n\n\n\n\n");
printf("\t\t\t-----圖書信息管理系統----\n");
printf("\t\t1:輸入\n");
printf("\t\t2:輸出\n");
printf("\t\t3:保存\n");
printf("\t\t4:查找\n");
printf("\t\t5:修改\n");
printf("\t\t6:刪除\n");
printf("\t\t7:統計\n");
printf("\t\t0:退出\n");
printf("\t\tplease choose a operation(0-6):\n");
scanf("%s",m);
n=atoi(m);
return(n);
} /*主要界面*/
void input(void)
{
int i;
char m[3];
for(i=all;i
all ;
printf("請輸入信息:\n");
printf("\t\t號碼:\n");
scanf("%d",&lib[i].num);
printf("\t\t書名:\n");
scanf("%s",lib[i].book_name);
printf("\t\t作者:\n");
scanf("%s",lib[i].writer);
printf("\t\tsort_num:\n");
scanf("%s",lib[i].sort_num);
printf("\t\t出版商:\n");
scanf("%s",lib[i].pub_company);
printf("\t\t出版時間:\n");
scanf("%s",lib[i].pub_time);
printf("\t\t價格:\n");
scanf("%s",lib[i].prise);
a: printf("\t\tyes/哦了?\n");
printf("\t\t1:yes\n");
printf("\t\t2:no\n");
scanf("%s",m);
if(atoi(m)==1)
continue;
else if(atoi(m)==2)
return;
else
{
printf("\t\t錯誤!\n");
goto a;
} /* 輸入條件查找*/
}
}
void output(void)
{
int i;
for(i=0;i
printf("\t\t%d\n",lib[i].num);
printf("%s\t\t%s\t\t%s\n",lib[i].book_name,lib[i].writer,lib[i].sort_num);
printf("%s\t\t%s\t\t%s\n",lib[i].pub_company,lib[i].pub_time,lib[i].prise);
}
} /*輸出書名作者出版時間價格等*/
void save(void)
{
int i;
if((fp=fopen("file.c","wb"))==null)
{
printf("can not open the file");
exit(1);
}
for(i=0;i
if(fwrite(&lib[i],sizeof(lib),1,fp)!=1)
{
printf("can not write!");
exit(1);
}
} /*條件不符合時拒絕存儲*/
fclose(fp);
}
void search(void)
{
int i,flag;
char m[3];
char name[30];
printf("\t\t請選擇您的存儲方式:\n");
printf("\t\t1:按書名!\n");
printf("\t\t2:按作者!\n");
scanf("%s",m);
i=atoi(m);
switch(i)
{
case 1:{
printf("\t\t請輸入書名:\n");
scanf("%s",name);
flag=0;
for(i=0;i
if(strcmp(name,lib[i].book_name)==0)
{
printf("\t\t%d\n",lib[i].num);
printf("%s\t\t%s\t\t%s\n",lib[i].book_name,lib[i].writer,lib[i].sort_num);
printf("%s\t\t%s\t\t%s\n",lib[i].pub_company,lib[i].pub_time,lib[i].prise);
flag=1;break;
}
}
if(flag==0)
printf("\t\t沒有這本書!\n");
}
case 2:{
printf("\t\t請輸入作者:\n");
scanf("%s",name);
flag=0;
for(i=0;i
if(strcmp(name,lib[i].writer)==0)
{
printf("\t\t%d\n",lib[i].num);
printf("%s\t\t%s\t\t%s\n",lib[i].book_name,lib[i].writer,lib[i].sort_num);
printf("%s\t\t%s\t\t%s\n",lib[i].pub_company,lib[i].pub_time,lib[i].prise);
flag=1;break;
}
}
if(flag==0)
printf("\t\t沒有這個作者!\n");
}
} /*查找圖書按書名或作者並輸出*/
}
void xiugai(void)
{
int i,flag;
char name[30],n[3];
printf("\t\t請輸入要修改的書名 :\n");
scanf("%s",name); /*修改書名*/
flag=0;
for(i=0;i
if(strcmp(name,lib[i].book_name)==0)
{
printf("\t\t%d\n",lib[i].num);
printf("%s\t\t%s\t\t%s\n",lib[i].book_name,lib[i].writer,lib[i].sort_num);
printf("%s\t\t%s\t\t%s\n",lib[i].pub_company,lib[i].pub_time,lib[i].prise);
printf("\t\tplease input xiugai's the informations:\n");
printf("\t\tnum:\n");
scanf("%d",&lib[i].num);
printf("\t\tbook_name:\n");
scanf("%s",lib[i].book_name);
printf("\t\twriter:\n");
scanf("%s",lib[i].writer);
printf("\t\tsort_num:\n");
scanf("%s",lib[i].sort_num);
printf("\t\tpub_company:\n");
scanf("%s",lib[i].pub_company);
printf("\t\tpub_time:\n");
scanf("%s",lib[i].pub_time);
printf("\t\tprise:\n");
scanf("%s",lib[i].prise);
flag=1;break;
}
} /*增加圖書*/
if(flag==0)
printf("\t\t沒有找到啊!\n");
}
void del(void)
{
int i,j,flag;
char name[30];
printf("\t\t請輸入要刪除的書名:\n");
scanf("%s",name);
flag=0;
for(i=0;i
if(strcmp(name,lib[i].book_name)==0)
{
printf("\t\t%d\n",lib[i].num);
printf("%s\t\t%s\t\t%s\n",lib[i].book_name,lib[i].writer,lib[i].sort_num);
printf("%s\t\t%s\t\t%s\n",lib[i].pub_company,lib[i].pub_time,lib[i].prise);
for(j=n;j>i;j--)
{
lib[j-1].num=lib[j].num;
strcpy(lib[j-1].book_name,lib[j].book_name);
strcpy(lib[j-1].writer,lib[j].writer);
strcpy(lib[j-1].sort_num,lib[j].sort_num);
strcpy(lib[j-1].pub_company,lib[j].pub_company);
strcpy(lib[j-1].pub_time,lib[j].pub_time);
strcpy(lib[j-1].prise,lib[j].prise);
flag=1;
printf("\t\t已經刪除!\n");
break;
}
}
}
if(flag==0)
printf("\t\t沒有這本書!\n");
} /*刪除圖書*/
④ c語言程序100行
//學生成績管理系統c代碼
/*頭文件*/
#include
#include
#include /*其它說明*/
#include /*字元串函數*/
#include /*內存操作函數*/
#include /*字元操作函數*/
#include /*動態地址分配函數*/
#define len sizeof(student)
typedef struct stu /*定義結構體數組用於緩存數據*/
{
char num[6];
char name[5];
int score[3];
int sum;
float average;
int order;
struct stu *next;
}student;
/*函數原型*/
student *init(); /*初始化函數*/
int menu_select(); /*菜單函數*/
student *create(); /*創建鏈表*/
void print(student *head); /* 顯示全部記錄*/
void search(student *head); /*查找記錄*/
student *delete(student *head); /*刪除記錄*/
student *sort(student *head); /*排序*/
student *insert(student *head,student *newnode); /*插入記錄*/
void save(student *head); /*保存文件*/
student *load(); /*讀文件*/
/*主函數界面*/
main()
{
student *head,newnode;
head=init(); /*鏈表初始化,使head的值為null*/
for(;;) /*循環無限次*/
{
switch(menu_select())
{
case 1:head=create();break;
case 2:print(head);break;
case 3:search(head);break;
case 4:head=delete(head);break;
case 5:head=sort(head);break;
case 6:head=insert(head,&newnode);break; /*&newnode表示返回地址*/
case 7:save(head);break;
case 8:head=load(); break;
case 9:exit(0); /*如菜單返回值為9則程序結束*/
}
}
}
/*初始化函數*/
student *init()
{
return null; /*返回空指針*/
}
/*菜單選擇函數*/
menu_select()
{
int n;
struct date d; /*定義時間結構體*/
getdate(&d); /*讀取系統日期並把它放到結構體d中*/
printf("press any key to enter the menu......"); /*按任一鍵進入主菜單*/
getch(); /*從鍵盤讀取一個字元,但不顯示於屏幕*/
clrscr(); /*清屏*/
printf("********************************************************************************\n");
printf("\t\t welcome to\n");
printf("\n\t\t the student score manage system\n");
printf("*************************************menu***************************************\n");
printf("\t\t\t1. enter the record\n"); /*輸入學生成績記錄*/
printf("\t\t\t2. print the record\n"); /*顯示*/
printf("\t\t\t3. search record on name\n"); /*尋找*/
printf("\t\t\t4. delete a record\n"); /*刪除*/
printf("\t\t\t5. sort to make new a file\n"); /*排序*/
printf("\t\t\t6. insert record to list\n"); /*插入*/
printf("\t\t\t7. save the file\n"); /*保存*/
printf("\t\t\t8. load the file\n"); /*讀取*/
printf("\t\t\t9. quit\n"); /*退出*/
printf("\n\t\t made by hu haihong.\n");
printf("********************************************************************************\n");
printf("\t\t\t\t%d\\%d\\%d\n",d.da_year,d.da_mon,d.da_day); /*顯示當前系統日期*/
do{
printf("\n\t\t\tenter your choice(1~9):");
scanf("%d",&n);
}while(n9); /*如果選擇項不在1~9之間則重輸*/
return(n); /*返回選擇項,主函數根據該數調用相應的函數*/
}
/*輸入函數*/
student *create()
{
int i,s;
student *head=null,*p; /* 定義函數.此函數帶回一個指向鏈表頭的指針*/
clrscr();
for(;;)
{p=(student *)malloc(len); /*開辟一個新的單元*/
if(!p) /*如果指針p為空*/
{printf("\nout of memory."); /*輸出內存溢出*/
return (head); /*返回頭指針,下同*/
}
printf("enter the num(0:list end):");
scanf("%s",p->num);
if(p->num[0]=='0') break; /*如果學號首字元為0則結束輸入*/
printf("enter the name:");
scanf("%s",p->name);
printf("please enter the %d scores\n",3); /*提示開始輸入成績*/
s=0; /*計算每個學生的總分,初值為0*/
for(i=0;i<3;i ) /*3門課程循環3次*/
{
do{
printf("score%d:",i 1);
scanf("%d",&p->score[i]);
if(p->score[i]score[i]>100) /*確保成績在0~100之間*/
printf("data error,please enter again.\n");
}while(p->score[i]score[i]>100);
s=s p->score[i]; /*累加各門成績*/
}
p->sum=s; /*將總分保存*/
p->average=(float)s/3; /*先用強制類型轉換將s轉換成float型,再求平均值*/
p->order=0; /*未排序前此值為0*/
p->next=head; /*將頭結點做為新輸入結點的後繼結點*/
head=p; /*新輸入結點為新的頭結點*/
}
return(head);
}
/* 顯示全部記錄函數*/
void print(student *head)
{
int i=0; /* 統計記錄條數*/
student *p; /*移動指針*/
clrscr();
p=head; /*初值為頭指針*/
printf("\n************************************student************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("| rec | num | name | sc1 | sc2 | sc3 | sum | ave | order |\n");
printf("-------------------------------------------------------------------------------\n");
while(p!=null)
{
i ;
printf("| = | %4s | %-4s | = | = | = | = | %4.2f | %-5d|\n",
i, p->num,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order);
p=p->next;
}
printf("-------------------------------------------------------------------------------\n");
printf("**************************************end**************************************\n");
}
/*查找記錄函數*/
void search(student *head)
{
student *p; /* 移動指針*/
char s[5]; /*存放姓名用的字元數組*/
clrscr();
printf("please enter name for searching.\n");
scanf("%s",s);
p=head; /*將頭指針賦給p*/
while(strcmp(p->name,s) && p != null) /*當記錄的姓名不是要找的,或指針不為空時*/
p=p->next; /*移動指針,指向下一結點*/
if(p!=null) /*如果指針不為空*/
{printf("\n*************************************found************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("| num | name | sc1 | sc2 | sc3 | sum | ave | order |\n");
printf("-------------------------------------------------------------------------------\n");
printf("| %4s | %4s | = | = | = | = | %4.2f | %-5d|\n",
p->num,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order);
printf("-------------------------------------------------------------------------------\n");
printf("***************************************end**************************************\n");
}
else
printf("\nthere is no num %s student on the list.\n",s); /*顯示沒有該學生*/
}
/*刪除記錄函數*/
student *delete(student *head)
{int n;
student *p1,*p2; /*p1為查找到要刪除的結點指針,p2為其前驅指針*/
char c,s[6]; /*s[6]用來存放學號,c用來輸入字母*/
clrscr();
printf("please enter the deleted num: ");
scanf("%s",s);
p1=p2=head; /*給p1和p2賦初值頭指針*/
while(strcmp(p1->num,s) && p1 != null) /*當記錄的學號不是要找的,或指針不為空時*/
{p2=p1; /*將p1指針值賦給p2作為p1的前驅指針*/
p1=p1->next; /*將p1指針指向下一條記錄*/
}
if(strcmp(p1->num,s)==0) /*學號找到了*/
{printf("**************************************found************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("| num | name | sc1 | sc2 | sc3 | sum | ave | order |\n");
printf("-------------------------------------------------------------------------------\n");
printf("| %4s | %4s | = | = | = | = | %4.2f | %-5d|\n",
p1->num,p1->name,p1->score[0],p1->score[1],p1->score[2],p1->sum,p1->average,p1->order);
printf("-------------------------------------------------------------------------------\n");
printf("***************************************end**************************************\n");
printf("are you sure to delete the student y/n ?"); /*提示是否要刪除,輸入y刪除,n則退出*/
for(;;)
{scanf("%c",&c);
if(c=='n'||c=='n') break; /*如果不刪除,則跳出本循環*/
if(c=='y'||c=='y')
{
if(p1==head) /*若p1==head,說明被刪結點是首結點*/
head=p1->next; /*把第二個結點地址賦予head*/
else
p2->next=p1->next; /*否則將一下結點地址賦給前一結點地址*/
n=n-1;
printf("\nnum %s student have been deleted.\n",s);
printf("don't forget to save.\n");break; /*刪除後就跳出循環*/
}
}
}
else
printf("\nthere is no num %s student on the list.\n",s); /*找不到該結點*/
return(head);
}
/*排序函數*/
student *sort(student *head)
{int i=0; /*保存名次*/
student *p1,*p2,*t,*temp; /*定義臨時指針*/
temp=head->next; /*將原表的頭指針所指的下一個結點作頭指針*/
head->next=null; /*第一個結點為新表的頭結點*/
while(temp!=null) /*當原表不為空時,進行排序*/
{
t=temp; /*取原表的頭結點*/
temp=temp->next; /*原表頭結點指針後移*/
p1=head; /*設定移動指針p1,從頭指針開始*/
p2=head; /*設定移動指針p2做為p1的前驅,初值為頭指針*/
while(t->averageaverage&&p1!=null) /*作成績平均分比較*/
{
p2=p1; /*待排序點值小,則新表指針後移*/
p1=p1->next;
}
if(p1==p2) /*p1==p2,說明待排序點值大,應排在首位*/
{
t->next=p1; /*待排序點的後繼為p*/
head=t; /*新頭結點為待排序點*/
}
else /*待排序點應插入在中間某個位置p2和p1之間,如p為空則是尾部*/
{
t->next=p1; /*t的後繼是p1*/
p2->next=t; /*p2的後繼是t*/
}
}
p1=head; /*已排好序的頭指針賦給p1,准備填寫名次*/
while(p1!=null) /*當p1不為空時,進行下列操作*/
{
i ; /*結點序號*/
p1->order=i; /*將結點序號賦值給名次*/
p1=p1->next; /*指針後移*/
}
printf("sorting is sucessful.\n"); /*排序成功*/
return (head);
}
/*插入記錄函數*/
student *insert(student *head,student *newnode)
{student *p0,*p1,*p2;
int n,sum1,i;
p1=head; /*使p1指向第一個結點*/
p0=newnode; /*p0指向要插入的結點*/
printf("\nplease enter a newnode record.\n"); /*提示輸入記錄信息*/
printf("enter the num:");
scanf("%s",newnode->num);
printf("enter the name:");
scanf("%s",newnode->name);
printf("please enter the %d scores.\n",3);
sum1=0; /*保存新記錄的總分,初值為0*/
for(i=0;i<3;i )
{
do{
printf("score%d:",i 1);
scanf("%d",&newnode->score[i]);
if(newnode->score[i]>100||newnode->score[i]<0)
printf("data error,please enter again.\n");
}while(newnode->score[i]>100||newnode->score[i]<0);
sum1=sum1 newnode->score[i]; /*累加各門成績*/
}
newnode->sum=sum1; /*將總分存入新記錄中*/
newnode->average=(float)sum1/3;
newnode->order=0;
if(head==null) /*原來的鏈表是空表*/
{head=p0;p0->next=null;} /*使p0指向的結點作為頭結點*/
else
{while((p0->averageaverage)&&(p1->next!=null))
{p2=p1; /*使p2指向剛才p1指向的結點*/
p1=p1->next; /*p1後移一個結點*/
}
if(p0->average>=p1->average)
{if(head==p1)head=p0; /*插到原來第一個結點之前*/
else p2->next=p0; /*插到p2指向的結點之後*/
p0->next=p1;}
else
{p1->next=p0;p0->next=null;} /*插到最後的結點之後*/
}
n=n 1; /*結點數加1*/
head=sort(head); /*調用排序的函數,將學生成績重新排序*/
printf("\nstudent %s have been inserted.\n",newnode->name);
printf("don't forget to save the newnode file.\n");
return(head);
}
/*保存數據到文件函數*/
void save(student *head)
{file *fp; /*定義指向文件的指針*/
student *p; /* 定義移動指針*/
char outfile[10];
printf("enter outfile name,for example c:\\score\n");
scanf("%s",outfile);
if((fp=fopen(outfile,"wb"))==null) /*為輸出打開一個二進制文件,為只寫方式*/
{
printf("cannot open the file\n");
return; /*若打不開則返回菜單*/
}
printf("\nsaving the file......\n");
p=head; /*移動指針從頭指針開始*/
while(p!=null) /*如p不為空*/
{
fwrite(p,len,1,fp); /*寫入一條記錄*/
p=p->next; /*指針後移*/
}
fclose(fp); /*關閉文件*/
printf("save the file successfully!\n");
}
/* 從文件讀數據函數*/
student *load()
{student *p1,*p2,*head=null; /*定義記錄指針變數*/
file *fp; /* 定義指向文件的指針*/
char infile[10];
printf("enter infile name,for example c:\\score\n");
scanf("%s",infile);
if((fp=fopen(infile,"rb"))==null) /*打開一個二進制文件,為只讀方式*/
{
printf("can not open the file.\n");
return(head);
}
printf("\nloading the file!\n");
p1=(student *)malloc(len); /*開辟一個新單元*/
if(!p1)
{
printf("out of memory!\n");
return(head);
}
head=p1; /*申請到空間,將其作為頭指針*/
while(!feof(fp)) /*循環讀數據直到文件尾結束*/
{
if(fread(p1,len,1,fp)!=1) break; /*如果沒讀到數據,跳出循環*/
p1->next=(student *)malloc(len); /*為下一個結點開辟空間*/
if(!p1->next)
{
printf("out of memory!\n");
return (head);
}
p2=p1; /*使p2指向剛才p1指向的結點*/
p1=p1->next; /*指針後移,新讀入數據鏈到當前表尾*/
}
p2->next=null; /*最後一個結點的後繼指針為空*/
fclose(fp);
printf("you have success to read data from the file!\n");
return (head);
}
滿意請採納。