Mouvement de coordonnées avancées en langage C

Mouvement de coordonnées avancées en langage C

Développer un outil de calcul de coordonnées, A signifie se déplacer vers la gauche, D signifie se déplacer vers la droite, W signifie monter et S signifie descendre. Le point (0,0) commence à se déplacer, lit certaines coordonnées de la chaîne d'entrée et sort le résultat d'entrée final dans le fichier de sortie.
Entrée
A10; S20; W10; D30; X; A1A; B10A11 ;; A10;
sortie
10, -10

#include<stdio.h>
int main(){
    
    
	int x = 0, y = 0;
	char ch[5000];
	scanf("%s",ch);
	for (int i = 0; ch[i] != '\0'; ++i){
    
    
		if (ch[i] == 'A'){
    
    
			if ((ch[i + 1] >= 0 && ch[i + 1] <= '9') && (ch[i + 2] >= 0 && ch[i + 2] <= '9')){
    
    //移2位数
				x = x - (ch[i + 1] - 48) * 10 - (ch[i + 2] - 48);
				if (ch[i+3] != ';'){
    
    //即输入格式不正确,跳过错误格式,到下一个正确格式中,下同
					x = x + (ch[i + 1] - 48) * 10 + (ch[i + 2] - 48);
				}
				i = i + 3;
			}
			else if (ch[i + 1] >= 0 && ch[i + 1] <= '9'){
    
    //移1位数
				x = x - (ch[i + 1] - 48);
				if (ch[i+2] != ';'){
    
    
					x = x + (ch[i + 1] - 48);
				}
				i = i + 2;
			}
			else {
    
    
				while (ch[i] != ';'){
    
    

					i++;
				}
			}
		}
		else if (ch[i] == 'D'){
    
    
			if ((ch[i + 1] >= 0 && ch[i + 1] <= '9') && (ch[i + 2] >= 0 && ch[i + 2] <= '9')){
    
    
				x = x + (ch[i + 1] - 48) * 10 + (ch[i + 2] - 48);
				if (ch[i+3] != ';'){
    
    
					x = x - (ch[i + 1] - 48) * 10 - (ch[i + 2] - 48);
				}
				i = i + 3;
			}
			else if (ch[i + 1] >= 0 && ch[i + 1] <= '9'){
    
    
				x = x + (ch[i + 1] - 48);
				if (ch[i+2] != ';'){
    
    
					x = x - (ch[i + 1] - 48);
				}
				i = i + 2;
			}
			else {
    
    
				while (ch[i] != ';'){
    
    
					i++;
				}
			}
		}
		else if (ch[i] == 'W'){
    
    
			if ((ch[i + 1] >= 0 && ch[i + 1] <= '9') && (ch[i + 2] >= 0 && ch[i + 2] <= '9')){
    
    
				y = y + (ch[i + 1] - 48) * 10 + (ch[i + 2] - 48);
				if (ch[i+3] != ';'){
    
    
					y = y - (ch[i + 1] - 48) * 10 - (ch[i + 2] - 48);
				}
				i = i + 3;
			}
			else if (ch[i + 1] >= 0 && ch[i + 1] <= '9'){
    
    
				y = y + (ch[i + 1] - 48);
				if (ch[i+2] != ';'){
    
    
					y = y - (ch[i + 1] - 48);
				}
				i = i + 2;
			}
			else {
    
    
				while (ch[i] != ';'){
    
    
					i++;
				}
			}
		}
		else if (ch[i] == 'S'){
    
    
			if ((ch[i + 1] >= 0 && ch[i + 1] <= '9') && (ch[i + 2] >= 0 && ch[i + 2] <= '9')){
    
    
				y = y - (ch[i + 1] - 48) * 10 - (ch[i + 2] - 48);
				if (ch[i+3] != ';'){
    
    
					y = y + (ch[i + 1] - 48) * 10 + (ch[i + 2] - 48);
				}
				i = i + 3;
			}
			else if (ch[i + 1] >= 0 && ch[i + 1] <= '9'){
    
    
				y = y - (ch[i + 1] - 48);
				if (ch[i+2] != ';'){
    
    
					y = y + (ch[i + 1] - 48);
				}
				i = i + 2;
			}
			else {
    
    
				while (ch[i] != ';'){
    
    
					i++;
				}
			}
		}
		else{
    
    
			while (ch[i] != ';'){
    
    //当方向不是A,B,C,D是,则跳过,跳到‘;’位置
				i++;
			}
		}
	}
	printf("%d,%d\n",x,y);
	return 0;
}

Résultat de l'opération:
entrée
A10; S20; W10; D30; X; A1A; B10A11 ;; A10;
sortie
10, -10

Je suppose que tu aimes

Origine blog.csdn.net/qq_45841205/article/details/109556332
conseillé
Classement