C编程从整数中删除数字

时间:2017-09-21 01:39:59

标签: c

是否可以从整数中删除数字?
例如,如果我想删除所有 偶数123658的数字,并留下
135我怎么能这样做?

这是一个赋值,我不能使用数组,字符变量 或Math.h函数。

这是我到目前为止编写的代码

package client;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Collections;
import java.util.StringTokenizer;



public class ClientBackground implements Runnable{

    Socket socket;
    private DataInputStream in;
    private DataOutputStream out;
    private ClientGui gui;
    private String msg;
    String id;
    private String pass;
    private boolean islogin;
    private login_Frame lf;
    private String info;
    String packet="";
    String tmp;


    public final void setGui(ClientGui gui) {
        this.gui = gui;
    }

    public void run() {
        try {
            socket = new Socket("127.0.0.1", 7777);
            System.out.println("connect.");
            out = new DataOutputStream(socket.getOutputStream());
            in = new DataInputStream(socket.getInputStream());    
            System.out.println("message success");
            while(in!=null){
                packet = in.readUTF();
                System.out.println(packet.equals(null));
                System.out.println(packet);
                StringTokenizer st = new StringTokenizer(packet,"/");
                tmp = st.nextToken();
                msg = st.nextToken();
                int protocol = Integer.parseInt(tmp);
                switch(protocol){

                case 3000 :{
                    System.out.println("success");
                    this.lf.test = true;
                }
                break;

                case 3001 :{
                    System.out.println("wrong!");
                    this.lf.test = false;
                }
                break;

                default :{
                    gui.jta.append(msg);
                    System.out.println(msg);
                }
                break;
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        ClientBackground clientBackground = new ClientBackground();
        new Thread(clientBackground).start();
        clientBackground.lf = new login_Frame();
        clientBackground.lf.Clientback(clientBackground);


    }
    public void showFrameTest(){
        this.lf.dispose(); 
        this.gui = new ClientGui();
        this.gui.Clientback(this);
    }

    public void sendMessage(String msg2) {
        try {
            out.writeUTF(msg2);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public boolean lcheck() {
        return islogin;
    }

    public void setid(String id, String pass) {
        this.id = id;
        this.pass = pass;
    }
    public String getid() {
        return id;
    }

}

2 个答案:

答案 0 :(得分:0)

  

例如,如果我想删除123658中的所有偶数,请将其保留   135我怎么能这样做?   ...不能使用数组,字符变量或Math.h函数。

OP的代码有些接近。

doc.pipe fs.createWriteStream('/path/to/file.pdf') # write to PDF doc.pipe res # HTTP response # add stuff to PDF here using methods described below... # finalize the PDF and end the stream doc.end(); 让我想起了BASIC。在C中,phaseInt = 0;代替值。

我确信一个非递归的解决方案,像OP这样的东西可以编写,但递归在这里很紧张。

伪递归代码不能全部放弃

return

@BLUEPIXY所述,全偶数位数需要返回号。希望0没问题。

或非递归

uint32_t PhaseInt_SansEven(uint32_t n) {
  if (has_a_small_value_that_needs_no_recursion_and_can_return_0(n)) {
    return 0;
  }

  uint32_t msdigits = all_digits_except_the_least(n);
  uint32_t lsdigit = the_least_digit(n);

  // If odd digit
  if (test_for_oddness(lsdigit) {
    // How to "append" the left digits and the 1 right one
    return PhaseInt_SansEven(msdigits) * TBD1 + lsdigit;
  } else {
    // How to "append" the left digits and ""
    return PhaseInt_SansEven(msdigits) + TBD2;
  }
}

答案 1 :(得分:0)

我没有看到在这里使用递归的必要性,它只会使事情变得复杂 - 尽管 - 你可以使用简单的while循环和if语句:

int digit,input,output = 0,currentFactor=1;
//read input
while(input!=0)
{
    digit = input % 10; //read digit
    input = input / 10; //remove digit from input

    if(digit % 2 == 1) //if digit is odd
    {
        output +=  digit*currentFactor; //add digit to output at the position determined by factor
        currentFactor *= 10; //move the position by one digit (multiply factor by 10)
    }
}
return output;