鱼C论坛

 找回密码
 立即注册
查看: 1408|回复: 6

[已解决]输入不得重复

[复制链接]
发表于 2023-11-7 19:20:49 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 Eat.随 于 2023-11-7 21:07 编辑

想要编写一个C语言程序,并实现以下功能
1.用户持续输入数字
2.当用户输入的数字与之前输入每一个的数字重复时,停止。
最佳答案
2023-11-7 20:33:34
  1. sh-5.2$ cat main.c
  2. #include "list.h"
  3. #include <stdio.h>

  4. size_t find(const list_t *list, int n) {
  5.     for(size_t i = 0; i < list_size(list); ++i) {
  6.         int x; list_get(list, i, &x, sizeof(x));
  7.         if(x == n) return i;
  8.     }
  9.     return -1;
  10. }

  11. int main(void) {
  12.     list_t *list = list_init();
  13.     while(1) {
  14.         int n; scanf("%d", &n);
  15.         if(find(list, n) != -1) break;
  16.         list_append(list, &n, sizeof(n));
  17.     }
  18.     list_deinit(list);
  19.     return 0;
  20. }
  21. sh-5.2$ cat list.h
  22. #ifndef _LIST_H_
  23. #define _LIST_H_

  24. #include <stddef.h>
  25. #include <stdbool.h>

  26. struct list_node_tag {
  27.     void *data; size_t size;
  28.     struct list_node_tag *next;
  29. };

  30. typedef struct {
  31.     struct list_node_tag *head;
  32.     size_t size;
  33. } list_t;

  34. list_t *list_init(void);
  35. void list_deinit(list_t *list);
  36. bool list_clean(list_t *list);
  37. bool list_insert(list_t *list, size_t index, const void *data, size_t size);
  38. bool list_delete(list_t *list, size_t index);
  39. bool list_get(const list_t *list, size_t index, void *data, size_t size);
  40. bool list_set(list_t *list, size_t index, const void *data, size_t size);
  41. bool list_append(list_t *list, const void *data, size_t size);
  42. size_t list_size(const list_t *list);
  43. bool list_empty(const list_t *list);

  44. #endif
  45. sh-5.2$ cat list.c
  46. #include "list.h"
  47. #include <stdlib.h>
  48. #include <string.h>

  49. static struct list_node_tag *new_node(const void *data, size_t size) {
  50.     struct list_node_tag *result = malloc(sizeof(*result));
  51.     if(!result) return NULL;
  52.     result->data = malloc(size);
  53.     if(!result->data) {free(result); return NULL;}
  54.     memcpy(result->data, data, size);
  55.     result->size = size;
  56.     result->next = NULL;
  57.     return result;
  58. }

  59. static void free_node(struct list_node_tag *n) {
  60.     free(n->data);
  61.     free(n);
  62. }

  63. static struct list_node_tag *const *locate_node_const(const list_t *list, size_t index) {
  64.     struct list_node_tag *const *result = &list->head;
  65.     while(index--) result = &(*result)->next;
  66.     return result;
  67. }

  68. static struct list_node_tag **locate_node(list_t *list, size_t index) {
  69.     return (struct list_node_tag **)locate_node_const(list, index);
  70. }

  71. list_t *list_init(void) {
  72.     list_t *list = malloc(sizeof(*list));
  73.     if(!list) return NULL;
  74.     list->head = NULL;
  75.     list->size = 0;
  76.     return list;
  77. }

  78. void list_deinit(list_t *list) {
  79.     if(!list) return;
  80.     list_clean(list);
  81.     free(list);
  82. }

  83. bool list_clean(list_t *list) {
  84.     if(!list) return false;
  85.     while(!list_empty(list)) if(!list_delete(list, 0)) return false;
  86.     return true;
  87. }

  88. bool list_insert(list_t *list, size_t index, const void *data, size_t size) {
  89.     if(!list) return false;
  90.     if(list_size(list) < index) return false;
  91.     if(!data) return false;
  92.     struct list_node_tag **current = locate_node(list, index);
  93.     struct list_node_tag *node = new_node(data, size);
  94.     if(!node) return false;
  95.     node->next = *current;
  96.     *current = node;
  97.     ++list->size;
  98.     return true;
  99. }

  100. bool list_delete(list_t *list, size_t index) {
  101.     if(!list) return false;
  102.     if(list_size(list) <= index) return false;
  103.     struct list_node_tag **current = locate_node(list, index);
  104.     struct list_node_tag *temp = *current;
  105.     *current = temp->next;
  106.     free_node(temp);
  107.     --list->size;
  108.     return true;
  109. }

  110. bool list_get(const list_t *list, size_t index, void *data, size_t size) {
  111.     if(!list) return false;
  112.     if(list_size(list) <= index) return false;
  113.     if(!data) return false;
  114.     struct list_node_tag *current = *locate_node_const(list, index);
  115.     if(current->size > size) return false;
  116.     memcpy(data, current->data, current->size);
  117.     return true;
  118. }

  119. bool list_set(list_t *list, size_t index, const void *data, size_t size) {
  120.     bool res = list_delete(list, index);
  121.     return res ? list_insert(list, index, data, size) : res;
  122. }

  123. bool list_append(list_t *list, const void *data, size_t size) {
  124.     if(!list) return false;
  125.     return list_insert(list, list_size(list), data, size);
  126. }

  127. size_t list_size(const list_t *list) {
  128.     if(!list) return 0;
  129.     return list->size;
  130. }

  131. bool list_empty(const list_t *list) {
  132.     if(!list) return true;
  133.     return list_size(list) == 0;
  134. }
  135. sh-5.2$ ./main
  136. 1 2 3
  137. 1
  138. sh-5.2$
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-11-7 19:20:57 | 显示全部楼层
你可以使用Python来实现这个功能。你可以使用一个集合(set)来存储用户输入的数字,并在每次输入后检查是否重复。下面是一个示例代码:

  1. numbers = set()
  2. while True:
  3.     num = input("请输入一个数字:")
  4.     if num in numbers:
  5.         print("输入重复,程序停止。")
  6.         break
  7.     numbers.add(num)
复制代码


在这个示例中,我们使用了一个集合(set)来存储用户输入的数字。每次用户输入一个数字后,我们都会检查这个数字是否已经在集合中出现过,如果是的话就停止程序。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-11-7 19:22:22 | 显示全部楼层
  1. #include <stdio.h>
  2. int main() {
  3.     int prev_num = -1;  // 用于存储上一个输入的数字,初始值设为-1表示第一个输入不可能重复
  4.     int current_num;
  5.     printf("请输入数字:");
  6.     scanf("%d", &current_num);
  7.     while (current_num != prev_num) {
  8.         prev_num = current_num;  // 更新上一个输入的数字为当前输入的数字
  9.         printf("请输入数字:");
  10.         scanf("%d", &current_num);
  11.     }
  12.     printf("输入重复,程序终止。\n");
  13.     return 0;
  14. }
复制代码

以上是一个简单的C语言程序,实现了用户持续输入数字,当用户输入的数字与之前输入的数字重复时,程序停止。希望能帮到你。

球一个最佳答案谢谢啦!这对我非常重要!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-11-7 19:24:48 | 显示全部楼层
Mike_python小 发表于 2023-11-7 19:22
以上是一个简单的C语言程序,实现了用户持续输入数字,当用户输入的数字与之前输入的数字重复时,程序停 ...

