C语言结构体编程

C语言结构体编程,第1张

1、struct REC

{int n char c}

REC t1,t2//修改 struct REC t1,t2

因为不是所有编译器都支持你那种写法

2、

typedef struct REC

{int n=0char c=’A’}t1,t2

REC t1,t2

不知道你什么意思,不过可以这样写

typedef struct

{int n=0char c=’A’}

REC

REC t1,t2

表示将这个结构体重命名为REC,否则使用这类型的时候需加struct(特别是C编译器)

3、struct

{int nchar c}REC

这种方式编译应该通过的,不过是将REC认为是变量,而不是类型

总之你要定义一个t1或者t2的变量,常用以下几种写法

struct

{

......

}t1,t2

struct REC

{

....

}

struct REC t1,t2

typedef struct

{

.....

} REC

REC t1,t2

希望你能理解原理哈

//main.c文件

#include<stdio.h>

#include<stdlib.h>

#define BufLen 20

#define KeChengCount 3

typedef struct tagStuInfor

{

char xh[BufLen]//学号

char xm[BufLen]//姓名

char kchm[KeChengCount][BufLen]//课程名

float pshchj[KeChengCount]//平时成绩

float kshchj[KeChengCount]//考试成绩

float zpchj[KeChengCount]//总评成绩

float zf

}StudentInformation

typedef struct tagBookInfor

{

char shm[BufLen]//书名

float dj//单价

}BookInformation

void shurustu()

void zongfen()

void shuchumaxmin()

void sortbook()

void freeall()

//学生数,书数

int xshsh,shsh

StudentInformation *ptstu=NULL

BookInformation *ptbook=NULL

int main()

{

char in[]="data.txt",out[]="T_data.txt"

int i

float chjs[6],zf

FILE *fin,*fout

printf("250这个数字太不好了,能换成350吗?\n\n")

shurustu()

zongfen()

shuchumaxmin()

printf("请输入书数:")

scanf("%d",&shsh)

ptbook=(BookInformation*)calloc(shsh,sizeof(BookInformation))

for(i=0i<shshi++)

{

printf("请输入第%d本书的书名:",i+1)

scanf("%s",ptbook[i].shm)

printf("请输入第%d本书的单价:",i+1)

scanf("%f",&ptbook[i].dj)

}

printf("\n")

sortbook()

printf("按书的单价升序排序后的结果:\n书名\t单价\n")

for(i=0i<shshi++)

{

printf("%s\t%.2f\n",ptbook[i].shm,ptbook[i].dj)

}

fin=fopen(in,"r")

fout=fopen(out,"w")

while(!feof(fin))

{

for(i=0i<6i++)

{

fscanf(fin,"%f",&chjs[i])

}

zf=0.2*(chjs[0]+chjs[1]+chjs[2])+0.8*(chjs[3]+chjs[4]+chjs[5])

printf("%.2f\n",zf)

fprintf(fout,"%.2f\r\n",zf)

}

printf("\n")

fclose(fin)

fclose(fout)

freeall()

system("PAUSE")

return EXIT_SUCCESS

}

void shurustu()

{

int i

printf("请输入学生的个数:")

scanf("%d",&xshsh)

ptstu=(StudentInformation*)calloc(xshsh,sizeof(StudentInformation))

for(i=0i<xshshi++)

{

strcpy(ptstu[i].kchm[0],"C语言")

strcpy(ptstu[i].kchm[1],"高等数学")

strcpy(ptstu[i].kchm[2],"英语")

printf("请输入第%d个学生的学号:",i+1)

scanf("%s",ptstu[i].xh)

printf("请输入第%d个学生的姓名:",i+1)

scanf("%s",ptstu[i].xm)

printf("请输入第%d个学生的C语言课程的平时成绩和考试成绩:",i+1)

scanf("%f %f",&ptstu[i].pshchj[0],&ptstu[i].kshchj[0])

printf("请输入第%d个学生的高等数学课程的平时成绩和考试成绩:",i+1)

scanf("%f %f",&ptstu[i].pshchj[1],&ptstu[i].kshchj[1])

printf("请输入第%d个学生的英语课程的平时成绩和考试成绩:",i+1)

scanf("%f %f",&ptstu[i].pshchj[2],&ptstu[i].kshchj[2])

}

printf("\n")

}

