C语言结构体函数问题 急

2024-11-24 11:48:21
推荐回答(2个)
回答1:

这个程序已经很简单了,我写的,选个好的编译器吧
原来是主函数中定义结构体数组出错了- -,汗,这下应该没问题了,都运行过很多次了:)

/*******************************************/
#include
#define N 5
struct st
{ char name[20];
float yuwen;
float English;
float sum;
};
void input(struct st a[N])

{
int i;
float x,y;
for(i=0;i { printf("input the name yuwen English:\n");
scanf("%s%f%f",a[i].name,&x,&y);/*这个地方我之前用a[i].yuwen,不行- -。*/
a[i].yuwen=x;
a[i].English=y;
a[i].sum=a[i].yuwen+a[i].English;
}
}
void zongfenjiangxu(struct st a[N])/*总分排序的*/
{int i,j;
struct st temp;
for(i=0;i for(j=N-1;j>i;j--)
{if(a[i].sum {temp=a[i];a[i]=a[j];a[j]=temp;

}
}
puts("-----------------------");
for(i=0;i printf(" %d : %s :%3.2f\n",i+1,a[i].name,a[i].sum);
puts("-----------------------");
}

void yuwenjiangxu(struct st a[N])/*按语文排序的*/
{int i,j;
struct st temp;
for(i=0;i for(j=N-1;j>i;j--)
{if(a[i].yuwen {temp=a[i];a[i]=a[j];a[j]=temp;

}
}
puts("-----------------------");
for(i=0;i printf(" %d : %s :%3.2f\n",i+1,a[i].name,a[i].yuwen);
puts("-----------------------");
}
void nameshengxu(struct st a[N])/*按名字排序的*/
{int i,j;
struct st temp;
for(i=0;i for(j=N-1;j>i;j--)
{if(strcmp(a[i].name,a[j].name)>=0)
{temp=a[i];a[i]=a[j];a[j]=temp;
}
}
puts("-----------------------");
for(i=0;i printf(" %d : %s \n",i+1,a[i].name);
puts("-----------------------");
}
void print(struct st a[N])/*打印函数*/
{int i;
puts("the result is :");
puts("-----------------------");
for(i=0;i printf(" %d : %s yuwen:%3.2f English %3.2f zongfen:%3.2f\n",i+1,a[i].name,a[i].yuwen,a[i].English,a[i].sum);
puts("-----------------------");
}

void main()
{
struct st a[N];
int i;
char sum,yuwen,name;
loop:
printf(" what do you want?\n");
printf("*****************************\n");
printf("\t1.input\n");
printf("\t2.zongfenpaixu\n");
printf("\t3.yuwenpaixu\n");
printf("\t4.namepaixu\n");
printf("\t5.print result\n");
printf("\t6.quit\n");
printf("*****************************\n");
scanf("%d",&i);
switch(i)
{
case 1:input(a);break;
case 2:zongfenjiangxu(a);break;
case 3:yuwenjiangxu(a);break;
case 4:nameshengxu(a);break;
case 5:print(a);break;
case 6:exit(1);break;
default:printf("input error\n");
} goto loop;

}

/*****************************************/

再给你发个前辈的吧。

===========================================================
/*编制一个学生成绩管理系统,每个学生信息包括:学号、姓名、C语言、高数和英语成绩。具体功能:
(1)创建信息链表并以磁盘文件保存;
(2)读取磁盘文件并显示输出所有学生的成绩;
(3)按学号或姓名查询成绩;
(4)添加成绩记录;
(5)修改指定姓名或学号的学生的成绩并可存盘;
(6)显示输出60分以下、60~79、80~89、90分以上各分数段的学生信息。以C语言成绩为例*/
#include
#include
#include
#include
#define N 20
typedef struct
{
char name[10];
int num;
float c;
float math;
float english;
float aver;
}stu;

void menu() /*便于操作员使用*/
{ /*清屏*/
system("cls");
printf(" 学生平时成绩管理\n");
printf("=======================\n");
printf(" 1.成绩录入\n");
printf(" 2.计算\n");
printf(" 3.查询\n");
printf(" 4.修改\n");
printf(" 5.插入\n");
printf(" 6.显示\n");
printf(" 7.统计\n");
printf(" 8.删除\n");
printf(" 0.退出\n");
printf("=======================\n");
printf("请选择相应的功能:");

}

void input()
{
FILE *fp;
stu xs; /*定义一个结构体变量来装从文件中读出来的数据*/
if((fp=fopen("student.txt","ab"))==NULL) {printf("can't open file!\n");exit(0);}
printf("\n录入格式: 姓名 学号 C语言 高数 英语 当输入姓名为ps结束录入\n");
while(1) /*中间加油break,可使循环中断*/
{
scanf("%s",xs.name);
if(strcmp(xs.name,"ps")==0) break;/*判断姓名是否为ps,用字符串判断*/
scanf("%d%f%f%f",&xs.num,&xs.c,&xs.math,&xs.english);
xs.aver=0;/*给平均分变量赋初值*/
fwrite(&xs,sizeof(stu),1,fp);
}
fclose(fp);
}
/*计算平均分*/
void sum()
{

int i=0,n;
stu xs[N];
FILE *fp;
if((fp=fopen("student.dat","rb+"))==NULL) {printf("can't open file!\n");exit(0);}
while(!feof(fp))/*文件读入内存,读取指针未到文件尾时返回值为0,读到尾时返回值为1,加!取反后,结束即为0*/
{
fread(&xs[i],sizeof(stu),1,fp);/*读入结构体变量*/
if(feof(fp)) break;/*上面while循环那个会多读一次因为读到尾时那个0还没返回,这里判断如果以读到文件尾,则直接停止,避免重复输入*/
xs[i].aver=(xs[i].c+xs[i].math+xs[i].english)/3;
i++;
}
n=i;/*把i的末尾值给n,用于下面循环写入*/
i=0;
fclose(fp);
if((fp=fopen("student.dat","wb"))==NULL) {printf("can't open file!\n");exit(0);}
while(i {
fwrite(&xs[i],sizeof(stu),1,fp);
i++;
}
fclose(fp);
}
void xingming()
{
FILE *fp;
stu xs;
char name[10];
printf("\n输入要查学生的姓名: ");scanf("%s",name);
if((fp=fopen("student.dat","rb"))==NULL) {printf("can't open file!\n");exit(0);}
while(!feof(fp))
{
fread(&xs,sizeof(stu),1,fp);
if(strcmp(xs.name,name)==0)/*按姓名查询时判断输入的姓名是否存在,会循环从数据中找取*/
{
printf(" 学生的信息如下:\n");
printf("\n%10s %6s %6s %6s %6s %6s\n","姓名","学号","C语言","高数"," 英语","平均成绩");
printf("%10s %6d %6.2f %6.2f %6.2f %6.2f\n",xs.name,xs.num,xs.c,xs.math,xs.english,xs.aver);
break;
}
}
if(feof(fp)) printf("查找失败!!!"); /*学生不存在*/
fclose(fp);
}
void xuehao()
{
FILE *fp;
stu xs;
int num;
printf("\n输入要查学生的学号: ");scanf("%d",&num);
if((fp=fopen("student.dat","rb"))==NULL) {printf("can't open file!\n");exit(0);}
while(!feof(fp))
{
fread(&xs,sizeof(stu),1,fp);
if(xs.num==num)/*按学号查询时判断输入的学号是否存在,会循环从数据中找取*/
{
printf(" 学生的信息如下:\n");
printf("\n%10s %6s %6s %6s %6s %6s\n","姓名","学号","C语言","高数"," 英语","平均成绩");
printf("%10s %6d %6.2f %6.2f %6.2f %6.2f\n",xs.name,xs.num,xs.c,xs.math,xs.english,xs.aver);
break;
}
}
if(feof(fp)) printf("查找失败!!!");
fclose(fp);
}
/*查询 */
void search()
{
char ch1;
system("cls");
printf("请输入按何种方式查询:\n");
printf("1-姓名,2-学号:");
ch1=getchar();/*用字符接收*/
switch(ch1)/*判断*/
{
case '1':xingming();break;
case '2':xuehao();break;
}
getch();/*让屏幕停留同时按任意键返回*/
}
/*修改:输入学生的姓名,查找该学生,若找到则修改该生的成绩信息,并显示修改前后的结果?/
void modi()
{
stu xs;
FILE *fp;
char name[10];
system("cls");
printf("\n输入要查学生的姓名: ");scanf("%s",name);
if((fp=fopen("student.dat","rb+"))==NULL) {printf("can't open file!\n");exit(0);}
while(!feof(fp))
{
fread(&xs,sizeof(stu),1,fp);
if(strcmp(xs.name,name)==0)/*判断输入要修改的姓名是否存在*/
{
printf(" 修改前学生的信息如下:\n");
printf("\n%10s %6s %6s %6s %6s %6s\n","姓名","学号","C语言","高数"," 英语","平均成绩");
printf("%10s %6d %6.2f %6.2f %6.2f %6.2f\n",xs.name,xs.num,xs.c,xs.math,xs.english,xs.aver);
printf("\n请输入该生的C语言成绩:");scanf("%f",&xs.c);
printf("\n高数成绩:");scanf("%f",&xs.math);
printf("\n英语成绩:");scanf("%f",&xs.english);
xs.aver=(xs.c+xs.math+xs.english)/3;
break;
}
}
if(!feof(fp))
{
printf(" \n 修改后的学生的信息如下:\n");
printf("\n%10s %6s %6s %6s %6s %6s\n","姓名","学号","C语言","高数"," 英语","平均成绩");
printf("%10s %6d %6.2f %6.2f %6.2f %6.2f\n",xs.name,xs.num,xs.c,xs.math,xs.english,xs.aver);
fseek(fp,-sizeof(stu),1);/*负号表示后退,1表示文件当前位置*/
fwrite(&xs,sizeof(stu),1,fp);/*再写入,同时把要修改的那个覆盖掉*/
}
else
printf("\n学生不存在,修改失败!!");
fclose(fp);
getch();
}
/*插入:输入学生的平时的各项成绩,插入到指定位置;当指定的位置超出范围时,将其插入到全班的最后,并显示插入成功后的结果?/
void insert()
{
int i=0,j,k,lenth;
FILE *fp;
stu s,xs[N];
system("cls");
printf("\n请输入插入的学生的姓名:");scanf("%s",&s.name);
printf("\n学生的学号:");scanf("%d",&s.num);
printf("\n学生的C成绩:");scanf("%f",&s.c);
printf("\n高数成绩:");scanf("%f",&s.math);
printf("\n英语成绩:");scanf("%f",&s.english);
s.aver=(s.c+s.math+s.english)/3;
printf("\n插入位置:");scanf("%d",&k);

if((fp=fopen("student.dat","rb"))==NULL) {printf("can't open file!\n");exit(0);}
while(!feof(fp))
{
fread(&xs[i],sizeof(stu),1,fp);/*把数据读入到结构体数组中*/
if(feof(fp)) break;
i++;
}
fclose(fp);
lenth=i;
if(k=0)/*k的位置要位于数组范围之内*/
{
for(j=lenth-1;j>=k;j--)
xs[j+1]=xs[j];/*一开始j是最大的,往后移一个这样移动避免数据被覆盖*/
/*循环是只循环这一句的*/
xs[k]=s;/*将输入的学生信息加入这个k位置*/
lenth++;/*长度+1*/

if((fp=fopen("student.dat","wb"))==NULL) {printf("不能建立文件!!\n");exit(0);}
for(i=0;i fwrite(&xs[i],sizeof(stu),1,fp);
}
else/*不再数组范围之内*/
{
if((fp=fopen("student.dat","ab"))==NULL) {printf("不能建立文件!!\n");exit(0);}
fwrite(&s,sizeof(stu),1,fp);
}
fclose(fp);
}

void print()
{
FILE *fp;
stu s;
system("cls");
if((fp=fopen("student.dat","rb"))==NULL) {printf("can't open file!\n");exit(0);}
printf("\n%10s %s %6s %6s %6s %6s\n","姓名","学号","C语言","高数"," 英语","平均成绩");
while(!feof(fp))
{
fread(&s,sizeof(stu),1,fp);
if(feof(fp)) break;
printf("%10s %6d %6.2f %6.2f %6.2f %6.2f\n",s.name,s.num,s.c,s.math,s.english,s.aver);
}
fclose(fp);
getch();
}

void tongji()
{

int i=0,j=0,k=0,l=0,m=0,n,p,q,r,t,u;
FILE *fp;
stu s[N], *p1[N],*p2[N],*p3[N],*p4[N];/*用指针数组*/
system("cls");
if((fp=fopen("student.dat","rb"))==NULL) {printf("cant't open file!\n");exit(0);}
while(!feof(fp))
{
fread(&s[i++],sizeof(stu),1,fp);/*读入数据*/
if(feof(fp)) break;
}
t=i;i=0;
for(u=0;u {
if(s[i].c<60) {p1[j++]=&s[i++];continue;}/*每个学生信息只能位于一个分数段*/
if(s[i].c>=60&&s[i].c<80) {p2[k++]=&s[i++];continue;}
if(s[i].c>=80&&s[i].c<90) {p3[l++]=&s[i++];continue;}
if(s[i].c>=90) {p4[m++]=&s[i++];continue;}
}
printf("不及格:姓名 学号 C语言\n");
for(n=0;n {
printf("%10s %6d %6.2f\n",p1[n]->name,p1[n]->num,p1[n]->c);
}
printf("60-79:姓名 学号 C语言\n");
for(p=0;p {
printf("%10s %6d %6.2f\n",p2[p]->name,p2[p]->num,p2[p]->c);
}
printf("80-89:姓名 学号 C语言\n");
for(q=0;q {
printf("%10s %6d %6.2f\n",p3[q]->name,p3[q]->num,p3[q]->c);
}
printf("90以上:姓名 学号 C语言\n");
for(r=0;r {
printf("%10s %6d %6.2f\n",p4[r]->name,p4[r]->num,p4[r]->c);
}
fclose(fp);
getch();
}

void dele()
{
stu xs[N]; void print();/*删除前学生信息*/
FILE *fp;
char name[10];
int i=0,len,k=-1/*k<0是为后面if判断用的*/;
system("cls");
printf("\n输入要删除的学生姓名: ");scanf("%s",name);
if((fp=fopen("student.dat","rb+"))==NULL) {printf("can't open file!\n");exit(0);}
while(!feof(fp))
{
fread(&xs[i],sizeof(stu),1,fp);
if(feof(fp)) break;
if(strcmp(xs[i].name,name)==0) k=i;
i++;
}
fclose(fp);

len=i;/*结构体数组上限*/
if(k>=0)/*k>0说明找到删除学生位置*/
{
if((fp=fopen("student.dat","wb+"))==NULL) {printf("can't open file!\n");exit(0);}
for(i=0;i {
if(i==k) continue;/*从这个位置开始写入*/
fwrite(&xs[i],sizeof(stu),1,fp);
}
fclose(fp);
}
else printf("删除失败!!!\n"); /*失败后重新返回菜单*/
print();
}

void main()
{
char ch;
while(1)
{
menu();
ch=getch();
if(ch=='0') break;
switch(ch)
{
case '1': input();break;
case '2':sum();break;
case '3':search();break;
case '4':modi();break;
case '5':insert();break;
case '6':print( );break;
case '7':tongji();break;
case '8':dele();break;
}
}
printf("\n欢迎使用!!!\n");
}

回答2:

#include"stdio.h"
struct
ymd
{
int
year;
int
month;
int
day;
};
int
day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int
days(struct
ymd
*p)
{
int
i,d=p->day;
if(p->year%4==0&&p->year%100!=0||p->year%400==0)
day[2]=29;
for(i=1;imonth;i++)//这里把i写成了1真无语了
d=d+day[i];
return
d;
}
void
main()
{
struct
ymd
date;
printf("输入年份:");
scanf("%d",&date.year);
printf("输入月份:");
scanf("%d",&date.month);
printf("输入日期:");
scanf("%d",&date.day);
int
d=days(&date);
printf("\n该天在本年的第%d天",d);
}