/*将s所指字符串中的字母转换成按字母序列的后续字母(但Z转化A,z转化a)其他的字符不变*/
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
void fun(char *s)
{
while (*s)
{
if(*s>='A'&&*s<='Z'||*s>='a'&&*s<='z') /*判断字符是否为字母*/
{
if (*s=='Z')
*s='A';
else if (*s=='z')
*s='a';
else
*s=*s+1; /*该字母的ASCII码值加1*/
}
s++; /*字符指针后移*/
}
}
main()
{
char s[80];
printf("\nEnter a string with length <80:\n\n");
gets(s);
printf("\nThe string :\n\n");
puts(s);
fun(s);
printf("\n\n The Cords:\n\n");
puts(s);
}
测试题7 (!)
#include <conio.h>
#include <stdio.h>
#include <string.h>
void fun(char *ss)
{
int i;
for(i=0;ss[i]!='\0';i++)
if (i%2==1 && ss[i]>='a' && ss[i]<='z')
ss[i]=ss[i]-32;
}
main()
{
FILE *wf;
char tt[81],s[10]="abc4Efg";
printf("\nPlease enter an string within 80 characters:\n");
gets(tt);
printf("\n\n After changing the string \n%s",tt);
fun(tt);
printf("\n becones \n%s\n",tt);
wf=fopen("out.dat","w");
fun(s);
fprintf(wf,"s",s);
fclose(wf);
getch();
}
/*将单链表的元素从小到大排序,即若原链表元素为:10, 4,2,8,6,则排序后为2,4,6,8,10*/
#include <stdio.h>
#include <stdlib.h>
#define NULL 0
typedef struct node
{
int data;
struct node *next;
}NODE;
void fun(NODE *h)
{
NODE *p,*q;
int t ;
p=h;
while (p)
{
q=p->next;
while (q)
{
if (p->data>q->data)
{
t=p->data;
p->data=q->data;
q->data=t;
}
q=q->next;
}
p=p->next;
}
}
void main()
{
NODE *h=NULL,*p,*q;
int i,a[6]={0,10,4,2,8,6};
for(i=1;i<=5;i++)
{
q=(NODE*)malloc(sizeof(NODE));
q->data=a[i];
q->next=NULL;
if (h==NULL)
h=p=q;
else
{
p->next=q;
p=q;
}
}
printf("1----");
p=h;
while(p)
{
printf("%d ",p->data);
p=p->next;
}
fun(h);
printf("\n2----");
p=h;
while(p)
{
printf("%d ",p->data);
p=p->next;
}
}
/*N名学生的数据已在主函数中放入结构体数组s中,下列给定的程序中,函数fun()的功能是:
把分数最高的学生数据放在h所指的数组中。注意:分数最高的学生可能不止一个,函数返回分数最高的学生的人数*/
#include <stdio.h>
#define N 16
typedef struct
{
char num[10];
int s;
}STREC;
int fun (STREC *a,STREC *b)
{
int i,j=0,max=a[0].s;
for(i=0;i<N;i++)
if(max<a[i].s )
max =a[i].s;
for (i=0;i<N;i++)
if (max==a[i].s )
b[j++]=a[i];
return j;
}
main ()
{
STREC s[N]={{"GA005",85},{"GA003",76},{"GA004",85},{"GA002",69},
{"GA001",91},{"GA007",72},{"GA008",64},{"GA006",86},{"GA015",85},{"GA013",91},
{"GA012",64},{"GA014",91},{"GA011",66},{"GA017",64},{"GA018",64},{"GA016",72}};
STREC h[N];
int i,n;
FILE *out;
n=fun(s,h);
printf("The %d highest score :\n",n);
for (i=0;i<n;i++)
printf("%s %4d \n",h[i].num,h[i].s);
printf("\n");
out=fopen("out45.dat","w");
fprintf(out,"%d\n",n);
for(i=0;i<n;i++)
fprintf(out,"%4d\n",h[i].s);
fclose(out);
}
#include <stdio.h>
#define N 16
typedef struct
{
char num[10];
int s ;
}STREC;
int fun (STREC a[])
{
int i=0,j=0;
STREC strTEMP;
for(;i<N;i++)
for(j=i;j<N;j++)
{
if(a[i].s <a[j].s)
{
strTEMP =a[i];
a[i]=a[j];
a[j]=strTEMP;
}
}
return 0;
}
main()
{
STREC s[N]={{"GA005",85},{"GA003",76},{"GA004",85},{"GA002",69},
{"GA001",93},{"GA007",72},{"GA008",64},{"GA006",86},{"GA015",85},{"GA013",91},
{"GA012",64},{"GA014",91},{"GA011",66},{"GA017",64},{"GA018",64},{"GA016",72}};
int i;
FILE *out;
fun(s);
printf("The data after scored :\n");
for (i=0;i<N;i++)
{
if ((i)%4==0)
printf("\n");
printf("%s %4d ",s[i].num,s[i].s);
for (i=0;i<N;i++)
{
if ((i)%4==0 &&i )
fprintf(out,"\n");
fprintf(out,"%4d ",s[i].s);
}
fprintf(out,"\n");
fclose(out);
}
}