该程序应该接受学生的姓氏(LN),名字(FN),中间名(MI),地址(ADD),第一,第二,第三和第四季度(FIR,SEC,THR) ,FTH),计算四个输入等级的平均值,并按字母顺序(姓氏)或降序(平均值)显示所有信息。
应该看起来像这样:
.
. Last name: First name: Middle Intial: Address:
.
. First Quarter grades:
. Second Quarter grades:
. Third Quarter grades:
. Fourth Quarter grades:
. Average:
.
. Which way do you want to arrange the names? Enter A to alphabetize or B to arrange in descending order by grade.
之后,它应清除屏幕并仅显示名称。
.
. List:
.
. Apple, Johnny K. from Kansas 89 91 89 91 90.0
. Graham, Crackers L. from N.Y. 79 81 79 81 80.0
.
实际上应该接受90名学生,但我将3作为考试编号。
输入所有数据后,它将结束“分段故障”。
有人可以告诉我我做错了什么吗?我应该在最后一部分使用puts而不是printf吗?如果是这样,我该怎么做?
#include <stdio.h>
#include <string.h>
#include <conio.h>
int main()
{
int a=0, SW=1, x, y, i, j, FIR[100], SEC[100], THR[100], FTH[100], AVE[100], sum=0, temp2[100];
char LN[15][90], FN[15][90], MI[5][90], ADD[50][90], OPT, temp1[50];
while(SW)
{
clrscr();
sum=0;
gotoxy(10,2);
printf("Family name: ");
gotoxy(35,2);
printf("First name: ");
gotoxy(60,2);
printf("Middle initial: ");
gotoxy(80,2);
printf("Address: ");
gotoxy(25,4);
printf("1st Quarter Grade: ");
gotoxy(25,5);
printf("2nd Quarter Grade: ");
gotoxy(25,6);
printf("3rd Quarter Grade: ");
gotoxy(25,7);
printf("4th Quarter Grade: ");
gotoxy(25,8);
printf("Average: ");
gotoxy(23,2);
fgets(LN[a],15,stdin);
gotoxy(47,2);
fgets(FN[a],15,stdin);
gotoxy(76,2);
fgets(MI[a],15,stdin);
gotoxy(89,2);
fgets(ADD[a],15,stdin);
gotoxy(44,4);
scanf("%d",&FIR[a]);
sum+=FIR[a];
gotoxy(44,5);
scanf("%d",&SEC[a]);
sum+=SEC[a];
gotoxy(44,6);
scanf("%d",&THR[a]);
sum+=THR[a];
gotoxy(44,7);
scanf("%d",&FTH[a]);
sum+=FTH[a];
AVE[a]=(float)sum/4;
gotoxy(34,8);
printf("%d",AVE[a]);
a++;
if(a==3)
SW=0;
getch();
}
gotoxy(10,10);
printf("Which way do you want to arrange the names? Enter A to alphabetize or B to arrange in descending order by grade. ");
gotoxy(10,11);
scanf("%c",&OPT);
if(OPT=='A' || OPT=='a')
{
for(x=0;x<=2;x++){
for(y=x+1;y<=2;y++){
if(strcmp(LN[x],LN[y])>0){
strcpy(temp1, LN[x]);
strcpy(LN[x], LN[y]);
strcpy(LN[y], temp1);
strcpy(temp1, FN[x]);
strcpy(FN[x], FN[y]);
strcpy(FN[y], temp1);
strcpy(temp1, MI[x]);
strcpy(MI[x], MI[y]);
strcpy(MI[y], temp1);
strcpy(temp1, ADD[x]);
strcpy(ADD[x], ADD[y]);
strcpy(ADD[y], temp1);
strcpy(temp2, FIR[x]);
strcpy(FIR[x], FIR[y]);
strcpy(FIR[y], temp2);
strcpy(temp2, SEC[x]);
strcpy(SEC[x], SEC[y]);
strcpy(SEC[y], temp2);
strcpy(temp2, THR[x]);
strcpy(THR[x], THR[y]);
strcpy(THR[y], temp2);
strcpy(temp2, FTH[x]);
strcpy(FTH[x], FTH[y]);
strcpy(FTH[y], temp2);
strcpy(temp2, AVE[x]);
strcpy(AVE[x], AVE[y]);
strcpy(AVE[y], temp2);
}
}
}
}
else
{
for(x=0;x<=2;x++){
for(y=x+1;x<=2;x++){
if(AVE[x]<AVE[y]){
strcpy(temp1, LN[x]);
strcpy(LN[x], LN[y]);
strcpy(LN[y], temp1);
strcpy(temp1, FN[x]);
strcpy(FN[x], FN[y]);
strcpy(FN[y], temp1);
strcpy(temp1, MI[x]);
strcpy(MI[x], MI[y]);
strcpy(MI[y], temp1);
strcpy(temp1, ADD[x]);
strcpy(ADD[x], ADD[y]);
strcpy(ADD[y], temp1);
strcpy(temp2, FIR[x]);
strcpy(FIR[x], FIR[y]);
strcpy(FIR[y], temp2);
strcpy(temp2, SEC[x]);
strcpy(SEC[x], SEC[y]);
strcpy(SEC[y], temp2);
strcpy(temp2, THR[x]);
strcpy(THR[x], THR[y]);
strcpy(THR[y], temp2);
strcpy(temp2, FTH[x]);
strcpy(FTH[x], FTH[y]);
strcpy(FTH[y], temp2);
strcpy(temp2, AVE[x]);
strcpy(AVE[x], AVE[y]);
strcpy(AVE[y], temp2);
}
}
}
}
gotoxy(10,15);
printf("List: ");
for(j=17;j<=19;j++){
for(x=0;x<=2;x++){
i=10;
gotoxy(i,j);
printf("%s,%s %s",LN[x],FN[x],MI[x]);
i+=40;
gotoxy(i,j);
printf("%f",FIR[x]);
i+=5;
gotoxy(i,j);
printf("%f",SEC[x]);
i+=5;
gotoxy(i,j);
printf("%f",THR[x]);
i+=5;
gotoxy(i,j);
printf("%f",FTH[x]);
i+=5;
gotoxy(i,j);
printf("%f",AVE[x]);
i+=5;
gotoxy(i,j);
printf("from %s",ADD[x]);
}
}
}
编辑:我不知道我在做什么。我只是想通过八年级的计算机科学。
答案 0 :(得分:0)
该程序应该接受学生的姓氏(LN),名字(FN),中间名(MI),地址(ADD),第一,第二,第三和第四季度(FIR) ,SEC,THR,FTH),计算四个输入等级的平均值,并按字母顺序(姓氏)或降序(平均值)显示所有信息。
您目前使用的设计无法维护和混乱。听起来你需要用一个结构代表一个学生:
#define MAX_LNAME 80
#define MAX_FNAME 32
#define MAX_ADDRESS 128
typedef struct {
char lname[MAX_LNAME];
char fname[MAX_FNAME];
char address[MAX_ADDRESS];
double fir, sec, thr, fth;
} student;
然后是一些将操纵或创建student
结构
student mkstudent(char *lname, char *fname, char *address,
double fir, double sec, double thr, double fth)
{
student ret;
strcpy(ret.lname, lname);
strcpy(ret.fname, fname);
strcpy(ret.address, address);
ret.fir = fir; ret.sec = sec; ret.thr = thr; ret.fth = fth;
return ret;
}
double getAve(const student *s)
{
return (s->fir + s->sec + s->thr + s->fth) / 4.;
}
void print(const student *s)
{
printf("%s, %s from %s: %lf %lf %lf %lf %lf\n", s->lname, s->fname,
s->address, s->fir, s->sec, s->thr, s->fth, getAve(s));
}
您的计划要求对学生进行排序,因此您需要使用qsort
(来自<stdlib.h>
)
int sortAlpha(const void *a, const void *b)
{
return strcmp( ((const student *) a)->lname,
((const student *) b)->lname );
}
int sortAves(const void *pa, const void *pb)
{
const student *a = (const student *) pa;
const student *b = (const student *) pb;
double aAve = getAve(a), bAve = getAve(b);
if (aAve == bAve) return 0;
if (aAve > bAve) return -1;
if (aAve < bAve) return 1;
}
#define SORT_LNAME 0 /* sort by last name */
#define SORT_AVES 1 /* sort by average, in descending order */
/* howSort will be one of the two macros defined above */
void sort(students *roster, size_t n, int howSort)
{
if (howSort == SORT_LNAME)
qsort(roster, n, sizeof *roster, sortAlpha);
else (howSort == SORT_AVES)
qsort(roster, n, sizeof *roster, sortAves);
}
现在您已编写此接口,您可以通过调用上面实现的各种例程来编写主函数
int main()
{
/* array of student structures, possibly read them in from a file,
possibly print them, then ask user how to sort, etc. */
}