C语言问题,C语言问题,C语言问题 80C

编写一个程序,要求:输入N个学生的学号和成绩,并保存在stu.txt文本文件中,对学生成绩进行排序并把排序后的结果输出到score.txt文件中,同时在屏幕上输出高于平均成绩的学生的学号和成绩。

c
0

5个回答

图片说明

10
weixin_41491490
weixin_41491490 厉害了
大约一年之前 回复
weixin_42679365
weixin_42679365 厉害
大约一年之前 回复
weixin_42684795
猜猜 我 是谁 又学到一招
大约一年之前 回复
cyueyong
cyueyong 正解,可以采纳了
大约一年之前 回复
wait1210day
wait1210day 正解
大约一年之前 回复
showbo
支付宝加好友偷能量挖 正解,可以采纳了
大约一年之前 回复

同时在屏幕上输出高于平均成绩的学生的学号和成绩
这个刚才忘记了

// Q694588.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include "stdio.h"
#include "stdlib.h"

typedef struct
{
    char name[10];
    int score;
} sc;

int cmp(const void *a,const void *b)
{
return(*(sc *)a).score - (*(sc *)b).score;
}

int _tmain(int argc, _TCHAR* argv[])
{
    int n;
    printf("n = ?");
    scanf("%d", &n);
    FILE *f1, *f2;
    f1 = fopen("stu.txt", "w+");
    f2 = fopen("score.txt", "w+");
    sc * data = (sc *)malloc(sizeof(sc) * n);
    float sum = 0.0;
    for (int i = 0; i < n; i++)
    {
        printf("student #%d: name,score\n", i + 1);
        scanf("\n%[^,],%d", &data[i].name, &data[i].score);
    }
    printf("before sorting:\n");
    for (int i = 0; i < n; i++)
    {
        printf("student #%d: name %s,score %d\n", i + 1, data[i].name, data[i].score);
        fprintf(f1, "%s,%d\n", data[i].name, data[i].score);
        sum += data[i].score;
    }
    qsort(data, n, sizeof(sc), cmp);
    printf("after sorting:\n");
    for (int i = 0; i < n; i++)
    {
        printf("student #%d: name %s,score %d\n", i + 1, data[i].name, data[i].score);
        fprintf(f2, "%s,%d\n", data[i].name, data[i].score);
    }
    printf("avg score is: %f\n", sum / (float)n);
    for (int i = 0; i < n; i++)
    {
        if (sum / (float)n < data[i].score)
        {
            printf("student #%d: name %s,score %d\n", i + 1, data[i].name, data[i].score);
            fprintf(f2, "%s,%d\n", data[i].name, data[i].score);
        }
    }
    fclose(f1);
    fclose(f2);
    return 0;
}


n = ?5
student #1: name,score
a,45
student #2: name,score
b,53
student #3: name,score
c,74
student #4: name,score
d,84
student #5: name,score
e,60
before sorting:
student #1: name a,score 45
student #2: name b,score 53
student #3: name c,score 74
student #4: name d,score 84
student #5: name e,score 60
after sorting:
student #1: name a,score 45
student #2: name b,score 53
student #3: name e,score 60
student #4: name c,score 74
student #5: name d,score 84
avg score is: 63.200000
student #4: name c,score 74
student #5: name d,score 84
Press any key to continue . . .

2
 // Q694588.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include "stdio.h"
#include "stdlib.h"

typedef struct
{
    char name[10];
    int score;
} sc;

int cmp(const void *a,const void *b)
{
return(*(sc *)a).score - (*(sc *)b).score;
}

int _tmain(int argc, _TCHAR* argv[])
{
    int n;
    printf("n = ?");
    scanf("%d", &n);
    FILE *f1, *f2;
    f1 = fopen("stu.txt", "w+");
    f2 = fopen("score.txt", "w+");
    sc * data = (sc *)malloc(sizeof(sc) * n);
    for (int i = 0; i < n; i++)
    {
        printf("student #%d: name,score\n", i + 1);
        scanf("\n%[^,],%d", &data[i].name, &data[i].score);
    }
    printf("before sorting:\n");
    for (int i = 0; i < n; i++)
    {
        printf("student #%d: name %s,score %d\n", i + 1, data[i].name, data[i].score);
        fprintf(f1, "%s,%d\n", data[i].name, data[i].score);
    }
    qsort(data, n, sizeof(sc), cmp);
    printf("after sorting:\n");
    for (int i = 0; i < n; i++)
    {
        printf("student #%d: name %s,score %d\n", i + 1, data[i].name, data[i].score);
        fprintf(f2, "%s,%d\n", data[i].name, data[i].score);
    }
    fclose(f1);
    fclose(f2);
    return 0;
}


