当前位置:首页 » 编程软件 » 编程里乘法

编程里乘法

发布时间: 2022-06-14 21:48:32

1. 编程语言能做出来九九乘法表吗

  1. 当然能,以C为例发一个给你看

  2. main()
    {
    int i,j;
    for(i=1;i<=9;i++)
    {
    for(j=1;j<=i;j++)
    {
    printf("%d*%d=%d ",i,j,i*j);
    }
    printf(" ");
    }
    }

2. c++编程 多项式的乘法

#include <iostream>
#include<algorithm>
using namespace std;

class Polynomial;
class Term{//多项式的每一项
friend Polynomial;
public:
float coef;//系数
int exp;//指数
};

class Polynomial{//多项式类
friend ostream & operator<<(ostream &o,const Polynomial & poly);
public:
Polynomial();
Polynomial(const Polynomial & poly);
~Polynomial();
Polynomial operator+(const Polynomial & poly);//多项式加法
Polynomial operator*(const Polynomial & poly);//多项式乘法
float Eval(float x);//数x代入多项式求值
void NewTerm(float coef,int exp);//添加一项,若有相同的指数项,则合并
private:
void insertTerm(const Term & term);//项的有序插入
private:
Term *termArray;//非零系数项数组
int capacity;//数组大小
int terms;//非零系数的项数
};

Polynomial::Polynomial()
{
this->terms=0;
this->capacity=10;
termArray=new Term[this->capacity];
}

Polynomial::Polynomial(const Polynomial & b)
{
this->terms=0;
this->capacity=b.capacity;
termArray = new Term[this->capacity];
for(int i=0;i<b.terms;i++){
NewTerm(b.termArray[i].coef,b.termArray[i].exp);
}
}

Polynomial::~Polynomial()
{
delete [] termArray;
}

Polynomial Polynomial::operator+(const Polynomial & b)
{
Polynomial c;
int aPos=0;
int bPos=0;
while(aPos<terms && bPos<b.terms){
if(termArray[aPos].exp == b.termArray[bPos].exp){
float coef=termArray[aPos].coef+b.termArray[bPos].coef;
if(coef)c.NewTerm(coef,termArray[aPos].exp);
aPos++;bPos++;
}else if(termArray[bPos].exp < b.termArray[bPos].exp){
c.NewTerm(b.termArray[bPos].coef,b.termArray[bPos].exp);
bPos++;
}else{
c.NewTerm(termArray[aPos].coef,termArray[aPos].exp);
aPos++;
}
}
while (aPos < terms){
c.NewTerm(termArray[aPos].coef,termArray[aPos].exp);
aPos++;
}
while (bPos < b.terms){
c.NewTerm(b.termArray[bPos].coef,b.termArray[bPos].exp);
bPos++;
}
return c;
}

Polynomial Polynomial::operator*(const Polynomial & b)
{
Polynomial c;
for(int i=0; i<terms; i++){
for(int j=0; j<b.terms; j++){
float coef = termArray[i].coef*b.termArray[j].coef;
int exp = termArray[i].exp + b.termArray[j].exp;
c.NewTerm(coef,exp);
}
}
return c;
}
void Polynomial::NewTerm(float coef, int exp)
{
if(terms == capacity){
capacity *= 2;
Term *tmp = new Term[capacity];
(termArray,termArray+terms,tmp);
delete [] termArray;
termArray = tmp;
}
Term ATerm;
ATerm.coef=coef;ATerm.exp=exp;
insertTerm(ATerm);
}
void Polynomial::insertTerm(const Term & term)
{
int i;
for(i=0; i<terms && term.exp<termArray[i].exp; i++){
}
if(term.exp == termArray[i].exp){
termArray[i].coef += term.coef;
if(!termArray[i].coef){
for(int j=i; j<terms-1; j++)
termArray[j]= termArray[j+1];
terms--;
}
}else{
for(int j=terms-1; j>=i;j--)
termArray[j+1]=termArray[j];
termArray[i] = term;
terms++;
}
}

float Polynomial::Eval(float x)
{
float res=0.0;
for(int i=0;i<terms; i++){
res += termArray[i].coef * pow(x,termArray[i].exp);
}
return res;
}

ostream & operator<<(ostream & o,const Polynomial & poly)
{
for(int i=0;i<poly.terms-1;i++){
o<<poly.termArray[i].coef<<"x^"<<poly.termArray[i].exp<<" + ";
}
o<<poly.termArray[poly.terms-1].coef<<"x^"<<poly.termArray[poly.terms-1].exp;
return o;
}

