1. 程式人生 > >一個DirectSound的例子,即錄即放

一個DirectSound的例子,即錄即放

部分程式碼
--------------------------------------------------------------------------
#include "sound.h"
LPDIRECTSOUND8 lpDirectSound =NULL;       //DirectSound 裝置
LPDIRECTSOUNDBUFFER8 lpDSBuffer8 = NULL; //播放緩衝區(第二緩衝區)
HANDLE soundEvent[3];    //播放通知
LPDIRECTSOUNDCAPTURE8        lpDSCapture = NULL;//捕獲裝置物件指標
LPDIRECTSOUNDCAPTUREBUFFER8   lpDSBCapture = NULL;//捕獲緩衝區物件指標
HANDLE captureEvent[3];    //播放通知
//int   FreeBufferSectionNum =0;   //第幾段的 播放緩衝可以寫了
int   SoundBufferLength=0;     //播放緩衝的總長度, 一個緩衝設定分成3段,設定3個通知訊號
int   BufferSectionLength=0; //每一段的長度 BYTE   SwapBuffer[29400] ;//應該是SwapBuffer[BufferSectionLength];   ,
                                       //不過我懶的動態申請了,所以固定了,用於把臨時儲存捕獲的音訊資料,
                                        //在從這裡複製到播放快取裡面去。 其實直接複製也是可以的,不過加多一個快取在這裡,便於處理啊,網路傳送等等。
                          
