栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

最详细的一元稀疏多项式计算器C语言实现

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

最详细的一元稀疏多项式计算器C语言实现

目录

1.程序设计思路

1.框架构想

2.数据结构的选择

2.相应功能的函数实现及程序变量解释

1.处理读入的字符串并将其转换成链表存储多项式信息

2.多项式加减法的实现

 3.多项式升序和降序结果显示

4.计算器界面的实现

5.所用库和宏定义

3.源程序代码


1.程序设计思路

1.框架构想

        本程序的设计目的旨在使用C语言设计一个一元稀疏多项式计算器,其具有计算器可视化界面,多项式的加减法计算功能和自然语言的输入输出方式的功能,计算器根据用户输入的多项式和指定的功能来进行计算,最后输出计算结果。

2.数据结构的选择

        由于多项式是稀疏多项式,故不应该使用数组的形式存储多项式,而应该使用链表存储多项式。

typedef struct 
{
    double coef;                //多项式系数
    int index;                  //多项式指数
}term;

typedef struct poly             //多项式链表存储结构,稀疏多项式所以用链表存储结构
{
    term data;
    poly* next;
}poly_list;

2.相应功能的函数实现及程序变量解释

1.处理读入的字符串并将其转换成链表存储多项式信息

        为了更加贴合日常使用的要求,该程序实现了自然语言读入的方式来进行多项式的输入。为了实现该功能需要将多项式中每项的系数和指数提取出来存入到数组里面,为此设计了Get_coef函数来从字符串当前位置获取一个数字,Getnum函数来处理整个字符串实现多项式信息的转换。

double Get_coef(char *str)  					//在输入的字符串中提取系数
{
	double s = 0.0;
	double d = 10.0;							//处理大于10的系数
	bool flag = false;							//flag为false即为正数
	while (*str == ' ')  
		str++;
	if (*str == '-')							//记录数字正负  
	{
		flag = true;  
		str++;					
		if (*str == 'x' || *str == 'X') 		//如果系数为-1则直接返回
			return -1.0;
	}
	else if ((*str == '+'&& (*(str + 1) == 'x' || *(str + 1) == 'X')) || (*str == 'x')) 		//系数为1则直接返回
		return 1.0;		
	if (*str == '+' && (*(str + 1) >= '0' && *(str + 1) <= '9'))
		str++;
	if (!(*str >= '0' && *str <= '9'))     		//如果一开始非数字则退出,返回0.0 
		return s;     
	while (*str >= '0' && *str <= '9' && *str != '.')				//计算小数点前整数部分  
	{
		s = s * 10.0 + *str - '0';
		str++;
	}           
	if (*str == '.')                            //以后为小数部分
        str++;         			      
	while (*str >= '0'&&*str <= '9')  			//计算小数部分  
	{
		s = s + (*str - '0') / d;
		d *= 10.0;
		str++;
	}
	return s * (flag ? -1.0 : 1.0);             //根据flag值返回相应的正负数
}


void GetNums(int &cnt, double* coefs, int* indexs) 					
{
	int i = 0;
	cnt = 0;
	double coef;
	int expn = 0;
	char str[80];
	gets (str);														//存放输入的字符串
	while (*(str + i))
	{
		coef = Get_coef(str + i);
		if (*(str + i) != 'x' && *(str + i ) != 'X') 
			i++;
		while ((*(str + i) >= '0' && *(str + i) <= '9') || (*(str + i) == '.') || (*(str + i) == '-'))  	
			i++;
		if (*(str + i) == '+' || *(str + i) == '')      									//如果没有X则直接将指数赋值为零      
			expn = 0;
		else if (*(str + i) == 'x' || *(str + i) == 'X') 
		{
			i++;
			if (*(str + i) == '+' || *(str + i) == '' || *(str + i) == '-') 				//只有x没有数字说明指数为1			
				expn = 1;							
			else if (*(str + i) == '^')
			{
				i++;
				expn = (int)Get_coef(str + i);
				if (*(str + i) == '-')
					i++;
				while ((*(str + i) >= '0'&&*(str + i) <= '9') || (*(str + i) == '.'))
					i++;
			}
		}
		coefs[cnt] = coef;
		indexs[cnt] = expn;
		cnt++;
	}
}

        在执行完Getnum函数后,从字符串中获得的多项式信息已经存储在coefs和indexs两个数组中,再使用create_poly函数实现将数组信息转化成链表。

