freepeople性欧美熟妇, 色戒完整版无删减158分钟hd, 无码精品国产vα在线观看DVD, 丰满少妇伦精品无码专区在线观看,艾栗栗与纹身男宾馆3p50分钟,国产AV片在线观看,黑人与美女高潮,18岁女RAPPERDISSSUBS,国产手机在机看影片

正文內(nèi)容

程序設(shè)計基礎(chǔ)第6章-閱讀頁

2025-01-21 18:28本頁面
  

【正文】 字符串的輸入輸出 用于存儲字符串的字符數(shù)組,其元素可以通過下標運算符訪問,這與一般字符數(shù)組和其他任何類型的數(shù)組是相同的。如假定 a[11]為一個字符數(shù)組,則: (1) cina。 是允許的,即允許在提取或插入操作符后面使用一個字符數(shù)組名實現(xiàn)向數(shù)組輸入字符串或輸出數(shù)組中保存的字符串的目的。 ?字符串的輸入輸出 例如:字符數(shù)組長度為 10 ? 輸入字符串 string,結(jié)果為 string ? 輸入字符串 str ing,結(jié)果為 str ? 輸入字符串 stringandchar,結(jié)果報錯。另外,輸入的字符串不需要另加雙引號定界符,只要輸入字符串本身即可,假如輸入了雙引號則被視為一般字符。 確定算法: 用字符數(shù)組 str[256]保存字符串 , 用整型變量 strlen記錄長度 , 初值為 0 。\039。 int main() { char str[256] = 。 cout 請輸入一字符串 。 int i = 0。\039。 i ++。 return 0。 確定算法: 用字符數(shù)組 str[256]保存字符串 , 用整型變量 count記錄字母 e的個數(shù) , 初值為 0 。 int main() { char str[256] = 。 cout 請輸入一字符串 。 int i = 0。\039。e39。E39。 } i ++。 return 0。 char str1[80]。 int i, j。 //ab cin str2。 while(str1[i] != 39。) i ++。 while(str2[j] != 39。) { str1[i] = str2[j]。 j++。\039。 //abcde 39。 b a 39。 f e d 39。 e d c b a 0 1 2 0 1 2 3 0 1 2 3 4 5 例:從鍵盤輸入兩個字符串 ,再把第二個字符串接到第一個字符串的后面 。 char str2[80]。 cin str1。 i = 0。\039。 j = 0。\039。 //i++。 } str1[i] = 39。 cout 拼接后字符串 : str1 \n。 int i, j。 cin str2。 while(str1[i] != 39。) i ++。 while((str1[i++]=str2[j++])!=39。) 。 進一步的簡化也是可行的: while((str1[i++]=str2[j++])) 。 char str[80]。 cin str。 for( i=0。 i++) count[i]=0。\039。break。break。break。\039。a39。amp。z39。a39。a39。A39。amp。Z39。A39。 i++。 i 26。 39。 假定每一百單位外幣折合人民幣如下: 英鎊 港幣 美元 確定算法: 用實型數(shù)組 list[4]存放匯率 , 從下標 1開始使用 , 在聲明時初始化;用整型變量 choice存放鍵盤輸入的選擇 , 用實型變量 money存放鍵盤輸入的金額 , 用實型變量 rmb存放計算結(jié)果 。 int main() { float list[4] = {0,}。 float money,rmb。 cin choice。 case 1: case 2: case 3: cout 輸入金額 。 rmb = money * list[choice] / 100。 break。 } } return 0。 確定算法: 用整型數(shù)組 numbers[20]存放數(shù)列的前 20項 , 從下標 0開始使用 , 開頭 2項在聲明時初始化 循環(huán)變量 i取初值 3 如果 i 19, 轉(zhuǎn)到 5 numbers[i] = numbers[i2] + numbers[i1] i加 1, 轉(zhuǎn)回到 2 跳出本次循環(huán) , 執(zhí)行下一條語句 , 進入另一個新循環(huán) , 循環(huán)變量 i取初值 1 如果 i 19, 轉(zhuǎn)到 9 輸出 numbers[i] i加 1, 轉(zhuǎn)回到 6 結(jié)束運行 include iostream using namespace std。 //其余各位為零 int i。 i = 19。 } for (i = 0。 i ++) { cout numbers[i] \t。 return 0。 確定算法: 用整型數(shù)組 numbers[20]存放數(shù)據(jù) , 從下標 0開始使用;用變量 count計算一行中已經(jīng)輸出的個數(shù) , 在聲明時初始化為 0。 int main() { int numbers[20]。 cout 請輸入 k:。 for (i = 0。 i++) { numbers[i] = k + i。 i = 19。 count ++。 count = 0。 return 0。 假定成績表存放在數(shù)組 scores中 , 輸入的成績是 x, 首先要在成績表中從頭開始找到第一個比 x小的成績 , x就應(yīng)該插在這個成績之前 , 為了空出 x的存放位置 , 從這個成績開始 , 直到最后一個成績 , 都要向后移動一個位置 。 輸出原來的成績表 , 輸入 x 準備用變量 p對準插入位置 , p的初值為 1 只要 p不大于 N, 并且 x的值小于 scores[p]的值 , 就反復(fù)把 p加 1 循環(huán)變量 i的初值為 N 如果 i小于 p, 轉(zhuǎn)到 8 把 scores[i]的值復(fù)制到 scores[i+1] i減 1, 轉(zhuǎn)回到 5 把 x的值復(fù)制到 scores[p] 輸出新的成績表 include iostream using namespace std。 float x。 cout 原成績表: 。 i = N。 } cout\n輸入一個需插入的成績: 。 p = 1。amp。 } for (i = N。 i ) { scores[i+1] = scores[i]。 cout 新成績表: 。 i = N+1。 } cout \n。 } 例題:編寫程序 , 實現(xiàn)功能:對一個包含 10個成績的無序成績表進行排序 , 其成為降序排列的成績表 , 最后輸出結(jié)果 。 在整個成績表中尋找一個最高成績 , 然后把它換到第 1個位置 。 再 , 排除已經(jīng)處理妥當(dāng)?shù)那?2個位置 , 在其余位置上尋找一個最大的 , 然后把它換到第 3個位置 。 如無序成績表: 67,34,90,88,55,74,95,82,43,92 92 43 82 95 74 55 88 90 34 67 34 43 55 67 74 82 88 90 92 95 92 43 82 67 74 55 88 90 34 95 34 43 55 67 74 82 88 90 92 95 34 43 82 67 74 55 88 90 92 95 34 43 55 67 74 82 88 90 92 95 尋找最高成績的程序 , 其核心片段如下 max = scores[1]。 for (i = 2。 i ++) { if (scores[i] max) { max = scores[i]。 } } 成績表存放在數(shù)組 scores[11]中 , 從下標 1的位置開始存放 。 外循環(huán)變量 j表示處理第幾個位置 , 內(nèi)循環(huán)變量 i控制尋找當(dāng)前循環(huán)中的最大值 。 int main() { int scores[11] = {0,67,34,90,88,55,74,95,82,43,92}。 for (j = 1。 j ++) { max = scores[j]。 for (i = j + 1。 i ++) { if (scores[i] max) { max = scores[i]。 } } scores[max_index] = scores[j]。 } cout 排序結(jié)果 。 j = 10。 } cout endl。 } 最簡單的一種加密方法:約定一個字符變換表 , 把字符串中的每個字符都按變換表轉(zhuǎn)換成另一個字符 。 輸出加密后的字符串 。 顯然這個數(shù)組中的 26個字符不能有重復(fù) , 否則將導(dǎo)致兩個不同的原始字母都轉(zhuǎn)換成同一個字符 , 以后無法正確解密 。 int main() { char list[27] = kczuytmxsejdlribnhvawofpgq。 int i,j。 cin str。 while (str[i] != 39。) { j = str[i] 97。 i ++。 return 0。連接后 ,串 1的‘ \0’取消 , 新串最后加‘ \0’ ?字符串拷貝函數(shù) strcpy 格式: strcpy(字符數(shù)組 1,字符串 2) 功能:將字符串 2,拷貝到字符數(shù)組 1中去 返值:返回字符數(shù)組 1的首地址 說明: ?字符數(shù)組 1必須足夠大 ?拷貝時‘ \0’一同拷貝 ?不能使用賦值語句為一個字符數(shù)組賦值 例 char str1[20],str2[20]。 (?) str2=str1。 int main() { char destination[25]。 strcpy(destination, visual)。 strcat(destination, c)。 return 0。A39。\039。B39。C39。\039。D39。 ( 2) char s[ ]=\t\v\\\0will\n。 答案: 1 3 1 include include void main() { char str1[] = Hello!, str2[] = How are you?,str[20]。 len1=strlen(str1)。 if(strcmp(str1, str2)0) { strcpy(str,str1)。 } else if (strcmp(str1, str2)0) { strcpy(str,str2)。 } else strcpy(str,str1)。 coutstrendl。 } 例 strcmp與 strlen舉例 How are you?Hello! Len1=6,Len2=12,Len3=18 例 比較 int a[2][3]={{5,6},{7,8}}。 5 6 0 7 8 0 5 6 7 8 0 0 例 int a[][10]。 例 int a[5]。 例 int a[10]。 a[i]=10。 float weight[]。 例 char str[]=“Hello”。 h e l l o 0 2 3 1 4 h e l l o \0 0 2 3 1 4 5 練習(xí) 教學(xué)與實驗輔導(dǎo)(第 3版)實驗 6
點擊復(fù)制文檔內(nèi)容
醫(yī)療健康相關(guān)推薦
文庫吧 www.dybbs8.com
備案圖鄂ICP備17016276號-1