例題:學生的記錄由學號和成績組成,M名學生的資料已在主函式中放入結構體陣列stu中,請撰寫函式fun,它的功能是:函式回傳指定學號的學生資料,指定的學號在主函式中輸入,若沒找到指定學號,在結構體變數中給學號置空串,給成績置-1,作為函式值回傳(用于字串比較的函式是strcmp),
請勿改動主函式main與其它函式中的任何內容,僅在函式fun的花括號中填入所撰寫的若干陳述句,
代碼如下:
#include<stdio.h>
#include<string.h>
#define M 10
#define M 10
typedef struct
{
char num[10];
int s;
}SCORE;
SCORE fun(SCORE*p,char*q)
{
int i;
SCORE t;
for(i=0;i<M;i++)
if(strcmp(p[i].num,q)==0)
{
t=p[i];
break;
}
else
{
strcpy(t.num,"");
t.s=-1;
}
return t;
}
main()
{
SCORE stu[M]={{"G002",69},{"G004",85},{"G001",96},{"G007",72},{"G008",64},{"G006",87},{"G015",85},{"G013",94},{"G012",64},{"G014",91}};
SCORE t;
char m[10];
int i;
FILE*out;
printf("The original data:\n");
for(i=0;i<M;i++)
{
if(i%4==0)
printf("\n");
printf("%s %3d\n",stu[i].num,stu[i].s);
}
printf("\n\nEnter the number:");
gets(m);
t=fun(stu,m);
printf("The data :");
printf("\n%s %4d\n",t.num,t.s);
printf("\n");
out=fopen("outfile.dat","w");
t=fun(stu,"G013");
fprintf(out,"%s %4d\n",t.num,t.s);
fclose(out);
}
輸出運行視窗如下:

越努力越幸運!
加油,奧力給!!!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/243549.html
標籤:其他
上一篇:prometheus監控k8s