poly_list* Create_poly()                             
{
	double coefs[80];  								//存系数 
	int indexs[80];     							//存指数 
	int count;
	GetNums(count, coefs, indexs);
    poly_list* head;
    head = (poly_list*)malloc(sizeof(poly_list));
    head->next = NULL;
    poly_list* term_list;
    for (int i = 0; i < count; i++)
    {
    	term_list = (poly_list*)malloc(sizeof(poly_list));
    	term_list->next = NULL;
    	term_list->data.coef = coefs[i];
    	term_list->data.index = indexs[i];
    	Insert_list (head, term_list);
	}
    return head;
}

        其中Insert_list函数实现将当前节点的指数按降序存储到链表中去。

int cmp (term a, term b)                    //比较两个多项式元素的指数大小
{
    return  a.index - b.index; 
}

void Delete_list (poly_list* head, poly_list* q)                 //删除链表指定位置节点
{
    poly_list* p;
    p = head;
    while (p->next != q)
    	p = p->next;
    p->next = q->next;
}


void Insert_list (poly_list* head, poly_list* q) 
{        
    poly_list* p = head;
    poly_list* prior = head;
    p = p->next;
    while (p != NULL)
    {
        if (cmp (p->data, q->data) == 0)                 //若链表中存在的指数相同的项,则直接相加
        {
            p->data.coef += q->data.coef;
            if (p->data.coef == 0)                       //遇到系数为0的极端情况删除该节点
                Delete_list (head, p);
            return;
        }
        else if (cmp (p->data, q->data) > 0)             //指数小于当前节点则向后移
        {
            p = p->next;
            prior = prior->next;
        }
        else                                            //大于则插入链表
        {
            prior->next = q;
            q->next = p;
            return;
        } 
    }
    if (p == NULL)	                                    //空链表直接插入
    {
    	prior->next = q;
    	q->next = NULL;
    	if (q->data.coef == 0)                          
    		Delete_list (head, q);
	}
}

        至此已经完成了所有链表创建的所有工作,链表信息存储在head头指针中。

2.多项式加减法的实现

        AddDecrease_poly函数用于计算两个多项式p、q多项式的和或差,使用flag来控制计算模式,flag为1进行加法,flag为2进行减法,计算结果存储在result指针中。其具体实现逻辑并不难,在此不多说明。

poly_list* AddorDecrease_poly (poly_list* p, poly_list* q, int flag)                 //多项式加法运算函数
{
    poly_list* result;      //存储结构的链表
    result = (poly_list*)malloc(sizeof(poly_list));
    result->next = NULL;
    poly_list* term_list;
    poly_list* index1 = p->next;                      //两个指针分别指向加数和被加数
    poly_list* index2 = q->next;
    while (index1 != NULL && index2 != NULL)            //若当前两链表节点指数相同则直接相加
    {
          
		term_list = (poly_list*)malloc(sizeof(poly_list));
    	term_list->next = NULL;
        if (cmp (index1->data, index2->data) == 0 && flag == 1)
        {
            term_list->data.coef = index1->data.coef + index2->data.coef;
            term_list->data.index = index1->data.index;
            Insert_list (result,  term_list);
            index1 = index1->next;
            index2 = index2->next;
        }
        else if (cmp (index1->data, index2->data) == 0 && flag == 2)
        {
            term_list->data.coef = index1->data.coef - index2->data.coef;
            term_list->data.index = index1->data.index;
            Insert_list (result,  term_list);
            index1 = index1->next;
            index2 = index2->next;
        }
        else if (cmp(index1->data, index2->data) > 0)               //将当前节点指数大的插入result链表
        {
            term_list->data.coef = index1->data.coef;
            term_list->data.index = index1->data.index;
            Insert_list (result, term_list);
            index1 = index1->next;
        }
        else
        {
            term_list->data.coef = index2->data.coef;
            term_list->data.index = index2->data.index;
            Insert_list (result,  term_list);
            index2 = index2->next;
        }
    }
    
    if (index1 != NULL)             //index1长的情况
        while (index1 != NULL)
        {
        	term_list = (poly_list*)malloc(sizeof(poly_list));
    		term_list->next = NULL;
            term_list->data.coef = index1->data.coef;
            term_list->data.index = index1->data.index;
            Insert_list (result, term_list);
            index1 = index1->next;
        }
    else if (index2 != NULL)            //index2长的情况
        while (index2 != NULL)
        {
        	term_list = (poly_list*)malloc(sizeof(poly_list));
    		term_list->next = NULL;
            term_list->data.coef = index2->data.coef;
            term_list->data.index = index2->data.index;
            Insert_list(result, term_list);
            index2 = index2->next;
        }
    return result;
}

 3.多项式升序和降序结果显示

        Show_poly函数对传入参数head进行多项式输出,值得一提的是需要处理各种特殊的情况比如指数为1时不输出1,若系数为整数的时候不输出浮点数样式(使用Control_float来判断输出情况)

