C Language (7)--high-precision addition, subtraction, multiplication, today is the day of the week, four square number, the candidate vote question

Source: Internet
Author: User
Tags getdate ticket

1. High-precision addition, subtraction, multiplication

#include <stdio.h> #include <string.h> #include <malloc.h>void plus (char *a,char *b,char *c);// Custom high-precision addition function void sub (char *a,char *b,char *c);//Custom high-precision subtraction function void multiply (char *a,char *b,char *c);//Custom high-precision multiplication function int main () {C Har a[1000],b[1000],c[1000],s;//a,b array save operand, c array save operation result, s save operator printf ("Please enter calculation: \ n"); scanf ("%s%c%s", &a,&s, &AMP;B);p rintf ("%s%c%s=", a,s,b), switch (s) {case ' + ':p LUs (a,b,c); Break;case '-': Sub (a,b,c); Break;case ' * ': Multiply ( A,B,C); break;}     printf ("%s\n", c); return 0;} void Plus (char *a,char *b,char *c) {int i,index_a,index_b,index_c,carry=0,ten= ' 9 ' +1,temp_index_c;index_a=strlen (a)-1 The//index_a variable points to a last digit, the lowest bit index_b=strlen (b) -1;//index_b variable points to the last digit of B, the lowest bit index_c=index_a>index_b?index_a:index_b;// Detects which number of digits is more and assigns a value to Index_c temp_index_c=index_c; if (index_a>=index_b) {//a number of bits is more than B for (i=index_b+1;i>=0;i--)// Traverse all elements in B b[i+ (Index_a-index_b)]=b[i];//number back, so that the last with a pair of its for (i=0;i<index_a-index_b;i++)//To the number B in the upper part of the more than the traversal assigned 0 b[i]= ' 0 ';} Else{for (i=index_a+1;i>=0;i--) a[i+ (index_b-index_a)]=a[i];for (i=0;i<index_b-index_a;i++) a[i]= ' 0 ';} while (index_c>=0) {//cycle condition; A and b correspond to digits on the number plus c[index_c]=a[index_c]+b[index_c]+carry-' 0 ';//carry is the number of digits obtained from the last digit, initially 0 if (c [Index_c]>=ten) {//the digit is greater than or equal to c[index_c]-=ten-' 0 '; carry=1;//carry 1}elsecarry=0;index_c--;//Compute high}if (carry==1) {// The highest bit is still in 1 for (i=temp_index_c;i>0;i--) c[i+1]=c[i];//c[0]=1;//the highest bit is 1}}void sub (char *a,char *b,char *c) {int I,j,ca , Cb;ca=strlen (a);//ca is the length of a cb=strlen (b);//CB is the length of B if (ca>cb| | CA==CB&AMP;&AMP;STRCMP (A, b) >=0) {//a length greater than B or a is equal to B and string a>=b for (i=ca-1,j=cb-1;j>=0;i--, j--)// Traverse A and B public length characters number a[i]-= (b[j]-' 0 ');//calculate its result for (i=ca-1;i>=0;i--) {//Traverse all subscript if (a[i]< ' 0 ') of a {//if the current subscript corresponds to a value less than 0, the borrow operation a[ i]+=10;//Current Value plus a[i-1]--;//high 1}}i=0;while (a[i]== ' 0 ')//current subscript corresponding value is 0i++;//subscript plus 1 if (a[i]== ' + ') {//current subscript to string tail c[0]= ' 0 ';/ A value of 0 c[1]= ' + ';//Add the end flag}else{for (j=0;a[i]!= ' c[j]=a[i];//'; i++,j++) to move the active part of a to C c[j]= ' + ';//Add end flag}}else{//similar to the above section For (i=ca-1,j=cb-1;i>=0;i--, j--) b[j]-= (a[i]-' 0 '), for (j=cb-1;j>=0;j--) {if (b[j]< ' 0 ') {b[j]+=10;b[j-1]--;}} J=0;while (b[j]== ' 0 ')//Remove the high 0 j++;c[0]= '-';//The result of the operation is negative for (i=1;b[j]!= '; j++,i++ ') c[i]=b[j];c[i]= ';}} void Multiply (char *a,char *b,char *c) {int I,j,ca,cb,*s;ca=strlen (a)//ca is the length of a cb=strlen (b);//CB is the length of b s= (int *) malloc ( (CA+CB) *sizeof (int));//Application space for (i=0;i<ca+cb-1;i++)//traverse The requested space all elements s[i]=0;//initially assigned a value of 0 for (i=0;i<ca;i++) {for (j=0; j<cb;j++) s[i+j]+= (a[i]-' 0 ') * (b[j]-' 0 ');//calculate multiplication to save to s}for (i=ca+cb-2;i>0;i--) {//Traversal request array, inverted assignment if (s[i]>=10 {s[i-1]+=s[i]/10;//carry Operation s[i]%=10;//takes the remainder, retains to that position}}if (s[0]>=10) {///highest bit >=10, then one extra bit for (i=ca+cb-2;i>0;i--) {// Traverse to move back s[i+1]=s[i];} s[1]=s[0]%10;//the second high number is the remainder s[0]/=10;} I=0;for (j=0;i<ca+cb-1;i++,j++)//traversal all the numbers in the array to save the product c[j]=s[i]+ ' 0 ';//Refresh the digital product c[j]= ' + ';//Add End flag free (s);//Release Memory}
Run:




