Skip to content

Instantly share code, notes, and snippets.

@chuongmep
Created July 6, 2022 09:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chuongmep/b2485439a9eb53528af78bd7cd40072d to your computer and use it in GitHub Desktop.
Save chuongmep/b2485439a9eb53528af78bd7cd40072d to your computer and use it in GitHub Desktop.
# Way 1
a = "12354+656565865"
num1 = ""
num2 = ""
index = 1
for i in range(len(a)):
if str.isdigit(a[i]):
num1 += a[i]
index += 1
else:
for i in range(index,len(a)):
num2 += a[i]
break
print(num1,num2)
# Way 2
a = "12354+656565865"
num1 = ""
num2 = ""
index = 1
for i in range(len(a)):
if str.isdigit(a[i]):
num1 += a[i]
index += 1
else:
print(index)
num2 = a[index:len(a)]
break
print(num1,num2)
# way 3
a = "12354+65656-5865"
num1 = ""
num2 = ""
index = 1
#regex match number
import re
print(re.findall(r'\d+', a))
@vinh-hinody
Copy link

solution c++ :

void slpit2(char str[], char left[], char right[], char dau) {
	int index=0;
	for(index; index<strlen(str) ; index++) {
		if(isdigita(str[index])) {
			left[index] = str[index];
			cout<<"lesft: "<<str[index];
		} else {
			index += 1;
			int inj = 0;
			for(index; index<strlen(str);index++){
				right[inj] = str[index];
				inj += 1;
			}
			return;
		}	
	}
}

@vinh-hinody
Copy link

void slpit3(char str[], char left[], char right[], char dau){
	int n = 0;
	int ktra= 0;
	for(int i = 0; i<strlen(str); i++){
		if(isdigita(str[i]) && ktra == 0){
			left[i] = str[i];
			cout<<left[i];
		} else if(!isdigita(str[i])){
			ktra = -1;
			left[i] = NULL;
			cout<<"\n";
		} else if(ktra == -1){
			right[n] = str[i];	
			cout<<right[n];
			n++;	
		}
	}
}

@vinh-hinody
Copy link

CALCULATOR

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

using namespace std;

int characterToNumber(int character);

int main() {
	char str[] = "999";
	int number[100];
	float res[100];
	int intt = 0;
	char sign[100];
	int indexNum = 0,indexSign=0;
	int a=0;
	int i = 0;
	
	cout<<"String srouce: "<<str;

//	CHON DAU DAU TIEN
	if(isdigit(str[0])==1) {
		sign[0] = '+';
		indexSign +=1;
	}

//	TACH CHUOI
	for(i; i< strlen(str); i++) {
		if(isdigit(str[i])==0) {
			sign[indexSign] = str[i];
			indexSign += 1;
		} else {
			if(isdigit(str[i-1])==0) {
				a = 0;
				a = a*10+characterToNumber(str[i]);
			} else
				a = a*10+characterToNumber(str[i]);
			if(isdigit(str[i+1])==0) {
				number[indexNum] = a;
				indexNum += 1;
			}

		}
	}

//	DOI DAU
	for(int u = 0; u<indexSign; u++) {
		if(sign[u]=='-') {
			number[u] *= -1;
			sign[u] = '+';
		}
	}
	
//	NHAN / CHIA / CONG / TRU
	for(int i = 0; i<indexSign; i++) {
		if(sign[i]=='+') {
			if(sign[i+1]=='+') {
				res[intt] = number[i];
				intt +=1;
			}
			else if(sign[i+1]==NULL){
				res[intt] = number[i];
				intt +=1;
			}
			if(sign[i+1]=='*'||sign[i+1]=='/') {
				float s = number[i];
				i += 1;
				for(i; i<indexNum; i++) {
					if(sign[i]=='+'){
						break;
					}
					
					if(sign[i]=='*') {
						s *= number[i];
						continue;
					}
					if(sign[i]=='/'){
						s /= number[i];
						continue;
					}
				}
				res[intt] = s;
				intt++;
				i -= 1;
			}
		}
	}
//	IN KQ:
	float aa = 0;
	for(int i = 0; i<intt; i++) {
		aa += res[i];
	}
	
	cout<<"\nResult: "<<aa;
	
}

int characterToNumber(int character) {
	if(character=='1')return 1;
	else if(character=='2')return 2;
	else if(character=='3')return 3;
	else if(character=='4')return 4;
	else if(character=='5')return 5;
	else if(character=='6')return 6;
	else if(character=='7')return 7;
	else if(character=='8')return 8;
	else if(character=='9')return 9;
	else if(character=='0')return 0;
	else return -1;
}

@chuongmep
Copy link
Author

a = "235+2*5*13/20+6*2"
import re
m = re.split(r'[^\d]',a)
print(m)
n = re.split(r'\d+',a)
def mul(m):
    s = 1
    for i in m:
        s *= int(i)
    return s
def div(m):
    s = 1
    for i in m:
        s /= int(i)
    return s
def sum(m):
    s = 1
    for i in m:
        s += int(i)
    return s
def sub(m):
    s = 1
    for i in m:
        s -= int(i)
    return s
def changetonum(str):
    return int(str)
print(n)

Output :
m = ['235', '2', '5', '13', '20', '6', '2']
n = ['', '+', '', '', '/', '+', '*', '']

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment