100行c語言實例,c語言100行代碼

本文目錄一覽:

c語言100行簡單一點的代碼

登錄幼兒園200個小朋友的數據:姓名、性別、年齡、身高、體重、出生日期,分別按年齡排序後輸出。

#includestdio.h

#define N 200

struct child

{

char name[10];

char sex[3];

int age;

int height;

float weight;

struct {

int year;

int month;

int day;

}bdate;

}ch[N];

void input()

{

int i;

for(i=0;iN;i++)

{

printf(“\n請輸入第%d名小朋友信息:\n”,i+1);

printf(“姓名:”);

scanf(“%s”,ch[i].name);

printf(“性別:”);

scanf(“%s”,ch[i].sex);

printf(“年齡:”);

scanf(“%d”,ch[i].age);

printf(“身高:”);

scanf(“%d”,ch[i].height);

printf(“體重:”);

scanf(“%f”,ch[i].weight);

printf(“出生日期[YYYY-MM-DD]:”);

scanf(“%d-%d-%d”,ch[i].bdate.year,ch[i].bdate.month,ch[i].bdate.day);

}

}

void sort()

{

struct child ct;

int i,j;

for(i=0;iN-1;i++)

for(j=0;jN-i-1;j++)

if(ch[j].heightch[j+1].height)

{

ct=ch[j];

ch[j]=ch[j+1];

ch[j+1]=ct;

}

}

void output()

{

int i;

printf(“\n\t幼兒園小朋友一覽(依身高排序)\n”);

printf(“===================================================\n”);

printf(” 姓名 性別 年齡 身高 體重 出生日期 \n”);

printf(“===================================================\n”);

for(i=0;iN;i++)

printf(” %-8s %-2s %2d %d %3.1f %d.%d.%d\n”,ch[i].name,ch[i].sex,ch[i].age,ch[i].height,ch[i].weight,ch[i].bdate.year,ch[i].bdate.month,ch[i].bdate.day);

}

void main()

{

input();

sort();

output();

}

求一個簡單100行c語言程序,一定要原創

答案voidmain(){intsele=1,t;floatx;system(“cls”);printf(“歡迎使用簡易菜單!本菜單在VC++平台編譯通過\n”);printf(“有何建議請聯繫本人!\n”);printf(“成績管理菜單\n”);printf(“\n”);printf(“1.輸入成績2.計算總分3.求平均值4.輸出總分與平均5.清理屏幕6.高低排列7.上平均分人數0.退出8.全部情況:總分平均分第一名及格人數”);scanf(“%d”,sele);puts(“”);if(sele=0sele1.輸入成績2.計算總分3.求平均值4.輸出總分與平均5.清理屏幕6.高低排列7.上平均分人數0.退出8.全部情況:總分平均分第一名及格人數\n”);break;case6:gaodi(a);break;case7:super(a);break;case8:full(t,x);break;}elseprintf(“你的輸入有誤,請重新:”);}

C語言100行的程序,要自己寫的 謝謝. 所有分全給了.

這是我寫的貪吃蛇,樓主看行不行?不行換一個

#includestdio.h

#includeconio.h

#includetime.h

#includewindows.h

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;ilength-1;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;ilength;i++)//判斷產生的食物是否在蛇身上,在flag=1,否則為0

if(food[0]==line[i][0] food[1]==line[i][1])

{ 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;ilength-1;i++)//判斷是否自己咬到自己

if( head[0]==line[i][0] head[1]==line[i][1])

{

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(lengthmax)

max=length;

update_score();

if(delay100)

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環境下運行的

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整除:”;

cinn;

if(n%4 != 0 )cout”笨蛋,看清楚題目!\n”endl;

else if(n64)coutn”! 這麼大, 想累死我啊, 不給算了!\n”endl;

else break;

}

//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++)

{

couta[i][j]”\t”;

sum+=a[i][j];

}

cout” | “sum”\n\n”;

}

//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];

coutsum”\t”;

}

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; bxMaxBlock/2; bx++)

