鱼C论坛

 找回密码
 立即注册
查看: 2549|回复: 10

[已解决]用c语言编写,完成下列题目

[复制链接]
发表于 2022-5-22 12:31:36 | 显示全部楼层 |阅读模式
60鱼币
1、将一个3×4二维数组中每一行的值按逆序重新存放,例如原来的顺序为:{1,2,3,4,5,6,7,8,9,10,11,12},要求改为:{4,3,2,1,8,7,6,5,12,11,10,9}。
2、输入一行字符,编程统计出其中大写字母,小写字母,数字,空格和其他字符各是多少个?
3、请编写一个求整数绝对值的函数,要求在主函数中输入数据,然后调用你自己写的绝对值函数求该数的绝对值并输出。
4、请编写一个求两个实数乘法的函数,要求在主函数中输入数据,然后调用你自己写的函数求所输入数据的相乘结果并输出。
5、写一个函数,使输入的一个字符串按反序存放,如输入CHINA,输出ANICH。在主函数中输入和输出字符串。
6、写一个函数,使得给定的一个3*3的二维整型数组转置,即行列互换。
7、写一个函数,输入一个4位数字,要求输出这4个数字字符,但每两个数字间空一个空格。如输入2016,应输出“2 0 1 6”。
最佳答案
2022-5-22 12:31:37
本帖最后由 jhq999 于 2022-5-22 15:45 编辑

  1. void change(int *mt,int m,int n)/////////(1)
  2. {
  3.         int i=0,j=0,tmp=0;
  4.         for(i=0;i<m;i++)
  5.         {
  6.                 for(j=0;j<n/2;j++)
  7.                 {
  8.                         tmp=mt[i*n+j];
  9.                         mt[i*n+j]=mt[i*n+n-j-1];
  10.                         mt[i*n+n-j-1]=tmp;
  11.                 }
  12.         }
  13. }
复制代码


  1. int i=0,bigcount=0,lowcount=0,spacecount=0,numcount=0,othercount=0;
  2. while(str[i])
  3. {
  4.         if(str[i]>='a'&&str[i]<='z')bigcount++;
  5.         else if(str[i]>='A'&&str[i]<='Z')lowcount++;
  6.         else if(str[i]>='0'&&str[i]<='9')numcount++;
  7.         else if(0x20==str[i])spacecount++;
  8.        
  9.         i++;
  10. }
  11. othercount=i- bigcount-lowcount-spacecount-numcount;
复制代码

  1. ////template <class T>
  2. double abs(double n)
  3. {
  4.         return n<0?-1*n:n;
  5. }
复制代码
  1. double mul(double a,double b)
  2. {
  3.         return a*b;
  4. }
复制代码


  1. void invertstr(char* str)
  2. {
  3.         int len=0;
  4.         while(str[len])len++;
  5.         for(int i=0;i<len/2;i++)
  6.         {
  7.                 str[len]=str[i];
  8.                 str[i]=str[len-i-1];
  9.                 str[len-i-1]=str[len];
  10.         }
  11.         str[len]='\0';
  12. }

复制代码

  1. void invertmt(int (*mt)[3])
  2. {
  3.            for (int i = 0; i < 3;i++)
  4.            {
  5.                    for (int j = i+1; j < 3; j++)
  6.                    {
  7.                            int t=mt[i][j];
  8.                            mt[i][j]=mt[j][i];
  9.                            mt[j][i]=t;
  10.                    }
  11.            }
  12. }
复制代码

  1. void outputstr(char* str)
  2. {
  3.         int i=0;
  4.         do
  5.         {
  6.                 printf("%c",str[i]);
  7.                 printf("%c",0x20);
  8.                 i++;
  9.         }while(str[i+1]);
  10.         printf("%c",str[i]);
  11. }
复制代码

