要多难的C++还是C??
#include
#include
using namespace std;
struct Student
{
int No;
float Grade1;
float Grade2;
float Grade3;
float AverGrade;
float SumGrade;
Student* next;
};
int n;
bool flag;
Student* Create()
{
flag = true;
Student* head;
Student* p1;
Student* p2;
n = 0;
cout<<"Now,begin create the new list:"<
cout<<"Please input data:"<
p1->AverGrade = (float)(p1->Grade1+p1->Grade2+p1->Grade3)/3;
p1->SumGrade = (float)(p1->Grade1+p1->Grade2+p1->Grade3);
head = NULL;
while(p1->No != -1)
{
n++;
if(n == 1)
{
head = p1;
}
else
{
p2->next = p1;
}
p2 = p1;
p1 = (Student*)malloc(sizeof(Student));
cout<<"Please input data:"<
p1->AverGrade = (float)(p1->Grade1+p1->Grade2+p1->Grade3)/3;
p1->SumGrade = (float)(p1->Grade1+p1->Grade2+p1->Grade3);
}
delete p1;
p2->next =NULL;
return head;
}
Student* Delete(Student* head,int num)
{
Student *p1;
Student *p2;
if (head == NULL)
{
cout<<"this list is not exist"<
else
{
p1 = head;
while(p1->No != num && p1->next !=NULL)
{
p2 = p1;
p1 = p1->next;
}
if(p1->No == num )
{
if(p1 == head)
{
head = p1->next;
}
else
{
p2->next = p1->next;
}
cout<<"the number "<
if(n == 0)
{
flag = false;
}
}
else
{
cout<<"the number "<
}
return head;
}
Student* Insert(Student* head,int num)
{
Student* p;
Student* p2;
p2 = (Student* )malloc(sizeof(Student));
p2->No = num;
p = head;
if(head == NULL)
{
head->next = p2;
}
else
{
while(p->next != NULL)
{
p = p->next;
}
p->next = p2;
}
p2->next = NULL;
n++;
return head;
}
void print(Student* head)
{
Student* p;
if (head == NULL)
{
cout<<"this list is not exist"<
else
{
cout<<"these "<
do
{
cout<
<
<
}while(p != NULL);
}
}
void print_Sum(Student* head, float left,float right)
{
Student* p;
if (head == NULL)
{
cout<<"this list is not exist"<
else
{
cout<<"these "<
do
{
if (p->SumGrade>=left && right>=p->SumGrade)
{
cout<
<
<
p = p->next;
}while(p != NULL);
}
}
void DisplayInfo()
{
cout<<"========= the list operations .please select:======================="<
int main()
{
Student* Head;
float left,right;
int num,select,insertnum;
Head = NULL;
DisplayInfo();
cin>>select;
while (select !=6)
{
switch(select)
{
case 1:
Head = Create();
break;
case 2:
cout<<"please input the delete number:"<
Head = Delete(Head,num);
break;
case 3:
print(Head);
break;
case 4:
cout<<"Please input the need insert data:"<
Head = Insert(Head,insertnum);
break;
case 5:
cout<<"Please input the the left number and the right number:"<
print_Sum(Head,left,right);
break;
case 6:
cout<<"Exit the operation"<
default:
{
cout<<"Your select is error,please reset select:"<
break;
}
DisplayInfo();
cin>>select;
}
delete Head;
Head = NULL;
return 0;
}
给,C语言作的,已经编译运行确认:
#include
#include
#include
#define NN 50 // 是学生人数上限 。可自己调整。
#define SCMAX 50 // 课程 数上限
int SCLoad;
typedef struct student{
char no[10];
char name[11];
double score[SCMAX];
double total;
double avg;
};
student stu[NN+1];
int display(int n,int m,int SC) //综合显示函数。
{
int i,j,temp,flag=0;
temp=n;
n>m?n=m,m=temp:1 ;
printf(" 序 号 | 学 号 | 姓 名 |");
for(j=1;j<=SC;j++)
printf("课程%3d ",j);
printf("| 总 分 | 平均分 |\n");
if((n==0&&m==0) || (!n&&!m))n=1,m=1;
if((n==0&&m!=0) || (n!=0&&m==0))
{ n=0?n=m:m=n;flag=stu[n].total==0?1:0;}
if(stu[1].total==0 || flag){printf("没有学生信息!");return 0;}
for(i=n;i<=m;i++)
if(stu[i].total>0)
{ printf(" %5d |%6s |%6s |",i,stu[i].no,stu[i].name);
for(j=0;j
printf("|%6.2lf |%6.2lf |\n",stu[i].total,stu[i].avg);
}
}
int searchmax(int n,int SC) //找最高分,并输出这个学生信息。
{ int i,j,k=1;
double max;
max=0;
for(i=1;i<=n;i++)
for(j=0;j
max=stu[i].score[j];
k=i;
}
display(k,k,SC);
printf(">>>最高分数:%3.1lf\n",max);
return k;
}
double avgsi(int n,int scorenum) //求 单科平均分
{ int i,j;
double s=0,avg;
for(i=1;i<=n;i++)s+=stu[i].score[scorenum-1];
return s/n;
}
int input(int i,int SC) //信息录入
{ int j;
flag: printf("输入第%d个学生学号:",i);
scanf("%s",stu[i].no);
printf("输入第%d个学生姓名:",i);
fflush(stdin);
gets(stu[i].name);
printf("输入第%d个学生成绩依次为(1 2 3....)空格隔开\n ",i);
stu[i].total=0;
stu[i].avg=0;
for(j=0;j
//
stu[i].total+=stu[i].score[j];
}
stu[i].avg=stu[i].total/SC;
if(stu[i].no&&stu[i].name&&stu[i].total&&stu[i].avg)return 1;
else {printf("输入信息不完整,请重新输入!");goto flag;}
return 1;
}
void sortbyavg(int n) //按平均分高到低排序
{
int i,j;
student temp;
for(i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
if(stu[i].avg
stu[i]=stu[j];
stu[j]=temp;
}
printf("排序完成!");
return;
}
int sortbyscid(int n,int SC) //按输入的课程名,分数从高到低排序
{
int i,j,k;
int p;
student temp;
printf("请输入排序参考课程id:");
scanf("%d",&p);
if(p>SC||p<=0){printf("无法排序,原因没有这门课!\n");
return 0;}
for(i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
if(stu[i].score[p-1]
printf("排序完成!");
return 1;
}
void displaysample(int n) //显示总分和平均分
{ int i;
printf(" 序 号 | 姓 名 | 总 分 | 平均分 |\n");
for(i=1;i<=n;i++)
if(stu[i].total!=0)printf(" %5d %7s %7.2lf %7.2lf\n",i,stu[i].name,stu[i].total,stu[i].avg);
return;
}
void dispsavg(int n) //显示各科的平均分
{ int i;
double k;
for(i=1;i<=SCMAX;i++){
k=avgsi(n,i);
if(k==0)return;
printf("<课程%2d > 平均分:%.2lf\n",i,k);
}
}
void help()
{
printf(" ====================欢迎使用本程序==================== \n");
printf(" 以下是功能菜单\n");
printf(" 在提示符\">>\"下有以下命令可用!\n");
printf(" A 显示所有信息;\n B 显示总分,平均分;\n C 平均分排序;\n D 显示各科平均分;\n");
printf(" E 按输入课程名排序;\n F 找最高分学生输出;\n S 将信息保存到文件;\n O 打开已存在的数据文件;\n");
printf(" I 学生信息输入;\n H 显示本帮助。\n Z 结束程序!\n");
printf("\n>>");}
int save(int n) //将信息保存在一个文件中。
{
FILE *fp;
int i;
char stu_msg[20];
printf("输入要保存的文件名:");
scanf("%s",stu_msg);
if((fp=fopen(stu_msg,"w"))==NULL)
{
printf("无法打开文件!\n");
return 0;
}
for(i=1;i<=n;i++)
if(fwrite(&stu[i],sizeof(student),1,fp)!=1)
printf("文件写入错误!");
fclose(fp);
printf("保存成功!");
}
int openf() //文件放在程序同目录下。
{
FILE *fp;
int i;
char stu_msg[20];
printf("输入要打开的文件名:");
scanf("%s",stu_msg);
if((fp=fopen(stu_msg,"r"))==NULL)
{printf("无法打开文件!\n");return 0;}
for(i=1;i<=NN;i++)
if(fread(&stu[i],sizeof(student),1,fp)!=1 )
{ SCLoad=0;
while(stu[1].score[SCLoad++]!=0);
printf("读入结束!\n");
return i;}
fclose(fp);
}
int main()
{
int i,j,n;
int SC;
char cc,runfirst;
printf(" ======================================================== \n");
printf(" ==== 班级成绩管理系统 ==== \n");
printf(" ======================================================== \n");
printf("\n\n是否从已有文件中获得学生信息?Y/N:");
runfirst=getchar();
if(runfirst=='Y'||runfirst=='y'){n=openf();SC=SCLoad-1;}
else {
flag: printf("输入学生人数( <=%d ):",NN);
scanf("%d",&n);
printf("输入课程数( <=%d ):",SCMAX);
scanf("%d",&SC);
while(n>NN){
printf("超出允许范围(<=%d),请重新输入:",NN);
scanf("%d",&n);}
for(i=1;i<=n;i++)input(i,SC);
printf("输入完毕!\n");
}
system("pause");
system("cls");
printf("\n\n\n");
help();
printf("你想进行什么操作?\n");
printf(">>");
while(cc=getchar())
{
switch(cc)
{
case 'I':case 'i':goto flag;break;
case 'A':case 'a':display(1,n,SC);break;
case 'B':case 'b':displaysample(n);break;
case 'C':case 'c':sortbyavg(n);break;
case 'D':case 'd':dispsavg(n);break;
case 'E':case 'e':sortbyscid(n,SC);break;
case 'F':case 'f':searchmax(n,SC);break;
case 'H':case 'h':help();break;
case 'S':case 's':save(n);break;
case 'O':case 'o':n=openf();SC=SCLoad-1;break;
case 'Z':case 'z':printf("谢谢!\n");return 0;
}
printf("\n>>");
fflush(stdin);
}
return 0;
}