(V
isu
al
C+
+
6.
0
7
——
7
(V
isu
al
C+
+
6.
0
7.1
7.2
7.3
7.4
7.5
(V
isu
al
C+
+
6.
0
7.1
7.1.1
7.1.2
7.1.3
7.1.4
7.1.1
(V
isu
al
C+
+
6.
0
“
1
2
3
1
(V
isu
al
C+
+
6.
0
2
(V
isu
al
C+
+
6.
0,
3
(V
isu
al
C+
+
6.
0
struct box
{ float length;
float width;
float height;
};
struct box4x3=12
struct box box1;
box1struct box12
7.1.2
(V
isu
al
C+
+
6.
0
<>.<>
1
2
3
4
7.1
7.11
(V
isu
al
C+
+
6.
0
7.1l7_1.cpp
#include "stdio.h"
struct stu
{ long int num;
char name[20];
char sex;
float score;
}student1,student2={960002,"Zhang hong li",'W',98},student3;/*student2*/
main()
{ student1.num=960001; /*student1*/
scanf("%s",student1.name);
student1.sex='M';
student1.score=65;
student3=student1; /*student3*/
7.12
(V
isu
al
C+
+
6.
0
printf("\n%ld\t%20s\t%c\t%f",student1.num,student1.name,student1.sex,
student1.score);
printf("\n%ld\t%20s\t%c\t%f",student2.num,student2.name,student2.sex,
student2.score);
printf("\n%ld\t%20s\t%c\t%f",student3.num,student3.name,student3.sex,
student3.score);
}
Lilin< CR >
960001 Lilin M 65
960002 Zhang hong li W 98
960001 Lilin M 65
[7.1]
7.1.3
(V
isu
al
C+
+
6.
0 1
2,
7.2
1
(V
isu
al
C+
+
6.
0
2.
(V
isu
al
C+
+
6.
0
struct <> <>[n]={<>};
n {}”
[ ]”
7.21
(V
isu
al
C+
+
6.
0
7.2l7_2.cpp
#include "stdio.h"
struct stu
{ long int num;
char name[20];
char sex;
float score;
}student[3]={{200001,"Li li",'W',99},{200002,"Wang hai",'M',85},
{200003,"Liu ying",'W',50}};
main()
{ int i,n;
float average,sum;
n=0;
sum=0;
7.221
(V
isu
al
C+
+
6.
0
for(i=0;i<3;i++)
{ sum+=student[i].score;
if(student[i].score<60) n+=1;
}
printf("sum=%f\n",sum);
average=sum/3;
printf("average=%f\ncount=%d\n",average,n);
}
sum=234.000000
average=78.000000
count=1.
7.2
7.1.4
(V
isu
al
C+
+
6.
0
1
2
1
(V
isu
al
C+
+
6.
0
1
(V
isu
al
C+
+
6.
0
(3)
(1)
(2)
(1)
(V
isu
al
C+
+
6.
0
7.3
7.31
(V
isu
al
C+
+
6.
0
7.3l7_3.cpp
#include "stdio.h"
struct stu
{ int num;
char name[20];
char sex;
float score;
}student1={102,"Zhang ping",'M',78.5},*s;
main()
{ s=&student1; /**/
printf("Number=%d\tName=%s\t",student1.num,student1.name);
printf("Sex=%c\tScore=%f\n",student1.sex,student1.score);
printf("Number=%d\tName=%s\t",(*s).num,(*s).name);
printf("Sex=%c\tScore=%f\n",(*s).sex,(*s).score);
printf("Number=%d\tName=%s\t",s->num,s->name);
printf("Sex=%c\tScore=%f\n",s->sex,s->score);
}
7.321
(V
isu
al
C+
+
6.
0
Number=102 Name=Zhang ping Sex=M Score=78.500000
Number=102 Name=Zhang ping Sex=M Score=78.500000
Number=102 Name=Zhang ping Sex=M Score=78.500000
7.3
(2)
(V
isu
al
C+
+
6.
0
7.4
7.4(1)
(V
isu
al
C+
+
6.
0
7.4l7_4.cpp
#include "stdio.h"
struct stu
{ long int num;
char name[20];
char sex;
float score;
}student[3]={{200001,"Li li",'W',99},{200002,"Wang hai",'M',85},
{200003,"Liuying ",'W',50}};
main()
{ struct stu *s;
printf("Num\tName\t\tSex\tScore\t\n");
for(s=student;s<student+3;s++)
printf("%ld\t%-10s\t%c\t%f\t\n",s->num,s->name,s->sex,s->score);
}
7.4(21
(V
isu
al
C+
+
6.
0
Num Name Sex Score
200001 Li li W 99.000000
200002 Wang hai M 85.000000
200003 Liuying W 50.000000.
7.4
(3)
(V
isu
al
C+
+
6.
0
7.5
7.51
(V
isu
al
C+
+
6.
0
7.5l7_5.cpp
#include "stdio.h"
struct stu
{ long int num;
char name[20];
char sex;
float score;
}student[3]={{200001,"Li li",'W',99},{200002,"Wang hai",'M',85},
{200003,"Liuying ",'W',50}};
void average(struct stu *ps)
{ int n=0,i;
float ave,s=0;
7.521
(V
isu
al
C+
+
6.
0
for(i=0;i<3;i++,ps++)
{ s+=ps->score;
if(ps->score<60) n+=1;
}
printf("s=%f\n",s);
ave=s/3;
printf("average=%f\ncount=%d\n",ave,n);
}
main()
{ struct stu *s;
s=student;
average(s);
}
s=234.000000
average=78.000000
count=1.
7.5
2
(V
isu
al
C+
+
6.
0
(1)
(2)
(1)
(V
isu
al
C+
+
6.
0
malloc
calloc
free
7.6
malloc
(V
isu
al
C+
+
6.
0
”
”
calloc
(V
isu
al
C+
+
6.
0
”
free
(V
isu
al
C+
+
6.
0
7.61
(V
isu
al
C+
+
6.
0
7.6l7_6.cpp
#include "stdio.h"
#include "malloc.h"
#include "string.h"
main()
{ struct stu
{ int num;
char name[20];
char sex;
float score;
}*s;
s=(struct stu*)malloc(sizeof(struct stu));
7.621
(V
isu
al
C+
+
6.
0
s->num=102;
strcpy(s->name,"Zhang ping");
s->sex='M';
s->score=62.5;
printf("Number=%d\nName=%s\n",s->num,s->name);
printf("Sex=%c\nScore=%f\n",s->sex,s->score);
free(s);
}
Num=102
Name=Zhang ping
Sex=M
Score=62.500000
7.6
(2)
(V
isu
al
C+
+
6.
0
(2)
(V
isu
al
C+
+
6.
0
1
(V
isu
al
C+
+
6.
0
21
(V
isu
al
C+
+
6.
0
32
(V
isu
al
C+
+
6.
0
7-7
7-8
7-9(a)
7-9(b)
7-9(c)
7-10(a)
7-10(b)
7-10(c)
7-11
(V
isu
al
C+
+
6.
0
7-12
(V
isu
al
C+
+
6.
0
(V
isu
al
C+
+
6.
0
(V
isu
al
C+
+
6.
0
7-13
7-14(a)
7-14(b)
7-14(c)
(V
isu
al
C+
+
6.
0
(V
isu
al
C+
+
6.
0
(V
isu
al
C+
+
6.
0
(V
isu
al
C+
+
6.
0
7-15(a)
7-15(b)
7-15(c)
7-15(d)
7.11
(V
isu
al
C+
+
6.
0
7.11mainl7_7891011.cpp
main
#include "stdio.h"
main()
{ struct stu * head,stud;
long int num;
printf("input records:\n ");
head=creat(); /*creathead*/
print(head); /*print*/
printf("Input the deleted number,");
scanf(“%ld”,&num); /**/
head=dele(head,num); /*dele*/
7.11
(V
isu
al
C+
+
6.
0
print(head); /*print*/
printf("Input the inserted number and score,");
scanf("%ld,%f",&stud.num,&stud.score); /**/
head=insert(head,&stu); /* insertpnum*/
print(head); /*print*/
}
input records:
96001,85< CR >
96002,75.2< CR >
96005,62< CR >
0,0< CR >
7.11
(V
isu
al
C+
+
6.
0
Now,these 3 records are:
96001,85.000000
96002,75.199997
96005,62.000000
Input the deleted number,96002 < CR >
Now,these 2 records are:
96001,85.000000
96005,62.000000
Input the insert number and score,96004,78< CR >
Now,these 3 records are:
96001,85.000000
96004,78.000000
96005,62.000000
7.11
(V
isu
al
C+
+
6.
0
7.2
7.2.1
7.2.2
C
7.12
7.2.1
(V
isu
al
C+
+
6.
0
1
2
1
(V
isu
al
C+
+
6.
0
2
(V
isu
al
C+
+
6.
0
7.2.2
(V
isu
al
C+
+
6.
0
1
2
1
(V
isu
al
C+
+
6.
0
2
(V
isu
al
C+
+
6.
0
7.12
(V
isu
al
C+
+
6.
0
7.127-1l7_12.cpp
#include "stdio.h"
main()
{ union t
{ char *name;
int age;
int income;
};
union t list;
printf("%d\n",sizeof(union t*)); /**/
list.name="Zhang hai"; /**/
printf("%s\t",list.name);
list.age=20; /**/
printf("%d\t",list.age);
list.income=2500; /**/
printf("%d\n",list.income);
printf("%s\t%d\t%d\n",list.name,list.age,list.income);/**/
}
7.12
(V
isu
al
C+
+
6.
0
7.3
7.3.1
7.3.2
7.14
7.3.1
(V
isu
al
C+
+
6.
0
1
2
1
(V
isu
al
C+
+
6.
0
”
2
(V
isu
al
C+
+
6.
0
7.3.2
(V
isu
al
C+
+
6.
0
…
……
7.141
(V
isu
al
C+
+
6.
0
7.14zhaowangzhangli31zhanglt7_14.cpp
#include "stdio.h"
main()
{ enum body
{ zhao,wang,zhang,li
}month[31],j;
int i;
j=zhang;
for(i=1;i<=30;i++)
{ month[i]=j;
j=(enum body)(j+1); /**/
if (j>li)
j=zhao;
}
7.1421
(V
isu
al
C+
+
6.
0
for(i=1;i<=30;i++)
{ switch(month[i])
{ case zhao:printf(" %2d %6s\t",i,"zhao"); break;
case wang:printf(" %2d %6s\t",i,"wang"); break;
case zhang:printf(" %2d %6s\t",i,"zhang"); break;
case li:printf(" %2d %6s\t",i,"li"); break;
default:break;
}
}
printf("\n");
}
7.14
(V
isu
al
C+
+
6.
0
7.4
1
2
3typedef
(V
isu
al
C+
+
6.
0
1
(1)
typedef <> <>
(2)
(int a;)
aINTEGER
typedeftypedef int INTEGER
(V
isu
al
C+
+
6.
0
2
(1)typedef
typedef char NAME[20];NAME20
NAMENAME a1,a2,s1,s2;
char a1[20],a2[20],s1[20],s2[20]
STUstruct stuSTU
STU body1,body2;
(2)typedeftypedef
(V
isu
al
C+
+
6.
0
3typedef
1typedef
2typedef
3typedeftypedef
(V
isu
al
C+
+
6.
0
7.5
7.15
7.16
7.15(1)
(V
isu
al
C+
+
6.
0
7.157-2l7_15.cpp
#include "stdio.h"
struct
{ int num;
char name[10];
char job;
union
{ int classnum;
char office[10];
} depa;
}body[2];
7.15(21
(V
isu
al
C+
+
6.
0
main()
{ int n,i;
for(i=0;i<2;i++)
{ printf("input num,name,job and department\n");
scanf("%d %s %c",&body[i].num,body[i].name,&body[i].job);
if(body[i].job=='s')
scanf("%d",&body[i].depa.classnum);
else if (body[i].job=='t')
scanf("%s",body[i].depa.office);
else
printf( "input error");
}
printf("num\tname\t\tjob\t department \n");
7.15(32
(V
isu
al
C+
+
6.
0
for(i=0;i<2;i++)
{ if(body[i].job=='s')
printf("%d\t%s\t%c\t%d\n",body[i].num,body[i].name,body[i].job,
body[i].depa.classnum);
else
printf("%d\t%s\t%c\t%s\n",body[i].num,body[i].name,body[i].job,
body[i].depa.office);
}
}
7.15(43
(V
isu
al
C+
+
6.
0
input num,name,job,and department
9601 zhanghai s 302< CR >
input num,name,job,and department
9603 lixiaoming t jiaowuke < CR >
num name job department
9601 zhanghai s 302
9603 lixiaoming t jiaowuke
7.15
7.161
(V
isu
al
C+
+
6.
0
7.163l7_16.cpp
ijk5i≠j≠k
#include "stdio.h"
main()
{ enum color{red,yellow,blue,white,black}i,j,k,pri;
int n,loop;
n=0;
for(i=red;i<=black;i=(enum color)(i+1))
for(j=red;j<=black; j=(enum color)(j+1))
if(i!=j)
7.1621
(V
isu
al
C+
+
6.
0
{ for(k=red;k<=black; k=(enum color)(k+1))
if((k!=i)&&(k!=j))
{ n=n+1;
printf("%-4d",n);
for(loop=1;loop<=3;loop++)
{ switch(loop)
{ case 1:pri=i; break;
case 2:pri=j; break;
case 3:pri=k; break;
default:break;
}
7.1632
(V
isu
al
C+
+
6.
0
switch(pri)
{ case red:printf("%-10s","red");break;
case yellow:printf("%-10s","yellow");break;
case blue:printf("%-10s","blue");break;
case white:printf("%-10s","white");break;
case black:printf("%-10s","black");break;
default:break;
}
}
printf("\n");
}
}
printf("\ntotal:%5d\n",n);
}
7.16
isu
al
C+
+
6.
0
7
——
7
(V
isu
al
C+
+
6.
0
7.1
7.2
7.3
7.4
7.5
(V
isu
al
C+
+
6.
0
7.1
7.1.1
7.1.2
7.1.3
7.1.4
7.1.1
(V
isu
al
C+
+
6.
0
“
1
2
3
1
(V
isu
al
C+
+
6.
0
2
(V
isu
al
C+
+
6.
0,
3
(V
isu
al
C+
+
6.
0
struct box
{ float length;
float width;
float height;
};
struct box4x3=12
struct box box1;
box1struct box12
7.1.2
(V
isu
al
C+
+
6.
0
<>.<>
1
2
3
4
7.1
7.11
(V
isu
al
C+
+
6.
0
7.1l7_1.cpp
#include "stdio.h"
struct stu
{ long int num;
char name[20];
char sex;
float score;
}student1,student2={960002,"Zhang hong li",'W',98},student3;/*student2*/
main()
{ student1.num=960001; /*student1*/
scanf("%s",student1.name);
student1.sex='M';
student1.score=65;
student3=student1; /*student3*/
7.12
(V
isu
al
C+
+
6.
0
printf("\n%ld\t%20s\t%c\t%f",student1.num,student1.name,student1.sex,
student1.score);
printf("\n%ld\t%20s\t%c\t%f",student2.num,student2.name,student2.sex,
student2.score);
printf("\n%ld\t%20s\t%c\t%f",student3.num,student3.name,student3.sex,
student3.score);
}
Lilin< CR >
960001 Lilin M 65
960002 Zhang hong li W 98
960001 Lilin M 65
[7.1]
7.1.3
(V
isu
al
C+
+
6.
0 1
2,
7.2
1
(V
isu
al
C+
+
6.
0
2.
(V
isu
al
C+
+
6.
0
struct <> <>[n]={<>};
n {}”
[ ]”
7.21
(V
isu
al
C+
+
6.
0
7.2l7_2.cpp
#include "stdio.h"
struct stu
{ long int num;
char name[20];
char sex;
float score;
}student[3]={{200001,"Li li",'W',99},{200002,"Wang hai",'M',85},
{200003,"Liu ying",'W',50}};
main()
{ int i,n;
float average,sum;
n=0;
sum=0;
7.221
(V
isu
al
C+
+
6.
0
for(i=0;i<3;i++)
{ sum+=student[i].score;
if(student[i].score<60) n+=1;
}
printf("sum=%f\n",sum);
average=sum/3;
printf("average=%f\ncount=%d\n",average,n);
}
sum=234.000000
average=78.000000
count=1.
7.2
7.1.4
(V
isu
al
C+
+
6.
0
1
2
1
(V
isu
al
C+
+
6.
0
1
(V
isu
al
C+
+
6.
0
(3)
(1)
(2)
(1)
(V
isu
al
C+
+
6.
0
7.3
7.31
(V
isu
al
C+
+
6.
0
7.3l7_3.cpp
#include "stdio.h"
struct stu
{ int num;
char name[20];
char sex;
float score;
}student1={102,"Zhang ping",'M',78.5},*s;
main()
{ s=&student1; /**/
printf("Number=%d\tName=%s\t",student1.num,student1.name);
printf("Sex=%c\tScore=%f\n",student1.sex,student1.score);
printf("Number=%d\tName=%s\t",(*s).num,(*s).name);
printf("Sex=%c\tScore=%f\n",(*s).sex,(*s).score);
printf("Number=%d\tName=%s\t",s->num,s->name);
printf("Sex=%c\tScore=%f\n",s->sex,s->score);
}
7.321
(V
isu
al
C+
+
6.
0
Number=102 Name=Zhang ping Sex=M Score=78.500000
Number=102 Name=Zhang ping Sex=M Score=78.500000
Number=102 Name=Zhang ping Sex=M Score=78.500000
7.3
(2)
(V
isu
al
C+
+
6.
0
7.4
7.4(1)
(V
isu
al
C+
+
6.
0
7.4l7_4.cpp
#include "stdio.h"
struct stu
{ long int num;
char name[20];
char sex;
float score;
}student[3]={{200001,"Li li",'W',99},{200002,"Wang hai",'M',85},
{200003,"Liuying ",'W',50}};
main()
{ struct stu *s;
printf("Num\tName\t\tSex\tScore\t\n");
for(s=student;s<student+3;s++)
printf("%ld\t%-10s\t%c\t%f\t\n",s->num,s->name,s->sex,s->score);
}
7.4(21
(V
isu
al
C+
+
6.
0
Num Name Sex Score
200001 Li li W 99.000000
200002 Wang hai M 85.000000
200003 Liuying W 50.000000.
7.4
(3)
(V
isu
al
C+
+
6.
0
7.5
7.51
(V
isu
al
C+
+
6.
0
7.5l7_5.cpp
#include "stdio.h"
struct stu
{ long int num;
char name[20];
char sex;
float score;
}student[3]={{200001,"Li li",'W',99},{200002,"Wang hai",'M',85},
{200003,"Liuying ",'W',50}};
void average(struct stu *ps)
{ int n=0,i;
float ave,s=0;
7.521
(V
isu
al
C+
+
6.
0
for(i=0;i<3;i++,ps++)
{ s+=ps->score;
if(ps->score<60) n+=1;
}
printf("s=%f\n",s);
ave=s/3;
printf("average=%f\ncount=%d\n",ave,n);
}
main()
{ struct stu *s;
s=student;
average(s);
}
s=234.000000
average=78.000000
count=1.
7.5
2
(V
isu
al
C+
+
6.
0
(1)
(2)
(1)
(V
isu
al
C+
+
6.
0
malloc
calloc
free
7.6
malloc
(V
isu
al
C+
+
6.
0
”
”
calloc
(V
isu
al
C+
+
6.
0
”
free
(V
isu
al
C+
+
6.
0
7.61
(V
isu
al
C+
+
6.
0
7.6l7_6.cpp
#include "stdio.h"
#include "malloc.h"
#include "string.h"
main()
{ struct stu
{ int num;
char name[20];
char sex;
float score;
}*s;
s=(struct stu*)malloc(sizeof(struct stu));
7.621
(V
isu
al
C+
+
6.
0
s->num=102;
strcpy(s->name,"Zhang ping");
s->sex='M';
s->score=62.5;
printf("Number=%d\nName=%s\n",s->num,s->name);
printf("Sex=%c\nScore=%f\n",s->sex,s->score);
free(s);
}
Num=102
Name=Zhang ping
Sex=M
Score=62.500000
7.6
(2)
(V
isu
al
C+
+
6.
0
(2)
(V
isu
al
C+
+
6.
0
1
(V
isu
al
C+
+
6.
0
21
(V
isu
al
C+
+
6.
0
32
(V
isu
al
C+
+
6.
0
7-7
7-8
7-9(a)
7-9(b)
7-9(c)
7-10(a)
7-10(b)
7-10(c)
7-11
(V
isu
al
C+
+
6.
0
7-12
(V
isu
al
C+
+
6.
0
(V
isu
al
C+
+
6.
0
(V
isu
al
C+
+
6.
0
7-13
7-14(a)
7-14(b)
7-14(c)
(V
isu
al
C+
+
6.
0
(V
isu
al
C+
+
6.
0
(V
isu
al
C+
+
6.
0
(V
isu
al
C+
+
6.
0
7-15(a)
7-15(b)
7-15(c)
7-15(d)
7.11
(V
isu
al
C+
+
6.
0
7.11mainl7_7891011.cpp
main
#include "stdio.h"
main()
{ struct stu * head,stud;
long int num;
printf("input records:\n ");
head=creat(); /*creathead*/
print(head); /*print*/
printf("Input the deleted number,");
scanf(“%ld”,&num); /**/
head=dele(head,num); /*dele*/
7.11
(V
isu
al
C+
+
6.
0
print(head); /*print*/
printf("Input the inserted number and score,");
scanf("%ld,%f",&stud.num,&stud.score); /**/
head=insert(head,&stu); /* insertpnum*/
print(head); /*print*/
}
input records:
96001,85< CR >
96002,75.2< CR >
96005,62< CR >
0,0< CR >
7.11
(V
isu
al
C+
+
6.
0
Now,these 3 records are:
96001,85.000000
96002,75.199997
96005,62.000000
Input the deleted number,96002 < CR >
Now,these 2 records are:
96001,85.000000
96005,62.000000
Input the insert number and score,96004,78< CR >
Now,these 3 records are:
96001,85.000000
96004,78.000000
96005,62.000000
7.11
(V
isu
al
C+
+
6.
0
7.2
7.2.1
7.2.2
C
7.12
7.2.1
(V
isu
al
C+
+
6.
0
1
2
1
(V
isu
al
C+
+
6.
0
2
(V
isu
al
C+
+
6.
0
7.2.2
(V
isu
al
C+
+
6.
0
1
2
1
(V
isu
al
C+
+
6.
0
2
(V
isu
al
C+
+
6.
0
7.12
(V
isu
al
C+
+
6.
0
7.127-1l7_12.cpp
#include "stdio.h"
main()
{ union t
{ char *name;
int age;
int income;
};
union t list;
printf("%d\n",sizeof(union t*)); /**/
list.name="Zhang hai"; /**/
printf("%s\t",list.name);
list.age=20; /**/
printf("%d\t",list.age);
list.income=2500; /**/
printf("%d\n",list.income);
printf("%s\t%d\t%d\n",list.name,list.age,list.income);/**/
}
7.12
(V
isu
al
C+
+
6.
0
7.3
7.3.1
7.3.2
7.14
7.3.1
(V
isu
al
C+
+
6.
0
1
2
1
(V
isu
al
C+
+
6.
0
”
2
(V
isu
al
C+
+
6.
0
7.3.2
(V
isu
al
C+
+
6.
0
…
……
7.141
(V
isu
al
C+
+
6.
0
7.14zhaowangzhangli31zhanglt7_14.cpp
#include "stdio.h"
main()
{ enum body
{ zhao,wang,zhang,li
}month[31],j;
int i;
j=zhang;
for(i=1;i<=30;i++)
{ month[i]=j;
j=(enum body)(j+1); /**/
if (j>li)
j=zhao;
}
7.1421
(V
isu
al
C+
+
6.
0
for(i=1;i<=30;i++)
{ switch(month[i])
{ case zhao:printf(" %2d %6s\t",i,"zhao"); break;
case wang:printf(" %2d %6s\t",i,"wang"); break;
case zhang:printf(" %2d %6s\t",i,"zhang"); break;
case li:printf(" %2d %6s\t",i,"li"); break;
default:break;
}
}
printf("\n");
}
7.14
(V
isu
al
C+
+
6.
0
7.4
1
2
3typedef
(V
isu
al
C+
+
6.
0
1
(1)
typedef <> <>
(2)
(int a;)
aINTEGER
typedeftypedef int INTEGER
(V
isu
al
C+
+
6.
0
2
(1)typedef
typedef char NAME[20];NAME20
NAMENAME a1,a2,s1,s2;
char a1[20],a2[20],s1[20],s2[20]
STUstruct stuSTU
STU body1,body2;
(2)typedeftypedef
(V
isu
al
C+
+
6.
0
3typedef
1typedef
2typedef
3typedeftypedef
(V
isu
al
C+
+
6.
0
7.5
7.15
7.16
7.15(1)
(V
isu
al
C+
+
6.
0
7.157-2l7_15.cpp
#include "stdio.h"
struct
{ int num;
char name[10];
char job;
union
{ int classnum;
char office[10];
} depa;
}body[2];
7.15(21
(V
isu
al
C+
+
6.
0
main()
{ int n,i;
for(i=0;i<2;i++)
{ printf("input num,name,job and department\n");
scanf("%d %s %c",&body[i].num,body[i].name,&body[i].job);
if(body[i].job=='s')
scanf("%d",&body[i].depa.classnum);
else if (body[i].job=='t')
scanf("%s",body[i].depa.office);
else
printf( "input error");
}
printf("num\tname\t\tjob\t department \n");
7.15(32
(V
isu
al
C+
+
6.
0
for(i=0;i<2;i++)
{ if(body[i].job=='s')
printf("%d\t%s\t%c\t%d\n",body[i].num,body[i].name,body[i].job,
body[i].depa.classnum);
else
printf("%d\t%s\t%c\t%s\n",body[i].num,body[i].name,body[i].job,
body[i].depa.office);
}
}
7.15(43
(V
isu
al
C+
+
6.
0
input num,name,job,and department
9601 zhanghai s 302< CR >
input num,name,job,and department
9603 lixiaoming t jiaowuke < CR >
num name job department
9601 zhanghai s 302
9603 lixiaoming t jiaowuke
7.15
7.161
(V
isu
al
C+
+
6.
0
7.163l7_16.cpp
ijk5i≠j≠k
#include "stdio.h"
main()
{ enum color{red,yellow,blue,white,black}i,j,k,pri;
int n,loop;
n=0;
for(i=red;i<=black;i=(enum color)(i+1))
for(j=red;j<=black; j=(enum color)(j+1))
if(i!=j)
7.1621
(V
isu
al
C+
+
6.
0
{ for(k=red;k<=black; k=(enum color)(k+1))
if((k!=i)&&(k!=j))
{ n=n+1;
printf("%-4d",n);
for(loop=1;loop<=3;loop++)
{ switch(loop)
{ case 1:pri=i; break;
case 2:pri=j; break;
case 3:pri=k; break;
default:break;
}
7.1632
(V
isu
al
C+
+
6.
0
switch(pri)
{ case red:printf("%-10s","red");break;
case yellow:printf("%-10s","yellow");break;
case blue:printf("%-10s","blue");break;
case white:printf("%-10s","white");break;
case black:printf("%-10s","black");break;
default:break;
}
}
printf("\n");
}
}
printf("\ntotal:%5d\n",n);
}
7.16