void test()
{
Polynomial p1;
p1.NewTerm(3,2);
p1.NewTerm(2.1,3);

Polynomial p2;
p2.NewTerm(1,2);
p2.NewTerm(1,3);
p2.NewTerm(5,1);

cout<<"("<<p1<<") + ("<<p2<<") = "<<p1+p2<<endl;
cout<<"F(x=2) = "<<(p1+p2).Eval(2)<<endl;
cout<<"("<<p1<<") * ("<<p2<<") = "<<p1 * p2<<endl;
}

int main()
{
test();
system("Pause");
return 0;
}
#include <iostream>
#include<algorithm>
using namespace std;

class Polynomial;
class Term{//多项式的每一项
friend Polynomial;
public:
float coef;//系数
int exp;//指数
};
class Polynomial{//多项式类
friend ostream & operator<<(ostream &o,const Polynomial & poly);
public:
Polynomial();
Polynomial(const Polynomial & poly);
~Polynomial();
Polynomial operator+(const Polynomial & poly);//多项式加法
Polynomial operator*(const Polynomial & poly);//多项式乘法
float Eval(float x);//数x代入多项式求值
void NewTerm(float coef,int exp);//添加一项,若有相同的指数项,则合并
private:
void insertTerm(const Term & term);//项的有序插入
private:
Term *termArray;//非零系数项数组
int capacity;//数组大小
int terms;//非零系数的项数
};
Polynomial::Polynomial()
{
this->terms=0;
this->capacity=10;
termArray=new Term[this->capacity];
}
Polynomial::Polynomial(const Polynomial & b)
{
this->terms=0;
this->capacity=b.capacity;
termArray = new Term[this->capacity];
for(int i=0;i<b.terms;i++){
NewTerm(b.termArray[i].coef,b.termArray[i].exp);
}
}

Polynomial::
~Polynomial()
{
delete [] termArray;
}

Polynomial Polynomial::operator+(const Polynomial & b)
{
Polynomial c;
int aPos=0;
int bPos=0;
while(aPos<terms && bPos<b.terms){
if(termArray[aPos].exp == b.termArray[bPos].exp){
float coef=termArray[aPos].coef+b.termArray[bPos].coef;
if(coef)c.NewTerm(coef,termArray[aPos].exp);
aPos++;bPos++;
}else if(termArray[bPos].exp < b.termArray[bPos].exp){
c.NewTerm(b.termArray[bPos].coef,b.termArray[bPos].exp);
bPos++;
}else{
c.NewTerm(termArray[aPos].coef,termArray[aPos].exp);
aPos++;
}
}
while (aPos < terms){
c.NewTerm(termArray[aPos].coef,termArray[aPos].exp);
aPos++;
}
while (bPos < b.terms){
c.NewTerm(b.termArray[bPos].coef,b.termArray[bPos].exp);
bPos++;
}
return c;
}

Polynomial Polynomial::operator*(const Polynomial & b)
{
Polynomial c;
for(int i=0; i<terms; i++){
for(int j=0; j<b.terms; j++){
float coef = termArray[i].coef*b.termArray[j].coef;
int exp = termArray[i].exp + b.termArray[j].exp;
c.NewTerm(coef,exp);
}
}
return c;
}
void Polynomial::NewTerm(float coef, int exp)
{
if(terms == capacity){
capacity *= 2;
Term *tmp = new Term[capacity];
(termArray,termArray+terms,tmp);
delete [] termArray;
termArray = tmp;
}
Term ATerm;
ATerm.coef=coef;ATerm.exp=exp;
insertTerm(ATerm);
}
void Polynomial::insertTerm(const Term & term)
{
int i;
for(i=0; i<terms && term.exp<termArray[i].exp; i++){
}
if(term.exp == termArray[i].exp){
termArray[i].coef += term.coef;
if(!termArray[i].coef){
for(int j=i; j<terms-1; j++)
termArray[j]= termArray[j+1];
terms--;
}
}else{
for(int j=terms-1; j>=i;j--)
termArray[j+1]=termArray[j];
termArray[i] = term;
terms++;
}
}

float Polynomial::Eval(float x)
{
float res=0.0;
for(int i=0;i<terms; i++){
res += termArray[i].coef * pow(x,termArray[i].exp);
}
return res;
}

ostream & operator<<(ostream & o,const Polynomial & poly)
{
for(int i=0;i<poly.terms-1;i++){
o<<poly.termArray[i].coef<<"x^"<<poly.termArray[i].exp<<" + ";
}
o<<poly.termArray[poly.terms-1].coef<<"x^"<<poly.termArray[poly.terms-1].exp;
return o;
}

