❶ c語言結構體編程題,求助大神
#include<stdio.h>
typedef struct __student_info
{
char num[10];
char name[10];
float score;
} student_info;
int main(void)
{
student_info info[5];
printf("請輸入5名學生信息(學號 姓名 成績):
");
for(int i = 0; i < 5; i++)
{
scanf("%s %s %f",info[i].num,info[i].name,&info[i].score);
}
float sum = 0;
for(int i = 0; i < 5; i++)
{
sum += info[i].score;
}
float average = sum/5;
printf("高於平均成績的學生信息如下:
");
for(int i = 0; i < 5; i++)
{
if(info[i].score > average)
{
printf("%s %s %.1f
",info[i].num,info[i].name,info[i].score);
}
}
return 0;
}
❷ c語言使用結構體的一道題
#include<stdio.h>
struct DATE {
int year;
int month;
int day;
};
void push(int d[12],int*year,int*month,int*day) {
if(*day<d[*month-1])
(*day)++;
else if(*day>d[(*month)-1])printf("error input!");
else if(*month<12)(*month)++,*day=1;
else (*year)++,*month=1,*day=1;
}
int main(void)
{
int d1[12]= {31,28,31,30,31,30,31,31,30,31,30,31},d2[12]= {31,29,31,30,31,30,31,31,30,31,30,31};
struct DATE before;
scanf("%d%d%d",&before.year,&before.month,&before.day);
if((before.year%4==0&&before.year%100!=0)||before.year%400==0)push(d2,&before.year,&before.month,&before.day);
else push(d1,&before.year,&before.month,&before.day);
printf("%d年%d月%d日",before.year,before.month,before.day);
return 0;
}
❸ 二級C語言關於結構體的題目
struct
s
{
int
i;
struct
s
*i2;
};
static
struct
s
a[3]={1,
&a[1],2,&a[2],3,&a[0]};
static
struct
s
*ptr;
ptr=&a[1];
A)ptr->i++
B)ptr++->I
C)*ptr->i
D)++ptr->i
A選項:ptr指向的是a[1],ptr->i可表示為a[1].i,也就是2。i++運算是先用後加,所以A表達式的值為2
D選項:++ptr->i的意思是將指針ptr向後移一個元素,再取這個元素中i的值,也就是a[2].i的值3
#include
<stdio.h>
typedef
union
{
long
i;
//4位元組
int
k[5];
//2*5=10位元組
char
c;
//1位元組
}
DATE;
//DATA類型的變數將佔用10位元組
struct
date
{
int
cat;
//2位元組
DATE
cow;
//10位元組
double
dog;//8位元組
}
too;
//too佔用2+10+8=20位元組
DATE
max;
main()
{
printf("%d\n",sizeof(struct
date)+sizeof(max));
sizeof()的功能是求某種數據類型或某個變數所佔位元組數
struct
data類型的數據佔20個位元組。max佔10個位元組。相加為30
}
A)25
B)30
C)18
D)8
❹ C語言結構體編程題,求大神!
已經調試過了,可以直接使用
#include<stdio.h>
#define
N
3
//學生的數目
typedef
struct
{
char
num[20];
char
name[20];
char
area[20];
float
score;
}
student;
student
stu[N];
float
average
=
0;
void
input()
{
int
i,j;
printf("請輸入%d個學生的成績\n",N);
for(i=0;i<N;i++)
{
printf("學號:");
scanf("%s",&stu[i].num);
printf("姓名:");
scanf("%s",&stu[i].name);
printf("籍貫:");
scanf("%s",&stu[i].area);
printf("C語言的成績:\n");
scanf("%f",&stu[i].score);
}
}
void
output()
{
int
i,j;
printf("學生的信息如下:\n");
for(i=0;i<N;i++)
{
printf("學號:%s\n",stu[i].num);
printf("姓名:%s\n",stu[i].name);
printf("籍貫:%s\n",stu[i].area);
printf("成績:%f\n",stu[i].score);
}
printf("平均分:%f\n",average);
}
void
calucate()
{
int
i;
float
total;
for(i=0;i<N;i++)
{
total
=
total
+
stu[i].score;
}
average
=
total/N;
}
int
main()
{
char
a[100]={0};
input();
calucate();
output();
}
❺ C語言結構體編程題,求助大神啊
根據題意:
一、教師信息定義為結構體,其成員年月也為結構體(可直接定義成兩個成員變數,本題沒有強制要求),婚姻狀態採用共用體類型。
二、輸入信息直接寫在main函數,實際軟體開發應單獨寫輸入函數,輸入驗證我只寫了性別和婚姻狀態的驗證,其餘成員的輸入驗證需要自己添加。(實際開發,驗證也應單獨寫函數調用)。
三、輸出單獨函數。
#include <stdio.h>
typedef union maType//結婚狀態共用體
{
int type;//0未婚1已婚2離異
}MTE;
typedef struct s_date
{
int year;
int month;
}SDATE;
typedef struct teacher
{
char idNum[20];
char name[20];
int sex;//0女1男
SDATE date;
MTE mtype;
char dtName[100];//工作部門名稱
}TEER;
void printfTinfo(TEER tInfo);
int main()
{
TEER tInfo;
printf("輸入一名教師信息: ");
printf("工資卡號(20個字元):"),scanf("%s",tInfo.idNum);
printf("姓名(20個字元):"),scanf("%s",tInfo.name);
do
printf("性別(0女1男):"),scanf("%d",&tInfo.sex);
while(tInfo.sex!=0 && tInfo.sex!=1);
printf("出生年月(格式:YYYY MM):"),scanf("%d%d",&tInfo.date.year,&tInfo.date.month);
do
printf("婚姻狀態(0未婚1已婚2離異):"),scanf("%d",&tInfo.mtype.type);
while(tInfo.mtype.type!=0 && tInfo.mtype.type!=1 && tInfo.mtype.type!=2);
printfTinfo(tInfo);
return 0;
}
void printfTinfo(TEER tInfo)
{
printf(" ---------------- 輸入的信息為: ");
printf("工資卡號:%s ",tInfo.idNum);
printf("姓名:%s ",tInfo.name);
printf("性別:%s ",tInfo.sex?"男":"女");
printf("出生年月:%04d-%02d ",tInfo.date.year,tInfo.date.month);
printf("婚姻狀態:");
switch(tInfo.mtype.type)
{
case 0:printf("未婚 ");break;
case 1:printf("已婚 ");break;
case 2:printf("離異 ");break;
}
}
❻ C語言結構體題
#include<stdio.h>
#defineN5
voidinput_stu(structStu*stu);
voidoutput_avg(structStu*stu);
structStu
{
intid;
charname[10];
intsex;
floatmath;
floateng;
floatc;
};
intmain()
{
structStustu[N];
input_stu(stu);
output_avg(stu);
return0;
}
voidinput_stu(structStu*stu)
{
inti=1;
do
{
printf("請輸入第%d個學生的姓名學號性別數學成績英語成績C語言成績:",i);
structStu*s=stu+i-1;
scanf("%s%d%d%f%f%f",s->name,&s->id,&s->sex,&s->math,&s->eng,&s->c);
i++;
}while(i<=N);
}
voidoutput_avg(structStu*stu)
{
inti=1;
printf("%-8s%-8s%-8s%-8s%-8s%-8s%-8s","學號","姓名","性別","高數","英語","C","平均分");
structStu*s=stu+i-1;
floatmath_avg=stu->math,eng_avg=stu->eng,c_avg=stu->c;
putchar(10);
do
{
structStu*s=stu+i-1;
math_avg=((i-1)*math_avg+s->math)/i;
eng_avg=((i-1)*eng_avg+s->eng)/i;
c_avg=((i-1)*c_avg+s->c)/i;
printf("%-8d%-8s%-8s%-8.1f%-8.1f%-8.1f%-8.1f",
s->id,s->name,s->sex==1?"男":"女",s->math,s->eng,s->c,(s->math+s->eng+s->c)/3);
putchar(10);
i++;
}while(i<=N);
printf("%-12s%-12s%-12s%","數學平均分","英語平均分","C平均分");
putchar(10);
printf("%-12.1f%-12.1f%-12.1f",math_avg,eng_avg,c_avg);
}
❼ 二級C語言關於結構體的題目
#include <stdio.h>
#include<string.h>
struct st{
char a[15];
long b;
};
int main ()
{
struct st s[111];
long end[111];
char t[50][14],other[14]={'333'},son[50][14],gson[50][14];
char x[11];
int n;
scanf("%d",&n);
getchar();
for(int i=0;i<n;i++)
{
scanf("%s%ld",s[i].a,&s[i].b);
}
for(int i=0;i<n;i++)//排序
{
for(int j=0;j<n-1-i;j++)
{
if(strcmp(s[j].a,s[j+1].a)>0)
{
strcpy(x,s[j].a);
strcpy(s[j].a,s[j+1].a);
strcpy(s[j+1].a,x);
}
}
}
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
if(strcmp(s[i].a,s[j].a)==0&&s[i].b==s[j].b)
{
strcpy(s[i].a,other);//去名字和號碼同時重復的
s[i].b=0;
}
}
}
int k=0;
for(int i=0;i<n;i++)
{
if(strcmp(s[i].a,other)!=0)
{
strcpy(t[k],s[i].a);
end[k]=s[i].b;
k++;
}
}
for(int i=0;i<k;i++)
{
strcpy(son[i],t[i]);
}
for(int i=0;i<k-1;i++)//在上面的基礎上再去名字重復的;
{
for(int j=i+1;j<k;j++)
{
if(strcmp(son[i],son[j])==0)
{
strcpy(son[i],other);
}
}
}
int word=0;
for(int i=0;i<k;i++)
{
if(strcmp(son[i],other)!=0)
{
strcpy(gson[word],son[i]);
word++;
}
}
printf(" ");
int sum=0;
for(int i=0;i<word;i++)
{
sum=0;
for(int j=0;j<k;j++)
{
if(strcmp(gson[i],t[j])==0)
{
if(sum==0)
printf("%s %ld ",gson[i],end[j]);
if(sum!=0)
printf("%s_%d %ld ",gson[i],sum,end[j]);
sum++;
}
}
}
return 0;
}