| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 589 人关注过本帖, 1 人收藏
标题:【求助】怎样在查询函数中求出他的平均分呢,string转化为float很难
只看楼主 加入收藏
星星点灯xrs
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2011-12-26
结帖率:0
收藏(1)
已结贴  问题点数:20 回复次数:3 
【求助】怎样在查询函数中求出他的平均分呢,string转化为float很难
功能需求    1)    输入学生基本信息及所选课程、课程成绩
2)    根据学生姓名和选课名称修改课程成绩
3)    查询所有学生的所有选课信息
4)根据名字查询某个学生的选课信息及所有选课的总平均成绩。
下面是我编的程序:
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
#include<cmath>
using namespace std;
#define M 200   //可以存信息总量
int N=0;        //学生总量
class student
{
public:
    student();
    void set();       //初始化信息,第一次对信息的录入
    void change();    //修改某学生信息
    void search();    //查询某学生信息
    void display();   //显示全部学生信息
private:
    string name,sex,Math,Physical,Computer,English,Score;static int num;
};
int student::num=110;
student::student() {}


void student::set()
{
    int i;
    string setname,setMath,setPhysical,setComputer,setEnglish,setsex;
    ofstream outfile;
    outfile.open("f1.txt",ios::out);   //----打开文件----
    if(!outfile)
    {
        cerr<<" open error"<<endl;
        exit(1);
    }
//----依次录入相应个数的学生信息----
    for(i=0;i<M;i++)
    {
        cout<<"当姓名输入为'0'时,停止输入!!!"<<endl;
        cout<<"请输入姓名:"<<endl;
        cin>>setname;
        name=setname;
        if(setname=="0") break;
        if(setname!="0")
        {
            N++;
            num++;
            outfile<<num<<"  ";
               outfile<<setname<<"  ";
            cout<<"请输入性别:"<<endl;
            cin>>setsex;
            sex=setsex;
            outfile<<setsex<<"  ";
            cout<<"请输入数学成绩:"<<endl;
            cin>>setMath;
            Math=setMath;
            outfile<<setMath<<"  ";
            cout<<"请输入物理成绩:"<<endl;
            cin>>setPhysical;
            Physical=setPhysical;
            outfile<<setPhysical<<" ";
            cout<<"请输入计算机成绩:"<<endl;
            cin>>setComputer;
            Computer=setComputer;
            outfile<<setComputer<<" ";
            cout<<"请输入英语成绩:"<<endl;
            cin>>setEnglish;
            English=setEnglish;
            outfile<<setEnglish<<" "<<endl;
        }
    }
    outfile.close();    //----关闭文件----
}
void student::change()
{
    int i,k;
    int num[M];
    string name[M],sex[M],Math[M],Physical[M],Computer[M],English[M];
    ifstream infile("f1.txt",ios::in);
    if(!infile)
    {
        cerr<<" open error"<<endl;
        exit(1);
    }
    for(i=0;i<N;i++)
    {
        infile>>num[i];
        cout<<setw(9)<<num[i];
        infile>>name[i];
        cout<<setw(9)<<name[i];
        infile>>sex[i];
        cout<<setw(9)<<sex[i];
        infile>>Math[i];
        cout<<setw(9)<<Math[i];
        infile>>Physical[i];
        cout<<setw(9)<<Physical[i];
        infile>>Computer[i];
        cout<<setw(9)<<Computer[i];
        infile>>English[i];
        cout<<setw(9)<<English[i]<<endl;
    }
    infile.close();
    ofstream outfile("f1.txt",ios::out);
    if(!outfile)
        {
            cerr<<" open error"<<endl;
            exit(1);
        }
    cout<<"你想修改第几个学生的成绩?"<<endl;
    cin>>k;
    k=k-1;
    cout<<"请输入新的数据:"<<endl;
    cout<<"请输入姓名:"<<endl;
    cin>>name[k];
    cout<<"请输入性别:"<<endl;
    cin>>sex[k];
    cout<<"请输入数学成绩:"<<endl;
    cin>>Math[k];
    cout<<"请输入物理成绩:"<<endl;
    cin>>Physical[k];
    cout<<"请输入计算机成绩:"<<endl;
    cin>>Computer[k];
    cout<<"请输入英语成绩:"<<endl;
    cin>>English[k];
    for(i=0;i<N;i++)
    {
            outfile<<num[i]<<"   ";
            outfile<<name[i]<<"   ";
            outfile<<sex[i]<<"   ";
            outfile<<Math[i]<<"   ";
            outfile<<Physical[i]<<"   "<<endl;
            outfile<<Computer[i]<<"   ";
            outfile<<English[i]<<"   ";
    }
    outfile.close();
    cout<<"已修改成功!"<<endl;
}