2. Today is the day of the week

Requirements: Enter a valid date to determine the day of the week

Idea: Calculate the number of days from the year of the year A.D. January 1, divided by 7 to convert to day of the week

#include <stdio.h> #include <stdlib.h>char date[7][7]={"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};int Isleapyear (int year) {if (year%4==0&&year%100!=0| | year%400==0) return 1;elsereturn 0;} int main () {int year,month,day;int year1[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};int year2[13]={ 0,31,29,31,30,31,30,31,31,30,31,30,31};int Days=0,k=0,j=0;//days is the number of days from the first day of the A.D. Char *getdate;while (scanf ("%d%d", &year,&month,&day)!=eof) {for (k=1;k<year;k++) {//calculates the number of days separated from the first day of A.D. if (Isleapyear (k)) days+=366;elsedays +=365; } for (j=1;j<month;j++) {if (Isleapyear (year)) days+=year2[j];elsedays+=year1[j];} days+=day;//plus the number of days past this month getdate=date[(day)%7];//divided by 7 to find the remainder of printf ("%s\n", getDate); Days=0;} System ("pause");    
Run:



3. Four-bit square number

Ask for a four-bit square number and the first two digits of the number are the same, and the last two digits are the same and differ from each other

#include <stdio.h>int issquare (int n), int main () {int a,b,num;for (a=1;a<=9;a++) {///first two digits for (b=0;b<=9;b++) { The latter two digits num=a*1000+a*100+b*10+b;//the current A/b corresponds to the number if (Issquare (num) &&a!=b) printf ("%d\n", num);}}    return 0;} int issquare (int n)//Determines whether a number is an integer squared function {int i,sum=0;for (i=1;; i+=2) {///squared is an attribute of n^2=1+3+5+...+ (2*n-1) sum+=i;if (sum>=n) break;} if (sum==n) return 1;elsereturn 0;}

Run:



4. Question of candidate votes

There are three candidates for Mr. Soccer, named "a", "B" and "C", which are scored as 5,3,2 according to the order in which they were numbered on the ballot paper by the voters. Please program the number of votes and voting results from the keyboard, count their scores, and output who is elected Mr. Football. Please note: If there are two identical codes on the same ticket, then "This ticket is invalid" and if the top two points are the same, the "re-vote" should be displayed, otherwise three candidates will be shown the score and output "Congratulations XX elected Mr. Football!" ”


Train of Thought: (1) candidate structure construction
(2) votes
(3) Statistical scoring and sequencing
(4) Judging, showing the result


#include <stdio.h> #include <stdlib.h> #define M 100//Poll Max # # 3//number of candidates struct CAN{CHAR code;  code int score; Score}can[n],st;int Main () {int n,c=0;//n is the actual voter turnout, C is a valid number of votes int I,j,k;char temp[n+1];//The intermediate variable that holds the vote int s[n]={5,3,2};for (i=0;i <n;i++) {printf ("Enter code for%d candidates:", i+1), scanf ("%c", &can[i].code);//Enter candidate character code fflush (stdin);//clear carriage return can[i]. score=0; } printf ("Enter poll Number:"), scanf ("%d", &n), for (i=0;i<n;i++) {printf ("Enter%d Ballot:", i+1), scanf ("%s", &temp); if ( temp[0]==temp[1]| | temp[1]==temp[2]| | TEMP[0]==TEMP[2]//Invalid ballot printf ("The Ticket is invalid:%s\n", temp); else{c++;//valid votes plus 1for (j=0;j<n;j++) {//Traversal lookup for (k=0;k<n;k++ if (Can[k].code==temp[j]) can[k].score+=s[j];//score accumulate}}}for (i=0;i<n-1;i++)//score Sort: Select Sort method {k=i;for (j=i+1;j<n;j+ +) {if (Can[j].score>can[k].score) k=j;} St=can[i];can[i]=can[k];can[k]=st;} if (can[0].score==can[1].score) printf ("The top two scored the same, are all%d points, please vote again!") \ n ", Can[0].score); else{printf (" candidate \ t score \ n "); for (i=0;i<n;i++) printf ("%c\t%d\n ", Can[i].code,can[i].score); printf ("Congratulations to%c for being elected Mr. Soccer!" \ n ", can[0].code);}    System ("pause"); return 0;}


Run:



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C Language (7)--high-precision addition, subtraction, multiplication, today is the day of the week, four square number, the candidate vote question

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.