精品偷拍一区二区三区,亚洲精品永久 码,亚洲综合日韩精品欧美国产,亚洲国产日韩a在线亚洲

  • <center id="usuqs"></center>
  • 
    
  • C++長整數、高精度計算器

    C++長整數、高精度計算器
    設計一個程序實現兩個任意長的整數(包括正數和負數)、任意精度實數的算術運算.

    要求:
    (1)用動態(tài)鏈表存貯數據,每結點含一個整型變量,表示若干位數.
    (2)整數輸入和輸出按中國對于長整數的習慣表示,每3位1組,組間用逗號隔開.
    (3)實現加、減運算.
    (4)程序運行界面清晰實用.
    其他人氣:800 ℃時間:2019-09-09 18:38:50
    優(yōu)質解答
    dos 界面
    我剛編過,實現加減乘除:從文件讀取數字和符號,文件格式是
    1000
    2000
    +
    299
    188
    -
    這樣的:
    程序如下,你可以按你要求修改:
    //read a file name
    //the file contains nonnegtive integers and operators including + - *
    //the format is two lines of integers and a line of operator
    //the integers can be arbitrarily large
    #include
    #include
    #include
    using namespace std;
    struct Node {
    int digit;
    Node* next;
    Node* previous;
    };
    void insert(Node* &h,Node* &e,int num)
    {
    Node* p=h;
    if(!p){
    Node*temp=new Node;
    temp->digit=num;
    temp->next=NULL;
    temp->previous=NULL;
    h=temp;
    e=temp;
    return ;
    }
    while(p->next){
    p=p->next;
    }
    Node* temp=new Node;
    temp->digit=num;
    temp->next=NULL;
    temp->previous=p;
    p->next=temp;
    e=temp;
    return ;
    }
    Node* add(Node *e1,Node* e2)
    {
    Node*p1=e1;
    Node*p2=e2;
    Node*p3=NULL;
    int carry=0;
    int digit=0;
    while(p1&&p2){
    digit=p1->digit+p2->digit+carry;
    carry=digit/10;
    digit%=10;
    Node* temp=new Node;
    temp->next=NULL;
    temp->digit=digit;
    temp->next=p3;
    p3=temp;
    p1=p1->previous;
    p2=p2->previous;
    }
    while(p1){
    digit=p1->digit+carry;
    carry=digit/10;
    digit%=10;
    Node* temp=new Node;
    temp->digit=digit;
    temp->next=p3;
    p3=temp;
    p1=p1->previous;
    }
    while(p2){
    int digit=p2->digit+carry;
    carry=digit/10;
    digit%=10;
    Node* temp=new Node;
    temp->digit=digit;
    temp->next=p3;
    p3=temp;
    p2=p2->previous;
    }
    if(carry){
    Node* temp=new Node;
    temp->digit=carry;
    temp->next=p3;
    p3=temp;
    }
    return p3;
    }
    Node* multiply(Node* h1,Node* e1,Node* h2,Node* e2)
    {
    Node* p2=NULL;
    Node* pos=NULL;
    Node* p=NULL;
    Node* pre=NULL;
    int digit=0;
    int carry;
    while(e2){
    carry=0;
    p=e1;
    pre=pos;
    p2=pos;
    while(p){
    digit=(e2->digit*p->digit+carry);
    if(p2){
    digit+=p2->digit;
    p2->digit=digit%10;
    carry=digit/10;
    }
    else if(!p2&&!pre){
    Node* temp=new Node;
    temp->next=NULL;
    temp->previous=NULL;
    temp->digit=digit%10;
    carry=digit/10;
    p2=temp;
    pos=temp;
    }
    else{
    Node* temp=new Node;
    temp->next=pre;
    temp->previous=NULL;
    pre->previous=temp;
    temp->digit=digit%10;
    carry=digit/10;
    p2=temp;
    }
    p=p->previous;
    pre=p2;
    p2=p2->previous;
    }
    if(carry){
    Node* temp=new Node;
    temp->digit=carry;
    temp->next=pre;
    pre->previous=temp;
    temp->previous=NULL;
    p2=temp;
    pre=p2;
    }
    pos=pos->previous;
    e2=e2->previous;
    }
    return pre;
    }
    bool cmp_digit(Node* e1,Node* h1,Node* e2,Node* h2)
    {
    int n_digit1=0;
    int n_digit2=0;
    Node* p1=e1;
    Node* p2=e2;
    Node* q1=h1;
    Node* q2=h2;
    while(p1){
    n_digit1++;
    p1=p1->previous;
    }
    while(p2){
    n_digit2++;
    p2=p2->previous;
    }
    if(n_digit1>n_digit2){
    return true;
    }
    else if(n_digit1digit>q2->digit){
    return true;
    }
    else if(q1->digitdigit){
    return false;
    }
    else{
    q1=q1->next;
    q2=q2->next;
    }
    }
    }
    return true;
    }
    Node* subtract(Node* h1,Node* e1,Node* h2,Node* e2)
    {
    Node* p1h=h1;
    Node* p1e=e1;
    Node* p2h=h2;
    Node* p2e=e2;
    int digit=0;
    bool flag=(cmp_digit(p1e,p1h,p2e,p2h));
    if(flag==true){
    Node* p3=NULL;
    while(e2){
    if(e2->digit>e1->digit){
    digit=10+e1->digit-e2->digit;
    e1->previous->digit--;
    Node *temp=new Node;
    temp->digit=digit;
    temp->next=p3;
    p3=temp;
    }
    else{
    digit=e1->digit-e2->digit;
    Node* temp=new Node;
    temp->digit=digit;
    temp->next=p3;
    p3=temp;
    }
    e2=e2->previous;
    e1=e1->previous;
    }
    while(e1){
    Node *temp=new Node;
    if(e1->digitdigit+=10;
    e1->previous->digit--;
    }
    temp->digit=e1->digit;
    temp->next=p3;
    p3=temp;
    e1=e1->previous;
    }
    return p3;
    }
    else{
    Node* p3=NULL;
    while(e1){
    if(e1->digit>e2->digit){
    digit=10+e2->digit-e1->digit;
    e2->previous->digit--;
    Node *temp=new Node;
    temp->digit=digit;
    temp->next=p3;
    p3=temp;
    }
    else{
    digit=e2->digit-e1->digit;
    Node* temp=new Node;
    temp->digit=digit;
    temp->next=p3;
    p3=temp;
    }
    e2=e2->previous;
    e1=e1->previous;
    }
    while(e2){
    Node *temp=new Node;
    if(e2->digitdigit+=10;
    e2->previous->digit--;
    }
    temp->digit=e2->digit;
    temp->next=p3;
    p3=temp;
    e2=e2->previous;
    }
    return p3;
    }
    }
    void output(Node* h)
    {
    Node*p=h;
    while(p&&p->digit==0){
    p=p->next;
    }
    if(!p){
    cout
    我來回答
    類似推薦
    請使用1024x768 IE6.0或更高版本瀏覽器瀏覽本站點,以保證最佳閱讀效果。本頁提供作業(yè)小助手,一起搜作業(yè)以及作業(yè)好幫手最新版!
    版權所有 CopyRight © 2012-2024 作業(yè)小助手 All Rights Reserved. 手機版