Re: [問題] 如何得知一串數字由幾個數字組成
//數字範圍可超過200位
//只接受數字和ENTER鍵
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
int ASCII;
int Count = 0;
char *a = (char *) malloc ((Count + 1) * sizeof(char));
printf("請輸入數字:");
while ( !_kbhit() ) {
ASCII = _getch();
if ( ASCII == 0xD )
break;
else {
if ( ASCII >= 48 && ASCII <= 57 ) {
printf("%c", ASCII);
a = (char *) realloc (a, (Count + 1) * sizeof(char));
a[Count] = (char) ASCII;
Count++;
if (Count == 1 && ASCII == 48)
break;
}
}
}
printf("\n有%d個數字為: ", Count);
while (Count--) {
printf("%c", *(a++));
}
return 0;
}
※ 引述《GoodmanYY (GOgoHigher)》之銘言:
: 開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
: DEV C++
: 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
: 問題(Question):
: 問題:使用一個會隨使用者所輸入的數字字元個數而改變大小的字元陣列
: 使用者輸入最少一個,最多六個數字(範圍0~999999)
: 假如輸入5的話,陣列裡面有一個元素為5
: 假如輸入555的話,陣列裡面有三個元素為555
: 假如輸入555555的話,陣列裡面有六個元素為555555
: 不想預先配置陣列大小
: 想使用動態配置記憶體字元陣列大小下手
: 但是卻打不出關鍵程式碼
: 餵入的資料(Input):
: 1).5
: 2).55
: 3).555
: 預期的正確結果(Expected Output):
: 1).有1個數字為5
: 2).有3個數字為555
: 3).有6個數字為555555
: 錯誤結果(Wrong Output):
: 程式碼(Code):(請善用置底文網頁, 記得排版)
: #include <stdio.h>
: #include <stdlib.h>
: int main()
: {
: int *digptr;
: char dig[?]; //不知問號該填什麼
: printf("請輸入數字:");
: scanf("%c", &dig); //輸入數字並放在dig陣列裡面
: digptr = (char *) malloc( ? * sizeof(char) );
: printf("有%d個數字為%c\n", ,dig);
: system("pause");
: return 0;
: }
: *****補充說明:*****
: char[?]和malloc的問號是我不知該如何下手
: 因為卡在不知道該如何得知會輸入幾個數字個數
: 問題可能對大家來說很淺,但是我想破頭還是不知該如何實現
: 請各位提點一下謝謝。
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 203.70.79.226
※ 文章網址: http://www.ptt.cc/bbs/C_and_CPP/M.1403306397.A.D2C.html
推
06/22 15:46, , 1F
06/22 15:46, 1F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 3 之 3 篇):