杭州專業(yè)網(wǎng)站制作凡科建站下載
目錄
1、計(jì)算學(xué)生平均身高
?2、使用指針實(shí)現(xiàn)數(shù)據(jù)交換
3、使用指針實(shí)現(xiàn)整數(shù)排序
5、使用指針輸出數(shù)據(jù)元素
6、使用指針查找數(shù)組中的最大值和最小值。
7、使用指針連接兩個(gè)字符串
8、用指針數(shù)組構(gòu)造字符串?dāng)?shù)組
9、使用指針的指針輸出字符串
10、找出最高分
1、計(jì)算學(xué)生平均身高
輸入學(xué)生數(shù)并逐個(gè)輸入學(xué)生的身高,輸出身高的平均值。
#include<stdio.h>
float average(float arry[],int n);
int main()
{float average(float arry[],int n);float height[100],aver;int i,n;printf("請(qǐng)輸入學(xué)生的數(shù)量:\n");scanf("%d",&n);printf("請(qǐng)輸入學(xué)生們的身高:\n");for(i=0;i<n;i++)scanf("%f",&height[i]);printf("\n");aver=average(height,n);printf("學(xué)生的平均身高為:%6.2f\n",aver);return 0;} float average(float array[],int n){int i;float aver,sum=0;for(i=0;i<n;i++)sum+=array[i];aver=sum/n;return(aver);}
運(yùn)行結(jié)果
?2、使用指針實(shí)現(xiàn)數(shù)據(jù)交換
輸入兩個(gè)整型數(shù)值,將變量a,b中的值交換,然后輸出道窗體上。
#include<stdio.h>
int swap(int *p1, int *p2);
int main()
{int a,b;int *pointer1, *pointer2;scanf("%d%d",&a,&b);pointer1 = &a;pointer2 = &b;swap(pointer1, pointer2);printf("\nThe result is :%d,%d\n",a,b);return 0;
}
swap(int *p1,int *p2)
{int temp;temp = *p1;*p1 = *p2;*p2 = temp;
}
運(yùn)行結(jié)果
3、使用指針實(shí)現(xiàn)整數(shù)排序
輸入三個(gè)整數(shù),將這三個(gè)整數(shù)按照由大到小的順序輸出,顯示在屏幕上。?
#include<stdio.h>
int swap(int *p1,int *p2);
int exchange(int *pt1,int *pt2,int *pt3);
int main()
{int a,b,c,*q1,*q2,*q3;puts("Please input three key numbers you want to rank:");scanf("%d,%d,%d",&a,&b,&c);q1 = &a;q2 = &b;q3 = &c;exchange(q1,q2,q3);printf("\n%d,%d,%d\n",a,b,c);puts("\n Press any key to quit...");return 0;
}
swap(int *p1,int *p2)
{int temp;temp = *p1;*p1 = *p2;*p2 = temp;
}
exchange(int *pt1,int *pt2,int *pt3)
{if(*pt1<*pt2)swap(pt1,pt2);if(*pt1<*pt3)swap(pt1,pt3);if(*pt2<*pt3)swap(pt2,pt3);}
運(yùn)行結(jié)果
?4、指向結(jié)構(gòu)體變量的指針
通過(guò)指向結(jié)構(gòu)體指針變量實(shí)現(xiàn)在窗體上顯示學(xué)生的信息。
#include<stdio.h>
struct student
{int num;char name[20];char sex;int age;float score;
};
int main()
{struct student student1={1001,"李xx",'M',25,90.3};struct student *p;p = &student1;printf("Numbers:%d\n",p->num);printf("Name:%s\n",p->name);printf("Sex:%c\n",p->sex);printf("Age:%d\n",p->age);printf("Score:%5.1f\n",p->score);
}
運(yùn)行結(jié)果
5、使用指針輸出數(shù)據(jù)元素
通過(guò)指針變量輸出數(shù)組的各元素值,程序運(yùn)行后,輸入10個(gè)數(shù)字后,可以看到輸出的數(shù)組元素值。?
#include<stdio.h>
int main()
{int *p,a[10],i;p=&a[0];printf("請(qǐng)輸入10個(gè)數(shù)字:\n");for(i=0;i<10;i++){scanf("%d",&a[i]);}printf("數(shù)組中的元素為:\n");for(i=0;i<10;i++){printf("%d",*(p+i));}printf("\n");
}
運(yùn)行結(jié)果
6、使用指針查找數(shù)組中的最大值和最小值。
輸入10整型數(shù),自動(dòng)查找數(shù)組中的最大值和最小值,并顯示在窗體上。
?
#include<stdio.h>
int max_min(int a[],int n,int *max,int *min);
int main()
{int i,a[10];int max,min;printf("Input 10 int eger numbers you want to operate:\n");for(i=0;i<10;i++){scanf("%d",&a[i]);}max_min(a,10,&max,&min);printf("\nThe maximun number is:%d\n",max);printf("The minimun number is :%d\n",min);
}
int max_min(int a[],int n,int *max,int *min)
{int *p;*max = *min = *a;for(p=a+1;p<a+n;p++)if(*p>*max){*max =*p;}else if(*p<*min){*min = *p;}return 0;}
運(yùn)行結(jié)果
7、使用指針連接兩個(gè)字符串
要求實(shí)現(xiàn)將兩個(gè)已知的字符串連接,放到另外一個(gè)字符串?dāng)?shù)組中,并將連接后的字符串輸出到屏幕上。?
#include<stdio.h>
#define N 20
char *MyStrcat(char *dstStr,char *srcStr)
{char *pStr = dstStr;while(*dstStr !='\0'){dstStr++;}for(;*srcStr!='\0';dstStr++,srcStr++){*dstStr = *srcStr;}*dstStr = '\0';return pStr;
}
int main()
{char first[2*N];char second[N];char *result = NULL;printf("輸入第一組:");gets(first);printf("輸入第二組:");gets(second);result = MyStrcat(first,second);printf("結(jié)果:%s\n",result);return 0;
}
運(yùn)行結(jié)果
8、用指針數(shù)組構(gòu)造字符串?dāng)?shù)組
要求實(shí)現(xiàn)輸入一個(gè)星期中對(duì)應(yīng)的第幾天,可顯示其英文寫法。如,輸入‘4’,則顯示星期四對(duì)應(yīng)的英文名。?
#include<stdio.h>
int main()
{char *Week[]={"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};int i;printf("Please enter a number for week\n");scanf("%d",&i);printf("The week is :");printf("%s\n",Week[i-1]);return 0;
}
運(yùn)行結(jié)果
9、使用指針的指針輸出字符串
首先使用指針數(shù)組創(chuàng)建一個(gè)字符串?dāng)?shù)組,然后定義指向指針的指針,使其指向字符串?dāng)?shù)組,并使用其將數(shù)組中字符串輸出。?
#include<stdio.h>
int main()
{char *strings[]={"趙xx","錢xx","孫xx","李xx","周xx"};char **p,i;p=strings;printf("%s\n",strings[0]);for(i=0;i<5;i++){printf("%s\n",*(p+i));}return 0;
}
運(yùn)行結(jié)果
10、找出最高分
通過(guò)結(jié)構(gòu)體變量記錄學(xué)生成績(jī),比較得到記錄中的最高成績(jī),輸出該學(xué)生的信息。?
#include<stdio.h>
struct student
{int num;char name[20];float score;
};
int main()
{int i,m;float maxscore;struct student stu[5]={{101,"李明",89},{102,"苑達(dá)",95},{103,"孫佳",89},{104,"王子川",85},{105,"劉春月",75}, };m=0;maxscore = stu[0].score;for(i=1;i<5;i++){if(stu[i].score>maxscore){maxscore = stu[i].score;m=i;}} printf("最高分是:%5.1f\n",maxscore);printf("最高分學(xué)生的學(xué)號(hào)是:%d\n",stu[m].num);printf("最高分學(xué)生的姓名是:%s\n",stu[m].name);
}
運(yùn)行結(jié)果
?