C语言:编写一个函数print,打印一个学生的成绩结构数组,该数组中有5个学生的数据记录,(续接问题补充)

每个记录包括num、name、score[3],用主函数输入这些记录,用print函数输出这些记录。
求高人解答,急用,谢谢。。。(*^__^*) 嘻嘻……

#define _CRT_SECURE_NO_WARNINGS

#include<stdio.h>

#include<stdlib.h>

#define N 5

struct student {

int num;

char name[1024];

int score[3];

};

typedef struct student stu;

void print(stu* p) {

printf("学号\t姓名\t成绩1\t成绩2\t成绩3\n");

for (int i = 0; i < N; ++i) {

printf("%d\t", (p + i)->num);

printf("%s\t", (p + i)->name);

for (int j = 0; j < 3; ++j) {

printf("%d\t", (p + i)->score[j]);

}

printf("\n");

}

}

void main() {

stu arr[N];

stu* p = arr;

//输入

for (int i = 0; i < N; ++i) {

printf("请输入第%d个学生的信息\n", i + 1);

printf("请输入num\n");

scanf("%d", &(p + i)->num);

printf("请输入姓名\n");

scanf("%s", &(p + i)->name);

for (int j = 0; j < 3; ++j) {

printf("请输入score%d\n", j + 1);

scanf("%d", &(p + i)->score[j]);

}

}

//输出

print(p);

system("pause");

}

运行效果:

扩展资料:

printf函数使用注意事项

1、域宽

%d:按整型数据的实际长度输出。

如果想输出指定宽度可以指定域宽,%md--&gt;m域宽,打印出来以后,在控制台上,显示m位;

如果我们要打印的数的位数如果超过我们设定m则原样输出;

如果我们要打印的数的位数如果小于我们设定的位数,则补空白,具体如下:

如果m为正数,则左对齐(左侧补空白);

如果m为负数,则右对齐(右侧补空白)。

2、转义字符

如果想输出字符"%",则应该在“格式控制”字符串中用连续两个%表示。

如:printf("%f%%",1.0/3);输出结果:0.333333%。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-02-17
struct StudentScore{
int num;
char name[20];
float score[3];
}
void print(StudentScore s,int length)
{
int i;
for(i=0;i<length;i++)
{
printf("第%d个学生的学号:%d\n",i+1,s[i].num);
printf("第%d个学生的姓名:%s\n",i+1,s[i].name);
printf("第%d个学生的成绩:%f,%f,%f\n",i+1,s[i].score[0],s[i].score[1],s[i].score[2]);
}
}
void main()
{
StudentScore stuScore[5];
int i,j;
for(i=0;i<5;i++)
{
printf("请输入第%d个学生的学号:",i);
scanf("%d",&stuScore[i].num);
printf("请输入第%d个学生的姓名:",i);
scanf("%s",stuScore[i].name);
printf("请输入第%d个学生的三门成绩:",i);
scanf("%f%f%f",&stuScore[i].score[0],&stuScore[i].score[1],&stuScore[i].score[2]);
}
print(stuScore,5);
}
第2个回答  推荐于2018-02-27
#include <stdio.h>
struct student
{
int num;
char name[20];
int score[3];
}stu[100];
int main()
{
int n, i;
printf("学生数:");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
scanf("%d", &stu[i].num);
scanf("%s", stu[i].name);
getchar();
scanf("%d%d%d", &stu[i].score[0], &stu[i].score[1], &stu[i].score[2]);
}
for (i = 0; i < n; i++)
{
printf("%d ", stu[i].num);
printf("%s ", stu[i].name);
printf("%d %d %d\n", stu[i].score[0], stu[i].score[1], stu[i].score[2]);
}
return 0;
}本回答被网友采纳
第3个回答  2012-02-17
#include <stdio.h>
#include "stdlib.h"

#define N 3 //学生的个数
struct StudentScore{
char num[10];
char name[10];
float score[3];
};
void main()
{
StudentScore stuScore[N];
int i,j;
void MyPrint(StudentScore*, int);
printf("start...\n");
for (i = 0; i < N; i++)
{
printf("Please enter the %dst's student number:", i);
scanf("%s", stuScore[i].num);
printf("Please enter name:");
scanf("%s", stuScore[i].name);
for (j = 0; j < 3; j++)
{
printf("Please enter the %dst score:", j + 1);
scanf("%f", stuScore[i].score + j);
}
printf("\n");
}
MyPrint(stuScore, N);
}

void MyPrint(StudentScore *stuScore, int n)
{
int i,j;
printf("-----Myprint Function Start-----\n");
for (i = 0; i < n; i++)
{
printf("name: %s\n", stuScore[i].name);
printf("number: %s\n", stuScore[i].num);
for (j = 0; j < 3; j++)
{
printf("The %dst score: %f\n", j, stuScore[i].score[j]);
}
printf("\n");
}
printf("-----Myprint Function Over-----\n");
}
第4个回答  2012-02-17
一模一样,你看看吧,你懂的

参考资料:http://zhidao.baidu.com/question/346808416.html?oldq=1