- 论坛徽章:
- 2
|
算法5没有发现在哪里。算法4有问题。- /* number.c */
- #include <ctype.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define ALEN(a) (sizeof(a)/sizeof((a)[0]))
- int get_line(char *buf, int buf_len, int *o_len)
- {
- if (fgets(buf, buf_len, stdin) != NULL) {
- if (o_len != NULL) {
- *o_len = strlen(buf);
- }
- return 0;
- }
- return -1;
- }
- int get_number(const char *s, int off, int lmt, int *o_off, int *o_val)
- {
- const char *pstart;
- char *pend;
- int len;
- long value;
- if (off < lmt) {
- pstart = s + off;
- pend = (char *)pstart;
- value = strtol(pstart, &pend, 10);
- if (pend != pstart) {
- len = pend - s;
- if (len <= lmt) {
- *o_off = len;
- *o_val = value;
- return 0;
- }
- }
- }
- return -1;
- }
- int get_numbers(const char *text, int *o_array, int a_len, int *o_len)
- {
- char line_buf[256];
- int lmt;
- int off;
- int i;
- printf("%s: ", text == NULL ? "" : text);
- if (get_line(line_buf, sizeof(line_buf), &lmt) == 0) {
- off = 0;
- for (i = 0; i < a_len; ++i) {
- if (get_number(line_buf, off, lmt, &off, &o_array[i]) == 0) {
- continue;
- }
- break;
- }
- if (i > 0) {
- if (o_len != NULL) {
- *o_len = i;
- }
- return 0;
- }
- }
- return -1;
- }
- int select_menu(void)
- {
- const char *menu_text[] = {
- "1 input numbers and query greater numbers",
- "2 input double column numbers and query greater numbers",
- "3 input triple column numbers and query greater numbers",
- "4 input numbers and do operations",
- "5",
- "6 quit",
- };
- int i;
- int val;
- for (;;) {
- for (i = 0; i < ALEN(menu_text); ++i) {
- puts(menu_text[i]);
- }
- if (get_numbers("Please input the menu number", &val, 1, NULL) == 0) {
- if (val >= 1 && val <= ALEN(menu_text)) {
- return val;
- }
- }
- puts("ERROR: invalid input, input again");
- }
- }
- int compare_numbers(const int *num1, const int *num2, int cnt)
- {
- int i;
- for (i = 0; i < cnt; ++i) {
- if (num1[i] <= num2[i]) {
- return 0;
- }
- }
- return 1;
- }
- int query_123(const int *ai, int n, const int *num, int cnt)
- {
- int i;
- int counter;
-
- counter = 0;
- for (i = 0; i < n; ++i) {
- if (compare_numbers(ai + i * cnt, num, cnt)) {
- ++counter;
- }
- }
- return counter;
- }
- int do_menu_123(int cnt)
- {
- int n;
- int m;
- int num[3];
- int i;
- int len;
- int *ai;
- int *result;
- if (get_numbers(NULL, &n, 1, NULL) == 0 && n > 0) {
- ai = (int *)malloc(n * sizeof(int) * cnt);
- if (ai != NULL) {
- memset(ai, 0, n * sizeof(int) * cnt);
- if (cnt == 1) {
- for (;;) {
- if (get_numbers(NULL, ai, n, &len) == 0 && len == n) {
- break;
- }
- }
- }else {
- for (i = 0; i < n;) {
- if (get_numbers(NULL, ai + i * cnt, cnt, &len) == 0 && len == cnt) {
- ++i;
- }
- }
- }
- if (get_numbers(NULL, &m, 1, NULL) == 0 && m > 0) {
- result = (int *)malloc(m * sizeof(int));
- if (result != NULL) {
- memset(result, 0, sizeof(int) * m);
- for (i = 0; i < m;) {
- if (get_numbers(NULL, num, cnt, NULL) == 0) {
- result[i++] = query_123(ai, n, num, cnt);
- }
- }
- for (i = 0; i < m; ++i) {
- printf("%d\n", result[i]);
- }
- free(result);
- }
- }
- free(ai);
- }
- }
- return 0;
- }
- int get_identifier(const char *s, int off, int lmt, int *o_off)
- {
- if (isalpha(s[off])) {
- do {
- ++off;
- }while (isalpha(s[off]) || isdigit(s[off]));
- *o_off = off;
- return 0;
- }
- return -1;
- }
- int get_space(const char *s, int off, int lmt, int *o_off)
- {
- if (isspace(s[off])) {
- do {
- ++off;
- }while (isspace(s[off]));
- *o_off = off;
- return 0;
- }
- return -1;
- }
- int compare_text(const char *s, int off, int lmt, const char *tab[], int tab_len)
- {
- int len;
- int i;
- for (i = 0; i < tab_len; ++i) {
- len = strlen(tab[i]);
- if (off + len == lmt && strncmp(s + off, tab[i], len) == 0) {
- return i;
- }
- }
- return -1;
- }
- int do_menu_4(void)
- {
- char line_buf[256];
- const char *cmd[] = {"a", "d", "q"};
- const char *var[] = {"a1", "a2"};
- int a1, a2;
- int lmt;
- int off;
- int next;
- int cmd_id;
- int p1;
- int p2;
- int val;
- int n;
- int i;
- a1 = 0;
- a2 = 0;
- if (get_numbers(NULL, &n, 1, NULL) == 0) {
- for (i = 0; i < n; ++i) {
- if (get_line(line_buf, sizeof(line_buf), &lmt) == 0) {
- off = 0;
- get_space(line_buf, off, lmt, &off);
- if (get_identifier(line_buf, off, lmt, &next) == 0) {
- cmd_id = compare_text(line_buf, off, next, cmd, ALEN(cmd));
- if (cmd_id != -1) {
- off = next;
- switch (cmd_id) {
- case 0:
- get_space(line_buf, off, lmt, &off);
- if (get_identifier(line_buf, off, lmt, &next) == 0) {
- cmd_id = compare_text(line_buf, off, next, var, ALEN(var));
- if (cmd_id != -1) {
- off = next;
- if (get_number(line_buf, off, lmt, &off, &val) == 0) {
- switch (cmd_id) {
- case 0:
- a1 += val;
- break;
- case 1:
- a2 += val;
- break;
- }
- }
- }
- }
- break;
- case 1:
- get_space(line_buf, off, lmt, &off);
- if (get_identifier(line_buf, off, lmt, &next) == 0) {
- cmd_id = compare_text(line_buf, off, next, var, ALEN(var));
- if (cmd_id != -1) {
- off = next;
- if (get_number(line_buf, off, lmt, &off, &val) == 0) {
- switch (cmd_id) {
- case 0:
- a1 -= val;
- break;
- case 1:
- a2 -= val;
- break;
- }
- }
- }
- }
- break;
- case 2:
- get_space(line_buf, off, lmt, &off);
- if (get_identifier(line_buf, off, lmt, &next) == 0) {
- p1 = compare_text(line_buf, off, next, var, ALEN(var));
- if (p1 != -1) {
- off = next;
- get_space(line_buf, off, lmt, &off);
- if (get_identifier(line_buf, off, lmt, &next) == 0) {
- p2 = compare_text(line_buf, off, next, var, ALEN(var));
- if (p2 != -1) {
- }
- }else {
- switch (p1) {
- case 0:
- printf("%d\n", a1);
- break;
- case 1:
- printf("%d\n", a2);
- break;
- }
- }
- }
- }
- break;
- }
- }
- }
- }
- }
- }
- return 0;
- }
- int main(void)
- {
- int menu_do;
- for (;;) {
- menu_do = select_menu();
- switch (menu_do) {
- case 1:
- do_menu_123(1);
- break;
- case 2:
- do_menu_123(2);
- break;
- case 3:
- do_menu_123(3);
- break;
- case 4:
- do_menu_4();
- case 5:
- break;
- case 6:
- return 0;
- }
- }
- }
复制代码 |
|