• 练习11


    1
     1 //-------------------------------------
     2 // EX1101.cpp
     3 // 解方程
     4 //-------------------------------------
     5 #include"Root.h"
     6 #include<iostream>
     7 using namespace std;
     8 //-------------------------------------
     9 int main(){
    10   for(double a,b,c; cin>>a>>b>>c; )
    11     Root(a,b,c).solve();
    12 }//------------------------------------
    View Code
     1 //-------------------------------------
     2 // Root.h
     3 //-------------------------------------
     4 #ifndef HEADER_ROOT
     5 #define HEADER_ROOT
     6 //-------------------------------------
     7 class Root{
     8   double aa,bb,cc;
     9 public:
    10   Root(double a, double b, double c):aa(a), bb(b), cc(c){}
    11   void solve()const;
    12 };//-----------------------------------
    13 #endif   // HEADER_ROOT
    14 
    15 /*
    16 #ifndef HEADER_ROOT
    17 #define HEARDER_ROOT
    18 
    19 
    20 #endif HEADER_ROOT
    21 */
    View Code
     1 //-------------------------------------
     2 // Root.cpp
     3 //-------------------------------------
     4 #include"Root.h"
     5 #include"Complex.h"
     6 #include<cmath>  // for sqrt()
     7 //-------------------------------------
     8 void Root::solve()const{
     9   Complex x1(-bb/(2*aa)), x2(-bb/(2*aa));
    10   double delta = bb*bb - 4*aa*cc;
    11   if(delta){
    12     bool flag=delta<0;
    13     delta = (flag?-1:1)*delta;//三目运算符,-1乘以负的delta得正数
    14     delta = std::sqrt(delta)/(2*aa);
    15     if(flag)
    16         x1.im = delta, x2.im = -delta;//imag
    17     else   
    18         x1.re += delta, x2.re -= delta;//real
    19   }
    20   cout<<"The solution is
    ";
    21   cout<<"  x1 = "<<x1<<"
    ";
    22   cout<<"  x2 = "<<x2<<"
    ";
    23 }//------------------------------------
    View Code
     1 //-------------------------------------
     2 // Real.h
     3 //-------------------------------------
     4 #ifndef HEADER_REAL
     5 #define HEADER_REAL
     6 #include<iostream>
     7 using namespace std;
     8 //-------------------------------------
     9 class Real{
    10 protected:
    11   double real;
    12 public:
    13   Real(double r=0):real(r){}
    14    double(const Real& a){ return a.real; }
    15   friend Real operator+(const Real& a, const Real& b){ return a.real+b.real; }
    16   friend Real operator-(const Real& a, const Real& b){ return a.real-b.real; }
    17   friend Real operator*(const Real& a, const Real& b){ return a.real*b.real; }
    18   friend Real operator/(const Real& a, const Real& b){ return a.real/b.real; }
    19   friend bool operator==(const Real& a, const Real& b){ return a.real==b.real; }
    20   friend ostream& operator<<(ostream& cout, const Real& a){ return cout<<a.real; }
    21   //friend class Root;
    22 };//-----------------------------------
    23 #endif   // HEADER_REAL
    24      
    25 
    26  
    View Code
     1 //-------------------------------------
     2 // Real.cpp
     3 //-------------------------------------
     4 #ifndef HEADER_COMPLEX
     5 #define HEADER_COMPLEX
     6 #include<iostream>
     7 using namespace std;
     8 //-------------------------------------
     9 class Complex{
    10   double real, virt;
    11 public:
    12   Complex(double r=0, double v=0):real(r),virt(v){}
    13   friend Complex operator+(const Complex& a, const Complex& b);
    14   friend ostream& operator<<(ostream& out, const Complex& a);
    15 };//-----------------------------------
    16 #endif   // HEADER_COMPLEX
    17      
    18 
    19  
    View Code
     1 //-------------------------------------
     2 // Complex.h
     3 //-------------------------------------
     4 #ifndef HEADER_COMPLEX
     5 #define HEADER_COMPLEX
     6 #include<iostream>
     7 using namespace std;
     8 //-------------------------------------
     9 class Complex{
    10 protected:
    11   double re, im;
    12 public:
    13   Complex(double r=0, double v=0):re(r),im(v){}
    14   friend Complex operator+(const Complex& a, const Complex& b);
    15   friend ostream& operator<<(ostream& out, const Complex& a);
    16   friend class Root;
    17 };//-----------------------------------
    18 #endif   // HEADER_COMPLEX
    19      
    View Code
     1 //-------------------------------------
     2 // Complex.cpp
     3 //-------------------------------------
     4 #include"Complex.h"
     5 #include<iostream>
     6 using namespace std;
     7 //-------------------------------------
     8 ostream& operator<<(ostream& cout, const Complex& a){
     9   return (a.im ? cout<<a.re<<" + "<<a.im<<"i" : cout<<a.re);
    10 }//------------------------------------
    11 Complex operator+(const Complex& a, const Complex& b){
    12   return Complex(a.re+b.re, a.im+b.im);
    13 }//--------------------------
    View Code

     2

     1 //=====================================
     2 // EX1102_2.cpp
     3 // 大数A+B (对象版)
     4 //=====================================
     5 #include"Number.h"
     6 #include<fstream>
     7 #include<iostream>
     8 using namespace std;
     9 //-------------------------------------
    10 int main(){
    11   ifstream cin("aplusb.in");
    12   for(Number a,b; cin>>a>>b; )
    13     cout<<a+b<<"
    ";
    14 }//====================================
    15  
    View Code
     1 //=====================================
     2 // Number.cpp
     3 //=====================================
     4 #include"Number.h"
     5 #include<iostream>
     6 #include<string>
     7 #include<vector>
     8 using namespace std;
     9 //-------------------------------------
    10 void Number::add(const Number& a){
    11   for(int i=s.length()-1,tmp=0; i>=0; --i, tmp/=10){
    12     tmp += a.s[i]-'0'+ s[i]-'0';
    13     s[i] = tmp%10 + '0';
    14   }
    15 }//------------------------------------
    16 void Number::comple(string& t){                 // 十进数取补
    17   for(int i=0; i<t.length(); ++i) //取反
    18     t[i] = 105 - t[i];  // '0'+'9'=105
    19   int i=t.length()-1;
    20   while(i>=0 && t[i]=='9')        //+1
    21     t[i--]='0';
    22   t[i]++;
    23 }//------------------------------------
    24 void Number::stretch(){               // 伸展至Num位
    25   if(s[0]!='-')
    26     s = string(Num-s.length(),'0')+s;
    27   else{
    28     s[0]='0';
    29     comple(s);
    30     s = string(Num-s.length(),'9')+s;
    31   }
    32 }//------------------------------------
    33 string Number::shrink()const{                // 按有效位压缩
    34   string t(s);
    35   if(t[0]=='9')
    36     comple(t);
    37   int pos = t.find_first_not_of('0');
    38   if(pos<0) return string("0");
    39   return (s[0]=='9'? "-":"")+t.substr(pos);
    40 }//------------------------------------
    41 Number operator+(const Number& a, const Number& b){
    42   Number w(a);
    43   w.add(b);  // w+=t
    44   return w;
    45 }//------------------------------------
    46 istream& operator>>(istream& cin, Number& a){
    47   cin>>a.s;
    48   a.stretch();
    49   return cin;
    50 }//------------------------------------
    51 ostream& operator<<(ostream& cout, const Number& a){
    52   return cout<<a.shrink();
    53 }//------------------------------------
    View Code
     1 //=====================================
     2 // Number.h
     3 // 大数类
     4 //=====================================
     5 #ifndef HEADER_NUMBER
     6 #define HEADER_NUMBER
     7 #include<iostream>
     8 #include<string>
     9 using namespace std;
    10 //-------------------------------------
    11 class Number{
    12   string s;
    13   void comple(string& s);     //s取补
    14   void stretch();             //s伸展,用于初始化
    15   string shrink()const;       //s压缩
    16   enum { Num=80 };
    17 public:
    18   Number():s(Num,'0'){}
    19   Number(const string& a):s(a){ stretch(); }
    20   void add(const Number& a);  //s+=a
    21   friend Number operator+(const Number& a, const Number& b);
    22   friend istream& operator>>(istream& cin, Number& a);
    23   friend ostream& operator<<(ostream& cout, const Number& a);
    24 };//-----------------------------------
    25 #endif  // HEADER_NUMBER
    View Code
    字串是从零开始的 下标0表示第一个字符
    length 返回的实际内容的长度 不包括0
    3
     1 //=====================================
     2 // Number.cpp
     3 //=====================================
     4 #include"Number.h"
     5 #include<iostream>
     6 #include<string>
     7 using namespace std;
     8 //-------------------------------------
     9 static void sub(string& x, const string& k);
    10 //-------------------------------------
    11 string Number::div(int n)const{
    12   string p(s), res=(p[0]=='9'?"-":"");
    13   if(p[0]=='9') comple(p);
    14   int pos=p.find_first_not_of('0');
    15   if(pos<0) return string();
    16   p=p.substr(pos);
    17 
    18   string k;
    19   for( ; n; n/=10) k = char(n%10+'0')+k;
    20 
    21   if(k.length()>p.length()) return string();
    22 
    23   int m = k.length()-(p.substr(0,k.length())>=k);
    24   string x(p.substr(0, m));
    25   for(int i=m; i<p.length(); ++i){
    26     x += p[i];
    27     int count=0;
    28     for( ; x.length()>=k.length() && x>=k; count++)
    29       sub(x,k);
    30     res += char(count+'0');
    31   }
    32   return res;  // x abandoned
    33 }//------------------------------------
    34 void sub(string& x, const string& k){
    35   int tmp=0,h=x.length()-k.length();
    36   for(int i=k.length()-1; i>=0; --i){
    37     tmp=x[i+h]-k[i]-tmp;
    38     if(tmp>=0) x[i+h]=tmp+'0', tmp=0;
    39     else x[i+h]=10+tmp+'0', tmp=1;
    40   }
    41   if(tmp) x[0]--;
    42   int pos = x.find_first_not_of('0');
    43   x = (pos<0 ? string() : x.substr(pos));
    44 }//------------------------------------
    45 void Number::operator()(const Number& a){
    46   add(a);
    47   cout<<a.shrink()<<"
    ";
    48 }//------------------------------------
    49 bool operator<(const Number& a, const Number& b){
    50   if(a.s[0]=='9' && b.s[0]!='9') return true;
    51   if(a.s[0]!='9' && b.s[0]=='9') return false;
    52   return a.s<b.s;
    53 }//------------------------------------
    54 void Number::add(const Number& a){
    55   for(int i=s.length()-1,tmp=0; i>=0; --i, tmp/=10){
    56     tmp += a.s[i]-'0'+ s[i]-'0';
    57     s[i] = tmp%10 + '0';
    58   }
    59 }//------------------------------------
    60 void Number::comple(string& t){                 // 十进数取补
    61   for(int i=0; i<t.length(); ++i) //取反
    62     t[i] = 105 - t[i];  // '0'+'9'=105
    63   int i=t.length()-1;
    64   while(i>=0 && t[i]=='9')        //+1
    65     t[i--]='0';
    66   t[i]++;
    67 }//------------------------------------
    68 void Number::stretch(){               // 伸展至Num位
    69   if(s[0]!='-')
    70     s = string(Num-s.length(),'0')+s;
    71   else{
    72     s[0]='0';
    73     comple(s);
    74     s = string(Num-s.length(),'9')+s;
    75   }
    76 }//------------------------------------
    77 string Number::shrink()const{                // 按有效位压缩
    78   string t(s);
    79   if(t[0]=='9')
    80     comple(t);
    81   int pos = t.find_first_not_of('0');
    82   if(pos<0) return string("0");
    83   return (s[0]=='9'? "-":"")+t.substr(pos);
    84 }//------------------------------------
    85 Number operator+(const Number& a, const Number& b){
    86   Number w(a);
    87   w.add(b);  // s+=t
    88   return w;
    89 }//------------------------------------
    90 istream& operator>>(istream& cin, Number& a){
    91   cin>>a.s;
    92   a.stretch();
    93   return cin;
    94 }//------------------------------------
    95 ostream& operator<<(ostream& cout, const Number& a){
    96   return cout<<a.shrink();
    97 }//------------------------------------
    View Code
     1 //=====================================
     2 // Number.h
     3 // 大数类
     4 //=====================================
     5 #ifndef HEADER_NUMBER
     6 #define HEADER_NUMBER
     7 #include<iostream>
     8 #include<string>
     9 using namespace std;
    10 //-------------------------------------
    11 class Number{
    12   string s;
    13   void comple(string& s);     //s取补
    14   void stretch();             //s伸展,用于初始化
    15   string shrink()const;       //s压缩
    16   enum { Num=100 };
    17 public:
    18   Number():s(string(Num,'0')){}
    19   Number(const string& a):s(a){ stretch(); }
    20   string div(int n)const;
    21   void add(const Number& a);  //s+=a
    22   void operator()(const Number& a);
    23   friend bool operator<(const Number& a, const Number& b);
    24   friend Number operator+(const Number& a, const Number& b);
    25   friend istream& operator>>(istream& cin, Number& a);
    26   friend ostream& operator<<(ostream& cout, const Number& a);
    27 };//-----------------------------------
    28 #endif  // HEADER_NUMBER
    View Code
     1 //=====================================
     2 // EX1102_2.cpp
     3 // 大数排序+求平均数
     4 //=====================================
     5 #include"Number.h"
     6 #include<fstream>
     7 #include<iostream>
     8 #include<vector>
     9 #include<algorithm>  // sort, for_each
    10 using namespace std;
    11 //-------------------------------------
    12 int main(){
    13   ifstream cin("aplusb.in");
    14   vector<Number> x;
    15   for(Number a; cin>>a; )
    16     x.push_back(a);
    17   sort(x.begin(), x.end());
    18   Number sum = for_each(x.begin(), x.end(), Number());
    19   cout<<"Average: "<<sum.div(x.size())<<"
    ";
    20 }//====================================
    View Code
     1 //=====================================
     2 // EX1102_1.cpp
     3 // 大数排序+求平均数
     4 //=====================================
     5 #include"Number.h"
     6 #include<fstream>
     7 #include<iostream>
     8 #include<vector>
     9 #include<algorithm>  // sort
    10 using namespace std;
    11 //-------------------------------------
    12 int main(){
    13   ifstream cin("aplusb.in");
    14   vector<Number> x;
    15   for(Number a; cin>>a; )
    16     x.push_back(a);
    17   sort(x.begin(), x.end());
    18   Number sum;
    19   for(int i=0; i<x.size(); i++){
    20     sum.add(x[i]);
    21     cout<<x[i]<<"
    ";
    22   }
    23   cout<<"Average: "<<sum.div(x.size())<<"
    ";
    24 }//====================================
    View Code
     
  • 相关阅读:
    WPF之元素绑定
    BC9050的IP地址设置
    C#之进程、线程
    EPLAN学习笔记01
    Beckhoff模拟量模块的使用
    倍福-基于EL2521的NC轴控制
    kali之ARP欺骗获取图片流
    PHP脚本实现凯撒加(解)密
    SQL注入(一)普通型注入
    linux卸载不完全
  • 原文地址:https://www.cnblogs.com/herizai/p/3145036.html
Copyright © 2020-2023  润新知