当前位置:首页 » 编程软件 » 如何编译76整数算数运算

如何编译76整数算数运算

发布时间: 2023-05-15 07:42:32

㈠ 用c语言编程实现一个简单的四则运算计算器

分类: 电脑让耐/网络 >> 程序设计 >> 其他编程语言
问题描述:

编程:编程实现一个简单的四则运算计算器:从键盘输入一个四则运算表达式(没有空格和括号),遇等瞎滑数号"="说明输入结束,输出结果。

假设计算器只能进行加减乘除运算,运算数和结果都是整数,4种运算符的优先级相同,按从左到右的顺序计算(即:2+3*5先计算2+3,再计算5*5)。

示例:括号内是说明

输入

1+2*10-10/2=

输出

10

解析:

#include <stdio.h>磨首

函数,读数操作数

int getNextNum()

{

int ret;

scanf("%d",&ret);

return ret;

}

函数,读运算符

char getOpt()

{

return getchar();

}

函数,计算

int caculate(int op1 , int op2 ,char opt)

{

if(opt=='+')return op1+op2;

if(opt=='-')return op1-op2;

if(opt=='*')return op1*op2;

if(opt=='/')return op1/op2;

return 0;

}

int main()

{

int op1,op2;

char opt;

计算结果放在第一个操作数

op1 = getNextNum();

while(1)

{

opt = getOpt();

if ( opt == '=' ) break;

op2 = getNextNum();

op1 = caculate(op1,op2,opt);

}

printf("%d\n",op1);

}

return 0;

}

㈡ 编程实现无符号超长整数的算术运算(加、减、乘、除)

参考答案: 玉不琢,不成器;人不学,不知道。(《礼记》)

㈢ c语言编写 编写一个简单的计算器,实现两个整型数的四则运算。

1、打开CodeBlocks,新建一个空白文件,先定义头文件和主函数,接着写程序多大的主体:

㈣ c语言表达式76%3的值是

很高兴和你一起学习和讨论C/C++,一下是我对 此题租羡大目的理解派陵,希望对你有所帮助。
1, % 在C语言中是 求余数 运算符,其左右 操作数 都必须是整数,结果也是整弊竖数
2. 通过数学计算可以得到76/3=25.....1 ,所以76%3的结果为1

㈤ C语言中76除以10=7,为什么不四舍五入得8

这不是巧携数学运算,在C语言里叫做整除运孝宽伏算,整除是舍弃余数的,而与整除巧渣配套的还有取余运算,运算结果就是余数了。

76/10 = 7
76%10 = 6

㈥ 用C语言编译数学运算

这个是我的实验报告,跟你的这个一样的。如果只要代码的话就看最后面的。我感觉应该算比较全面的。你看看,有什么具体要求可以提出来。
一、需求分析
1、 功能:疏如一行表达式,若表达式有误,则输出“表达式有错” ,否则计算出表达式的值并输出。 运算符包括加、减、乘、除、乘方、一目减。 括号均为小括号,但可以层层嵌套。操作数可以是浮点数,也包括有多个字母组成的变量。
2、 输入的形式为表达式,按回车结束。输入值的范围不超过浮点数的范围。含有变量,变量名由字母组成,大小写不限。
3、 若计算结果为整数,则输出整数,若含有小数,则输出浮点数。
二、概要设计
1、 总体思路,先读入一行表达式,用一个字符数组存储。然后依次读每个字符,进行判断。边读入边进行计算。程序中用到了两个栈,一个字符栈以及一个数字栈,分别用来存储运算符和数字,根据运算符的优先顺序进行计算。最后输出结果。

2、程序包括几个模块,主函数和几个基本函数。
说明几个函数:
bool stackempty(save1 s)用来判断操作数栈s是否为空。
void push(save1 &s,char e)若栈满则输出“栈已满”,否则将元素e入栈
void pop(save1 &s, char &e)若栈为空则输出“栈为空”,否则将栈顶元素赋给e
bool stackempty2(save2 s)用来判断运算符栈s是否为空。
void push2(save2 &s, char e)若运算符栈满则输出“栈已满”,否则将元素e入栈
void pop2(save2 &s, char &e)若栈为空则输出“栈为空”,否则将栈顶元素赋给e
int in(char e)返回运算符e在栈内的优先级别
int out(char e) 返回运算符e在栈外的优先级别
void count(char a,char ope, char b)将a、b进行相应的运算,并将运算结果入栈
3、具体操作步骤:
1、先读入一行表达式,用一个字符数组line[]存储
2、依次读入每个字符并进行处理同是进行表达式判错:
1. 遇数字,则继续判断下一个字符,直到下一个字符不是数字且不是小数点,若该数含有两个小以上数点,则表示输入错误。否则即可保证该操作数是完整的浮点数,然后将该数入操作数栈。