void zongfen()

{

int i

for(i=0i<xshshi++)

{

ptstu[i].zpchj[0]=0.2*ptstu[i].pshchj[0]+0.8*ptstu[i].kshchj[0]

ptstu[i].zpchj[1]=0.2*ptstu[i].pshchj[1]+0.8*ptstu[i].kshchj[1]

ptstu[i].zpchj[2]=0.2*ptstu[i].pshchj[2]+0.8*ptstu[i].kshchj[2]

ptstu[i].zf=ptstu[i].zpchj[0]+ptstu[i].zpchj[1]+ptstu[i].zpchj[2]

printf("%s的%s,%s,%s的总评成绩是:",ptstu[i].xm,ptstu[i].kchm[0],ptstu[i].kchm[1],ptstu[i].kchm[2])

printf("%.2f,%.2f,%.2f\n",ptstu[i].zpchj[0],ptstu[i].zpchj[1],ptstu[i].zpchj[2])

printf("%s的总分是:%.2f\n",ptstu[i].xm,ptstu[i].zf)

}

printf("\n")

}

void shuchumaxmin()

{

int i,min,max

min=max=0

for(i=1i<xshshi++)

{

if(ptstu[i].zf<ptstu[min].zf)

{

min=i

}

if(ptstu[i].zf>ptstu[max].zf)

{

max=i

}

}

printf("总分最高的学生的信息:\n学号\t姓名\t")

printf("%s(平时)\t%s(平时)\t%s(平时)",ptstu[max].kchm[0],ptstu[max].kchm[1],ptstu[max].kchm[2])

printf("\t%s(考试)\t%s(考试)\t%s(考试)\t总分\n",ptstu[max].kchm[0],ptstu[max].kchm[1],ptstu[max].kchm[2])

printf("%s\t%s\t",ptstu[max].xh,ptstu[max].xm)

printf("%.2f\t%.2f\t%.2f\t",ptstu[max].pshchj[0],ptstu[max].pshchj[1],ptstu[max].pshchj[2])

printf("%.2f\t%.2f\t%.2f\t%.2f\n\n",ptstu[max].kshchj[0],ptstu[max].kshchj[1],ptstu[max].kshchj[2],ptstu[max].zf)

printf("总分最低的学生的信息:\n学号\t姓名\t")

printf("%s(平时)\t%s(平时)\t%s(平时)",ptstu[min].kchm[0],ptstu[min].kchm[1],ptstu[min].kchm[2])

printf("\t%s(考试)\t%s(考试)\t%s(考试)\t总分\n",ptstu[min].kchm[0],ptstu[min].kchm[1],ptstu[min].kchm[2])

printf("%s\t%s\t",ptstu[min].xh,ptstu[min].xm)

printf("%.2f\t%.2f\t%.2f\t",ptstu[min].pshchj[0],ptstu[min].pshchj[1],ptstu[min].pshchj[2])

printf("%.2f\t%.2f\t%.2f\t%.2f\n\n",ptstu[min].kshchj[0],ptstu[min].kshchj[1],ptstu[min].kshchj[2],ptstu[min].zf)

}

void sortbook()

{

int i,j,min

BookInformation t

for(i=0i<shsh-1i++)

{

min=i

for(j=i+1j<shshj++)

{

if(ptbook[j].dj<ptbook[min].dj)

{

min=j

}

}

t=ptbook[i]

ptbook[i]=ptbook[min]

ptbook[min]=t

}

}

void freeall()

{

if(NULL!=ptstu)

{

free(ptstu)

}

if(NULL!=ptbook)

{

free(ptbook)

}

}

//data.txt文件

50 65 70 88 97 80

66 87 95 46 88 97

77 82 65 58 91 58

78 87 91 48 66 70


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/11863738.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存