Created
April 23, 2012 14:40
-
-
Save hirosof/2471340 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <stdio.h> | |
using namespace std; | |
#define SignToPlus(x) ((x<0)?((-1)*x):x) | |
/* | |
n次式÷1次式をする関数 | |
書式:s_div_n1(割られる数の次数 , | |
割られる数の次数ごとの係数を代入した配列へのポインタ , | |
割る数の次数ごとの係数を代入した配列へのポインタ, | |
商と余りの代入先配列へのポインタ); | |
戻り値:0・・・失敗(引数に不備がある) , 1・・・成功 | |
*/ | |
int s_div_n1(const int degree ,const double *beingdiv ,const double *diving ,double *divided){ | |
double fac,cons,prev=0; | |
if(!(degree && beingdiv && diving && divided))return 0; | |
cons = (-1)*(*(diving + 1)); | |
fac = *diving; | |
cout << "\n-----途中計算リスト----------" << endl; | |
for(int i=0;i<=degree;i++){ | |
if(!i)*(divided)=*(beingdiv) / fac; | |
else *(divided+i)=(*(beingdiv+i) + (prev *cons)) / fac; | |
prev = *(divided+i); | |
cout << "(" << i+1 << ")・・・" << *(divided+i) <<endl; | |
} | |
cout << "--------あまりの調整---------" <<endl; | |
*(divided +degree)*=fac; | |
cout << "(" << degree+1 << ")・・・" << *(divided +degree) <<endl ; | |
cout << "-----------------------------" <<endl; | |
return 1; | |
} | |
void FactorForInput(const int deg ,const char moji ,const double *val){ | |
cout << "\n割られる文字式の係数の値を次の指示に従って入力してください。" <<endl; | |
for(int i=0;i<=deg;i++){ | |
if(i==deg){ | |
cout << "定数項の値を入力してください。:"; | |
}else{ | |
cout << moji ; | |
if((deg - i) > 1)cout << "^" << deg - i; | |
cout<< "の係数を入力してください。:"; | |
} | |
scanf("%lf",val + i); | |
} | |
return; | |
} | |
void Print_ResultData(const int deg,const char moji,const double *ans){ | |
cout << "\n計算結果は次の通りです。" <<endl; | |
for(int i=0;i<=deg;i++){ | |
if(*(ans + i)){ | |
if(i==deg){ | |
cout <<" あまり " <<*(ans+i); | |
}else{ | |
if(SignToPlus (*(ans+i))==1){ | |
if(!i && (deg==1))cout << *(ans); | |
else cout << ((*(ans+i)<0)?"-":((i)?"+":"")); | |
}else{ | |
if(!i)cout << *ans; | |
else cout << ((*(ans+i)>0)?"+":"") << (*(ans+i)); | |
} | |
if(i<(deg-1))cout <<moji; | |
if((deg - 1 -i) > 1)cout << "^" << deg - 1 - i; | |
} | |
} | |
} | |
return; | |
} | |
bool isAlphaString(const char *String,const unsigned int len=0){ | |
char moji; | |
bool data[3]; | |
int count; | |
if(!String)return false; | |
count = len; | |
//あえてstrlenを使わずに文字数(バイト単位)を確認 | |
if(!count){ | |
while(1){ | |
char mojicode = *(String + count); | |
if(!mojicode)break; | |
count++; | |
} | |
} | |
//文字チェック | |
for(int i=0;i<count;i++){ | |
moji = *(String + i); | |
data[0] = (moji>='A')&&(moji<='Z'); | |
data[1] = (moji>='a')&&(moji<='z'); | |
data[2] = data [0] || data[1]; | |
if(!data[2])return false; | |
} | |
return true; | |
} | |
//動作テスト | |
int main(void){ | |
int deg; | |
double *ans , *being , diving[2]; | |
char moji; | |
//文字式で使う文字をユーザに入力してもらう(ex:2xのxにあたるところ | |
cout << "文字式で使う1文字を入力してください:"; | |
scanf("%c",&moji); | |
//入力された文字がアルファベットでなければxとする。 | |
if(!isAlphaString(&moji,1)){ | |
moji = 'x'; | |
cout << "無効な文字が入力されましたので文字式に使う1文字はxとしました。" << endl; | |
} | |
//割られる文字式の次数をユーザに入力してもらう | |
cout << "割られる文字式の次数を入力してください:"; | |
scanf("%d",°); | |
if(!deg)return 0; //0が入力されたら終了 | |
else if(deg<0)deg*=-1; //マイナス値だったら-1倍して符号を+にする | |
//メモリ確保 | |
being = new double[deg+1]; | |
ans = new double[deg+1]; | |
//割られる文字式の係数の値を入力してもらう | |
FactorForInput(deg,moji,being); | |
//割る文字式の係数の値を入力してもらう | |
cout << "\n割る文字式の係数の値を次の指示に従って入力してください。" << endl; | |
cout << moji << "の係数を入力してください。:"; | |
scanf("%lf",diving); | |
cout << "定数項の値を入力してください。:"; | |
scanf("%lf",diving + 1); | |
//計算する | |
s_div_n1(deg,being,diving,ans); | |
//計算結果を出力する | |
Print_ResultData(deg,moji,ans); | |
//メモリ解放 | |
delete []being; | |
delete []ans; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment