鱼C论坛

 找回密码
 立即注册
查看: 2353|回复: 2

[技术交流] 某贴发的面试题解决

[复制链接]
发表于 2018-1-20 19:17:03 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 鱼C丶彪哥 于 2018-1-21 19:52 编辑

1.实现strcat


  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>

  4. char *mystrcat(char *s1, char *s2)
  5. {
  6.         char *tmp = s1;
  7.         while (*(++tmp));
  8.         for (int i = 0; i <= strlen(s2); i++)
  9.         {
  10.                 *(tmp + i) = *(s2 + i);
  11.         }
  12.         return s1;
  13. }

  14. int main(void)
  15. {
  16.         char *s1 = (char *)malloc(sizeof(char)* 50);
  17.         char *s2 = "1401902399";
  18.         strcpy(s1, "BiaoGe");
  19.         mystrcat(s1, s2);
  20.         printf("s1 = %s\n", s1);
  21.         printf("Hello World!\n");
  22.         return 0;
  23. }
复制代码




2.用链表实现简单用户管理系统

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>

  4. void printMenum(void)
  5. {
  6.         printf("\t\t1.录入用户\n");
  7.         printf("\t\t2.查询用户\n");
  8.         printf("\t\t3.排序输出\n");
  9.         printf("\t\t4.删除用户\n");
  10.         printf("\t\t5.保存用户\n");
  11.         printf("\t\t6.载入用户\n");
  12.         printf("\t\t7.退出程序\n");
  13. }

  14. typedef struct TAGUSER
  15. {
  16.         char name[50];
  17.         char phone[12];
  18.         struct TAGUSER *next;
  19. } USER, *pUSER;

  20. pUSER g_uHeader;

  21. void _OutUser(pUSER head)
  22. {
  23.         pUSER tmp;
  24.         tmp = head->next;
  25.         printf("姓  名    手 机 号\n");
  26.         while (tmp)
  27.         {
  28.                 printf("%-10s%s\n", tmp->name, tmp->phone);
  29.                 tmp = tmp->next;
  30.         }
  31. }

  32. void InputUser(pUSER head)
  33. {
  34.         pUSER tmp = head;
  35.         pUSER user = (pUSER)malloc(sizeof(struct TAGUSER));
  36.         fflush(stdin);
  37.         printf("请输入用户名:");
  38.         scanf_s("%s", user->name, 49);
  39.         printf("请输入手机号:");
  40.         scanf_s("%s", user->phone,11);
  41.         while (tmp->next)
  42.         {
  43.                 tmp = tmp->next;
  44.         }
  45.         user->next = NULL;
  46.         tmp->next = user;
  47.         printf("录入成功!\n\t姓名:%s\n\t手机号%s\n", tmp->next->name, tmp->next->phone);
  48. }

  49. void QueryUser(pUSER head)
  50. {
  51.         pUSER tmp = head->next;
  52.         char name[50] = { 0 };
  53.         printf("请输入要查询的名字:");
  54.         scanf_s("%s", name,49);
  55.         while (tmp)
  56.         {
  57.                 if (!strcmp(name, tmp->name))
  58.                 {
  59.                         printf("该用户信息如下:\n");
  60.                         printf("\t姓名:%s\n\t手机号:%s\n",tmp->name,tmp->phone);
  61.                         return;
  62.                 }
  63.                 tmp = tmp->next;
  64.         }
  65.         printf("抱歉,没有该用户!\n");
  66. }

  67. void SortOutput(pUSER head)
  68. {
  69.         pUSER tmp1 = head->next;
  70.         pUSER tmp2 = head->next->next;
  71.         pUSER temp = (pUSER)malloc(sizeof(struct TAGUSER));

  72.         while (tmp1)
  73.         {
  74.                 while (tmp2)
  75.                 {
  76.                         if (strcmp(tmp1->name, tmp2->name) > 0)
  77.                         {
  78.                                 strcpy(temp->name, tmp1->name);
  79.                                 strcpy(temp->phone, tmp1->phone);
  80.                                 strcpy(tmp1->name, tmp2->name);
  81.                                 strcpy(tmp1->phone, tmp2->phone);
  82.                                 strcpy(tmp2->name, temp->name);
  83.                                 strcpy(tmp2->phone, temp->phone);
  84.                         }
  85.                         tmp2 = tmp2->next;
  86.                 }
  87.                 if (!tmp1->next)
  88.                         break;
  89.                 tmp1 = tmp1->next;
  90.                 tmp2 = tmp1->next;
  91.         }
  92.         _OutUser(head);
  93. }

  94. void SaveUser(pUSER head)
  95. {
  96.         pUSER tmp = head->next;
  97.         FILE *wj = fopen("userdata.dat", "w");
  98.         if (!wj)
  99.         {
  100.                 printf("文件打开失败!\n");
  101.                 return;
  102.         }
  103.         while (tmp)
  104.         {
  105.                 fwrite(tmp, sizeof(struct TAGUSER), 1, wj);
  106.                 tmp = tmp->next;
  107.         }
  108.         fclose(wj);
  109.         printf("文件写入完毕,同目录下userdata.dat\n");
  110. }

  111. void DeleteUser(pUSER head)
  112. {
  113.         pUSER tmp = head->next;
  114.         pUSER pre = head;
  115.         char name[50] = { 0 };
  116.         printf("请输入要删除的用户:");
  117.         scanf_s("%s", name,49);
  118.         while (tmp)
  119.         {
  120.                 if (!strcmp(name, tmp->name))
  121.                 {
  122.                         pre->next = tmp->next;
  123.                         free(tmp);
  124.                         printf("删除成功!\n");
  125.                         _OutUser(head);
  126.                         return;
  127.                 }
  128.                 pre = tmp;
  129.                 tmp = tmp->next;
  130.         }
  131. }

  132. void LoadUser(pUSER head)
  133. {
  134.         pUSER tmp = head;
  135.         pUSER tmp1 = (pUSER)malloc(sizeof(struct TAGUSER));
  136.         FILE *wj = fopen("userdata.dat", "r");
  137.         while (fread(tmp1, sizeof(struct TAGUSER), 1, wj))
  138.         {
  139.                 tmp1->next = NULL;
  140.                 tmp->next = tmp1;
  141.                 tmp = tmp->next;
  142.                 tmp1 = (pUSER)malloc(sizeof(struct TAGUSER));
  143.         }
  144.         free(tmp1);
  145.         fclose(wj);
  146.         _OutUser(head);
  147. }

  148. void ExitPro()
  149. {
  150.         printf("感谢您的使用,再见!\n");
  151.         exit(0);
  152. }

  153. int main(void)
  154. {
  155.         int n = 0;
  156.         g_uHeader = (pUSER)malloc(sizeof(struct TAGUSER));
  157.         g_uHeader->next = NULL;
  158.         while (1)
  159.         {
  160.                 system("cls");
  161.                 printMenum();
  162.                 printf("\n请输入:");
  163.                 scanf("%d", &n);
  164.                 switch (n)
  165.                 {
  166.                 case 1:
  167.                         InputUser(g_uHeader);
  168.                         break;
  169.                 case 2:
  170.                         QueryUser(g_uHeader);
  171.                         break;
  172.                 case 3:
  173.                         SortOutput(g_uHeader);
  174.                         break;
  175.                 case 4:
  176.                         DeleteUser(g_uHeader);
  177.                         break;
  178.                 case 5:
  179.                         SaveUser(g_uHeader);
  180.                         break;
  181.                 case 6:
  182.                         system("cls");
  183.                         LoadUser(g_uHeader);
  184.                         break;
  185.                 case 7:
  186.                         ExitPro(g_uHeader);
  187.                         break;
  188.                 default:
  189.                         printf("\n输入有误,请重新输入!\n");
  190.                 }
  191.                 n = 0;
  192.                 system("pause");
  193.         }
  194.         printf("Hello World!\n");
  195.         return 0;
  196. }
复制代码





想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-1-20 21:03:52 | 显示全部楼层
没有吗?

  1. #include <stdio.h>

  2. int main(void)
  3. {
  4.         printf("hello world!\n");
  5.         return 0;
  6. }
复制代码


无标题.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-1-21 13:38:43 | 显示全部楼层

原来这个就是啊,我还以为这是HTML代码呢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-20 23:07

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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