若数字不是表达式的最后一位,且数字后面跟的不是“+、-、*、/、^、)”,则为表达式错误

2. 遇运算符,则分两种情况:
1、若运算符为负号(该运算符为符号的情况有两种:一为负号在最开头,一为符号前面是“(” ),则先将0入操作数栈,然后再将负号入运算符栈。
2、该运算符不是负号则与运算符栈的栈顶元素比:
(1) 若栈顶元素优先级低, 新输入的运算符入栈。
(2) 若栈顶元素优先级高,
1) 从符号栈弹出一个运算符,
2) 从对象栈弹出一个/两个操作数,
3) 运算结果压入对象栈。
(3) 优先级相等,则栈顶元素出栈,与输入元素对消。

若“(、+、-、*、/、^”放在表达式最后面,则表达式错误
若“+、-、*、/、^”后面跟的不是数字或者变量,表达式错误

3、遇字母变量,则继续判断下一个字符,直到下一个字符不是字母变量,即可保证该变量是完整的,然后输出“请输入变量的值”,再将输入的变量值入操作数栈。
若变量后面跟的不是“+、-、*、/、^、)”,则表达式错误
4、若所读的该字符不是上述情况中的一种,则表达式错误

3、当将所有的字符都读一遍之后,若表达式正确的话,则必然不含有“(”或者“)”。即若运算符栈中含有“(”或者“)”,则表达式必错误。 再考虑表达式正确的情况:运算符栈可能为空,则操作符栈中必剩下一个操作数,即最后的结果。若不为空,则留在运算符栈中的运算符的优先级别从栈顶至栈底依次递减。故可从运算符栈顶开始弹出一个运算符,从操作数栈中弹出两个操作数进行运算,再将运算结果入操作数栈,一直循环至运算符栈为空。此时操作数栈剩下的唯一一个操作数就是运算结果。

三、结论及体会
1、实验结论
a)、实验完成了题目的要求,自己添加了对浮点数的操作,并进行判错。
b)、编写代码基本上能够满足编程规范的要求,代码的变量命名,以及注释的书写,基本能按照要求进行。
b)、将数据结构中的队列和堆栈的知识复习到,并且学会创新,在代码的编写中,学习了编程规范,学习了结构化编程。

2、实验体会
a)、通过本设计实验将数据结构中的堆栈和队列的知识复习到,并且能够自己设计一些东西,学会了在设计实验过程时的基本步骤。基本上能够有条理的解决这些问题。
b)、在试验中遇到了很多的问题,都是以前没有发现的,这些问题设计的方面很多,有以前的C++基础的,也有最近学习的数据结构的知识。通过实验的设计,让我发现了自己的不足。自己在学习知识上面的漏洞。自己在细节方面的考虑还不够全面,很多细节都是通过调试才发现的。比如刚开始时忘了考虑变量之前有负号的情况以及将整个式子读一遍之后,栈中的操作数可能还有剩,还得继续进行计算等。希望通过弥补这些发现的漏洞,提高自己的专业知识水平。
c)、设计过程中的解决问题的方法,让我明白了如何学习会更有效。如何学习才不会耽误太多的时间。也学会了解决问题的一般方法:向老师、同学请教,借助网络等等。
d)、实验过程中也走了很多的弯路,由于在开始设计的时候思路不时很清晰,对于一些问题不能很好的提出解决问题的方法,在设计过程中,代码总是重复的修改,在很多问题上,代码并不时最优的。相信在以后的学习中,随着知识的增多,问题会逐渐得到解决。
四、程序源代码
#include<iostream>
#include<cmath>
#include<cstdlib>
using namespace std;
#define MAX 1000

struct save1
{
float n[MAX];
int top;
}stack1;

struct save2
{
char n[MAX];
int top;
}stack2;
//stack1存储数字,stack2存储运算符号.

