Skip to content

Instantly share code, notes, and snippets.

@arosh
Created June 24, 2011 13:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arosh/1044779 to your computer and use it in GitHub Desktop.
Save arosh/1044779 to your computer and use it in GitHub Desktop.
kuro_rekishi
黒歴史レベルのコードを偶然見つけてしまったのでうp
・ポインタ知らない
・文字列使えない
・変数名ひどい
というとんでもないコード。
正直見直していて欝になるorz
#include<stdio.h>
#include<stdbool.h>
//(x-3)^2等の形の場合
//b==0の場合
//D>0(実数)で根号の中が1の時
int a,b,c;
bool imaginary = false ,DZero = false ,BZero = false;
//int hugouAlpha=-1 ,hugouBeta=-1;
struct kai {
int bunbo ,bunshi ,soto ,naka ,hugou1 ,hugou2;
};
struct kai alpha ,beta;
void testing() {
printf("%d" ,alpha.bunshi);
if(alpha.hugou2<0) printf("-");
else printf("+");
printf("%d√ %d" ,alpha.soto ,alpha.naka);
if(imaginary==true) printf("i");
printf("\n");
if(alpha.hugou1>0) printf("+");
else printf("- ");
printf("-------\n");
printf(" %d\n" ,alpha.bunbo);
printf("%d" ,beta.bunshi);
if(beta.hugou2<0) printf("-");
else printf("+");
printf("%d√ %d" ,beta.soto ,beta.naka);
if(imaginary==true) printf("i");
printf("\n");
if(beta.hugou1>0) printf("+");
else printf("- ");
printf("-------\n");
printf(" %d\n\n" ,beta.bunbo);
}
int kouyakusu(int x ,int y ,int z) {
//3つの数の最大公約数を返します
int i=1,w=2;
if(x==0||y==0||z==0) return 0;
if(x<0&&y<0&&z<0) goto minus;
if(x<0) x*=-1;
if(y<0) y*=-1;
if(z<0) z*=-1;
for(w; w<=x||w<=y||w<=z;w++) {
if(x%w==0 && y%w==0 && z%w==0) i = w;
}
return i;
minus:
if(x<0) x*=-1;
if(y<0) y*=-1;
if(z<0) z*=-1;
for(w; w<=x||w<=y||w<=z;w++) {
if(x%w==0 && y%w==0 && z%w==0) i = w;
}
return -i;
}
int root(int D) {
//根号の内側にくる値を引数にすると、根号の外側にくる値を返す 例:引数8 => 返却値2
int w ,soto=0;
if(D<0) D*=-1;
if(D==0) return 0;
if(D==1) return 1;
for(w=1; w<=D/2; w++) {
if(D%(w*w)==0)
soto = w;
}
return soto;
}
int heihoukon(int D) {
//平方根の内側にくる値を引数にすると、
//グローバル変数imaginary,alpha.soto,alpha.naka,beta.soto,beta.nakaに
//整形して代入
//D==0のときはエラーとして1を返す
if(D==0) return 1;
if(D<0) {
imaginary = true;
D*=-1;
}
alpha.soto = beta.soto = root(D);
alpha.naka = beta.naka = D/(root(D)*root(D));
return 0;
}
int ketasuu(int x) {
//引数の桁数を返します
int i=1,w;
for(w=10;w<=x;w*=10) {
if(x/w>=1) i+=1;
}
return i;
}
void printAndScan() {
//a,b,cの値を入れるだけ
printf("ax^2 + bx + c\n");
printf("a = ");
scanf("%d" ,&a);
printf("b = ");
scanf("%d" ,&b);
printf("c = ");
scanf("%d" ,&c);
}
void valueOfA(int a) {
//a=1の時は表示せず、a=-1の時は"-"だけ表示し、ほかの数の場合はそのまま表示
if(a==1) printf("");
else if(a==-1) printf("-");
else printf("%d" ,a);
}
void spaceValueOfA(int a) {
//a=1以外の時は、aの桁数だけスペースを入れる(a<0の時は"-"の分1文字追加
if((a!=1)&&(a>0)) {
int i=ketasuu(a),j;
for(j=1;j<=i;j++) printf(" ");
}
else if((a!=1)&&(a<0)) {
int i=ketasuu(a)+1,j;
for(j=1;j<=i;j++) printf(" ");
}
}
void hugouKeisan() {
if(alpha.bunbo<0) {
//分母が負の数の場合は、
alpha.hugou1*=-1;
beta.hugou1*=-1;
alpha.bunbo*=-1;
beta.bunbo*=-1;
}
if(beta.bunshi<0&&beta.hugou1<0&&beta.hugou2<0) {
beta.bunshi*=-1;
beta.hugou1*=-1;
beta.hugou2*=-1;
}
if(alpha.hugou1>0&&alpha.bunshi<0&&alpha.hugou2<0) {
alpha.hugou1*=-1;
alpha.bunshi*=-1;
alpha.hugou2*=-1;
}
if(beta.hugou1>0&&beta.bunshi<0&&beta.hugou2<0) {
beta.hugou1*=-1;
beta.bunshi*=-1;
beta.hugou2*=-1;
}
if(alpha.bunbo==1&&alpha.hugou1<1) {
alpha.hugou1*=-1;
alpha.bunshi*=-1;
alpha.hugou2*=-1;
}
if(beta.bunbo==1&&beta.hugou1<1) {
beta.hugou1*=-1;
beta.bunshi*=-1;
beta.hugou2*=-1;
}
if(alpha.bunbo!=1&&alpha.hugou1<0&&alpha.bunshi<0){
alpha.hugou1*=-1;
alpha.bunshi*=-1;
alpha.hugou2*=-1;
}
if(beta.bunbo!=1&&beta.hugou1<0&&beta.bunshi<0){
beta.hugou1*=-1;
beta.bunshi*=-1;
beta.hugou2*=-1;
}
}
void hugou(int i) {
switch(i) {
case 0:
if(alpha.hugou1>0) printf("+");
else printf("- ");
break;
case 1:
if(beta.hugou1>0) printf("+");
else printf("- ");
break;
}
}
void printImaginary() {
//imaginary==trueのとき"iを表示"
if(imaginary==true) printf("i");
}
void printBunshiAlpha() {
if(BZero==false) printf("%d" ,alpha.bunshi);
if(DZero==false) {
if(alpha.hugou2==-1) printf("-");
else printf("+");
if(alpha.soto!=1) printf("%d" ,alpha.soto);
if(alpha.naka!=1) printf("√ %d" ,alpha.naka);
printImaginary();
}
}
void printBunshiBeta() {
if(BZero==false) printf("%d" ,beta.bunshi);
if(DZero==false) {
if(beta.hugou2==-1) printf("-");
else printf("+");
if(beta.soto!=1) printf("%d" ,beta.soto);
if(beta.naka!=1) printf("√ %d" ,beta.naka);
printImaginary();
}
}
/*
1±1√ 1 1±1√ 1
= a( x - ----------- )( x - ----------- )
1 1
*/
void mode3() {
printf(" = ");
valueOfA(a);
printf("( x");
if(alpha.hugou1>0) {
if(alpha.bunshi>0) printf("+");
printBunshiAlpha();
}
else {
printf("-");
alpha.bunshi*=-1;
alpha.hugou2+=-1;
printBunshiAlpha();
}
printf(" )( x");
if(beta.hugou1>0) {
if(beta.bunshi>0) printf("+");
printBunshiBeta();
}
else {
printf("-");
beta.bunshi*=-1;
beta.hugou2+=-1;
printBunshiBeta();
}
printf(" )\n");
}
void printing() {
int mode=0;
if(alpha.bunbo==1) mode+=1;
if(beta.bunbo==1) mode+=2;
/*
0.どちらも分数の場合
1.α側が分数の場合
2.β側が分数の場合
3.どちらも分数でない場合
*/
// printf("mode=%d\n" ,mode);
switch(mode) {
case 0:
spaceValueOfA(a);
printf(" ");
printBunshiAlpha();
printf(" ");
// BunshiKuuhaku();
printBunshiBeta();
printf("\n");
printf(" = ");
valueOfA(a);
printf ("( x ");
if(alpha.hugou1==-1) printf("-");
else printf("+");
printf(" ----------- )( x ");
if(beta.hugou1==-1) printf("-");
else printf("+");
puts(" ----------- )");
spaceValueOfA(a);
printf(" %d %d\n" ,alpha.bunbo ,beta.bunbo);
break;
case 1:
spaceValueOfA(a);
printBunshiAlpha();
printf("\n");
printf(" = ");
valueOfA(a);
printf("( x ");
if(alpha.hugou1==-1) printf("-");
else printf("+");
printf(" ----------- )( x ");
if(beta.hugou1==-1) printf("-");
else printf("+ ");//-(1-3)のときはどうする?
printBunshiBeta();
printf("\n");
spaceValueOfA(a);
printf(" %d\n" ,alpha.bunbo);
break;
case 2:
spaceValueOfA(a);
printf(" ");
printBunshiBeta();
printf("\n");
printf(" = ");
valueOfA(a);
printf("( x ");
if(alpha.hugou1==-1) printf("-");
else printf("+");//-(1-3)のときはどうする?
printBunshiAlpha();
printf(" )( x ");
if(beta.hugou1==-1) printf("-");
else printf("+ ");
printf(" ----------- )");
printf("\n");
spaceValueOfA(a);
printf(" %d\n" ,beta.bunbo);
break;
case 3:
mode3();
break;
default: puts("another mode!!");
}
}
void yakubun() {
int i = kouyakusu(alpha.bunbo ,alpha.bunshi ,alpha.soto);
if(alpha.bunshi==0) alpha.bunshi = alpha.bunbo;
if(DZero==true) alpha.soto = alpha.bunbo;
// printf("%d\n" ,i);
alpha.bunbo/=i;
alpha.bunshi/=i;
alpha.soto/=i;
beta.bunbo/=i;
beta.bunshi/=i;
beta.soto/=i;
}
void deta() {
int D=(b*b)-(a*c*4);
alpha.bunbo = beta.bunbo = a*2;
alpha.bunshi = beta.bunshi = b*-1;
alpha.hugou1 = beta.hugou1 = beta.hugou2 = -1;
alpha.hugou2 = 1;
if(b==0) BZero = true;
if(heihoukon(D)==1) DZero = true;
testing();
yakubun();
hugouKeisan();
testing();
}
int main(void) {
printAndScan();
deta();
printing();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment