C++文件操作 10
#include<iostream.h>
#include<string.h>
struct xinxi
{
char name[10],zhuanye[10],state[10];
int number,math,english,policy,major,total;
};
void input1(xinxi a[],int b)
{
cout<<"请按学生姓名、学号、是否应届、报考专业、数学成绩、英语成绩、政治成绩、专业课成绩的顺序输入"<<endl;
for(int i=0;i<b;i++)
{
cin>>a[i].name>>a[i].number>>a[i].state>>a[i].zhuanye>>a[i].math>>a[i].english>>a[i].policy>>a[i].major;
a[i].total=a[i].math+a[i].english+a[i].policy+a[i].major;
}
}
void input2(double &m,double &n,double &p,double &q,double &r)
{
cout<<"请输入录取数学最低分"<<endl;
cin>>m;
cout<<"请输入录取英语最低分"<<endl;
cin>>n;
cout<<"请输入录取政治最低分"<<endl;
cin>>p;
cout<<"请输入录取专业课最低分"<<endl;
cin>>q;
cout<<"请输入录取总分最低分"<<endl;
cin>>r;
}
void shaixuan(xinxi a[] ,xinxi b[],double m,double n,double p,double q,double r,double c,int &j)
{
int d;
j=0;
for (int i=0;i<c;i++)
{
if (a[i].math>=m&&a[i].english>=n&&a[i].policy>=p&&a[i].major>=q&&a[i].total>=r)
{
b[j]=a[i];
j=j+1;
}
}
cout<<"初试合格人数"<<j<<endl;
for ( i=0;i<j-1;i++)
for(d=0;d<j-1-i;d++)
{
if(b[d].total<b[d+1].total)
{
xinxi t=b[d];
b[d]=b[d+1];
b[d+1]=t;
}
}
cout<<"过线的考生"<<endl;
for(i=0;i<j;i++)
cout<<b[i].name<<'\t'<<b[i].number<<'\t'<<b[i].state<<'\t'<<b[i].zhuanye<<'\t'<<b[i].math<<'\t'<<b[i].english<<'\t'<<b[i].policy<<'\t'<<b[i].major<<'\t'<<b[i].total<<endl;
}
void output1(xinxi b[],int c,int j)
{
double y,z, k=0,s=0;
for (int i=0;i<j;i++)
{
if(strcmp(b[i].state,"应届"))
s=s+1;
else
k=k+1;
}
cout<<"应届考生合格人数"<<'\t'<<k<<endl;
z=k/c;
cout<<"应届考生合格所占比例"<<'\t'<<z<<endl;
cout<<"非应届考生合格人数"<<'\t'<<s<<endl;
y=s/c;
cout<<"非应届考生合格所占比例"<<'\t'<<y<<endl;
}
void output2(xinxi a[],int c)
{
cout<<"各科均大于60的考生"<<endl;
for(int i=0;i<c;i++)
{
if(a[i].math>60&&a[i].english>60&&a[i].policy>60&&a[i].major>60)
cout<<a[i].name<<'\t'<<a[i].number<<'\t'<<a[i].state<<'\t'<<a[i].zhuanye<<'\t'<<a[i].math<<'\t'<<a[i].english<<'\t'<<a[i].policy<<'\t'<<a[i].major<<'\t'<<a[i].total<<endl;
}
}
void main()
{
double m,n,p,q,r;
int c,j;
xinxi a[100],b[100];
cout<<"输入考生总人数"<<endl;
cin>>c;
input1(a,c);
input2(m,n,p,q,r);
shaixuan(a,b,m,n,p,q,r,c,j);
output1(b,c,j);
output2(a,c);
} 展开
// ConsoleApplication5.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
struct xinxi
{
char name[10], zhuanye[10], state[10];
int number, math, english, policy, major, total;
};
void input1(xinxi a[], int b)
{
ifstream file;
file.open("C:\\Users\\x\\Desktop\\stu1.txt");
if (!file.is_open())
{
cout << "读取失败" << endl;
exit(-1);
}
//cout<<"请按学生姓名、学号、是否应届、报考专业、数学成绩、英语成绩、政治成绩、专业课成绩的顺序输入"<<endl;
for (int i = 0; i<b; i++)
{
file >> a[i].name >> a[i].number >> a[i].state >> a[i].zhuanye >> a[i].math >> a[i].english >> a[i].policy >> a[i].major;
a[i].total = a[i].math + a[i].english + a[i].policy + a[i].major;
}
file.close();
}
void input2(double &m, double &n, double &p, double &q, double &r)
{
ifstream file;
file.open("C:\\Users\\x\\Desktop\\stu2.txt");
if (!file.is_open())
{
cout << "读取出错" << endl;
exit(-1);
}
//cout<<"请输入录取数学最低分"<<endl;
file >> m;
//cout<<"请输入录取英语最低分"<<endl;
file >> n;
//cout<<"请输入录取政治最低分"<<endl;
file >> p;
//cout<<"请输入录取专业课最低分"<<endl;
file >> q;
//cout<<"请输入录取总分最低分"<<endl;
file >> r;
file.close();
}
void shaixuan(xinxi a[], xinxi b[], double m, double n, double p, double q, double r, double c, int &j)
{
int d;
j = 0;
for (int i = 0; i<c; i++)
{
if (a[i].math >= m&&a[i].english >= n&&a[i].policy >= p&&a[i].major >= q&&a[i].total >= r)
{
b[j] = a[i];
j = j + 1;
}
}
cout << "初试合格人数" << j << endl;
for (int i = 0; i<j - 1; i++)
for (d = 0; d<j - 1 - i; d++)
{
if (b[d].total<b[d + 1].total)
{
xinxi t = b[d];
b[d] = b[d + 1];
b[d + 1] = t;
}
}
cout << "过线的考生" << endl;
for (int i = 0; i<j; i++)
cout << b[i].name << '\t' << b[i].number << '\t' << b[i].state << '\t' << b[i].zhuanye << '\t' << b[i].math << '\t' << b[i].english << '\t' << b[i].policy << '\t' << b[i].major << '\t' << b[i].total << endl;
}
void output1(xinxi b[], int c, int j)
{
double y, z, k = 0, s = 0;
for (int i = 0; i<j; i++)
{
if (strcmp(b[i].state, "应届"))
s = s + 1;
else
k = k + 1;
}
cout << "应届考生合格人数" << '\t' << k << endl;
z = k / c;
cout << "应届考生合格所占比例" << '\t' << z << endl;
cout << "非应届考生合格人数" << '\t' << s << endl;
y = s / c;
cout << "非应届考生合格所占比例" << '\t' << y << endl;
}
void output2(xinxi a[], int c)
{
cout << "各科均大于60的考生" << endl;
for (int i = 0; i<c; i++)
{
if (a[i].math>60 && a[i].english>60 && a[i].policy>60 && a[i].major>60)
cout << a[i].name << '\t' << a[i].number << '\t' << a[i].state << '\t' << a[i].zhuanye << '\t' << a[i].math << '\t' << a[i].english << '\t' << a[i].policy << '\t' << a[i].major << '\t' << a[i].total << endl;
}
}
int main()
{
ifstream file;
file.open("C:\\Users\\x\\Desktop\\stu3.txt");
if (!file.is_open())
{
cout << "读入出错" << endl;
exit(-1);
}
double m, n, p, q, r;
int c, j;
xinxi a[100], b[100];
cout << "输入考生总人数" << endl;
file >> c;
input1(a, c);
input2(m, n, p, q, r);
shaixuan(a, b, m, n, p, q, r, c, j);
output1(b, c, j);
output2(a, c);
file.close();
}
需要事先在桌面上创建三个文本文件 stu1,stu2,stu3。 没把你代码看完, 马上下班了来不及了,应该没得问题。