等等,在代码中都已经处理好了,实现方法不难,只是需要处理较多细节,读者请自行研究。

void Show_poly (poly_list* head)                     //将计算结果多项式链表输出
{
    poly_list* p = head;
    p = p->next;
    if (p == NULL)
    	printf ("0");
    
    while (p != NULL)
    {
        if (p == head->next)                         //第一个节点输出和后续节点输出方式不同,第一个节点不含加减号
        {
        	if (p->data.coef == 1 && p->data.index ==0)
        		printf ("%.0f", p->data.coef);          //另外当指数为0时不需要输出x
        	if (p->data.coef != 1)
            {
            	if (Control_float (p->data.coef) == 0)          //判断系数是否为整数,整数则输出整数,浮点数则输出浮点数
                    printf ("%.0f", p->data.coef);
                    else printf ("%.1f", p->data.coef);
            }
            if (p->data.index != 0)
            {
		        printf ("X^");
        	    printf ("%d", p->data.index);
        	}
        }
        else
        {
            if (p->data.coef > 0)           //后续节点需要输出加减号
                printf ("+");
            else if (p->data.coef < 0)
                printf ("-");
            if (fabs (p->data.coef) != 1)
            {
            	if (Control_float (p->data.coef) == 0)
                    printf ("%.0f", fabs(p->data.coef));
                else printf ("%.1f", fabs(p->data.coef));
            }
            if (p->data.index != 0)
            {
		        printf ("X^");
        	    printf ("%d", p->data.index);
        	}
        }
        p = p->next;
    }
    printf ("nnn");
}
int Control_float (double a)                      //判断该浮点数是否是整数
{
    if (fabs (a - (int)a) < 0.01)
        return 0;
    else return 1;
}

        另外如果读者需要按照升序的形式显示多项式则可以使用reverse函数来进行链表逆转输出

void reverse (poly_list* head)
{
    float a[100];               //使用堆栈操作进行逆序输出
    int b[100];
    int i = 0;
    poly_list* p = head->next;
    while (p != NULL)           //压栈
    {
        a[i] = p->data.coef;
        b[i] = p->data.index;
        i++;
        p = p->next;
    }
        
    printf ("%d", i);
    printf (" ");
    for (int j = i - 1; j >= 0; j--)
    {
        if (j == i - 1)                         //第一个节点输出和后续节点输出方式不同,第一个节点不含加减号
        {
        	if (a[j] == 1 && b[j] ==0)
        	{
                printf ("%.0f", a[j]);
                printf (",");
                printf ("%d", b[j]);
            }
            else if (a[j] == 1)
                printf ("1");
        	if (a[j] != 1)
            {
            	if (Control_float (a[j]) == 0)
                    printf ("%.0f", a[j]);
                    else printf ("%.1f", a[j]);
            }
            if (b[j] != 0)
            {
                printf (",");
        	    printf ("%d", b[j]);
                printf (" ");
        	}
        }
        else
        {
             if (a[j] < 0)
                printf ("-");
            if (fabs (a[j]) != 1)
            {
            	if (Control_float (a[j]) == 0)
                    printf ("%.0f", fabs(a[j]));
                else printf ("%.1f", fabs(a[j]));
            }
            else if (a[j] == 1)
                printf ("1");
            if (b[j] != 0)
            {
                printf (",");
        	    printf ("%d", b[j]);
                printf (" ");
        	}
        }
    }
}

