This presentation is the property of its rightful owner.
Sponsored Links
1 / 84

计算机实习 PowerPoint PPT Presentation


  • 65 Views
  • Uploaded on
  • Presentation posted in: General

计算机实习. 第 1 讲. 教学任务. 作品 [ 申请成绩为优 ] 考核(结构体、文件、链表) 平时的实验 实习无补考 (2 次课堂教学 ,2 次实验 ,1 次考核 ). 3. 3. 复习数组. 1. 3. 文件的基本操作. 归纳与提高. 2. 4. 结构体数组. 目 录. 1 .数组的基本概念. 数组. #include "stdio.h" void f1(int a[],int n) { int i; for(i=0;i<n;i++) { printf(" 输入第 %d 个数据 ",i+1);

Download Presentation

计算机实习

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


6247250

1


6247250

  • []

    • (2,2,1)


6247250

3

3

1

3

2

4


6247250

1

#include "stdio.h"

void f1(int a[],int n)

{ int i;

for(i=0;i<n;i++)

{ printf("%d\n",i+1);

scanf("%d",&a[i]);}}

main()

{ int x,i,s[50];

printf("\n");

scanf("%d",&x); f1(s,x);

for(i=0;i<x;i++)

printf("%3d",s[i]);

printf("\n");getch();

}

int a[100];


6247250

n1n2:

(int)((rand()/32768.0)*(n2-n1+1)+n1);


6247250

#include <stdio.h>

#include"stdlib.h"

#include "time.h"

#define N 100

void f1(int a[],int n1,int n2,int n)

{int i;

srand( (unsigned)time( NULL ) );

for (i=0; i<n;i++)

{a[i] =(int)((rand()/32768.0)*(n2-n1+1)+n1);

printf("%4d,",a[i]); }}


6247250

main()

{

int a[N];

f1(a,40,150,20);

}


6247250

:

excel,

FILE *fp;

fopen("cj.txt","r")

fscanf(fp,"%d",&x[i]);

fclose(fp);


6247250

#include <stdio.h>

#define SIZE 143

main()

{ int x[SIZE],i,max,min;

FILE *fp;

printf("Enter %d integers:\n",SIZE);

if((fp=fopen("cj.txt","r"))==NULL)

{

printf("");

getch();

exit(1);

}


6247250

for(i=0;i<SIZE;i++)

fscanf(fp,"%d",&x[i]);

fclose(fp);

max=min=x[0];

for(i=1;i<SIZE;i++)

{ if(max<x[i]) max=x[i];

if(min>x[i]) min=x[i];

}

printf("Maximum is %d\n",max);

printf("Minimum is %d\n",min);

}


6247250

C

,


6247250

1


6247250

    • ASCIIASCII


6247250

int10000

ASCII

0011000100110000001100000011000000110000

0010011100010000

0010011100010000

:

:


6247250

a


6247250

2

  • FILE

    • FILE

    • FILEstdio.h

typedef struct

{ int _fd; //

int _cleft; //

int _mode; //

char *_next; //

char *_buff; //

}FILE;


6247250

C

  • FILE *fp;

    • ,


6247250

r/rb ()

/

w/wb ()

/

a/ab ()

/

r+/rb+ ()

//

w+/wb+ ()

//

a+/ab+ ()

//

3

FILE *fp;

fp=fopen(aa.c,w);

if(fp==NULL)

{ printf(File open error!\n);

exit(0);

}

  • C,stdio.h

  • :-->/-->

    • ------ stdin

    • ------ stdout

    • ----- stderr

  • fopen

    • FILE *fopen(char *name,char *mode)

FILE *fp;

char *filename=c:\\fengyi\\bkc\\test.dat

fp= fopen(filename,r);

FILE *fp;

fp= fopen (c:\\fengyi\\bkc\\test.dat,r);

  • NULL


Fclose

a

fclose

fclose

  • :

  • int fclose(FILE *fp)

  • fp

  • 0;,0


6247250

4

  • I/O:fputcfgetc

    • fputc

      • int fputc(int c, FILE *fp)

      • cfp

      • c;EOF

  • fgetc

    • int fgetc(FILE *fp)

    • fp

    • ;EOF

I/OI/O

#define putc(ch,fp) fputc(ch,fp)

#define getc(fp) fgetc(fp)

#define putchar( c ) fputc(c,stdout)

#define getchar( ) fgetc(stdin)


6247250

#

#include <stdio.h>

main()

