2007年5月19日 星期六

C的學習日誌-簡易的微分積分計算

Posted by Picasa學校資料結構的作業
跟同學討論之後想到的程式碼下:
#include
#include
#include
int tmp=0;
struct node {
int num;
int degree;
struct node *next;
};
struct node *ptr,*first,*newnode;
void input()
{
do
{
if (tmp == 0)
{
first=(struct node *)malloc(sizeof(first));
scanf("%dx^%d",&first->num,&first->degree);
ptr=first;
} else {
newnode=(struct node *)malloc(sizeof(newnode));
scanf("%dx^%d",&newnode->num,&newnode->degree);
ptr->next = newnode; ptr=newnode;
}
tmp++;
}while(ptr->num!=0);ptr=first;
}
void add()
{
float tmpa=0, tmpb=0, temp=0;
int a,b,i;
scanf("%d",&a);
scanf("%d",&b);
while (ptr->num != 0)
{
tmpa+=(ptr->num/(ptr->degree+1)*((int)pow(a,ptr->degree+1)));
tmpb+=(ptr->num/(ptr->degree+1)*((int)pow(b,ptr->degree+1)));
ptr=ptr->next;
}
temp=tmpa-tmpb;
printf("%f\n",temp);
printf("\n\nEDIT BY FU");
printf("\nhttp://fufuk-k.blogspot.com/\n") ;
system("PAUSE");
}
void show()
{
system("cls");
while (ptr->num != 0)
{
ptr->num=ptr->num*ptr->degree;
ptr->degree++;
printf("%dx^%d",ptr->num,ptr->degree);
ptr=ptr->next;
if (ptr->num != 0)
printf("+");
}
printf("\n\nEDIT BY FU");
printf("\nhttp://fufuk-k.blogspot.com/\n") ;
system("PAUSE");
}
char choose_menu(void)
{
char option1;
system("cls");
printf("\n***********************************");
printf("\n\t請選擇輸入的為微分或積分:");
printf("\n\t0.離開程式");
printf("\n\t1.選擇微分計算");
printf("\n\t2.選擇積入計算");
printf("\n***********************************\n");
do
{
option1=getch();
}while((option1!='0')&&(option1!='1')&&(option1!='2'));
return option1;
}int main(int argc, char *argv[]){ char op='1'; while(1)
{
do{
op=choose_menu();
}while(op!='1'&&op!='2'&&op!='0');
switch (op) {
case '1':
printf("\n請開始以降冪排列輸入,輸入0代表結束\n");
printf("EXP:3x^5+8x^4+3x^2\n");
printf("3x^5\n");
printf("8x^4\n");
printf("3x^2\n");
printf("0\n");
input();
show();
break;
case '2':
system("cls");
printf("\na \n");
printf(" 多項式 \n");
printf("b \n");
printf("\n請先輸入多項式部份,再輸入0代表多項輸入結束\n");
printf("再輸入a與b\n");
printf("EXP:");
printf("\n4 \n");
printf(" 3x^5+8x^4+3x^2 \n");
printf("1 \n");
printf("3x^5\n");
printf("8x^4\n");
printf("3x^2\n");
printf("0\n");
printf("4\n");
printf("1\n\n");
input();
add();
break;
case '0':
printf("\n\nEDIT BY FU");
printf("\nhttp://fufuk-k.blogspot.com/\n") ;
system("PAUSE");
return;
}
}
}


請網路上的高手們能把程式的BUG留言或來信跟我說

1 則留言:

提到...

哇不懂ㄋㄟ!!
所以才會被當嗎?@@"