logo资料库

c++基本编程案例(有100个).doc

第1页 / 共138页
第2页 / 共138页
第3页 / 共138页
第4页 / 共138页
第5页 / 共138页
第6页 / 共138页
第7页 / 共138页
第8页 / 共138页
资料共138页,剩余部分请下载后查看
1、
2、
3、
4、
5、
6、
7、
8、
9、
10、
11、
12、
13、
14、
15、
16、
17、
18、
19、
20、
21、
22、
23、
24、
25、
26、
27、
28、
29、
30、
31、
32、
33、
34、
35、
36、
37、
38、
39、
40、
41、
42、
43、
44、
45、
46、
47、
48、
49、
50、
51、
52、
53、
54、
55、
56、
57、
58、
59、
60、
61、
62、
63、
64、
65、
66、
67、
68、
69、
70、
71、
72、
73、
74、
75、
76、
77、
78、
79、
80、
81、
82、
83、
84、
85、
86、
87、
88、
89、
90、
91、
92、
93、
94、
95、
96、
97、
98、
100、
1、 /*------------------------------------------------ 【程序设计】 -------------------------------------------------- 题目:下面的函数 ave 求三个数的平均值并返回,完成该 函数的编写。 ------------------------------------------------*/ #include #include using namespace std; void grading(); float ave(int a, int b, int c) { /**********Program**********/ float x; x=(a+b+c)/3.0; return x; /********** End **********/ } int main() { cout<<"3,6,8 的平均值是:"<>a>>b>>c; outfile<<(int)(ave(a,b,c)*100)<
2、 /*----------------------------------------------------------------------------- - 【程序设计】 ------------------------------------------------------------------------------- - 题目:某服装店经营套服,也单件出售。若买的不少于 50 套, 每套 80 元;若不足 50 套,则每套 90 元;单件上衣 ,每件 60 元;单条裤子,每条 45 元。 已知要买的上衣和裤子数,编写函数计算应付款。 函数原型是 int Payfor(int yifu,int kuzi); 第一个参数表示衣服的数量,第二个参数表示裤子的数量 -------------------------------------------------------*/ #include #include using namespace std; void wwff(); /**********Program**********/ int Payfor(int yifu,int kuzi) { int p=0; if(yifu>=kuzi) { if(kuzi>=50) p=kuzi*80+(yifu-kuzi)*60; else p=kuzi*90+(yifu-kuzi)*60; } else { if(yifu>=50) p=yifu*80+(kuzi-yifu)*45; else p=yifu*90+(kuzi-yifu)*45; } return p; } /********** int main() { End **********/ cout<< Payfor(1,2)<
cout<< Payfor(45,60)<>y>>k; ofstream wf("out.txt",ios::out); wf< #include using namespace std; void grading(); int myfun(int x,int n) { /**********Program**********/ int z=1,i; for(i=0;i
ifstream rf("in.txt",ios::in); ofstream wf("out.txt",ios::out); int n,x; for (int i=0; i<10; i++){ rf>>x>>n; wf< #include using namespace std; void wwff(); long myfun(int x,int n) { /**********Program**********/ long z=1.0; int i; for(i=0;i
ifstream rf("in.txt",ios::in); int n,x; rf>>x>>n; ofstream wf("out.txt",ios::out); wf< #include using namespace std; int countWeek(int week, int n) { /**********Program**********/ int w; w=(week+n)%7; return w; /********** End **********/ } void grading(); int main() { int tWeek, n; cout <<"请输入当天的星期(星期日:0,星期一:1,......)"; cin >> tWeek; cout << "请输入要计算的星期是多少天后的:"; cin >> n; cout <
n << "天后是星期一\n"; n << "天后是星期二\n"; n << "天后是星期三\n"; n << "天后是星期四\n"; n << "天后是星期五\n"; n << "天后是星期六\n"; cout << break; case 2: cout << break; case 3: cout << break; case 4: cout << break; case 5: cout << break; case 6: cout << break; default: cout<<"错误!"; break; grading(); return 0; } void grading() { fstream myfile; myfile.open("out169.txt",ios::out); myfile << countWeek(0,20); myfile << countWeek(1,30); myfile << countWeek(6,40); myfile << countWeek(3,300) << endl; myfile << countWeek(4,12); myfile << countWeek(5,43); myfile << countWeek(0,1503) << endl; myfile.close(); } 6、 /*------------------------------------------------ 【程序设计】 -------------------------------------------------- 题目: 完成下面函数 reverse 的编写,该函数将数组 a 中的 所有元素逆序排列,参数 len 是数组 a 中元素的个
数。 ------------------------------------------------*/ #include #include using namespace std; void grading(); void reverse(int a[], int len) { /**********Program**********/ int i; for(i=0;i<(len/2);i++) { int temp; temp=a[i]; a[i]=a[len-i-1]; a[len-i-1]=temp; } /********** End **********/ } int main() { int a[6] = {2,4,3,7,5,8}; reverse(a,6); for (int i=0;i<6;i++) "; cout<>a[i]; reverse(a,10); for (i=0;i<10;i++) outfile<
infile.close(); outfile.close(); } 7、 /*------------------------------------------------------- 【程序设计】 --------------------------------------------------------- 题目:求 100 以内(不含 100)能被 3 整除且个位数为 6 的所有整数, 并按从小到大的顺序存入数组 a 中。 -------------------------------------------------------*/ #include #include using namespace std; void wwjt(int b[]); int main() { int a[50]={0}; int j=0 ; /**********Program**********/ for(int i=1 ; i<100 ; i++) { if(i%3==0 && i%10==6) {a[j] = i ; j++ ; } } /********** wwjt(a); return 0; End **********/ } void wwjt(int b[]) { fstream myfile; myfile.open("out.txt",ios::out); int i = 0; while(b[i]!=0) { myfile<
分享到:
收藏