Problem1:给一个字符串,将其转化成相应的整数(进制天哪,我的C啊!!功底暴漏无遗)
#include<iostream> #include<cstring> #include<cstdio> using namespace std; class Integer { public: Integer(int a) { x_ = a; } Integer(char *s, int m) //其实主要是纠结在了进制的转换。。 { x_ = 0; int num ; if(s[0] >= '0' && s[0] <= '9') num = s[0] - '0'; else if(s[0] >= 'A' && s[0] <= 'Z') num = s[0] - 'A' + 35; else if(s[0] >= 'a' && s[0] <= 'z') num = s[0] - 'a' + 10; x_ = num; for(int i = 1; s[i] != '\0'; i++) { if(s[i] >= '0' && s[i] <= '9') num = s[i] - '0'; else if(s[i] >= 'A' && s[i] <= 'Z') num = s[i] - 'A' + 35; else if(s[i] >= 'a' && s[i] <= 'z') num = s[i] - 'a' + 10; x_ = x_ * m + num; } } int getValue() { return x_; } private: int x_; }; int main() { char str[100]; int numOfData, numOfStr; int data, i, radix; cin>>numOfData; for (i = 0; i < numOfData; i++){ cin>>data; Integer anInteger(data); cout<<anInteger.getValue()<<endl; } cin>>numOfStr; for (i = 0; i < numOfStr; i++){ cin>>str>>radix; Integer anInteger(str,radix); cout<<anInteger.getValue()<<endl; } return 0; }Problem2: 给出一个函数求其解(对于输出格式的控制)
#include<iostream> #include<cmath> #include<iomanip> using namespace std; class Equation { public: Equation(double a, double b, double c) { a_ = a; b_ = b; c_ = c; } void solve() { x = (-b_ + sqrt(b_*b_ - 4*a_*c_)) / 2 / a_; y = (-b_ - sqrt(b_*b_ - 4*a_*c_)) / 2 / a_; } void printRoot() { if(x >= y) cout << fixed << setprecision(2) << x << " " << setprecision(2) << y; else cout << fixed << setprecision(2) << y << " " << setprecision(2) << x; } private: double a_, b_, c_, x, y; }; int main() { double a, b, c; while (cin>>a>>b>>c) { Equation equ(a,b,c); equ.solve(); equ.printRoot(); } return 0; }