求把这个c++程序转成普通c语言!最好有malloc

来源:互联网 发布:怎样安装三菱编程软件 编辑:程序博客网 时间:2024/05/21 11:14
你被要求写使用实现考试成绩处理系统结构的程序。
有5名学生。每个学生都有一个学生证和5门课的不同分数。
学生ID存储为8位数字串。
为学生数据最初存储在文本文件“student.txt”,应该由程序读出。

该方案必须:

运用结构语句struct
不使用任何全局变量(global variable)。
使用数组或指针数组,来存储数据,每一个学生。
阅读为每个学生的数据。
打印学生最低的组合标志(ID和总分)。
打印学生最高的组合标志(ID和总分)。
打印学生的所有标记(ID和最高分)的单一最高分。

所有的答案将是独一无二的。

示例:
如果该文件包含student.txt:
11111111 1 2 3 4 5
22222222 95 96 97 98 98
33333333 30 40 50 60 70
44444444 66 69 34 84 51
55555555 90 100 97 98 98

则输出是:
11111111 15
22222222 484
55555555 100
下面是c++版本,现在希望得到c语言版本。最好使用malloc函数和“.”,“->”
#include <iostream>
#include <array>
#include <fstream>
#include <cstring>

using namespace std;

struct Student
{
    char id[25];
    int m1;
    int m2;
    int m3;
    int m4;
    int m5;
};

void CopyRecords(string Fname,Student p[5]);
void Display(Student p[5]);
void getTotal(Student p[5],int totals[5]);

int main()
{

    Student p[5];
    
    int totals[5],hmarks[5],total=0,min=0,max=0,high;
    // Copy data from file to structure array p 
    CopyRecords("student.txt",p);
    
    // Display uppercase 
    cout<<"Data Read in Student structure array is \n\n";
    Display(p);
    cout<<endl;
    // Make Name variable all uppercase
    getTotal(p,totals);

    min=totals[0];
    max=totals[0];
    //high=p[0].m1;
    int maxindx=0,minindx=0,hindx=0;
    for(int i=0;i<5;i++)
    {
        if(totals[i]>max)
        {
            max=totals[i];
            maxindx=i;
        }
        if(totals[i]<min)
        {
            min=totals[i];
            minindx=i;
        }
    }
    cout<<"\nStudent with lowest total is " << p[minindx].id<<" "<<totals[minindx]<<endl;
    
    cout<<"\nStudent with highest total is " << p[maxindx].id<<" "<<totals[maxindx]<<endl;
    
    //hmarks[0]=
        max=0;
        for(int i=0;i<5;i++)
        {
            max=p[i].m1;
            if(p[i].m2>max)
            {
                max=p[i].m2;
            }
            if(p[i].m3>max)
            {
                max=p[i].m3;
            }
            if(p[i].m4>max)
            {
                max=p[i].m4;
            }
            if(p[i].m5>max)
            {
                max=p[i].m5;
            }
            hmarks[i]=max;
        }


    cout<<"\nHighest marks obtained by any student is \n\n";
    max=0,maxindx=0;
    for(int i=0;i<5;i++)
    {
        if(hmarks[i]>max)
        {
            max=hmarks[i];
            maxindx=i;
        }
    }
    cout<<"Id: "<
}


void getTotal(Student p[5],int totals[5])
{
    int total;
    for(int i=0;i<5;i++)
    {
        totals[i]=0;
        totals[i]= p[i].m1 + p[i].m2+ p[i].m3+ p[i].m4+ p[i].m5;
    }


}


<p[maxindx].id<<" "<<"Marks: "<<max<<endl;


    //system("PAUSE");
    return 0;
}


void CopyRecords(string Fname,Student p[5])
{


   ifstream f;
    f.open(Fname);


    for (int i = 0; i < 5; i++)
    {


        //f.getline(p[i].id, 8, ' ');
        f >> p[i].id >> p[i].m1 >> p[i].m2 >> p[i].m3 >> p[i].m4 >> p[i].m5;
    }


    f.close();
}




void Display(Student p[5])
{
    
    
    for (int i = 0; i < 5; i++)
    {
        cout << p[i].id << " " << p[i].m1 << " " << p[i].m2<< " " << p[i].m3<< " " << p[i].m4<< " " << p[i].m5<<endl;
    }
    
    
    // Have all data under variable Name uppercase
0 0
原创粉丝点击