當前位置:首頁 » 編程軟體 » 如何編譯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-14 01:27:38 瀏覽:783
php開啟錯誤 發布:2025-07-14 01:16:49 瀏覽:998
esp資料庫 發布:2025-07-14 01:16:44 瀏覽:980
python查找文件路徑 發布:2025-07-14 01:16:03 瀏覽:514
phpapachetomcat 發布:2025-07-14 01:08:41 瀏覽:123
伺服器運維看什麼書 發布:2025-07-14 01:07:32 瀏覽:988
密碼器動態密碼怎麼弄 發布:2025-07-14 00:44:27 瀏覽:386
小米怎麼把視頻加密 發布:2025-07-14 00:42:59 瀏覽:406
在線申訴找回密碼根本什麼都沒有 發布:2025-07-14 00:41:22 瀏覽:306
拉新用什麼安卓手機可以 發布:2025-07-14 00:41:19 瀏覽:418