鱼C论坛

 找回密码
 立即注册
查看: 3558|回复: 10

结构体问题,大侠帮忙

[复制链接]
发表于 2012-3-25 21:15:50 | 显示全部楼层 |阅读模式
6鱼币
#include <iostream>
using namespace std;
struct stu
{int no;
float eng,math,phy,sum;

}
float sum(stu s[],int n)
{for(int i=0;i<n;i++)
s[i].sum=s[i].eng+s[i].math+s[i].phy;
}
int rew(stu s[],int n)
{float temp;
for(int i=0;i<n;i++)
for(int j=0;j<n-i;j++)
if(s[j].sum>s[j+1].sum)
{temp=s[j].sum;
s[j].sum=s[j+1].sum;
s[j+1].sum=temp;
}
}
void main()
{stu s[3];
for(int i=0;i<3;i++)
cin>>s[i].no>>s[i].eng>>s[i].math>>s[i].phy;
rew(s,3);
for(int i=0;i<3;i++)
  cout<<s[i].no<<s[i].eng<<s[i].math<<s[i].phy<<s[i].sum;
system("pause");
}
能编译,但不能正确输出

最佳答案

小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-3-25 21:15:51 | 显示全部楼层
  1. #include <iostream>//程序没有注释,习惯不好-,-
  2. using namespace std;
  3. struct stu
  4. {
  5.         int no;
  6.         float eng,math,phy,sum;
  7. };//结构体结束要加";"
  8. void sum(stu s[],int n)//定义float没有返回值
  9. {
  10.         for(int i=0;i<n;i++)
  11.         s[i].sum=s[i].eng+s[i].math+s[i].phy;
  12. }
  13. /*void rew(stu s[],int n)//同理,ps:这是按照sum进行排序吧?如果是怎么能这么写呢,要换也要全部换。
  14. {
  15.         float temp;
  16.         for(int i=0;i<n;i++)
  17.         for(int j=0;j<n-i;j++)
  18.         if(s[j].sum>s[j+1].sum)
  19.         {
  20.                 temp=s[j].sum;
  21.                 s[j].sum=s[j+1].sum;
  22.                 s[j+1].sum=temp;
  23.         }
  24. }*/
  25. void rew(stu s[],int n)//帮你重新写了个sort
  26. {
  27.         stu temp;
  28.         for(int i=0;i<n;i++)
  29.         {
  30.                 for(int j=i+1;j<n;j++)
  31.                 {
  32.                         if(s[j].sum<s[i].sum)//从小到大
  33.                         {
  34.                                 temp=s[i];
  35.                                 s[i]=s[j];
  36.                                 s[j]=temp;
  37.                         }
  38.                 }
  39.         }
  40. }
  41. void main()
  42. {
  43.         stu s[3];
  44.         for(int i=0;i<3;i++)
  45.                 cin>>s[i].no>>s[i].eng>>s[i].math>>s[i].phy;
  46.         sum(s,3);//sum函数调用了有用到么?这是没结果的关键
  47.         rew(s,3);
  48.         for(i=0;i<3;i++)
  49.     cout<<s[i].no<<"\t"<<s[i].eng<<"\t"<<s[i].math<<"\t"<<s[i].phy<<"\t"<<s[i].sum<<endl;//另外输出格式最好能区分不同部分的。
  50.         system("pause");
  51. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-3-25 23:02:49 | 显示全部楼层
C++我不懂  不好意思 帮不了你
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-3-26 17:02:25 | 显示全部楼层
LZ 的习惯真心不好。况且你是来提问的,最好打好注释,简练一下代码吧。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-3-26 18:38:56 | 显示全部楼层
代码风格很糟,题目意思也不讲,没这么多功夫看代码。。就纠正了一些语法错误。。

#include <iostream>
using namespace std;
struct stu
{
        int no;
        float eng,math,phy,sum;
};
float sum(stu s[],int n)
{
        for(int i=0;i<n;i++)
        s[i].sum=s[i].eng+s[i].math+s[i].phy;
        return s[i].sum;
}
int rew(stu s[],int n)
{
        float temp;
        for(int i=0;i<n;i++)
        for(int j=0;j<n-i;j++)
        if(s[j].sum>s[j+1].sum)
        {
                temp=s[j].sum;
                s[j].sum=s[j+1].sum;
                s[j+1].sum=temp;
        }
        return s[i].sum;
}
void main()
{
        stu s[3];
        for(int i=0;i<3;i++)
        cin>>s[i].no>>s[i].eng>>s[i].math>>s[i].phy;
        rew(s,3);
        for(int j=0;j<3;j++)
        cout<<s[j].no<<s[j].eng<<s[j].math<<s[j].phy<<s[j].sum;
        system("pause");
}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-3-26 18:40:17 | 显示全部楼层
main()函数中重复定义了i,,我把它改成了j.
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-3-26 20:26:55 | 显示全部楼层
这个是我刚才修改的,可以满足要求。
include <iostream>
using namespace std;

struct stu
{
        int num;               
        float eng;      //这里应该分开写
        float math;
        float phy;
        float sum;
};

void sum(stu s[],int n)
{
        for(int i=0;i<n;i++)
        {
                s[i].sum=s[i].eng+s[i].math+s[i].phy;
        }
}

void sort(stu s[],int n)
{
        float temp = 0;
        for(int i = 0; i < n; i++)
        {
                for(int j=i;j<n - 1;j++)                    //这里是n - 1,不然多执行一次数组s[3]中存放的是不确定的数导致出错。
                {                                                      
                        if(s[i].sum<s[j+1].sum)                        //判断大小,不满足条件就交换整个结构的位置。
                        {
                                s[i]= s[j+1];                       
                        }
                }
        }
}

void main()
{
        stu s[3];     
        for(int i=0;i<3;i++)
        {
                cin>>s[i].num>>s[i].eng>>s[i].math>>s[i].phy;
        }

        sum(s,3);                    
        sort(s,3);

        for(int i=0;i<3;i++)
        {
                cout<<s[i].num<< '\t'<<s[i].eng<< '\t'<<s[i].math<< '\t'<<s[i].phy<< '\t'<<s[i].sum;     //按大到小顺序输出
                cout << endl;
        }
        system("pause");
}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-3-26 20:35:50 | 显示全部楼层
给个利用C++ STL 特性的代码:
先建立文件:  D:\data.txt,内容如下(表示1到5号学生的各门成绩):
1        89        34        19
3        99        98        22
2        19        17        12
5        66        78        99
4        22        55        66
程序代码:
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iterator>
  4. #include <algorithm>
  5. #include <vector>
  6. using namespace std;

  7. struct Stu{
  8.         int no;
  9.         float eng;
  10.         float math;
  11.         float phy;
  12.         float sum;
  13. };

  14. inline istream& operator>>(istream& in, Stu& s){
  15.         in>>s.no>>s.eng>>s.math>>s.phy;
  16.         s.sum =s.eng+s.math+s.phy;
  17.         return in;
  18. }

  19. inline ostream& operator<<(ostream& out, const Stu& s){
  20.         return out<<"Number:"<<s.no<<"\t"
  21.                 <<"Eng:"<<s.eng<<"\t"
  22.                 <<"Math:"<<s.math<<"\t"
  23.                 <<"Phy:"<<s.phy<<"\t"
  24.                 <<"Sum:"<<s.sum;
  25. }

  26. inline bool sum_less(const Stu& a, const Stu& b){
  27.         return a.sum<b.sum;
  28. }

  29. inline bool num_less(const Stu& a, const Stu& b){
  30.         return a.no<b.no;
  31. }

  32. inline bool math_less(const Stu& a, const Stu& b){
  33.         return a.math<b.math;
  34. }

  35. int main(){
  36.         vector<Stu> vec;
  37.         ifstream in("E:\\data.txt");
  38.         typedef istream_iterator<Stu> IsIT;
  39.         copy( IsIT(in),IsIT(), back_inserter(vec) );


  40.         cout<<"\n------------------sort by sum-----------------\n";
  41.         sort( vec.begin(), vec.end(), sum_less );
  42.         copy( vec.begin(), vec.end(),ostream_iterator<Stu>(cout,"\n") );

  43.         cout<<"\n------------------sort by no-----------------\n";
  44.         sort( vec.begin(), vec.end(), num_less );
  45.         copy( vec.begin(), vec.end(),ostream_iterator<Stu>(cout,"\n") );

  46.         cout<<"\n------------------sort by math-----------------\n";
  47.         sort( vec.begin(), vec.end(), math_less );
  48.         copy( vec.begin(), vec.end(),ostream_iterator<Stu>(cout,"\n") );
  49. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2012-3-26 22:21:16 | 显示全部楼层

太感谢了,由于只能选一个最佳答案,我就选了第一个回答出来的,抱歉,再次感谢哦。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2012-3-26 22:22:04 | 显示全部楼层

这么长的代码,真心感谢了。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-3-26 22:51:12 | 显示全部楼层
淡淡的幽香 发表于 2012-3-26 22:22
这么长的代码,真心感谢了。

呵呵,谢谢~不过lz代码风格要注意下了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-11-12 09:00

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表