| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 776 人关注过本帖
标题:C++ 编程
只看楼主 加入收藏
helloworldll
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2014-12-1
结帖率:100%
收藏
 问题点数:0 回复次数:4 
C++ 编程
#include <iostream>
#include <fstream>
#include <string>
 
using namespace std;
 
int main() {
    ifstream fin;
    int sumtime[50] = {0};
    int themax;
    string name[50];
    int time[50][50];
     
    fin.open("d:\\fish.txt");
     
    fin >> themax;
    for(int i=0; i<themax; i++) {
        fin >> name[i];
        for(int j=0; j<7; j++) {
            fin >> time[i][j];
            sumtime[i] += time[i][j];
        }
    }
    fin.close();
     
     
    for(i=0; i< themax; i++) {
        cout << name[i] << " ";
        for(int j=0; j<7; j++) {
            cout << time[i][j] << " ";
        }
        cout << sumtime[i] << endl;
    }
     
    for (i = 0; i<themax-1; i++) {
        int k = i;
        int minnum;
        for (int j=i+1; j<themax; j++) {
            minnum = sumtime[i];            
            if (sumtime[j] < sumtime[k]) {
                k = j;
            }
        }
        if(i != k) {
            swap(name[i], name[k]);
            swap(sumtime[i], sumtime[k]);
            for (int n=0; n<7; n++) {
                swap(time[i][n], time[k][n]);
            }
        }
    }
     
    cout << "******************" << endl;
    for(i=0; i< themax; i++) {
        cout << name[i] << " ";
        for(int j=0; j<7; j++) {
            cout << time[i][j] << " ";
        }
        cout << sumtime[i] << endl;
    }
     
    return 0;
}
 这是我自己写的一个程序,请问我如何把这个程序分为三个自己定义的函数,然后再主函数中直接调用。分为1:读入文件,将数据存储在数组;2:冒泡排序。3:输出结果。请在我程序的基础上修改一下,谢谢了。
搜索更多相关主题的帖子: include 
2014-12-04 17:35
tiucdccsgz
Rank: 2
等 级:论坛游民
威 望:2
帖 子:21
专家分:54
注 册:2014-12-14
收藏
得分:0 
嗯嗯,有必要帮助一下
程序代码:
#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

ifstream & fileopen(ifstream &fin,int &themax,string *name,int sumtime[],int time[][50])
{

      fin.open("d:\\fish.txt");

        fin >> themax;
        for(int i=0; i<themax; i++) {
        fin >> name[i];
        for(int j=0; j<7; j++) {
            fin >> time[i][j];
            sumtime[i] += time[i][j];
        }
    }
    fin.close();
}
void getorder(int sumtime[] ,int themax,string *name,int  time[][50])
{


    for(int i=0; i< themax; i++) {
        cout << name[i] << " ";
        for(int j=0; j<7; j++) {
            cout << time[i][j] << " ";
        }
        cout << sumtime[i] << endl;
    }

    for (int i = 0; i<themax-1; i++) {
        int k = i;
        int minnum;
        for (int j=i+1; j<themax; j++) {
            minnum = sumtime[i];
            if (sumtime[j] < sumtime[k]) {
                k = j;
            }
        }
        if(i != k) {
            swap(name[i], name[k]);
            swap(sumtime[i], sumtime[k]);
            for (int n=0; n<7; n++) {
                swap(time[i][n], time[k][n]);
            }
        }
    }

}
void out(int sumtime[] ,int themax,string *name,int  time[][50])
{
     cout << "******************" << endl;
    for(int i=0; i< themax; i++) {
        cout << name[i] << " ";
        for(int j=0; j<7; j++) {
            cout << time[i][j] << " ";
        }
        cout << sumtime[i] << endl;
    }
}
int main() {
    ifstream fin;
    int sumtime[50] = {0};
    int themax;
    string name[50];
    int time[50][50];

    fileopen( fin, themax, name, sumtime, time);
    getorder(sumtime ,themax,name,time);
    out(sumtime ,themax,name,time);



    return 0;
}
2014-12-14 12:07
tiucdccsgz
Rank: 2
等 级:论坛游民
威 望:2
帖 子:21
专家分:54
注 册:2014-12-14
收藏
得分:0 
呵呵忘了返回了哈
程序代码:
ifstream & fileopen(ifstream &fin,int &themax,string *name,int sumtime[],int time[][50])
{

      fin.open("d:\\fish.txt");

        fin >> themax;
        for(int i=0; i<themax; i++) {
        fin >> name[i];
        for(int j=0; j<7; j++) {
            fin >> time[i][j];
            sumtime[i] += time[i][j];
        }
    }
    fin.close();
    return fin;
}
2014-12-14 12:09
tiucdccsgz
Rank: 2
等 级:论坛游民
威 望:2
帖 子:21
专家分:54
注 册:2014-12-14
收藏
得分:0 
以上在CB中可以通过
2014-12-14 12:13
荒魁
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2015-1-15
收藏
得分:0 
2015-01-15 10:28
快速回复:C++ 编程
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012864 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved