- UID
- 32468
注册时间2007-6-1
阅读权限10
最后登录1970-1-1
周游历练
该用户从未签到
|
引自第八阁论坛:http://bbs.chinadbg.cn/
输入5个学生的成绩,计算出总分和平均分,按总分进行排序,然后打印输出。
要求用,结构体指针。有数学,语文,英语,物理,化学。5个科目。
不太清楚的可以到http://www.pconline.com.cn/pcedu/empolder/gj/c/0503/567930.html这里看看
freesoft:C++
- #include <iOStream>
- using namespace std;
- struct STU
- {
- int id;
- char name[20];
- int yuwen;
- int shuxue;
- int yingyu;
- int wule;
- int huaxue;
- float pingjunf;
- float zongfen;
- };
- void setdata(STU *s);
- void show(STU *s);
- void sort(STU *s);
- void jisuan(STU *s);
- void main()
- {
- STU stu[5];//声明一个结构体数组并依次调用函数进行赋值,计算,排序,显示。
- setdata(stu);
- jisuan(stu);
- sort(stu);
- show(stu);
- }
- void show(STU *s)//此函数用于显示信息
- {
- int i;
- cout<<"学号 姓名 语文 数学 英语 物理 化学 平均分 总分\n";
- for(i=0;i<5;i++)
- {
- cout
- <<s[i].id<<" "
- <<s[i].name<<" "
- <<s[i].yuwen<<" "
- <<s[i].shuxue<<" "
- <<s[i].yingyu<<" "
- <<s[i].wule<<" "
- <<s[i].huaxue<<" "
- <<s[i].zongfen<<" "
- <<s[i].pingjunf;
- }
- }
- void jisuan(STU *s)//此函数用于计算总分和平均分
- {
- int i;
- for(i=0;i<5;i++)
- {
- s[i].zongfen=(float)(s[i].yuwen+s[i].shuxue+s[i].yingyu+s[i].wule+s[i].huaxue);
- s[i].pingjunf=(float)(s[i].zongfen/5.0);
- }
- }
- void sort(STU *s)//此函数用于按总分进行排序
- {
- int i,j;
- STU temp;
- for(i=0;i<5;i++)
- {
- for(j=i+1;j<5;j++)
- {
- if(s[i].zongfen<s[j].zongfen)
- {
- temp=s[i];
- s[i]=s[j];
- s[j]=temp;
- }
- }
- }
- }
- void setdata(STU *s)//此函数用于添加数据
- {
- int i;
- cout<<"请输入5位学生的资料\n";
- for(i=0;i<5;i++)
- {
- cout<<"请输入第"<<i+1<<"学生的资料\n";
- cout<<"学号:";
- cin>>s[i].id;
- cout<<"姓名:";
- cin>>s[i].name;
- cout<<"语文:";
- cin>>s[i].yuwen;
- cout<<"数学:";
- cin>>s[i].shuxue;
- cout<<"英语:";
- cin>>s[i].yingyu;
- cout<<"物理:";
- cin>>s[i].wule;
- cout<<"化学:";
- cin>>s[i].huaxue;
- }
- }
-
复制代码
天圆地方:Delphi
- var
- Form1: TForm1;
- implementation
- {$R *.dfm}
- //*-----------user define-------*//
- Type
- TMYRecord=class(Tobject)
- Name:array[0..5] of ShortString;
- ShuXue:array[0..5] of real;
- YuWen:array[0..5] of real;
- YingYu:array[0..5] of real;
- WuLi:array[0..5] of real;
- HuaXue:array[0..5] of real;
- ZongFen:array[0..5] of real;
- PinJunFen:array[0..5] of real;
- private
- { Private declarations }
- function AverNum:boolean;
- public
- { Public declarations }
- Function SumNum:boolean;
- Function SortSum:boolean;
- end;
- var
- MyRecord:TMyRecord;
- Function TMyRecord.AverNum:boolean;
- var
- i:integer;
- begin
- For i:=0 to 4 do
- begin
- MyRecord.PinJunFen[i]:=(MyRecord.ShuXue[i]+MyRecord.YuWen[i]+MyRecord.YingYu[i]+MyRecord.WuLi[i]+MyRecord.HuaXue[i])/5;
- end;
- Result:=True;
- end;
- Function TMyRecord.SumNum:boolean;
- var
- i:integer;
- begin
- For i:=0 to 4 do
- begin
- MyRecord.ZongFen[i]:=MyRecord.ShuXue[i]+MyRecord.YuWen[i]+MyRecord.YingYu[i]+MyRecord.WuLi[i]+MyRecord.HuaXue[i];
- end;
- Result:=True;
- end;
- Function TMyRecord.SortSum:boolean;
- var
- i,j:Integer;
- begin
- For i:=0 to 4 do
- begin
- For j:=i+1 to 4 do
- begin
- if MyRecord.ZongFen[i]<MyRecord.ZongFen[j] then
- begin
- //change number
- MyRecord.ZongFen[5]:=MyRecord.ZongFen[i];
- MyRecord.ZongFen[i]:=MyRecord.ZongFen[j];
- MyRecord.ZongFen[j]:=MyRecord.ZongFen[5];
- MyRecord.ShuXue[5]:=MyRecord.ShuXue[i];
- MyRecord.ShuXue[i]:=MyRecord.ShuXue[j];
- MyRecord.ShuXue[j]:=MyRecord.ShuXue[5];
- MyRecord.YuWen[5]:=MyRecord.YuWen[i];
- MyRecord.YuWen[i]:=MyRecord.YuWen[j];
- MyRecord.YuWen[j]:=MyRecord.YuWen[5];
- MyRecord.YingYu[5]:=MyRecord.YingYu[i];
- MyRecord.YingYu[i]:=MyRecord.YingYu[j];
- MyRecord.YingYu[j]:=MyRecord.YingYu[5];
- MyRecord.WuLi[5]:=MyRecord.WuLi[i];
- MyRecord.WuLi[i]:=MyRecord.WuLi[j];
- MyRecord.WuLi[j]:=MyRecord.WuLi[5];
- MyRecord.HuaXue[5]:=MyRecord.HuaXue[i];
- MyRecord.HuaXue[i]:=MyRecord.HuaXue[j];
- MyRecord.HuaXue[j]:=MyRecord.HuaXue[5];
- MyRecord.PinJunFen[5]:=MyRecord.PinJunFen[i];
- MyRecord.PinJunFen[i]:=MyRecord.PinJunFen[j];
- MyRecord.PinJunFen[j]:=MyRecord.PinJunFen[5];
- MyRecord.Name[5]:=MyRecord.Name[i];
- MyRecord.Name[i]:=MyRecord.Name[j];
- MyRecord.Name[j]:=MyRecord.Name[5];
- end;
- end;
- end;
- Result:=True;
- end;
- procedure TForm1.Button1Click(Sender: TObject);
- var
- i:integer;
- begin
- MyRecord:=TMyRecord.Create;
- For i:=0 to 4 do
- begin
- MyRecord.Name[i]:=InputBox('输入','姓名:',' 张'+IntToStr(i+1));
- MyRecord.ShuXue[i]:=StrToFloat(InputBox('输入成绩','数学',FloatToStr(0.3+Random(100))));
- MyRecord.YuWen[i]:=StrToFloat(InputBox('输入成绩','语文',FloatToStr(0.5+Random(100))));
- MyRecord.YingYu[i]:=StrToFloat(InputBox('输入成绩','英语',FloatToStr(0.6+Random(100))));
- MyRecord.WuLi[i]:=StrToFloat(InputBox('输入成绩','物理',FloatToStr(0.8+Random(100))));
- MyRecord.HuaXue[i]:=StrToFloat(InputBox('输入成绩','化学',FloatToStr(0.9+Random(100))));
- end;
- MyRecord.AverNum;
- MyRecord.SumNum;
- MyRecord.SortSum;
- For i:=0 to 4 do
- begin
- ShowMessage('总分第'+IntToStr(i+1)+' 名:'+' 姓名:'+MyRecord.Name[i]+'数学:'+FloatToStr(MyRecord.ShuXue[i])+' 语文:'
- +FloatToStr(MyRecord.YuWen[i])+' 英语:'+FloatToStr(MyRecord.YingYu[i])
- +' 物理:'+FloatToStr(MyRecord.WuLi[i])+' 化学:'+FloatToStr(MyRecord.HuaXue[i])
- +' 总分:'+FloatToStr(MyRecord.ZongFen[i]));
- end;
- MyRecord.Free;
- end;
复制代码 |
|