但是当我输入1,2,3,再次输入1,此时程序不会停止
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-11-7 20:32:31 | 显示全部楼层
  1. sh-5.2$ cat main.cpp
  2. #include <iostream>
  3. #include <vector>
  4. #include <algorithm>

  5. using std::cin;
  6. using std::vector;
  7. using std::find;

  8. int main() {
  9.     vector<int> v;
  10.     while(true) {
  11.         int n; cin >> n;
  12.         if(find(v.begin(), v.end(), n) != v.end()) break;
  13.         v.push_back(n);
  14.     }
  15.     return 0;
  16. }
  17. sh-5.2$ ./main
  18. 1 2 3
  19. 1
  20. sh-5.2$
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-11-7 20:33:34 | 显示全部楼层    本楼为最佳答案   
  1. sh-5.2$ cat main.c
  2. #include "list.h"
  3. #include <stdio.h>

  4. size_t find(const list_t *list, int n) {
  5.     for(size_t i = 0; i < list_size(list); ++i) {
  6.         int x; list_get(list, i, &x, sizeof(x));
  7.         if(x == n) return i;
  8.     }
  9.     return -1;
  10. }

  11. int main(void) {
  12.     list_t *list = list_init();
  13.     while(1) {
  14.         int n; scanf("%d", &n);
  15.         if(find(list, n) != -1) break;
  16.         list_append(list, &n, sizeof(n));
  17.     }
  18.     list_deinit(list);
  19.     return 0;
  20. }
  21. sh-5.2$ cat list.h
  22. #ifndef _LIST_H_
  23. #define _LIST_H_

  24. #include <stddef.h>
  25. #include <stdbool.h>

  26. struct list_node_tag {
  27.     void *data; size_t size;
  28.     struct list_node_tag *next;
  29. };

  30. typedef struct {
  31.     struct list_node_tag *head;
  32.     size_t size;
  33. } list_t;

  34. list_t *list_init(void);
  35. void list_deinit(list_t *list);
  36. bool list_clean(list_t *list);
  37. bool list_insert(list_t *list, size_t index, const void *data, size_t size);
  38. bool list_delete(list_t *list, size_t index);
  39. bool list_get(const list_t *list, size_t index, void *data, size_t size);
  40. bool list_set(list_t *list, size_t index, const void *data, size_t size);
  41. bool list_append(list_t *list, const void *data, size_t size);
  42. size_t list_size(const list_t *list);
  43. bool list_empty(const list_t *list);

  44. #endif
  45. sh-5.2$ cat list.c
  46. #include "list.h"
  47. #include <stdlib.h>
  48. #include <string.h>

  49. static struct list_node_tag *new_node(const void *data, size_t size) {
  50.     struct list_node_tag *result = malloc(sizeof(*result));
  51.     if(!result) return NULL;
  52.     result->data = malloc(size);
  53.     if(!result->data) {free(result); return NULL;}
  54.     memcpy(result->data, data, size);
  55.     result->size = size;
  56.     result->next = NULL;
  57.     return result;
  58. }

  59. static void free_node(struct list_node_tag *n) {
  60.     free(n->data);
  61.     free(n);
  62. }

  63. static struct list_node_tag *const *locate_node_const(const list_t *list, size_t index) {
  64.     struct list_node_tag *const *result = &list->head;
  65.     while(index--) result = &(*result)->next;
  66.     return result;
  67. }

  68. static struct list_node_tag **locate_node(list_t *list, size_t index) {
  69.     return (struct list_node_tag **)locate_node_const(list, index);
  70. }

  71. list_t *list_init(void) {
  72.     list_t *list = malloc(sizeof(*list));
  73.     if(!list) return NULL;
  74.     list->head = NULL;
  75.     list->size = 0;
  76.     return list;
  77. }

  78. void list_deinit(list_t *list) {
  79.     if(!list) return;
  80.     list_clean(list);
  81.     free(list);
  82. }

  83. bool list_clean(list_t *list) {
  84.     if(!list) return false;
  85.     while(!list_empty(list)) if(!list_delete(list, 0)) return false;
  86.     return true;
  87. }

  88. bool list_insert(list_t *list, size_t index, const void *data, size_t size) {
  89.     if(!list) return false;
  90.     if(list_size(list) < index) return false;
  91.     if(!data) return false;
  92.     struct list_node_tag **current = locate_node(list, index);
  93.     struct list_node_tag *node = new_node(data, size);
  94.     if(!node) return false;
  95.     node->next = *current;
  96.     *current = node;
  97.     ++list->size;
  98.     return true;
  99. }

  100. bool list_delete(list_t *list, size_t index) {
  101.     if(!list) return false;
  102.     if(list_size(list) <= index) return false;
  103.     struct list_node_tag **current = locate_node(list, index);
  104.     struct list_node_tag *temp = *current;
  105.     *current = temp->next;
  106.     free_node(temp);
  107.     --list->size;
  108.     return true;
  109. }

  110. bool list_get(const list_t *list, size_t index, void *data, size_t size) {
  111.     if(!list) return false;
  112.     if(list_size(list) <= index) return false;
  113.     if(!data) return false;
  114.     struct list_node_tag *current = *locate_node_const(list, index);
  115.     if(current->size > size) return false;
  116.     memcpy(data, current->data, current->size);
  117.     return true;
  118. }

  119. bool list_set(list_t *list, size_t index, const void *data, size_t size) {
  120.     bool res = list_delete(list, index);
  121.     return res ? list_insert(list, index, data, size) : res;
  122. }

  123. bool list_append(list_t *list, const void *data, size_t size) {
  124.     if(!list) return false;
  125.     return list_insert(list, list_size(list), data, size);
  126. }

  127. size_t list_size(const list_t *list) {
  128.     if(!list) return 0;
  129.     return list->size;
  130. }

  131. bool list_empty(const list_t *list) {
  132.     if(!list) return true;
  133.     return list_size(list) == 0;
  134. }
  135. sh-5.2$ ./main
  136. 1 2 3
  137. 1
  138. sh-5.2$
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-11-7 20:53:51 | 显示全部楼层
本帖最后由 jackz007 于 2023-11-7 20:58 编辑
  1. #include <stdio.h>
  2. #include <conio.h>

  3. int main(void)
  4. {
  5.         char c , s[2048]                    ;
  6.         int  k , n , m                      ;
  7.         for(k = m = 0 ; k < 2048 ;) {
  8.                 c = getch()                 ;
  9.                 n = 0                       ;
  10.                 while(n < m) {
  11.                         if(s[n] == c) break ;
  12.                         n ++                ;
  13.                 }
  14.                 if(n == m) {
  15.                         s[m ++] = c         ;
  16.                         putch(c)            ;
  17.                 } else {
  18.                         break               ;
  19.                 }
  20.         }
  21.         putch('\r')                         ;
  22. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-22 01:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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