//CWaveFile   waveFile= NULL;  
bool SoundCreate(HWND hwnd )
{      HRESULT hr = DirectSoundCreate8(NULL, & lpDirectSound, NULL); if (FAILED(hr))
     {    
           MessageBox(hwnd,_T("建立DirectSound介面失敗。"),_T("widebright"),MB_OK);    // Add error-handling here.
        return false;
     }      hr = lpDirectSound->SetCooperativeLevel(hwnd, DSSCL_PRIORITY   ); //DSSCL_NORMAL      if (FAILED(hr))
     {    
     MessageBox(hwnd,_T("設定DirectSound協作級別失敗。"),_T("widebright"),MB_OK);    // Add error-handling here.
           lpDirectSound->Release ();    // Add error-handling here.
        return false;
     } hr =   CreateBasicBuffer(lpDirectSound ,& lpDSBuffer8);      if (FAILED(hr))
     {    
     MessageBox(hwnd,_T("建立DirectSound聲音播放緩衝區失敗。"),_T("widebright"),MB_OK);    // Add error-handling here.
           lpDirectSound->Release ();    // Add error-handling here.
        return false;
     } //g_pDSBuffer8->Lock(0,0,&lplockbuf,&len,NULL,NULL,DSBLOCK_ENTIREBUFFER);
//g_pWaveFile->Read((BYTE*)lplockbuf,len,&dwWrite);
//g_pDSBuffer8->Unlock(lplockbuf,len,NULL,0);
//g_pDSBuffer8->SetCurrentPosition(0);
//g_pDSBuffer8->Play(0,0,DSBPLAY_LOOPING);
   return true;
} //建立 播放緩衝
//
//The example function creates a streaming buffer large enough to hold 3 seconds of streaming data. Nonstreaming buffers should be made just large enough to accommodate the entire sound.
//
//The DSBCAPS_GLOBALFOCUS flag in the example ensures that the buffer will continue playing even when the application window is not in the foreground. Without this flag, the buffer will be muted when another application or even a dialog box has the input focus.
//
//If the location of a buffer is not specified, DirectSound places it in hardware-controlled memory if possible. Because hardware buffers are mixed by the sound card processor, they have much less impact on application performance.
//
//If you wish to specify the location of a buffer rather than letting DirectSound decide where it belongs, set either the DSBCAPS_LOCHARDWARE or DSBCAPS_LOCSOFTWARE flag in the DSBUFFERDESC structure. If the DSBCAPS_LOCHARDWARE flag is set and there are insufficient hardware resources, the buffer creation request fails.
//
//To take advantage of the voice management features of DirectSound, specify the DSBCAPS_LOCDEFER flag when creating the buffer. This flag defers the allocation of resources for the buffer until it is played. For more information, see Dynamic Voice Management.
//
//You can ascertain the location of an existing buffer by using the IDirectSoundBuffer8::GetCaps method and checking the dwFlags member of the DSBCAPS structure for either the DSBCAPS_LOCHARDWARE or DSBCAPS_LOCSOFTWARE flags. One or the other is always specified.
//
//Buffer objects are owned by the device object that created them. When the device object is released, all buffers created by that object are also released and should not be referenced.
HRESULT CreateBasicBuffer(LPDIRECTSOUND8 lpDirectSound, LPDIRECTSOUNDBUFFER8* ppDsb8)
{
   WAVEFORMATEX wfx;
   DSBUFFERDESC dsbdesc;
   LPDIRECTSOUNDBUFFER pDsb = NULL;
   HRESULT hr;

   // Set up WAV format structure.    memset(&wfx, 0, sizeof(WAVEFORMATEX));
   wfx.wFormatTag = WAVE_FORMAT_PCM;
   wfx.nChannels = 2;
   wfx.nSamplesPerSec = 22050;
   wfx.nBlockAlign = 4;
   wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
   wfx.wBitsPerSample = 16;

   // Set up DSBUFFERDESC structure.

   memset(&dsbdesc, 0, sizeof(DSBUFFERDESC));
   dsbdesc.dwSize = sizeof(DSBUFFERDESC);
   dsbdesc.dwFlags =
      DSBCAPS_CTRLPAN      //聲源 是否可以左右移動
//DSBCAPS_CTRL3D      //聲源是否可以在   3D空移動。
//DSBCAPS_CTRLFX      //特效處理支援
| DSBCAPS_CTRLVOLUME           //音量控制
//| DSBCAPS_CTRLFREQUENCY     //頻率控制
| DSBCAPS_CTRLPOSITIONNOTIFY //可以設定播放位置通知
     | DSBCAPS_GLOBALFOCUS;   //在程式失去焦點的時候,依然播放聲音
   dsbdesc.dwBufferBytes =   wfx.nAvgBytesPerSec/2;       //設定緩衝區長度 為多少秒,就設定這裡為wfx.nAvgBytesPerSec 乘以多少,這裡和捕獲的統一設定成一秒
   dsbdesc.lpwfxFormat = &wfx;
SoundBufferLength=dsbdesc.dwBufferBytes ; //記錄緩衝的總長度**************************************************
BufferSectionLength = SoundBufferLength/3   ; //平均分成3段,每段的長度
BufferSectionLength -=   BufferSectionLength % wfx.nBlockAlign; //記憶體對齊    // Create buffer.

   hr = lpDirectSound->CreateSoundBuffer(&dsbdesc, &pDsb, NULL);
   if (SUCCEEDED(hr))
   {
      hr = pDsb->QueryInterface(IID_IDirectSoundBuffer8, (LPVOID*) ppDsb8);
      pDsb->Release();
   }
   return hr;
}
//設定通知物件
//The buffer must be stopped when this method is called. //dwOffset---Offset from the beginning of the buffer where the notify event is to be triggered, or DSBPN_OFFSETSTOP. 通知觸發時buffer的偏移
// hEventNotify ---Handle to the event to be signaled when the offset has been reached.   通知觸發的event HRESULT SetNotification(HANDLE   *hEventNotify ,LPDIRECTSOUNDBUFFER8 lpDsbSecondary )
{    #define cEvents   3

   LPDIRECTSOUNDNOTIFY8 lpDsNotify;
   DSBPOSITIONNOTIFY PositionNotify[cEvents];
   HRESULT hr;
  
   if (FAILED( hr = lpDsbSecondary->QueryInterface(IID_IDirectSoundNotify8,
              (LPVOID*)&lpDsNotify)))
   {
     return hr;
   }    // Create events.
   for (int i = 0; i < cEvents; ++i)
   {
     hEventNotify[i] = CreateEvent(NULL, TRUE, FALSE, NULL);
     if (NULL == hEventNotify[i])
     {
       hr = GetLastError();
       return hr;
     }       PositionNotify[i].dwOffset =   BufferSectionLength   * (i+1)   -1;   ;    //設定這裡值 為DSBPN_OFFSETSTOP 時,可以建立一個停止播放通知
      PositionNotify[i].hEventNotify = hEventNotify[i];
   
   }
  
     
   //注意沒呼叫一次SetNotificationPositions函式就清除以前的通知物件,所以要設定多個通知物件只能使用一個PositionNotify陣列,而不能呼叫多次SetNotificationPositions函式
     hr = lpDsNotify->SetNotificationPositions(3, PositionNotify);   
     lpDsNotify->Release();
     return hr;
}
BOOL AppWriteDataToBuffer(
         LPDIRECTSOUNDBUFFER8 lpDsb,   // The buffer.
         DWORD dwOffset,               // Our own write cursor.
         LPBYTE lpbSoundData,          // Start of our data.
         DWORD dwSoundBytes)           // Size of block to copy.
     {
       LPVOID   lpvPtr1;
       DWORD dwBytes1;
       LPVOID   lpvPtr2;
       DWORD dwBytes2;
       HRESULT hr;
     
       // Obtain memory address of write block. This will be in two parts
       // if the block wraps around.
     
       hr = lpDsb->Lock(dwOffset, dwSoundBytes, &lpvPtr1,
           &dwBytes1, &lpvPtr2, &dwBytes2, 0);
     
       // If the buffer was lost, restore and retry lock.
     
       if (DSERR_BUFFERLOST == hr)
       {
         lpDsb->Restore();
         hr = lpDsb->Lock(dwOffset, dwSoundBytes,
             &lpvPtr1, &dwBytes1,
             &lpvPtr2, &dwBytes2, 0);
       }
       if (SUCCEEDED(hr))
       {
         // Write to pointers.
     
         CopyMemory(lpvPtr1, lpbSoundData, dwBytes1);
         if (NULL != lpvPtr2)   //如果lpvPtr2不為NULL,說明要寫的超出了緩衝的長度,這時lpvPtr2指向緩衝的開始部分,可以繼續寫進去
         {
           CopyMemory(lpvPtr2, lpbSoundData+dwBytes1, dwBytes2);
         }
     
         // Release the data back to DirectSound.
     
         hr = lpDsb->Unlock(lpvPtr1, dwBytes1, lpvPtr2,
         dwBytes2);
         if (SUCCEEDED(hr))
         {
           // Success.
           return TRUE;
         }
       }
     
       // Lock, Unlock, or Restore failed.
     
       return FALSE;
     }