4.计算器界面的实现

        由于c语言画图比较麻烦而且很多编译器不兼容graphic库,于是本程序采取光标移动的方式来实现计算器界面的实现。主要用到了windows.h库,首选编写Goto_xy函数来控制光标的使用,在编写menu函数来进行界面的输出。

void Goto_xy (int x,int y)            //设置光标的位置
{
    COORD c;
    c.X=x-1;
    c.Y=y-1;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}
void menu()                 
{   
    Goto_xy (35, 2);   printf ("**************************************************n");
	Goto_xy (35, 3);   printf ("*            polynomials caculators              *n");
	Goto_xy (35, 4);   printf ("**************************************************");
	Goto_xy (35, 5);   printf ("*     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~n"); Goto_xy(84, 5); printf("*n");
	Goto_xy (35, 6);   printf ("*     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~n"); Goto_xy(84, 6); printf("*n");
	Goto_xy (35, 7);   printf ("*     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~n"); Goto_xy(84, 7); printf("*n");
	Goto_xy (35, 8);   printf ("**************************************************");
	Goto_xy (35, 9);   printf ("*PolyA :");              Goto_xy(84, 9); printf("*");
	Goto_xy (35, 10);  printf ("**************************************************");
	Goto_xy (35, 11);  printf ("*PolyB :");             Goto_xy(84, 11); printf("*");
	Goto_xy (35, 12);  printf ("**************************************************");
	Goto_xy (35, 13);  printf ("*PolyResult :");         Goto_xy(84, 13); printf("*");
    Goto_xy (35, 14);  printf ("*             ");         Goto_xy(84, 14); printf("*");
	Goto_xy (35, 15);  printf ("**************************************************");
	Goto_xy (35, 16);  printf ("*   Put 1 to add      |    put 2 to decrease     *n");
	Goto_xy (35, 17);  printf ("**************************************************");
	Goto_xy (35, 18);  printf ("*   Put 3 to exit     |    put Enter to perform  *n");
	Goto_xy (35, 19);  printf ("**************************************************");
	Goto_xy (35, 20);  printf ("*                                                *n");
	Goto_xy (35, 21);  printf ("**************************************************");
	Goto_xy (35, 22);  printf ("     	*{polymials caculator}*  by htc            n");
}

5.所用库和宏定义
#include 
#include 
#include 
#include 
#define MaxLen 1000             //最大长度限制

        至此所有的功能模块都已经单独介绍完毕。

3.源程序代码

目录

1.程序设计思路

1.框架构想

2.数据结构的选择

2.相应功能的函数实现及程序变量解释

1.处理读入的字符串并将其转换成链表存储多项式信息

2.多项式加减法的实现

 3.多项式升序和降序结果显示

4.计算器界面的实现

5.所用库和宏定义

3.源程序代码


#include 
#include 
#include 
#include 
#define MaxLen 1000             //最大长度限制

typedef struct 
{
    double coef;                //多项式系数
    int index;                  //多项式指数
}term;

typedef struct poly             //多项式链表存储结构,稀疏多项式所以用链表存储结构
{
    term data;
    poly* next;
}poly_list;

int cmp (term a, term b)                    //比较两个多项式元素的指数大小
{
    return  a.index - b.index; 
}

void Delete_list (poly_list* head, poly_list* q)                 //删除链表指定位置节点
{
    poly_list* p;
    p = head;
    while (p->next != q)
    	p = p->next;
    p->next = q->next;
}


