086.把一个学生的信息(包括学号、姓名、性别、住址)放在一个结构体变量中,然后输出这个学生的信息。
#include<stdio.h>
int main()
{
struct Student
{
long int num;
char name[20];
char sex;
char addr[30];
}student1={1713010948,"guochuanchuan",'M',"software college of NUC"};
printf("NO:%d\nname:%s\nsex:%c\naddress:%s\n",student1.num,student1.name,student1.sex,student1.addr);
return 0;
}
087.输入两个学生的学号、姓名和成绩,输出成绩较高的学生的学号、姓名和成绩。
#include<stdio.h>
int main()
{
struct Student
{
long int num;
char name[20];
int score;
}student1,student2;
scanf("%d%s%d",&student1.num,student1.name,&student1.score);//输入学生一的数据
scanf("%d%s%d",&student2.num,student2.name,&student2.score);//输入学生二的数据
if(student1.score > student2.score)
{
printf("NO:%d\nName:%s\nScore:%d\n",student1.num,student1.name,student1.score);
}
else if(student1.score < student2.score)
{
printf("NO:%d\nName:%s\nScore:%d\n",student2.num,student2.name,student2.score);
}
else
{
printf("NO:%d\nName:%s\nScore:%d\n",student1.num,student1.name,student1.score);
printf("NO:%d\nName:%s\nScore:%d\n",student2.num,student2.name,student2.score);
}
return 0;
}
088.有3个候选人,每个选民只能投票选一人,要求编一个统计选票的程序,先后输入被选人的名字,最后输出个人得票结果。
#include<stdio.h>
#include<string.h>
struct Person
{
char name[20];
int count;
}leader[3] = {"Li",0,"Zhang",0,"Sun",0};
int main()
{
int i,j;
char leader_name[20];
for(i=0;i<5;i++)
{
scanf("%s",leader_name);
for(j=0;j<3;j++)
{
if(strcmp(leader_name,leader[j].name) == 0)
{
leader[j].count++;
}
}
}
printf("\n");
for(i=0;i<3;i++)
{
printf("%s:%d\n",leader[i].name,leader[i].count);
}
}
089.有n个学生的信息(包括学号、姓名、成绩),要求按照成绩的高低顺序输出个学生的信息。
#include<stdio.h>
struct Student
{
int num;
char name[20];
int score;
};
int main()
{
struct Student stu[5] = {100,"Li",99,102,"Zhang",100,103,"Guo",100,104,"Sun",88,105,"Qian",34};
//或者下面的方式更加直观:
// struct Student stu[5] = {{100,"Li",99},{102,"Zhang",100},{103,"Guo",100},{104,"Sun",88},{105,"Qian",34}};
struct Student temp;
const int n = 5;
int i,j,k;
for(i=0;i<n-1;i++)
{
k=i;
for(j=i+1;j<n;j++)
{
if(stu[j].score > stu[k].score)
{
k = j;
}
}
temp = stu[i];
stu[i] = stu[k];
stu[k] = temp;
}
for(i=0;i<n;i++)
{
printf("%d\t%-15s%d\n",stu[i].num,stu[i].name,stu[i].score);
}
return 0;
}
090.通过指向结构体变量的指针变量输出结构体变量中成员的信息。
#include<stdio.h>
#include<string.h>
struct Student
{
long num;
char name[20];
char sex;
float score;
};
int main()
{
struct Student stu_1;
struct Student *p;
p = &stu_1;
stu_1.num = 10101;
strcpy(stu_1.name,"Li Lin");
stu_1.sex = 'M';
stu_1.score = 89.5;
printf("NO.%d\nname:%s\nsex:%c\nscore:%.1f\n",stu_1.num,stu_1.name,stu_1.sex,stu_1.score);
printf("\n");
printf("NO.%d\nname:%s\nsex:%c\nscore:%.1f\n",(*p).num,(*p).name,(*p).sex,(*p).score);
return 0;
}
091.有三个学生的信息,放在结构体数组中,要求输出全部学生的信息。
#include<stdio.h>
#include<string.h>
struct Student
{
int num;
char name[20];
char sex;
int age;
};
int main()
{
struct Student stu[3] = {{100,"Li",'M',99},{102,"Zhang",'M',100},{103,"Guo",'M',100}};
struct Student *p;
for(p=stu;p<stu+3;p++)
{
printf("%-5d%-10s%-5c%-4d\n",p->num,p->name,p->sex,p->age);
}
return 0;
}
092.有n个结构体变量,内含学生学号、姓名和3门课程的成绩。要求输出平均成绩最高的的学生的信息(包括学号、姓名、3门课程成绩和平均成绩)。
#include<stdio.h>
#define N 3
struct Student
{
int num;
char name[20];
float score[3];
float aver;
};
int main()
{
void input(struct Student stu[]);
struct Student max(struct Student stu[]);
void print(struct Student stu);
struct Student stu[N], *p = stu;
input(p);
print(max(p));
return 0;
}
void input(struct Student stu[])
{
int i;
for(i=0;i<N;i++)
{
scanf("%d%s%f%f%f",&stu[i].num,stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
stu[i].aver = (stu[i].score[0]+stu[i].score[1]+stu[i].score[2])/3.0;
}
}
struct Student max(struct Student stu[])
{
int i,m=0;
for(i=0;i<N;i++)
{
if(stu[i].aver > stu[m].aver)
{
m = i;
}
}
return stu[m];
}
void print(struct Student stu)
{
printf("\n成绩最高的学生是:\n");
printf("学号:%d\n姓名:%s\n三门课成绩:%.1f,%.1f,%.1f\n平均成绩:%.1f",
stu.num,stu.name,stu.score[0],stu.score[1],stu.score[2],stu.aver);
}
093.建立一个简单链表,它由3个学生数据的结点组成,要求输出各结点中的数据。
#include<stdio.h>
struct Student
{
int num;
int score;
struct Student *next;
};
int main()
{
struct Student a,b,c,*head,*p;
a.num = 1001;
a.score = 98;
b.num = 1002;
b.score = 95;
c.num = 1003;
c.score = 92;
head = &a;
a.next = &b;
b.next = &c;
c.next = NULL;
p = head;
do
{
printf("%d%5d\n",p->num,p->score);
p = p->next;
}
while(p != NULL);
}
094.写一个函数建立一个有3名学生数据的单向动态链表。
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct Student)
struct Student
{
int num;
int score;
struct Student *next;
};
struct Student *creat()
{
int n;
struct Student *head,*p1,*p2;
n=0;
p1 = p2 = (struct Student *)malloc(LEN);
scanf("%d%d",&p1->num,&p1->score);
head = NULL;
while(p1->num != 0)
{
n=n+1;
if(n == 1)
{
head = p1;
}
else
{
p2->next = p1;
}
p2 = p1;
p1 = (struct Student *)malloc(LEN);
scanf("%d%d",&p1->num,&p1->score);
}
p2->next = NULL;
return head;
}
int main()
{
struct Student *pt;
pt = creat();
printf("%d%6d",pt->num,pt->score);
return 0;
}
095.对前一题添加输出链表数据的功能。
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct Student)
struct Student
{
int num;
int score;
struct Student *next;
};
struct Student *creat()
{
int n;
struct Student *head,*p1,*p2;
n=0;
p1 = p2 = (struct Student *)malloc(LEN);
scanf("%d%d",&p1->num,&p1->score);
head = NULL;
while(p1->num != 0)
{
n=n+1;
if(n == 1)
{
head = p1;
}
else
{
p2->next = p1;
}
p2 = p1;
p1 = (struct Student *)malloc(LEN);
scanf("%d%d",&p1->num,&p1->score);
}
p2->next = NULL;
return head;
}
void print(struct Student *head)
{
struct Student *p;
p = head;
if(head != NULL)
{
do
{
printf("%d%6d\n",p->num,p->score);
p = p->next;
}
while(p != NULL);
}
}
int main()
{
struct Student *pt;
pt = creat();
printf("%d%6d\n",pt->num,pt->score);
print(pt);
return 0;
}
096.有若干个人员的数据,其中有学生和教师。学生的数据中包括:姓名、号码、性别、职业、班级。教师的数据包括:姓名、号码、性别、职业、职务。要求用同一个表格来处理。
#include<stdio.h>
struct
{
int num;
char name[10];
char sex;
char job;
union
{
int clas;
char position[10];
} category;
} person[2];
int main()
{
int i;
for(i=0;i<2;i++)
{
scanf("%d %s %c %c",&person[i].num,person[i].name,&person[i].sex,&person[i].job);
if(person[i].job == 's')
{
scanf("%d",&person[i].category.clas);
}
else
{
scanf("%s",person[i].category.position);
}
}
for(i=0;i<2;i++)
{
if(person[i].job == 's')
{
printf("%d\t%s\t%c\t%c\t%d\n",person[i].num,person[i].name,person[i].sex,person[i].job,person[i].category.clas);
}
else
{
printf("%d\t%s\t%c\t%c\t%s\n",person[i].num,person[i].name,person[i].sex,person[i].job,person[i].category.position);
}
}
return 0;
}
097.口袋中有红、黄、蓝、白、黑5种颜色的球若干个。每次从口袋中先后取出3个球,问得到3种不同颜色的球的可能取法,输出每种排列的情况。
#include<stdio.h>
int main()
{
enum Color
{
red,yellow,blue,white,black
};
enum Color i,j,k,pri;
int n,loop;
n=0;
for(i=red;i<=black;i++)
{
for(j=red;j<=black;j++)
{
if(i != j)
{
for(k=red;k<=black;k++)
{
if(i!=k && j!=k)
{
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;
}
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("total:%d",n);
}
098.从键盘输入一些字符,并逐个把他们送到磁盘上去,直到用户输入一个“#”为止。
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp;
char ch,filename[10];
printf("请输入所用的文件名:");
scanf("%s",filename);
getchar();
if((fp=fopen(filename,"w")) == NULL)
{
printf("cannot open file\n");
exit(0);
}
printf("请输入一个准备存储到磁盘的字符串(以#结束):");
ch = getchar();
while(ch!='#')
{
fputc(ch,fp);
putchar(ch);
ch = getchar();
}
fclose(fp);
putchar(10);
return 0;
}
099.将一个磁盘文件中的信息复制到另一个磁盘文件中。今要求将上例建立的file1.dat文件中的内容复制到另一个磁盘文件file2.dat中
在这里插入代码片
101.从键盘读入若干个字符串,对他们按字母大小的顺序排序,然后把排好的字符串送到磁盘文件中保存。
在这里插入代码片