JavaプログラムをC言語に変換する方法

このQ&Aのポイント
  • Java言語のプログラムをC言語に変換する方法を知りたいです。C言語でのプログラミング経験はありません。
  • 具体的には、以下の2つのJava言語のプログラムをC言語に変換したいです。
  • どのように書けばいいでしょうか?
回答を見る
  • ベストアンサー

Java言語のプログラムをC言語にする場合

次の2つのJava言語のプログラムをC言語にしたいのですが、C言語でプログラムを書いたことがありません。C言語にする場合はどう書けばいいのでしょうか? import java.io.*; public class Sort { public static void main(String[] args) { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); try{ System.out.println("整数値を入力してください"); String line = reader.readLine(); int a = Integer.parseInt(line); String line2 = reader.readLine(); int b = Integer.parseInt(line2); String line3 = reader.readLine(); int c = Integer.parseInt(line3); String line4 = reader.readLine(); int d = Integer.parseInt(line4); int[] data = {a, b, c, d}; for (int i = 0; i< data.length - 1 ; i++) { for (int j = i + 1; j< data.length; j++) { if(data[i] > data[j]) { int e = data[i]; data[i] = data[j]; data[j] = e; } } } System.out.println("昇順に並べ替えると、"); for (int i = 0; i< data.length; i++) { System.out.print(data[i] + " "); } System.out.println("です。"); } catch (IOException e){ System.out.println(e); } catch (NumberFormatException e) { System.out.println("数式の形式が正しくありません。"); } } } import java.io.*; public class Yakusu { public static void main(String[] args) { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); try{ System.out.println("2つの整数値を入力してください"); System.out.print("整数A : "); String line = reader.readLine(); int a = Integer.parseInt(line); System.out.print("整数B : "); String line2 = reader.readLine(); int b = Integer.parseInt(line2); if(a%b == 0){ System.out.println("BはAの約数です"); } else { System.out.println("BはAの約数ではありません"); } } catch (IOException e){ System.out.println(e); } catch (NumberFormatException e) { System.out.println("数式の形式が正しくありません。"); } } }

質問者が選んだベストアンサー

  • ベストアンサー
  • maiko0318
  • ベストアンサー率21% (1483/6970)
回答No.1

半角スペース2個を全角スペース1個に置換しています。 #include <stdio.h> int main(void) {  int a,b,c,d;  printf("整数値を入力してください\n");  scanf("%d",&a);  scanf("%d",&b);  scanf("%d",&c);  scanf("%d",&d);  int data[] = {a,b,c,d};  int length = 4;  int i,j,e;  for (i = 0; i< length - 1 ; i++) {   for (j = i + 1; j< length; j++) {    if(data[i] > data[j]) {     e = data[i];     data[i] = data[j];     data[j] = e;    }   }  }  printf("昇順に並べ替えると、");  for (i = 0; i< length; i++) {   printf("%d ",data[i]);  }  printf("です。\n"); } #include <stdio.h> int main(void) {  int a,b;  printf("2つの整数値を入力してください\n");  printf("整数A : ");  scanf("%d",&a);  printf("整数B : ");  scanf("%d",&b);  if(a%b == 0){   printf("BはAの約数です\n");  } else {   printf("BはAの約数ではありません\n");  } }

saybr125
質問者

お礼

ありがとうございました。C言語がわからなかったのでとても安心しました。

