変数表を作る課題
変数を登録する部分だけ
変数表(もどき)は変数名.型名,有効範囲だけで良いとのこと
あとはソースから変数を登録しまくればいけるような気がする.
#include <iostream> #include <string> #include <map> using namespace std; typedef struct _VariableInfo{ //変数の情報 string Type; //型名 string Scope; //所属関数名 }VariableInfo; class VariableTableClass{ map<string,VariableInfo> m_VariableInfo; //変数表 public: int insert(string,VariableInfo); //変数表に変数を登録する int search(string); //変数表から変数を検索する int output(); //変数表に登録されている変数をすべて表示する }; int VariableTableClass::insert(string sVariableName,VariableInfo vinfo){ m_VariableInfo.insert(make_pair(sVariableName,vinfo)); return 0; } int VariableTableClass::search(string sVariableName){ map<string,VariableInfo>::iterator p; p = m_VariableInfo.find(sVariableName); if(p != m_VariableInfo.end()){ //変数が見つかった cout << "変数名:" << p->first << "\t型:" << p->second.Type << "\t有効範囲:" << p->second.Scope << endl; } else{ //見つからなかった cout << sVariableName << "は登録されていません" << endl; } return 0; } int VariableTableClass::output(){ map<string,VariableInfo>::iterator p; p = m_VariableInfo.begin(); while(p != m_VariableInfo.end()){ //マップに登録されている変数をすべて表示する cout << "変数名:" << p->first << "\t型:" << p->second.Type << "\t有効範囲:" << p->second.Scope << endl; p++; } return 0; } //適当に実験 int main(){ VariableTableClass testclass; VariableInfo testinfo; string foo; testinfo.Scope = "グローバル"; testinfo.Type = "int"; foo = "testValue"; testclass.insert(foo,testinfo); testinfo.Scope = "適当な関数名"; testinfo.Type = "char"; foo = "testValue2"; testclass.insert(foo,testinfo); //登録 testclass.search(foo); //foo(testValue2)を検索 cout << endl; foo = "aaa"; testclass.search(foo); //全然関係ない変数(aaa)を検索 testclass.output(); //全部表示 return 0; }