• 締切済み

Pascalへの変換について

C言語のプログラムをPascalに変更してほしいのですが 分かりません。 #include<iostream> #include<list> #include<utility> #include<algorithm> void output(const std::pair<char,int>&p){ std::cout << "入力文字'"<< p.first << "' 入力回数 "<< p.second << std::endl; } int main(){ typedef std::list<std::pair<char,int> >list_t; list_t count; for(char c;std::cin.get(c) && c != '.';){ if(c == '\n')continue; list_t::iterator it = count.begin(),end=count.end(); for(;it != end;++it) if(it->first == c)break; if(it == end){ count.push_back(std::make_pair(c,0)); it = count.end(); --it; } ++it->second; } std::for_each(count.begin(),count.end(),output); } よろしくお願いします

  • diva7
  • お礼率33% (3/9)

みんなの回答

回答No.1

もともとのプログラムが、C++(の STL)の 練習問題風で、これを、Pascal に持って行く のは無理でしょう。 「出現する文字の文字数をカウントする」プロ グラムを、Pascal で新たに考えたほうがいいの ではないかと思いますが。 元のプログラムも、「リスト構造の解説のため にわざわざ面倒な方法でプログラムしました」 というような構造です。

関連するQ&A

  • マップとアルゴリズム

    またまた質問です。 以下の処理をさせるとエラーが出ます。 どなたか見ていただけますか? 環境はWindows XP, Visual Studio.NETです。 -----処理部分------------------ map<char, int> m; for(i=0; i<10; i++){  m.insert(pair<char, int>('A'+i, i)); } reverse(m.begin(), m.end()); map<char, int>::iterator p; p = m.begin(); while( p != m.end() ){  cout << p->first << " " << p->second << endl;  p++; } ------以下エラーログ--------------- C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\utility(16) : error C2166: 左辺値は const オブジェクトに指定されています。 C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\utility(46) : コンパイルされたクラスのテンプレートのインスタンス化 'void std::swap<const _Ty1>(const _Ty &,const _Ty &)' の参照を確認してください with [ _Ty1=int, _Ty=int ] C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\utility(45): クラス テンプレートのメンバ関数 'void std::pair<_Ty1,_Ty2>::swap(std::pair<_Ty1,_Ty2>::_Myt &)' のコンパイル中 with [ _Ty1=const int, _Ty2=int ] ClusteringMain.cpp(45) : コンパイルされたクラスのテンプレートのインスタンス化 'std::pair<_Ty1,_Ty2>' の参照を確認してください with [ _Ty1=const int, _Ty2=int ] C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\utility(16) : error C2166: 左辺値は const オブジェクトに指定されています。

  • Sortプログラムについて2

    えっと、前回もSortのプログラムについて、質問させていただいたのですが、このプログラムでは、二つのファイルを使ってソートしてるのですが、一つのファイルだけを使って、それに上書きするためにはどうするればいいでしょうか? 下に、ソースを貼っておきます。 #include <iostream> #include <fstream> #include <list> #include <string> using namespace std; int main() { char Str[255]; list<string> str; int count = 0; ifstream in("ttest", ios::binary | ios::in); if (!in){ cout << "入力ファイルが読み込めない" << endl; exit(1); } ofstream out("out", ios::binary | ios::out); if (!out){ cout << "出力ファイルが読み込めない" << endl; exit(1); } while (!in.eof()){ //!!!! in.getline(Str,255); str.push_back(Str); count++; } str.sort(); list<string>::iterator p; p = str.begin(); while(p!=str.end()){ out << *p << endl; p++; } in.close(); out.close(); getchar(); return 0; } よろしくお願いします。

  • 自分で作成したプログラムについて…。

    visual stdio 2013 デスクトップ版のものを使い、下に書くプログラムを作ったのですが、 visual stdio 2013では、ファイルが開けませんとなり、途中で、終了してしまうのですが、mingwでは、ちゃんとファイルが開けて、プログラムが、最後まで動作するのですが、なぜでしょうか? 原因を詳しく教えてください。また、改善方法も教えてください。 プログラム #include <iostream> #include <fstream> #include <ctime> #include <cstdlib> #include <list> #include <string> using namespace std; int main() { srand((unsigned)time(NULL)); list<string> str; char sstr[255]; int count = 0; int i; ifstream in("ttest", ios::in | ios::binary); if (!in){ cout << "入力ファイルが開けません\n"; getchar(); return 1; } while (!in.eof()){ in.getline(sstr, 255); str.push_back(sstr); count++; } i = rand() % count ; list<string>::iterator p; p = str.begin(); for (int j = 2; j <= i; j++)p++; cout << *p; cout << endl; getchar(); return 0; }

  • vectorの中にmap

    vectorの中にmapを入れて 添字:ノードID [どのノードから来たのか|それまでのコスト] を表現しようと考えています. #include<iostream> #include<vector> #include<map> #include<list> // MACROS #define UNDEF -1 // PROTOTYPE DCLARE void init_path(std::vector<std::map<char, int> >, int size); int main(void) { //source -> source node // //size -> the number of node // //path -> store path infomation // ex: // path[1]: 1 is node id // char : from node id // cost : how cost from source to here // //adj -> show adjacency list eace node int source; int size; std::vector<std::map<char, int> > path; std::vector<std::list<char> > adj; size = 5; std::cout << "before" << std::endl; init_path(path, size); std::cout << "after" << std::endl; std::map<char, int>::iterator it; for(int i = 0; i < size; i++) { it = path[i].begin(); // std::cout << it->first << ":" << it->second << std::endl; } return 0; } void init_path(std::vector<std::map<char, int> > path, int size) { std::map<char, int> init; init.insert( std::map<char, int>::value_type('-', UNDEF) ); for(int i = 0; i < size ; i++) { path.push_back(init); } return; } *結果 before after Segmentation fault となり初期化をする所までは正常に動いたっぽいのですが どこが悪いのかわかりません(おそらくイテレータあたりかと思うのですが・・・・ 具体的にどうしたらいいのか分からないのでご指導ねがいます.

  • CSVを用いた検索プログラム動かし方

    #pragma warning( disable: 4996 ) #include <stdio.h> #include <string.h> #include <fstream> using namespace std; typedef struct tagKOTOWAZA{ char japanese[50]; char english[50]; }KOTOWAZA; int main() { char buf[256]; KOTOWAZA c[200]; int i, count; /*ifstream strtok strcpy を使ってファイルを読み込む*/ //=====ここから===== ifstream fin("Book1.csv"); if(fin.is_open()){ //ファイル内容の表示とクローズ for(count=0;fin.getline(buf, sizeof(buf)), !fin.eof();count++){//読み込める間 //printf("%s",buf);デバッグ用 char *p; p = strtok(buf,","); if(p)strcpy(c[count].japanese,p); p = strtok(NULL,","); if(p)strcpy(c[count].english,p); } fin.close(); }else{ printf("ファイルのオープンに失敗しました。\n"); return 1; } //=====ここまで==== return 0; } ここからどうすればCSVファイルに書いた 漢字,English を検索できるのかがわかりません・・・ 言語はC++を使っていますvisualstudio2012を使っています。 どうすればいいのか全く分かりません。 よろしくお願いします。

  • テンプレートクラスとSTLを利用したMyListクラス

    こんにちは。STLのリストを使い自分だけのMyListクラスを作ろうとしたのですが、コンパイルできません。 エラーメッセージは警告 std::list<T>::iterator' : 依存名は型ではありません。 とでます。 ご教授お願いします。 #include<list> #include<iterator> template <class T> class CMyList { public: CMyList(); //virtual ~CMyList(); //bool HasNext(); //T Next(); //void Pushback( T t ); //void EraseCheck(); //T GetFirst(); private: std::list< T > m_List; std::list< T >::iterator m_It;//コンパイルエラー }; template <class T> CMyList<T>::CMyList() { m_List.clear(); std::list< T > ::iterator it = m_List.begin();//こう宣言する分にはOK m_It = m_List.begin(); } int main() { CMyList<int> m_List; return 0; }

  • ベクターの初歩について

    ベクターについて勉強し始めた所です。ベクターについて解らない事があるので教えて下さい。 今ベクターに10個数字を登録します。 後で3か5があれば取り除きます。 #include <vector> #include <iostream> int main(){   using namespace std;   vector<int> array1;   for( int i = 0; i < 10; ++i )     array1.push_back( i );   vector<int>::iterator it;   for(it = array1.begin(); it != array1.end(); ){     if(*it == 3 || *it==5)       it = array1.erase(it);     else       ++it;   }   for( it = array1.begin(); it != array1.end(); ++it )     cout << *it << endl;   return 0; } 一応出来たんですが、これが構造体だったらどうしたらいいのでしょう? typedef struct{   int x,y; }xy_t; vector<xy_t> array1; だとして、最初にx,yのそれぞれ10個に適当な値を入れておき、xが3か5ならそれを削除するにはどうしたらいいのでしょうか。 また、一つずつ削除する方法と、remove関数で出来そうな気がするので、もし一括で出来る方法があればそちらも2種類お願いします。 (構造体を用いたベクターの使い方が書いてある参考サイトでも結構です) XP Pro VS2005Pro よろしくお願いします。

  • C++ template コンパイルできないパターン

    度々お世話になります。 以下のソースがコンパイルできません。 ご存知の方がいらっしゃれば教えて下さい。 #include <iostream> #include <exception> template<typename T, T C> inline T check(T x) { if (x == C) { throw(std::exception()); } return x; } int main() { using namespace std; int x; try { char * p = "abc"; check<char*, 0>(p); // コレがコンパイルできない int i = 2; check<int, 4>(i); // コレはコンパイルできる } catch (...) { cerr << "err" << endl; } return 0; } 手元の環境だと % g++ foo.cc foo.cc: In function 'int main()': foo.cc:18: error: no matching function for call to 'check(char*&)' となります。 関係するのか分からないのですが char* をテンプレートの引数にしているのに コンパイラのエラーメッセージは char*& となっているのがよく分かりません。 目的としては、エラーチェックをして エラーをであれば、例外を投げるというものです。

  • プログラムの説明

    C++の初心者です。 ↓のプログラムの動作はさっぱりわかりませんが、それについての説明は具体的に教えていただきたいです。(できれば、詳しく) #include <iostream> #include <string> int getNinzu(int ARGC, char *ARGV[]) throw (char const *){ if(ARGC!=2){ throw "Needs only one argument."; } int ninzu=std::atoi(ARGV[1]); if(ninzu<=0){ throw "Value is too small."; } return ninzu; } #include <cstdlib> #include <ctime> int randfive(){ static bool firsttime=true; if(firsttime){ firsttime=false; std::srand(std::time(NULL)); } return static_cast<int>(static_cast<double>(std::rand())/RAND_MAX*(5+1)); } #include <iomanip> int main(int ARGC, char* ARGV[]){ std::string cmdname=ARGV[0]; int ninzu; try{ ninzu=getNinzu(ARGC,ARGV); std::cout << std::setfill('0'); for (int i = 1; i <= ninzu; ++i) { int score = 0; for (int k = 0; k < 20; ++k) score += randfive(); std::cout << "C" << std::setw(5) << i << " " << score << '\n'; } }catch(char const *str){ std::cerr << str << std::endl << "Usage: " << cmdname << " ninzu" << std::endl; return 1; } }

  • char型・int型を相互変換させるプログラミング

    今、C++で2つのPC間で数値データの送受信を出来るようにしようと思い、実際にデータを飛ばせるところまで来たのですが、送れるデータの型はchar型のみであるとのこと。 ですので、intの値を一旦charにして送り、送った先でまたintにしたらいいかなと考えてプログラミングしました。 簡単にできると思ったのですが、上手くいきません・・・。プログラムのどの部分が間違っているのでしょうか? 一人では、これ以上悩んでも好転しないので、皆さんのご指導をお願いします 以下プログラム抜粋、このプログラムでは型変換がおこなわれているかの確認ができればいいので、それのみでプログラミングしています =================================================== #include <stdio.h> #include <stdlib.h> #include <string> #include <iostream> int main() { int a=123; char *b=new char; int c=1; //変換部分 *b=(char)a; std::cout<<"int→char>>>"<< c << "\n"; //逆変換部分 d=atoi(c); std::cout<<"char→int>>>"<< d << "\n"; } ======================================================

専門家に質問してみよう