void Insert_list (poly_list* head, poly_list* q) 
{        
    poly_list* p = head;
    poly_list* prior = head;
    p = p->next;
    while (p != NULL)
    {
        if (cmp (p->data, q->data) == 0)                 //若链表中存在的指数相同的项,则直接相加
        {
            p->data.coef += q->data.coef;
            if (p->data.coef == 0)                       //遇到系数为0的极端情况删除该节点
                Delete_list (head, p);
            return;
        }
        else if (cmp (p->data, q->data) > 0)             //指数小于当前节点则向后移
        {
            p = p->next;
            prior = prior->next;
        }
        else                                            //大于则插入链表
        {
            prior->next = q;
            q->next = p;
            return;
        } 
    }
    if (p == NULL)	                                    //空链表直接插入
    {
    	prior->next = q;
    	q->next = NULL;
    	if (q->data.coef == 0)                          
    		Delete_list (head, q);
	}
}

int Control_float (double a)                      //判断该浮点数是否是整数
{
    if (fabs (a - (int)a) < 0.01)
        return 0;
    else return 1;
}

double Get_coef(char *str)  					//在输入的字符串中提取系数
{
	double s = 0.0;
	double d = 10.0;							//处理大于10的系数
	bool flag = false;							//flag为false即为正数
	while (*str == ' ')  
		str++;
	if (*str == '-')							//记录数字正负  
	{
		flag = true;  
		str++;					
		if (*str == 'x' || *str == 'X') 		//如果系数为-1则直接返回
			return -1.0;
	}
	else if ((*str == '+'&& (*(str + 1) == 'x' || *(str + 1) == 'X')) || (*str == 'x')) 		//系数为1则直接返回
		return 1.0;		
	if (*str == '+' && (*(str + 1) >= '0' && *(str + 1) <= '9'))
		str++;
	if (!(*str >= '0' && *str <= '9'))     		//如果一开始非数字则退出,返回0.0 
		return s;     
	while (*str >= '0' && *str <= '9' && *str != '.')				//计算小数点前整数部分  
	{
		s = s * 10.0 + *str - '0';
		str++;
	}           
	if (*str == '.')                            //以后为小数部分
        str++;         			      
	while (*str >= '0'&&*str <= '9')  			//计算小数部分  
	{
		s = s + (*str - '0') / d;
		d *= 10.0;
		str++;
	}
	return s * (flag ? -1.0 : 1.0);             //根据flag值返回相应的正负数
}


void GetNums(int &cnt, double* coefs, int* indexs) 					
{
	int i = 0;
	cnt = 0;
	double coef;
	int expn = 0;
	char str[80];
	gets (str);														//存放输入的字符串
	while (*(str + i))
	{
		coef = Get_coef(str + i);
		if (*(str + i) != 'x' && *(str + i ) != 'X') 
			i++;
		while ((*(str + i) >= '0' && *(str + i) <= '9') || (*(str + i) == '.') || (*(str + i) == '-'))  	
			i++;
		if (*(str + i) == '+' || *(str + i) == '')      									//如果没有X则直接将指数赋值为零      
			expn = 0;
		else if (*(str + i) == 'x' || *(str + i) == 'X') 
		{
			i++;
			if (*(str + i) == '+' || *(str + i) == '' || *(str + i) == '-') 				//只有x没有数字说明指数为1			
				expn = 1;							
			else if (*(str + i) == '^')
			{
				i++;
				expn = (int)Get_coef(str + i);
				if (*(str + i) == '-')
					i++;
				while ((*(str + i) >= '0'&&*(str + i) <= '9') || (*(str + i) == '.'))
					i++;
			}
		}
		coefs[cnt] = coef;
		indexs[cnt] = expn;
		cnt++;
	}
}


poly_list* Create_poly()                             
{
	double coefs[80];  								//存系数 
	int indexs[80];     							//存指数 
	int count;
	GetNums(count, coefs, indexs);
    poly_list* head;
    head = (poly_list*)malloc(sizeof(poly_list));
    head->next = NULL;
    poly_list* term_list;
    for (int i = 0; i < count; i++)
    {
    	term_list = (poly_list*)malloc(sizeof(poly_list));
    	term_list->next = NULL;
    	term_list->data.coef = coefs[i];
    	term_list->data.index = indexs[i];
    	Insert_list (head, term_list);
	}
    return head;
}

