标题:关于函数转化为类的问题。
只看楼主
bbb222
Rank: 2
等 级:论坛游民
帖 子:31
专家分:54
注 册:2012-11-17
结帖率:77.78%
已结贴  问题点数:20 回复次数:4 
关于函数转化为类的问题。
例如:
struct Employee{
    string name;
    int hour[8];
};

Employee workers[50];
我想在我的代码中使用类,代码如下,请问如何改动,可以和上面的例子不一样,只要转化就好。
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring>

using namespace std;

int in(int &num, int hour[][7], string name[]);
int * sum(int  hour[][7], int num);
int * sort(int * total, int num);
void out(int num, int * tot,  int * total,
         string * name, int hour[][7]);

int main(){
    int num;
    int hour[50][7];
    string name[50];
    //step 1:judge success or failure of open file and input data of ont file
    int file = in(num, hour, name);
    if(file == 0)
        return 0;
    //step 2:calculate the sum of hours
    int * total = sum(hour, num);
    //step 3:sort from small to large
    int * tot = sort(total, num);
    //step 4:output data to another file
    out(num, tot, total, name, hour);
    return 0;
}

int in(int &num, int hour[][7], string name[]){
    ifstream input;
    input.open("empdata4.txt");
    if(input.fail()){
        cout << "input file error" << endl;
        return 0;
    }
    input >> num;

    for(int i = 0; i < num; i++){
        input >> name[i];

        for(int j = 0; j < 7; j++)
            input >> hour[i][j];
    }
    input.close();
    return 1;
}

int * sum(int  hour[][7], int num){
    int * total = new int[num];

    for(int i = 0; i < num; i++){
        total[i] = 0;

        for(int j = 0; j < 7; j++)
            total[i] += hour[i][j];
    }
    return total;
}

int * sort(int * total, int num){
    int temp;
    int * result = new int[num];

    for(int i = 0; i < num; i++)
        result[i] = total[i];

    for(int i = 0; i < num - 1; i++)

        for(int j = 0; j < num - 1; j++)
            if(result[j] < result[j+1]){
                temp = result[j];
                result[j] = result[j+1];
                result[j+1] = temp;
            }
    return result;
}

void out(int num, int * tot,  int * total,
         string * name, int hour[][7]){
    ofstream output;
    output.open("data4.txt");
    output << "Employee Weekly Hours:" << endl << endl;
    output << setw(20) << left << "Name:"
           << setw(3) << "S" << setw(3) << "M"
           << setw(3) << "T" << setw(3) << "W"
           << setw(3) << "T" << setw(3) << "F"
           << setw(3) << "S" << setw(3) << "TTL"
           << endl << endl;

    for(int i = 0; i < num; i++)

    for(int j = 0; j < num; j++)
        if(tot[i] == total[j]){
            output << setw(18) << left << name[j] << " ";

            for(int k = 0; k < 7; k++)
                output << setw(2) << right << hour[j][k] << " " ;

            output << setw(4) << tot[i] << endl;
            total[j] = -1;
            break;
        }
    output.close();
}
搜索更多相关主题的帖子: total include 如何 
2012-11-18 18:10
w527705090
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:11
帖 子:441
专家分:1882
注 册:2011-6-28
得分:10 
蛮复杂的。。。。等待大神解答。。。

有心者,千方百计;无心者,千难万难。
2012-11-19 00:36
bbb222
Rank: 2
等 级:论坛游民
帖 子:31
专家分:54
注 册:2012-11-17
得分:0 
回复 2楼 w527705090
等了老久了...还未见人解答...
2012-11-19 20:26
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
得分:10 
描述清楚你程序的功能和意图

授人以渔,不授人以鱼。
2012-11-20 01:23
bbb222
Rank: 2
等 级:论坛游民
帖 子:31
专家分:54
注 册:2012-11-17
得分:0 
已经解决了 散分结贴...
谢谢各位关注
2012-11-22 23:43



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-387484-1-1.html




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

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