c++添加swich case string 的支持


    #pragma once  
    // ----------------------------------------------------------------------------  
    // string_switch_case.h  
    //  
    // These macros together implement switch-case functionality for C strings and  
    // std::string.  When first encountered, the switch-case structure does two  
    // passes.  The first pass is a one-time initialization of a std::map with the  
    // strings as keys, and line numbers as values, to get the unique keys needed   
    // for a numeric switch-case.  All subsequent passes use the std::map to  
    // retrieve an unsigned integer for a given string, that was mapped during the  
    // first pass, to use for a numeric switch case.  This switch-case approach can  
    // be used repeatedly and even nested.  An example of the usage is as follows:  
    //  
    // switch_string(a)  
    // {  
    // case_string("first string")  
    //    printf("first string");  
    //    break;  
    // case_string("second string")  
    //    printf("second string");  
    // case_string("third string")  
    //    printf("falls through to third string");  
    //    break;  
    // default_case_string  
    //    printf("default");  
    // }  
    // end_switch_string  
    //  
    // The end_switch_string macro is required after the closing brace of the  
    // switch-case structure.  Each case_string statement must be on a unique line.  
    // Each case_string has its own local scope.  A case_string statement is  
    // ignored if declared within scope brackets beneath a sibling case statement.  
    // ----------------------------------------------------------------------------  
      
    #include <map>  
    #include <string>  
      
      
    // switch macro  
    #define switch_string(a) while (true) {     static std::map<std::string, unsigned int> _string_lookup_;   static bool _init_ = true;  unsigned int _val_ = 0;     if (!_init_) {      std::map<std::string, unsigned int>::iterator _iter_ =            _string_lookup_.find(a);            if (_iter_ != _string_lookup_.end()) {              _val_ = (*_iter_).second; }             else {              _val_ = 0xffffffff;             }   }   switch(_val_) {     case 0:  
      
      
    // case macro  
    #define case_string(a)      }   case __LINE__:      if (_init_)             _string_lookup_.insert              (std::pair<std::string, unsigned int>(a, __LINE__));      else {  
      
      
    // default case macro  
    #define default_case_string         }   default:        if (!_init_) {  
      
      
    // required macro for the end of the switch-case  
    #define end_switch_string   }   if (_init_) {       _init_ = false;     }   else {      break;  } }  





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include<iostream> #include<string> #include<fstream> #include <vector> #include <functional> #include <algorithm> #include<string> using namespace std; class Employee{ protected: int number; string name; public: Employee(){} Employee(int num,string nam); void setname(string n); void setnumber(int n); string getname(void); int getnumber(void); virtual void display(void); friend bool less_number(const Employee & m1, const Employee & m2); }; class EmployeeDetial:public Employee{ protected: string sex; int age; public: EmployeeDetial(){} EmployeeDetial(int num,string nam,string se,int ag); void setsex (string s); void setage(int a); string getsex(void); int getage(void); void display(void); friend bool less_age(const EmployeeDetial & m1, const EmployeeDetial & m2); }; void Employee::setname(string n){ name=n; } void Employee::setnumber(int n){ number=n; } string Employee::getname(void){ return name; } int Employee::getnumber(void){ return number; } void Employee::display(void){ // cout<<"E.display"<<endl; cout<<number<<" "<<name<<endl; } void EmployeeDetial::setsex(string s){ sex=s; } void EmployeeDetial::setage(int a){ age=a; } string EmployeeDetial::getsex(void){ return sex; } int EmployeeDetial::getage(void){ return age; } void EmployeeDetial::display(void){ // cout<<"ED.display"<<endl; cout<<number<<" "<<name<<" "<<sex <<" "<<age<<" "<<endl; } EmployeeDetial::EmployeeDetial(int num,string nam,string se,int ag){ number=num; name=nam; sex=se; age=ag; } Employee::Employee(int num,string nam){ number=num; name=nam; } bool less_number(const Employee & m1, const Employee & m2) { return m1.number<m2.number; } bool less_age(const EmployeeDetial & m1, const EmployeeDetial & m2) { return m1.age<m2.age; } using namespace std; static int counter=0; vector<EmployeeDetial> vED; vector<Employee> vE; void load_f_ED(){ int num,age; string name,sex; ifstream fin("EmployeeDetial.txt"); if(!fin){ cout<<"open file error!"<<endl; exit(1); } while(fin>>num>>name>>sex>>age){ // fin>>num>>name>>sex>>age; vED.push_back(EmployeeDetial(num,name,sex,age)); counter++; } fin.close(); } bool load_f_E(){ int num; string name; ifstream fin("Employee.txt"); if(!fin)return false; while(fin>>num>>name){ // fin>>num>>name>>sex>>age; vE.push_back(Employee(num,name)); } fin.close(); return true; } void display_E(){ for(int i=0;i<counter;i++)vE.at(i).display(); } void display_ED(){ for(int i=0;i<counter;i++){ vED.at(i).display(); } } void E_fout(){ ofstream fout("Employee.txt"); for(int i=0;i<counter;i++){ fout<<vE.at(i).getnumber()<<" "<<vE.at(i).getname()<<"\n"; } fout.close(); } void ED_fout(){ ofstream fout("EmployeeDetial.txt"); for(int i=0;i<counter;i++){ fout<<vED.at(i).getnumber()<<" "<<vED.at(i).getname()<<" "<< vED.at(i).getsex()<<" "<<vED.at(i).getage()<<"\n"; } fout.close(); } void E_ED(){ for(int i=0;i<counter;i++)vE.push_back(Employee(vED.at(i).getnumber(),vED.at(i).getname())); } void add(){ int num,age; string name,sex; cout<<"input the number:"<<endl; cin>>num; cout<<"input the name:"<<endl; cin>>name; cout<<"input the sex:"<<endl; cin>>sex; cout<<"input the age:"<<endl; cin>>age; vED.push_back(EmployeeDetial(num,name,sex,age)); vE.push_back(Employee(num,name)); counter++; cout<<"add success!"<<endl; } void delet(){ int a; cout<<"input the number which you want to delete:"<<endl; cin>>a; for(int i=0;i<counter;i++){ if(vED.at(i).getnumber()==a){ for(int n=i;n<counter-1;n++){ vED.at(n)=vED.at(n+1); vE.at(n)=vE.at(n+1); } } } counter--; } void screen(){ cout<<"1. 增加职工记录"<<endl; cout<<"2. 删除职工记录"<<endl; cout<<"3. 生成信息简表"<<endl; cout<<"4. 显示原始记录"<<endl; cout<<"5. 显示简表记录"<<endl; cout<<"6. 原始记录排序"<<endl; cout<<"7. 简表记录排序"<<endl; cout<<"8. 结束程序运行"<<endl; } int dd(){ int a; screen(); cin>>a; return a; } void ED_sort_age(){ sort(vED.begin(), vED.end(), less_age); } void ED_sort_number(){ sort(vED.begin(), vED.end(), less_number); } void E_sort_number(){ sort(vE.begin(), vE.end(), less_number); } int main(){ int a=dd(); while(a!=8){ load_f_ED(); if(!load_f_E())E_ED(); switch(a){ case 1: add();break; case 2:delet();break; case 3: E_fout();cout<<"success!"<<endl;break; case 4: display_ED();break; case 5:display_E();break; case 6:cout<<"1.按number排序"<<endl; cout<<"2.按age排序"<<endl; cin>>a; if(1==a){ED_sort_number();display_ED();break;} if(2==a){ED_sort_age();display_ED();break;} case 7:E_sort_number();display_E();break; default:cout<<"what are you doing?"<<endl;;break; } ED_fout(); E_fout(); vED.clear(); vE.clear(); counter=0; a=dd(); } return 0; }
#define _CRT_SECURE_NO_WARNINGS 1 #ifndef __CONTACT_H_ #define __CONTACT_H_ #include<stdio.h> #include<stdlib.h> #include<string.h> #define PEO_MAX 30 #define STUID_MAX 10 #define NAME_MAX 20 #define SEX_MAX 10 #define TEL_MAX 15 typedef struct People { char id[STUID_MAX]; char name[NAME_MAX]; char sex[SEX_MAX]; char tel[TEL_MAX]; }*peo; typedef struct Contact { int count; struct People people[PEO_MAX]; }*pCon; void add_peo(pCon pcon); //添加联系人信息 void show_peo(pCon pcon); //显示指定联系人信息 void find_peo(pCon pcon); //查找联系人信息 void modify_peo(pCon pcon); //修改指定联系人信息 void clear_peo(pCon pcon); //清空联系人信息 void show_menu(); //菜单显示 #endif int search(pCon pcon, char *name) { int i = 0; for (i=0; i < pcon->count; i++) { if (strcmp(name, pcon->people[i].name) == 0) return i; } return -1; } void add_peo(pCon pcon) //添加联系人 { if (pcon->count == PEO_MAX) { printf("The contact has fullen."); return; } printf("please input studentID: "); scanf("%s",(pcon->people[pcon->count]).id); printf("please input name: "); scanf("%s", (pcon->people[pcon->count]).name); printf("please input sex : "); scanf("%s", (pcon->people[pcon->count]).sex); printf("please input tel: "); scanf("%s", (pcon->people[pcon->count]).tel); pcon->count++; } void show_peo(pCon pcon)//显示联系人 { int i = 0; for (; i < pcon->count; i++) { printf("studentID name sex tel \n"); printf("%s\t%s\t%s\t%s\n", pcon->people[i].id, pcon->people[i].name, pcon->people[i].sex, pcon->people[i].tel); } } void find_peo(pCon pcon) //查找联系人信息 { int i; char name[NAME_MAX]; printf("please input the people you want to find:"); scanf("%s", name); i = search(pcon, name); if (i == -1) printf("The people doesn't exsit.\n"); else printf("%s\t%s\t%s\t%s\n", pcon->people[i].id, pcon->people[i].name, pcon->people[i].sex, pcon->people[i].tel); } void modify_peo(pCon pcon) //修改指定联系人信息 { int i; char name[NAME_MAX]; printf("please input the people you want to modify:"); scanf("%s", name); i = search(pcon, name); if (i == -1) printf("The people doesn't exsit.\n"); else printf("please input studentID: "); scanf("%s", (pcon->people[i]).id); printf("please input name: "); scanf("%s", (pcon->people[i]).name); printf("please input sex : "); scanf("%s", (pcon->people[i]).sex); printf("please input tel: "); scanf("%s", (pcon->people[i]).tel); } void clear_peo(pCon pcon) //清空联系人 { pcon->count = 0; } void show_menu() //菜单显示 { printf("==========通讯录==========\n"); printf(" \n"); printf("*******[1]显示通讯录******\n"); printf("*******[2]查 询******\n"); printf("*******[3]修 改******\n"); printf("*******[4]结 束******\n"); } int main() { struct Contact my_contact; int input = 1; my_contact.count = 0; while (input) { show_menu(); printf("please input:"); scanf("%d", &input;); switch (input) { case 1: add_peo(&my;_contact); break; case 2: show_peo(&my;_contact); break; case 3: find_peo(&my;_contact); break; case 4: modify_peo(&my;_contact); break; case 5: clear_peo(&my;_contact); break; default: break; } } return 0; }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值