void Show_poly (poly_list* head)                     //将计算结果多项式链表输出
{
    poly_list* p = head;
    p = p->next;
    if (p == NULL)
    	printf ("0");
    
    while (p != NULL)
    {
        if (p == head->next)                         //第一个节点输出和后续节点输出方式不同,第一个节点不含加减号
        {
        	if (p->data.coef == 1 && p->data.index ==0)
        		printf ("%.0f", p->data.coef);          //另外当指数为0时不需要输出x
        	if (p->data.coef != 1)
            {
            	if (Control_float (p->data.coef) == 0)          //判断系数是否为整数,整数则输出整数,浮点数则输出浮点数
                    printf ("%.0f", p->data.coef);
                    else printf ("%.1f", p->data.coef);
            }
            if (p->data.index != 0)
            {
		        printf ("X^");
        	    printf ("%d", p->data.index);
        	}
        }
        else
        {
            if (p->data.coef > 0)           //后续节点需要输出加减号
                printf ("+");
            else if (p->data.coef < 0)
                printf ("-");
            if (fabs (p->data.coef) != 1)
            {
            	if (Control_float (p->data.coef) == 0)
                    printf ("%.0f", fabs(p->data.coef));
                else printf ("%.1f", fabs(p->data.coef));
            }
            if (p->data.index != 0)
            {
		        printf ("X^");
        	    printf ("%d", p->data.index);
        	}
        }
        p = p->next;
    }
    printf ("nnn");
}


poly_list* AddorDecrease_poly (poly_list* p, poly_list* q, int flag)                 //多项式加法运算函数
{
    poly_list* result;      //存储结构的链表
    result = (poly_list*)malloc(sizeof(poly_list));
    result->next = NULL;
    poly_list* term_list;
    poly_list* index1 = p->next;                      //两个指针分别指向加数和被加数
    poly_list* index2 = q->next;
    while (index1 != NULL && index2 != NULL)            //若当前两链表节点指数相同则直接相加
    {
          
		term_list = (poly_list*)malloc(sizeof(poly_list));
    	term_list->next = NULL;
        if (cmp (index1->data, index2->data) == 0 && flag == 1)
        {
            term_list->data.coef = index1->data.coef + index2->data.coef;
            term_list->data.index = index1->data.index;
            Insert_list (result,  term_list);
            index1 = index1->next;
            index2 = index2->next;
        }
        else if (cmp (index1->data, index2->data) == 0 && flag == 2)
        {
            term_list->data.coef = index1->data.coef - index2->data.coef;
            term_list->data.index = index1->data.index;
            Insert_list (result,  term_list);
            index1 = index1->next;
            index2 = index2->next;
        }
        else if (cmp(index1->data, index2->data) > 0)               //将当前节点指数大的插入result链表
        {
            term_list->data.coef = index1->data.coef;
            term_list->data.index = index1->data.index;
            Insert_list (result, term_list);
            index1 = index1->next;
        }
        else
        {
            term_list->data.coef = index2->data.coef;
            term_list->data.index = index2->data.index;
            Insert_list (result,  term_list);
            index2 = index2->next;
        }
    }
    
    if (index1 != NULL)             //index1长的情况
        while (index1 != NULL)
        {
        	term_list = (poly_list*)malloc(sizeof(poly_list));
    		term_list->next = NULL;
            term_list->data.coef = index1->data.coef;
            term_list->data.index = index1->data.index;
            Insert_list (result, term_list);
            index1 = index1->next;
        }
    else if (index2 != NULL)            //index2长的情况
        while (index2 != NULL)
        {
        	term_list = (poly_list*)malloc(sizeof(poly_list));
    		term_list->next = NULL;
            term_list->data.coef = index2->data.coef;
            term_list->data.index = index2->data.index;
            Insert_list(result, term_list);
            index2 = index2->next;
        }
    return result;
}

void Goto_xy (int x,int y)            //设置光标的位置
{
    COORD c;
    c.X=x-1;
    c.Y=y-1;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}


