Hướng dẫn subtracting characters in c++ - trừ các ký tự trong c ++

Xin chào, tôi muốn biết cách lập đơn chuỗi từ chuỗi

Ví dụ

Nếu chuỗi s = "124ab" Tôi có thể dễ dàng trích xuất số nguyên bằng cách sử dụng SStream nhưng tôi không biết cách trích xuất chuỗi

Tôi muốn trích xuất AB từ S; Tôi có hàng tấn chuỗi và chúng không có bất kỳ quy tắc nào. S có thể là "3456fdhgab" hoặc "34a678"

Đã hỏi ngày 6 tháng 5 lúc 8:05May 6 at 8:05

12

Bạn có thể sử dụng

ab
9 để kiểm tra xem một ký tự là một chữ số. Bạn có thể sử dụng thành ngữ xóa Erase để xóa các ký tự là chữ số.

Bởi vì

ab
9 có quá tải nên nó phải được bọc trong một lambda để được sử dụng trong thuật toán:

#include 
#include 
#include 
#include 

int main() {
    std::string inp{"124ab"};
    inp.erase(std::remove_if(inp.begin(),inp.end(),[](char c){return std::isdigit(c);}),inp.end());
    std::cout << inp;
}

Output:

ab

Và bởi vì bạn đã yêu cầu sử dụng một chuỗi chuỗi, đây là cách bạn có thể trích xuất các chữ số với một tùy chỉnh

#include 
#include 
#include 
#include 

struct only_non_digits {
    std::string& data;    
};

std::ostream& operator<<(std::ostream& os, const only_non_digits& x) {
    for (const auto& c : x.data){
        if (std::isdigit(c)) continue;
        os << c;
    }
    return os;
}    

int main() {
    std::string inp{"124ab"};
    std::cout << only_non_digits{inp} << "\n";
    std::stringstream ss;
    ss << only_non_digits{inp};
    std::cout << ss.str() << "\n";
}
1:

#include 
#include 
#include 
#include 

struct only_non_digits {
    std::string& data;    
};

std::ostream& operator<<(std::ostream& os, const only_non_digits& x) {
    for (const auto& c : x.data){
        if (std::isdigit(c)) continue;
        os << c;
    }
    return os;
}    

int main() {
    std::string inp{"124ab"};
    std::cout << only_non_digits{inp} << "\n";
    std::stringstream ss;
    ss << only_non_digits{inp};
    std::cout << ss.str() << "\n";
}

Output:

ab
ab

Đã trả lời ngày 6 tháng 5 lúc 8:41May 6 at 8:41

Hướng dẫn subtracting characters in c++ - trừ các ký tự trong c ++

0

string removeNumbers(string str)
{
    int current = 0;
    for (int i = 0; i < str.length(); i++)
    {
        if (!isdigit(str[i])) {
            str[current] = str[i];
            current++;
        }
    }
    return str.substr(0, current);
}