bool stackempty(save1 s)//判断是否为空
{
if (s.top== -1)
return 1;
else
return 0;
}

bool stackempty2(save2 s)//判断是否为空
{
if (s.top== -1)
return 1;
else
return 0;
}

void push(save1 &s,float e)//将e入栈
{
if(s.top==MAX-1)
{
cout<<"栈已满"<<endl;
return ;
}
s.top++;
s.n[s.top]=e;
}

void push2(save2 &s,char e)//将e入栈
{
if(s.top==MAX-1)
{
cout<<"栈已满"<<endl;
return ;
}
s.top++;
s.n[s.top]=e;
}

void pop(save1 &s,float &e)//将栈顶元素出栈,存到e中
{
if(s.top==-1)
{ cout<<"栈为空"<<endl; }
else
{e=s.n[s.top]; s.top--; }
}

void pop2(save2 &s,char &e)//将栈顶元素出栈,存到e中
{
if(s.top==-1)
{ cout<<"栈为空"<<endl; }
else
{e=s.n[s.top]; s.top--; }
}

int in(char e)//e在栈内的优先级别
{
if(e=='-' || e=='+') return 2;
if(e=='*' || e=='/') return 4;
if(e=='^') return 5;
if(e=='(') return 0;
if(e==')') return 7;
return -1;
}

int out(char e)//e在栈外的优先级别
{
if(e=='-' || e=='+') return 1;
if(e=='*' || e=='/') return 3;
if(e=='^') return 6;
if(e=='(') return 7;
if(e==')') return 0;
return -1;
}

void count(float a,char ope,float b)//进行计算并将计算结果入栈
{
float sum;
if(ope=='+') sum=a+b;
if(ope=='-') sum=a-b;
if(ope=='*') sum=a*b;
if(ope=='/') sum=a/b;
if(ope=='^') sum=pow(a,b);
push(stack1,sum);
}

int main()
{
int i=0,len,j,nofpoint,g=0;//len表示输入式子的长度。 g表示读入的字符是否是字母变量、数字以及运算符。
float a,b;//a、b用来存储操作数栈中弹出的操作数,便于代入函数中进行计算。
char line[MAX],operate,temp[20];
cout<<"请输入表达式"<<endl;
cin>>line;
len=strlen(line);
stack1.top=-1;//将栈置为空
stack2.top=-1;//将栈置为空
while(1)
{
g=0;
if(isdigit(line[i]))//若读入的字符为数字,则继续判断下一个字符,直到下一个字符不是数字或者不是小数点,即可保证该操作数是完整的小数,然后将该数入操作数栈。
{
j=0; g=1;
nofpoint=0;//记录所存的数中小数点的个数
while(isdigit(line[i]) || line[i]=='.')
{
if(line[i]=='.') nofpoint++;
temp[j++]=line[i];
i++;
if(i>=len) break;
}
if( nofpoint>1 || (i<len&&(line[i]!='-' && line[i]!='+' && line[i]!='*' && line[i]!='/' && line[i]!='^' && line[i]!=')')) )
{ cout<<"表达式有错"<<endl; return 0; }//所存数中含有不止一个小数点,或者数字后面跟的不是“+、-、*、/、^、)”,则为错误

temp[j]='\0';
b=atof(temp);
push(stack1,b);
if(i>=len) break;
}
else
{
if(line[i]=='-' || line[i]=='+' || line[i]=='*' || line[i]=='/' ||
line[i]=='^' || line[i]=='(' || line[i]==')' ) //若读入的字符为运算符的情况
{
g=1;
if(line[i]=='(' && i==len) { cout<<"表达式有错"<<endl; return 0; }// “(”放表达式最后面,错误
if(line[i]=='-' || line[i]=='+' || line[i]=='*' || line[i]=='/' || line[i]=='^')
{
if(i==len) { cout<<"表达式有错"<<endl; return 0; }//“+、-、*、/、^”放在表达式最后面,错误
if( (!isdigit(line[i+1])) && (!isalpha(line[i+1])) && line[i+1]!='(')//“+、-、*、/、^”后面跟的不是数字或者变量,错误
{ cout<<"表达式有错"<<endl; return 0; }
}

if(line[i]=='-' && (i==0 || line[i-1]=='(' ))//运算符是负号
{
push(stack1,0);
push2(stack2,line[i]);
i++;
}
else
{ //读入的运算符与运算符栈的栈顶元素相比,并进行相应的操作
if(in(stack2.n[stack2.top])<out(line[i])||stackempty2(stack2)) { push2(stack2,line[i]);i++;}
else
if(in(stack2.n[stack2.top])==out(line[i])) {i++; stack2.top--;}
else
if(in(stack2.n[stack2.top])>out(line[i]))
{
pop(stack1,a);
pop(stack1,b);
pop2(stack2,operate);
count(b,operate,a);
}
if(i>=len) break;
}
}
else
{
if(isalpha(line[i]))//读入的字符是字母变量的情况
{
g=1;
cout<<"请输入变量";
while( isalpha(line[i])) { cout<<line[i]; i++; }
cout<<"的值"<<endl;
cin>>b;
push(stack1,b);
if(i>=len) break;
if(line[i]!='-' && line[i]!='+' && line[i]!='*' && line[i]!='/' && line[i]!='^' && line[i]!=')')//变量后面跟的不是“+、-、*、/、^、)”,则为错误
{ cout<<"表达式有错"<<endl; return 0; }
}
}
}
if(g==0) { cout<<"表达式有错"<<endl; return 0; }//g=0表示该字符是不符合要求的字符
}

while(stack2.top!=-1)//读入结束后,继续进行操作,直到运算符栈为空
{
pop(stack1,a);
pop(stack1,b);
pop2(stack2,operate);
if(operate=='(' || operate==')') //括号多余的情况
{ cout<<"表达式有错"<<endl; return 0; }
count(b,operate,a);
}
cout<<stack1.n[stack1.top]<<endl;
return 0;
}

