編寫函式求將整數轉換成數字字串C語言

時間 2021-05-04 13:25:46

1樓:匿名使用者

僅供參考 沒有把數字按字串輸出 只是效果差不多#include

void main()

else break;

}//如果輸入數字為五位,

if(j==4)

//輸入數字小於五位時,j要減1,因為for還沒結束而i+1,又因為j=i;所以p[i]上沒有值

else

}else

printf("輸入有誤");}

2樓:

方法一:

將字串格式化為字串

int num;

string str;

str.format("%d",num);

方法二利用求餘求出各位上的數字

scanf(%d,&number)//已三位數為例int x1,x2,x3;

x1=number/100; //百位數

x2=(number/10)%10; //十位數x3=number%10; //個位數

3樓:

如果可以確定位數會好做一點

4樓:veket的小號

/////////////////////////////////////////

// c-free 4.1

// mingw 3.4.5

// veket的小號

/////////////////////////////////////////

#include

void inttostr(int x, char s);

void reverseorder(char s, int last);

int main()

void inttostr(int x, char s)

s[i] = '\0';

reverseorder(s, i-1);

}void reverseorder(char s, int last) }

c語言中實現輸入乙個數字字元,然後轉換成整數數字輸出.怎麼做? 10

5樓:匿名使用者

字串轉整數可以有兩種方法:

1.使用c語言自帶的庫函式:atoi。

函式原型:int atoi(const char *nptr);

功能:把字串轉成整型數。

例如:#include

#include

int main(void)

/*輸出:

int = 12345

*/2.可以自己編寫乙個轉換函式:

#include

#include

int atoi(char *s)

return(t);

}int main ()

/*輸出:

n = 12345*/

6樓:死亡盛世

我只知道資料型別能相互轉換,但貌似字元和整形無法用一句程式轉換吧,如果你想寫多句程式來轉換,可直接用switch語句,對'0'-'9'進行判斷,直接輸出0-9,不知道這樣行不行的通。

7樓:

char c;

int n;

c = getchar();

n = c;

printf(%d,n);

/*也可以直接輸出c*/

printf(%d,c);

如果要數字對應輸出就這樣寫

char c;

int n;

c = getchar();

n = c-48;/*48是0的ascii碼值*/printf(%d,n);

編寫乙個函式,將乙個數字字串轉換為乙個整數

8樓:依然特雷西

1、新建乙個 將字串轉換為數字.py,如圖所示。2、中文編碼宣告注釋:# coding=gbk,如圖所示。

3、int() 函式的作用:用於將乙個字串或數字轉換為整型。

4、定義乙個 str1 變數,**:str1="10"。

5、使用 int() 函式將 str1 變數轉換為數字。

6、執行指令碼,輸出 str1 變數,如圖所示。

9樓:凌亂心扉

#include

#include

long  fun ( char *p)

else t=1;

while(*p)

x=10*x+(*p-48),p++;

return x*t;

} main()   /* 主函式 */

10樓:

給你個範例吧!

差不多的、 自己思考 、舉一反三

/*從乙個字串中提取出數字*/

#include

int main()

k = j;

n = 0;

j = i;

while(j

printf("%d\n",n);

i = k;

continue;

}i++;

}return 0;}

編寫函式實現將乙個字串轉換為整數.在主函式中輸入乙個由數字字元組成的字串 10

11樓:藤原子大雄

|12345678910111213141516171819202122#includeint chartonumber(char *s) return num;} int main(int argc,char **argv)

c語言編寫函式對於任意輸入的整數轉換為

include stdafx.h vc 6.0 include stdio.h include string.h include stdlib.h char mychange char p,int n p j 0 return strrev p void main void include int6...

求vb編寫給定整數N,求該整數的原碼,反碼,補碼的程式!要用十進位制的方法急用

private function 原碼 n as integer as string dim s 15 as string 整數原碼16位,用陣列表示,第一位是符號位,1表示負數 dim i as integer dim sum as integer dim num as integer num n...

C語言函式編寫,求兩個整數的最大公約數和最小公倍數

編寫該程式的整體思路 分別定義最大公約數函式和最小公倍數函式,然後再main函式裡面呼叫它。c語言實現 如下 include include int fun gy int,int 宣告最大公約數函式 int fun gb int,int 宣告最小公倍數函式 main int a,b,gy,gb pr...