void test()
{
Polynomial p1;
p1.NewTerm(3,2);
p1.NewTerm(2.1,3);

Polynomial p2;
p2.NewTerm(1,2);
p2.NewTerm(1,3);
p2.NewTerm(5,1);

cout<<"("<<p1<<") + ("<<p2<<") = "<<p1+p2<<endl;
cout<<"F(x=2) = "<<(p1+p2).Eval(2)<<endl;
cout<<"("<<p1<<") * ("<<p2<<") = "<<p1 * p2<<endl;
}
int main()
{

test();

system("Pause");

return 0;
}
测试结果:
Cpp代码
(2.1x^3 + 3x^2) + (1x^3 + 1x^2 + 5x^1) = 3.1x^3 + 4x^2 + 5x^1
F(x=2) = 50.8
(2.1x^3 + 3x^2) * (1x^3 + 1x^2 + 5x^1) = 2.1x^6 + 5.1x^5 + 13.5x^4 + 15x^3
请按任意键继续. . .
(2.1x^3 + 3x^2) + (1x^3 + 1x^2 + 5x^1) = 3.1x^3 + 4x^2 + 5x^1
F(x=2) = 50.8
(2.1x^3 + 3x^2) * (1x^3 + 1x^2 + 5x^1) = 2.1x^6 + 5.1x^5 + 13.5x^4 + 15x^3
请按任意键继续. . .

3. 如何用c语言编程序:九九乘法表

九九乘法表共9行9列,重点考察for循环的掌握情况。下面给出了输出完整乘法表、右上、右下、左上、左下乘法表的代码。

1、【代码一】输出完整的三角形:

#include <stdio.h>

int main() {

int i,j; // i, j控制行或列

for(i=1;i<=9;i++) {

for(j=1;j<=9;j++)

// %2d 控制宽度为两个字符,且右对齐;如果改为 %-2d 则为左对齐

// 为tab缩进

printf("%d*%d=%2d ", i, j, i*j);

printf(" ");

}

return 0;

}

4. 编程里打印乘法口诀表怎么打

楼上的第一个是BASIC的,第二个是C 的都没有你要的PASCAL。我给你写一个。打印三角乘法表

var
i,j:integer;
begin
for i:=1 to 9 do
begin
for j:=1 to i do
write(j,'*',i,'=',i*j,' ');
writeln;
end;
end.

5. 计算机编程中矩阵乘法有什么用

线性代数是计算机特别是图形学中很重要的数学工具。3D游戏和CAD中的三维透视,科学计算中的方程组求解都需要用矩阵,人工智能中的人脑模型,都需要用矩阵算法中的加法,减法,乘法和除法(左除和右除)

6. 编程中九九乘法表

C语言输出九九乘法表,主要运用for循环进行控制输出。
需要注意的是:在何时需要加空格,何时需要换行。
#include<stdio.h>
void main()
{
int i=1,j=1;
for(i=1;i<=9;i++) // 控制行,一共9行
for(j=1;j<=i;j++) // 控制各行的列数
{
printf("%d*%d=%d",j,i,i*j); // 输出乘法表各项内容
if(i!=j) printf("\t"); // 相邻两项直接加空格
if(i==j) printf("\n"); // 控制列数
}
}

7. 用c编程乘法表

/*
shellwin231986
好好看看你的程序……^_^
logo0775,你是说我i
=
0那块吧,不影响的,有j<=i呢,不过确实是没看清,我狡辩一下而已,写成i
=
1
最好。
楼主,把#define
DD
这行的DD分别换成AA、BB、CC、DD看看结果。
另外,这个谈不上什么C与C++的区别,差不多都是这样做的,顶多在输出上可以用C++的标准库的一些函数而已,这个你就自己去改吧。
*/
#include
"stdio.h"
#define
DD
#ifdef
AA
int
main()
{
int
i
,
j;
for(
i
=
1
;
i
<
10
;
i++
)
{
for(
j
=
1
;
j
<=
i
;
j++
)
{
printf(
"%d*%d=%2d
"
,
j
,
i
,
i
*
j
);
}
printf(
"\n"
);
}
return
0;
}
#endif
#ifdef
BB
int
main()
{
int
i
,
j;
for(
i
=
1
;
i
<
10
;
i++
)
{
for(
j
=
i
;
j
<
10
;
j++
)
{
printf(
"%d*%d=%2d
"
,
i
,
j
,
i
*
j
);
}
printf(
"\n"
);
}
return
0;
}
#endif
#ifdef
CC
int
main()
{
int
i
,
j;
for(
i
=
1
;
i
<
10
;
i++
)
{
for(
j
=
1
;
j
<
10
;
j++
)
{
if(
j
<
i
)
{
printf(
"
"
);
}
else
{
printf(
"%d*%d=%2d
"
,
i
,
j
,
i
*
j
);
}
}
printf(
"\n"
);
}
return
0;
}
#endif
#ifdef
DD
int
main()
{
int
i
,
j;
for(
i
=
1
;
i
<
10
;
i++
)
{
for(
j
=
1
;
j
<
10
;
j++
)
{
if(
10
-
j
>
i
)
{
printf(
"
"
);
}
else
{
printf(
"%d*%d=%2d
"
,
i
+
j
-
9,
i
,
i
*
(
i
+
j
-
9
)
);
}
}
printf(
"\n"
);
}
return
0;
}
#endif

