成绩处理

来源:互联网 发布:网络21是传销吗 编辑:程序博客网 时间:2024/06/10 08:53

【缺点】:还不能判断两个float类型是否相等,把所有float 换成 int类型就行了。

// 成绩处理.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <iostream>#include <iomanip>#include <cmath>using namespace std;/*把所有float类型换成double效果更好  (1)输入小组人数及成绩;  (2)输出该小组的最高成绩、最低成绩、平均成绩;(没有加标准方差)    (3)输出考得最高成绩和最低成绩的同学的人数及对应的学号(设成绩对应的下标即学号,可能有相同的成绩)*/void InputScore(float *s, int n);/*成绩输入*/void OutputMax(float *s, int n, float *max1);/*输出最高成绩*/void OutputMin(float *s, int n, float *min1);/*输出最低成绩*/void Average(float *s, int n);/*输出平均成绩*/void Number(float *s, int n, float *max1, float *min1);/*输出相应的人数*/int main(){int len;/*数组长度*/float *score;/*分数*/float max, min;cout << "请输入小组人数:";cin >> len;score = new float[len];/*创建动态的数组*/InputScore(score, len);/*成绩输入*/OutputMax(score, len, &max);/*输出最大值*/OutputMin(score, len, &min);/*输出最低值*/Average(score, len);/*输出平均成绩*/Number(score, len, &max, &min);free(score);/*必须手动释放*/return 0;}void InputScore(float *s, int n)/*成绩输入*/{int i;cout << "\n请输入每个人的成绩(0~100):\n";/*输入成绩*/for ( i=0; i<n; ++i ){do/*具有一定的容错性*/{cout << "请输入第" << i+1 << "名同学的成绩:";cin >> s[i];while ( getchar() != '\n' )/*一定程度上避免一行输入多个成绩*/;}while ((s[i] < 0) || (s[i] > 100));}}void OutputMax(float *s, int n, float *max1)/*输出最高成绩*/{int i;int count = 0;/*用于计算相同成绩*/float max;max = s[0];/*初始化max*/for ( i=1; i<n; ++i ){if ( max <= s[i] )/*如果条件成立,max赋值*/{max = s[i];}}for ( i=0; i<n; ++i ){if ( max == s[i] ){++count;}}cout << "最高成绩为:" << max << ",共有:" << count << "人。" << endl;}void OutputMin(float *s, int n, float *min1)/*输出最低成绩*/{int i;int count = 0;/*用于计算相同成绩*/float min;min = s[0];/*初始化min*/for ( i=1; i<n; ++i ){if ( min >= s[i] )/*如果条件成立,min赋值*/{min = s[i];}}for ( i=0; i<n; ++i )/*计数,有点笨额*/{if ( min == s[i] ){++count;}}cout << "最高成绩为:" << min << ",共有:" << count << "人。" << endl;}void Average(float *s, int n)/*输出平均成绩*/{int i;float sum = 0;/*注意此处要初始化*/for ( i=0; i<n; ++i ){sum += s[i];}cout << setprecision(1) << setiosflags(ios::fixed) << "平均成绩为:" << sum/n << resetiosflags(ios::fixed) << setprecision(6) << endl;}void Number(float *s, int n, float *max1, float *min1)/*输出相应的人数*/{int i;//int temp;cout << "获最高成绩的学生(学号)有:";for ( i=0; i<n; ++i ){//temp = fabs(*max1 - s[i]);if ( fabs(*max1 - s[i]) <= (1e-6) ){cout << i+1 << " ";}}cout << "\n获最低成绩的学生(学号)有:";for ( i=0; i<n; ++i ){//temp = fabs(s[i] - *min1);if ( fabs(s[i] - *min1) <= 1e-6 ){cout << i+1 << " ";}}cout << endl;}


 

原创粉丝点击