→ warp2:1.寫出一個function可以把ASCII轉成十進位和十六進位 10/10 22:34
→ warp2:2.泡沫排序法 10/10 22:35
→ warp2:3.寫出一個function可以做字串組合(like strcat function) 10/10 22:35
→ warp2:4-1利用rand()寫出一個function可以產生6個1~10000的亂數 10/10 22:37
→ warp2:4-2同上,寫出一個function產生六個1~100的亂數(不得重複) 10/10 22:38
→ warp2:以
1.相加
2.字串反轉
#include stdio.h
#include stdlib.h
#include string.h
int main(){
int i;
char a[16]="This is a book~~";
char u[16];
for(i=0;i<=15;i++){
u[i]=a[i];
}
for(i=0;i<=15;i++){
a[15-i]=u[i];
}
printf("%s\n",a);
}
3.十進位轉二進位
main(){
int in_b=0;
int in_c=0;
char bi[29]; //需先建立 字元陣列以存放 位元
printf("n請輸入Bt");
scanf("%d",&in_b);
itoa(in_b, bi, 2);
// in_b 是我們想轉的十進位數字
// bi 是存放 我們要的結果
// 2 是代表 我們想轉成二進位 若要16進位 則是16
printf("將%d 轉成二進位 %s",in_b,bi);
}
4.還有一題是輸入 60+40要輸出100
#include stdio.h
#include iostream
#include sstream
#include string
using namespace std ;
int main(){
string line ;
getline( cin , line ) ;
istringstream istr(line) ;
int no , sum = 0 ;
while ( istr >> no ) {
sum += no ;
}
cout << sum << endl ;
printf("%d\n",sum);
return 0 ;
}
還有一題是memory的copy
#include stdio.h
#include stdlib.h
#include malloc.h
void main(void){
char *ptr;
ptr=(char*)malloc(sizeof(char)*8);
}
第一提就是CHAR換成十進制和十六進制
應該用%C %D %X轉換就可以了
第二題輸入三個數字比較大小
這個用很多排序法都可以 想要用IF ELSE 也是可以
第三題要你組合字串
就輸入字串A=abc B=def輸入FUNCTION後得到
abcdef
C++ 裡面 宣告 string物件 他就有這種加法的operator.
#include stdio.h
#include iostream
#include string.h
void main(void){
char string1[20],string2[20]="囧",string3[20];
char dest_string[20];
scanf("%s",&string1);
scanf("%s",&string3);
strcpy(dest_string,string1);
strcat(dest_string,string2);
strcat(dest_string,string3);
printf("%s",dest_string);
}
第四提有兩小題
有個亂數FUNC.讓你呼叫 rand(void)
A. 利用rand(void)產生六個1-10000的亂數序列
B. 產生1-100亂數序列,但不重複
#include stdio.h
#include stdlib.h
#include time.h
void main()
{
int a,run=6;
int i=0,s=0;
srand(time(NULL));
for(i=0;i<6;i++){
for(s;s<1000;s++){
a=(rand()%1000)+1;
printf("%d ",a);
}
printf("\n\n");
}
}
計算n!
#include stdio.h
long factorial(long n);
void main(void){
//int i;
printf("%d",factorial(6));
}
long factorial(long n){
if(n==0)
return 0;
if(n==1)
return 1;
if(n>1)
return(n*factorial(n-1));
else
printf("wrong!!");
}
其他函式參考:
sprintf() 函數: 將數值轉成string
例如:
char s[40];
sprintf(s, "%f", 143.5);
ex:
#include
#include
void main(void){
char s[40];
sprintf(s,"%x",123);
printf("%s",s);
}
ans:7b
atoi() 函數: 將string轉成數值
例如:
char s[]="340";
int n = atoi(s);
沒有留言:
張貼留言