{ FILE *fp;

char ch,*filename=out.txt;

if((fp=fopen(filename,"w"))==NULL)

{ printf("cannot open file\n");

exit(0);

}

printf("Please input string:");

ch=getchar();

while(ch!='#')

{ fputc(ch,fp);

putchar(ch);

ch=getchar();

}

fclose(fp);

}

out.txtC


6247250

  • upper.txt!upper.txt


I o fread fwrite

I/O: freadfwrite

size_t fread(void *buffer,size_t size, size_t count,FILE *fp)

size_t fwrite(void *buffer,size_t size, size_t count,FILE *fp)

  • /

  • /0

    • typedef unsigned size_t;

    • buffer: /

    • size: /

    • count: /

    • fp: /

    • freadfwrite /


6247250

float f[2];

FILE *fp;

fp=fopen(aa.dat,rb);

fread(f,4,2,fp);

for(i=0;i<2;i++)

fread(&f[i],4,1,fp);

struct student

{ int num;

char name[20];

char sex;

int age;

float score[3];

}stud[10];

for(i=0;i<10;i++)

fread(&stud[i],sizeof(struct student),1,fp);


6247250

4

void save()

{ FILE *fp;

int i;

if((fp=fopen("d:\\fengyi\\exe\\stu_dat","wb"))==NULL)

{ printf("cannot open file\n");

return;

}

for(i=0;i<SIZE;i++)

if(fwrite(&stud[i],sizeof(struct student_type),1,fp)!=1)

printf("file write error\n");

fclose(fp);

}

void display()

{ FILE *fp;

int i;

if((fp=fopen("d:\\fengyi\\exe\\stu_dat","rb"))==NULL)

{ printf("cannot open file\n");

return;

}

for(i=0;i<SIZE;i++)

{ fread(&stud[i],sizeof(struct student_type),1,fp);

printf("%-10s %4d %4d %-15s\n",stud[i].name,

stud[i].num,stud[i].age,stud[i].addr);

}

fclose(fp);

}

#include <stdio.h>

#define SIZE 4

struct student_type

{ char name[10];

int num;

int age;

char addr[15];

}stud[SIZE];

main()

{

int i;

for(i=0;i<SIZE;i++)

scanf("%s%d%d%s",stud[i].name,&stud[i].num,

&stud[i].age,stud[i].addr);

save();

display();

}


6247250

#include <stdio.h>

main()

{ char s[80],c[80];

int a,b;

FILE *fp;

if((fp=fopen("test","w"))==NULL)

{ puts("can't open file"); exit() ; }

fscanf(stdin,"%s%d",s,&a);/*read from keaboard*/

fprintf(fp,"%s %d",s,a);/*write to file*/

fclose(fp);

if((fp=fopen("test","r"))==NULL)

{ puts("can't open file"); exit(); }

fscanf(fp,"%s%d",c,&b);/*read from file*/

fprintf(stdout,"%s %d",c,b);/*print to screen*/

fclose(fp);

}

int fprintf(FILE *fp,const char *format[,argument,])

int fscanf(FILE *fp,const char *format[,address,])

  • I/O:fprintffscanf

  • I/O

  • ,I/O;,EOF

fprintf(fp,%d,%6.2f,i,t); //it%d,%6.2ffp

fscanf(fp,%d,%f,&i,&t); //3,4.5 ,3i, 4.5t


I o fgets fputs

I/O: fgetsfputs

#include<stdio.h>

main()

{ FILE *fp;

char string[81];

if((fp=fopen("file.txt","w"))==NULL)

{ printf("cann't open file");exit(0); }

while(strlen(gets(string))>0)

{ fputs(string,fp);

fputs("\n",fp);

}

fclose(fp);

if((fp=fopen("file.txt","r"))==NULL)

{ printf("cann't open file");exit(0); }

while(fgets(string,81,fp)!=NULL)

fputs(string,stdout);

fclose(fp);

}

char *fgets(char *s,int n,FILE *fp)

int fputs(char *s,FILE *fp)

  • fp/

    • fgetsNULL

    • fputsEOF

fputssfp

fgetsfpn-1s,

\0

(n-1EOF)


6247250

5

    • -----

      • ~

      • ~

  • rewind

    • void rewind(FILE *fp)

#include <stdio.h>

main()

{ FILE *fp1,*fp2;

fp1=fopen("d:\\fengyi\\bkc\\ch12_4.c","r");

fp2=fopen("d:\\fengyi\\bkc\\ch12_41.c","w");

while(!feof(fp1)) putchar(fgetc(fp1));

rewind(fp1);

while(!feof(fp1)) fputc(fgetc(fp1),fp2);

fclose(fp1);

fclose(fp2);

}


Fseek

main()