1

//

#include <stdio.h>

#include

typedef struct Student
{
int number;
int score;

}student;

#define MaxNumber 1024

student students[MaxNumber] = {};
int studentNumber = 0;
float average = 0;

void InputStuentsMessage() {

int i = 0;
while (i<MaxNumber)
{
    scanf("%d %d", &students[i].number, &students[i].score);
    if (students[i++].number == 0) {
        break;
    }
} 
studentNumber = i-1;

}

bool writeStudentsMessageToStu() {
FILE *fpWrite = fopen("stu.txt", "w");
if (fpWrite == NULL)
{
return false;
}
for (int i = 0; i < MaxNumber; ++i) {
if (students[i].number == 0) {
break;
}
fprintf(fpWrite, "%d %d\n", students[i].number, students[i].score);
}
fclose(fpWrite);
fpWrite = NULL;
return true;
}

void orderbyScore() {
int i = 0,j=0;
for (i = 0; i {
for (j = 0; j {
if (students[j].score > students[j + 1].score)//后一位数比前一位数小的话,就交换两个数的位置(升序)
{
student t;
t.number = students[j + 1].number;
t.score = students[j + 1].score;
students[j + 1].number = students[j].number;
students[j + 1].score = students[j].score;
students[j].number = t.number;
students[j].score = t.score;
}
}
}

}

void calculateAverage() {
float sum = 0;
int i = 0;
for (i = 0; i < studentNumber;++i) {
sum = students[i].score + sum;
}
average = sum / (float)studentNumber;
}

bool writeStutentsMessageToScore() {
FILE *p_file = fopen("score.txt","w");
if (p_file) {
for (int i = 0; i < studentNumber;++i) {
fprintf(p_file, "%d %d\n", students[i].number, students[i].score);
}

fclose(p_file);
p_file = NULL;
}
else
{
return false;
}

return true; 

}

void printToConsole() {

for (int i = 0; i < studentNumber;++i) {
    if (students[i].score > average) {
        printf("学生学号:%d    学生成绩:%d\n", students[i].number, students[i].score);
    }
}

}

int main()
{
printf("请输入学生学号和成绩,并以0 0结束!\n");
printf("例如:20 90 表示学号20 成绩90分\n 0 0 表示学号为0 成绩为0结束输入\n");
InputStuentsMessage();

if (!writeStudentsMessageToStu()) {
    printf("写入信息失败\n");
    return 0;
}
//排序
orderbyScore();

//輸出到文本

if (!writeStutentsMessageToScore()) {
    printf("寫入信息到 Score.txt 失敗\n");
}

//计算平均值
calculateAverage();

//打印輸出

printToConsole(); 

printf("输入任意字符結束\n");
getchar(); 
return 0;

}

1

那么完整的注释,随便给几个硬币也好啊,,,,
Qwq

#include<stdio.h>//输入输出
#include<stdlib.h>//排序函数

typedef struct{//结构表示学生
    char name[10];
    int score;
}sc;
//qsort排序函数用到的结构体比较大小
bool cmp(const void *a,const void *b)
{
    return(*(sc *)a).score < (*(sc *)b).score;
}

int main(){
    //文件指针, 新建两个文本
    FILE *f1, *f2;
    f1 = fopen("stu.txt", "w+");
    f2 = fopen("score.txt", "w+");
    //输入学生人数
    int n;
    scanf("%d", &n);
    //分配n个变量
    sc *data = (sc *)malloc(sizeof(sc) * n);
    //获取每个学生的姓名和成绩
    for(int i = 0; i < n; i++)
        scanf("%d%d", &data[i].name, &data[i].score);
    //输出到stu.txt文本中
    for(int i = 0; i < n; i++)
        fprintf(f1, "%s,%d\n", data[i].name, data[i].score);
    //对学生成绩进行排序
    qsort(data, n, sizeof(sc), cmp);
    //将排序后的学生成绩输出到score.txt文本中
    for(int i = 0; i < n; i++)
        fprintf(f2, "%s,%d\n", data[i].name, data[i].score);
    //计算平均成绩
    int avg = 0;
    for(int i = 0; i < n; i++)avg += data[i].score;
    avg /= n;
    //在屏幕输出高于平均成绩的学生
    for(int i = 0; i < n; i++)
        if(data[i].score>avg)
            printf("%s %d",data[i].name,data[i].score);
    //关闭文件读写
    fclose(f1);
    fclose(f2);
    return 0;
}
0
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!