㈦ 长整数运算,用 c++解决

#include<iostream>
#include<string>
#include<iomanip>
#include<algorithm>
using namespace std;
#define MAXN 9999
#define DLEN 4
class BigNum{
public:
int a[10000];//?????????
int len; //????
public:
BigNum(){len = 1;memset(a,0,sizeof(a));}
BigNum(const int);
BigNum(const char*);
BigNum(const BigNum &);
BigNum &operator=(const BigNum &);
//BigNum &operator>>塌虚
// friend ostream& operator>>(ostream&, BigNum&);
BigNum operator+(const BigNum &) const;
BigNum operator-(const BigNum &) const;
BigNum operator*(const BigNum &) const;
BigNum operator/(const int &) const;
BigNum operator^(const int &) const;
int operator%(const int &) const;
bool operator>(const BigNum & T)const;
void print();
};
// ostream& operator>>(ostream&os, BigNum &b)
// {
// char ch;
// int i = -1;
// ch = cin.get();
// while (ch != '\n')
// {
// b.a[++i] = ch - 48;
// ch = cin.get();
// }
//汪余 b.len = i;
// return os;
//
//}

BigNum::BigNum(const int b)
{
int c,d = b;
len = 0;
memset(a,0,sizeof(a));
while(d > MAXN){
c = d - (d / (MAXN + 1)) * (MAXN + 1);
d = d / (MAXN + 1); a[len++] = c;
}
a[len++] = d;
}
BigNum::BigNum(const char*s)
{
int t,k,index,l;
memset(a,0,sizeof(a));
l=strlen(s);
len=l/DLEN;
if(l%DLEN)len++;
index=0;
for(int i=l-1;i>团陵燃=0;i-=DLEN){
t=0;k=i-DLEN+1;
if(k<0)k=0;
for(int j=k;j<=i;j++)
t=t*10+s[j]-'0';
a[index++]=t;
}
}
BigNum::BigNum(const BigNum & T) : len(T.len)
{
int i;
memset(a,0,sizeof(a));
for(i = 0 ; i < len ; i++)a = T.a;
}
BigNum & BigNum::operator=(const BigNum & n)
{
len = n.len;
memset(a,0,sizeof(a));
for(int i = 0 ; i < len ; i++)
a = n.a;
return *this;
}
BigNum BigNum::operator+(const BigNum & T) const
{ BigNum t(*this);
int i,big;//??
big = T.len > len ? T.len : len;
for(i = 0 ; i < big ; i++)
{
t.a +=T.a;
if(t.a > MAXN)
{
t.a[i + 1]++;
t.a -=MAXN+1;
}
}
if(t.a[big] != 0) t.len = big + 1;
else t.len = big;
return t;
}
BigNum BigNum::operator-(const BigNum & T) const
{
int i,j,big;
bool flag;
BigNum t1,t2;
if(*this>T){
t1=*this;
t2=T;
flag=0;
}else{
t1=T;
t2=*this;
flag=1;
}
big=t1.len;
for(i = 0 ; i < big ; i++){
if(t1.a < t2.a){
j = i + 1;
while(t1.a[j] == 0) j++;
t1.a[j--]--;
while(j > i) t1.a[j--] += MAXN;
t1.a += MAXN + 1 - t2.a;
}
else t1.a -= t2.a;
}
t1.len = big;
while(t1.a[len - 1] == 0 && t1.len > 1){
t1.len--;
big--;
}
if(flag)t1.a[big-1]=0-t1.a[big-1];
return t1;
}
BigNum BigNum::operator*(const BigNum & T) const
{
BigNum ret;
int i,j,up;
int temp,temp1;
for(i = 0 ; i < len ; i++){
up = 0;
for(j = 0 ; j < T.len ; j++){
temp = a * T.a[j] + ret.a[i + j] + up;
if(temp > MAXN){
temp1 = temp - temp / (MAXN + 1) * (MAXN + 1);
up = temp / (MAXN + 1);
ret.a[i + j] = temp1;
}
else {
up = 0;
ret.a[i + j] = temp;
}
}
if(up != 0)
ret.a[i + j] = up;
}
ret.len = i + j;
while(ret.a[ret.len - 1] == 0 && ret.len > 1) ret.len--;
return ret;
}
BigNum BigNum::operator/(const int & b) const
{
BigNum ret;
int i,down = 0;
for(i = len - 1 ; i >= 0 ; i--){
ret.a = (a + down * (MAXN + 1)) / b;
down = a + down * (MAXN + 1) - ret.a * b;
}
ret.len = len;
while(ret.a[ret.len - 1] == 0 && ret.len > 1) ret.len--;
return ret;
}
int BigNum::operator %(const int & b) const
{
int i,d=0;
for (i = len-1; i>=0; i--){
d = ((d * (MAXN+1))% b + a)% b;
}
return d;
}
BigNum BigNum::operator^(const int & n) const
{
BigNum t,ret(1);
int i;
if(n<0)exit(-1);
if(n==0)return 1;
if(n==1)return *this;
int m=n;
while(m>1){
t=*this;
for( i=1;i<<1<=m;i<<=1){
t=t*t;
}
m-=i;
ret=ret*t;
if(m==1)ret=ret*(*this);
}
return ret;
}
bool BigNum::operator>(const BigNum & T) const
{
int ln;
if(len > T.len) return true;
else if(len == T.len){
ln = len - 1;
while(a[ln] == T.a[ln] && ln >= 0) ln--;
if(ln >= 0 && a[ln] > T.a[ln]) return true;
else return false;
}
else return false;
}
void BigNum::print()
{
int i;
cout << a[len - 1];
for(i = len - 2 ; i >= 0 ; i--){
cout.width(DLEN);
cout.fill('0');
cout << a;
}
cout << endl;
}