void student::search()
{
    int i,w=0,num[M],a[M];
    string searchname,name[M],sex[M],Math[M],Phyiscal[M],Computer[M],English[M],b[M][6];
    ifstream infile("f1.txt",ios::in);
    if(!infile)
    {
        cerr<<" open error"<<endl;
        exit(1);
    }
    for(i=0;i<N;i++)
    {
        infile>>num[i];a[i]=num[i];
        infile>>name[i];b[i][0]=name[i];
        infile>>sex[i];b[i][1]=sex[i];
        infile>>Math[i];b[i][2]=Math[i];
        infile>>Physical[i];b[i][3]=Physical[i];
        infile>>Computer[i];b[i][4]=Computer[i];
        infile>>English[i];b[i][5]=English[i];
    }
        cout<<"请输入姓名!"<<endl;
        cin>>searchname;
        for(i=0;i<N;i++)
            if(name[i]==searchname)
            {
                w=1;
                cout<<"           学号      姓名     性别     数学     物理     计算机     英语"<<endl;
                cout<<"     ";
                cout<<setw(9)<<a[i];
                 cout<<setw(9)<<b[i][0]<<" ";
                cout<<setw(9)<<b[i][1]<<" ";
                 cout<<setw(9)<<b[i][2]<<" ";
                cout<<setw(9)<<b[i][3]<<" ";
                cout<<setw(9)<<b[i][4]<<" ";
                cout<<setw(9)<<b[i][5]<<" "<<endl;
            }
       if(w==0)
       {cout<<"无此人信息!"<<endl;
    }
}


void student::display()
{
    int k,num;
    string name,sex,Math,Physical,Computer,English;
    ifstream infile("f1.txt",ios::in);
    if(!infile)
    {
        cerr<<" open error"<<endl;
        exit(1);
    }
    cout<<"           学号      姓名     性别    数学     物理     计算机     英语"<<endl;
    for(k=0;k<N;k++)
    {
        cout<<"     ";
        infile>>num;
        cout<<setw(9)<<num;
        infile>>name;
        cout<<setw(9)<<name;
        infile>>sex;
        cout<<setw(9)<<sex;
        infile>>Math;
        cout<<setw(9)<<Math;
        infile>>Physical;
        cout<<setw(9)<<Physical;
        infile>>Computer;
        cout<<setw(9)<<Computer;
        infile>>English;
        cout<<setw(9)<<English<<endl;
    }
    infile.close();
}


student stu;
void main()
{
    char ch;
    void menu();
    menu();
    cin>>ch;
    while(ch!='0')
    {
        system("cls");
        switch(ch)
        {
            case '1':stu.set();break;
            case '2':stu.change();break;
            case '3':stu.search();break;
            case '4':stu.display();break;
            case '0':cout<<"退出系统!";break;
            default:cout<<"没有此选项!"<<endl;break;
        }
        menu();
        cin>>ch;
    }
}

void menu()
{
    cout<<"******************"<<endl;
    cout<<"*     1.输入     *"<<endl;
    cout<<"*     2.修改     *"<<endl;
    cout<<"*     3.查询     *"<<endl;
    cout<<"*     4.显示全部 *"<<endl;
    cout<<"*     0.退出     *"<<endl;
    cout<<"******************"<<endl;
}

搜索更多相关主题的帖子: 信息 课程 include public change 
2011-12-26 22:22
我是菜鸟C
Rank: 4
等 级:业余侠客
帖 子:74
专家分:200
注 册:2011-3-27
收藏
得分:7 
search()里面为什么要定义 string searchname,name[M],sex[M],Math[M],Phyiscal[M],Computer[M],English[M],b[M][6];呢??
你可以直接通过类名来访问它的成员啊比如Student s,s.sex,s.math等等。这样的话就可以直接的求平均值了啊。
2011-12-27 08:18
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9031
专家分:54061
注 册:2011-1-18
收藏
得分:7 
string转化为float很难
----------------------

程序代码:
#include <sstream>

class bad_lexical_cast : public std::exception {
};

template<typename Target, typename Source>
Target lexical_cast(Source arg) {
    std::stringstream interpreter;
    Target result;
    if( !(interpreter<<arg) || !(interpreter>>result) || !(interpreter>>std::ws).eof() )
        throw bad_lexical_cast();
    return result;
}


#include <string>
#include <iostream>
using namespace std;

int main()
{   

    string str = "123.456";

    try {
        float f = lexical_cast<float,string>(str);
        cout << f << endl;
    } catch( bad_lexical_cast) {
        cerr << "error\n";
    }

    return 0;
}

2011-12-27 11:23
pangding
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:北京
等 级:贵宾
威 望:94
帖 子:6784
专家分:16751
注 册:2008-12-20
收藏
得分:7 
我想到的也是用 stringstream。

如果楼主很不熟悉的话,用 C 语言里的 strtod 也可行。
不过 stringstream 会使了之后,会觉得用 C 里的那套方法也挺费劲的。
2011-12-28 01:37
快速回复:【求助】怎样在查询函数中求出他的平均分呢,string转化为float很难
数据加载中...
 
   



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

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