VS2015bug ->const char[]+string后开头多了“?”符号

本文介绍了一种在C++中将整数转换为字符串,并与文件路径进行拼接的方法,通过使用sprintf和stringstream,实现了动态创建文件路径的需求。

string+char[]相加后输出

cv::Mat frame = imread(files[i].c_str());
		char str[25] = { 0 };
		sprintf(str, "%d", i);
		cout << "‪C:\\Users\\m1873\\Desktop\\newout\\" + (std::string)str << endl;//luji

在这里插入图片描述

解决办法 string + string

 char str[25] = { 0 };
    sprintf(str, "%d", i);
	string base_out_path = "C:\\Users\\m1873\\Desktop\\newout\\";
	cout << base_out_path + (std::string)str << endl;//luji

在这里插入图片描述

char *GetSetClass::strtokq(char *s1, const char *delim, STRTOKQ_PRMS *pPrms) { char *p; cyg_int16 index; if (s1 != NULL) { /* First token */ pPrms->pStart = s1; // pPrms->pStart is only used for error checking pPrms->pEnd = s1 + strlen(s1); index = strcspn(s1, delim); /* Skip delimiters */ while (!index && *s1) { index = strcspn(++s1, delim); } if (*s1 == 0) { return (0); } pPrms->pNext = s1; pPrms->pNext++; if (*s1 == QUOTATION_CHAR) { index = 1; while (index) { while (*pPrms->pNext && (*pPrms->pNext != QUOTATION_CHAR)) { pPrms->pNext++; } if (*pPrms->pNext) { index = strcspn(++pPrms->pNext, delim); if (index == 0) { *pPrms->pNext = 0; } else { pPrms->pNext += index; } } else { /* End of input string */ index = 0; } } } else { pPrms->pNext = s1 + index; *pPrms->pNext = 0; } return (s1); } /* error checking */ if ((pPrms->pNext > pPrms->pEnd) || (pPrms->pNext < pPrms->pStart) || (pPrms->pEnd < pPrms->pStart) || (pPrms->pStart == NULL)) { // BUG !! errCode = GETSET_TOKENS_ERROR; return (0); } /* Next token */ if (pPrms->pNext == pPrms->pEnd) { return (0); } pPrms->pNext++; index = strcspn(pPrms->pNext, delim); /* Skip delimiters */ while (!index && *pPrms->pNext) { index = strcspn(++pPrms->pNext, delim); } if (*pPrms->pNext == 0) { return (0); } p = pPrms->pNext; pPrms->pNext++; if (*p == QUOTATION_CHAR) { index = 1; while (index) { while (*pPrms->pNext && (*pPrms->pNext != QUOTATION_CHAR)) { pPrms->pNext++; } if (*pPrms->pNext) { index = strcspn(++pPrms->pNext, delim); if (index == 0) { *pPrms->pNext = 0; } else { pPrms->pNext += index; } } else { /* End of input string */ index = 0; } } } else if (*p == LEFT_ANGLE_BRACKET) { index = 1; while (index) { while (*pPrms->pNext && (*pPrms->pNext != RIGHT_ANGLE_BRACKET)) { pPrms->pNext++; } if (*pPrms->pNext) { index = strcspn(++pPrms->pNext, delim); if (index == 0) { *pPrms->pNext = 0; } else { pPrms->pNext += index; } } else { /* End of input string */ index = 0; } } } else { pPrms->pNext = p + index; *pPrms->pNext = 0; } return (p); } /* * * Description: * Copy strtokq for temp_cmd */ char *GetSetClass::strtokq1(char *s1, const char *delim, STRTOKQ_PRMS *pPrms) { char *p; cyg_int16 index; if (s1 != NULL) { /* First token */ pPrms->pStart = s1; // pPrms->pStart is only used for error checking pPrms->pEnd = s1 + strlen(s1); index = strcspn(s1, delim); /* Skip delimiters */ while (!index && *s1) { index = strcspn(++s1, delim); } if (*s1 == 0) { return (0); } pPrms->pNext = s1; pPrms->pNext++; if (*s1 == QUOTATION_CHAR) { index = 1; while (index) { while (*pPrms->pNext && (*pPrms->pNext != QUOTATION_CHAR)) { pPrms->pNext++; } if (*pPrms->pNext) { index = strcspn(++pPrms->pNext, delim); if (index == 0) { *pPrms->pNext = 0; } else { pPrms->pNext += index; } } else { /* End of input string */ index = 0; } } } else { pPrms->pNext = s1 + index; *pPrms->pNext = 0; } return (s1); } /* error checking */ if ((pPrms->pNext > pPrms->pEnd) || (pPrms->pNext < pPrms->pStart) || (pPrms->pEnd < pPrms->pStart) || (pPrms->pStart == NULL)) { // BUG !! errCode = GETSET_TOKENS_ERROR; return (0); } /* Next token */ if (pPrms->pNext == pPrms->pEnd) { return (0); } pPrms->pNext++; index = strcspn(pPrms->pNext, delim); /* Skip delimiters */ while (!index && *pPrms->pNext) { index = strcspn(++pPrms->pNext, delim); } if (*pPrms->pNext == 0) { return (0); } p = pPrms->pNext; pPrms->pNext++; if (*p == QUOTATION_CHAR) { index = 1; while (index) { while (*pPrms->pNext && (*pPrms->pNext != QUOTATION_CHAR)) { pPrms->pNext++; } if (*pPrms->pNext) { index = strcspn(++pPrms->pNext, delim); if (index == 0) { *pPrms->pNext = 0; } else { pPrms->pNext += index; } } else { /* End of input string */ index = 0; } } } else if (*p == LEFT_ANGLE_BRACKET) { index = 1; while (index) { while (*pPrms->pNext && (*pPrms->pNext != RIGHT_ANGLE_BRACKET)) { pPrms->pNext++; } if (*pPrms->pNext) { index = strcspn(++pPrms->pNext, delim); if (index == 0) { *pPrms->pNext = 0; } else { pPrms->pNext += index; } } else { /* End of input string */ index = 0; } } } else { pPrms->pNext = p + index; *pPrms->pNext = 0; } return (p); }这个函数帮忙解析下
08-27
以下是一个完整的 **C语言配置文件解析模块**,支持: - **INI 格式**(如 `hostapd.conf`):支持 `[General]`、`[BSS]` 等 section。 - **WPA_SUPPLICANT 格式**(如 `wpa_supplicant.conf`):支持 `network={...}` 结构。 - **值配置项**(如 `key=val1 val2`)。 - **section_id 支持**:区分个 `network` 段。 - **通用 API 接口**:支持增删改查。 --- ## 📁 文件结构 ``` config_parser/ ├── config_parser.h ├── config_parser.c └── main.c ``` --- ## 📄 1. `config_parser.h` ```c #ifndef CONFIG_PARSER_H #define CONFIG_PARSER_H #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #define MAX_KEY_LEN 256 #define MAX_VAL_LEN 1024 #define MAX_VALUES 32 #define MAX_SECTION_LEN 256 // 值配置项 typedef struct config_item { char key[MAX_KEY_LEN]; char values[MAX_VALUES][MAX_VAL_LEN]; int value_count; } ConfigItem; // 支持嵌套结构(如 network={...}) typedef struct config_section { char name[MAX_SECTION_LEN]; // 段名,如 "network" char section_id[MAX_VAL_LEN]; // 段标识,如 ssid="my_ssid" ConfigItem **items; int item_count; } ConfigSection; // 整个配置 typedef struct { ConfigSection **sections; int section_count; } Config; // 创建和释放 Config *config_new(); void config_free(Config *config); // hostapd.conf 读写(INI 风格) int config_read(Config *config, const char *filename); int config_write(Config *config, const char *filename); // wpa_supplicant.conf 读写(network={...} 风格) int config_read_wpa(Config *config, const char *filename); int config_write_wpa(Config *config, const char *filename); // 获取某个 key 的值(支持 BSS 的 bss=xxx) int config_get_value(Config *config, const char *section, const char *bss, const char *key, char *value, size_t len); // 设置某个 key 的值(不存在则新增) int config_set_value(Config *config, const char *section, const char *bss, const char *key, const char *value); // 删除某个 key(支持 BSS) int config_del_key(Config *config, const char *section, const char *bss, const char *key); // 删除某个 section(支持 BSS) int config_del_section(Config *config, const char *section, const char *bss); // 新增 section(支持 BSS) int config_add_section(Config *config, const char *section, const char *bss); #endif // CONFIG_PARSER_H ``` --- ## 📄 2. `config_parser.c` ```c #include "config_parser.h" // 工具函数:去除字符串两端空格 void trim(char *str) { char *end; while (isspace((unsigned char)*str)) str++; if (*str == 0) { *str = '\0'; return; } end = str + strlen(str) - 1; while (end > str && isspace((unsigned char)*end)) end--; *(end + 1) = '\0'; } // 创建配置 Config *config_new() { return calloc(1, sizeof(Config)); } // 释放配置 void config_free(Config *config) { for (int i = 0; i < config->section_count; i++) { ConfigSection *sec = config->sections[i]; for (int j = 0; j < sec->item_count; j++) free(sec->items[j]); free(sec->items); free(sec); } free(config->sections); free(config); } // 查找 section(支持 bss) ConfigSection *find_section(Config *config, const char *section_name, const char *bss_name) { for (int i = 0; i < config->section_count; i++) { ConfigSection *sec = config->sections[i]; if (strcmp(sec->name, section_name) == 0) { if (strcmp(section_name, "BSS") == 0) { if (bss_name && strcmp(sec->bss_name, bss_name) == 0) return sec; } else { return sec; } } } return NULL; } // 获取某个 key 的值 int config_get_value(Config *config, const char *section, const char *bss, const char *key, char *value, size_t len) { ConfigSection *sec = find_section(config, section, bss); if (!sec) return -1; for (int i = 0; i < sec->item_count; i++) { if (strcmp(sec->items[i]->key, key) == 0) { strncpy(value, sec->items[i]->values[0], len); return 0; } } return -1; } // 设置某个 key 的值(不存在则新增) int config_set_value(Config *config, const char *section, const char *bss, const char *key, const char *value) { ConfigSection *sec = find_section(config, section, bss); if (!sec) { config_add_section(config, section, bss); sec = find_section(config, section, bss); } for (int i = 0; i < sec->item_count; i++) { if (strcmp(sec->items[i]->key, key) == 0) { char *token = strtok((char *)value, " "); int count = 0; while (token && count < MAX_VALUES) { strncpy(sec->items[i]->values[count++], token, MAX_VAL_LEN - 1); token = strtok(NULL, " "); } sec->items[i]->value_count = count; return 0; } } ConfigItem *new_item = calloc(1, sizeof(ConfigItem)); strncpy(new_item->key, key, MAX_KEY_LEN - 1); char *token = strtok((char *)value, " "); int count = 0; while (token && count < MAX_VALUES) { strncpy(new_item->values[count++], token, MAX_VAL_LEN - 1); token = strtok(NULL, " "); } new_item->value_count = count; sec->items = realloc(sec->items, (sec->item_count + 1) * sizeof(ConfigItem *)); sec->items[sec->item_count++] = new_item; return 0; } // 删除某个 key(支持 BSS) int config_del_key(Config *config, const char *section, const char *bss, const char *key) { ConfigSection *sec = find_section(config, section, bss); if (!sec) return -1; for (int i = 0; i < sec->item_count; i++) { if (strcmp(sec->items[i]->key, key) == 0) { free(sec->items[i]); for (int j = i; j < sec->item_count - 1; j++) sec->items[j] = sec->items[j + 1]; sec->item_count--; return 0; } } return -1; } // 删除某个 section(支持 BSS) int config_del_section(Config *config, const char *section, const char *bss) { for (int i = 0; i < config->section_count; i++) { ConfigSection *sec = config->sections[i]; if (strcmp(sec->name, section) == 0) { if (strcmp(section, "BSS") == 0) { if (bss && strcmp(sec->bss_name, bss) == 0) { for (int j = 0; j < sec->item_count; j++) free(sec->items[j]); free(sec->items); free(sec); for (int j = i; j < config->section_count - 1; j++) config->sections[j] = config->sections[j + 1]; config->section_count--; return 0; } } else { for (int j = 0; j < sec->item_count; j++) free(sec->items[j]); free(sec->items); free(sec); for (int j = i; j < config->section_count - 1; j++) config->sections[j] = config->sections[j + 1]; config->section_count--; return 0; } } } return -1; } // 新增 section(支持 BSS) int config_add_section(Config *config, const char *section, const char *bss) { ConfigSection *new_sec = calloc(1, sizeof(ConfigSection)); strncpy(new_sec->name, section, MAX_SECTION_LEN - 1); if (strcmp(section, "BSS") == 0 && bss) strncpy(new_sec->bss_name, bss, MAX_VAL_LEN - 1); new_sec->items = NULL; new_sec->item_count = 0; config->sections = realloc(config->sections, (config->section_count + 1) * sizeof(ConfigSection *)); config->sections[config->section_count++] = new_sec; return 0; } // 读取 INI 格式配置文件(hostapd.conf) int config_read(Config *config, const char *filename) { FILE *fp = fopen(filename, "r"); if (!fp) return -1; char line[1024]; ConfigSection *current_section = NULL; while (fgets(line, sizeof(line), fp)) { char *comment = strchr(line, '#'); if (comment) *comment = '\0'; char *end = line + strlen(line) - 1; while (end >= line && isspace((unsigned char)*end)) end--; *(end + 1) = '\0'; if (strlen(line) == 0) continue; if (line[0] == '[') { char *end_bracket = strchr(line, ']'); if (end_bracket) { *end_bracket = '\0'; char *name = line + 1; current_section = find_section(config, name, NULL); if (!current_section) { config_add_section(config, name, NULL); current_section = find_section(config, name, NULL); } } } else { char key[MAX_KEY_LEN], val[MAX_VAL_LEN]; if (sscanf(line, "%[^=]=%s", key, val) == 2) { trim(key); trim(val); if (current_section) { config_set_value(config, current_section->name, current_section->bss_name, key, val); } else { config_set_value(config, "General", NULL, key, val); } } } } fclose(fp); return 0; } // 写入 INI 格式配置文件 int config_write(Config *config, const char *filename) { FILE *fp = fopen(filename, "w"); if (!fp) return -1; for (int i = 0; i < config->section_count; i++) { ConfigSection *sec = config->sections[i]; if (strcmp(sec->name, "") != 0) { if (strcmp(sec->name, "BSS") == 0 && sec->bss_name[0] != '\0') { fprintf(fp, "[%s]\n", sec->name); fprintf(fp, "bss=%s\n", sec->bss_name); } else { fprintf(fp, "[%s]\n", sec->name); } } for (int j = 0; j < sec->item_count; j++) { ConfigItem *item = sec->items[j]; fprintf(fp, "%s=", item->key); for (int k = 0; k < item->value_count; k++) { fprintf(fp, "%s ", item->values[k]); } fprintf(fp, "\n"); } fprintf(fp, "\n"); } fclose(fp); return 0; } // 读取 WPA_SUPPLICANT 格式配置文件(network={...}) int config_read_wpa(Config *config, const char *filename) { FILE *fp = fopen(filename, "r"); if (!fp) return -1; char line[1024]; ConfigSection *current_section = NULL; int in_section = 0; while (fgets(line, sizeof(line), fp)) { char *comment = strchr(line, '#'); if (comment) *comment = '\0'; char *end = line + strlen(line) - 1; while (end >= line && isspace((unsigned char)*end)) end--; *(end + 1) = '\0'; if (strlen(line) == 0) continue; if (strncmp(line, "network={", 9) == 0) { current_section = calloc(1, sizeof(ConfigSection)); strncpy(current_section->name, "network", MAX_SECTION_LEN - 1); current_section->items = NULL; current_section->item_count = 0; config->sections = realloc(config->sections, (config->section_count + 1) * sizeof(ConfigSection *)); config->sections[config->section_count++] = current_section; in_section = 1; continue; } if (in_section && strcmp(line, "}") == 0) { in_section = 0; // 尝试从当前 section 中找 ssid,用于 section_id for (int i = 0; i < current_section->item_count; i++) { if (strcmp(current_section->items[i]->key, "ssid") == 0) { strncpy(current_section->section_id, current_section->items[i]->values[0], sizeof(current_section->section_id)); break; } } current_section = NULL; continue; } if (in_section) { char key[MAX_KEY_LEN], val[MAX_VAL_LEN]; if (sscanf(line, "%[^=]=%s", key, val) == 2) { trim(key); trim(val); ConfigItem *new_item = calloc(1, sizeof(ConfigItem)); strncpy(new_item->key, key, MAX_KEY_LEN - 1); char *token = strtok(val, " "); int count = 0; while (token && count < MAX_VALUES) { strncpy(new_item->values[count++], token, MAX_VAL_LEN - 1); token = strtok(NULL, " "); } new_item->value_count = count; current_section->items = realloc(current_section->items, (current_section->item_count + 1) * sizeof(ConfigItem *)); current_section->items[current_section->item_count++] = new_item; } } } fclose(fp); return 0; } // 写入 WPA_SUPPLICANT 格式配置文件 int config_write_wpa(Config *config, const char *filename) { FILE *fp = fopen(filename, "w"); if (!fp) return -1; for (int i = 0; i < config->section_count; i++) { ConfigSection *sec = config->sections[i]; if (strcmp(sec->name, "network") == 0) { fprintf(fp, "network={\n"); for (int j = 0; j < sec->item_count; j++) { ConfigItem *item = sec->items[j]; fprintf(fp, " %s=", item->key); for (int k = 0; k < item->value_count; k++) { fprintf(fp, "%s ", item->values[k]); } fprintf(fp, "\n"); } fprintf(fp, "}\n\n"); } } fclose(fp); return 0; } ``` --- ## 📄 3. `main.c` 测试文件 ```c #include "config_parser.h" #include <stdio.h> int main() { Config *config = config_new(); // 读取 hostapd.conf if (config_read(config, "hostapd.conf") == 0) { char value[1024]; config_get_value(config, "BSS", "wlan0.1", "ssid", value, sizeof(value)); printf("SSID: %s\n", value); config_set_value(config, "BSS", "wlan0.1", "ssid", "NewHome1"); config_add_section(config, "BSS", "wlan0.3"); config_set_value(config, "BSS", "wlan0.3", "ssid", "Guest"); config_write(config, "new_hostapd.conf"); config_free(config); } config = config_new(); // 读取 wpa_supplicant.conf if (config_read_wpa(config, "wpa_supplicant.conf") == 0) { for (int i = 0; i < config->section_count; i++) { ConfigSection *sec = config->sections[i]; if (strcmp(sec->name, "network") == 0 && strcmp(sec->section_id, "\"my_ssid\"") == 0) { for (int j = 0; j < sec->item_count; j++) { if (strcmp(sec->items[j]->key, "psk") == 0) { strncpy(sec->items[j]->values[0], "\"new_password\"", sizeof(sec->items[j]->values[0])); sec->items[j]->value_count = 1; break; } } break; } } config_write_wpa(config, "new_wpa.conf"); config_free(config); } return 0; } ``` --- ## ✅ 编译命令(Linux) ```bash gcc -o config_parser main.c config_parser.c ``` ---其中nt config_write(Config *config, const char *filename) { FILE *fp = fopen(filename, "w"); if (!fp) return -1; for (int i = 0; i < config->section_count; i++) { ConfigSection *sec = config->sections[i]; if (strcmp(sec->name, "") != 0) { if (strcmp(sec->name, "BSS") == 0 && sec->section_id[0] != '\0') { fprintf(fp, "[%s]\n", sec->name); fprintf(fp, "bss=%s\n", sec->bss_name); } else { fprintf(fp, "[%s]\n", sec->name); } } for (int j = 0; j < sec->item_count; j++) { ConfigItem *item = sec->items[j]; fprintf(fp, "%s=", item->key); for (int k = 0; k < item->value_count; k++) { fprintf(fp, "%s ", item->values[k]); } fprintf(fp, "\n"); } fprintf(fp, "\n"); } fclose(fp); return 0; }并没有sec->bss_name)定义
08-27
#include <stdio.h> #include <string.h> #define MAX 1000 #define NAME_MAX 20 #define SEX_MAX 6 #define ADDR_MAX 30 #define TELE_MAX 13 typedef struct peoinfo { char name[NAME_MAX]; int age; char sex[SEX_MAX]; char addr[ADDR_MAX]; char tele[TELE_MAX]; } peoinfo; typedef struct contact { peoinfo data[MAX]; int sz; } contact; void Initcontact(contact* p) { p->sz = 0; } void Addcontact(contact* p) { if (p->sz >= MAX) { printf("通讯录已满,无法添加更联系人!\n"); return; } printf("请输入姓名:"); scanf("%s", p->data[p->sz].name); printf("请输入年龄:"); scanf("%d", &p->data[p->sz].age); printf("请输入性别:"); scanf("%s", p->data[p->sz].sex); printf("请输入地址:"); scanf("%s", p->data[p->sz].addr); printf("请输入电话号码:"); scanf("%s", p->data[p->sz].tele); p->sz++; printf("联系人添加成功!\n"); } void Showcontact(contact* p) { if (p->sz == 0) { printf("通讯录为空!\n"); return; } for (int i = 0; i < p->sz; i++) { printf("姓名:%s\n", p->data[i].name); printf("年龄:%d\n", p->data[i].age); printf("性别:%s\n", p->data[i].sex); printf("地址:%s\n", p->data[i].addr); printf("电话:%s\n", p->data[i].tele); printf("-----------------\n"); } } void Delcontact(contact* p) { char name[NAME_MAX]; printf("请输入要删除的联系人姓名:"); scanf("%s", name); int flag = 0; for (int i = 0; i < p->sz; i++) { if (strcmp(p->data[i].name, name) == 0) { for (int j = i; j < p->sz - 1; j++) { p->data[j] = p->data[j + 1]; } p->sz--; printf("联系人删除成功!\n"); flag = 1; break; } } if (!flag) { printf("未找到该联系人!\n"); } } void Searchcontact(contact* p) { char name[NAME_MAX]; printf("请输入要查找的联系人姓名:"); scanf("%s", name); int flag = 0; for (int i = 0; i < p->sz; i++) { if (strcmp(p->data[i].name, name) == 0) { printf("姓名:%s\n", p->data[i].name); printf("年龄:%d\n", p->data[i].age); printf("性别:%s\n", p->data[i].sex); printf("地址:%s\n", p->data[i].addr); printf("电话:%s\n", p->data[i].tele); flag = 1; break; } } if (!flag) { printf("未找到该联系人!\n"); } } void Modifycontact(contact* p) { char name[NAME_MAX]; printf("请输入要修改的联系人姓名:"); scanf("%s", name); int flag = 0; for (int i = 0; i < p->sz; i++) { if (strcmp(p->data[i].name, name) == 0) { printf("请输入新的姓名:"); scanf("%s", p->data[i].name); printf("请输入新的年龄:"); scanf("%d", &p->data[i].age); printf("请输入新的性别:"); scanf("%s", p->data[i].sex); printf("请输入新的地址:"); scanf("%s", p->data[i].addr); printf("请输入新的电话号码:"); scanf("%s", p->data[i].tele); printf("联系人修改成功!\n"); flag = 1; break; } } if (!flag) { printf("未找到该联系人!\n"); } } int main() { contact c; Initcontact(&c); int choice; while (1) { printf("请选择操作:\n"); printf("1. 添加联系人\n"); printf("2. 显示所有联系人\n"); printf("3. 删除联系人\n"); printf("4. 查找联系人\n"); printf("5. 修改联系人\n"); printf("0. 退出\n"); scanf("%d", &choice); switch (choice) { case 1: Addcontact(&c); break; case 2: Showcontact(&c); break; case 3: Delcontact(&c); break; case 4: Searchcontact(&c); break; case 5: Modifycontact(&c); break; case 0: return 0; default: printf("无效的选择,请重新输入!\n"); } } } 加入存储文件功能并且按字母顺序排列联系人
06-24
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct{ char *name; double x; double y; }*city; typedef struct Node{ city ct; struct Node* next; }Node, *List; List creatNullList(){ List llist = (List)malloc(sizeof(Node)); if(llist!=NULL) llist->next=NULL; else printf("OUT OF SPACE!\n"); return llist; } int isNullList(List llist){ return (llist->next==NULL); } Node* searchbyName(char* name, List llist){ Node* p; if(llist->next==NULL) return NULL; p=llist->next; while(p!=NULL&&p->ct->name!=name) p=p->next; return p; } Node* searchbyLoc(double x, double y, List llist){ Node* p; if(llist->next==NULL) return NULL; p=llist->next; while(p!=NULL&&p->ct->x!=x&&p->ct->y!=y) p=p->next; return p; } int insertlist(city ct, List llist){ Node* q=llist; while(q!=NULL) q=q->next; Node* p=(Node*)malloc(sizeof(Node)); if(p==NULL){ printf("OUT OF SPACE!\n"); return 0; } else{ p->ct=ct; p->next=NULL; q->next=p; } return 1; } int deletebyName(char* name, List llist){ Node* q=llist, p; while(q->next!=NULL&&q->next->ct->name!=name) q=q->next; if(q->next==NULL) return 0; p=q->next; q->next=p->next; free(p); return 1; } } int deletebyLoc(double x, double y, List llist){ Node* q=llist, p; while(q->next!=NULL&&q->next->ct->x!=x&&q->next->ct->y!=y) q=q->next; if(q->next==NULL) return 0; p=q->next; q->next=p->next; free(p); return 1; } } void printlist(List llist){ Node* p=llist; while(p!=NULL){ printf("%s (%f,%f)\n", p->ct->name, p->ct->x, p->ct->y); p=p->next; } } void update(city newcity, city ct, List llist){ Node* p; if(llist->next==NULL) return NULL; p=llist->next; while(p!=NULL&&p->ct!=ct) p=p->next; if(p==NULL){ printf("NOT FOUND!\n"); return; } p->ct=newcity; printf("%s的信息已更新!", p->ct->name); } void initSampleData(List llist) { city cities[] = { {"北京", 39.90, 116.40}, {"上海", 31.23, 121.47}, {"广州", 23.13, 113.26}, {"深圳", 22.54, 114.06}, {"成都", 30.57, 104.07}, {"武汉", 30.59, 114.31}, {"南京", 32.06, 118.80}, {"西安", 34.34, 108.94}, {"杭州", 30.27, 120.16}, {"重庆", 29.43, 106.91} }; int i; for (i = 0; i < sizeof(cities)/sizeof(cities[0]); i++) { insertlist(cities[i], llist); } } int main(int argc, char *argv[]) { List llist=createNULList(); initSampleData(llist); int choice; city ct,newct; char name[50]; Node* result; while(1){ printf("1 插入\n"); printf("2 按名称查找\n"); printf("3 按坐标查找\n"); printf("4 按名称删除\n"); printf("5 按坐标删除\n"); printf("6 按名称更新\n"); printf("7 按坐标更新\n"); printf("选择操作: "); scanf("%d", &choice); switch (choice) { case 1: // 插入 printf("输入城市名、纬度及经度,用空格隔开: "); scanf("%s %f %f", ct->name, &ct->x, &ct->y); insertlist(ct,llist); break; case 2: // 按名称查找 printf("输入要查找的城市名: "); scanf("%s", name); result=searchbyName(name,llist); if (result) { printf("找到城市: %s (%.4f, %.4f)\n", result->ct->name, result->city->x, result->city->y); } else { printf("未找到名为%s的城市\n", name); } break; case 3: // 按坐标查找 printf("输入要查找的纬度、经度,用空格隔开: "); scanf("%lf %lf", &x, &y); result=searchbyName(name,llist); if (result) { printf("找到城市: %s (%.4f, %.4f)\n", result->ct->name, result->city->x, result->city->y); } else { printf("未找到坐标为(%.4f, %.4f)的城市\n", x, y); } break; case 4: // 按名称删除 printf("输入要删除的城市名: "); scanf("%s", name); if (deletebyName(name,llist)) { printf("已删除城市: %s (%.4f, %.4f)\n", result->ct->name, result->city->x, result->city->y); } else { printf("未找到名为%s的城市\n", name); } break; case 5: // 按坐标删除 printf("输入要删除的城市纬度和经度,用空格隔开: "); scanf("%f %f", &x, &y); if (deletebyName(name,llist)) { printf("已删除城市: %s (%.4f, %.4f)\n", result->ct->name, result->city->x, result->city->y); } else { printf("未找到坐标为(%.4f, %.4f)的城市\n", x, y); } break; case 6: // 按名称更新 printf("输入要更新的城市名: "); scanf("%s", name); city ct=searchbyName(name,llist)->ct; printf("输入要更新的城市信息,用空格隔开: "); scanf("%s %f %f", newct->name, &newct->x, &newct->y); update(newct, ct, llist); break; case 7: // 按坐标更新 printf("输入要更新的城市的坐标: "); scanf("%f %f", &x, &y); city ct=searchbyLoc(x, y, llist)->ct; printf("输入要更新的城市信息,用空格隔开: "); scanf("%s %f %f", newct->name, &newct->x, &newct->y); update(newct, ct, llist); break; } } return 0; } 改正bug
06-26
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值