int main()
{
     string str;
     cout <<"Enter the string : " <

Đã trả lời ngày 6 tháng 5 lúc 8:26May 6 at 8:26

4

Trừ (phức tạp, đôi)

Trừ một số thực có độ chính xác kép từ một số phức và trả về kết quả.

Thông số

phức tạp

  • Giá trị phức tạp để trừ từ (minuend).

đúng gấp đôi

Giá trị thực có độ chính xác kép để trừ (Subtrahend).

Trả lại

Tổ hợp

Kết quả của việc trừ #include #include #include #include struct only_non_digits { std::string& data; }; std::ostream& operator<<(std::ostream& os, const only_non_digits& x) { for (const auto& c : x.data){ if (std::isdigit(c)) continue; os << c; } return os; } int main() { std::string inp{"124ab"}; std::cout << only_non_digits{inp} << "\n"; std::stringstream ss; ss << only_non_digits{inp}; std::cout << ss.str() << "\n"; } 2 từ #include #include #include #include struct only_non_digits { std::string& data; }; std::ostream& operator<<(std::ostream& os, const only_non_digits& x) { for (const auto& c : x.data){ if (std::isdigit(c)) continue; os << c; } return os; } int main() { std::string inp{"124ab"}; std::cout << only_non_digits{inp} << "\n"; std::stringstream ss; ss << only_non_digits{inp}; std::cout << ss.str() << "\n"; } 3, dưới dạng một số phức.

Nhận xét

using System;
using System.Numerics;

public class Example
{
   public static void Main()
   {
      Complex c1 = new Complex(4.93, 6.87);
      Complex[] values = { new Complex(12.5, 9.6),
                           new Complex(4.3, -8.1),
                           new Complex(-1.9, 7.4),
                           new Complex(-5.3, -6.6) };

      foreach (var c2 in values)
         Console.WriteLine("{0} - {1} = {2}", c1, c2,
                           Complex.Subtract(c1, c2));
   }
}
// The example displays the following output:
//       (4.93, 6.87) - (12.5, 9.6) = (-7.57, -2.73)
//       (4.93, 6.87) - (4.3, -8.1) = (0.63, 14.97)
//       (4.93, 6.87) - (-1.9, 7.4) = (6.83, -0.53)
//       (4.93, 6.87) - (-5.3, -6.6) = (10.23, 13.47)
Imports System.Numerics

Module Example
   Public Sub Main()
      Dim c1 As New Complex(4.93, 6.87)
      Dim values() As Complex = { New Complex(12.5, 9.6), 
                                  New Complex(4.3, -8.1), 
                                  New Complex(-1.9, 7.4), 
                                  New Complex(-5.3, -6.6) }

      For Each c2 In values
         Console.WriteLine("{0} - {1} = {2}", c1, c2, 
                           Complex.Subtract(c1, c2))
      Next
   End Sub
End Module
' The example displays the following output:
'       (4.93, 6.87) - (12.5, 9.6) = (-7.57, -2.73)
'       (4.93, 6.87) - (4.3, -8.1) = (0.63, 14.97)
'       (4.93, 6.87) - (-1.9, 7.4) = (6.83, -0.53)
'       (4.93, 6.87) - (-5.3, -6.6) = (10.23, 13.47)

Nhận xét

Việc trừ một số thực (có thể được coi là số phức C + 0i) từ một số phức (A + BI) có dạng sau:

(A - C) + BI

Các ngôn ngữ hỗ trợ các toán tử tùy chỉnh có thể sử dụng toán tử tương đương phức tạp (phức tạp, gấp đôi).

Các phương thức trừ nhận một nhân đôi hiệu quả hơn phương thức nhận được hai số phức.

Trừ (phức tạp, phức tạp)

Trừ một số phức từ một số khác và trả về kết quả.

public:
 static System::Numerics::Complex Subtract(System::Numerics::Complex left, System::Numerics::Complex right);
public static System.Numerics.Complex Subtract (System.Numerics.Complex left, System.Numerics.Complex right);
static member Subtract : System.Numerics.Complex * System.Numerics.Complex -> System.Numerics.Complex
ab
0

Thông số

phức tạp Complex

Giá trị để trừ từ (minuend).

đúng phức tạp Complex

Giá trị để trừ (Subtrahend).

Trả lại

Tổ hợp

Kết quả của việc trừ

#include 
#include 
#include 
#include 

struct only_non_digits {
    std::string& data;    
};

std::ostream& operator<<(std::ostream& os, const only_non_digits& x) {
    for (const auto& c : x.data){
        if (std::isdigit(c)) continue;
        os << c;
    }
    return os;
}    

int main() {
    std::string inp{"124ab"};
    std::cout << only_non_digits{inp} << "\n";
    std::stringstream ss;
    ss << only_non_digits{inp};
    std::cout << ss.str() << "\n";
}
2 từ
#include 
#include 
#include 
#include 

struct only_non_digits {
    std::string& data;    
};

std::ostream& operator<<(std::ostream& os, const only_non_digits& x) {
    for (const auto& c : x.data){
        if (std::isdigit(c)) continue;
        os << c;
    }
    return os;
}    

int main() {
    std::string inp{"124ab"};
    std::cout << only_non_digits{inp} << "\n";
    std::stringstream ss;
    ss << only_non_digits{inp};
    std::cout << ss.str() << "\n";
}
3.

Nhận xét

Phép trừ của một số phức, C + DI, từ một số phức, A + BI, có dạng sau:

(A - C) + (B - D) I

Các ngôn ngữ hỗ trợ các toán tử tùy chỉnh có thể sử dụng toán tử tương đương phức tạp (phức tạp, phức tạp).

Xem thêm

  • Phép trừ (phức tạp, phức tạp)

Áp dụng cho

Trừ (đôi, phức tạp)

Trừ một số phức từ số thực có độ chính xác kép và trả về kết quả.

ab
1
ab
2
ab
3
ab
4

Thông số

phức tạp Double

Giá trị để trừ từ (minuend).

đúng phức tạp Complex

Giá trị để trừ (Subtrahend).

Trả lại

Tổ hợp

Kết quả của việc trừ

#include 
#include 
#include 
#include 

struct only_non_digits {
    std::string& data;    
};

std::ostream& operator<<(std::ostream& os, const only_non_digits& x) {
    for (const auto& c : x.data){
        if (std::isdigit(c)) continue;
        os << c;
    }
    return os;
}    

int main() {
    std::string inp{"124ab"};
    std::cout << only_non_digits{inp} << "\n";
    std::stringstream ss;
    ss << only_non_digits{inp};
    std::cout << ss.str() << "\n";
}
2 từ
#include 
#include 
#include 
#include 

struct only_non_digits {
    std::string& data;    
};

std::ostream& operator<<(std::ostream& os, const only_non_digits& x) {
    for (const auto& c : x.data){
        if (std::isdigit(c)) continue;
        os << c;
    }
    return os;
}    

int main() {
    std::string inp{"124ab"};
    std::cout << only_non_digits{inp} << "\n";
    std::stringstream ss;
    ss << only_non_digits{inp};
    std::cout << ss.str() << "\n";
}
3.

Nhận xét

Phép trừ của một số phức, C + DI, từ một số phức, A + BI, có dạng sau:

(A - C) + (B - D) I

Các ngôn ngữ hỗ trợ các toán tử tùy chỉnh có thể sử dụng toán tử tương đương phức tạp (phức tạp, phức tạp).

Xem thêm

  • Phép trừ (phức tạp, phức tạp)

Áp dụng cho

Trừ (đôi, phức tạp)

Trừ một số phức từ số thực có độ chính xác kép và trả về kết quả.

ab
5
ab
6
ab
7
ab
8

Thông số

phức tạp Complex

Giá trị để trừ từ (minuend).

đúng phức tạp Double

Giá trị để trừ (Subtrahend).

Trả lại

Tổ hợp

Kết quả của việc trừ

#include 
#include 
#include 
#include 

struct only_non_digits {
    std::string& data;    
};

std::ostream& operator<<(std::ostream& os, const only_non_digits& x) {
    for (const auto& c : x.data){
        if (std::isdigit(c)) continue;
        os << c;
    }
    return os;
}    

int main() {
    std::string inp{"124ab"};
    std::cout << only_non_digits{inp} << "\n";
    std::stringstream ss;
    ss << only_non_digits{inp};
    std::cout << ss.str() << "\n";
}
2 từ
#include 
#include 
#include 
#include 

struct only_non_digits {
    std::string& data;    
};

std::ostream& operator<<(std::ostream& os, const only_non_digits& x) {
    for (const auto& c : x.data){
        if (std::isdigit(c)) continue;
        os << c;
    }
    return os;
}    

int main() {
    std::string inp{"124ab"};
    std::cout << only_non_digits{inp} << "\n";
    std::stringstream ss;
    ss << only_non_digits{inp};
    std::cout << ss.str() << "\n";
}
3.

Nhận xét

Phép trừ của một số phức, C + DI, từ một số phức, A + BI, có dạng sau:

(A - C) + (B - D) I

Các ngôn ngữ hỗ trợ các toán tử tùy chỉnh có thể sử dụng toán tử tương đương phức tạp (phức tạp, phức tạp).

Xem thêm

  • Phép trừ (phức tạp, phức tạp)

Áp dụng cho