这个是大数的模版。

㈧ 如何用c语言编写可以实现四则运算的计算器

#include <stdio.h>#include <stdlib.h>#include <string.h>void resolve(char *p);void del(int n);void find(char *p);int last();char s[80];int num[80];char sign[80]; /*此程序所有数组为全局变量*/int main(){ int n=1; while(n==1) { printf("Enter:\n"); scanf("%s",s); resolve(s); find(sign); /*寻找乘号和除号*/ printf("result= %d\n\n",last()); printf("please Enter:\n1:continue 2:exit\n\n "); scanf("%d",&n); }
return 0;}
void resolve(char *p) /*数字放在NUM中,符号放在SIGN中*/{ int n=0,k=0; while(*p!='\0') { if(*p>='0'&&*p<='9') n=n*10+(*p-'0'); else {
num[k]=n; n=0; sign[k++]=*p; } p++; } num[k]=n;
}
void find(char *p) { int i,n; for(i=0;*p;i++) { if(*p=='*'||*p=='/') { n=(*p=='*' ? num[i]*num[i+1] : num[i]/num[i+1]); num[i]=n; del(i); /*删除其中的乘除号和已经运算过的数字*/ i--; } else p++;
} }

void del(int n){ int i; for(i=n;i<strlen(sign);i++) { sign[i]=sign[i+1]; num[i+1]=num[i+2]; }
}
int last(){ int i,sum; sum=(sign[0]=='+' ? num[0]+num[1] : num[0]-num[1]); for(i=1;sign[i];i++) sum=(sign[i]=='+' ? sum+num[i+1] : sum-num[i+1]); return sum;
}
刚出炉的 没有实现其中的括号功能 也只能处理整数