///////////////////////////////////
// 啟動 播放聲音
bool   SoundStart(HWND hwnd )
{
HRESULT hr;       if( ! SoundCreate(hwnd)) exit(0);
    

//不採用 播放通知了,播放通知控制起來不方便,只需要的捕獲通知裡面複製 到播放快取的相應位置好了
//    
//   HRESULT   hr = SetNotification(soundEvent,lpDSBuffer8);
//   if (FAILED(hr))
//    {    
//    MessageBox(hwnd,_T("建立播放位置通知失敗"),_T("widebright"),MB_OK);  
//    exit(0);
//}


ZeroMemory( SwapBuffer,29400);   //無聲 AppWriteDataToBuffer (lpDSBuffer8,0,SwapBuffer,29400); lpDSBuffer8->SetVolume(DSBVOLUME_MAX);
lpDSBuffer8->SetCurrentPosition (0);
if (SUCCEEDED( hr = lpDSBuffer8->Play (0,0,DSBPLAY_LOOPING))) //開始播放
{
     return true;
}else{      return false;
}
//不採用 播放通知了,播放通知控制起來不方便,只需要的捕獲通知裡面複製 到播放快取的相應位置好了
//while (1)
//   {
// //       DWORD   notifyIndex =   WaitForMultipleObjects(3,soundEvent,FALSE,INFINITE) ;     //INFINITE 表示一直等待 ,除非hEvent產生了訊號。 這裡
// //      
// //notifyIndex = notifyIndex - WAIT_OBJECT_0;    //得到觸發 通知物件序號
// //       ResetEvent(soundEvent[ notifyIndex]);
//               
//      //FreeBufferSectionNum = notifyIndex;
//    WaitForSingleObject(soundEvent[0], INFINITE);       
//    ResetEvent(soundEvent[ 0]);
//       FreeBufferSectionNum =0;
//    WaitForSingleObject(soundEvent[1], INFINITE);       
//    ResetEvent(soundEvent[1]);
//    FreeBufferSectionNum =1;
//    WaitForSingleObject(soundEvent[2], INFINITE);       
//    ResetEvent(soundEvent[ 2]);
//       FreeBufferSectionNum =2;
//
//}   }
////////////////////
//停止播放聲音
void SoundStop()
{ if ( ! lpDSBuffer8)   {lpDSBuffer8->Stop(); lpDSBuffer8->Release ();}
     if ( ! lpDirectSound) lpDirectSound->Release ();
    
} /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////下面是錄音部分/////////////////////////////////////////////////////////////////////
bool CaptureCreate(HWND hwnd )
{
                 
     HRESULT hr = DirectSoundCaptureCreate8 (&DSDEVID_DefaultCapture , &lpDSCapture,NULL);
if (FAILED(hr))
     {    
           MessageBox(hwnd,_T("建立DirectCapture介面失敗。"),_T("widebright"),MB_OK);    // Add error-handling here.
        return false;
     } hr =   CreateCaptureBuffer(lpDSCapture , &lpDSBCapture);      if (FAILED(hr))
     {    
     MessageBox(hwnd,_T("建立DirectCapture聲音播放緩衝區失敗。"),_T("widebright"),MB_OK);    // Add error-handling here.
           lpDSCapture->Release ();    // Add error-handling here.
        return false;
     } //g_pDSBuffer8->Lock(0,0,&lplockbuf,&len,NULL,NULL,DSBLOCK_ENTIREBUFFER);
//g_pWaveFile->Read((BYTE*)lplockbuf,len,&dwWrite);
//g_pDSBuffer8->Unlock(lplockbuf,len,NULL,0);
//g_pDSBuffer8->SetCurrentPosition(0);
//g_pDSBuffer8->Play(0,0,DSBPLAY_LOOPING);
   return true;
} HRESULT CreateCaptureBuffer(LPDIRECTSOUNDCAPTURE8 pDSC,
                             LPDIRECTSOUNDCAPTUREBUFFER8* ppDSCB8)
{
   HRESULT hr;
   DSCBUFFERDESC                dscbd;
   LPDIRECTSOUNDCAPTUREBUFFER   pDSCB;
   WAVEFORMATEX                 wfx =
      {WAVE_FORMAT_PCM, 2, 22050, 88200, 4, 16, 0};
     // {WAVE_FORMAT_PCM, 2, 44100, 176400, 4, 16, 0};
     // wFormatTag, nChannels, nSamplesPerSec, mAvgBytesPerSec,
     // nBlockAlign, wBitsPerSample, cbSize

   if ((NULL == pDSC) || (NULL == ppDSCB8)) return E_INVALIDARG;
   dscbd.dwSize = sizeof(DSCBUFFERDESC);
   dscbd.dwFlags = 0;
   dscbd.dwBufferBytes =   wfx.nAvgBytesPerSec/2;      //*******儲存多少秒的資料 就設定這裡為多少乘以 wfx.nAvgBytesPerSec*********
   dscbd.dwReserved = 0;
   dscbd.lpwfxFormat = &wfx;
   dscbd.dwFXCount = 0;
   dscbd.lpDSCFXDesc = NULL;

   if (SUCCEEDED(hr = pDSC->CreateCaptureBuffer(&dscbd, &pDSCB, NULL)))
   {
     hr = pDSCB->QueryInterface(IID_IDirectSoundCaptureBuffer8, (LPVOID*)ppDSCB8);
     pDSCB->Release();  
   }
   return hr;
} HRESULT SetCaptureNotifications(HANDLE   *rghEvent, LPDIRECTSOUNDCAPTUREBUFFER8 pDSCB)
{
   #define cEvents   3    LPDIRECTSOUNDNOTIFY8 pDSNotify;
   WAVEFORMATEX          wfx;  
   //HANDLE      rghEvent[cEvents] = {0};
   DSBPOSITIONNOTIFY   rgdsbpn[cEvents];
   HRESULT     hr;    if (NULL == pDSCB) return E_INVALIDARG;
   if (FAILED(hr = pDSCB->QueryInterface(IID_IDirectSoundNotify, (LPVOID*)&pDSNotify)))
   {
     return hr;
   }
   if (FAILED(hr = pDSCB->GetFormat(&wfx, sizeof(WAVEFORMATEX), NULL)))
   {
     return hr;
   }    // Create events.
   for (int i = 0; i < cEvents; ++i)
   {
     rghEvent[i] = CreateEvent(NULL, TRUE, FALSE, NULL);
     if (NULL == rghEvent[i])
     {
       hr = GetLastError();
       return hr;
     }        // Describe notifications.
         rgdsbpn[i].dwOffset =   BufferSectionLength   * (i+1)   -1;
         rgdsbpn[i].hEventNotify = rghEvent[0];    //rghEvent[i];   可以多個通知物件共用一個event的    }
   // Create notifications.

   hr = pDSNotify->SetNotificationPositions(cEvents, rgdsbpn);
   pDSNotify->Release();
   return hr;
} ///////////////////////////////////
// 啟動 播放聲音
bool   CaptureStart(HWND hwnd )
{
     if( ! CaptureCreate(hwnd)) exit(0);
    
  
   HRESULT   hr = SetCaptureNotifications( captureEvent,lpDSBCapture );
      if (FAILED(hr))
   {    
     MessageBox(hwnd,_T("建立捕獲位置通知失敗"),_T("widebright"),MB_OK);  
        exit(0);
   }
  
    
lpDSBCapture->Start (DSCBSTART_LOOPING); //開始捕獲     int NextCaptureOffset =0;   //捕獲快取區 偏移
     DWORD playCursor=0,writeCursor=0;
   int NextSoundOffset = 0;   //播放快取區 的偏移,
     lpDSBuffer8->GetCurrentPosition(&playCursor,&writeCursor);
     NextSoundOffset = (writeCursor + 600 )% SoundBufferLength; while (1)
    {
     //    DWORD   notifyIndex =   WaitForMultipleObjects(3,captureEvent,FALSE,INFINITE) ;     //INFINITE 表示一直等待 ,除非hEvent產生了訊號。 這裡
     //    
     //notifyIndex = notifyIndex - WAIT_OBJECT_0;    //得到觸發 通知物件序號
     //    ResetEvent(captureEvent[ notifyIndex]);
      VOID* pDSCaptureLockedBuffer     = NULL;
      DWORD dwDSCaptureLockedBufferSize;      WaitForSingleObject(captureEvent[0], INFINITE);       
     ResetEvent(captureEvent[ 0]);
         if( SUCCEEDED( hr = lpDSBCapture->Lock(NextCaptureOffset, BufferSectionLength,
                                           &pDSCaptureLockedBuffer,
                                           &dwDSCaptureLockedBufferSize,
                                           NULL, NULL, 0L ) ) )
   {
        CopyMemory( SwapBuffer,
                 pDSCaptureLockedBuffer,
                 dwDSCaptureLockedBufferSize );
            
    lpDSBCapture->Unlock( pDSCaptureLockedBuffer, dwDSCaptureLockedBufferSize,
                            NULL, 0 );
           
            lpDSBuffer8->GetCurrentPosition(&playCursor,&writeCursor);
//     AppWriteDataToBuffer (lpDSBuffer8,writeCursor   ,SwapBuffer,dwDSCaptureLockedBufferSize);
   AppWriteDataToBuffer (lpDSBuffer8, NextSoundOffset ,SwapBuffer,dwDSCaptureLockedBufferSize);

   }
        NextSoundOffset +=dwDSCaptureLockedBufferSize;
        NextSoundOffset %= SoundBufferLength; // Circular buffer
        NextCaptureOffset +=   BufferSectionLength;
        NextCaptureOffset %= SoundBufferLength; // Circular buffer
    
       
} }
////////////////////
//停止播放聲音
void CaptureStop()
{ if ( ! lpDSBCapture)   {lpDSBuffer8->Stop (); lpDSBCapture->Release ();}
     if ( ! lpDSCapture ) lpDSCapture ->Release ();
    

相關推薦

一個DirectSound例子

部分程式碼 --------------------------------------------------------------------------#include "sound.h" LPDIRECTSOUND8 lpDirectSound =NULL;       //Direc

npm install —— 從一個簡單例子看本地安裝與全域性安裝的區別

npm的包安裝分為本地安裝(local)、全域性安裝(global)兩種,從敲的命令列來看,差別只是有沒有-g而已,比如 npm install grunt # 本地安裝 npm install -g grunt-cli # 全域性安裝 這兩種安裝方式有什麼區別呢?從

我的第一個requirejs例子簡單的demo

看介紹是很簡單的,但是我做這個簡單的demo,過程還是很曲折的。引進jquery,老是沒引到,後來加上shim好了,再後來,去掉shim也是好的,我崩潰了。。。 二話不說上程式碼,給初學者看看。因為我在網上沒找到一個完整的程式碼。要是有一個簡單的demo就好了

oracle job:初學通過一個例子說說job

--先看例子:每分鐘向一個表中插入一條資料,所有操作都是在pl/sql環境中,下面提到的異常也都針對pl/sql 例項: create or replace procedure stopApplyBatch_procedure as begin update BIZ_PRO

08 集合[11,22,33,44,55,66,77,88,99]將所有<66的值保存至字典的第一個key中將所有>=66的值保存至字典的第二個key中。:{'k1':<66的所有值'k2':>=66的所有值}

pen nbsp print bsp [] dict 集合 key app li = [11,22,33,44,55,66,77,88,99]dict = {‘k1‘:[],‘k2‘:[]}for i in li: if i < 66: dict[

編寫一個方法計算一個字串中第一個不重複的字元在當前字串中的索引。是這個字串唯一一個存在的字元第一個出現的位置

1.編寫一個方法,計算一個字串中,第一個不重複的字元在當前字串中的索引。即是這個字串唯一一個存在的字元第一個出現的位置 比如saaaaafss 輸出f saaaaaf 輸出 s public class S1 { public static void main(String args

java--建立一個帶預設構造方法(無參構造)的類在構造方法中列印一條訊息"Hello Constructor";再為這個類新增一個過載構造方法令其接收一個字串引數將其一起打印出來

題目描述:建立一個帶預設構造方法(即無參構造)的類,在構造方法中列印一條訊息"Hello Constructor";再為這個類新增一個過載構造方法,令其接收一個字串引數,並在這個有參構造方法中把"Hello Constructor"和接收的引數一起打印出來。 //Person類 class Pe

java--建立一個帶預設構造方法(無參構造)的類在構造方法中列印一條訊息"Hello Constructor";再為這個類新增一個過載構造方法令其接收一個字串引數將其一起打印出來

題目描述:建立一個帶預設構造方法(即無參構造)的類,在構造方法中列印一條訊息"Hello Constructor";再為這個類新增一個過載構造方法,令其接收一個字串引數,並在這個有參構造方法中把"Hello Constructor"和接收的引數一起打印出來。 //Perso

問題:在pycharm中執行程式兩個.py檔案屬於同一目錄且都存在一個.py檔案無法使用from...import命令引用另一個.py檔案from...import...有紅色波浪線

轉自:https://blog.csdn.net/l8947943/article/details/79874180 https://blog.csdn.net/wcx1293296315/article/details/81156036 問題具體如圖: 兩個.py檔案屬於同一個檔案

# 從鍵盤輸入一個正整數用2的冪次方的形式輸出。約定冪次方用括號來表示表示為2(b),b=1時冪省略。例如139=2^7+2^3+2^1+2^0:2(7)+2(3)+2+2(0)

樣例輸入: 402 樣例輸出: 2(8)+2(7)+2(4)+2 要求:冪不能重複,如:139=26+26+23+21+20(出現了2個6次方) 參考 C 程式碼: #include<stdio.h> #include<stdlib.h>

定義一個整數陣列判斷是否是遞增有序的陣列中的每一個數都不大於其後面的數

1 int arr[] = new int[5]; 2 System.out.println("請輸入5個數"); 3 Scanner input = new Scanner(System.in); 4 5 for (int i = 0; i

Java之建立一個帶預設構造方法(無參構造)的類在構造方法中列印一條訊息"Hello Constructor";再為這個類新增一個過載構造方法令其接收一個字串引數。

建立一個帶預設構造方法(即無參構造)的類,在構造方法中列印一條訊息"Hello Constructor";再為這個類新增一個過載構造方法,令其接收一個字串引數,並在這個有參構造方法中把"Hello Constructor"和接收的引數一起打印出來。 效果如下: 附上程

從撲克牌中隨機抽5張排判斷是不是一個順子這5張牌是不是連續的。2~10為數字本身A為 1J為11Q為12K為13而大小王可以看成任意數字。

#define  _CRT_SECURE_NO_WARNINGS   #include<stdio.h>   #include<math.h.>   #include<assert.h>   #define n 5   int main()   {    

一個5位數判斷它是不是迴文數。12321是迴文數個位與萬位相同十位與千位相同

//第一種方法:輸入數後,將數字的每一位儲存到一個數組中,比較陣列的前後     /*     int a = 0, b[5] = {0} , i = 0;     printf("請輸入一個5位的正整數:\n");//45     scanf("%d", &a);

題目:一個5位數判斷它是不是迴文數。 12321是迴文數個位與萬位相同十位與千位相同。

Console.WriteLine("請輸入一個五位數"); string str = Console.ReadLine(); int a = Convert.ToInt32(s

7.,一個5位數判斷它是不是迴文數。12321是迴文數個位與萬位相同十位與千位相同

    int a[5] ={0},i = 0 ;         for (i = 0; i < 5; i++) {             if (i == 0) {                 a[i]=arc4random()%9+1;          

輸入一個5位數判斷它是不是迴文數。12321是迴文數個位與萬位相同十位與千位相同

直接看程式碼:(這裡增加難度,改成任意位數!) public class Top { public static void main(String[] args) throws IOExceptio

Java演算法題目:一個5位數判斷它是不是迴文數。12321是迴文數個位與萬位相同十位與千位相同。

題目:一個5位數,判斷它是不是迴文數。即12321是迴文數,個位與萬位相同,十位與千位相同。 網上的結果如下: import java.util.Scanner; public class Ex25 { static int[] a = new int[5]; static

一個純字串編寫一段程式碼列出其所有字元的大小寫組合 如 :字串“ji”字母組合['ji','jI','Ji','JI'] 每個字元的大小寫組合

def word_group(word):    word = word.lower()    dict = {}    list = []    for letter in word:        if not dict:            list.append(letter)           

C100-30 題目:一個5位數判斷它是不是迴文數。12321是迴文數個位與萬位相同十位與千位相同。

改一下 輸入一個數,判斷該數是否是迴文數? 程式碼來自 百度知道 原文地址 程式碼如下 #include <stdio.h> int main() { int num, temp; int sum=0; printf("Please