本文目錄一覽:
c語言 編程
#include stdio.h
/*
3、用一維數組解如下問題:讀取20個在10到100之間的不重複的整數。每讀取一個值時,如果它與已讀取的值
不重複,就打印該值。用儘可能小的數組解決這個問題。
*/
void noRepeat() {
int a[20];
int i;
int j;
int count = 0;
printf(“\n請輸入20個10-100的整數:\n”);
for (i=0; i20; i++) {
scanf(“%d”,a[count]);
if (a[count]10 || a[count]100) {
printf(“輸入錯誤!\n”);
i–;
continue;
}
for (j=0; jcount; j++) {
if (a[count] == a[j]) {
break;
}
}
if (j == count) {
printf(“[%d]\n”,a[count]);
count++;
}
}
}
/*
5、Eratoshenes篩選是一種尋找素數的方法,該方法表述如下:
1)創建一個一維數組,把所有的元素初始化為1(表示真),下標為素數的元素保持1不變,其它下標的元素
最終被置為0
2)從數組下標2出發,每次發現值為1的數組元素時,則看其後的所有元素,把下標是它們倍數的那些元素置
為0。例如,對下標2來說,凡是2的倍數的下標(4,6,8,10,···)都將其元素置為0,對下標3來說,
凡是否的倍數的下標(6,9,12,15,···)將其元素置為0。
當以上過程結束後,仍為1的數組元素的下標就是素數,將這些下標打印輸出即可。編寫程序,
用含有1000個元素的數組確定並打印出1~999之間的所有素數
*/
void Eratoshenes() {
int a[1000];
int i;
int j;
for (i=2; i1000; i++) {
a[i] = 1;
}
for (i=2; i1000; i++) {
if (a[i] == 1) {
for (j=2; j*i1000; j++) {
a[j*i] = 0;
}
}
}
for (i=2; i1000; i++) {
if (a[i] == 1) {
printf(“%d “,i);
}
}
}
/*
7、學生成績統計。某班共6名學生,學習6門功課(數學分析,高等代數,大學物理,計算導論,經濟學,英語),
每門功課有平時,期中和期末三項成績,按平時佔20%,期中佔30%,期末佔50%,求每人每門功課的平時成績和
各個人6門功課的平均成績及總平均成績,最後要求按各人平均成績的高低排序並打印成表格
*/
void func_average(int s[6][6][3], int a[6]){
int i;
int j;
float temp;
for (i=0; i6; i++) {
temp = 0;
for (j=0; j6; j++) {
temp += s[i][j][0] * 0.2f + s[i][j][1] * 0.3f + s[i][j][2] * 0.5f;
}
a[i] = (int)(temp / 6);
}
}
void func_score() {
int score[6][6][3] = {
{{10,10,10},{10,10,10},{10,10,10},{10,10,10},{10,10,10},{10,10,10}},
{{20,20,20},{20,20,20},{20,20,20},{20,20,20},{20,20,20},{20,20,20}},
{{30,30,30},{30,30,30},{30,30,30},{30,30,30},{30,30,30},{30,30,30}},
{{40,40,40},{40,40,40},{40,40,40},{40,40,40},{40,40,40},{40,40,40}},
{{50,50,50},{50,50,50},{50,50,50},{50,50,50},{50,50,50},{50,50,50}},
{{60,60,60},{60,60,60},{60,60,60},{60,60,60},{60,60,60},{60,60,60}}
};
char name[6][20] = {“abc”,”xyz”,”def”,”ghi”,”aaa”,”bbb”};
int average[6] = {0};
int index[6];
int i;
int j;
int temp;
func_average(score,average);
for (i=0; i6; i++) {
index[i] = 0;
for (j=0; j6; j++) {
if (average[index[i]]!=-1 average[j]=average[index[i]]) {
index[i] = j;
}
}
average[index[i]] = -1;
}
func_average(score,average);
for (i=0; i6; i++) {
printf(“%4s”,name[index[i]]);
for (j=0; j6; j++) {
temp = (int)(score[index[i]][j][0] * 0.2f +
score[index[i]][j][1] * 0.3f + score[index[i]][j][2] * 0.5f);
printf(“%3d”,temp);
}
printf(“%3d\n”,average[index[i]]);
}
}
/*
8、已知一個有限輸入字符集合?={a,b},寫一個程序能夠識別集合L={anbn:0≤n≤N}。
說明:該問題實質上是判定輸入字符串是否呈現aa ··· abb ··· b (a,b均為n個) 。
設字符串string有c個字符,則c=2n,且string[1]=··· =string[n]=a, string[n+1]= ···
=string[2n]=b (或進一步有這樣的關係string[1],string[2]··· ,string[n]={a}, string[n+1],
string[n+2],··· ,string[2n]={b})。
*/
void f(char *str) {
int i = 0;
int j = 0;
char a;
char b;
char *p;
a = str[0];
for (i=0,p=str; *p!=0; p++,i++) {
if (a != *p) {
b = *p;
break;
}
}
for (j=0; *p!=0; p++,j++) {
if (b != *p) {
printf(“不屬於集合!\n”);
return;
}
}
if (i == j) {
printf(“屬於集合!\n”);
} else {
printf(“不屬於集合!\n”);
}
}
void main() {
// Eratoshenes();
// noRepeat();
// func_score();
f(“cccdddcc”);
}
程序在vc6.0下調試通過.
C語言程序編程
1.編寫一個程序它的功能是;打印出1到1000之內的能被7或11整除,但不能同時被7和11整除的所有數
#includestdio.h
void main(void)
{
int i,j;
for(i=1,j=0;i1000;i++)
{
if((!(i%7)||!(i%11))(i%77))
{
printf(“%d\t”,i);j++;
if(!(j%8)) printf(“\n”);
}
}
}
2.編寫一個程序其功能是;將兩個數位的整數放在C中併合並的方式是;將A的十位和個位依次放在C數的百位和個位,例如;當A=45,B=15
得到的結果是C=4515
你的第2題表達不清,我按照自己的理解給你寫了一個,如果不行你要把題目的意思說清楚我才能做。
#includestdio.h
void main(void)
{
int a,b,c,flag;
do{
flag=0;
printf(“請輸入a,b(兩位整數):”);
scanf(“%d%d”,a,b);
if(a0||b0||a100||b100)
{
flag=1;
printf(“對不起,你輸入錯誤,請重新輸入.\n”);
}
}while(flag==1);
c=a*100+b;
printf(“c=%d\n”,c);
}
3.編寫一個程序其功能是;計算並輸出下列多項式值;
Sn=1+1/1!+1/2!+1/3!+1/4!+……..+1/n!如主函數鍵盤給n輸入15輸出為S=2.718282
#includestdio.h
int mul(int x);
void main(void)
{
double SN=1.0f;
int i,N;
scanf(“%d”,N);
if(N=0) printf(“對不起,請確保N=1\n”);
for(i=1;i=N;i++)
SN+=1.0/mul(i);
printf(“SN=%lf\n”,SN);
}
int mul(int x)
{
int i,y;
if(!x) return 1;
for(i=1,y=1;i=x;i++)
y*=i;
return y;
}
4.編寫一個程序其功能是;判斷一個數是不是迴文數(所謂迴文數是指,從左到右讀和從右到左讀是同一樣的結果)如;121是迴文數而
1211就不是
#includestdio.h
#includestdlib.h
void main(void)
{
int x,y;
scanf(“%d”,x);
if(x0)
{
printf(“輸入錯誤!\n”);
exit(-1);
}
y=x;
int i,j,k;
for(i=1,k=0;x!=0;x/=10)
{
j=x%10;
k=k*10+j;
}
if(y==k) printf(“%d是迴文數\n”,y);
else printf(“%d不是迴文數\n”,y);
}
5.編寫一個程序其功能是;完成5個數的大到小的排序.如;1,2,3,4,5輸出接個是5,4,3,2,1
#includestdio.h
#includestdlib.h
void main(void)
{
int i,j,k,a[5];
i=0;
do
{
printf(“請輸入第%d個數:”,i+1);
scanf(“%d”,a+i);
i++;
}while(i5);
for(i=0;i5;i++)
for(j=i;j5;j++)
{
if(a[i]a[j])
{
k=a[i];
a[i]=a[j];
a[j]=k;
}
}
for(i=0;i5;i++)
printf(“%d\t”,a[i]);
}
C語言程序編寫!
鄙人大一時的課程設計
/*TopBoy出品,品質保證*/
/*如果編譯失敗可將第一句#includemalloc.h改為#includealloc.h*/
#includemalloc.h
#includectype.h
#include graphics.h
#include stdio.h
#include conio.h
#define NULL 0
#define F10 17408
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define SPACE 0x3920
#define ESC 0x011b
#define ENTER 0x1c0d
#define LEN sizeof(struct student)
struct student *insert(struct student *head);
struct student *print(struct student *head);
struct student *change(struct student *head,int);
struct student *del(struct student *head,int);/*刪除記錄*/
struct student *search(struct student *head);
void stat(struct student *head);
void save(struct student *head);
void printrecord(struct student *head);
void menuinit(void);
struct student *order(struct student *head);
struct student
{
long num;
char name[20];
char sex[7];
int score;
struct student *next;
};
int n; /*記錄總數*/
int x=1,y=1,max;/*x表示顯示記錄的頁數 y表示該頁第y個記錄*/
char *menutext[]={“Order”,
“Insert”,
“Change”,
“Delete”,
“Search”,
“Statis”,
“Quit”};
void init(void) /* 圖形初始化 */
{
int gd=DETECT,gm;
initgraph(gd,gm,””);
}
void screeninit()
{
int i;
window(1,2,80,25);
textbackground(BLUE);
textcolor(7);
clrscr();
gotoxy(1,2);
putch(0xc9);/*輸出左上角邊框┏*/
for(i=1;i79;i++)putch(0xcd); /*輸出上邊框水平線*/
putch(0xbb); /*輸出右上角邊框 ┓*/
for(i=3;i24;i++)
{
gotoxy(1,i);putch(0xba); /*輸出左垂直線*/
gotoxy(80,i);putch(0xba); /*輸出右垂直線*/
}
gotoxy(1,24);putch(0xc8); /*輸出左下角邊框┗*/
for(i=1;i79;i++)putch(0xcd); /*輸出下邊框水平線*/
putch(0xbc); /*輸出右下角邊框┛*/
textbackground(7);
textcolor(BLACK);
window(1,25,80,25);
clrscr();
cputs(” You can press F10 to use Menu or ESC to quit.”);
}
int chooseab(int locx,int locy,char str1[],char str2[])
{
int i,text1=4,text2=14,back1=15,back2=1,key;
while(1)
{
window(locx,locy,locx+strlen(str1)-1,locy);
textbackground(back1);textcolor(text1);
clrscr();gotoxy(1,1);printf(“%s”,str1);
window(locx+strlen(str1)+3,locy,locx+strlen(str1)+strlen(str2)+2,locy);
textbackground(back2);textcolor(text2);clrscr();gotoxy(1,1);printf(“%s”,str2);
key=bioskey(0);
if(key==LEFT||key==RIGHT)
{
i=text1;text1=text2;text2=i;
i=back1;back1=back2;back2=i;
}
else if(key==ENTER)break;
}
window(59,5,74,20);
if(text1==4)return 1;
else return 2;
}
void menuinit()
{
int i;
window(1,1,80,1);
textbackground(7);
textcolor(BLACK);
clrscr();
for(i=0;i7;i++)
{
gotoxy(i*10+5,1);
cprintf(“%s”,menutext[i]);
}
}
struct student *menu(struct student *head,int choose)
{
int i=0,ii=0,key;
window(1,1,80,1);
gotoxy(i*10+5,1);
textbackground(BLACK);
textcolor(WHITE);
cprintf(“%s”,menutext[i]);
for(;;)
{
switch(key)
{
case LEFT:
{
ii=i-1;
if(ii0)ii=6;
break;
}
case RIGHT:
{
ii=i+1;
if(ii6)ii=0;
break;
}
}
gotoxy(i*10+5,1);
textbackground(7);
textcolor(BLACK);
cprintf(“%s”,menutext[i]);
gotoxy(ii*10+5,1);
textbackground(BLACK);
textcolor(WHITE);
cprintf(“%s”,menutext[ii]);
i=ii;
key=bioskey(0);
if(key==ESC)
{
gotoxy(i*10+5,1);
textbackground(7);
textcolor(BLACK);
cprintf(“%s”,menutext[i]);
break;
}
if(key==ENTER)
{
switch(i)
{
case 0:head=order(head);break;
case 1:head=insert(head);break;
case 2:change(head,choose);break;
case 3:head=del(head,choose);break;
case 4:search(head);break;
case 5:stat(head);break;
case 6:save(head);break;
}
menuinit();
printrecord(head);
break;
}
}
window(2,3,78,23);
textbackground(BLUE);
textcolor(YELLOW);
return(head);
}
int aboutpro()
{
int i;
window(58,4,78,23);
textbackground(BLUE);
textcolor(YELLOW);
clrscr();
putch(218);
for(i=3;i20;i++)putch(196); /*輸出上邊框水平線*/
putch(191);
for(i=2;i20;i++)
{
gotoxy(1,i);putch(179); /*輸出左垂直線*/
gotoxy(19,i);putch(179); /*輸出右垂直線*/
}
gotoxy(1,19);putch(192);
for(i=3;i20;i++)putch(196);
putch(217);
gotoxy(3,4);cputs(“Version:V2.0”);
gotoxy(3,6);cputs(“Author:”);
gotoxy(3,7);cputs(“Zhang Shou Song”);
gotoxy(5,9);cputs(“NEU C.S. 053”);
window(1,1,80,1);
}
struct student *order(struct student *head)
{
struct student *p,*q,*t,*h1;
h1=head-next;
head-next=NULL;
while(h1!=NULL)
{
t=h1;
h1=h1-next;
p=head;
q=head;
while(t-scorep-scorep!=NULL)
{
q=p;
p=p-next;
}
if(p==q)
{
t-next=p;
head=t;
}
else
{
t-next=p;
q-next=t;
}
}
return head;
}
struct student *insert(struct student *head)
{
struct student *p0,*p1,*p2;
window(59,5,74,20);
textbackground(BLUE);
textcolor(YELLOW);
while(1)
{
clrscr();
p0=head;
p1=(struct student *)malloc(LEN);
gotoxy(5,1);printf(“Insert”);
gotoxy(1,3);printf(“Quit by num=0”);
gotoxy(1,5);printf(“number:”);
scanf(“%ld”,p1-num);
if(p1-num==0)break;
gotoxy(1,6);printf(“name:”);
scanf(“%s”,p1-name);
gotoxy(1,7);printf(“sex:”);
scanf(“%s”,p1-sex);
do{
gotoxy(1,8);printf(“score:”);
scanf(“%d”,p1-score);
}while(p1-score0||p1-score100);
if(head==NULL)
{
head=p1;
p1-next=NULL;
}
else
{
while((p1-scorep0-score)(p0-next!=NULL))
{
p2=p0;
p0=p0-next;
}
if(p1-score=p0-score)
{
if(head==p0)head=p1;
else p2-next=p1;
p1-next=p0;
}
else
{
p0-next=p1;
p1-next=NULL;
}
}
n++;
}
aboutpro();
clrscr();
return(head);
}
void printrecord(struct student *head)
{
struct student *p;
int i,j;
window(3,7,51,21);
textbackground(BLUE);
textcolor(YELLOW);
clrscr();
p=head;
for(i=1;ix;i++) /*處理分頁*/
for(j=0;j15;j++)
p=p-next;
y=1;max=1;
if(head!=NULL)
do
{ gotoxy(3,y);
printf(“%-8d%-9ld%-16s%-8s%-7d\n”,y+(x-1)*15,p-num,p-name,p-sex,p-score);
p=p-next;
y++;
max=y-1;
if(y15)
{
max=15;
break;
}
}while(p!=NULL);
window(2,3,78,23);
}
void searchresult(struct student *p)
{
window(4,7,50,20);
textbackground(BLUE);
textcolor(YELLOW);
if(y==1)clrscr();
gotoxy(2,y);
printf(“%-8d %-9ld%-16s%-8s%-7d\n”,y,p-num,p-name,p-sex,p-score);
}
struct student *chooserecord(struct student *head)
{
int locate=1,key;
gotoxy(2,locate+4);putch(16);gotoxy(2,locate+4);
for(;;)
{
switch(key)
{
case UP:
{
locate–;
if(locate1n=15)locate=max;
if(locate1n15)
{
x–;
if(x1)x=n/15+1;
printrecord(head);
locate=max;
}
break;
}
case DOWN:
{
locate++;
if(locatemaxn=15)locate=1;
if(locatemaxn15)
{
x++;
if(x(n/15+1))x=1;/*判斷是否到達頁尾*/
locate=1;
printrecord(head);
}
break;
}
case F10:{head=menu(head,locate+(x-1)*15);break;}
}
putch(0);
gotoxy(2,locate+4);putch(16);gotoxy(2,locate+4);
key=bioskey(0);
if(key==ESC)save(head);
}
}
struct student *print(struct student *head)
{
window(2,3,78,23);
gotoxy(3,1);printf(“******************** Student’s Score Manage System **********************\n”);
gotoxy(3,2);printf(“| record | number | name | sex | score |\n”);
gotoxy(3,3);printf(“|——–|——–|—————|——-|——-|\n”);
printrecord(head);
chooserecord(head);
}
struct student *change(struct student *head,int choose)
{
struct student *p;
long stunum;
int i,key;
window(59,5,74,20);
textbackground(BLUE);
textcolor(YELLOW);
clrscr();
gotoxy(5,1);printf(“Change”);
p=head;
for(i=1;ichoose;i++)p=p-next;
gotoxy(1,3);printf(“input new name:”);
gotoxy(1,4);scanf(“%s”,p-name);
gotoxy(1,5);printf(“input new sex:”);
gotoxy(1,6);scanf(“%s”,p-sex);
gotoxy(1,7);printf(“input new score:”);
gotoxy(1,8);scanf(“%d”,p-score);
gotoxy(2,10);printf(“Change Success!\n”);
getch();
aboutpro();
clrscr();
return(head);
}
struct student *del(struct student *head,int choose)/*刪除記錄*/
{
struct student *p,*q;
long stunum;
int i;
window(59,5,74,20);
textbackground(BLUE);
textcolor(YELLOW);
clrscr();
q=p=head;
for(i=1;ichoose;i++)
{
q=p;
p=p-next;
}
gotoxy(5,1);printf(“Delete”);
gotoxy(1,3);printf(“Are you sure”);/*確認信息*/
gotoxy(2,4);printf(” to delete it?”);
if(chooseab(62,11,”Yes”,”No”)==1)
{
if(p==head)head=p-next;
else q-next=p-next;
free(p); /*釋放內存*/
n–;
}
aboutpro();
clrscr();
return(head);
}
struct student *search(struct student *head)
{
struct student *p;
long stunum;
char *stuname;
int i,ii,score1,score2,key;
char *searchwith[]={“num”,”name”,”score”};
y=1;max=0;
window(59,5,74,20);
textbackground(BLUE);
textcolor(YELLOW);
clrscr();
textbackground(BLUE);
textcolor(YELLOW);
for(i=1;i3;i++)
{
gotoxy(i*5+2,2);
cprintf(“%s”,searchwith[i]);
}
gotoxy(2,2);
textbackground(RED);
textcolor(WHITE);
cprintf(“%s”,searchwith[0]);
i=0;ii=0;
key=0;
while (1)
{
switch(key)
{
case LEFT:
{
ii=i-1;
if(ii0)ii=2;
break;
}
case RIGHT:
{
ii=i+1;
if(ii2)ii=0;
break;
}
}
gotoxy(i*5+2,2);
textbackground(BLUE);
textcolor(YELLOW);
cprintf(“%s”,searchwith[i]);
gotoxy(ii*5+2,2);
textbackground(RED);
textcolor(WHITE);
cprintf(“%s”,searchwith[ii]);
i=ii;
key=bioskey(0);
if(key==ESC)
{
gotoxy(i*10+5,1);
textbackground(7);
textcolor(BLACK);
cprintf(“%s”,searchwith[i]);
break;
}
if(key==ENTER)
{
switch(i)
{
case 0:
{
gotoxy(1,5);printf(“number:”);
scanf(“%ld”,stunum);
p=head;
while(1)
{
while(p-num!=stunump!=NULL)p=p-next;
if(p==NULL)break;
else
{
searchresult(p);
y++;max++;
p=p-next;
}
}
break;
}
case 1:
{
gotoxy(1,5);printf(“name:”);
gotoxy(1,6);scanf(“%s”,stuname);
p=head;
while(1)
{
while(strstr(p-name,stuname)==NULLp!=NULL)p=p-next;
if(p==NULL)break;
else
{
searchresult(p);
y++;max++;
p=p-next;
}
}
break;
}
case 2:
{
gotoxy(1,5);printf(“from score1:”);scanf(“%d”,score1);
gotoxy(1,6);printf(“to score2:”);scanf(“%d”,score2);
p=head;
while(1)
{
while((p-scorescore1||p-scorescore2)p!=NULL)p=p-next;
if(p==NULL)break;
else
{
searchresult(p);
y++;max++;
p=p-next;
}
}
break;
}
}
break;
}
}
if(y==1){window(59,5,74,20);gotoxy(4,7);printf(“Not Found”);}
getch();
aboutpro();
clrscr();
}
void stat(struct student *head)
{
int i,j,begin=0,end=0,stutotal[5]={0,0,0,0,0};
int pointx1=50,pointy1=281,pointx2,pointy2;
char str[5];
struct student *p;
p=head;
for(i=0;in;i++)
{
if(p-score=0p-score60)stutotal[0]++;
else if(p-score=60p-score70)stutotal[1]++;
else if(p-score=70p-score80)stutotal[2]++;
else if(p-score=80p-score90)stutotal[3]++;
else if(p-score=90p-score=100)stutotal[4]++;
p=p-next;
}
init();
for(i=0;i4;i++) /*根據比例畫扇形*/
{
setfillstyle(1,i+1);
begin=end; end=end+360*stutotal[i]/n;
pieslice(200,180,begin,end,100);
if(end==360)break;
}
if(end!=360)
{ setfillstyle(1,5);
pieslice(200,180,end,360,100);
}
rectangle(340,90,560,272);
settextstyle(0,0,1);
outtextxy(350,100,”color”);/*顯示圖示的說明*/
outtextxy(410,100,”range”);
outtextxy(500,100,”number”);
outtextxy(400,132,”0 to 60:”);
outtextxy(400,162,”60 to 70:”);
outtextxy(400,192,”70 to 80:”);
outtextxy(400,222,”80 to 90:”);
outtextxy(400,252,”90 to 100:”);
outtextxy(420,292,”total:”);
sprintf(str,”%d”,n);
outtextxy(520,292,str);
for(i=0;i5;i++)
{
sprintf(str,”%d”,stutotal[i]);
outtextxy(520,132+i*30,str);
}
for(i=1;i=5;i++)
{
setfillstyle(1,i);
bar(350,100+i*30,380,110+i*30);
}
getch();
cleardevice();
setfillstyle(1,0); /*畫折線圖*/
bar(100,80,300,280);
line(50,281,280,281);
line(50,281,50,80);
outtextxy(60,285,”0-60″);
outtextxy(105,285,”60-70″);
outtextxy(150,285,”70-80″);
outtextxy(195,285,”80-90″);
outtextxy(250,285,”90-100″);
for(i=0;i5;i++)
{ setfillstyle(1,i+1);
pointx2=pointx1+45; pointy2=280-200*stutotal[i]/n;
line(45,pointy2,50,pointy2);
sprintf(str,”%d”,stutotal[i]);
outtextxy(30,pointy2,str);
line(pointx1,pointy1,pointx2,pointy2);
pointx1=pointx2;pointy1=pointy2;
}
line(45,80,50,80);
sprintf(str,”%d”,n);
outtextxy(30,80,str);
outtextxy(30,280,”0″); getch();
closegraph();
screeninit();
window(2,3,78,23);
textbackground(BLUE);
textcolor(YELLOW);
clrscr();
gotoxy(3,1);printf(“******************** Student’s Score Manage System **********************\n”);
gotoxy(3,2);printf(“| record | number | name | sex | score |\n”);
gotoxy(3,3);printf(“|——–|——–|—————|——-|——-|\n”);
aboutpro();
clrscr();
}
void save(struct student *head)
{
FILE *fp;
int i;
struct student *p;
p=head;
fp=fopen(“c:\\studin.txt”,”wb”);
fprintf(fp,”zhangshousong”); /*將驗證字符串寫入文件*/
fprintf(fp,”\r\n”); /*將換行符號寫入文件*/
fprintf(fp,”%d”,n); /*將記錄數寫入文件*/
fprintf(fp,”\r\n”); /*將換行符號寫入文件*/
for(i=0;in;i++)
{
fprintf(fp,”%-20ld%-30s%-10s%-10d”,p-num,p-name,p-sex,p-score);/*格式寫入記錄*/
fprintf(fp,”\r\n”); /*將換行符號寫入文件*/
p=p-next;
}
fclose(fp);
exit(0);
}
struct student *readdata()
{
FILE *fp;
int i;
char str[20]=”zhangshousong”,str2[20];
struct student *p,*q,*head;
p=(struct student *)malloc(LEN);
head=p;
if((fp=fopen(“c:\\studin.txt”,”rb”))==NULL)/*打開文件*/
{
head=NULL;
n=0;
}
else
{
fscanf(fp,”%s”,str2);
if(strcmp(str,str2)!=0)head=NULL;/*判斷文件是不是合法的*/
else
{
fscanf(fp,”%d”,n); /*讀入記錄數*/
if(n==0)head=NULL;
else
{
for(i=0;in;i++)
{
fscanf(fp,”%20ld%30s%10s%10d”,p-num,p-name,p-sex,p-score); /*按格式讀入記錄*/
p-next=(struct student *)malloc(LEN);
q=p;
p=p-next;
}
q-next=NULL;
}
}
}
fclose(fp);
return head;
}
void main()
{
struct student *head;
int key=0,i;
init();
closegraph();
clrscr();
head=readdata();
menuinit();/*菜單初始化*/
screeninit();
window(2,3,78,23);
textbackground(BLUE);
textcolor(YELLOW);
clrscr();
aboutpro();
head=print(head);
}
原創文章,作者:MNKD,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/139810.html
微信掃一掃
支付寶掃一掃