{ int i;

FILE *fp;

if((fp=fopen("studat","rb"))==NULL)

{ printf("can't open file\n");exit(0); }

for(i=0;i<3;i+=2)

{ fseek(fp,i*sizeof(struct student_type),0);

fread(&stud[i],sizeof(struct student_type),1,fp);

printf("%s %d %d %s\n",

stud[i].name,stud[i].num,stud[i].age,stud[i].addr);

}

fclose(fp);

}

#include <stdio.h>

struct student_type

{ int num;

char name[10];

int age;

char addr[15];

}stud[3];

fseek

  • int fseek(FILE *fp,long offset,int whence)

  • 00

SEEK_SET 0

SEEK_CUR 1

SEEK_END 2

,)

>0

<0

  • ftell

    • long ftell(FILE *fp)

    • ()

    • -1L

fseek(fp,100L,0);

fseek(fp,50L,1);

fseek(fp,-10L,2);

313


6247250

6

  • ferror

    • int ferror(FILE *fp)

    • 00

      • ferror

      • fopenferror0


Clearerr

clearerr

#include <stdio.h>

int main(void)

{ FILE *stream;

stream = fopen("DUMMY.FIL", "w");

getc(stream);

if (ferror(stream))

{ printf("Error reading from DUMMY.FIL\n");

clearerr(stream);

}

if(!ferror(stream))

printf("Error indicator cleared!");

fclose(stream);

return 0;

}

  • void clearerr(FILE *fp)

  • 0

  • clearerr(fp)rewind

ferror()clearerr()


6247250

proc1()

{if((fp=fopen("book.txt","r"))==NULL)

{

printf("");

getch();

exit(1);

}

}


6247250

int proc2(BOOK a[])

{ int i=0;

while (!feof(fp))

{

fscanf(fp,"%s %s %s %d %d %d %f",a[i].bno,a[i].bname,a[i].cbadd,&a[i].bcdate.day,&a[i].bcdate.month,&a[i].bcdate.year,&a[i].bprice);

i++;

}

return i;fclose(fp);

}


6247250


6247250


6247250

3

3.6

int x=3;float y=3.6;

{10203040}

int a[10]={10203040}

{" zhang" , 27 , " li " , 18 , " wang " , 73} ;

struct man

{ char name[20] ;

int age ; };

struct man person [N] ={ " zhang" , 27 , " li " , 18 , " wang " , 73} ;


6247250

Name age sex address

wang 20 F 10#222

Zhang 19 M 16#321

Liu 18 M 16#324

zhao 20 F 10#242

50


6247250

1

,

struct

{ 1 1;

n n;};

(1)

struct student

{int num;

char name[20];

char sex;

float score;

};

2,;()

3.


6247250

(2)

struct address

{ char name[30]; /* */

char street[40]; /* */

char city[20]; /* */

char state[2]; /* */

unsigned long zip; /* ,*/

};

struct person

{char name[8]; /**/

char sex; /**/

int age; /**/

float height , weight; /**/

char add[40]; /**/

};

(3) :


6247250

(5) :

struct date

{int year; /**/

int month; /**/

int day; /**/

};

struct time

{int hh; /**/

int mm; /**/

int ss; /**/

};

(4) :

struct point2

{float x; /**/

float y; /**/

};

struct point3

{float x; /*X*/

float y; /*Y*/

float z; /*Z*/

};


6247250

(6) :

struct complex

{

float re; /**/

float ie; /**/

};

(7) :

struct sjx

{

float a b c; /**/

};


6247250

struct

{

};

.

1

struct student s1, s2;

student s1, s2

2

struct student

{int num;

char name[20];

char sex;

int age;

float score;

char addr[30]; }s1, s2;

3

struct

{

};

struct

{ int num; char name[20];

int age;

float score;

}s1, s2;


6247250

Name age sex address

wang 20 F 10#222

Zhang 19 M 16#321

Liu 18 M 16#324

zhao 20 F 10#242

(,)


6247250

struct student zhang; /**/

struct person p[100]; /*100*/

struct point2 p1 p2; /**/

struct point3 po1 po2; /**/


6247250

s1

num

name

age

score

(1).

(2)struct


6247250

3.,#include

#define STUDENT struct student

STUDENT

{int num;

char name[20];

int age;

float score;

};

STUDENT s1, s2;


6247250

  • .

    .

    s1.num=10001;

    1.

    2.


6247250

(1) struct studentzhang

zhang.nozhang.namezhang.sexzhang.age

(2) struct point2p1

p1.x p1.y

(3)

struct data

{ int month; int day; int year; };

struct stud

