C++ 做题 Ⅶ

x33g5p2x  于2022-04-10 转载在 其他  
字(3.6k)|赞(0)|评价(0)|浏览(405)

1、结构体统计学生信息
输入N个学生的姓名、语文数学成绩,按总分从高到低输出。
【分析】:

  1. 定义结构体,字符串类型-名字,数值类型-语文、数学、总分。
  2. 输入N,循环输入数据个数到N。
  3. 对输入数据进行冒泡排序。
  4. for循环输出控制从大到小输出。
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <string>
  4. using namespace std;
  5. struct student{ //定义结构体:名字,语文数学成绩,总分
  6. string name;
  7. float chinese,math;
  8. float total;
  9. };
  10. student a[101];
  11. int n;
  12. int main(){
  13. cout<<"请输入n个学生信息:";
  14. cin>>n;
  15. cout<<"姓名 语文 数学"<<endl;
  16. for(int i=0;i<n;++i){ //输入信息
  17. cin>>a[i].name;
  18. cin>>a[i].chinese>>a[i].math;
  19. a[i].total = a[i].chinese+a[i].math;
  20. }
  21. for(int i=n-1;i>0;--i)
  22. for(int j=0;j<i;++j){ //冒泡排序
  23. if(a[j].total<a[j-1].total)
  24. swap(a[j],a[j-1]);
  25. }
  26. cout<<"------------------------------------------"<<endl;
  27. cout<<"姓名 语文 数学 总分"<<endl;
  28. for(int i=0;i<n;++i) //输出信息
  29. cout<<a[i].name<<" "<<a[i].chinese<<" "<<a[i].math<<" "<<a[i].total<<endl;
  30. return 0;
  31. }

2、对三个整型变量进行排序,将最小值赋给第一个变量,大于最小值的赋给第二个变量,最大值赋给第三个变量。

  1. #include <iostream>
  2. #include <cstdio>
  3. using namespace std;
  4. void swap(int *x,int *y){ //交换函数
  5. int t=*x;
  6. *x=*y;
  7. *y=t;
  8. }
  9. void sort(int *x,int *y,int *z){ //排序函数
  10. if(*x>*y) swap(x,y);
  11. if(*x>*z) swap(x,z);
  12. if(*y>*z) swap(y,z);
  13. }
  14. int main(){
  15. int a,b,c;
  16. scanf("%d %d %d",&a,&b,&c);
  17. sort(&a,&b,&c);
  18. printf("%d %d %d",a,b,c);
  19. return 0;
  20. }

3、编写一个函数,在包含N个函数的数组中找到第一个质数,若有则返回该数的地址,否则返回NULL。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <string>
  4. #include <cmath>
  5. using namespace std;
  6. int n,a[101];
  7. bool prime(int n){ //判断素数函数
  8. if (n<2) return false;
  9. if(n==2) return true;
  10. for(int i=2;i<=sqrt(n);++i)
  11. if(n%i==0)
  12. return false;
  13. return true;
  14. }
  15. int *find(){ // 如果是素数,则返回地址
  16. for(int i=1;i<=n;++i)
  17. if(prime(a[i]))
  18. return &a[i];
  19. return NULL;
  20. }
  21. int main(){
  22. scanf("%d",&n);
  23. for(int i=1;i<=n;++i)
  24. scanf("%d",&a[i]); //输入值
  25. int *p=find();
  26. if(p!=NULL)
  27. printf("%d\n%d",p,*p);
  28. else
  29. printf("不能发现");
  30. return 0;
  31. }

4、函数指针
函数指针基本操作3:

  1. 声明函数指针,函数原型是 int test(int a); 那函数指针声明 int (*fp)(int a); 注意不能写成 int *fp(int a); 不然计算机会编译成 声明函数 fp(int a),返回类型 int * 。
  2. 获取函数的地址,简单使用函数名赋值即可,例如 fp=test;这表明函数名与数组名一样可以看作是指针。
  3. 使用函数指针调用函数,建议用(*fp)(int数值)
    函数指针调用函数实例
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <string>
  4. using namespace std;
  5. int t(int a){ //定义函数t
  6. return a*2;
  7. }
  8. int main(){
  9. printf("%d\n%d",t,*t);
  10. int (*p)(int b); //定义函数指针变量p,形参可不一致
  11. p=t; //把t地址赋给函数指针p
  12. printf("\n%d\n%d",t(1),t(2));
  13. printf("\n%d,%d",p(5),(*p)(10));
  14. return 0;
  15. }

这里 *p <==> 函数t,定义int (*p)(int b); 可以进行赋值p=t。

使用typedef 定义函数指针示例。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <string>
  4. #include <cmath>
  5. using namespace std;
  6. int t(int a){ //定义函数t
  7. return a*2;
  8. }
  9. typedef int (*LP)(int);
  10. int main(){
  11. printf("%d\n%d",t,*t);
  12. // int (*p)(int b); //定义函数指针变量p
  13. LP p=t; //定义LP类型函数指针p,把t赋给p
  14. printf("\n%d\n%d",t(1),t(2));
  15. printf("\n%d,%d",p(5),(*p)(10));
  16. return 0;
  17. }

5、使用函数指针数组,模拟菜单功能。

  1. #include <iostream>
  2. #include <cstdio>
  3. using namespace std;
  4. void t1(){ cout<<"1号"; }
  5. void t2(){ cout<<"2号"; }
  6. void t3(){ cout<<"3号"; }
  7. void t4(){ cout<<"4号"; }
  8. typedef void (*LP)();
  9. int main(){
  10. LP a[]={t1,t2,t3,t4};
  11. int x;
  12. cin>>x;
  13. a[x]();
  14. return 0;
  15. }

6、结构体指针
结构体指针变量定义:
结构体名 *指针变量名(student *p;)

  1. struct student{
  2. char name[20];
  3. char sex;
  4. float score;
  5. }; student *p;

引用结构体指针变量指向结构体变量的成员如下:

  1. 指针名->成员名 p->sex
  2. (*指针名).成员名 (*p).sex

结构体指针运用实例

  1. #include <iostream>
  2. #include <cstdio>
  3. using namespace std;
  4. struct student{
  5. char name[20];
  6. char sex;
  7. int score;
  8. }s[3]={{"小明",'f',98},
  9. {"吴昊",'m',88},
  10. {"天猫",'f',58},
  11. };
  12. int main(){
  13. student *p;
  14. for(p=s;p<s+3;++p)
  15. printf("%s%7c%7d\n",p->name,(*p).sex,(*p).score);
  16. return 0;
  17. }

相关文章