CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  C/C++ >  C语言

吐血求教??急

楼主rosky(郁闷++)2003-06-01 18:16:44 在 C/C++ / C语言 提问

这是我的一个成绩排序的程序,我用define决定studnet数组的大小,写入文件,然后读出,排序  
  再写入文件,并显示。  
  但是老师要求用new动态分配内存,并用外排序,我不知怎么做,请指教?  
  #include<iostream.h>  
  #include<fstream.h>  
  #include<stdlib.h>  
  #include<iomanip.h>  
  #include<string.h>  
  #define   M   100  
   
  struct   studnet{  
  int   no;  
                  char   name[10];  
  int   degree;  
  };  
   
  void   main()  
  {  
  int   i=0;  
  int   j=0;  
          int   m=0;  
  int   t=0;  
  studnet   stud[M];  
  while(1)  
  {  
                  cout   <<   "请输入学号:   ";  
                  cin   >>   stud[t].no;  
                  if   (   stud[t].no   ==   0   )break;  
                  cout   <<   "请输入姓名:   ";  
                  cin   >>   stud[t].name;  
                  cout   <<   "请输入成绩:   ";  
                  cin   >>   stud[t].degree;  
  t++;  
  }  
   
  fstream   infile,outfile;  
  outfile.open("110.dat",ios::trunc|ios::out);  
  if(!outfile)  
  {  
  cout<<"can   not   open   the   file,l   am   sorry"<<endl;  
          abort();  
  }  
   
        //写入文件  
  for(i=0;i<t;i++)  
  {  
  outfile.write((char*)&stud[i],sizeof(stud[i]));  
                   
  }  
  outfile.close();  
  //显示文件  
  infile.open("110.dat",ios::in|ios::binary);  
  for(i=0;i<t;i++)  
  {  
                    infile.read((char*)&stud[i],sizeof(stud[i]));  
                    cout<<stud[i].no<<setw(10)<<stud[i].name   <<setw(10)<<stud[i].degree<<endl;  
  }  
            infile.close();  
   
  //按照学分的从低到高排序  
  cout<<"是否需要排序?需要输入1,输入其它数不排序"<<endl;  
  cout<<"情选择!"<<endl;  
  cin>>m;  
  if(m==1)  
  {  
  cout<<"按学分排序后:"<<endl;  
  for(i=0;i<t;i++)  
  for(j=t-1;j>i;j--)  
  {  
  if(stud[j].degree<stud[j-1].degree)  
  {  
   
  int   d;  
  int   n;  
  char   na[10];  
  n=stud[j].no;  
  stud[j].no=stud[j-1].no;  
  stud[j-1].no=n;                            
  d=stud[j].degree;  
  stud[j].degree=stud[j-1].degree;  
  stud[j-1].degree=d;                              
  strcpy(na,stud[j].name);          
  strcpy(stud[j].name,stud[j-1].name);                            
  strcpy(stud[j-1].name,na);  
  }  
  }  
  //重新写入文件  
  infile.open("110.dat",ios::trunc|ios::out);  
  cout<<"the   new   list   is:"<<endl;      
  for(i=0;i<t;i++)  
  {        
  outfile.write((char*)&stud[i],sizeof(stud[i]));  
  }  
  infile.close();  
  //显示文件  
  outfile.open("110.dat",ios::trunc|ios::out);  
  for(i=0;i<t;i++)  
  {                  
  outfile.read((char*)&stud[i],sizeof(stud[i]));    
  cout<<stud[i].no<<setw(10)<<stud[i].name   <<setw(10)<<stud[i].degree<<endl;  
  }  
  outfile.open("110.dat",ios::trunc|ios::in);  
  }  
  }  
  问题点数:0、回复次数:5Top

1 楼pzytony(507)回复于 2003-06-01 22:51:19 得分 0

 
  struct   student   a   =   new(student);  
   
  delete(a);  
  Top

2 楼rosky(郁闷++)回复于 2003-06-02 22:04:19 得分 0

noone   can   answer   ????Top

3 楼BinaryWorld(为实现中华软件产业自强而读书!)回复于 2003-06-02 23:23:22 得分 0

#include   <new.h>  
   
  type   *varname=new   varname;  
   
  delete   varname   or   delete   []   varname;  
   
  外排序?是不是选择排序,如下:  
   
  int   nArray[100];  
  ...输入代码  
  for(int   i=0;i<100;i++)  
  {  
  for(int   j=i;j<100;j++)  
  {  
  if(nArray[i]>nArray[j])  
  {  
  int   nTemp=nArray[i];  
  nArray[i]=nArray[j];  
  nArray[j]=nTemp;  
  }  
  }  
  }Top

4 楼treamboy(亲月)回复于 2003-06-03 10:13:35 得分 0

#define   M   100  
  ......  
  int   main()   {  
          ..............  
          studnet   stud[M];//不要固定申請100個可以用指針來作,每次添加一個學生動態new個不就可以了,最後插入連表.用連表來作不管甚麼排序都easy  
                                        //struct   *student     =   new   student();  
  }Top

5 楼purerain(rain)回复于 2003-06-03 19:28:07 得分 0

gzTop

相关问题

  • 吐血求助!!!!急急急急!!!!
  • 急急急!吐血送分!关于C++的文件操作
  • 紧急求救,吐血送分-----关于修改IE的问题
  • 这问题急死人,吐血而不可解决!
  • 求助,急的吐血了:有关打印问题
  • C#的BUG吗?真是吐血!我很菜?急需大侠看!
  • 吐血送分
  • **************~~~~~~~~~~~~~~~吐血问题~~~~~~~~**************
  • 吐血求教!
  • 吐血求救

关键词

  • 排序
  • narray
  • studnet
  • stud
  • varname
  • outfile
  • cout
  • define
  • include

得分解答快速导航

  • 帖主:rosky

相关链接

  • C/C++ Blog
  • C/C++类图书
  • C/C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo