第八章
用一个数组存放图书信息,每本书是一个结构,包括下列几项信息:书名、作者、出版年月、借出否,试写出描述这些信息的说明,并编写一个程序,读入若干本书的信息,然后打印出以上信息。#include <>
typedef struct
{
char Name[20];
char Author[20];
int Date_Year;
int Date_Month;
int loaned;
} BOOK;
#define N 10
void main()
{
BOOK books[N];
int i;
for (i=0;i<N;i++)
{
printf("Input Book's Name:");
gets(books[i].Name);
printf("Input Book's Author:");
gets(books[i].Author);
printf("Input Book's Year of Publishing:");
scanf("%d",&books[i].Date_Year);
printf("Input Book's Month of Publishing:");
scanf("%d",&books[i].Date_Month);
printf("Input Book's Status, 1-Loaned, 2-Keepin:");
scanf("%d",&books[i].loaned);
}
for (i=0;i<N;i++)
{
printf("Book: %s, Author: %s, Publishing:%d-%d, Status:%d\n", books[i].Name, books[i].Author, books[i].Date_Year, books[i].Date_Month, books[i].loaned);
}
enum c++}
编写一个函数,统计并打印所输入的正文中的各个英文单词出现的次数,并按次数的递减顺序输出。#include <>
#include <>
typedef struct
{
char s[20];
int count;
} WORD;
#define N 100
WORD words[N]={0};
int word_count=0;
int IsSeparator(char c)
{
if ( 'A'<=c && c<='Z' ||
'a'<=c && c<='z' ||
'0'<=c && c<='9' ||
'_'==c ||
'-'==c)
return 0;
else
return 1;
}
void AddWord(char *s)
{
int i,found=0;
char ss[20];
for(i=0;*s && !IsSeparator(*s);s++,i++)
ss[i]=*s;
ss[i]=0;
for (i=0;i<word_count;i++)
if (strcmp(ss, words[i].s)==0)
{
found=1;
break;
}
if (found==0)
{
words[word_count].count=1;
strcpy(words[word_count].s, ss);
word_count++;
}
else
{
words[i].count++;
}
}
void func(char *s)
{
int i,j,t;
char c,lc;
WORD temp;
lc=*s;
if (!IsSeparator(lc))
AddWord(s);
for (i=1;c=*(s+i);i++)
{
if (!IsSeparator(c) && IsSeparator(lc))
AddWord(s+i);
lc=c;
}
for (i=0;i<word_count-1;i++)
{
t=i;
for(j=i+1;j<word_count;j++)
if (words[t].count<words[j].count)
t=j;
if (i!=t)
{
temp=words[t];
words[t]=words[i];
words[i]=temp;
}
}
for (i=0;i<word_count;i++)
{
printf("%s : %d \n", words[i].s, words[i].count);
}
}
void main()
{
char *s="Every C program has a primary main function that must be named main";
func(s);
}
有10个学生,每个学生的数据包括学号、姓名和三门课的成绩。用键盘输入10个学生的数据,要求打印出每个学生三门课的平均成绩,以及最高分的学生的数据(学号、姓名、三门课的成绩和平均分数)。
#include <>
#include <>
typedef struct
{
char name[20];
char sno[20];
int score_1;
int score_2;
int score_3;
double score_all;
} STUDENT;
#define N 10
STUDENT students[N]={0};
void main()
{
int i,max;
for (i=0;i<N;i++)
{
printf("Input #%d Student's Name:");
scanf("%s",students[i].name);
printf("Input #%d Student's SNO:");
scanf("%s",students[i].sno);
printf("Input #%d Student's Score_1:");
scanf("%d",&students[i].score_1);
printf("Input #%d Student's Score_2:");
scanf("%d",&students[i].score_2);
printf("Input #%d Student's Score_3:");
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论