課題 2-2

テストプログラム例

C 言語


#include <stdio.h>
int cmp(char *s1, char *s2){
 /* ここは自分で書く */
}
int main(void){
  char *teststr[]={"i","c0","c1","int",NULL};
  int i,j;
  for(i=0;teststr[i]!=NULL;i++){
    for(j=0;teststr[j]!=NULL;j++){
      printf("%s %s: %d\n",teststr[i], teststr[j],cmp(teststr[i],teststr[j]));
    }
  }
  return 0;
}

テスト結果

i i: 0
i c0: 1
i c1: 1
i int: 1
c0 i: -1
c0 c0: 0
c0 c1: 1
c0 int: 1
c1 i: -1
c1 c0: -1
c1 c1: 0
c1 int: 1
int i: -1
int c0: -1
int c1: -1
int int: 0

C++ 言語


class Cmp : public std::binary_function<std::string,std::string,bool> {
public:
  bool operator()(const std::string& s1, const std::string& s2){
  // ここは自分で書く
  }
};
int main(void){
  std::string teststr[]={"i","c0","c1","int",""};
  Cmp c;
  for(int i=0; teststr[i]!=""; ++i){
    for(int j=0; teststr[j]!=""; ++j){
      std::cout << teststr[i] << " "
		<< teststr[j] << ": "
		<< c(teststr[i],teststr[j]) << std::endl;
    }
  }
  return 0;
}

テスト結果

i i: 0
i c0: 0
i c1: 0
i int: 0
c0 i: 1
c0 c0: 0
c0 c1: 0
c0 int: 0
c1 i: 1
c1 c0: 1
c1 c1: 0
c1 int: 0
int i: 1
int c0: 1
int c1: 1
int int: 0

Java 5 言語


class Cmp implements java.util.Comparator<String> {
    public int compare(String s1, String s2){
    // ここは自分で書く
    }
}
class CmpTest {
    public static void main(String[] arg){
	java.util.Comparator<String> c = new Cmp();
	String[] teststr=new String[] {"i","c0","c1","int"};
	for(String s1 : teststr){
	    for(String s2 : teststr){
		System.out.println(s1+" "+s2+": "+ c.compare(s1,s2));
	    }
	}
    }
}

テスト結果

i i: 0
i c0: 1
i c1: 1
i int: 1
c0 i: -1
c0 c0: 0
c0 c1: 1
c0 int: 1
c1 i: -1
c1 c0: -1
c1 c1: 0
c1 int: 1
int i: -1
int c0: -1
int c1: -1
int int: 0

Java 1.4 言語


class Cmp implements java.util.Comparator {
    public int compare(Object o1, Object o2){
    // ここは自分で書く
    }
}
class CmpTest {
    public static void main(String[] arg){
	java.util.Comparator c = new Cmp();
	String[] teststr=new String[] {"i","c0","c1","int"};
	for(int i=0; i<teststr.length; i++){
	    for(int j=0; j<teststr.length; j++){
		System.out.print(teststr[i]+" ");
		System.out.print(teststr[j]+": ");
		System.out.println(c.compare(teststr[i],teststr[j]));
	    }
	}
    }
}

テスト結果

i i: 0
i c0: 1
i c1: 1
i int: 1
c0 i: -1
c0 c0: 0
c0 c1: 1
c0 int: 1
c1 i: -1
c1 c0: -1
c1 c1: 0
c1 int: 1
int i: -1
int c0: -1
int c1: -1
int int: 0