Saturday, July 23, 2011

字串小寫變大寫

// 小寫變大寫.cpp : 定義主控台應用程式的進入點。
//

//一個能把輸入的字串中,所有字第一個字母變大寫,
//和把全部所有字母都變大寫的C++程式


#include "stdafx.h"
#include<iostream>
#include <stdlib.h>
#include<string>

using namespace std;

int main(){


    int i = 0 ;
    string sentence = "this is a sentence.";

    cout << "Input the string: \n";
    getline(cin, sentence);


    if( sentence[i] >= 'a' && sentence[i] <= 'z' )
        sentence[i++] -= 32 ;


    while ( i < sentence.size() )
    {
         if( sentence[i] == ' ' &&  sentence[i+1] >= 'a' && sentence[i+1] <= 'z' )
        {
            sentence[++i] -= 32 ;
        }
        ++i;
    }
    cout << "每個單字的第一個character都是大寫的句子為:\n";
    cout << sentence << endl;


    for (i = 0; i < sentence.size(); i++ )
    {   
        if ( sentence[i] >= 'a' && sentence[i] <= 'z' )
             sentence[i] -= 32;
    }

    cout << "所有characters都是大寫的句子為:\n";
    cout << sentence << endl;

 
    system("pause");
    return 0 ;
}

輸入學生成績並計算平均成績

// AverageScore.cpp : 定義主控台應用程式的進入點。
//

#include "stdafx.h"
#include <iostream>
#include <cstdlib>

using namespace std;

struct score
{
       char name[30];   //名字
       float chi;   //國文
       float eng;   //英文
       float math;  //數學
       float total; //總分
       float avg;   //平均
};

int main(int argc, char *argv[])
{
    score stu[100];
    int n;


    cout << "請輸入學生人數\n";
    cin >> n;


  
    //輸入n位學生的資料
    for (int i=0;i<n;i++)
    {
        cout << "請輸入第 "<< i+1 << "位學生的名字: ";
        cin >> stu[i].name;

        cout << "請輸入第 "<< i+1 << "位學生的國文成績: ";
        cin >> stu[i].chi;

        cout << "請輸入第 "<< i+1 << "位學生的數學成績: ";
        cin >> stu[i].math;
        cout << "請輸入第 "<< i+1 << "位學生的英文成績: ";
        cin >> stu[i].eng;
       
        stu[i].total = stu[i].chi + stu[i].math + stu[i].eng;
        stu[i].avg = stu[i].total / 3;
    }

    for (int i=0;i<n;i++)
    {
        cout <<  stu[i].name  << "的總成績為: " << stu[i].total << endl;
        cout <<  stu[i].name << "的平均成績為: " << stu[i].avg << endl;
    }


    system("pause");
    return 0;

}

The 3n + 1 problem

// 程式若輸入一整數,就會輸出the 3n+1problem的數列,以及經幾步之後會達到1


// test 1.cpp : 定義主控台應用程式的進入點。
//

#include "stdafx.h"
#include <iostream>
#include <stdlib.h>

using namespace std;


int threenja1(int *, int);


int main(int argc, _TCHAR* argv[])
{

    int ptr[300] = {0};
    int length = 0;
    int n = 1; int i;

   
    while (n != 0)
    {

    cout << "Please input a positive integer  n: \n  (Enter 0 to end the program) ";
    cin >> n;

        if ( n > 0 )
        {
            length = threenja1(ptr,n);

            cout << "length = " << length << endl;


            cout << "the process is : \n";
            for ( i = 0; i < length; i ++)
                cout << *(ptr + i) << "  ";

            cout << endl << endl;
        }
       
        else if (n < 0)
        {
            cout << n << " is not a positive integer. \n\n";
        }

        else
        {    cout << "The program is ended. \n\n" ;
            break;
        }



    }


    system("pause");
    return 0;
}


int threenja1(int *ptr , int n)
{
    int i = 1;

    *ptr = n;

    while ( n != 1 )
    {
        if ( n%2 == 0 )
        {   
            n = n/2;
            ptr[i] = n;
            i ++ ;
        }
        else
        {   
            n = 3*n + 1;
            ptr[i] = n;
            i ++ ;
        }
    }

    const int j = i;
    int  *p  = new int[j];

   
    for ( int k = 0 ; k < j ; k++) ;
        *(p + j) = ptr[j];

    ptr = p;

    return i;

}

Thursday, July 21, 2011

反轉字串的C程式

// a2.cpp : 定義主控台應用程式的進入點。
//
#include "stdafx.h"
反轉字串的C程式

輸入任意字串,輸出為這個字串的反轉


// a2.cpp : 定義主控台應用程式的進入點。
//
#include "stdafx.h"
#include <iostream>
#include <cstdlib>

using namespace std;
char* inv(char *, int);

int main(int argc, _TCHAR* argv[])
{
 int len = 0;
 char str[30];
 char *p;
 printf("請輸入一字串:");
 gets_s(str);
 len = strlen(str);
 p = inv(str,len);
 printf("反轉的字串為:");

 for (int i = 0; i < len ; i++)
  printf("%c",*(p+i));
 system("pause");
 return 0;
}

char* inv(char *s, int len)
{

 char *s2 = new char[len];
 for (int i = 0; i < len ; i++)
  *(s2 + i) = *(s + len - 1 -i);

 return s2;
}