㈨ 使用C++编写一个程序实现无符号超长整数的算术运算(加、减、乘、除)发送至[email protected]

很久以前写的竖宏,不过实现的是 有符埋伍号的 加余液册减运算,没乘除 头文件 :myint.h --------------------------- #ifndef _MYINT_H #define _MYINT_H #include <iostream> #include <string> using namespace std; const int MAX=20; class MyInt { private: int arry[MAX]; int plus; int size; protected: int bigger(const MyInt & lhs) { if(size> lhs.size) return 1; else if(size <lhs.size) return 0; //lhs bigger else if(size=lhs.size) { for(int i=size-1;i> =0;i--) { if(arry[i]> lhs.arry[i]) return 1; else if(arry[i] <lhs.arry[i]) return 0; } } return 0; } void add(const MyInt &lhs,int pp) { int pp_plus=lhs.plus; if(pp==1) pp_plus=(pp_plus+1)%2; int temp=0; //----------------- if(plus==0&&pp_plus==0) { for(int i=0;i <MAX;i++) { temp=arry[i]+lhs.arry[i]; if(temp> 9) { temp=temp-10; arry[i+1]=arry[i+1]+1; } arry[i]=temp; } } //-------------------- if(plus==0&&pp_plus==1) { if(bigger(lhs)==1) { for(int i=0;i <MAX;i++) { temp=arry[i]-lhs.arry[i]; if(temp <0) { temp=temp+10; arry[i+1]=arry[i+1]-1; } arry[i]=temp; } } else { int swap[MAX]; for(int i=0;i <MAX;i++) swap[i]=arry[i]; for(i=0;i <MAX;i++) arry[i]=lhs.arry[i]; for( i=0;i <MAX;i++) { temp=arry[i]-swap[i]; if(temp <0) { temp=temp+10; arry[i+1]=arry[i+1]-1; } arry[i]=temp; } } } //------------------------- if(plus==1&&pp_plus==1) { for(int i=0;i <MAX;i++) { temp=arry[i]+lhs.arry[i]; if(temp> 9) { temp=temp-10; arry[i+1]=arry[i+1]+1; } arry[i]=temp; } } //------------------- if(plus==1&&pp_plus==0) { if(bigger(lhs)==1) { for(int i=0;i <MAX;i++) { temp=arry[i]-lhs.arry[i]; if(temp <0) { temp=temp+10; arry[i+1]=arry[i+1]-1; } arry[i]=temp; } } else { int swap[MAX]; for(int i=0;i <MAX;i++) swap[i]=arry[i]; for(i=0;i <MAX;i++) arry[i]=lhs.arry[i]; for( i=0;i <MAX;i++) { temp=arry[i]-swap[i]; if(temp <0) { temp=temp+10; arry[i+1]=arry[i+1]-1; } arry[i]=temp; } } } } public: MyInt() : plus(0),size(0) { for(int i=0;i <MAX;i++) arry[i]=0; } MyInt(string str) { int temp=0; for(int i=0;i <MAX;i++) arry[i]=0; if(str[0]== '- ') { plus=1; size=str.size()-1; for(i=str.size()-1;i> 0;i--) { temp=int(str[i])-48; arry[size-i]=temp; } } else { plus=0; size=str.size(); for(i=str.size()-1;i> =0;i--) { temp=int(str[i])-48; arry[size-1-i]=temp; } } } void operator =(const MyInt &lhs) { plus=lhs.plus; size=lhs.size; for(int i=0;i <MAX;i++) { arry[i]=0; arry[i]=lhs.arry[i]; } } void operator +=( const MyInt &lhs) { add(lhs,0); } void operator -=(const MyInt &lhs) { add(lhs,1); } void display() { if(plus==1) cout < < "- "; int flag=0; for (int i=MAX-1;i> =0;i--) { if(arry[i]!=0||flag==1) { flag=1; if(flag==1) cout < <arry[i]; } } } }; #endif //---------- 头文件 complex.h #ifndef _COMPLEX_H #define _COMPLEX_H #include "myint.h " class ComplexInt { private: MyInt real; MyInt unreal; public: ComplexInt() : real( "0 "),unreal( "0 "){} ComplexInt(string r,string ur) :real(r),unreal(ur) {} ComplexInt(string r) :real(r),unreal( "0 ") {} void operator +=(const ComplexInt & lhs) { real+=lhs.real; unreal+=lhs.unreal; } void operator -=(const ComplexInt & lhs) { real-=lhs.real; unreal-=lhs.unreal; } void operator =(const ComplexInt & lhs) { real=lhs.real; unreal=lhs.unreal; } void display() { real.display(); cout < < "+ "; unreal.display(); cout < < "i "; } }; #endif 主函数:main.cpp #include "complex.h " #include "myint.h " void Display(MyInt myint) { cout < < "This is MyInt::Display, the result is: "; myint.display(); cout < <endl; } void Display(ComplexInt complex ) { cout < < "This is ComplexInt::Display, the result is: "; complex.display(); cout < <endl; } void main(int argc, char* argv[]) { MyInt iM( "1234567890123 "); // 整数对象初始化 MyInt iN( "-4445555544444 "); MyInt iResult1; iResult1 = iM; iResult1 += iN; // 整数加法操作 MyInt iResult2 = iM; iResult2 -= iN; // 整数减法操作 // 结果输出 Display(iResult1); // iResult1: -1976419764198 Display(iResult2); // iResult2: 4445555544444 // 实部: 1234567890123;虚部: 4445555544444 ComplexInt ciM( "1234567890123 ", "4445555544444 "); // 复数对象初始化 // 实部: -4445555544444;虚部: 0 ComplexInt ciN( "-4445555544444 "); // 复数对象初始化 ComplexInt ciResult1 = ciM; ComplexInt ciResult2 = ciM; ciResult1 += ciN; // 复数加法操作 ciResult2 -= ciN; // 复数减法操作 // 结果输出 Display(ciResult1); Display(ciResult2); }

