c++基础问题:头文件 , 源文件 , 资源文件,分别是什么。要在你面定义什么样的东西。

我用的是visual studio 2008 ,

头文件就是.h文件,一般用来放类的声明,平时你用的#include<string.h>就是包含这种文件
源文件就是.cpp文件,一般用来放你在头文件里声明的类成员函数的具体实现,一般一个.h文件总会有一个同名的.cpp
譬如现在我有一个类
class T
{
void printf(){}
}
这些内容就放在.h文件中
void T::printf(){......}
这些内容就放在.cpp文件中
(MFC的消息衍射类都是这样的)
至于资源文件就是.rc文件,用来存放一些譬如菜单,或者图标等资源的内容
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-11-25
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

#define EXCELLENT 90
#define GOOD 80
#define MODERATE 60
#define PASS 60

struct GradesInfo{
int Total()
void input();
string studentNumber;
string name;
int score1;
int score2;
} grades[5];

void GradesInfo::input()
{
cout << "请输入学号: ";
cin >> studentNumber;
cout << "请输入姓名: ";
cin >> name;
cout << "请输入成绩: ";
cin >> score1;
cout << "请输入成绩2: ";
cin >> score2;
cout <<endl;
}

void save(){
ofstream of;
of.open("record.txt",ofstream::out | ofstream::app);
for(int i(0);i<5;i++)
{
of << grades[i].studentNumber << endl;
of << grades[i].name << endl;
of << grades[i].score1 << endl;
of << grades[i].score2 << endl;
of << endl;
}
of.close();
}
void sort()
{
for(int i(0);i<5-1;i++)
for(int j(i+1);j<5;j++)
{
if( grades[i].Total() < grades[j].Total())
{
GradesInfo gi = grades[i];
grades[i] = grades[j];
grades[j] = gi;
}
}
}
void saveSorted(){
ofstream of;
of.open("sort.txt",ofstream::out | ofstream::app);

for(int i(0);i<5;i++)
{
of << grades[i].studentNumber << endl;
of << grades[i].name << endl;
of << grades[i].score1 << endl;
of << grades[i].score2 << endl;
of << grades[i].Total() << endl;
of << endl;
}
of.close();
}

void input(){
for(int i(0);i<5;i++)
{
cout << "请输入第"<<(1+i)<<"个学生"<<endl;
grades[i].input();
}
save();
sort();
saveSorted();
}
void displayAverage(){
double avg(0.0);
for(int i(0);i<5;i++)
avg += grades[i].Total();
avg /= 5;
cout << endl<< "平均成绩: " << avg << endl<< endl;
}
void displayFailRate(){
int f(0);
for(int i(0);i<5;i++)
{
if(grades[i].Total() < PASS * 2)
f++;
}
double fr = 100*f / 5;
cout << endl<< "不及格比例: " << fr <<"%"<< endl<< endl;
}
void displayPassRate(){
int p(0);
for(int i(0);i<5;i++)
{
if(grades[i].Total() >= PASS * 2)
p++;
}
double pr = 100.0*p / 5;
cout << endl<< "及格比例: " << pr <<"%"<< endl<< endl;
}
void displayGradeRate(){
int e(0);
int g(0);
int m(0);
for(int i(0);i<5;i++)
{
if(grades[i].Total() >= EXCELLENT * 2)
e++;
else if(grades[i].Total() >= GOOD * 2)
g++;
else if(grades[i].Total() >= MODERATE * 2)
m++;
}
double er = 100.0*e / 5;
double gr = 100.0*g / 5;
double mr = 100.0*m / 5;
cout << endl;
cout << "优比例: " << er <<"%"<< endl;
cout << "良比例: " << gr <<"%"<< endl;
cout << "中比例: " << mr <<"%"<< endl<< endl;
}

int menu() {
int opt(0);
cout << "=====================" << endl;
cout << "1. 重新输入成绩" << endl;
cout << "2. 平均成绩" << endl;
cout << "3. 不及格比例" << endl;
cout << "4. 及格比例" << endl;
cout << "5. 优良中所占比例" << endl;
cout << "0. 退出" << endl;
cout << "=====================" << endl;
cin >> opt;
return opt;
}
int main()
{
int opt(1);
do
{
system("cls");
switch(opt) {
case 1: input(); break;
case 2: displayAverage(); break;
case 3: displayFailRate(); break;
case 4: displayPassRate(); break;
case 5: displayGradeRate(); break;
}
opt = menu();
} while(opt);
return 0;
}
相似回答