{ int num;

char name[20];

struct data birthday;

}stud1;

stud1.birthday.day12;


6247250

3.: s1.age=s2.age;

++s1.age;

4.

s1=s2;

s1={20102,Li Hao,20,10};


6247250

5.I/O

scanf(%d,&s1.num);

6.


6247250

struct student

{long int num;

char name[20];

char sex;

char addr[30];

}a={89031,li lin,m,123 beijing road};

0\00\0


6247250

1.

struct student

{long int num=89031;

char name[20]= li lin;

char sex=m;

char addr[30]= 123 beijing road;

}student1;


6247250

2

struct data

{ int month; int day; int year; };

struct stud

{ long int num;

char name[20];

char sex;

struct data birthday;;

char addr[30];

}stud1={89031,lilin,m,3,28,1988,beijing road};


6247250

main()

{ struct point

{ float x;

float y;

}p1, p2;

float d;

printf(": ");

scanf("%f, %f", &p1.x, &p1.y);

printf(": ");

scanf("%f, %f", &p2.x, &p2.y);

d=sqrt((p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));

printf("=%8.3f", d);

}


6247250

  • 1.,()

    struct student

    { int num;

    char name[20];

    float score;

    char addr[30];

    } ;

    struct student stu[3];

    56(2)


6247250

2.

struct student

{ int num; char name[20];

char sex; char addr[30];

}a[3]=

{{89031,lilin,m,123 beijing road},

{89032,zhangmin,f,123 beijing road},

{89033,he li,m,123 beijing road}};


6247250

3.1

3

[].

,

#include <string.h>

struct person

{ char name[20];

int count;

} leader[3]={li,0,zhang,0,fun,0};


6247250

main()

{ int i,j; char leader_name[20];

for (i=1;i<=10;i++)

{ scanf(%s,leader_name);

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

if(strcmp(leader_name,leader[j].name)==0)

leader[j].count++;

}

for (i=0;i<3;i++)

{ printf(%5s:,leader[i]. name);

printf(%d\n,,leader[i].count); }

}


6247250

.

(1) ()

(2)

4main()print

#define FORMAT %d\n%s\n%f\n%f\n%f\n

struct student

{ int num; char name[20]; float score[3]; };


6247250

main()

{ void print(struct student);

struct student stu;

stu.num=12345;

strcpy(stu.nam,li lin);

stu.score[0]=67.5; stu.score[1]=89;

stu.score[2]=78.6; print(stu);

}

void print(struct student stu)

{

printf(FORMAT,stu.num,stu.name, stu.score[0],

stu.acore[1],stu.score[2]);}


6247250

(3)

struct ()

{ ;

return();}


6247250

#include "string.h"

#include "stdio.h"

#define BOOK struct book

BOOK

{

char bno[10];

char bname[50];

struct date

{

int day;

int month;

int year;

}bcdate;

char cbadd[40];

float bprice;

};


6247250

void welcome()

{ printf("\n\t\t\t*****************************\n");

printf("\t\t\t* *\n");

printf("\t\t\t* *\n");

printf("\t\t\t* *\n");

printf("\t\t\t*****************************\n");

}


6247250

void mainmenu()

{

printf("\n\n\n");

printf("\t\t\t*******************************\n");

printf("\t\t\t* 1.---- *\n");

printf("\t\t\t* 2.---- *\n");

printf("\t\t\t* 3.---- *\n");

printf("\t\t\t*******************************\n");

}


6247250

void f1(BOOK a[],int n)

{

int i,b1,b2,b3;float x;

printf("\n");

for(i=0;i<n;i++)

{ printf("%d\n",i+1);

printf(" \n");scanf("%s",a[i].bno);puts(a[i].bno);getch();

scanf("%s",a[i].bname);puts(a[i].bname);getch();scanf("%s",a[i].cbadd);puts(a[i].cbadd);getch();

printf("\n");

scanf("%d%d%d",&b1,&b2,&b3);a[i].bcdate.day=b1;a[i].bcdate.month=b2;

a[i].bcdate.year=b3;

printf("\n"); scanf("%f",&x);

a[i].bprice=x;

}

}


6247250

void f2(BOOK a[],int n)