㈩ C语言两个整型数的算术运算

第1空填 scanf("%d",&a);

第2空填 scanf("%d",&b);

第3空填 a+b;

第4空填 a-b;

第5空填 a*b;

第6空填 a/b;

第7空填 a%b;

第8空填 printf("差=%d ",sub);

第9空填 printf("积=%d ",mul);

第10空填 printf("商=%d ",div);

第11空填 printf("余数=%d ",oct);

完整的C语言猛唤程序如下

#include<stdio.h>

int main(void)

{

int a,b;//a和b是输入的两个整型值

int add,sub,mul,div,oct;//分别存放和,差,积,商,余数

scanf("%d",&a);//输入a

scanf("%d",&b);//输入b

add=a+b;//和

sub=a-b;//差

mul=a*b;//积

div=a/b;//商

oct=a%b;//余数

printf("和=%d ",add);//输出和

printf("差=%d ",sub);//输出差

printf("积肆蔽=%d ",mul);//输出积

printf("商=%d ",div);//输出商

printf("余数=%d ",oct);//输出余数

枝雹凯return 0;

}

热点内容
比较便宜的云服务器 发布:2025-07-13 18:29:59 浏览:406
切换ftp 发布:2025-07-13 18:29:07 浏览:738
锐龙哪个配置最高画质 发布:2025-07-13 18:22:34 浏览:196
压缩机工作原理图 发布:2025-07-13 18:10:15 浏览:39
黑暗追求者安卓怎么联机 发布:2025-07-13 18:10:07 浏览:617
北大保安自学编程 发布:2025-07-13 18:09:58 浏览:858
java递归排列 发布:2025-07-13 18:02:43 浏览:473
轻量云服务器如何换成d盘 发布:2025-07-13 17:58:45 浏览:931
重置腾讯云服务器时间 发布:2025-07-13 17:54:55 浏览:326
aes256java加密 发布:2025-07-13 17:54:46 浏览:710