复习题一般我只把书上写不下的发布出来,其他的都在书上直接做了
8
编写一个函数,其原型如下:
int replace(char * str, char c1, char c2);
该函数将字符串中所有的c1都替换为c2,并返回替换次数。
#include <iostream> int replace(char *str, char c1, char c2); int main() { using namespace std; char st[16] = "Hi heaao worad"; int num = replace(st, 'a', 'l'); cout << st << endl; cout << "替换次数: " << num << endl; cout.put('\n'); system("pause"); return 0; } int replace(char *str, char c1, char c2) { int count = 0; while (*str != '\0') { if (*str == c1) { *str = c2; count++; } str++; } return count; }
12
假设有如下结构声明:
struct application{ char name[30]; int credit_ratings[3]; };
a. 编写一个函数,它将application结构作为参数,并显示该结构的内容。
b. 编写一个函数,它将application结构的地址作为参数,并显示该参数指向的结构的内容。
#include <iostream> using namespace std; struct applicant { char name[30]; int credit_ratings[3]; }; void show(applicant pst); int main() { applicant list = { "Mr.lei",10,10,10 }; show(list); system("pause"); return 0; } void show(applicant pst) { cout << "名字 \t信用评级\n"; cout << pst.name << " " << pst.credit_ratings[0]<< " " << pst.credit_ratings[1]<< " " << pst.credit_ratings[2] << endl; }
#include <iostream> using namespace std; struct applicant { char name[30]; int credit_ratings[3]; }; void show(const applicant *pst); int main() { applicant list = { "Mr.lei",10,10,10 }; show(&list); system("pause"); return 0; } void show(const applicant *pst) { cout << "名字 \t信用评级\n"; cout << pst->name << " " << pst->credit_ratings[0]<< " " << pst->credit_ratings[1]<< " " << pst->credit_ratings[2] << endl; }
@果然:

@神秘:
真得很厉害