for(int by=0; byMaxBlock; 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]);

}

}

}

else

{

for(int bx = 0; bxMaxBlock/2; bx++)

{

for(int by=0; byMaxBlock; 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; byMaxBlock; 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]);

}

}

}

}

跪求100行左右的c語言簡單代碼,大一水平就行,什麼類型都可以。

//學生成績管理系統C代碼

/*頭文件*/

#include stdio.h

#includedos.h

#includestdlib.h /*其它說明*/

#includestring.h /*字符串函數*/

#includemem.h /*內存操作函數*/

#includectype.h /*字符操作函數*/

#includealloc.h /*動態地址分配函數*/

#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(n1||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;i3;i++) /*3門課程循環3次*/

 {

 do{

 printf(“score%d:”,i+1);

 scanf(“%d”,p-score[i]);

 if(p-score[i]0 || p-score[i]100) /*確保成績在0~100之間*/

 printf(“Data error,please enter again.\n”);

 }while(p-score[i]0 || p-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(“| %3d | %4s | %-4s | %3d | %3d | %3d | %3d | %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 | %3d | %3d | %3d | %3d | %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 | %3d | %3d | %3d | %3d | %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-averagep1-averagep1!=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;i3;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-averagep1-average)(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);

}

求C語言編程實例100行以上,要有文字大概解釋一下

#include stdio.h

main(int a,char **date)

{

int year=0,month=0,day=0,week;

int d,i,dm,dy,m2;

char WEEK[9];

if (a==1)

{

printf (“\n ERROR! you forgot to enter the date you want to view\n”);

exit (0);

}

i=0; d=-1;

while (date[1][i])/*遍歷傳入的參數日期,計算出year,month,day*/

{

if ((date[1][i]==’/’||date[1][i]==’.’)d==-1) { d=0; i++; continue; }

if ((date[1][i]==’/’||date[1][i]==’.’)d==0) { d=1; i++; continue; }

if (d==-1) year=year*10+(date[1][i]-‘0’);

if (d==0) month=month*10+(date[1][i]-‘0’);

if (d==1) day=day*10+(date[1][i]-‘0’);

i++;

}

if (month1||month12)/*若月份傳入錯誤數字*/

{

printf (“\n ERROR! the entered MONTH is invalid\n”);

exit (0);

}

if (year==2000)

{

dy=0; /*年引起的星期差為0個*/

m2=1; /*2月引起的星期差為1個*/

goto la_100;

}

if (year2000)

d=(year-1-2000)/4-(year-1-2000)/100+(year-1-2000)/400+1;

else

d=(year-2000)/4-(year-2000)/100+(year-2000)/400;

dy=(year-2000)+d; /*** 該年 1月1號 到2000年1月1號的 ” 星期差 ” ***/

if((year%4==0year%100!=0)||(year%100==0year%400==0))

m2=1;

else

m2=0; /*** 該年是否潤 ***/

la_100: /**** la_100 ****/

/*** 該月以前的月所引起的 ” 星期差 ” ***/

switch (month)

{

case 1: dm=0; month=31; break; /*** month 在此存放該月天數 ***/

case 2: dm=3; month=d==1? 29:28; break;

case 3: dm=3+m2; month=31; break;

case 4: dm=6+m2; month=30; break;

case 5: dm=1+m2; month=31; break;

case 6: dm=4+m2; month=30; break;

case 7: dm=6+m2; month=31; break;

case 8: dm=2+m2; month=31; break;

case 9: dm=5+m2; month=30; break;

case 10: dm=m2; month=31; break;

case 11: dm=3+m2; month=30; break;

case 12: dm=5+m2; month=31; break;

}

if (day0||daymonth)

{

printf (“\n ERROR! the entered DAY is invalid\n”);

exit (0);

}

week=(dy+dm+day-1+6)%7;

if(week0)

week+=7;

if (day0) /*** 判定查看類型 ***/

{

switch (week)

{

case 0: strcpy (WEEK,”SUNDAY”); break;

case 1: strcpy (WEEK,”MONDAY”); break;

case 2: strcpy (WEEK,”TUESDAY”); break;

case 3: strcpy (WEEK,”WEDNESDAY”); break;

case 4: strcpy (WEEK,”THURSDAY”); break;

case 5: strcpy (WEEK,”FRIDAY”); break;

case 6: strcpy (WEEK,”SATURDAY”); break;

}

printf (“\n this day is %s \( %d \)\n\n OK!\n”,WEEK,week);

}

else

{

week=++week%7;

printf (“\n the calender of this month as following\n”);

printf (“\n *********************************\n”);

printf (” SUN MON TUE WEN THU FRI STA\n”);

for (i=0;iweek;i++)

printf (” “);

for (i=1;i=month;i++)

{

printf (” %2d “,i);

week++;

if (week%7==0i!=month)

printf (“\n”);

}

printf (“\n *********************************\n”);

printf (“\n OK!\n”);

}

}

這是一個萬年曆的小程序,效率不怎麼高,不過涉及到很多基礎知識,可以讀讀

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/150705.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-11-09 02:13
下一篇 2024-11-09 02:13

相關推薦

  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • Python字符串寬度不限制怎麼打代碼

    本文將為大家詳細介紹Python字符串寬度不限制時如何打代碼的幾個方面。 一、保持代碼風格的統一 在Python字符串寬度不限制的情況下,我們可以寫出很長很長的一行代碼。但是,為了…

    編程 2025-04-29
  • Python基礎代碼用法介紹

    本文將從多個方面對Python基礎代碼進行解析和詳細闡述,力求讓讀者深刻理解Python基礎代碼。通過本文的學習,相信大家對Python的學習和應用會更加輕鬆和高效。 一、變量和數…

    編程 2025-04-29
  • AES加密解密算法的C語言實現

    AES(Advanced Encryption Standard)是一種對稱加密算法,可用於對數據進行加密和解密。在本篇文章中,我們將介紹C語言中如何實現AES算法,並對實現過程進…

    編程 2025-04-29
  • Python生成隨機數的應用和實例

    本文將向您介紹如何使用Python生成50個60到100之間的隨機數,並將列舉使用隨機數的幾個實際應用場景。 一、生成隨機數的代碼示例 import random # 生成50個6…

    編程 2025-04-29
  • 倉庫管理系統代碼設計Python

    這篇文章將詳細探討如何設計一個基於Python的倉庫管理系統。 一、基本需求 在着手設計之前,我們首先需要確定倉庫管理系統的基本需求。 我們可以將需求分為以下幾個方面: 1、庫存管…

    編程 2025-04-29
  • 學習Python對學習C語言有幫助嗎?

    Python和C語言是兩種非常受歡迎的編程語言,在程序開發中都扮演着非常重要的角色。那麼,學習Python對學習C語言有幫助嗎?答案是肯定的。在本文中,我們將從多個角度探討Pyth…

    編程 2025-04-29
  • Python滿天星代碼:讓編程變得更加簡單

    本文將從多個方面詳細闡述Python滿天星代碼,為大家介紹它的優點以及如何在編程中使用。無論是剛剛接觸編程還是資深程序員,都能從中獲得一定的收穫。 一、簡介 Python滿天星代碼…

    編程 2025-04-29
  • 寫代碼新手教程

    本文將從語言選擇、學習方法、編碼規範以及常見問題解答等多個方面,為編程新手提供實用、簡明的教程。 一、語言選擇 作為編程新手,選擇一門編程語言是很關鍵的一步。以下是幾個有代表性的編…

    編程 2025-04-29
  • Python實現簡易心形代碼

    在這個文章中,我們將會介紹如何用Python語言編寫一個非常簡單的代碼來生成一個心形圖案。我們將會從安裝Python開始介紹,逐步深入了解如何實現這一任務。 一、安裝Python …

    編程 2025-04-29

發表回復

登錄後才能評論