関連するQ&A

  • ifを使った判断プログラムです(おそらく簡単)

    以下のプログラムをコンパイルしたいのですが、8行目のシンボル:変数readerが見つからないと表示されます。 いくら見直しても改善方法がわからないので、質問しました。 おそらく簡単な問題であると思うのですがわからなくて・・・ それともreader.readLine();がコマンドプロントンでは反応できないものなのでしょうか。 教えてください。よろしくお願いします。 import java.io.*; public class Kasa1{ public static void main(String[] args){ BufferedReader br = new BufferedReader(new InputStreamReader(System.in),1); try{ System.out.println("降水確率を入力してください。"); String line = reader.readLine(); int n = Integer.parseInt(line); System.out.println("降水確率は" + n + "%です。"); if(n >= 50){ System.out.println("傘を忘れずにね。"); } else { System.out.println("傘はいりません。"); } System.out.println("いってらっしゃい。"); } catch(IOException e){ System.out.println(e); } catch(NumberFormatException e){ System.out.println("数字の形式が正しくありません。"); } } }

    • ベストアンサー
    • Java
  • Javaのforとwhileの使い方

    プログラミングをはじめたばかりのものです。 「何回か数字を入力してその合計が100になったら終了 するプログラム(100になるまで入力待ちを繰り返すプログラム」 というのを作っているのですが、 以下のようにするとエラーが出ました。 public class upto100 { public static void main(String[] args) { for (int i=0; i <= 100; i=i+j) { String line = reader.readLine(); int j = Integer.parseInt(line); System.out.println("まだ100じゃない"); } System.out.println("もう100"); } } 入力待ちにするString line~~と int j~~~の場所が間違っているのだろうと 思うのですが、どのようにすればよろしいのでしょうか?

    • ベストアンサー
    • Java
  • 前回の質問のプログラムの応用 Java言語

    MAX_QUESTION の部分を指定できるようにしたいのです! たとえば、20と入力したら、20問、問題が生成されるようなプログラムです。 教えてください。よろしくお願いします。 import java.io.*; public class Java05 { /** 表示する問題の個数 */ public static final int MAX_QUESTION=10; /** * 足し算の問題をMAX_QUESTION回繰り返して出題する。 * 最後に正答率を表示する。 */ public static void main(String[] args){ int goodAnswer=0; //正答の個数 System.out.println("これから足し算の問題を"+MAX_QUESTION+"問出します。"); /* * 以下、問題を繰り返し表示し、ユーザからの解答を判断する。 * その後、正答の数を数える。 */ for(int i=0;i<MAX_QUESTION;i++){ boolean ok=showQuestion(i+1); if(ok){ goodAnswer++; } } double rate=goodAnswer*100.0/MAX_QUESTION; System.out.println(""); System.out.println("問題は"+MAX_QUESTION+"問"); System.out.println("正答数は"+goodAnswer+"問"); System.out.println("不正解数"+(MAX_QUESTION-goodAnswer)+"問"); System.out.println("正答率は"+rate+"%"); } /** * showQuestionは足し算の問題を1問出し、答えを待つ。 * 正答、誤答の別を表示する。 * 正答の場合は ture を返す。 */ public static boolean showQuestion(int questno){ double dblA=Math.random()*1000; double dblB=Math.random()*1000; int intA=(int)dblA; int intB=(int)dblB; BufferedReader reader=new BufferedReader(new InputStreamReader(System.in)); try{ System.out.println("[第"+questno+"問] "+intA+"+"+intB+"="); String line=reader.readLine(); int result=Integer.parseInt(line); if(intA+intB==result){ System.out.println("正解!"); return true; }else{ System.out.println("不正解!"); return false; } }catch(IOException e){ System.out.println(e); }catch(NumberFormatException e){ System.out.println("入力が正しくありません。"); } return false; } }

  • 配列の値の参照について

    下記のプログラムの-------------で囲まれた部分が、コンパイルは通るのですが実行すると"value[1][0]"以降が参照できません(ここではcount=3 となっています)。 for文を使わずに"System.out.println(value[1][0])"とすると値を取り出せるのですが…。 実行時のエラーは Exception in thread "main" java.lang.ArrayIndexOutOfBoundException: 3 です。 ソースコードをそのまま載せて長文になってしまい申し訳ありません。 プログラム初心者なので、些細なことでもいいので回答お願いします。 import java.util.*; import java.io.*; public class Search { static String dataFileName = "CityData.txt"; ArrayList<String> array = new ArrayList<String>(); GetDataCount gdc = new GetDataCount(dataFileName); int count = gdc.getCount(); int count2 = 0; String[][] value = new String[count][3]; public Search(String dataFileName){ try{ BufferedReader reader = new BufferedReader( new FileReader(dataFileName) ); String line = ""; while((line = reader.readLine()) != null){ Vector<String> variable = readLine(line); System.out.println(variable); count2++; } }catch(IOException e){ System.out.println(e); } //---------------------------------------------------------- for (int i=0; i<count2; i++){ for (int j=0; i<3; j++){ System.out.println(value[i][j]); } } //---------------------------------------------------------- } public Vector<String> readLine( String line ){ StringTokenizer st = new StringTokenizer( line, "," ); Vector<String> variable = new Vector<String>(); for( int i=0 ; i<3 ; i++ ){ String data = st.nextToken(); variable.addElement( data ); value[count2][i] = data; System.out.println(value[count2][i]); } return variable; } public class GetDataCount{ int count = 0; public GetDataCount(String dataFileName){ try{ BufferedReader reader = new BufferedReader( new FileReader(dataFileName) ); String line = ""; while((line = reader.readLine()) != null){ count++; } }catch(IOException e){ System.out.println(e); } } public int getCount(){ return count; } } public static void main(String[] args){ new Search(dataFileName); } }

  • クラスのフィールドの比較方法

    2つのクラスのフィールドを比較して、その結果を以下のように表示させたいのですが、どのようにプログラミングをすればいいのでしょうか? 例 class Car { String carname; int height; int width; int length; } class Garage { String garagename; int height; int width; int depth; } public class example { public static void main (String[] args) throws IOException { Car car[]; car = new Car[5]; System.out.println("車の情報を入力してください。"); BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in); for(int i=0; i<5; i++){ car[i] = new Car(); System.out.println(i+1 + "台目の車の名前を入力してください。"); String str1 = br1.readLine(); System.out.println("車の高さを入力してください。"); String str2 = br1.readLine(); System.out.println("車の幅を入力してください。"); String str3 = br1.readLine(); System.out.println("車の長さを入力してください。"); String str4 = br1.readLine(); int h1 = Integer.parseInt(str2); int w1 = Integer.parseInt(str3); int l1 = Integer.parseInt(str4); car[i].carname = str1; car[i].height = h1; car[i].weith =w1; car[i].length =l1; } for(int i=0; i<5; i++){ garage[i] = new Garage(); System.out.println(i+1 + "つ目の車庫の名前を入力してください。"); String str5 = br2.readLine(); System.out.println("車庫の高さを入力してください。"); String str6 = br2.readLine(); System.out.println("車庫の幅を入力してください。"); String str7 = br2.readLine(); System.out.println("車庫の長さを入力してください。"); String str8 = br2.readLine(); int h2 = Integer.parseInt(str6); int w2 = Integer.parseInt(str7); int d2 = Integer.parseInt(str8); garage[i].garagename = str2; garage[i].height = h2; garage[i].weith =w2; garage[i].depth =d2; } } 出力例 garage1 car3 garage2 car5 garage3 car4 garage4 car2 garage5 car1 よろしくお願いします。

    • ベストアンサー
    • Java
  • プログラムの組み合わせ方

    java初心者です。 2つのプログラムを組み合わせて1つのプログラムにしたいんですが、イマイチ分かりません。 このプログラムと、 import java.io.*; class Hello2 {  public static void main(String[] args) throws IOException {   System.out.println("名前を入力してください");   BufferedReader br = new BufferedReader(new InputStreamReader(System.in));   String str = br.readLine();   System.out.println(str + "さん、こんにちは!");  } } このプログラムを、 import java.io.*; class Sanbai{  public static void main(String[] args) throws IOException {   System.out.println("好きな数を入力してください");   BufferedReader br = new BufferedReader(new InputStreamReader(System.in));   String str = br.readLine();   int x;   x = Integer.parseInt(str);   System.out.println("あなたの入力した数:" + x);   System.out.println("その3倍の数:" + (x*3));  } } 組み合わせて1つのプログラムにするにはどうしたらいいですか?(>_<)

    • ベストアンサー
    • Java
  • 入力された入力値と最大値、最小値を表示させるプログラムで、最大値と最小

    入力された入力値と最大値、最小値を表示させるプログラムで、最大値と最小値のプログラムを下記に作成してみたら最小値が0になりました。そして、もう一つ繰り返し文を作ってみたら、最大値・最小値がうまく表示されました。この違いはどうしてですか?教えてください。 import java.io.*; class Hairetu1 { public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("データはいくつですか?"); String str = br.readLine(); int num = Integer.parseInt(str); int a[]=new int[num]; int i; int max=0; int min=a[0]; for(i=0; i<a.length; i++){ System.out.print((i+1)+"番目は?"); str = br.readLine(); int tmp = Integer.parseInt(str); a[i] = tmp; if(max<tmp){ max=tmp; } if(min>tmp){ min=tmp; } } System.out.println("入力した値は"+test.length); System.out.println("最大は"+max); System.out.println("最小は"+min); } }

    • ベストアンサー
    • Java
  • クラスのフィールドの値を配列に代入する方法

    フィールドの値を代入したいのですが、 java.lang.ArrayIndexOutOfBoundsException というエラーが出てしまい、どうすればいいのかわかりません。 どのように対処すればいいのでしょうか? class Car { String carname; int height; int width; int length; } class public static void main(String[] args) throw IOException { Car car[]; car = new Car[5]; System.out.println("車の情報を入力してください。"); BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in); for(int i=0; i<5; i++){ car[i] = new Car(); System.out.println(i+1 + "台目の車の名前を入力してください。"); String str1 = br1.readLine(); System.out.println("車の高さを入力してください。"); String str2 = br1.readLine(); System.out.println("車の幅を入力してください。"); String str3 = br1.readLine(); System.out.println("車の長さを入力してください。"); String str4 = br1.readLine(); int h1 = Integer.parseInt(str2); int w1 = Integer.parseInt(str3); int l1 = Integer.parseInt(str4); car[i].carname = str1; car[i].height = h1; car[i].weith =w1; car[i].length =l1; } for(int i=0; i<5; i++){ int c_h[] = new int[5]; c_h[i] = car[i].height; } } よろしくお願いします。

    • ベストアンサー
    • Java
  • コンパイル後のエラー。

    こんにちは、質問があります。 下のプログラムを組みました。 コンパイルには成功しましたが、実行することができません。 import java.io.*; public class Gyouretu { public static void main(String[] args) { BufferedReader reader = new BufferedReader( new InputStreamReader(System.in), 1); try{ System.out.println("行列数を入力"); String line = reader.readLine(); int n = Integer.parseInt(line); int a[][] = new int[n][n]; int b[][] = new int[n][n]; int c[][] = new int[n][n]; int i,j,k; for ( i = 0; i < n; i++){ for ( j = 0; j < n; j++){ c[i][j]=0; for ( k = 0; k < n; k++){ c[i][j] += a[i][k] * b[k][j]; System.out.print(c); System.out.print(i); System.out.print(j); } } } }catch (IOException e){ System.out.println("end"); } } } 行列の計算をするプログラムなのですが、コンパイル後に行列数(何行何列か?)を入力するとこんなエラーが出ます。 「'2' は、内部コマンドまたは外部コマンド、  操作可能なプログラムまたはバッチ ファイルとして認識されていません。」 なぜなのか、どうしてもわかりません。 よろしくお願いします。

    • ベストアンサー
    • Java
  • 2進数変換について教えてくださ。

    public class Class1 { public static void main (String[] args) throws Exception { System.out.println("任意の数値を入力してください:"); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in),1); try{ String line=reader.readLine(); int n = Integer.parseInt(line); for (int m=n ; m>0; m/=2) { int r =m % 2; System.out.print(r); } System.out.println("\n逆から見ると"+n+"の2進数になっています。"); System.in.read(); } catch (IOException e) { System.err.println("Error Message:"+e); System.in.read(); } catch (NumberFormatException e){ System.out.println("数字ではありません"); System.in.read(); } } } 上のプログラムを実行して例えば8という数字を入力するとすると、 任意の数値を入力してください。 8 0001 逆から見ると8の2進数になってます。 というような感じになるのですが、逆から見ないでも2進数になるように (0001)を(1000)のような感じで数値を逆に表示させるにはどのようにすればいいのでしょうか? よろしくお願いします。

    • ベストアンサー
    • Java

専門家に質問してみよう