C++捷径之一

 
所有 量在 用前必 被初始化。
所有的用 户输 入,必 须进 行合法性 检查
不要比 浮点数的相等, 如: 10.0 * 0.1 == 1.0 不可靠
程序与 境或状 态发 ,必 生的意外事件,如文件能否 逻辑锁 定、打印机是否 机等。
测试 也是 程的一部份,提交 联调测试 的程序必 过单 测试
 
所有程序在正常 应该 返回 0
函数不能嵌套定
唯一不需要原型声明的函数是 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;
void play(int m);
int main()
{
   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);
      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;
}
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;
}
 
#include <iostream> 要在 #include <conio.h> 之前
 
//-- 程序# 用冒泡排序法 排序
#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;
  
   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;
}
 
//-- 程序# 13  4 个字符串 理函数
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int main()
{
   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";
   strcat(s1,s2);
   cout<<"s1: "<<s1<<' '<<"s2: "<<s2<<endl;
   strcpy(s1,s2);
   cout<<s1<<" and "<<s2<<' '<<"are now the same./n";
   cout<<endl<<"======================="<<endl;
   //getchar();
   return 0;
}
 
//-- 程序# 19  一个 简单 的雇 数据 程序
#include <iostream>
using namespace std;
char name[10][80];
char phone[10][20];
float hours[10];
float wage[10];
int menu();
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);
   cout<<endl<<"======================="<<endl;
   //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;
}
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
}
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
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值