Professional Documents
Culture Documents
Hãy viết hàm int findSpan(const char s1[], int start, const char s2[]) để tìm từ vị trí start trong
chuỗi s1, vị trí đầu tiên xuất hiện bất kỳ ký tự nào của chuỗi s2.
- Trường hợp start < 0, hàm bắt đầu tìm từ đầu chuỗi.
- Trường hợp không tìm thấy hoặc start vượt quá chuỗi s1, hàm trả về độ dài chuỗi s1 (vị trí
kết thúc chuỗi '\0').
Yêu cầu:
- Có thể phát sinh các hàm phụ để hỗ trợ.
- Không dùng std::string hoặc các hàm trong thư viện <string.h>.
Ví dụ:
findSpan("the@quick#fox", 0, "@#") trả về 3.
findSpan("the@quick#fox", 5, "@#") trả về 9.
findSpan("the@quick#fox", 0, "!&") trả về 13.
#include <iostream>
#include <string.h>
#include <stdlib.h>
#define MAX 100
typedef struct {
int numerator, denominator;
} Fraction;
// So luong phan so
int count = 0, sizeOfFrac;
while(index != size){
sizeOfFrac = countSpan(s, index, numList);
strncpy(num[count], s + index, sizeOfFrac);
num[count++][sizeOfFrac] = '\0';
index = findSpan(s, index + sizeOfFrac, numList);
}
for (int i = 0; i < count; i++){
phanSo[i] = parseFraction(num[i]);
}
}
int main(){
char s[MAX] = "xyz2/4@#&!12@ab/6";
char num[MAX][MAX];
Fraction phanSo[MAX];
splitAndParse(s, num, phanSo);
for (int i = 0; i < 3; i++){
cout << phanSo[i].numerator << " " << phanSo[i].denominator << endl;
}
}