會寫程式的貓
2008年12月15日
引數的處理-轉換數字
User:
MY BLOG
Class:
c++
,
c++ 疑難雜症
在命令列 輸入兩引數 需為數字 並其顯示相除的商數餘數
#include
#include
using namespace std; int main(int x , char *c[]){ double a=0 ; double b=0 ; double d[strlen(c[1])]; for( int x=0 ; x < strlen( c[1] ) ; x++ ){ a+=((double)c[1][x]-48)*(pow(10,strlen(c[1])-x-1)); } for( int x=0 ; x < strlen( c[2] ) ; x++ ){ b+=((double)c[2][x]-48)*(pow(10,strlen(c[2])-x-1)); } cout << a << " / " << b << " = " << ( (int)a / (int)b ) << endl; cout << a << " % " << b << " = " << ( (int)a % (int)b ) << endl; }
寫成函數的型態 比較好看
#include
#include
using namespace std; double f( char arr[] ){ double ans=0; for( int x=0 ; x < strlen(arr) ; x++ ){ ans+=((double)arr[x]-48)*(pow(10,strlen(arr)-x-1)); } return ans; } int main(int x , char *c[]){ int a = (int)f(c[1]); int b = (int)f(c[2]); cout << a << " / " << b << " = " << ( a / b ) << endl; cout << a << " % " << b << " = " << ( a % b ) << endl; }
沒有留言:
張貼留言
較新的文章
較舊的文章
首頁
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言