フィルターのクリア

Need help with conversion program

7 ビュー (過去 30 日間)
Joseph
Joseph 2014 年 2 月 13 日
コメント済み: nor ain maisarah 2020 年 12 月 24 日
Need help creating a program that is capable of converting from cm or in or ft into units of cm or in or ft. If incorrect units are input by the program user print an appropriate message and do not perform conversion.
ex] Command window should prompt 3 times:
Enter the Input Distance: 1
Enter the input Units(cm,in,ft): ft
Enter output units to convert to(cm,in,ft): in
1 ft = 12 in
ex]
Enter the Input Distance: 1
Enter the input Units(cm,in,ft): x
Incorrect input units
Thanks
  2 件のコメント
Thomas
Thomas 2014 年 2 月 13 日
what have you done so far? and where are you getting stuck?
Joseph
Joseph 2014 年 2 月 13 日
in=1
cm=2.54*in
ft=12*in
Distance=input('Enter a Distance:');
Units=input('Enter Units Of Distance(cm,ft,in):');
Output=input('Enter Output Units to Convert to(cm,in,ft):');
if (Units='in' & Output='cm')
Out=Distance*cm
end
fprintf('\n\nConversion=%g\n',Out)

サインインしてコメントする。

採用された回答

Anuj
Anuj 2014 年 2 月 13 日
There can be many solutions for this, smaller one also, but for a beginner level, this will sure work for you.
d= input('Enter the Input Distance: ');
i= input('Enter the input Units(cm,in,ft): ');
o= input('Enter output units to convert to(cm,in,ft): ');
switch i
case 'cm'
if o=='in'
x=d/2.54;
else
if o=='ft'
x=d/30.48;
else
if o=='cm'
x=d;
end
end
end
case 'in'
if o=='in'
x=d;
else
if o=='ft'
x=d/12;
else
if o=='cm'
x=2.54*d;
end
end
end
case 'ft'
if o=='in'
x=d*12;
else
if o=='ft'
x=d;
else
if o=='cm'
x=d*30.48;
end
end
end
str = fprintf('%d %s = %d %s',d,i,x,o);
otherwise
disp('wrong input');
end
  1 件のコメント
nor ain maisarah
nor ain maisarah 2020 年 12 月 24 日
i did not get the answers using these codes, it appears that 'unrecognized cm' when i put cm in i input ?

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2014 年 2 月 13 日
編集済み: Walter Roberson 2014 年 2 月 13 日
Units=input('Enter Units Of Distance(cm,ft,in):', 's');
Also, use strcmp() to compare strings, not "="

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by