12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- /*
- * Copyright (C) 2020, 2019, 2018, 2017 Girish M
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
- * MA 02110-1301, USA.
- *
- */
- /*----------------------------------
- Name: Girish M
- Roll number: cs1713
- Date: 25 July 2017
- Program description: Sum of n integers
- Acknowledgements:http://pubs.opengroup.org/onlinepubs/7908799/xsh/regcomp.html
- ------------------------------------*/
- #include <stdio.h>
- #include <stdlib.h>
- #include <regex.h>
- int match(const char *string, char *pattern)
- {
- int status;
- regex_t re;
- if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB) != 0) {
- return(0); /* report error */
- }
- status = regexec(&re, string, (size_t) 0, NULL, 0);
- regfree(&re);
- if (status != 0) {
- return(0); /* report error */
- }
- return(1);
- }
- int main(int argc, char* argv[])
- {
- int i, sum=0;
- char* pattern = "^[+-]?[0-9]+$";
- if(argc > 1)
- {
- for(i=1; i<argc; i++)
- {
- if(match(argv[i], pattern))
- sum = sum + atoi(argv[i]);
- else
- printf("\n%s is not an integer. Excluding in sum.\n", argv[i]);
- }
- printf("\nSum is %d\n", sum);
- }
- else
- {
- printf("\nUsage: ./cs1713-day1-prog3.o int1 int2 ...\n");
- }
- return 0;
- }
|