欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C語言

當(dāng)前位置:主頁 > 軟件編程 > C語言 >

C語言 坐標(biāo)移動(dòng)詳解及實(shí)例代碼

來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C語言|點(diǎn)擊: 次

題目描述

開發(fā)一個(gè)坐標(biāo)計(jì)算工具, A表示向左移動(dòng),D表示向右移動(dòng),W表示向上移動(dòng),S表示向下移動(dòng)。從(0,0)點(diǎn)開始移動(dòng),從輸入字符串里面讀取一些坐標(biāo),并將最終輸入結(jié)果輸出到輸出文件里面。

 輸入: 

合法坐標(biāo)為A(或者D或者W或者S) + 數(shù)字(兩位以內(nèi)) 

坐標(biāo)之間以;分隔。

 非法坐標(biāo)點(diǎn)需要進(jìn)行丟棄。如AA10;  A1A;  $%$;  YAD; 等。

 下面是一個(gè)簡單的例子 如: 

A10;S20;W10;D30;X;A1A;B10A11;;A10; 

處理過程: 

起點(diǎn)(0,0)
 
+  A10  = (-10,0)
 
+  S20  = (-10,-20)
 
+  W10 = (-10,-10)
 
+  D30 = (20,-10)
 
+  x  = 無效
 
+  A1A  = 無效
 
+  B10A11  = 無效
 
+ 一個(gè)空 不影響
 
+  A10 = (10,-10)
 

 結(jié)果 (10, -10)

輸入描述:

一行字符串

輸出描述:

最終坐標(biāo),以,分隔

輸入例子:

A10;S20;W10;D30;X;A1A;B10A11;;A10;

輸出例子:

10,-10

Code:

#include<iostream> 
#include<string> 
using namespace std; 
 
bool isValid(string s, char &key, int &step){ 
  if (s.size()<2 || s.size()>3)return false; 
  if (s[0] != 'A' && s[0] != 'D' && s[0] != 'W' && s[0] != 'S') 
    return false; 
  key = s[0]; 
  if (s.size() == 2 && s[1] >= '0' && s[1] <= '9'){ 
    step = s[1] - '0'; 
    return true; 
  } 
  if (s.size() == 3 && s[1] >= '0' && s[1] <= '9' && s[2] >= '0' && s[2] <= '9'){ 
    step = (s[1] - '0') * 10 + (s[2] - '0'); 
    return true; 
  } 
  return false; 
} 
 
 
void caculator(string s, int &x, int &y, char key, int step){ 
  switch (key){ 
  case 'A': 
    x -= step; 
    break; 
  case 'D': 
    x += step; 
    break; 
  case 'W': 
    y += step; 
    break; 
  case 'S': 
    y -= step; 
    break; 
  } 
  return; 
} 
 
int main(){ 
  string str; 
  while (cin >> str){ 
    int x = 0; 
    int y = 0; 
    int i = 0; 
    while (i<str.size()){ 
      string temp; 
      char key; 
      int step; 
      while (str[i] != ';'){ 
        temp.push_back(str[i]); 
        i++; 
      } 
      if (isValid(temp, key, step)) 
        caculator(temp, x, y, key, step); 
      i++; 
    } 
    cout << x << ',' << y<<endl; //must add endl(wtf...,waste time) 
  } 
  return 0; 
} 

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

上一篇:C++中求余運(yùn)算符(%)示例詳解

欄    目:C語言

下一篇:C++派生訪問說明符小記(推薦)

本文標(biāo)題:C語言 坐標(biāo)移動(dòng)詳解及實(shí)例代碼

本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/1810.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有