if( ) if(str[i]>=’a’&&str[i]
else if(str[i]==’z’||str[i]==’Z’) str[i]-=25; } } #end if
printf(?output:\\n%s?,str); }
14
实验九 指针(2学时) 实验性质:验证性实验 一、实验目的
1.掌握指针的概念,会定义和使用指针变量; 2.学会使用数组的指针和指向数组的指针变量; 3.学会使用字符串的指针和指向字符串的指针变量; 4.学会使用指向函数的指针变量; 二、实验内容
1、运行程序,并分析程序完成的功能。
main( )
{ char *str1[20],*str2[20],*str3[20]; char swap( );
printf(?input three lines:\\n?); gets(str1); gets(str2); gets(str3);
if(strcmp(str1,str2)>0) swap(str1,str2); if(strcmp(str1,str3)>0) swap(str1,str3); if(strcmp(str2,str3)>0) swap(str2,str3); printf(?now,the order is:\\n?);
pritnf(?%s\\n%s\\n%s\\n?,str1,str2,str3) ;} char swap(char *p1,char *p2) { char *p[20]; strcpy(p,p1); strcpy(p1,p2); strcpy(p2,p); }
2、运行程序,并分析程序完成的功能。
#include
15
main( )
{ void sort(char s[ ] [ ]); int i;
char str[10][6];
printf(?input 10 strings:\\n?); for(i=0;i<10;i++) scanf(?%s?,str[i ]); sort(str);
printf(?now ,the sequence is:\\n?); for(i=0;i<10;i++) printf(?%s\\n?,str[i]);} void sort(char s[10][6]) { int i,j;
char *p ,temp[10]; p=temp; for(i=0;i<9;i++) for(j=0;j<9-i;j++) if(strcmp(s[j],s[j+1])>0) {strcpy(p,s[j]); strcpy(s[j],s[j+1]); strcpy(s[j+1],p); } }
16
实验十 结构体和共用体(2学时) 实验性质:验证性实验 一、实验目的
1.掌握结构体类型变量的定义和使用; 2.掌握结构体类型数组的概念和应用; 3.掌握共用体的概念与使用。 二、实验内容
编程序,然后上机调试运行。
1、运行程序,并分析程序所完成的功能。 #define N 10 struct student { char num[6];
char name[8]; int score[4]; float avr; } stu[10]; main( )
{ int i ,j ,max ,maxi ,sum; float average; for(i=0;i { printf(?\\n input scores of student:%d\\n?, printf(?No:?); scanf(?%s?,str[i].num); printf(?name:?); scanf(?%s?,stu[i].name); for(j=0;j<3;j++) { printf(?score%d:?,j+1); scanf(?%d?,&str[i].score[j]); } 17 i+1); } average=0; max=0; maxi=0; for(i=0;i printf(?No. name score1 score2 score3 average\\n?); for(i=0;i { printf(?%5ss?,stu[i].num ,stu[i].name); for(j=0;j<3;j++) printf(???,stu[i].score[j]); printf(?%8.2f\\n?,stu[i].avr); } printf(?average=%6.2f\\n?,average); printf(?the highest score is:%s,score total:%d?,stu[maxi].name,} 2、输入和运行以下程序: union data {int i[2]; float a; long b; 18 max);