实验八:指针(一)
注:实验八~实验十的所有程序都使用指针方法操作
1、编写求字符串串长的函数(strlen),并在主函数中调用。
#include"stdio.h"
void main()
{char *str[80];
printf("please input a string :\n");
scanf("%s",str);
printf("the length of the string is %d",LEN(str)); getch();
}
LEN(char *p)
{int n=0;
while(*p!=0)
{n++;p++;}
return(n);
}
2、编写连接两字符串的函数(strcat),并在主函数中调用。
#include "stdio.h"
char* strcat(char *p1,char *p2)
{
char *temp = p1;
while(*p1) ++p1;
while(*p1 = *p2) ++p1, ++p2;
return(temp);
}
void main()
{
int *str1,*str2;
printf("please input str1:\n");
scanf("%s",str1);
printf("\nplease input str2:\n");
scanf("%s",str2);
printf("\nthe strcat answer is : %s",strcat(str1,str2));
getch();
}
3、sort()对一维整数数组的内容进行排序;编写函数in_data()读入一组整数;编写函数out_data输出整数数组的内容;在主函数中调用这些函数,输入一组整数,输出排序前和排序后的情况。
#include"string.h"
void in_data(int a[10])
{
int i;
printf("please inputhe numbers:\n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
}
void sort(int a[10])
{
int i,j,t;
for(j=0;j<9;j++)
for(i=0;i<9-j;i++)
if(a[i]>a[i+1])
{
t=a[i];
a[i]=a[i+1];
a[i+1]=t;
}
}
int out_data(int a[10])
{
int i;
for(i=0;i<10;i++)
printf("%d",a[i]);
}
main()
{ int b[10];
in_data(b[10]);
sort(b[10]);
out_data(b[10]);
getch();
}
实验九:指针(二)
1、编写函数 reverse(*s),实现对字符串S的倒排,编写主函数对其进行调用。
#include"stdio.h"
#include"string.h"
void main()
{void reverse(char s[]);
char s[50];
printf("please input the string:\n");
scanf("%s",s);
reverse(s);
printf("\nthe string has been reversed : %s",s);
getch();
}
void reverse(char *p)
{
int i,j,k;
i=strlen(p)-1;
for(j=0;j<=i/2;j++)
{
k=p[j];
p[j]=p[i-j];
p[i-j]=k;
}
}
2、编写函数strcmp(*s1,*s2),实现对两字符串的比较,编写主函数对其进
行调用。
#include"stdio.h"
int strcmp(char *s1,char *s2)
{
int i;
for(i=0;;)
{
if((s1+i)>(s2+i)) {return(1); break;};
if((s1+i)<(s2+i)) {return(-1); break;};
if((s1+i)==(s2+i)) i++;
}
}
void main()
{
char str1,str2;
printf("please input str1:\n");
scanf("%s",str1);
printf("please input str2:\n");
scanf("%s",str2);
printf("answer is:%d",strcmp(str1,str2));
getch();
}
3、有一字符串,包含n个字符,写一函数,将此字符串中从第m个字符开始的全部字符复制成为另一个字符串。
#include "stdio.h"
void main()
{ char a[20],b[20],*p,*q;
int m;
printf("input a string a:\n\t");
gets(a);
printf("\ninput a number m:\n\t");
scanf("%d",&m);
p=a+m;q=b;
strcpy(q,p);
printf("\nthe new string is:%s",q);
getch();
}
实验十:指针(三)
1、有一个班4个学生5门课程。(1)求第一门课程的平均分;(2)找出有2门以上课程不及格的学生,输出他们的学号和全部课程成绩及平均成绩;(3)找出平均成绩在90分以上的学生。分别编3个函数实现以上要求。
{老师,这题我能运行,但是结果不对!}
#define N 4
#define M 5
main()
{
void ave_score();
void score_0_59();
void score_90_100();
int i,j,a[N][M+2];
for(i=0;i { printf("NO : a[%d]=",i+1); scanf("%d",a[i][0]); printf("score :\n"); for(j=1;j<=M;j++) { printf("a[%d][%d]=",i+1,j); scanf("%d",&a[i][j]); } } ave_score(a,1); score_0_59(a,2); score_90_100(a); getch(); } void ave_score(int a[N][M+2],int n) /*第n门课的平均成绩*/ { int i,p=0; for(i=0;i for(i=0;i printf("ave%7.1f\n,(float)P/N"); } void score_0_59(int a[N][N+2],int n) /*n门课不及格的学生*/ { int i,j,k; for(i=0;i { for(k=a[i][M+1],j=1;j<=M;j++) {k+=a[i][j]<60; a[i][M+1]+=a[i][j];} if(k printf("%5d:",a[i][0]); for(j=1;j<=M;j++) printf("%5d",a[i][j]); printf("ave:%5.1f",(float)a[i][M+1]/M); } } void score_90_100(int a[N][M+2]) /*平均成绩在90分以上的学生*/ { int i,j; for(i=0;i { for(j=1;j<=M;j++) if(a[i][j]<85) break; if(j>M||a[i][M+1]>90*M) { printf("%5d:",a[i][0]); for(j=0;j<=M;j++) printf("%5d", a[i][j]); printf("ave :%5.1f\n",(float)a[i][M+1]/M); } } } 2、编写一个程序,输入月份,输出该月的英文月名。例如,输入“3”,则输出“March”,要求用指针数组实现。 #include "stdio.h" void main() { char *p[12]={"January","February","March","April","May","June","July","Aug ust","September","October","November","December"}; int i; printf("please input a number:\n"); scanf("%d",&i); if(i>12||i<=0) printf("error\n"); else printf("\nNO.%d month is: %s\n",i,*(p+i-1)); getch(); } 实验十一:结构体与公用体(一) 1、用结构体表示日期,编写程序计算伦敦奥运会的倒计时的天数并输出(2012年8月15日)。 #include "stdio.h" #define N 31+29+31+30+31+30+31+31+15 struct {int year; int month; int day; }date; main() { int days; printf("input the date: year month day\n\t\t"); scanf("%d %d %d",&date.year,&date.month,&date.day); switch(date.month) { case 1: days=(2012-date.year)*365+N-date.day; break; case 2: days=(2012-date.year)*365+N-31-date.day; break; case 3: days=(2012-date.year)*365+N-59-date.day; break; case 4: days=(2012-date.year)*365+N-90-date.day; break; case 5: days=(2012-date.year)*365+N-120-date.day; break; case 6: days=(2012-date.year)*365+N-151-date.day; break; case 7: days=(2012-date.year)*365+N-181-date.day; break; case 8: days=(2012-date.year)*365+N-212-date.day; break; case 9: days=(2012-date.year)*365+N-273-date.day; break; case 10: days=(2012-date.year)*365+N-304-date.day; break; case 11: days=(2012-date.year)*365+N-334-date.day; break; case 12: days=(2012-date.year)*365+N-365-date.day; break; } printf("\nthe date %d-%d-%dis %ddays before the London Olympic\n",date.year,date.month,date.day,days); getch(); } 2、某系的成绩登记册中,每个班最多有40个学生,每份成绩表中的成绩信息包括:学号(9位字符),姓名(8位字符),成绩(百分制),备注(20位字符)。设计程序以处理一个班的成绩信息,包括输入、输出、查询(给定分数以上的学生信息)等。 #include "stdio.h" #define N 10 struct student {char num[9]; char name[8]; int score; char add[20]; }stu[N]; main() { int i,s; printf("input the student's data:\n num name score add\n"); for(i=0;i scanf("%s %s %d %s",&stu[i].num,&stu[i].name,&stu[i].score,&stu[i].ad d); printf("NO number name score add\n "); for(i=0;i printf("%d %s %s %d %s\n",i,stu[i].num,stu[i].name,stu[i ].score,stu[i].add); printf("\nplease input a score\n\t"); scanf("%d",&s); for(i=0;i if(stu[i].score>s) printf("\nNO.%d:number: %s, name: %s, score: %d, add:%s",i+1,stu[i].num,stu[i].name,stu[i].score,stu[i].add); getch(); } 实验十二:结构体与公用体(二) 1、为一个花店编写一个库存管理程序,花店库存的花用花的名称、颜色、单价和株的数量来表示。要求: (1)定义一个结构数组,存储库存的鲜花的数据; (2)编写函数input_data(),输入现有库存的鲜花数据; (3)编写一个函数buy(),向用户询问她需要购买的鲜花的种类和数量,计算总价,并从库存中减去用户购买的数量,返回总价格。 编写主函数调用上述函数。 #include"stdio.h" #define N 40 struct flower { char name; char corlor; int price; int number; } void input_data(flo[int i]) { scanf("%s,%s,%d,%d",&flo[i].name,&flo[i].corlor,&flo[i].price,&flo[i] .number); } int buy() { int *p; printf("print your flower's name,number\n"); scanf("%s,%d",&str,&i); for(p=flo;p while(https://www.doczj.com/doc/b9769937.html,==str) S=p.price*i; p->number=p.number-i; printf("you should pay %d! Thank you!",S); } main() { struct flower flo[N]; input_data(struct flo[]); buy(); } 实验十三:位运算 1、编写一函数,对一个16位的二进制数取出它的奇数位(左起)。 #include "stdio.h" getbit1(unsigned value,int n) { unsigned z; z=~0; z=z>>n; z=~z; z=z|(value>>n); return(z); } getbit2(unsigned valud,int n) { unsigned z; z=(~(1>>n)&(valud>>n)); } main() { int a,n,m; a=~0; if(a>>5)!a; {printf("\nTurbo C,logical move!\n"); m=0; } {printf("\nTurbo C,logical move!\n"); m=1; } printf("input an otcal number:\n"); scanf("%o",&a); printf("how many digit move thwards the right:"); scanf("%d",&n); if(m==0) printf("Arithmetic right move,result:%o\n",getbit1(a,n)); else printf("Logical right move ,result:%o",getbit2(a,n)); getch(); } 2、编写一函数用来实现左右循环移位。函数名为move,调用方法为:move(value,n),其中value为要循环移位的数,n为位移的位数(n>0右移,n<0左移)。(参考例12.2) #include "stdio.h" main() { unsigned moveright(unsigned,int); unsigned moveleft(unsigned,int); unsigned a; int n; printf("\n input an otcal number:"); scanf("%o",&a); printf("input n:"); scanf("%o",&n); if(n>0) {moveright(a,n); printf("result:%o\n",moveright(a,n)); } {n=-n; moveleft(a,n); printf("result:%o\n",moveleft(a,n)); } getch(); } unsigned moveright(unsigned value,int n) { unsigned z; z=(value>>n)|(value<<(16-n)); return(z); } unsigned moveleft(unsigned value,int n) { unsigned z; z=(value< return(z); } 实验十四:文件 1、编程统计一个文本文件 f.txt中所有字母和所有数字的总的出现次数。#include"stdio.h" #include"stdlib.h" main() { FILE *fp; char ch; int i=0,j=0; if((fp=fopen("D:\\f.txt","r"))==NULL) { printf("cannot open the file\n"); exit(0); } ch=fgetc(fp); while(ch!=EOF) { if(ch>'a'&&ch<'z'||ch>'A'&&ch<'Z') i++; if(ch<='9'&&ch>='0') j++; ch=fgetc(fp); } fclose(fp); printf("There are %d zimu %d shuzi in the f.txt",i,j); getchar(); } 2、编程将文本文件 f.txt中的所有小写字母变成大写字母并存入另一文本文件o.txt中。 #include"stdio.h" #include"stdlib.h" main() { FILE *fp1,*fp2; char ch; if((fp1=fopen("D:\\f.txt","r"))==NULL) { printf("cannot open the file\n"); exit(0); } if((fp2=fopen("D:\o.txt","w"))==NULL) { printf("cannot open the file\n"); exit(0); } ch=fgetc(fp1); while(ch!=EOF) { if(ch>='a'&&ch<='z') ch=ch-32; fputc(ch,fp2); ch=fgetc(fp1); } fclose(fp1);fclose(fp2); getch(); } 3、由键盘初始化5个学生3门课程的成绩,计算出平均成绩,将原有成绩及平均成绩分行存放在文件score.txt中。 #include"stdio.h" #include"stdlib.h" main() { FILE *fp2; int a[5][4]; int i,j,*fp1; char ch; printf("please input 15 numbers:\n"); for(i=0;i<5;i++) for(j=0;j<3;j++) scanf("%d",&a[i][j]); for(i=0;i<5;i++) {for(j=0,a[i][3]=0;j<3;j++) a[i][3]+=a[i][j]; a[i][3]/=3; } fp1=a; if((fp2=fopen("score.txt","w"))==NULL) { printf("cannot open the file\n"); exit(0); } ch=fgetc(fp1); fputc(ch,fp2); } fclose(fp2); }