文档库 最新最全的文档下载
当前位置:文档库 › 实验六.c

实验六.c

实验六.c
实验六.c

实验六文件系统设计

实验程序

#include "stdio.h"

#include "string.h"

#include "conio.h"

#include "stdlib.h"

#define MAXNAME 25 /*the largest length of mfdname,ufdname,filename*/

#define MAXCHILD 50 /*the largest child*/

#define MAX (MAXCHILD*MAXCHILD) /*the size of fpaddrno*/

typedef struct /*the structure of OSFILE*/

{

int fpaddr; /*file physical address*/

int flength; /*file length*/

int fmode; /*file mode:0-Read Only;1-Write Only;2-Read and Write(default);*/ char fname[MAXNAME]; /*file name*/

} OSFILE;

typedef struct /*the structure of OSUFD*/

{

char ufdname[MAXNAME]; /*ufd name*/

OSFILE ufdfile[MAXCHILD]; /*ufd own file*/

}OSUFD;

typedef struct /*the structure of OSUFD'LOGIN*/

{

char ufdname[MAXNAME]; /*ufd name*/

char ufdpword[8]; /*ufd password*/

} OSUFD_LOGIN;

typedef struct /*file open mode*/

{

int ifopen; /*ifopen:0-close,1-open*/

int openmode; /*0-read only,1-write only,2-read and write,3-initial*/

}OSUFD_OPENMODE;

OSUFD *ufd[MAXCHILD]; /*ufd and ufd own files*/

OSUFD_LOGIN ufd_lp;

int ucount=0; /*the count of mfd's ufds*/

int fcount[MAXCHILD]; /*the count of ufd's files*/

int loginsuc=0; /*whether login successfully*/

char username[MAXNAME]; /*record login user's name22*/

char dirname[MAXNAME];/*record current directory*/

int fpaddrno[MAX]; /*record file physical address num*/

OSUFD_OPENMODE ifopen[MAXCHILD][MAXCHILD]; /*record file open/close*/

int wgetchar; /*whether getchar()*/

FILE *fp_mfd,*fp_ufd,*fp_file_p,*fp_file;

void main()

