No.1 有5个学生,每个学生有3门课的成绩,从键盘输入数据(包括学生号,姓名,3门课的成绩),计算出平均成绩,将原有数据和计算出的平均分数存放在磁盘文件stud中。
程序:
#include<stdio.h>
#include<stdlib.h>
struct student
{
char num[10]
char name[8]
int score[3]
float ave
}stu[5]
int main()
{
int i,j,sum
FILE *fp
for(i=0i<5i++)
{
printf("\ninput score of student %d:\n",i+1)
printf("No.:")
scanf("%s",stu[i].num)
printf("name:")
scanf("%s",stu[i].name)
sum=0
for(j=0j<3j++)
{
printf("score %d:",j+1)
scanf("%d",&stu[i].score[j])
sum+=stu[i].score[j]
}
stu[i].ave=sum/3.0
}
fp=fopen("stud","w")
for(i=0i<5i++)
if(fwrite(&stu[i],sizeof(struct student),1,fp)!=1)
printf("file write error\n")
fclose(fp)
fp=fopen("stud","r")
for(i=0i<5i++)
{
fread(&stu[i],sizeof(struct student),1,fp)
printf("\n%s,%s,%d,%d,%d,%6.2f\n",stu[i].num,stu[i].name,
stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].ave)
}
system("pause")
return 0
}
No.2 将运行No.1程序所得到的stud文件中的学生数据按平均分进行排序处理,将已排序的学生数据存入一个新文件stu_sort中。
程序:
#include<stdio.h>
#include<stdlib.h>
#define N 10
struct student
{
char num[10]
char name[8]
int score[3]
float ave
}st[N],temp
int main()
{
FILE *fp
int i,j,n
if((fp=fopen("stud","r"))==NULL)
{
printf("can not open.")
exit(0)
}
printf("File 'stud':")
for(i=0fread(&st[i],sizeof(struct student),1,fp)!=0i++)
{
printf("\n%8s%8s",st[i].num,st[i].name)
for(j=0j<3j++)
printf("%8d",st[i].score[j])
printf("%10.2f",st[i].ave)
}
printf("\n")
fclose(fp)
n=i
for(i=0i<ni++)
for(j=i+1j<nj++)
if(st[i].ave<st[j].ave)
{
temp=st[i]
st[i]=st[j]
st[j]=temp
}
printf("\nNow:")
fp=fopen("stu_sort","w")
for(i=0i<ni++)
{
fwrite(&st[i],sizeof(struct student),1,fp)
printf("\n%8s%8s",st[i].num,st[i].name)
for(j=0j<3j++)
printf("%8d",st[i].score[j])
printf("%10.2f",st[i].ave)
}
printf("\n")
fclose(fp)
system("pause")
return 0
}
No.3 将题No.2已排序的学生成绩文件进行插入处理。插入一个学生的3门课程成绩,程序先计算新插入学生的平均成绩,然后将它按平均成绩高低顺序插入,插入后建立一个新文件。
程序:
#include<stdio.h>
#include<stdlib.h>
struct student
{
char num[10]
char name[8]
int score[3]
float ave
}st[10],s
int main()
{
FILE *fp,*fp1
int i,j,t,n
printf("\nNo.:")
scanf("%s",s.num)
printf("name:")
scanf("%s",s.name)
printf("score1,score2,score3:")
scanf("%d,%d,%d",&s.score[0],&s.score[1],&s.score[2])
s.ave=(s.score[0]+s.score[1]+s.score[2])/3.0
if((fp=fopen("stu_sort","r"))==NULL)
{
printf("can not open file.")
exit(0)
}
printf("Original data:\n")
for(i=0fread(&st[i],sizeof(struct student),1,fp)!=0i++)
{
printf("\n%8s%8s",st[i].num,st[i].name)
for(j=0j<3j++)
printf("%8d",st[i].score[j])
printf("%10.2f",st[i].ave)
}
n=i
for(t=0st[t].ave>s.ave&&t<nt++)
printf("\nNow:\n")
fp1=fopen("sort1.dat","w")
for(i=0i<ti++)
{
fwrite(&st[i],sizeof(struct student),1,fp1)
printf("\n%8s%8s",st[i].num,st[i].name)
for(j=0j<3j++)
printf("%8d",st[i].score[j])
printf("%10.2f",st[t].ave)
}
fwrite(&s,sizeof(struct student),1,fp1)
printf("\n%8s%8s%8d%8d%8d%10.2f",s.num,s.name,s.score[0],
s.score[1],s.score[2],s.ave)
for(i=ti<ni++)
{
fwrite(&st[i],sizeof(struct student),1,fp1)
printf("\n%8s%8s",st[i].num,st[i].name)
for(j=0j<3j++)
printf("%8d",st[i].score[j])
printf("%10.2f",st[i].ave)
}
printf("\n")
fclose(fp)
fclose(fp1)
system("pause")
return 0
}
No.4 将No.3的结果仍存入原有的文件而不另建立新文件。
程序:
#include<stdio.h>
#include<stdlib.h>
struct student
{
char num[10]
char name[8]
int score[3]
float ave
}st[10],s
int main()
{
FILE *fp
int i,j,t,n
printf("\nNo.:")
scanf("%s",s.num)
printf("name:")
scanf("%s",s.name)
printf("score1,score2,score3:")
scanf("%d,%d,%d",&s.score[0],&s.score[1],&s.score[2])
s.ave=(s.score[0]+s.score[1]+s.score[2])/3.0
if((fp=fopen("stu_sort","r"))==NULL)
{
printf("can not open file.")
exit(0)
}
printf("Original data:\n")
for(i=0fread(&st[i],sizeof(struct student),1,fp)!=0i++)
{
printf("\n%8s%8s",st[i].num,st[i].name)
for(j=0j<3j++)
printf("%8d",st[i].score[j])
printf("%10.2f",st[i].ave)
}
n=i
for(t=0st[t].ave>s.ave&&t<nt++)
printf("\nNow:\n")
fp=fopen("stu_sort","w")
for(i=0i<ti++)
{
fwrite(&st[i],sizeof(struct student),1,fp)
printf("\n%8s%8s",st[i].num,st[i].name)
for(j=0j<3j++)
printf("%8d",st[i].score[j])
printf("%10.2f",st[t].ave)
}
fwrite(&s,sizeof(struct student),1,fp)
printf("\n%8s%8s%8d%8d%8d%10.2f",s.num,s.name,s.score[0],
s.score[1],s.score[2],s.ave)
for(i=ti<ni++)
{
fwrite(&st[i],sizeof(struct student),1,fp)
printf("\n%8s%8s",st[i].num,st[i].name)
for(j=0j<3j++)
printf("%8d",st[i].score[j])
printf("%10.2f",st[i].ave)
}
printf("\n")
fclose(fp)
system("pause")
return 0
}
对链表的综合操作:
程序:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define LEN sizeof(struct student)
struct student
{
long num
float score
struct student *next
}
int n
struct student *creat(void)
{
struct student *head
struct student *p1,*p2
n=0
p1=p2=(struct student *)malloc(LEN)
scanf("%ld,%f",&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("%ld,%f",&p1->num,&p1->score)
}
p2->next=NULL
return(head)
}
void print(struct student *head)
{
struct student *p
printf("\nNow,These %d records are:\n",n)
p=head
if(head!=NULL)
do
{
printf("%ld %5.1f\n",p->num,p->score)
p=p->next
}while(p!=NULL)
}
struct student *del(struct student *head,long num)
{
struct student *p1,*p2
if(head==NULL)
{
printf("\nlist null!\n")
return(head)
}
p1=head
while(num!=p1->num&&p1->next!=NULL)
{
p2=p1p1=p1->next
}
if(num==p1->num)
{
if(p1==head) head=p1->next
else p2->next=p1->next
printf("delete:%ld\n",num)
n=n-1
}
else printf("%ld not been found!\n",num)
return(head)
}
struct student *insert(struct student *head,struct student *stud)
{
struct student *p0,*p1,*p2
p1=head
p0=stud
if(head==NULL)
{
head=p0
p0->next=NULL
}
else
{
while((p0->num>p1->num)&&(p1->next!=NULL))
{
p2=p1
p1=p1->next
}
if(p0->num<=p1->num)
{
if(head==p1) head=p0
else
{
p2->next=p0
p0->next=p1
}
}
else
{
p1->next=p0
p0->next=NULL
}
}
n=n+1
return(head)
}
int main()
{
struct student *head,*stu
long del_num
printf("input records:\n")
head=creat()
print(head)
printf("\ninput the deleted number:")
scanf("%ld",&del_num)
while(del_num!=0)
{
head=del(head,del_num)
print(head)
printf("\ninput the deleted number:")
scanf("%ld",&del_num)
}
printf("\ninput the inserted record:")
stu=(struct student *)malloc(LEN)
scanf("%ld,%f",&stu->num,&stu->score)
while(stu->num!=0)
{
head=insert(head,stu)
print(head)
printf("\ninput the inserted record:")
stu=(struct student *)malloc(LEN)
scanf("%ld,%f",&stu->num,&stu->score)
}
system("pause")
return 0
}
#include <stdio.h>
int main()
{
char strw[1000], stre[1000], *s, *t
scanf("%s %s", strw, stre)
s = strw
t = stre
while (*s != '\0' &&*s == *t)
++s, ++t
if (*s <*t)
printf("-1\n")
else if (*s >*t)
printf("1\n")
else
printf("0\n")
return 0
}
如果出现诸如 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. 的编译错误或警告,可将代码 "scanf("%s %s", strw, stre)" 用 "scanf_s("%s %s", strw, 1000, stre, 1000)" 替代;
部分测试样例
【4.1】已知银行整存整取存款不同期限的月息利率分别为:0.315% 期限一年
0.330% 期限二年
月息利率 = 0.345% 期限三年
0.375% 期限五年
0.420% 期限八年
要求输入存钱的本金和期限,求到期时能从银行得到的利息与本金的合计。
【4.2】输入年份year和月month,求该月有多少天。判断是否为闰年,可用如下C语言表达式:year%4==0 &&year0!=0 || year@0==0。若表达式成立(即表达式值为1),则year为闰年;否则,表达式不成立(即值为0),year为平年。
【4.3】编写一个简单计算器程序,输入格式为:data1 op data2。其中data1和data2是参加运算的两个数,op为运算符,它的取值只能是+、-、*、/。
【4.4】输入n值,输出如图所示矩形。
【4.5】输入n值,输出如图所示平行四边形。
【4.6】输入n值,输出如图所示高为n的等腰三角形。
【4.7】输入n值,输出如图所示高为n的等腰三角形。
【4.8】输入n值,输出如图所示高和上底均为n的等腰梯形。
【4.9】输入n值,输出如图所示高和上底均为n的等腰空心梯形。
【4.10】输入n值,输出如图所示边长为n的空心正六边型。