{ int i,j;BOOK temp;

printf("\n");

for(i=0;i<n-1;i++)

for(j=1;j<n-i;j++)

if(strcmp(a[j-1].bno,a[j].bno)>0)

{strcpy(temp.bno,a[j-1].bno);strcpy(temp.bname,a[j-1].bname);strcpy(temp.cbadd,a[j-1].cbadd);

strcpy(a[j-1].bno,a[j].bno);strcpy(a[j-1].bname,a[j].bname);strcpy(a[j-1].cbadd,a[j].cbadd);

strcpy(a[j].bno,temp.bno);strcpy(a[j].bname,temp.bname);strcpy(a[j].cbadd,temp.cbadd);

temp.bcdate.month=a[j-1].bcdate.month;

temp.bcdate.day=a[j-1].bcdate.day;

temp.bcdate.year=a[j-1].bcdate.year;

a[j-1].bcdate.month=a[j].bcdate.month;

a[j-1].bcdate.day=a[j].bcdate.day;

a[j-1].bcdate.year=a[j].bcdate.year;

a[j].bcdate.month=temp.bcdate.month;

a[j].bcdate.day=temp.bcdate.day;

a[j].bcdate.year=temp.bcdate.year;

temp.bprice=a[j-1].bprice; a[j-1].bprice=a[j].bprice;

a[j].bprice=temp.bprice; }}


6247250

void f3(BOOK a[],int n)

{

int i;

printf(" \n");

for(i=0;i<n;i++)

{printf("%s %s %s ",a[i].bno,a[i].bname,a[i].cbadd);

printf("%d-%d-%d %f\n",a[i].bcdate.day,a[i].bcdate.month,a[i].bcdate.year,a[i].bprice);}

getch();

}


6247250

void f3(BOOK a[],int n)

{

int i;

printf(" \n");

for(i=0;i<n;i++)

{printf("%s %s %s ",a[i].bno,a[i].bname,a[i].cbadd);

printf("%d-%d-%d %f\n",a[i].bcdate.day,a[i].bcdate.month,a[i].bcdate.year,a[i].bprice);}

getch();

}


6247250

void sele(BOOK a[],int n)

{

int k,m;char x[30];

while(1)

{system("cls");welcome();getch();system("cls");

mainmenu();

scanf("%d",&k);

while((k>4||k<1))

{system("cls");mainmenu();scanf("%d",&k);}

switch(k)

{case 1:

printf("\n");

scanf("%d",&m);

f1(a,m);f3(a,m);

break;

case 2:

f2(a,m);printf("\n");f3(a,m);

break;

case 3:printf("\n");exit(0);

}}

}


6247250

main()

{

BOOK s[20];

sele(s,20);

}


6247250

(1)

union data

{ int i; char ch; float f; }a,b,c;

(2)

union data

{ int i; char ch; float f; };

union a,b,c;

(3)

union

{ int i; char ch; float f; }a,b,c;


6247250

2.:

  • .

    . a.i a.ch

    1)


6247250

2)

union data

{ int i; char c; float f; }a;

main()

{ a.i=3; a.c=A; a.f=32;

printf(a.i=%d,a.c=%c,a.f=%f\n,a.i,a.c,a.f);

}

a.i=0,a.c= ,a.f=32.0000


6247250

3.

union data

{ int i; char c; float f; };

main()

{ union data a;

printf(&a=%d,&a.i=%d,&a.c=%d,&a.f=%d&a,&a.i,&a.c,&a.f);

}


6247250

4.

5.


6247250

/* */

union cf

{char clname[30]; /**/

int num; /**/

};

struct teachers

{int no; /**/

char name[12]; /**/

char zc; /**/

union cf x; /* */

} teach[3];


6247250

main( )

{ int i;

for(i=0; i<3; i++)

{ scanf("%d, %s,%c ", &teach[i].no,teach[i].name, &teach[i].zc);

if (teach[i].zc= ='L')/*L*/

scanf("%s" &teach[i].x .clname);

else if (teach[i].zc= ='P' )/*P*/

scanf ("%d" &teach[i].x .num);

else printf ("input data error\n");

}

for(i=0; i<3; i++)

{printf ("%d %s %c ", teach[i].no, teach[i].name,teach[i].zc);


6247250

if (teach[i].zc= ='L')printf ("%s\n" teach[i].x.. clname);

else if 'teach[i].name= ='P')printf ("%d\n", teach[i].x. num);

else printf("data error\n");

}

}


6247250

1.

A

B

C

D


6247250

2.,a

union u

{char st[4] ;

int i;

long l;

};

struct A

{int c;

union u;

}a;

A.4 B.5 C.6 D.8


6247250

3. struct fs

{ int fz; int fm; } x[ ]={{2,7}, {4,9}};

x[0].fz*x[1].fm+x[1].fz*x[0].fmx[0].fm*x[1].fm

4. struct student

{int num;char name[20];}st;

printfstnumname

0912000101


6247250


6247250

1

2


6247250


  • Login