void reverse (poly_list* head)
{
    float a[100];               //使用堆栈操作进行逆序输出
    int b[100];
    int i = 0;
    poly_list* p = head->next;
    while (p != NULL)           //压栈
    {
        a[i] = p->data.coef;
        b[i] = p->data.index;
        i++;
        p = p->next;
    }
        
    printf ("%d", i);
    printf (" ");
    for (int j = i - 1; j >= 0; j--)
    {
        if (j == i - 1)                         //第一个节点输出和后续节点输出方式不同,第一个节点不含加减号
        {
        	if (a[j] == 1 && b[j] ==0)
        	{
                printf ("%.0f", a[j]);
                printf (",");
                printf ("%d", b[j]);
            }
            else if (a[j] == 1)
                printf ("1");
        	if (a[j] != 1)
            {
            	if (Control_float (a[j]) == 0)
                    printf ("%.0f", a[j]);
                    else printf ("%.1f", a[j]);
            }
            if (b[j] != 0)
            {
                printf (",");
        	    printf ("%d", b[j]);
                printf (" ");
        	}
        }
        else
        {
             if (a[j] < 0)
                printf ("-");
            if (fabs (a[j]) != 1)
            {
            	if (Control_float (a[j]) == 0)
                    printf ("%.0f", fabs(a[j]));
                else printf ("%.1f", fabs(a[j]));
            }
            else if (a[j] == 1)
                printf ("1");
            if (b[j] != 0)
            {
                printf (",");
        	    printf ("%d", b[j]);
                printf (" ");
        	}
        }
    }
}


void menu()                 
{   
    Goto_xy (35, 2);   printf ("**************************************************n");
	Goto_xy (35, 3);   printf ("*            polynomials caculators              *n");
	Goto_xy (35, 4);   printf ("**************************************************");
	Goto_xy (35, 5);   printf ("*     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~n"); Goto_xy(84, 5); printf("*n");
	Goto_xy (35, 6);   printf ("*     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~n"); Goto_xy(84, 6); printf("*n");
	Goto_xy (35, 7);   printf ("*     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~n"); Goto_xy(84, 7); printf("*n");
	Goto_xy (35, 8);   printf ("**************************************************");
	Goto_xy (35, 9);   printf ("*PolyA :");              Goto_xy(84, 9); printf("*");
	Goto_xy (35, 10);  printf ("**************************************************");
	Goto_xy (35, 11);  printf ("*PolyB :");             Goto_xy(84, 11); printf("*");
	Goto_xy (35, 12);  printf ("**************************************************");
	Goto_xy (35, 13);  printf ("*PolyResult :");         Goto_xy(84, 13); printf("*");
    Goto_xy (35, 14);  printf ("*             ");         Goto_xy(84, 14); printf("*");
	Goto_xy (35, 15);  printf ("**************************************************");
	Goto_xy (35, 16);  printf ("*   Put 1 to add      |    put 2 to decrease     *n");
	Goto_xy (35, 17);  printf ("**************************************************");
	Goto_xy (35, 18);  printf ("*   Put 3 to exit     |    put Enter to perform  *n");
	Goto_xy (35, 19);  printf ("**************************************************");
	Goto_xy (35, 20);  printf ("*                                                *n");
	Goto_xy (35, 21);  printf ("**************************************************");
	Goto_xy (35, 22);  printf ("     	*{polymials caculator}*  by htc            n");
}


int main()
{
    menu ();
    COORD A, B, Result1, Result2;                     //记录对应计算器界面多项式输出处光标的位置
    A.X = 44, A.Y =9;
    B.X = 44, B.Y = 11;
    Result1.X = 49, Result1.Y = 13;
    Result2.X = 49, Result2.Y = 14;
    int flag = 0;
    while (scanf ("%d", &flag) && (flag != 3))          
    {
        system ("cls");                     //清屏
        menu ();
        getchar ();
        Goto_xy (A.X, A.Y);
        poly_list* p1, *p2, *result;            //两个输入的多项式及存储计算结果的多项式链表
        p1 = Create_poly (); 
        Goto_xy (B.X, B.Y);
        p2 = Create_poly ();
        result = AddorDecrease_poly (p1, p2, flag);
        Goto_xy (Result1.X, Result1.Y);         //将光标转移到输出位置
        Show_poly (result);
        Goto_xy (Result2.X, Result2.Y);
        reverse (result);
        menu ();
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/457332.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号