{

int i,j,choice1;

char choice[50]; /*choice operation:dir,create,delete,open,delete,modify,read,write*/ int choiceend=1; /*whether choice end*/

char *rtrim(char *str); /*remove the trailing blanks.*/

char *ltrim(char *str); /*remove the heading blanks.*/

void LoginF(); /*LOGIN FileSystem*/

void DirF(); /*Dir FileSystem*/

void CdF(); /*Change Dir*/

void CreateF(); /*Create File*/

void DeleteF(); /*Delete File*/

void ModifyFM(); /*Modify FileMode*/

void OpenF(); /*Open File*/

void CloseF(); /*Close File*/

void ReadF(); /*Read File*/

void WriteF(); /*Write File*/

void QuitF(); /*Quit FileSystem*/

void help();

if((fp_mfd=fopen("c:\\osfile\\mfd","rb"))==NULL)

{

fp_mfd=fopen("c:\\osfile\\mfd","wb");

fclose(fp_mfd);

}

for(i=0;i

/*textattr(BLACK*16|WHITE);*/

/*clrscr();*/ /*clear screen*/

LoginF(); /*user login*/

/*clrscr();*/

if(loginsuc==1) /*Login Successfully*/

{

while (1)

{

wgetchar=0;

if (choiceend==1)

{

printf("\n\nC:\\%s>",strupr(dirname));

}

else printf("Bad command or file name.\nC:\\%s>",strupr(username));

gets(choice);

strcpy(choice,ltrim(rtrim(strlwr(choice))));

if (strcmp(choice,"dir")==0) choice1=1;

else if(strcmp(choice,"create")==0) choice1=2;

else if(strcmp(choice,"delete")==0) choice1=3;

else if(strcmp(choice,"attrib")==0) choice1=4;

else if(strcmp(choice,"open")==0) choice1=5;

else if(strcmp(choice,"close")==0) choice1=6;

else if(strcmp(choice,"read")==0) choice1=7;

else if(strcmp(choice,"modify")==0) choice1=8;

else if(strcmp(choice,"exit")==0) choice1=9;

else if(strcmp(choice,"cls")==0) choice1=10;

else if(strcmp(choice,"cd")==0) choice1=11;

else if(strcmp(choice,"help")==0) choice1=20;

else choice1=12;

switch(choice1)

{

case 1:DirF();choiceend=1;break;

case 2:CreateF();choiceend=1;if(!wgetchar) getchar();break;

case 3:DeleteF();choiceend=1;if(!wgetchar)getchar();break;

case 4:ModifyFM();choiceend=1;if(!wgetchar) getchar();break;

case 5:choiceend=1;OpenF();if (!wgetchar) getchar();break;

case 6:choiceend=1;CloseF();if (!wgetchar) getchar();break;

case 7:choiceend=1;ReadF();if (!wgetchar) getchar();break;

case 8:choiceend=1;WriteF();if (!wgetchar) getchar();break;

case 9:printf("\nYou have exited this system.");

QuitF();exit(0);break;

case 10:choiceend=1;clrscr();break;

case 11:CdF();choiceend=1;break;

case 20:help();choiceend=1;break;

default:choiceend=0;

}

}

}

else printf("\nAccess denied.");

}

void help(void)

{

printf("\nThe Command List\n");

/*printf("\nCd Attrib Create Modify Read Open Cls Delete Exit Close\n");*/ printf("Create : Create a file(You can initialize file's attribute and content.)\n");

printf("Open : Open a file to modify\n");

printf("Close : Close a file.\n");

printf("Modify : Modify the opened file.\n");

printf("Delete : Delete existed files.\n");

printf("CD : Change current directory.\n");

printf("Exit : Exit this program.\n");

}

char *rtrim(char *str) /*remove the trailing blanks.*/

{

int n=strlen(str)-1;

while(n>=0)

{

if(*(str+n)!=' ')

{

*(str+n+1)='\0';

break;

}

else n--;

}

if (n<0) str[0]='\0';

return str;

}

char *ltrim(char *str) /*remove the heading blanks.*/

{

char *rtrim(char *str);

strrev(str);

rtrim(str);

strrev(str);

return str;

}

void LoginF() /*LOGIN FileSystem*/

{

char loginame[MAXNAME],loginpw[9],logincpw[9],str[50];

int i,j,flag=1;

char a[25];

int findout; /*login user not exist*/

char *rtrim(char *str); /*remove the trailing blanks.*/

char *ltrim(char *str); /*remove the heading blanks.*/

void InputPW(char *password); /*input password,use '*' replace*/

void SetPANo(int RorW); /*Set physical address num*/

while(1)

{

findout=0;

printf("\n\nLogin Name:");

gets(loginame);

ltrim(rtrim(loginame));

fp_mfd=fopen("c:\\osfile\\mfd","rb");

for(i=0;fread(&ufd_lp,sizeof(OSUFD_LOGIN),1,fp_mfd)!=0;i++)

if (strcmp(strupr(ufd_lp.ufdname),strupr(loginame))==0)

{

findout=1;

strcpy(logincpw,ufd_lp.ufdpword);

}

fclose(fp_mfd);

if (findout==1) /*user exist*/

{

printf("Login Password:");

InputPW(loginpw); /*input password,use '*' replace*/

if (strcmp(loginpw,logincpw)==0)

{

strcpy(username,strupr(loginame));

strcpy(dirname,username);

fp_mfd=fopen("c:\\osfile\\mfd","rb");

for(j=0;fread(&ufd_lp,sizeof(OSUFD_LOGIN),1,fp_mfd)!=0;j++)

{

strcpy(str,"c:\\osfile\\");

strcat(str,ufd_lp.ufdname);

ufd[j]=(OSUFD*)malloc(sizeof(OSUFD));

strcpy(ufd[j]->ufdname,strupr(ufd_lp.ufdname));

fp_ufd=fopen(str,"rb");

fcount[j]=0;

for(i=0;fread(&ufd[j]->ufdfile[i],sizeof(OSFILE),1,fp_ufd)!=0;i++,fcount[j]++) {

ifopen[j][i].ifopen=0;

ifopen[j][i].openmode=4;

}

fclose(fp_ufd);

}

fclose(fp_mfd);

ucount=j;

SetPANo(0);

printf("\n\nLogin successful! Welcome to this FileSystem\n\n");

loginsuc=1;

return;

}

else

{

printf("\n\n");

flag=1;

while(flag)

{

printf("Login Failed! Password Error. Try Again(Y/N):");

gets(a);

ltrim(rtrim(a));

if (strcmp(strupr(a),"Y")==0) {loginsuc=0;flag=0;}

else if(strcmp(strupr(a),"N")==0){loginsuc=0;flag=0;return;} }

}

}

else

{

printf("New Password(<=8):");

InputPW(loginpw); /*input new password,use '*' replace*/

printf("\nConfirm Password(<=8):"); /*input new password,use '*' replace*/ InputPW(logincpw);

if (strcmp(loginpw,logincpw)==0)

{

strcpy(ufd_lp.ufdname,strupr(loginame));

strcpy(ufd_lp.ufdpword,loginpw);

fp_mfd=fopen("c:\\osfile\\mfd","ab");

fwrite(&ufd_lp,sizeof(OSUFD_LOGIN),1,fp_mfd);

fclose(fp_mfd);

strcpy(username,strupr(loginame));

strcpy(dirname,loginame);

strcpy(str,"c:\\osfile\\");

strcat(str,username);

if((fp_ufd=fopen(str,"rb"))==NULL)

{

fp_ufd=fopen(str,"wb");

fclose(fp_ufd);

}

fp_mfd=fopen("c:\\osfile\\mfd","rb");

for(j=0;fread(&ufd_lp,sizeof(OSUFD_LOGIN),1,fp_mfd)!=0;j++) {

strcpy(str,"c:\\osfile\\");

strcat(str,ufd_lp.ufdname);

ufd[j]=(OSUFD*)malloc(sizeof(OSUFD));

strcpy(ufd[j]->ufdname,strupr(ufd_lp.ufdname));

fp_ufd=fopen(str,"rb");

for(i=0;fread(&ufd[j]->ufdfile[i],sizeof(OSFILE),1,fp_ufd)!=0;i++,fcount[j]++) {

ifopen[j][i].ifopen=0;

ifopen[j][i].openmode=4;

}

fclose(fp_ufd);

}

fclose(fp_mfd);

ucount=j;

SetPANo(0);

printf("\n\nLogin Successful! Welcome to this System\n\n");

loginsuc=1;

return;

}

else

{

printf("\n\n");

flag=1;

while(flag)

{

printf("Login Failed! Password Error. Try Again(Y/N):");

gets(a);

ltrim(rtrim(a));

if (strcmp(strupr(a),"Y")==0) {loginsuc=0;flag=0;}

else if(strcmp(strupr(a),"N")==0){loginsuc=0;flag=0;return;} }

}

}

}

}

void SetPANo(int RorW) /*Set physical address num,0-read,1-write*/

{

int i,j;

if (RorW==0)

{

if((fp_file_p=fopen("c:\\osfile\\file\\file_p","rb"))==NULL)

{

fp_file_p=fopen("c:\\osfile\\file\\file_p","wb");

fclose(fp_file_p);

fp_file_p=fopen("c:\\osfile\\file\\file_p","rb");

for(i=0;fread(&j,sizeof(int),1,fp_file_p)!=0;i++)

fpaddrno[j]=1;

/*for(i=1;i

if ((i%13)==0) fpaddrno[i]=1;*/

}

else

{

fp_file_p=fopen("c:\\osfile\\file\\file_p","wb");

/*for(i=1;i

if((i%13)==0) fpaddrno[i]=0;*/

for(i=0;i

if (fpaddrno[i]==1)

fwrite(&i,sizeof(int),1,fp_file_p);

}

fclose(fp_file_p);

}

void InputPW(char *password) /*input password,use '*' replace*/ {

int j;

for(j=0;j<=7;j++)

{

password[j]=getch();

if ((int)(password[j])!=13)

{

if((int)(password[j])!=8)

putchar('*');

else

{

if (j>0)

{

j--;j--;

putchar('\b');putchar(' ');putchar('\b');

}

else j--;

}

}

else

{

password[j]='\0';

break;

}

password[j]='\0';

}

void DirF() /*Dir FileSystem*/

{

int i,j,count=0;

char sfmode[25],sfpaddr[25],str[25];

int ExistD(char *dirname); /*Whether DirName Exist,Exist-i,Not Exist-0*/

/*clrscr();*/

if (strcmp(strupr(ltrim(rtrim(dirname))),"")!=0)

{

printf("\n\nC:\\%s>dir\n",dirname);

printf("\n%14s%16s%14s%10s%18s\n","FileName","FileAddress","FileLength","Type","Fil eMode");

j=ExistD(dirname);

for(i=0;i

{

if ((i%16==0)&&(i!=0))

{

printf("\nPress any key to continue..");

getch();

/*clrscr();*/

printf("\n%14s%16s%14s%10s%18s\n","FileName","FileAddress","FileLength","Type","Fil eMode");

}

itoa(ufd[j]->ufdfile[i].fpaddr,str,10);

strcpy(sfpaddr,"file");

strcat(sfpaddr,str);

if (ufd[j]->ufdfile[i].fmode==0) strcpy(sfmode,"Read Only");

else if(ufd[j]->ufdfile[i].fmode==1) strcpy(sfmode,"Write Only");

else if(ufd[j]->ufdfile[i].fmode==2)strcpy(sfmode,"Read And Write");

else strcpy(sfmode,"Protect");

printf("%14s%16s%14d%10s%18s\n",ufd[j]->ufdfile[i].fname,sfpaddr,ufd[j]->ufdfile[i].flen gth,"",sfmode);

}

printf("\n %3d file(s)\n",fcount[j]);

}

else

{

printf("\n\nC:\\>dir\n");

printf("\n%14s%18s%8s\n","DirName","OwnFileCount","Type");

for(i=0;i

{

if ((i%16==0)&&(i!=0))

{

printf("\nPress any key to continue...");

getch();

/*clrscr();*/

printf("\n%14s%18s%8s\n","DirName","OwnFileCount","Type");

}

printf("%14s%18d%8s\n",ufd[i]->ufdname,fcount[i],"");

count=count+fcount[i];

}

printf("\n %3d dir(s),%5d file(s)\n",ucount,count);

}

}

int ExistD(char *dirname) /*Whether DirName Exist,Exist-i,Not Exist-0*/

{

int i;

int exist=0;

for(i=0;i

if (strcmp(strupr(ufd[i]->ufdname),strupr(dirname))==0)

{

exist=1;

break;

}

if (exist) return(i);

else return(-1);

}

void CdF() /*Exchange Dir*/

{char dname[MAXNAME];

char *rtrim(char *str); /*remove the trailing blanks.*/

char *ltrim(char *str); /*remove the heading blanks.*/

int ExistD(char *filename); /*Whether FileName Exist,Exist-i,Not Exist-0*/ printf("\nPlease input DirName (cd..-Previous dir; DirNAME-cd [DirNAME]):"); gets(dname);

ltrim(rtrim(dname));

if (ExistD(dname)>=0) strcpy(dirname,strupr(dname));

else if(strcmp(strupr(dname),"CD..")==0) strcpy(ltrim(rtrim(dirname)),""); else printf("\nError.\'%s\' does not exist.\n",dname);

}

void CreateF() /*Create File*/

{int fpaddrno,flag=1,i;

char fname[MAXNAME],str[50],str1[50],strtext[255],a[25];

char fmode[25];

char *rtrim(char *str); /*remove the trailing blanks.*/

char *ltrim(char *str); /*remove the heading blanks.*/

int FindPANo(); /*find out physical address num*/

int WriteF1(); /*write file*/

int ExistF(char *filename); /*Whether FileName Exist,Exist-i,Not Exist-0*/

int ExistD(char *dirname);

if (strcmp(strupr(dirname),strupr(username))!=0)

{printf("\nError. You must create file in your own dir.\n");wgetchar=1;}

else

{

printf("\nPlease input FileName:");

gets(fname);

ltrim(rtrim(fname));

if (ExistF(fname)>=0)

{printf("\nError. Name \'%s\' has already existed.\n",fname);

wgetchar=1;

}

else

{printf("Please input FileMode(0-Read Only, 1-Write Only, 2-Read and Write, 3-Protect):");

gets(fmode);

ltrim(rtrim(fmode));

if((strcmp(fmode,"0")==0)||(strcmp(fmode,"1")==0)||(strcmp(fmode,"2")==0)||(strcmp(fmode ,"3")==0))

{fpaddrno=FindPANo();

if (fpaddrno>=0)

{i=ExistD(username);

strcpy(ufd[i]->ufdfile[fcount[i]].fname,fname);

ufd[i]->ufdfile[fcount[i]].fpaddr=fpaddrno;

ufd[i]->ufdfile[fcount[i]].fmode=atoi(fmode);

ifopen[i][fcount[i]].ifopen=0;

ifopen[i][fcount[i]].openmode=4;

strcpy(str,"c:\\osfile\\file\\file");

itoa(fpaddrno,str1,10);

strcat(str,str1);

fp_file=fopen(str,"wb");

fclose(fp_file);

fcount[i]++;

while(flag)

{printf("Input text now(Y/N):");

gets(a);

ltrim(rtrim(a));

ufd[i]->ufdfile[fcount[i]-1].flength=0;

if(strcmp(strupr(a),"Y")==0)

{fp_file=fopen(str,"wb+");

ufd[i]->ufdfile[fcount[i]-1].flength=WriteF1();

flag=0;

}

else if(strcmp(strupr(a),"N")==0){flag=0;wgetchar=1;}

}

printf("\n\'%s\' has been created successfully!\n",fname);

}

else

{printf("\nFail!No Disk Space. Please format your disk.\n");wgetchar=1;} }

else {printf("\nError. FileMode\'s Range is 0-3\n");wgetchar=1;} }}

}

int ExistF(char *filename) /*Whether FileName Exist,Exist-i,Not Exist-0*/ {int i,j;

int exist=0;

int ExistD(char *dirname);

j=ExistD(dirname);

for(i=0;i

if (strcmp(strupr(ufd[j]->ufdfile[i].fname),strupr(filename))==0) {exist=1;

break;

}

if (exist) return(i);

else return(-1);

}

int FindPANo() /*find out physical address num*/

{int i;

for(i=0;i

if (fpaddrno[i]==0) {fpaddrno[i]=1;break;}

if (i

else return(-1);

}

int WriteF1() /*write file*/

{int length=0;

char c;

printf("Please input text(\'#\' stands for end):\n");

while((c=getchar())!='#')

{fprintf(fp_file,"%c",c);

if (c!='\n') length++;

}

fprintf(fp_file,"\n");

fclose(fp_file);

return(length);

}

void DeleteF() /*Delete File*/

{char fname[MAXNAME];

char str[50],str1[50];

int i,j,k,flag=1;

char a[25]; /*whether delete*/

char *rtrim(char *str); /*remove the trailing blanks.*/

char *ltrim(char *str); /*remove the heading blanks.*/

int ExistF(char *filename); /*Whether FileName Exist,Exist-i,Not Exist-0*/

int ExistD(char *dirname);

if (strcmp(strupr(dirname),strupr(username))!=0)

{printf("\nError. You can only delete file in your own dir.\n");wgetchar=1;}

else

{printf("\nPlease input FileName:");

gets(fname);

ltrim(rtrim(fname));

i=ExistF(fname);

if (i>=0)

{k=ExistD(username);

if(ifopen[k][i].ifopen==1)

{printf("\nError. \'%s\' is in open status. Close it before delete.\n",fname);wgetchar=1;} else

{

while(flag)

{printf("\'%s\' will be deleted. Are you sure(Y/N):",fname);

gets(a);

ltrim(rtrim(a));

if(strcmp(strupr(a),"Y")==0)

{fpaddrno[ufd[k]->ufdfile[i].fpaddr]=0;

itoa(ufd[k]->ufdfile[i].fpaddr,str,10);

for(j=i;j

{strcpy(ufd[k]->ufdfile[j].fname,ufd[k]->ufdfile[j+1].fname);

ufd[k]->ufdfile[j].fpaddr=ufd[k]->ufdfile[j+1].fpaddr;

ufd[k]->ufdfile[j].flength=ufd[k]->ufdfile[j+1].flength;

ufd[k]->ufdfile[j].fmode=ufd[k]->ufdfile[j+1].fmode;

ifopen[k][j]=ifopen[k][j+1];

}

fcount[k]--;

strcpy(str1,"c:\\osfile\\file\\file");

strcat(str1,str);

remove(str1);

flag=0;

printf("\n\'%s\' has been deleted successfully.\n",fname);

wgetchar=1;

}

else if(strcmp(strupr(a),"N")==0)

{printf("\nError. \'%s\' hasn\'t been deleted.\n",fname);

wgetchar=1;

flag=0;}

}}}

else

{printf("\nError. \'%s\' does not exist.\n",fname);wgetchar=1;}}

}

void ModifyFM() /*Modify FileMode*/

{char fname[MAXNAME],str[50];

int i,j,k,flag;

char fmode[25]; /*whether delete*/

char *rtrim(char *str); /*remove the trailing blanks.*/

char *ltrim(char *str); /*remove the heading blanks.*/

void InputPW(char *password); /*input password,use '*' replace*/

void SetPANo(int RorW); /*Set physical address num*/

int ExistF(char *filename); /*Whether FileName Exist,Exist-i,Not Exist-0*/

int ExistD(char *dirname);

if (strcmp(strupr(dirname),strupr(username))!=0) {printf("\nError.You can only modify filemode in yourself dir.\n");wgetchar=1;}

else

{ printf("\nPlease input FileName:");

gets(fname);

ltrim(rtrim(fname));

i=ExistF(fname);

if (i>=0)

{k=ExistD(username);

if(ifopen[k][i].ifopen==1)

{printf("\nError.\'%s\' is in open status. Close it before modify.\n",fname);wgetchar=1;}

else

{

if(ufd[k]->ufdfile[i].fmode==0) strcpy(str,"read only"); /*FileMode*/

else if(ufd[k]->ufdfile[i].fmode==1) strcpy(str,"write only");

else if(ufd[k]->ufdfile[i].fmode==2) strcpy(str,"read and write");

else strcpy(str,"Protect");

printf("\'%s\' filemode is %s.\n",fname,strupr(str));

printf("Modify to(0-read only,1-write only,2-read and write,3-Protect):");

gets(fmode);

ltrim(rtrim(fmode));

if(strcmp(fmode,"0")==0)

{ufd[k]->ufdfile[i].fmode=0;

printf("\n\'%s\' has been modified to READ ONL Y mode successfully.\n",fname);

wgetchar=1;

}

else if(strcmp(fmode,"1")==0)

{ufd[k]->ufdfile[i].fmode=1;

printf("\n\'%s\' has been modified to WRITE ONL Y mode successfully.\n",fname);

wgetchar=1;

}

else if(strcmp(fmode,"2")==0)

{ufd[k]->ufdfile[i].fmode=2;

printf("\n\'%s\' has been modified to READ AND WRITE mode successfully.\n",fname);

wgetchar=1;

}

else if(strcmp(fmode,"3")==0)

{ufd[k]->ufdfile[i].fmode=3;

printf("\n\'%s\' has been modified to FORBID mode successfully.\n",fname);

wgetchar=1;

}

else {printf("\nError.\'%s\' is not modified.\n",fname);wgetchar=1;}

}

}

else

{printf("\nError. \'%s\' dose not exist.\n",fname);wgetchar=1;}}

}

void OpenF() /*Open File*/

{char fname[MAXNAME];

char str[25],str1[25],fmode[25];

int i,k;

char *rtrim(char *str); /*remove the trailing blanks.*/

char *ltrim(char *str); /*remove the heading blanks.*/

int ExistF(char *filename); /*Whether FileName Exist,Exist-i,Not Exist-0*/

int ExistD(char *dirname);

if (strcmp(strupr(ltrim(rtrim(dirname))),"")==0)

{printf("\nError. Please change to ufd dir before open.\n");wgetchar=1;return;}

printf("\nPlease input FileName:");

gets(fname);

ltrim(rtrim(fname));

i=ExistF(fname);

if (i>=0)

{k=ExistD(dirname);

if(!ifopen[k][i].ifopen)

{if (ufd[k]->ufdfile[i].fmode==3)

{printf("\nError. The file\'s mode is FORBID. Can not open.\n");wgetchar=1;}

else

{printf("Please input FileOpenMode(0-Read Only,1-Write Only,2-Read and Write):");

gets(fmode);

ltrim(rtrim(fmode));

if((strcmp(fmode,"0")==0)||(strcmp(fmode,"1")==0)||(strcmp(fmode,"2")==0))

{if(fmode[0]=='0') /*open file with read only mode*/

{strcpy(str,"read only");

if((ufd[k]->ufdfile[i].fmode==0)||(ufd[k]->ufdfile[i].fmode==2)) ifopen[k][i].ifopen=1;

}

else if(fmode[0]=='1') /*open file with write only mode*/

{strcpy(str,"write only");

if((ufd[k]->ufdfile[i].fmode==1)||(ufd[k]->ufdfile[i].fmode==2)) ifopen[k][i].ifopen=1;

}

else if(fmode[0]=='2') /*open file with read and write mode*/

{strcpy(str,"read and write");

if(ufd[k]->ufdfile[i].fmode==2) ifopen[k][i].ifopen=1;

}

if(ufd[k]->ufdfile[i].fmode==0) strcpy(str1,"read only"); /*FileMode*/

else if(ufd[k]->ufdfile[i].fmode==1) strcpy(str1,"write only");

else if(ufd[k]->ufdfile[i].fmode==2) strcpy(str1,"read and write");

if(ifopen[k][i].ifopen==1)

{ifopen[k][i].openmode=atoi(fmode);

if (ifopen[k][i].openmode==0) strcpy(str,"read only");

else if(ifopen[k][i].openmode==1) strcpy(str,"write only");

else if(ifopen[k][i].openmode==2) strcpy(str,"read and write");

printf("\n\'%s\' has been opened. OpenMode is %s,FileMode is %s\n",fname,strupr(str),strupr(str1));

wgetchar=1;

}

else

{printf("\nError. \'%s\' hasn\'t been opened. OpenMode Error. OpenMode is %s,but FileMode is %s\n",fname,strupr(str),strupr(str1));wgetchar=1;}

}

else {printf("\nError. FileOpenMode\'s Range is 0-2\n");wgetchar=1;}

}}

else {printf("\nError. \'%s\' is in open status.\n",fname);wgetchar=1;}

}

else

{printf("\nError. \'%s\' does not exist.\n",fname);wgetchar=1;}

}

void CloseF() /*Close File*/

{int i,k,n=0;

char fname[MAXNAME];

char *rtrim(char *str); /*remove the trailing blanks.*/

char *ltrim(char *str); /*remove the heading blanks.*/

int ExistF(char *filename); /*Whether FileName Exist,Exist-i,Not Exist-0*/ int ExistD(char *dirname);

if (strcmp(strupr(ltrim(rtrim(dirname))),"")==0)

{printf("\nError. Please convert to ufd dir before close.\n");wgetchar=1;return;} k=ExistD(dirname);

printf("\nOpen File(s) In This Ufd:\n");/*display openned file*/

for(i=0;i

{if (ifopen[k][i].ifopen==1) {printf("%15s",ufd[k]->ufdfile[i].fname);n++;} if((n%4==0)&&(n!=0)) printf("\n");

}

printf("\n%d files openned.\n",n);

if (n==0) wgetchar=1;

if(n!=0)

{printf("\nPlease input FileName:");

gets(fname);

ltrim(rtrim(fname));

i=ExistF(fname);

if(i>=0)

{if(ifopen[k][i].ifopen==1)

{ifopen[k][i].ifopen=0;

ifopen[k][i].openmode=4;

printf("\n\'%s\' has been closed successfully.\n",fname);

wgetchar=1;

}

else {printf("\nError.\'%s\' is in closing status.\n",fname);wgetchar=1;}

}

else {printf("\nError. \'%s\' is not exist.\n",fname);wgetchar=1;}

}

}

void ReadF() /*Read File*/

{int i,k,n=0;

char fname[MAXNAME];

char str[255],str1[255],c;

char *rtrim(char *str); /*remove the trailing blanks.*/

char *ltrim(char *str); /*remove the heading blanks.*/

int ExistF(char *filename); /*Whether FileName Exist,Exist-i,Not Exist-0*/

int ExistD(char *dirname);

if (strcmp(strupr(ltrim(rtrim(dirname))),"")==0) {printf("\nError.Please convert to ufd dir before read.\n");wgetchar=1;return;}

printf("\nCaution:Open file first\n");

printf("Opened File(s) List:\n");

k=ExistD(dirname);

for(i=0;i

{if (ifopen[k][i].ifopen==1)

if ((ifopen[k][i].openmode==0) ||(ifopen[k][i].openmode==2)) {printf("%15s",ufd[k]->ufdfile[i].fname);n++;}

if((n%4==0)&&(n!=0)) printf("\n");

}

printf("\n%d files openned.\n",n);

if (n==0) wgetchar=1;

if(n!=0)

{printf("\nPlease input FileName:");

gets(fname);

ltrim(rtrim(fname));

i=ExistF(fname);

if(i>=0)

{if(ifopen[k][i].ifopen==1)

{if((ifopen[k][i].openmode==0) ||(ifopen[k][i].openmode==2))

{itoa(ufd[k]->ufdfile[i].fpaddr,str,10);

strcpy(str1,"file");

strcat(str1,str);

strcpy(str,"c:\\osfile\\file\\");

strcat(str,str1);

fp_file=fopen(str,"rb");

fseek(fp_file,0,0);

printf("\nThe text is:\n\n");

printf(" ");

while(fscanf(fp_file,"%c",&c)!=EOF)

if (c=='\n') printf("\n ");

else printf("%c",c);

printf("\n\n%d Length.\n",ufd[k]->ufdfile[i].flength);

fclose(fp_file);

wgetchar=1;

}

else

{printf("\nError.\'%s\' has been opened with WRITE ONL Y mode. It isn\'t read.\n",fname);wgetchar=1;}

}

else {printf("\nError.\'%s\' is in closing status. Please open it before read\n",fname);wgetchar=1;}

}

else {printf("\nError. \'%s\' does not exist.\n",fname);wgetchar=1;}

}

}

void WriteF() /*Write File*/

{int i,k,n=0;

char fname[MAXNAME];

char str[50],str1[50],a[50];

char *rtrim(char *str); /*remove the trailing blanks.*/

char *ltrim(char *str); /*remove the heading blanks.*/

int ExistF(char *filename); /*Whether FileName Exist,Exist-i,Not Exist-0*/

int ExistD(char *dirname);

int WriteF1(); /*write file*/

if (strcmp(strupr(ltrim(rtrim(dirname))),"")==0) {printf("\nError. Please convert to ufd dir before write.\n");wgetchar=1;return;}

k=ExistD(dirname);

printf("\nOpen File(s) with write only mode or read and write mode:\n");/*display openned files with writable mode*/

for(i=0;i

{if (ifopen[k][i].ifopen==1)

if ((ifopen[k][i].openmode==1) ||(ifopen[k][i].openmode==2)) {printf("%15s",ufd[k]->ufdfile[i].fname);n++;}

if((n%4==0)&&(n!=0)) printf("\n");

}

printf("\n%d files open.\n",n);

if (n==0) wgetchar=1;

if(n!=0)

{printf("\nPlease input FileName:");

gets(fname);

ltrim(rtrim(fname));

i=ExistF(fname);

if(i>=0)

{if(ifopen[k][i].ifopen==1)

{if((ifopen[k][i].openmode==1) ||(ifopen[k][i].openmode==2))

{itoa(ufd[k]->ufdfile[i].fpaddr,str,10);

strcpy(str1,"file");

strcat(str1,str);

strcpy(str,"c:\\osfile\\file\\");

strcat(str,str1);

if (ufd[k]->ufdfile[i].flength!=0)

{printf("\n\'%s\' has text. Overwrite or Append(O-overwrite,A-Append,else-not write):",fname);

gets(a);

ltrim(rtrim(a));

if (fp_file!=NULL) fclose(fp_file);

if (strcmp(strupr(a),"O")==0)

{printf("\nOverwrite\n");

fp_file=fopen(str,"wb");

ufd[k]->ufdfile[i].flength=0;

ufd[k]->ufdfile[i].flength=WriteF1();

}

else if(strcmp(strupr(a),"A")==0)

{printf("\nAppend\n");

fp_file=fopen(str,"ab");

ufd[k]->ufdfile[i].flength=ufd[k]->ufdfile[i].flength+WriteF1();

}

else

{printf("\nError.\'%s\' has not been written.\n",fname);

fclose(fp_file);

wgetchar=1;

}

}

else

{fp_file=fopen(str,"wb");

ufd[k]->ufdfile[i].flength=WriteF1();

}

}

else

{printf("\nError. \'%s\' has been opened with read only mode.It isn\'t writed.\n",fname);wgetchar=1;}

}

else

{printf("\nError. \'%s\' is in closing status. Please open it before write\n",fname);wgetchar=1;}

}

else

{printf("\nError. \'%s\' does not exist.\n",fname);wgetchar=1;}

}

}

void QuitF() /*Quit FileSystem*/

{int i,j;

C语言实验报告《指针》.doc

C语言实验报告《指针》 学号:__________ 姓名:__________ 班级:__________ 日期:__________ 指导教师:__________ 成绩:__________ 实验五指针 一、实验目的 1、掌握指针的概念、会定义和使用指针变量 2、掌握指向数组的指针变量 3、掌握字符串指针的使用 二、实验内容2、写一函数,求一个字符串的长度。在main函数中输入字符串,并输出其长度。(习题10.6)#include #include long fun(char *p) { 填写程序 } void main() { char s[6]; long n; printf( enter a string:n gets(s); n=fun(s); printf( %ldn ,n); }

一、三、实验步骤与过程 四、程序调试记录 C语言实验报告《综合实验》 学号:__________ 姓名:__________ 班级:__________ 日期:__________ 指导教师:__________ 成绩:__________ 实验六综合实验 一、实验目的 1、掌握C语言中的变量、数组、函数、指针、结构体等主要知识点。 2、掌握C程序的结构化程序设计方法,能使用C语言开发简单的应用程序。 3、掌握C程序的运行、调试方法等。 二、实验内容 编写一个学生信息排序程序。要求:

1、程序运行时可输入n个学生的信息和成绩(n预先定义)。 2、学生信息包括:学号、英文姓名、年龄;学生成绩包括:语文、数学、计算机。 3、给出一个排序选择列表,能够按照上述所列信息(学号、姓名、年龄、语文、数学、计算机)中的至少一个字段进行排序,并显示其结果。 1、使用函数方法定义各个模块。 三、实验步骤与过程 物理实验报告·化学实验报告·生物实验报告·实验报告格式·实验报告模板 四、程序调试记录

C语言实验报告合集

《C语言程序设计》实验报告 专业:班级:学号:学生姓名:实验日期:成绩:指导老师: 实验单元一程序基本结构设计 一、实验题目 实验一熟悉VC++环境 二、实验目的 1. 熟悉C程序编辑环境,掌握主要菜单项的作用。 2. 熟悉编写一个C程序的上机过程(编辑、编译、链接和运行)。 3. 熟悉C程序的基本输入输出语句 三、实验内容 1. 书本第一章的例一程序。 2. 由键盘输入两个整数,求得它们的和并输出至屏幕。 四、实验环境 1.硬件环境:当前所有电脑硬件环境均支持。 2.软件环境:Visual C++ 6.0 五、实验代码及结果(程序运行结果请以截屏图给出) 1.源程序代码: #include "stdio.h" int main() {int a,b,sum; a=2; b=3; sum=a+b; printf("007zhang\nsum=%d\n",sum); return 0; } 运行结果为:

2.源程序代码: 运行结果为: 六、实验心得(请对每个实验题目编程及调试运行中遇到的问题及解决方案进行简单总结)

《C语言程序设计》实验报告 专业:班级:学号:学生姓名: 实验日期:成绩:指导老师: 实验单元一程序基本结构设计 一、实验题目 实验二顺序结构程序设计 二、实验目的 (1)掌握C语言数据类型以及变量的声明。 (2)掌握C语言的赋值运算符、赋值表达式、赋值语句。 (3)掌握C语言的整型和字符型的混合运算。 (4)掌握C语言的输入、输出函数的使用格式。 三、实验内容 1.有人用温度计测量出华氏温度,现输入华氏温度f,请编写程序把它转换为摄氏温度c。 已知华氏温度和摄氏温度有如下关系:c=5/9(f-32)。 2.输入长方形的长和宽,编程求该长方形的周长和面积。(要求周长和面积保留两位小数) 3.将字符串“China”译成密码,译码规则是:用字母表顺序中原来字母后面的第5个字母 替代原来的字母。请编写程序实现译码并输出。 四、实验环境 1.硬件环境:当前所有电脑硬件环境均支持。 2.软件环境:Visual C++ 6.0 五、实验代码及结果(程序运行结果请以截屏图给出) 1.源程序代码: 运行结果为: 2.源程序代码: 运行结果为: 3.源程序代码: 运行结果为: 六、实验心得(请对每个实验题目编程及调试运行中遇到的问题及解决方案进

C语言实验报告《函数》

C语言实验报告《函数》 C语言实验报告《函数》 学号: __________ 姓名: __________ 班级: __________ 日期: __________ 指导教师: __________ 成绩: __________ 实验四函数 一、实验目的 1、掌握函数定义、调用和声明的方法 2、掌握实参和形参之间的传递方式 3、函数的嵌套调用 二、实验内容 1、写一个函数,将两个字符串连接。(习题 8. 6) 2、编写一个函数,由实参传来一个字符串,统计此字符串中字母、数字、空格和其他字符的个数,在主函数中输入字符串以及输出上述的结果。(习题 8.9)

3、请将实验三中的实验内容三改正后,再改写成函数形式(排序部分)。物理实验报告·化学实验报告·生物实验报告·实验报告格式·实验报告模板 三、实验步骤与过程 四、程序调试记录 一、实验目的 1.观察植物细胞有丝分裂的过程,识别有丝分裂的不同时期。 初步掌握制作洋葱根尖有丝分裂装片的技能。 3.初步掌握绘制生物图的方法。 二、实验原理在植物体中,有丝分裂常见于根尖、茎尖等分生区细胞,高等植物细胞有丝分裂的过程,分为分裂间期和分裂期的前期、中期、后期、末期。可以用高倍显微镜观察植物细胞的有丝分裂的过程,根据各个时期细胞内染色体(或染色质)的变化情况,识别该细胞处于有丝分裂的哪个时期,细胞核内的染色体容易被碱性染料着色。 三、材料用具洋葱根尖、显微镜、载玻片、盖玻片、滴管、镊子、培养皿、铅笔、质量分数为15%的盐酸、体积分数为95%的酒精、质量分数为0.01gml的龙胆紫(或紫药水) 四、实验过程(见书P39) 1.洋葱根尖的培养(提前3—4天) 2.解离: 5min 3.漂洗: 10min 4.染色: 5min

c语言实验报告6 指针

实验6 指针1.实验目的和要求 (1)掌握指针的概念、指针变量定义格式和引用。 (2)掌握指针的运算方法。 (3)掌握数组的指针和指向数组的指针变量。 (4)掌握指针与函数的关系。 (5)能正确使用字符串的指针和指向字符串的指针变量。(6)了解指向函数的指针。 (7)了解指向指针变量的指针。 2.实验内容:实验指导书中的实验九。 3.实验步骤 9.1 调试程序,找出程序的错误。 改正后的程序代码(贴图): 运行结果(帖图): 9.2 运行程序:

运行结果:; 9.3 修改程序错误,并运行。 改正后程序代码: 运行结果(帖图): 9.4. 程序填空:

运行结果(请帖图):9.5. 程序填空:

程序填空: #include main() { int a[5][5]={0},*p[5],i,j; for(i=0; i<5; i++) p[i]= &a[i][0] ; for(i=0; i<5; i++) { *(p[i]+ i )=1; *(p[i]+5- (i+1) )=1; } for(i=0; i<5; i++) { for(j=0; j<5; j++) printf("%2d", p[i][j]); Printf("\n") ; } } 运行结果(请帖图):

9.6 编程(选作) #include main() { int *p,a[20],i,k,m,n; p=a; printf("请输入人数n:"); scanf("%d",&n); for(i=0;i

c语言实验报告

C语言实验报告 说明 1,所有程序均用VC6.0编译运行,文件名命名为姓名+日期,因为实验存在补做,所以并不是按照日期先后排列的。 2,为了使截图清晰,手动将运行窗口由“黑底白字”改为了“白底黑字”。 实验2 数据类型、运算符和表达式 一、实验目的: (1)掌握C语言数据类型,熟悉如何定义一个整型、字符型、实型变量、以及对它们赋值的方法。 (2)学会使用C的有关算术运算符,以及包含这些运算符的表达式,特别是自加(++)和自减(――)运算符的使用。 (3)掌握C语言的输入和输出函数的使用 (4)进一步熟悉C程序的编辑、编译、连接和运行的过程。 三、程序调试与问题解决: (1)输人并运行下面的程序 #include void main() { char c1,c2; c1='a'; c2='b'; printf("%c %c\n",c1,c2); } ○1运行此程序。 ○2在上面printf语句的下面再增加一个printf语句。

printf("%d %d\n",c1,c2); 再运行,并分析结果。 输出结果如图,编译成功,无错误。 ○3将第3行改为 int c1,c2; 再运行,并分析结果。 ○4再将第4、5行改为 c1=a; c2=b; 再运行,并分析结果。 a,b没有定义,编译报错。 ○5再将第4、5行改为 c1=‘’a‘’; c2=‘’b‘’; 再运行,并分析结果。 ○6再将第4、5行改为 c1=300; c2=400; 再运行,并分析结果。 以字符型输出时,输出的将是300,400对应的字符。 (2)输人并运行教材第3章习题3. 6给出的程序 #include main () { char c1='a',c2='b',c3='c',c4='\101',c5='\116';

C语言指针实验报告

C语言程序设计实验报告 1实验目的 (1)掌握指针的概念,会定义和使用指针变量; (2)能正确使用变量的指针和指向变量的指针变量; (3)能正确使用数组的指针和指向数组的指针变量; (4)能正确使用字符串的指针和指向字符串的指针变量; 2实验内容 将一个任意整数插入到已排序的整形数组中,插入后,数组中的数仍然保持有序;要求: (1)整形数组直接由赋值的方式初始化,要插入的整数有scanf()函数数入;(2)算法实现过程采用指针进行处理; (3)输入原始数据以及插入整数后的数据,并加以说明;

3算法描述流程图

4源程序 #include main() { int a[100],m,i,*p,n,w; printf("请输入要输入的数组的元素个数:\n"); scanf("%d",&n); printf("请输入已排好序的数组:\n"); for(i=0;i=w;i--) { a[i+1]=a[i]; } a[i+1]=m; for(i=0;i<=n;i++) { printf("%-4d",a[i]); } printf("\n"); } 5测试数据 “1,3,5,7,9,11,13,15,17,19······10” 6运行结果 7出现问题及解决方法 在编写过程中,

for(i=n-1;a[i]>=w;i--) { a[i+1]=a[i]; } a[i+1]=m; 这一步没有注意a[i++]=m和a[i+1]=m中i++和i+1不同,a[i++]=m是先将的值赋给a[i],然后在执行自增;而在实验过程中忽略了这一点,造成了不必要的麻烦; 8实验心得 通过这次指针实验掌握了指针的概念,会定义和使用指针变量,并且能利用指针来简单化一些问题,给以后的编程带来了很大的便利;

C语言实验

C语言上机指导指导教师:李辉李晓丽 东北电力大学 电气工程学院 2006年3月

实验2数据类型、运算符和表达式 1.实验目的 掌握C语言数据类型,熟悉如何定义一个整形、字符型和实型的变量,以及对他们赋值的方法。 掌握不同的类型数据之间赋值的规律。 学会使用C的有关算术运算符,以及包含这些运算符的表达式,特别是自加(++)和自减(--)运算符的使用。 进一步熟悉C程序的编辑、编译、连接和运行的过程。 2.实验内容和步骤 (2).在此基础上增加一个语句: printf(“%d%d\n”,c1,c2); 再运行,并分析结果。 (3).再将第2行改为: int c1,c2; 再使之运行,并观察结果。 (4).再将第3、4行改为: c1 = a; /* 不用单撇号*/ c2 = b; 再使之运行,分析其运行结果。

(5).再将第3、4行改为: c1 = ”a”; /* 用双撇号*/ c2 = ”b”; 再使之运行,分析其运行结果。 (6).再将第3、4行改为: c1 = 300; /* 用大于255的整数*/ c2 = 400; 再使之运行,分析其运行结果。 输入并运行教材第3章习题3.6给出的程序 (1).将一个负整数赋给一个无符号的变量,会得到什麽结果。画出它们在内存中的表示形式。 (2).将一个大于32765的长整数赋给整形变量,会得到什麽结果。画出它们在内存中的表示形式。 (3).将一个长整数赋给无符号的变量,会得到什麽结果(分别考虑该长整数的值大于或等于65535 以及小于65535 的情况)。画出它们在内存中的表示形式。 同学们可以改变程序中各变量的值,以便比较。例如:a = 65580, b = -40000, e = 65535, f = 65580。 输入习题3.10

C语言实验报告

《C语言程序设计实训1》报告 设计题目:基于数组的学生信息管理系统学院名称:信息科学技术学院

专业:计算机科学与技术 班级: 姓名:学号 指导教师: 提交日期: 2014年12月22日 一、实验内容 编写并调试程序,实现学校各专业班级学生信息的管理。10个学生的信息存储在文件中。在头文件中定义学生信息的结构体类型,包括: 学号、姓名、专业、班级、3门成绩;和符号常量N(学生数)。(同一班 级的学生可以属于不同的专业,同一专业的学生可以属于不同的班级)

二、实验要求 (1)main函数:以菜单形式将各项功能提供给用户,根据用户的选择, 调用相应的函数。 STU student[N]; 函数 #include "" void main() { int i,n,id,num,m,sub,corse;将从文件中读取10个人的信\n"); printf("\n2.您将从文件中随机读取第n(0<=n<=9)个学生的信息\n") printf("\n3.您将根据某一班级某一专业总分超过多少进行查找\n"); printf("\n4.您将求某一课程分数最高的学生序号的下标\n"); printf("\n5.您将对平均成绩由低到高进行简单选择排序法\n ");

printf("\n6.您将对某一个班的平均成绩由低到高进行起泡排序法\n"); printf("\n7.您将对某门专业的学生的某门课程成绩由低到高进行直接插入排序法\n"); printf("\n8.您将把学生信息存入文件\n"); scanf("%d",&id); getchar(); switch(id){ case 1: { printf("\n从文件中读取信息\n"); Input(students,sizeof(students)/sizeof(STU));Sort_select 函数 #include "" void Sort_select(STU * p) { int i,j,k; float sum,ave[N],t; STU tem; for(i=0;i

游戏C语言实验报告

嘉应学院计算机学院 实验报告 课程名称: C程序设计 开课学期: 2015—2016学年第1学期 班级:计算机1505 指导老师:陈广明 设计题目:游戏2048 学生姓名(学号):第3组:钟瞻宇

目录 一、实验目的和要求 .................................................................................................................................................... 二、实验环境、内容和方法 ........................................................................................................................................ 三、程序设计 ................................................................................................................................................................ 四、源代码 .................................................................................................................................................................... 五、调试与运行结果.................................................................................................................................................... 六、总结........................................................................................................................................................................

c语言实验六

常熟理工学院 《C语言程序设计》实验指导与报告书 ______学年第____ 学期 专业: ___________________________________________ 学号: ___________________________________________ 姓名: ___________________________________________ 实验地点:___________________________________________ 指导教师:___________________________________________ 计算机科学与工程学院 2014

实验7 数组程序设计(1) 7.1 实验目的 1.掌握一维数组的定义及初始化方法。 2.掌握用循环语句对一维数组进行处理的方法。 3.熟悉对数组元素进行处理的常规算法(如排序、插入、删除及查找等)。 7.2 示例程序 【实验7.1】本程序的功能是:取出一个十进制正整数中的所有偶数数字,用这些数字构成一个最大数。 程序代码: #include #define N 10 int main() { int i,j,k=0,t,d,a[N]; long n,m=0; printf("Please enter a long integer."); scanf("%ld",&n); while(n>0) /* 依次取出每位数,并将偶数存入数组*/ { t=n%10; /* 取出一位数*/ if(t%2==0) /* 判断是否偶数*/ a[k++]=t; /* 将偶数存入数组*/ n/=10; /* 为取下一位数做准备*/ } for(i=0;ia[d]) /* 后续元素依次与起始元素进行比较*/ d=j; /* 记录比起始元素大的元素的下标*/ if(d!=i) /* 一轮比较后判断记录的下标是否发生了变化*/ { t=a[d];a[d]=a[i];a[i]=t;} /* 若发生了变化,则交换元素值*/ } for(i=0;i

C语言实验报告参考答案原

C语言实验报告参考答案 实验一熟悉C语言程序开发环境及数据描述 四、程序清单 1.编写程序实现在屏幕上显示以下结果: The dress is long The shoes are big The trousers are black 答案: #include<> main() { printf("The dress is long\n"); printf("The shoes are big\n"); printf("The trousers are black\n"); } 2.编写程序: (1) a=150,b=20,c=45,编写求a/b、a/c(商)和a%b、a%c(余数)的程序。 (2)a=160,b=46,c=18,d=170, 编写求(a+b)/(b-c)*(c-d)的程序。 答案: (1) #include<> main() {

int a,b,c,x,y; a=150; b=20; c=45; x=a/b; y=a/c; printf("a/b的商=%d\n",x); printf("a/c的商=%d\n",y); x=a%b; y=a%c; printf("a/b的余数=%d\n",x); printf("a/c的余数=%d\n",y); } (2) #include<> main() { int a,b,c,d; float x; a=160; b=46; c=18;

d=170; x=(a+b)/(b-c)*(c-d); printf("(a+b)/(b-c)*(c-d)=%f\n",x); } 3. 设变量a的值为0,b的值为-10,编写程序:当a>b时,将b赋给c;当a<=b 时,将0赋给c。(提示:用条件运算符) 答案: #include<> main() { int a,b,c; a=0; b=-10; c= (a>b) b:a; printf("c = %d\n",c); } 五、调试和测试结果 1.编译、连接无错,运行后屏幕上显示以下结果: The dress is long The shoes are big The trousers are black 2、(1) 编译、连接无错,运行后屏幕上显示以下结果: a/b的商=7

C语言指针实验报告

C语言程序设计实验报告 1实验目得 (1)掌握指针得概念,会定义与使用指针变量; (2)能正确使用变量得指针与指向变量得指针变量; (3)能正确使用数组得指针与指向数组得指针变量; (4)能正确使用字符串得指针与指向字符串得指针变量; 2实验内容 将一个任意整数插入到已排序得整形数组中,插入后,数组中得数仍然保持有序; 要求: (1)整形数组直接由赋值得方式初始化,要插入得整数有scanf()函数数入; (2)算法实现过程采用指针进行处理; (3)输入原始数据以及插入整数后得数据,并加以说明;

3算法描述流程图

4源程序 #include main() { int a[100],m,i,*p,n,w; printf("请输入要输入得数组得元素个数:\n"); scanf("%d",&n); printf("请输入已排好序得数组:\n"); for(i=0;i=w;i--) { a[i+1]=a[i]; } a[i+1]=m; for(i=0;i<=n;i++) { printf("%-4d",a[i]); } printf("\n"); } 5测试数据 “1,3,5,7,9,11,13,15,17,19······10” 6运行结果 7出现问题及解决方法 在编写过程中,

for(i=n-1;a[i]>=w;i--) { a[i+1]=a[i]; } a[i+1]=m; 这一步没有注意a[i++]=m与a[i+1]=m中i++与i+1不同,a[i++]=m就是先将得值赋给a[i],然后在执行自增;而在实验过程中忽略了这一点,造成了不必要得麻烦; 8实验心得 通过这次指针实验掌握了指针得概念,会定义与使用指针变量,并且能利用指针来简单化一些问题,给以后得编程带来了很大得便利;

华北水利水电大学C语言实验报告(六)

华北水院高级语言程序设计(C语言)实验报告(六) --学年第学期 级专业:学号:姓名:……………………………………………………………………………………………… 一、实验题目:指针 二、实验目的:(略) 三、实验内容: 1. 程序验证(略) 2.程序设计 1)分别求出数组中所有3的倍数之和以及5的倍数之和。形参n给了数组中数据的个数,利用指针three返回3的倍数之和,利用指针five返回5的倍数之和。例如:数组中的值依次为:1,5,9,2,3,15,11,6,10;则利用指针three返回3的倍数之和33;利用指针five返回5的倍数之和30。请在下面空白处完善这一函数的功能,并且调试运行出结果。 源代码:运行结果: #include #define N 20 void fun(int *a,int n,int *three,int *five) { int i; *three=0;*five=0; for(i=0;i

C语言实验报告

实验一进制转换 一、实验要求 采用模块化程序设计完成进制转换。由键盘输入一个十进制正整数,然后将该数转换成指定的进制数(二、八、十六) 形式输出。指定的进制由用户输入。 二、实验目的 1、熟悉C 环境的安装、使用。 2、承上启下,复习《C 程序设计》等基础课程的知识。 3、掌握C 语言编程的方法。 三、预备知识 1、VC6.0的安装与使用。 2、C 程序设计基础知识。 四、实验内容 采用模块化程序设计完成进制转换。 五、程序框图 六、程序清单 1. 编写主函数:输入需转换的数与转换的进制 2. 编写子函数 (1)函数转换为除16进制以外的进制转换算数编程,使用while 循环实现计算进制的转换,并输出转换后的数字; (2)函数转换为16进制,用while 函数实现16进制转换的计算并输出16进制转换后的数据; 3. 编写数组,关于16进制的一系列字符 4. 编写主函数加入do while 使函数可以循环。

七、实验步骤 #include char num[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; void fun(int n, int m) { int i=-1, a[100]; if(m!=16) { while(n) { a[i++]=n%m; n=n/m; } while(i!=-1) printf("%d",a[--i]); printf("\n"); } else { while(n) { a[++i]=num[n%16]; n/=16; } while(i!=-1) printf("%c",a[i--]); printf("\n"); } } int main() { int a, c; char cIn = 0; do { printf("\n输入正整数,转换的进制:"); scanf("%d %d",&a,&c); fun(a,c); printf("Go on? (Y/N):"); cIn = getchar( ); if(cIn == 'N' || cIn == 'n') return 0; } while(1);fun(a,c);

C语言实验报告

实验一进制转换一、实验要求 采用模块化程序设计完成进制转换。由键盘输入一个十进制正整数,然后将该数转换成指定的进制数(二、八、十六) 形式输出。指定的进制由用户输入。 二、实验目的 1、熟悉C 环境的安装、使用。 2、承上启下,复习《C 程序设计》等基础课程的知识。 3、掌握C 语言编程的方法。 三、预备知识 1、VC6.0的安装与使用。 2、C 程序设计基础知识。 四、实验内容 采用模块化程序设计完成进制转换。

五、程序框图 六、程序清单 1. 编写主函数:输入需转换的数与转换的进制 2. 编写子函数 (1)函数转换为除16进制以外的进制转换算数编程,使用while 循环实现计算进制 的转换,并输出转换后的数字; (2)函数转换为16进制,用while 函数实现16进制转换的计算并输出16进制转换后的数据; 3. 编写数组,关于16进制的一系列字符 4. 编写主函数加入do while 使函数可以循环。

七、实验步骤 #include char num[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; void fun(int n, int m) { int i=-1, a[100]; if(m!=16) { while(n) { a[i++]=n%m; n=n/m; } while(i!=-1) printf("%d",a[--i]); printf("\n");

} else { while(n) { a[++i]=num[n%16]; n/=16; } while(i!=-1) printf("%c",a[i--]); printf("\n"); } } int main() { int a, c;

c语言实验报告

丽水学院计算机实验报告

丽水学院计算机实验报告 一、实验目的 1.熟悉Visual C++和C-Free等集成环境,掌握运行一个C程序的基本步骤,包括编辑、编译、连接和运行。 2.掌握算术表达式和赋值表达式的使用。 3.掌握基本输入、输出函数的使用。 4.能够编程实现简单的数据处理。 二、实验环境 硬件:Pentium以上的计算机。 软件:Windows XP操作系统、Visual C++和C-Free等集成环境。 三、实验内容和结果 1.编程题1 在屏幕上显示一个短句“What is a computer?” 思考: (1)如何在屏幕上显示你自己的学号,姓名和班级? (2)如何在屏幕上显示数字、英文字母和汉字等信息?例如:“你在机房吗?” 编程题1源程序: #include<> void main() { printf("What is a computer?\n");

} 程序运行结果: What is a computer? 思考题(1): #include<> void main() { printf(",小王,班级\n"); } 思考题(2): #include<> void main() { printf("英文字母abcdefgABCDEFG\n"); printf("汉字:哇哈哈啊哈和\n"); } 2.编程题2 在屏幕上显示下列图形。 * * * * * * * * * *

思考:如何在屏幕上显示下列图形? A A A A 编程题2源程序: #include<> void main() { int i,j; for(j=1;j<5;j++) { for(i=5;i>j;i--) printf("*"); printf("\n"); } } 程序运行结果: * * * * * * * * * * 思考题:

C语言实验报告(四)

C语言实验报告(四) 一、实验目的 1.掌握C语言中函数和模块 2.掌握怎样定义函数,如何调用或使用函数,如何声明函数 3.掌握函数的参数,了解在函数调用时,参数是如何传递的 4.在使用函数的过程中怎样确定函数的参数 5.如何使用局部变量和全局变量 二、实验内容 1. 偶数判断 描述: 编写一个用户自定义函数,该函数有一个整型参数,函数的功能是:当这个整型数的值是偶数时,函数的返回值为0,当这个整型数的值是奇数时,返回值为1 。编写一个程序,从键盘输入m个整数,分别调用用户自定义函数来判断这m个整数的奇偶性。 输入: 第一行为一个整数m,表示要判断m个整数的奇偶性;紧接着是m行,每行一个整数。 输出:有m行,分别对应输入的第2到第m+1行的整数的奇偶性样例输入: 2 1 6 样例输出: 奇数 偶数

·程序代码: #include"stdio.h" int isEven(int a) { if(a%2==0) return 1; else return 0; } int main() { int m,i,b; scanf("%d",&m); for(i=1;i<=m;i++) { scanf("%d",&b); if(isEven(b)==1) printf("偶数\n"); else printf("奇数\n"); } return 0; } 2.温度转换 描述: 编写一个用户自定义函数,函数的功能是:将华氏温度转F换为摄氏温度C。转换公式为:C=5*(F-32)/9。编写一个程序,输出指定范围的华氏温度与摄氏温度的对照表,其中华氏温度步长为4。 输入: 两个空格隔开的整数n,m(且0

C语言实验6(附参考答案)

实验6:运算符与表达式(5)实验目的: 1、熟悉字符的输入输出和格式化的输出,了解基本规则。 2、进一步熟悉C语言程序的编辑、编译、连接和运行的方法。 实验内容: 1、 getchar函数的作用是从终端输入____1______个字符。 2、如果从键盘输入字符’a’并按回车键,请写出以下程序运 行效果_____a_____。 main() { putchar(getchar()); } 3、以下程序的输出结果为 main() { char c=‘x’; printf(“c:dec=%d,oct=%o,ASCII=%c\n”,c,c,c); } 4、以下程序的输出结果为。 main() { int x=1,y=2;

printf(“x=%d y=%d *sum*=%d\n”,x,y,x+y); printf(“10 Squared is:%d\n”,10*10); } 5、以下程序的输出结果为____________________。 #include main() { float a=123.456; double b=8765.4567; printf(“(1) %f\n”,a); printf(“(2) %14.3f\n”,a); printf(“(3) %6.4f\n”,a); printf(“(4) %lf\n”,b); printf(“(5) %14.3lf\n”,b); printf(“(6) %8.4lf\n”,b); printf(“(7) %.4f\n”,b); } 二、程序设计题

1、若a=3,b=4,c=5,x=1.2,y=2.4,u=51274,n=128765,c1=’a’,c2=’b’。想得到以下的输出格式和结果,请写出程序(包括定义变量类型和设计输出)。 要求输出的结果如下:□表示空格 a=□3□□b=□4□□c=□5 x=1.200000,y=2.400000,z=-3.600000 x+y=□3.60□□y+z=-1.20□□z+x=-2.40 u=□51274□□n=□□□128765 c1=’a’□or□97(ASCII) c2=’B’□or□98(ASCII) 2、假定有:int a=1,b=2,设计一个程序,将变量a和b的值互换并且输出来。 3、已知变量a=23. 4、b=54.33、c=434.33,求它们的平均值。并按如下形式输出: average of **、** and ** is **.** 。

C语言实验报告范文

实验名称:指针及其应用日期:得分:指导老师: 专业:班次:姓名:学号: 实验目的 (1)掌握变量的指针及其基本用法。 (2)掌握一维数组的指针及其基本用法。 (3)掌握指针变量作为函数的参数时,参数的传递过程及其用法。一.实验内容( 1)运行以下程序,并从中了解变量的指针和指针变量的概念。 (2)运行以下程序,观察 &a[0] 、&a[i] 和 p 的变化,然后回答以下问题: 1.程序的功能是什么? 2.在开始进入循环体之前, p 指向谁? 3.循环每增加一次, p 的值(地址)增加多少?它指向谁? 4.退出循环后, p 指向谁? 5.你是否初步掌握了通过指针变量引用数组元素的方法?( 3)先分析以下程序的运行结果,然后上机验证,并通过此例掌握通过指针变量引用数组元素的各种方法。 ( 4)编写函数,将 n 个数按原来的顺序的逆序排列(要求用指针实现),然后编写主函数完成: ①输入 10 个数; ②调用此函数进行重排; ③输出重排后的结果。 二.分析与讨论 ( 1)指针的定义方法,指针和变量的关系。 定义方法: 数据类型 * 指针变量名 ; 如定义一个指向 int 型变量的指针—— int *p; 则我们可以继续写如下代码—— int a = 4; p = &a; printf("%d", *p); 在这里,我们定义了一个变量a,我们把它理解为内存空间连续的 4个字节(int 型占用 4字节),则这 4个字节的空间保存着一个数 4。&是取地址符号,即把变量a的地址(即这4个字节的首地址)赋给指针p (记住指针p的类型和变量a的类型要保持一致,否则的话,要进行类型转换)。这样子,指针p就保 存着变量a的地址。我们如果把指针p当做内存空间里面另外一个连续的 4个字节,那么这4个字节保存的数就是变量a的地址。printf("%d",*p)和 printf("%d",a)的结果是一样的。这里的*是取变量符号(与&刚好作用相反,通过变量的地址找到变量),与定义时 int *p 的*号作用不同(定义时的 *表示该变量是个指针变量,而非是取它指向的变量)。 ( 2)数组和指针的关系。 指针与数组是 C 语言中很重要的两个概念,它们之间有着密切的关系,利用这种

C语言程序设计—指针—实验报告

实验报告 专业软件工程班级X 班学号_ _ 姓名 实验日期:201X年X月X日报告退发(订正、重做) 课程C程序设计实验实验名称指针 一、实验目的 二、实验环境(描述实验的软件、硬件环境) ①软件环境:windows xp/win7等操作系统,Microsoft Visual C++ 6.0编译器; ②硬件环境:PC机一台 三、实验内容、步骤和结果分析 题目一:输入3个整数,按由小到大的顺序输出 要求: 使用指针方法实现; #include void function(int *a, int *b) { int temp; if (*a<*b) { temp = *a; *a = *b; *b = temp; } } int main() { int a, b, c; int *p1, *p2, *p3; p1 = &a; p2 = &b; p3 = &c; scanf("%d%d%d", &a, &b, &c); function(p1, p2); function(p1, p3); function(p2, p3); printf("%d %d %d\n", *p3, *p2, *p1); return 0;

题目二:将长度为10的整型数组arr中的元素按照从小到大排列并输出 要求: 使用指针方法实现; #include int main() { struct METRIC { float m; float cm; } m1, m2; struct BRITISH{ float foot; float inches; } b1, b2; printf("Enter the info of m1(米,厘米):"); scanf("%f%f", &m1.m, &m1.cm); printf("Enter the info of m2(米,厘米):"); scanf("%f%f", &m2.m, &m2.cm); printf("\nEnter the info of m2(英尺,英寸):"); scanf("%f%f", &b1.foot, &b1.inches); printf("Enter the info of m2(英尺,英寸):"); scanf("%f%f", &b2.foot, &b2.inches); printf("\nSum of m1 and m2 is:%.2f(厘米)\n", (m1.m + m2.m) * 100 + m1.cm + m2.cm); printf("Sum of b1 and b2 is:%.2f(厘米)\n\n", (b1.inches + b2.inches)*30.48 + (b1.foot + b2.foot)*2.54); return 0;

相关文档