8. c语言编程“乘法口诀表”.

/*
看了同学们的几个答案,简直...
奉上我的源码给大家,那个jplus变量才是这个小程序的点睛之处...
*/
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>//我需要使用其中的getch函数让程序暂停
intmain()
{
system("color4e");//纯属为了养眼
printf("利用双重循环输出乘法口决表 ");//程序介绍
inti,j;//两个变量分别控制行与列输出
intjplus=2;//在行确定时,控制到底需要输出多少列
for(i=1;i<10;i++)//行控制
{
for(j=1;j<jplus;j++)//列控制
{
printf("%d×%d=%d ",j,i,i*j);
}
printf(" ");
jplus++;//每行输出完毕后,下一行的列数都会加1
}
printf(" 输出完毕,按任意键退出...");
getch();//程序暂停以免一闪而过
return0;
}
/*
如果实在讨厌那个jplus,你还可以用i+1来代替它
写成如下的紧凑一点的格式,同样OK
*/
intmain(){
inti=0;
intj=0;
for(i=1;i<10;i++)
{
for(j=1;j<i+1;j++)
{
printf("%dx%d=%d ",j,i,i*j);
}
printf(" ");
}
return0;
}

9. C语言编程九九乘法表

九九乘法表共9行9列,重点考察for循环的掌握情况。下面给出了输出完整乘法表、右上、右下、左上、左下乘法表的代码。

1、【代码一】输出完整的三角形:

#include <stdio.h>

int main() {

int i,j; // i, j控制行或列

for(i=1;i<=9;i++) {

for(j=1;j<=9;j++)

// %2d 控制宽度为两个字符,且右对齐;如果改为 %-2d 则为左对齐

// 为tab缩进

printf("%d*%d=%2d ", i, j, i*j);

printf(" ");

}

return 0;

}

10. 乘法在c语言中怎么表示

估计你的意思是说C语言编译成汇编语言之后乘法怎么表示吧,因为在汇编中加法确实是用ADD表示的,下面给你运算符表:

ADD 加法.
ADC 带进位加法.
INC 加 1.
AAA 加法的ASCII码调整.
DAA 加法的十进制调整.
SUB 减法.
SBB 带借位减法.
DEC 减 1.
NEC 求反(以 0 减之).
CMP 比较.(两操作数作减法,仅修改标志位,不回送结果).
AAS 减法的ASCII码调整.
DAS 减法的十进制调整.
MUL 无符号乘法.
IMUL 整数乘法.
以上两条,结果回送AH和AL(字节运算),或DX和AX(字运算),
AAM 乘法的ASCII码调整.
DIV 无符号除法.
IDIV 整数除法.
以上两条,结果回送:
商回送AL,余数回送AH, (字节运算);
或 商回送AX,余数回送DX, (字运算).
AAD 除法的ASCII码调整.
CBW 字节转换为字. (把AL中字节的符号扩展到AH中去)
CWD 字转换为双字. (把AX中的字的符号扩展到DX中去)
CWDE 字转换为双字. (把AX中的字符号扩展到EAX中去)
CDQ 双字扩展. (把EAX中的字的符号扩展到EDX中去)

热点内容
网卡访问 发布:2025-05-18 03:35:04 浏览:504
接收和发送服务器地址 发布:2025-05-18 03:33:48 浏览:365
ef数据库查询数据 发布:2025-05-18 03:29:36 浏览:668
百度云下载文件夹 发布:2025-05-18 03:17:33 浏览:674
php云开发 发布:2025-05-18 03:12:41 浏览:447
sql语句显示表 发布:2025-05-18 03:12:30 浏览:690
数据库系统的例子 发布:2025-05-18 03:02:42 浏览:191
数字化储存与编译是什么 发布:2025-05-18 02:56:55 浏览:217
个人网站模板源码 发布:2025-05-18 02:51:17 浏览:490
主服务器ip地址 发布:2025-05-18 02:46:29 浏览:856