C语言实验 结构体与文件程序设计

C语言实验 结构体与文件程序设计,第1张

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

#define Size 64

#include<stdio.h>

int main()

{

char buf[Size],fn1[]="poem1.txt",fn2[]="poem2.txt"

FILE *f1=NULL,*f2=NULL

f1=fopen(fn1,"r")

f2=fopen(fn2,"w+")

while(fgets(buf,Size,f1))

{

fputs(buf,f2)

}

fseek(f2,0,SEEK_SET)

while(fgets(buf,Size,f2))

{

printf("%s",buf)

}

fclose(f1)

fclose(f2)

return 0

}

#include <stdio.h>

#include <string.h>

#include <ctype.h>

#define MAXSIZE 36

struct Word {

char word[MAXSIZE]

unsigned size

}words[10000]

int n = 0

void Sort() {

// 没有时间写了

}

void Additive(char word[]) {

int i,flag = 1

for(i = 0 i < n && flag ++i) {

if(strcmp(words[i].word,word) == 0) {

++words[i].size

flag = 0

}

}

if(flag) {

strcpy(words[n].word,word)

words[n].size = 1

++n

}

}

int main() {

int i,ch

char word[MAXSIZE]

FILE *fin = fopen("case1.in","rt")

if(fin == NULL) {

printf("无法打开数据文件!\n")

return 1

}

i = 0

while((ch = fgetc(fin)) != EOF) {

if(isalpha(ch)) word[i++] = tolower(ch)

else if(i) {

word[i] = '\0'

i = 0

Additive(word)

}

}

if(i) {

word[i] = '\0'

Additive(word)

}

fclose(fin)

Sort()

return 0

}


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

原文地址: https://outofmemory.cn/tougao/11947699.html

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

发表评论

登录后才能评论

评论列表(0条)

保存