在字符串中找出連續最長的數字串
#include<iostream> #include<cctype> unsigned int Continumax(char**, char*); using namespace std; const int MAX = 1000; int main() { char* str = new char[MAX]; cin.getline(str, MAX); char** p = new char*[MAX]; unsigned int n = Continumax(p,str); cout<< *p << ','<< n << endl; delete[] p; delete[] str; return 0; } unsigned int Continumax(char** pOutputstr, char* intputstr) { int a[MAX] = { 0 }; int t = 0; int i = 0; char* p[MAX]; while (i<=strlen(intputstr)) { bool b1 = isdigit(intputstr[i]); if (b1) { p[t] = intputstr + i; a[t] = i; for (int j = i + 1;; j++) { bool b2 = isdigit(intputstr[j]); if (!b2) { a[t] = j - a[t]; i = j + 1; t++; break; } } } else i++; } int temp = a[0]; int n = 0; for (int k = 1; k < t; k++) { if (a[k]>temp) { temp = a[k]; n = k; } } *pOutputstr = p[n]; return a[n]; }
計算日期到天數轉換
#include<iostream> int iConverDateToDay(int year,int month,int day); int Param; using namespace std; int main() { int year,month,day; cin>>year; cin>>month; cin>>day; int n=iConverDateToDay(year,month,day); if (n==-1) { cout<<"-1"; } else { cout<<Param; } return 0; } int iConverDateToDay(int year, int month, int day) { int y; int z; int sum=0; if(year<0||year>9999) { return -1; } if(month>12||month<0) { return -1; } else { if((year%4==0&&year%100!=0)||year%400==0) { int a1[13]={0,31,29,31,30,31,30,31,31,30,31,30,31}; for(int i=0;i<month;i++) sum+=a1[i]; } else { int a2[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; for(int i=0;i<month;i++) sum+=a2[i]; } y=sum; } if(day<0) { return -1; } else { if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) { if(day>31) { return -1; } } else if (month==4||month==6||month==9||month==11) { if(day>30) { return -1; } } if((year%4==0&&year%100!=0)||year%400==0) { if(month==2) { if(day>29) { return -1; } } } else { if(month==2) { if(day>28) { return -1; } } } } z=day; Param=y+z; return 0; }
找出字符串中第一個只出現一次的字符#include <iostream> bool FindChar(char* pInputString, char* pChar); const int MAX = 1000; int main() { using namespace std; char* str = new char[MAX]; cin.getline(str, MAX); char* pChar = new char; FindChar(str,pChar); cout<<*pChar<<endl; delete pChar; delete[]str; return 0; } bool FindChar(char* pInputString, char* pChar) { int i=0; int m; int n=strlen(pInputString); while (i<n) { m=0; for(int j=0;j<n;j++) { *pChar=pInputString[i]; if (pInputString[j]==* pChar&&j!=i) { m=1; break; } } if (m==0) break; i++; } if (m==1) { * pChar='.'; } return 0; }