本文目錄一覽:
- 1、c語言中如何讓輸出的數值分段
- 2、c語言數字切割
- 3、c語言如何拆分數字
c語言中如何讓輸出的數值分段
進行數值分段主要進行字元串分割,使用strtok函數即可實現字元串分割。這裡引用一段strtok用法:
The strtok() function returns a pointer to the next “token” in str1, where str2 contains the delimiters that determine the token. strtok() returns NULL if no token is found. In order to convert a string to tokens, the first call to strtok() should have str1 point to the string to be tokenized. All calls after this should have str1 be NULL.
For example:char str[] = “now # is the time for all # good men to come to the # aid of their country”;
char delims[] = “#”;
char *result = NULL;
result = strtok( str, delims );
while( result != NULL ) {undefined
printf( “result is \”%s\”\n”, result );
result = strtok( NULL, delims );
}
/* 何問起 hovertree.com */
The above code will display the following output:
result is “now “
result is ” is the time for all “
result is ” good men to come to the “
result is ” aid of their country”
c語言數字切割
c語言拆分數運算
從鍵盤上輸入一個4位數的整數n,編寫程序將其拆分為兩個2位數的整數a和b,計算並輸出拆分後的兩個數的加、減、乘、除和求余運算的結果。例如n=-4321,設拆分後的兩個整數為a,b,則a=-43,b=-21。除法運算結果要求精確到小數點後2位,數據類型為float。求余和除法運算需要考慮除數為0的情況,即如果拆分後b=0,則輸出提示信息”The second operater is zero!”
輸入提示信息:”Please input n:\n”
輸入格式: “%d”
輸出格式:
拆分後的兩個整數的輸出格式:”%d,%d\n”
加法、減法、乘法的輸出格式:”sum=%d,sub=%d,multi=%d\n”
除法和求余的輸出格式:”dev=%.2f,mod=%d\n”
除數為0的提示信息:”The second operator is zero!\n”
#include stdio.h
int main(void)
{
int input_number,separate_number_a,separate_number_b;
printf(“Please input (4 digit) Number n:\n”);
scanf(“%d”,input_number);
separate_number_b = input_number % 100;
separate_number_a = input_number / 100;
printf(“The separate number are:%d,%d\n”,separate_number_a,separate_number_b);
printf(“sum=%d,sub=%d,multi=%d\n”,separate_number_a+separate_number_b,separate_number_a-separate_number_b,separate_number_a*separate_number_b);
if(separate_number_b == 0)
printf(“The second operator is zero!\n”);
else
printf(“dev=%.2f,mod=%d\n”,(float)separate_number_a/separate_number_b,separate_number_a%separate_number_b);
return 0;
}
c語言如何拆分數字
小弟有這麼一種做法,我覺得這個比較快一點。寫得不是很好,你看下先啦!
#includestdio.h
#includestring.h
#includewindows.h
int main()
{
char a[30];
int i,l;
printf(“請輸入一整型數字:”);
gets(a);
printf(“數字拆分如下:\n”);
l=strlen(a);
for(i=0;il;i++)
printf(“%d “,a[i]-‘0’);//將數字字元轉為數字值
printf(“\n”);
}
你看一下,還可以的話,麻煩你採納我,Thank you。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/184808.html