所有
变
量在
调
用前必
须
被初始化。
对 所有的用 户输 入,必 须进 行合法性 检查 。
不要比 较 浮点数的相等, 如: 10.0 * 0.1 == 1.0 , 不可靠
程序与 环 境或状 态发 生 关 系 时 ,必 须 主 动 去 处 理 发 生的意外事件,如文件能否 逻辑锁 定、打印机是否 联 机等。
单 元 测试 也是 编 程的一部份,提交 联调测试 的程序必 须 通 过单 元 测试 。
对 所有的用 户输 入,必 须进 行合法性 检查 。
不要比 较 浮点数的相等, 如: 10.0 * 0.1 == 1.0 , 不可靠
程序与 环 境或状 态发 生 关 系 时 ,必 须 主 动 去 处 理 发 生的意外事件,如文件能否 逻辑锁 定、打印机是否 联 机等。
单 元 测试 也是 编 程的一部份,提交 联调测试 的程序必 须 通 过单 元 测试 。
所有程序在正常
结
束
时
都
应该
返回
0
。
函数不能嵌套定 义 。
唯一不需要原型声明的函数是 main() 。因 为 系 统 已 经预 定 义 了
函数不能嵌套定 义 。
唯一不需要原型声明的函数是 main() 。因 为 系 统 已 经预 定 义 了
getch( ); // #include <conio.h>
//
求取质数
2--1000
#include <conio.h>
#include <iostream>
using namespace std;
int main()
{
int i,j;
for(i=2; i<1000; i++)
{
for(j=2;
j<=(i/j)
; j++)
if(!(i%j)) break;
if(j>(i/j))
cout<<i<<" is prime./n";
}
cout<<endl<<"======================="<<endl;
getchar();
return 0;
}
#include <iostream>
#include <cstdlib>
using namespace std;
#include <cstdlib>
using namespace std;
void play(int m);
int main()
{
int option;
int magic;
magic=rand();
cout<<"magic is :"<<magic<<endl;
{
int option;
int magic;
magic=rand();
cout<<"magic is :"<<magic<<endl;
do{
cout<<"1. Get a new magic number."<<endl;
cout<<"2. Play."<<endl;
cout<<"3. Quit."<<endl;
do{
cout<<"Enter your choice: "<<endl;
cin>>option;
}while(option<1 || option>3);
cout<<"1. Get a new magic number."<<endl;
cout<<"2. Play."<<endl;
cout<<"3. Quit."<<endl;
do{
cout<<"Enter your choice: "<<endl;
cin>>option;
}while(option<1 || option>3);
switch(option)
{
case 1: magic=rand(); break;
case 2: play(magic); break;
case 3: cout<<"Goodbye."<<endl; break;
}
}while(option!=3);
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
{
case 1: magic=rand(); break;
case 2: play(magic); break;
case 3: cout<<"Goodbye."<<endl; break;
}
}while(option!=3);
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
void play(int m)
{
int t,x;
for(t=0; t<100; t++)
{
cout<<"Guess the number: ";
cin>>x;
if(x==m)
{
cout<<"**Right**"<<endl;
return;
}
else
{
if(x<m) cout<<"Too low."<<endl;
else cout<<"Too high."<<endl;
}
}
cout<<"You've used up all your guesses. try again."<<endl;
}
{
int t,x;
for(t=0; t<100; t++)
{
cout<<"Guess the number: ";
cin>>x;
if(x==m)
{
cout<<"**Right**"<<endl;
return;
}
else
{
if(x<m) cout<<"Too low."<<endl;
else cout<<"Too high."<<endl;
}
}
cout<<"You've used up all your guesses. try again."<<endl;
}
#include <iostream>
要在
#include <conio.h>
之前
//--
程序#
4
用冒泡排序法
对
数
组
排序
#include <iostream>
#include <cstdlib>
using namespace std;
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int nums[100];
int a,b,t;
int size;
size=100;
for(t=0; t<size; t++)
nums[t]=rand();
cout<<"Original array is: /n";
for(t=0; t<size; t++)
cout<<nums[t]<<' ';
cout<<endl<<"======================="<<endl;
{
int nums[100];
int a,b,t;
int size;
size=100;
for(t=0; t<size; t++)
nums[t]=rand();
cout<<"Original array is: /n";
for(t=0; t<size; t++)
cout<<nums[t]<<' ';
cout<<endl<<"======================="<<endl;
for(a=1; a<size; a++)//
遍
历
size-1
次
for(b=size-1; b>=a; b--)// 每 次找最小 值 的遍 历 次数
{
if(nums[b-1]>nums[b])
{
t=nums[b-1];
nums[b-1]=nums[b];
nums[b]=t;
}
}
cout<<"Sorted array is: /n";
for(t=0; t<size; t++)
cout<<nums[t]<<' ';
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
for(b=size-1; b>=a; b--)// 每 次找最小 值 的遍 历 次数
{
if(nums[b-1]>nums[b])
{
t=nums[b-1];
nums[b-1]=nums[b];
nums[b]=t;
}
}
cout<<"Sorted array is: /n";
for(t=0; t<size; t++)
cout<<nums[t]<<' ';
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
//--
程序#
13 4
个字符串
处
理函数
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int main()
{
char s1[80],s2[80];
cout<<"Enter two strings: ";
gets(s1);
gets(s2);
{
char s1[80],s2[80];
cout<<"Enter two strings: ";
gets(s1);
gets(s2);
cout<<"lengths: "<<strlen(s1)<<' '<<strlen(s2)<<endl;
if(!strcmp(s1,s2))
cout<<"The strings are equal./n";
else
cout<<"not equal./n";
cout<<"The strings are equal./n";
else
cout<<"not equal./n";
strcat(s1,s2);
cout<<"s1: "<<s1<<' '<<"s2: "<<s2<<endl;
cout<<"s1: "<<s1<<' '<<"s2: "<<s2<<endl;
strcpy(s1,s2);
cout<<s1<<" and "<<s2<<' '<<"are now the same./n";
cout<<s1<<" and "<<s2<<' '<<"are now the same./n";
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
//getchar();
return 0;
}
//--
程序#
19
一个
简单
的雇
员
数据
库
程序
#include <iostream>
using namespace std;
#include <iostream>
using namespace std;
char name[10][80];
char phone[10][20];
float hours[10];
float wage[10];
char phone[10][20];
float hours[10];
float wage[10];
int menu();
void enter(),report();// 这么 声明函数
void enter(),report();// 这么 声明函数
int main()
{
int choice;
do{
choice=menu();
switch(choice){
case 0: break;
case 1: enter();
break;
case 2: report();
break;
default:cout<<"Try again./n/n";
}
}while(choice!=0);
{
int choice;
do{
choice=menu();
switch(choice){
case 0: break;
case 1: enter();
break;
case 2: report();
break;
default:cout<<"Try again./n/n";
}
}while(choice!=0);
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
//getchar();
return 0;
}
int menu()
{
int choice;
cout<<"0. Quit./n";
cout<<"1. Enter information./n";
cout<<"2. Report information./n";
cout<<"/nChoose one: ";
cin>>choice;
return choice;
}
{
int choice;
cout<<"0. Quit./n";
cout<<"1. Enter information./n";
cout<<"2. Report information./n";
cout<<"/nChoose one: ";
cin>>choice;
return choice;
}
void enter()
{
int i;
for(i=0;i<10;i++){
cout<<"Enter last name: ";
cin>>name[i];
cout<<"Enter phone number: ";
cin>>phone[i];
cout<<"Enter number of hours worked: ";
cin>>hours[i];
cout<<"Enter wage: ";
cin>>wage[i];
}//for
}
{
int i;
for(i=0;i<10;i++){
cout<<"Enter last name: ";
cin>>name[i];
cout<<"Enter phone number: ";
cin>>phone[i];
cout<<"Enter number of hours worked: ";
cin>>hours[i];
cout<<"Enter wage: ";
cin>>wage[i];
}//for
}
void report()
{
int i;
for(i=0;i<10;i++){
cout<<name[i]<<' '<<phone[i]<<endl;
cout<<"Pay for the week: "<<wage[i]*hours[i]<<endl;
}//for
}
{
int i;
for(i=0;i<10;i++){
cout<<name[i]<<' '<<phone[i]<<endl;
cout<<"Pay for the week: "<<wage[i]*hours[i]<<endl;
}//for
}