每个记录包括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-->m域宽,打印出来以后,在控制台上,显示m位;
如果我们要打印的数的位数如果超过我们设定m则原样输出;
如果我们要打印的数的位数如果小于我们设定的位数,则补空白,具体如下:
如果m为正数,则左对齐(左侧补空白);
如果m为负数,则右对齐(右侧补空白)。
2、转义字符
如果想输出字符"%",则应该在“格式控制”字符串中用连续两个%表示。
如:printf("%f%%",1.0/3);输出结果:0.333333%。