最佳答案

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-5-22 12:31:37 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jhq999 于 2022-5-22 15:45 编辑

  1. void change(int *mt,int m,int n)/////////(1)
  2. {
  3.         int i=0,j=0,tmp=0;
  4.         for(i=0;i<m;i++)
  5.         {
  6.                 for(j=0;j<n/2;j++)
  7.                 {
  8.                         tmp=mt[i*n+j];
  9.                         mt[i*n+j]=mt[i*n+n-j-1];
  10.                         mt[i*n+n-j-1]=tmp;
  11.                 }
  12.         }
  13. }
复制代码


  1. int i=0,bigcount=0,lowcount=0,spacecount=0,numcount=0,othercount=0;
  2. while(str[i])
  3. {
  4.         if(str[i]>='a'&&str[i]<='z')bigcount++;
  5.         else if(str[i]>='A'&&str[i]<='Z')lowcount++;
  6.         else if(str[i]>='0'&&str[i]<='9')numcount++;
  7.         else if(0x20==str[i])spacecount++;
  8.        
  9.         i++;
  10. }
  11. othercount=i- bigcount-lowcount-spacecount-numcount;
复制代码

  1. ////template <class T>
  2. double abs(double n)
  3. {
  4.         return n<0?-1*n:n;
  5. }
复制代码
  1. double mul(double a,double b)
  2. {
  3.         return a*b;
  4. }
复制代码


  1. void invertstr(char* str)
  2. {
  3.         int len=0;
  4.         while(str[len])len++;
  5.         for(int i=0;i<len/2;i++)
  6.         {
  7.                 str[len]=str[i];
  8.                 str[i]=str[len-i-1];
  9.                 str[len-i-1]=str[len];
  10.         }
  11.         str[len]='\0';
  12. }

复制代码

  1. void invertmt(int (*mt)[3])
  2. {
  3.            for (int i = 0; i < 3;i++)
  4.            {
  5.                    for (int j = i+1; j < 3; j++)
  6.                    {
  7.                            int t=mt[i][j];
  8.                            mt[i][j]=mt[j][i];
  9.                            mt[j][i]=t;
  10.                    }
  11.            }
  12. }
