用一个数组存放图书信息每本书是一个结构包括下列几项信息书名、作者、出版年月、借出否试写出描述这些信息的说明并编写一个程序读入若干本书的信息然后打印出以上信息。
#include <stdio.h>
struct book{
char name[ 50 ] ;
char zzname[ 50 ] ;
int year;
int month;
char flag;
} liber[ 20 ] ;
int main ( ) {
int i;
for ( i= 0 ; i< 10 ; i++ ) {
printf ( "Input book name:" ) ;
scanf ( "%s" , liber[ i] . name) ;
printf ( "\n" ) ;
printf ( "Input author name:" ) ;
scanf ( "%s" , liber[ i] . zzname) ;
printf ( "\n" ) ;
printf ( "Input publication time:" ) ;
scanf ( "%d%d" , & liber[ i] . year, & liber[ i] . month) ;
printf ( "\n" ) ;
printf ( "Input lending information:" ) ;
scanf ( "%c" , & liber[ i] . flag) ;
printf ( "\n" ) ;
}
for ( i= 0 ; i< 10 ; i++ ) {
printf ( "%s,%s,%d-%d,%c \n" , liber[ i] . name, liber[ i] . zzname, liber[ i] . year, liber[ i] . month, liber[ i] . flag) ;
}
}
编写一个函数统计并打印所输入的正文中的各个英文单词出现的次数并按次数的递减顺序输出。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct list{
char words[ 100 ] ;
int num;
struct list * next;
} ;
typedef struct list node;
typedef node * link;
void count ( char * str) {
link ptr, head, ptrn, headnew;
int i= 0 , j= 0 , prei= 0 ;
char temp[ 20 ] ;
ptr= ( link) malloc ( sizeof ( node) ) ;
head= ptr;
ptr-> next= NULL ;
do {
if ( * ( str+ i) == ' ' || * ( str+ i) == '\0' ) {
temp[ j] = '\0' ;
ptr= head;
while ( ptr-> next!= NULL ) {
if ( strcmp ( ptr-> next-> words, temp) == 0 ) {
ptr-> next-> num++ ;
break ;
}
else {
ptr= ptr-> next;
}
}
if ( ptr-> next== NULL ) {
ptr-> next= ( link) malloc ( sizeof ( node) ) ;
strcpy ( ptr-> next-> words, temp) ;
ptr-> next-> num= 1 ;
ptr-> next-> next= NULL ;
}
if ( * ( str+ 1 ) == '\0' ) break ;
j= 0 ; i++ ;
continue ;
}
temp[ j++ ] = str[ i++ ] ;
} while ( 1 ) ;
ptrn= ( link) malloc ( sizeof ( node) ) ;
headnew= ptrn;
ptrn-> next= NULL ;
ptr= head-> next;
ptrn= headnew;
while ( head-> next!= NULL ) {
while ( ptrn-> next!= NULL && ptrn-> next-> num>= ptr-> num)
ptrn= ptrn-> next;
head-> next= ptr-> next;
ptr-> next= ptrn-> next;
ptrn-> next= ptr;
ptr= head-> next;
ptrn= headnew;
}
ptr= headnew-> next;
while ( ptr!= NULL ) {
puts ( ptr-> words) ;
printf ( "%d\n" , ptr-> num) ;
ptr= ptr-> next;
}
}
int main ( ) {
char str[ 500 ] ;
gets ( str) ;
count ( str) ;
}
编写input()和output()函数输入输出5个学生记录每个记录包括num、name、score[3]。
#include <stdio.h>
struct student{
int num;
char name[ 20 ] ;
int score[ 3 ] ;
} stu[ 5 ] ;
void input ( struct student * pstu) {
int n, j;
for ( n= 0 ; n< 5 ; n++ ) {
printf ( "Input students num:" ) ;
scanf ( "%d" , & pstu[ n] . num) ;
printf ( "\n" ) ;
printf ( "Input students name:" ) ;
scanf ( "%s" , & pstu[ n] . name) ;
printf ( "\n" ) ;
printf ( "Input students score:" ) ;
for ( j= 0 ; j< 3 ; j++ ) {
scanf ( "%d" , & pstu[ n] . score[ j] ) ;
}
printf ( "\n" ) ;
}
}
void output ( struct student stu[ ] ) {
int i= 0 , j;
for ( i; i< 5 ; i++ ) {
printf ( "%d,%s" , stu[ i] . num, stu[ i] . name) ;
for ( j= 0 ; j< 3 ; j++ ) {
printf ( "%3d" , stu[ i] . score[ j] ) ;
}
printf ( "\n" ) ;
}
}
int main ( ) {
void input ( struct student * pstu) ;
void output ( struct student stu[ ] ) ;
input ( stu) ;
output ( stu) ;
}
试利用指向结构体的指针编制一程序实现输入3个学生的学号、数学期中和期末考试成绩。然后计算其平均成绩并输出成绩表。
#include <stdio.h>
struct student
{
long number;
int score[ 3 ] ;
} ;
void input ( struct student* stu)
{
int i;
for ( i= 0 ; i< 3 ; i++ )
{
printf ( "num:" ) ;
scanf ( "%d" , & ( ( stu+ i) -> number) ) ;
printf ( "pleaseinputscore\n" ) ;
scanf ( "%d,%d" , & ( ( stu+ i) -> score[ 0 ] ) , & ( ( stu+ i) -> score[ 1 ] ) ) ;
( stu+ i) -> score[ 2 ] = ( ( stu+ i) -> score[ 0 ] + ( stu+ i) -> score[ 1 ] ) / 2 ;
printf ( "\n" ) ;
}
}
void print ( struct student* stu)
{
int i, j;
printf ( "\nNo.Sco1Sco2Sco3\n" ) ;
for ( i= 0 ; i< 3 ; i++ )
{
printf ( "%-6d" , ( stu+ i) -> number) ;
for ( j= 0 ; j< 3 ; j++ )
printf ( "%-8d" , ( stu+ i) -> score[ j] ) ;
printf ( "\n" ) ;
}
}
int main ( )
{
struct student stu[ 3 ] ;
input ( stu) ;
print ( stu) ;
}
输入某班30位学生的姓名及数学、英语成绩计算每位学生的平均分然后输出平均分最高的学生的姓名及其数学和英语成绩。
#include <stdio.h>
#define SIZE 50
struct student
{
char name[ 10 ] ;
int math, eng;
float aver;
} ;
int main ( )
{
struct student stu[ SIZE] ;
int i, maxstd= 0 ;
for ( i= 0 ; i< SIZE; i++ )
{
scanf ( "%s%d%d" , stu[ i] . name, & stu[ i] . math, & stu[ i] . eng) ;
stu[ i] . aver= ( stu[ i] . eng+ stu[ i] . math) / 2.0 ; }
for ( i= 1 ; i< SIZE; i++ )
if ( stu[ i] . aver> stu[ maxstd] . aver) maxstd= i;
printf ( "%10s%3d%3d\n" , stu[ maxstd] . name, stu[ maxstd] . math, stu[ maxstd] . eng) ;
}
已知学生的记录由学号和学习成绩构成N名学生的数据已存入a结构体数组中。试编写函数fun函数的功能是找出成绩最低的学生记录通过形参返回主函数规定只有一个最低分在主函数中调用fun。
#include <stdio.h>
#define N 10
struct student
{
long number;
int score;
} ;
void fun ( struct student std[ ] , struct student* min)
{
int i, m;
m= std[ 0 ] . score;
for ( i= 0 ; i< N; i++ )
{
if ( std[ i] . score< m)
{
m= std[ i] . score;
min-> number= ( std+ i) -> number;
min-> score= ( std+ i) -> score;
}
}
}
int main ( ) {
struct student a[ N] = { { 1 , 81 } , { 2 , 77 } , { 3 , 63 } , { 4 , 93 } , { 5 , 66 } , { 6 , 53 } , { 7 , 73 } , { 8 , 89 } , { 9 , 85 } , { 10 , 73 } } ;
struct student min;
fun ( a, & min) ;
printf ( "成绩最低的学生记录为:\n学号:%ld,成绩:%d" , min. number, min. score) ;
}
编程序建立一个带有头结点的单向链表链表结点中的数据通过键盘输入当输入数据为-1时表示输入结束。
#include <stdlib.h>
#include <stdio.h>
struct list
{
int data;
struct list* next;
} ;
typedef struct list node;
typedef node* link;
int main ( )
{
link ptr, head;
int num;
ptr= ( link) malloc ( sizeof ( node) ) ;
head= ptr;
printf ( "pleaseinputdata==>\n" ) ;
scanf ( "%d" , & num) ;
while ( num!= - 1 ) {
ptr-> data= num;
scanf ( "%d" , & num) ;
if ( num!= - 1 )
{
ptr-> next= ( link) malloc ( sizeof ( node) ) ;
ptr= ptr-> next;
}
else
{
ptr-> next= NULL ;
break ;
}
}
ptr= head;
while ( ptr!= NULL )
{
printf ( "Thevalueis==>%d\n" , ptr-> data) ;
ptr= ptr-> next;
}
}
建立一个链表每个结点包括学号、姓名、性别、年龄、地址。输入一个学号打印该学号的学生的所有信息。如无此学号则输出“没有找到"
#include <stdlib.h>
#include <stdio.h>
struct node{
long No;
char Name[ 10 ] ;
char sex;
int age;
struct node* next;
} ;
node* createlink ( )
{
struct node* h, * p, * q;
long num;
h= ( struct node* ) malloc ( sizeof ( struct node) ) ;
p= q= h;
printf ( "请输入学生学号(输入0退出输入)!\n" ) ;
scanf ( "%ld" , & num) ;
while ( num!= 0 )
{
p= ( struct node* ) malloc ( sizeof ( struct node) ) ;
p-> No= num;
printf ( "请输入学生姓名\n" ) ;
scanf ( "%s" , p-> Name) ;
printf ( "请输入学生年龄和性别\n" ) ;
scanf ( "%d,%c" , & p-> age, & p-> sex) ;
q-> next= p;
q= p;
printf ( "请输入学生学号(输入0退出输入)!\n" ) ;
scanf ( "%ld" , & num) ;
}
p-> next= NULL ;
return h;
}
void find ( struct node* phead, long num)
{
struct node* p;
if ( phead-> next== NULL )
{
printf ( "没有找到.\n" ) ;
return ;
}
p= phead-> next;
while ( p!= NULL && p-> No!= num)
p= p-> next;
if ( p== NULL )
{
printf ( "没有找到.\n" ) ;
return ;
}
else
printf ( "学生的姓名%s,年龄%d性别%c" , p-> Name, p-> age, p-> sex) ;
}
int main ( )
{
long num;
struct node* phead;
phead= createlink ( ) ;
printf ( "请输入要查找的学生的学号:\n" ) ;
scanf ( "%ld" , & num) ;
find ( phead, num) ;
}
说明一个枚举类型enummonth它的枚举元素为Jan、Feb、…、Dec。编写能显示上个月名称的函数last_month。例如输入Jan时能显示Dec。再编写另一个函数printmon用于打印枚举变量的值枚举元素。最后编写主函数调用上述函数生成一张12个月份及其前一个月份的对照表。
#include <stdio.h>
enum month{ Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec} ;
char * name[ 12 ] = { "Jan" , "Feb" , "Mar" , "Apr" , "May" , "Jun" , "Jul" , "Aug" , "Sep" ,
"Oct" , "Nov" , "Dec" } ;
void last_month ( enum month m1)
{
enum month m2= ( enum month) ( ( ( int ) m1- 1 + 12 ) % 12 ) ;
printf ( "%s" , name[ ( int ) m2] ) ;
}
void printmon ( enum month m)
{
printf ( "%s" , name[ ( int ) m] ) ;
}
int main ( )
{
enum month m;
for ( m= Jan; m<= Dec; m= ( enum month) ( m+ 1 ) )
printmon ( m) ;
printf ( "\n" ) ;
for ( m= Jan; m<= Dec; m= ( enum month) ( m+ 1 ) )
last_month ( m) ;
}
编写程序求解约瑟夫问题有n个小孩围成一圈给他们从1开始编号。现指定从第w个小孩开始报数报到s时出列然后从下个小孩开始重新报数报到s时出列如此重复下去直到所有的小孩都出列。求小孩出列的顺序。
#include <stdio.h>
#include <stdlib.h>
#define N 20
#define W 2
#define S 7
struct node{
int num;
struct node* next;
} ;
int main ( )
{
struct node* head, * p, * q;
int i;
p= q= head= ( struct node* ) malloc ( sizeof ( struct node) ) ;
for ( i= 0 ; i< N; i++ )
{
p-> num= i+ 1 ;
p= ( struct node* ) malloc ( sizeof ( struct node) ) ;
if ( i== N- 1 ) continue ;
q-> next= p;
q= p;
}
q-> next= head;
p= head;
while ( p-> num!= W)
{
q= p;
p= p-> next;
}
printf ( "%d个小孩的从第%d个开始报数报到%d时出列。出列顺序为:\n" , N, W, S) ;
while ( p!= p-> next)
{
for ( i= 1 ; i< S; i++ )
{
q= p;
p= p-> next;
}
printf ( "%-4d" , p-> num) ;
q-> next= p-> next;
free ( p) ;
p= q-> next;
}
printf ( "%-4d\n" , p-> num) ;
free ( p) ;
}
习题8-13有一包含职工编号、年龄和性别的单向链表分别使用函数完成以下功能
建立链表。 分别统计男女职工的人数。 在链表尾部插入新职工。 删除指定编号的职工。 删除60岁以上的男职工和55岁以上的女职工被删除的结点保存到另一个链表 中。在主函数中设计简单的菜单去调用上述函数。
#include <stdlib.h>
#include <stdio.h>
struct node{
long No;
int age;
char sex;
struct node* next;
} ;
node* establish ( )
{
struct node* h, * p, * q;
long num;
h= ( struct node* ) malloc ( sizeof ( struct node) ) ;
p= q= h;
printf ( "请输入职工编号(输入0退出输入)!\n" ) ;
scanf ( "%ld" , & num) ;
while ( num!= 0 )
{
p= ( struct node* ) malloc ( sizeof ( struct node) ) ;
p-> No= num;
printf ( "请输入职工年龄和性别\n" ) ;
scanf ( "%d,%c" , & p-> age, & p-> sex) ;
q-> next= p;
q= p;
printf ( "请输入职工编号(输入0退出输入)!\n" ) ;
scanf ( "%ld" , & num) ;
}
p-> next= NULL ;
return h;
}
void count ( node* phead)
{
int m= 0 , f= 0 ;
struct node* p;
p= phead-> next;
while ( p!= NULL )
{
if ( p-> sex== 'M' || p-> sex== 'm' )
m++ ;
else f++ ;
p= p-> next;
}
printf ( "男职工的人数为:%d\n女职工的人数为:%d\n" , m, f) ;
}
void addnew ( node* phead)
{
struct node* p, * q;
printf ( "请输入新职工编号年龄和性别!\n" ) ;
p= ( struct node* ) malloc ( sizeof ( struct node) ) ;
scanf ( "%ld,%d,%c" , & p-> No, & p-> age, & p-> sex) ;
q= phead-> next;
while ( q-> next!= NULL )
q= q-> next;
q-> next= p;
p-> next= NULL ;
}
void del ( struct node* phead, long num)
{
struct node* p, * q;
if ( phead-> next== NULL )
{
printf ( "Listisnull.\n" ) ;
return ;
}
p= phead-> next;
q= phead;
while ( p!= NULL && p-> No!= num)
{
q= p;
p= p-> next;
}
if ( p== NULL ) return ;
if ( q!= NULL )
{
p= q-> next;
q-> next= p-> next;
free ( p) ;
}
}
node * delToOther ( node * phead)
{
struct node* h, * p, * q, * r;
h= ( struct node* ) malloc ( sizeof ( struct node) ) ;
q= phead;
p= phead-> next;
r= h;
while ( p!= NULL )
{
if ( ( ( p-> sex== 'M' || p-> sex== 'm' ) && p-> age>= 60 ) || ( ( p-> sex== 'F' || p-> sex== 'f' ) && p-> age>= 55 ) )
{
q-> next= p-> next;
r-> next= p;
r= p;
r-> next= NULL ;
p= p-> next;
}
else
{
q= p;
p= p-> next;
}
}
return h;
}
int main ( )
{
char ch;
long num;
struct node* phead, * prest;
printf ( "1.创建链表.\n2.统计男女职工人数.\n3.添加新员工.\n4.删除指定编号的职工.\n5.删除60岁以上的男员工和55岁以上的女员工.\nEnter your choice:12345" ) ;
scanf ( "%d" , & ch) ;
while ( ch!= 0 )
{
switch ( ch)
{
case 1 : phead= establish ( ) ; break ;
case 2 : count ( prest) ; break ;
case 3 : addnew ( phead) ; break ;
case 4 : printf ( "请输入要删除的员工的编号:\n" ) ;
scanf ( "%ld" , & num) ;
del ( phead, num) ; break ;
case 5 : prest= delToOther ( phead) ; break ;
default : break ;
}
printf ( "\n1.创建链表.\n2.统计男女职工人数.\n3.添加新员工.\n4.删除指定编号的职工.\n5.删除60岁以上的男员工和55岁以上的女员工.\nEnter your choice:12345!" ) ;
scanf ( "%d" , & ch) ;
}
}