第二次博客
删除字符串中数字字符
实验代码
void delnum(char *s){
int i,j=0;
for(i=0;*(s+i)!='\0';i++){
if(*(s+i)>='0'&&*(s+i)<='9'){
}else{
*(s+j)=*(s+i);
j++;
}
}
*(s+j)='\0';
}
统计子串在母串出现的次数
实验代码
int fun(char *str,char *substr)
{
int i,j,k,num;
for(i=0;str[i]!='\0';i++)
{
for(j=i,k=0;substr[k]==str[j];k++,j++)
{
if(substr[k+1]=='\0')
{
num++;
break;
}
}
}
return num;
}
计算职工工资
实验代码
#include<stdio.h>
struct worker{
char name[15];
float bgong,fgong,x,w;
}s[10000];
int main()
{
int N,i;
scanf("%d",&N);
for(i=0;i<N;i++)
{
scanf("%s%f%f%f",&s[i].name,&s[i].bgong,&s[i].fgong,&s[i].x);
s[i].w=s[i].bgong+s[i].fgong-s[i].x;
printf("%s %.2f\n",s[i].name,s[i].w);
}
}
计算平均成绩
实验代码
#include<stdio.h>
struct student{
char num[10],name[15];
int x;
}s[15];
int main()
{
int i,n,sum=0;
float average;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%s%s%d",&s[i].num,&s[i].name,&s[i].x);
sum=sum+s[i].x;
}
average=sum*1.0/n;
printf("%.2f\n",average);
for(i=0;i<n;i++)
{
if(s[i].x<average)
printf("%s %s\n",s[i].name,s[i].num);
}
}
按等级统计学生成绩
实验代码
int set_grade( struct student *p, int n ){
int count = 0, i;
for(i = 0;i<n;i++,p++){
if(p->score<60){
p->grade = 'D';
count++;
}
else if((p->score<70)&&(p->score>=60)){
p->grade = 'C';
}
else if((p->score<85)&&(p->score>=70)){
p->grade = 'B';
}
else{
p->grade = 'A';
}
}
return count;
}
结构体数组按总分排序
实验代码
void calc(struct student *p,int n)
{
int i;
i=0;
for(i=0;i<n;i++)
{
p[i].sum = p[i].score[0]+ p[i].score[1]+ p[i].score[2];
}
}
void sort(struct student *p,int n)
{
int i,j;
i=0;
j=0;
struct student a ;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(p[j].sum<p[j+1].sum)
{
a = p[j];
p[j] =p[j+1];
p[j+1] = a;
}
}
}
}