复制代码

  1. void outputstr(char* str)
  2. {
  3.         int i=0;
  4.         do
  5.         {
  6.                 printf("%c",str[i]);
  7.                 printf("%c",0x20);
  8.                 i++;
  9.         }while(str[i+1]);
  10.         printf("%c",str[i]);
  11. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-5-22 15:25:53 | 显示全部楼层

你用的是什么编译器啊
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-5-22 15:27:23 | 显示全部楼层
本帖最后由 jhq999 于 2022-5-22 15:31 编辑
yangbaowen 发表于 2022-5-22 15:25
你用的是什么编译器啊


有什么问题吗?这些代码里面好像没有和编译器有关系的,模板那个我也改了,c里面没有模板
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-5-22 15:37:00 | 显示全部楼层
jhq999 发表于 2022-5-22 15:27
有什么问题吗?这些代码里面好像没有和编译器有关系的,模板那个我也改了,c里面没有模板

但是我用Devc++,报错了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-5-22 15:39:09 | 显示全部楼层
本帖最后由 jhq999 于 2022-5-22 15:43 编辑

那个报错了,我没用编译器,直接在回复里写的。可能有的写错了变量名,有的我只写了只要功能。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-5-22 15:57:21 | 显示全部楼层
jhq999 发表于 2022-5-22 15:39
那个报错了,我没用编译器,直接在回复里写的。可能有的写错了变量名,有的我只写了只要功能。

都报错了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-5-22 16:59:52 | 显示全部楼层
本帖最后由 jhq999 于 2022-5-22 17:16 编辑


你不会自己没写主函数吧
  1. void change(int *mt,int m,int n)/////////(1)
  2. {
  3.         int i=0,j=0,tmp=0;
  4.         for(i=0;i<m;i++)
  5.         {
  6.                 for(j=0;j<n/2;j++)
  7.                 {
  8.                         tmp=mt[i*n+j];
  9.                         mt[i*n+j]=mt[i*n+n-j-1];
  10.                         mt[i*n+n-j-1]=tmp;
  11.                 }
  12.         }
  13. }
  14. int main()
  15. {
  16.         int mt[12]={1,2,3,4,5,6,7,8,9,10,11,12};
  17.         change(mt,3,4);
  18.         for (int i = 0; i < 12; i++)
  19.         {
  20.                 if (0==i%4)
  21.                 {
  22.                         printf("\n");
  23.                 }
  24.                 printf("%4d ",mt[i]);
  25.                
  26.                
  27.         }
  28.         return 0;
  29. }
复制代码
  1. int main()
  2. {
  3.         char str[32]="abc fff  #A b 1234 5%&";
  4.         int i=0,bigcount=0,lowcount=0,spacecount=0,numcount=0,othercount=0;
  5.         while(str[i])
  6.         {
  7.                 if(str[i]>='a'&&str[i]<='z')lowcount++;
  8.                 else if(str[i]>='A'&&str[i]<='Z')bigcount++;
  9.                 else if(str[i]>='0'&&str[i]<='9')numcount++;
  10.                 else if(0x20==str[i])spacecount++;

  11.                 i++;
  12.         }
  13.         othercount=i- bigcount-lowcount-spacecount-numcount;
  14.         printf("%d %d %d %d %d",bigcount,lowcount,spacecount,numcount,othercount);
  15.         return 0;
  16. }
复制代码
  1. double abs(double n)
  2. {
  3.         return n<0?-1*n:n;
  4. }
  5. int main()
  6. {
  7.         int a=-4;
  8.         float b=-3.7f;
  9.         double c=7;
  10.         printf("%lf %lf %lf",abs(a),abs(b),abs(c));
  11.         return 0;
  12. }
复制代码
  1. double mul(double a,double b)
  2. {
  3.         return a*b;
  4. }
  5. int main()
  6. {
  7.        
  8.         printf("%lf",mul(4.2,5.1));
  9.         return 0;
  10. }
复制代码
  1. void invertstr(char* str)
  2. {
  3.         int len=0;
  4.         while(str[len])len++;
  5.         for(int i=0;i<len/2;i++)
  6.         {
  7.                 str[len]=str[i];
  8.                 str[i]=str[len-i-1];
  9.                 str[len-i-1]=str[len];
  10.         }
  11.         str[len]='\0';
  12. }


  13. int main()
  14. {
  15.        
  16.         char str[32]="abcdefg";
  17.         invertstr(str);
  18.         printf("%s",str);
  19.         return 0;
  20. }
复制代码
  1. void invertmt(int (*mt)[3])
  2. {
  3.            for (int i = 0; i < 3;i++)
  4.            {
  5.                    for (int j = i+1; j < 3; j++)
  6.                    {
  7.                            int t=mt[i][j];
  8.                            mt[i][j]=mt[j][i];
  9.                            mt[j][i]=t;
  10.                    }
  11.            }
  12. }


  13. int main()
  14. {
  15.        
  16.         int str[3][3]={1,2,3,4,5,6,7,8,9};
  17.         invertmt(str);
  18.         for (int i = 0; i < 3; i++)
  19.         {
  20.                 for (int j = 0; j < 3; j++)
  21.                 {
  22.                         printf("%4d",str[i][j]);
  23.                 }
  24.                 printf("\n");
  25.         }
  26.         return 0;
  27. }
复制代码
  1. void outputstr(char* str)
  2. {
  3.         int i=0;
  4.         do
  5.         {
  6.                 printf("%c",str[i]);
  7.                 printf("%c",0x20);
  8.                 i++;
  9.         }while(str[i+1]);
  10.          printf("%c",str[i]);
  11. }



  12. int main()
  13. {
  14.         char str[5];
  15.         scanf("%s",str);
  16.         outputstr(str);
  17.         return 0;
  18. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-5-22 17:15:47 | 显示全部楼层
jhq999 发表于 2022-5-22 16:59
你不会自己没写主函数吧

额,没有。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-5-22 17:19:34 | 显示全部楼层

我想偷个懒都不成
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-5-24 00:02:20 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-4 12:16

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表