#(the cout<
Explore tagged Tumblr posts
dont-open-dead-inside-net · 8 months ago
Text
i'm literally so cool...
Tumblr media
3 notes · View notes
sokosun · 2 years ago
Text
printf の書式は分かるのに std::cout で桁揃えする方法を思い出せないときに見るエントリ
概要
printf の書式はソラで書けるけど std::cout で同じことをする方法が出てこない俺のためのチートシート。特に std::format が使えない環境のための覚書。
書式
#include <iostream> #include <iomanip> // std::setw, std::setfill #include <bitset> // std::hex #include <format> // C++20
int i = 123;
// Output: "i = 123(0x007b)" printf("i = %4d(0x%04x)\n", i, i); std::cout << "i = " << std::setfill(' ') << std::setw(4) << i << "(0x" << std::setfill('0') << std::setw(4) << std::hex << i << ")" << std::endl; std::cout << std::format("i = {:4d}(0x{:04x})", i, i) << std::endl; // C++20
double d = 12.345;
// Output: "d = 012.35" printf("d = %06.2f\n", d); std::cout << "d = " << std::setfill('0') << std::setw(6) << std::fixed << std::setprecision(2) << d << std::endl; std::cout << std::format("d = {:06.2f}", d) << std::endl; // C++20
補足
左寄せ, 両寄せを使��する場合は #include して std::left, std::right, std::internal を適宜追加
std::showbase を使用すると std::setw の値が読みにくくなるので使わない
std::format は同じ引数を複数回参照できるが読みにくくなるので使わない
空白埋めだけでよいなら std::setfill は不要
末尾の改行なしで即時出力したいなら std::endl の代わりに std::flush を使う
0 notes
chelseasprojects · 4 years ago
Text
Creation of Program to Calculate Hotel Rate
April 2018 at Best Western, I used C++ to create a program that would calculate the base rate for a package.
The purpose was to make the hotel auditors’ job more efficient by only needing to enter values given on the PMS, instead of manually calculating. 
{ int i; for (i = 0; i < 20; i++) //loops 20 times {
float n; float scp; std:: cout<< fixed; std:: cout<< setprecision(2); //previous 2 lines rounds demical to 2 places   cout << “Enter the combined room rate plus tax for DY reservations \n”;   cin >> n;   scp = (n - 16) / 1.12;   //computes the SCP total   cout << “This is the new room rate\n”<< scp << “\nEnter this rate in rate detail.\n”;   cout << “Select SCP under the multiple tap.\n\n” ;
  //made by ck 106 for Best Western Cocoa Beach }
}
1 note · View note
shalala66 · 5 years ago
Text
ML(Machine Learning) and AIN(Analyze of Intellectual Resources)
One of the dynamic methods of regression(linear, logical, robust, etc.) to find criterial variables via using predictors(regressors). It's been created by myself with the C++ codes to get result of core algorithm:
#include <iostream> #include <iomanip> #define half_s 20 #define half_ss 10
float train[half_s] = {1.1, 1.3, 1.5, 2.0, 2.2, 2.9, 3.0, 3.2, 3.2, 3.7, 3.9, 4.0, 4.0, 4.1, 4.5, 4.9, 5.1, 5.3, 5.9, 6.0}; float test[half_ss] = {6.8, 7.1, 7.9, 8.2, 8.7, 9.0, 9.5, 9.6, 10.3, 10.5};
float train1[half_s] = {39343.0, 46205.0, 37731.0, 43525.0, 39891.0, 56642.0, 60150.0, 54445.0, 64445.0, 57189.0, 63218.0,                        55794.0, 56957.0, 57081.0, 61111.0, 67938.0, 66029.0, 83088.0, 81363.0, 93940.0};
float test1[half_ss] = {91738.0, 98273.0, 101302.0,113812.0, 109431.0, 105582.0,116969.0, 112635.0, 122391.0, 121872.00};
int main(int argc, char** argv) {    //X/2 - train    int i;    float sum_train_x = 0.0;    int count = 0;
   for(i = 0; i < half_s; ++i)    {        sum_train_x += train[i];        ++count;    }
   //X/2 - train1    float sum_train_y = 0.0;    for(i = 0; i < half_s; ++i)    {        sum_train_y += train1[i];    }
   //X/2 - train * train    float sum_train_x2 = 0.0;
   for(i = 0; i < half_s; ++i)    {        sum_train_x2 += train[i] * train[i];    }
   //X/2 - train * train1    float sum_train_xy = 0.0;
   for(i = 0; i < half_s; ++i)    {        sum_train_xy += train[i] * train1[i];    }
   //X/2 - test    float sum_test_x = 0.0;    int count1 = 0;
   for(i = 0; i < half_ss; ++i)    {        sum_test_x += test[i];        ++count1;    }
   //X/2 - test1    float sum_test_y = 0.0;    for(i = 0; i < half_ss; ++i)    {        sum_test_y += test1[i];    }
   //X/2 - test * test    float sum_test_x2 = 0.0;    for(i = 0; i < half_ss; ++i)    {        sum_test_x2 += test[i] * test[i];    }
   //X/2 - test * test1    float sum_test_xy = 0.0;
   for(i = 0; i < half_ss; ++i)    {        sum_test_xy += test[i] * test1[i];    }
   //Linear regression    float a, b;    float a1, b1;
   //a = (sum_y * sum_2x - sum_x * sum_xy) / (count * sum_2x - sum_x * sum_x);    a = (sum_train_y * sum_train_x2 - sum_train_x * sum_train_xy) / (count * sum_train_x2 - sum_train_x * sum_train_x);    a1 = (sum_test_y * sum_test_x2 - sum_test_x * sum_test_xy) / (count1 * sum_test_x2 - sum_test_x * sum_test_x);    std::cout << std::fixed;    std::cout << std::setprecision(4) << "a = " << a << std::endl << "a1 = " << a1 << std::endl;
   //b = (count * sum_xy - sum_x * sum_y) / (count * sum_2x - sum_x * sum_x);    b = (count * sum_train_xy - sum_train_x * sum_train_y) / (count * sum_train_x2 - sum_train_x * sum_train_x);    b1 = (count1 * sum_test_xy - sum_test_x * sum_test_y) / (count1 * sum_test_x2 - sum_test_x * sum_test_x);    std::cout << std::fixed;    std::cout << std::setprecision(4) << "b = " << b << std::endl << "b1 = " << b1 << std::endl;
   float h, q;    std::cout << "Enter the 'h' : ";    std::cin >> h;    q = b * h + a;    std::cout << "q = " << q << std::endl;
   float h1, q1;    std::cout << "Enter the 'h1' : ";    std::cin >> h1;    q1 = b1 * h1 + a1;    std::cout << "q1 = " << q1;
   return 0; }
Tumblr media
Then, extracted to SPDE(Spyder Python Development Enviroment - Anaconda Navigator) for a visualization response:
Tumblr media
0 notes
simplyprogramming · 7 years ago
Text
C++ I/O
need to add #include <iostream>
cout << “Length = “ << length << endl; // endl starts a new line
cout << setprecision(int) // sets floating point output to 2 decimal places
cout << fixed << scientific
cout.setf(ios::fixed); // to unset
cout << setw(int) // int is number of columns (auto right aligned unless << left)
cout.setfill(char) // sets the empty columns to the char
Strings:
>> stops at blanks so use getline(cin,varStr) // this stops at \n
cin >> feet >> inches; // skips all the leading whitespace characters
cin.get(varChar); // gets the input char and stores it in varChar
cin.ignore(intExp, charExp); // ignores the next intExp chars in the input or until it reaches charExp
cin.peak();
cin.putback(char); // puts the last character extracted from the input stream by get() back
int a, b; cin >> a >> g; // if input is 5.0 6, 5 is a but . tries to be in b so it goes into fail state. To fix this use cin.clear() and then ignore the rest of the line
0 notes
cis-170-blog · 8 years ago
Link
DEVRY CIS 170 C iLab 1 of 7 Getting Started
 Check this A+ tutorial guideline at
 http://www.cis170entirecourse.com/cis-170/cis-170-c-ilab-1-of-7-getting-started
For more classes visit
http://www.cis170entirecourse.com
 CIS 170 C iLab 1 of 7 Getting Started
Lab 1 of 7: Getting Started (Your First C++ Programs) Lab Overview - Scenario/Summary
Welcome to Programming with C++. The purpose of this three-part lab is to walk you through the following tutorial to become familiar with the actions of compiling and executing a C++ program.
In general, this lab will instruct you on:
how to create a project; how to enter and save a program; how to compile and run a program; how to, given a simple problem using input and output, code and test a program that meets the specifications; and how to debug a simple program of any syntax and logic errors.Deliverables
Section
Deliverable
Points
Part A
Step 6: Program Listing and Output
15
Part B
Program Listing and Output
15
Part C
Program Listing and Output
15
Lab Steps
Preparation:
If you are using the Citrix remote lab, follow the login instructions located in the iLab tab in Course Home.
Lab:
Part A: Getting Started
Step 1: Start the Application
From the File menu, choose "New Project." Choose “Win32 Console Application.” Enter a name in the name field. Click “Next” and choose the following options: Application Type: "Console Application" Additional options: Check mark “Empty project” and uncheck 8. Click Finish. Your project is now created.
Step 2: How to Add a Source Code File to Your Project (.cpp file)
In the Solution Explorer, right-click on the “Source Files” folder and select "Add" and then "New Item." In the next dialog box, choose C++ file (.cpp), enter a name for your source code file, and press the Add button. Type or copy and paste your code into the newly created source code file. Build the file by pressing F7, and then execute your program by pressing CTRL-F5 (start without debugging) or use the F5 key (Start Debugging).
Step 3: Create a Source Code File
Now enter the following C++ program exactly as you see it. Use the tab where appropriate. [Note: C++ is case sensitive.] Instead of John Doe, type your name.
#include
using namespace std;
void main()
{
cout< "john="" doe"=""><endl;="">
cout< "cis170c="" -="" programming="" using="" c++\n";="">
cout< "\n\n\nhello,="" world!\n\n";="">
}
When you execute a program in debug mode (F5), the console screen may appear and disappear
before you have an opportunity to view your output. There are several techniques you can use to
pause the console screen so you can read the output. On the very last line in the main() function:
a. insert the statement: system("pause");
-OR-
b. insert an input statement: cin<myvarable;="">
Step 4: Output
The black screen or console should read:
John Doe
CIS170C - Programming using C++ Hello, World -
Step 5: Save Program
Save your program by clicking File on the menu bar and then clicking Save Program.cpp, or by clicking the Save button on the toolbar, or Ctrl + S.
Step 6: Build Solution
To compile the program, click Build on the menu bar and then click the BuildSolution or Build LabA option. You should receive no error messages. If you see some error messages, check the code above to make sure you didn't key in something wrong. Once you make your corrections to the code, go ahead and click Build >> Build Solution again.
Step 7: Execute the Program
Once you have no syntax errors, to execute or run your program, click Debug on the menu bar, and then click Start Without Debugging.
Step 8: Capture the Output
Print a picture of your screen output. (Do a print screen and paste this into MS Word.)
Step 9: Print the Source Code
Copy your source code and paste it into the same Word document as your screen print. Save the Word Document as Lab01A_LastName_FirstInitial
Note: Using the Visual Studio editor to compile your programs creates a lot of overhead. These additional files will become important as you create more sophisticated C# projects. Projects can contain one or more source-code files. For this course, you will not have to worry about all the extra files that are created.
End of Part A
Part B: Calculate Total Tickets
Step 1: Create New Project
Now create a new project and name it LAB1B. Make sure you close your previous program by clicking File >> Close Solution.
Step 2: Type in Program
Like before, enter the following program. Type in your name for Developer and current date for Date Written.
// ---------------------------------------------------------------
// Programming Assignment: LAB1B
// Developer: ______________________
// Date Written: ______________________
// Purpose: Ticket Calculation Program
// ---------------------------------------------------------------
#include
using namespace std;
void main()
{
intchildTkts, adultTkts, totalTkts;
;
;
+ adultTkts;
cout<totaltkts=""><endl;="">
}
Step 3: Save Program
Save your program by clicking File on the menu bar and then clicking Save Program.cpp, or by clicking the Save button on the toolbar, or Ctrl + S.
Step 4: Build Solution
To compile the program, click Build on the menu bar and then click the BuildSolution or Build LabB option. You should receive no error messages. If you see some error messages, check the code above to make sure you didn't key in something wrong. Once you make your corrections to the code, go ahead and click Build >> Build Solution again.
Step 5: Execute the Program
Once you have no syntax errors, to execute or run your program, click Debug on the menu bar, and then click Start Without Debugging.
Step 6: Capture the Output
Capture a screen print of your output. (Do a PRINT SCREEN and paste into an MS Word document.) Copy your code and paste it into the same MS Word document that contains the screen print of your output. 3. Save the Word Document as Lab01B_LastName_FirstInitial.
End of Part B
Part C: Payroll Program
Step 1: Create a New Project
Create a new project and name it LAB1C. Make sure you close your previous program by clicking File >> Close Solution.
Include a comment box like what you coded in Part B. This can go at the very top of your program.
Step 2: Processing Logic
You need to write a program that calculates and displays the take-home pay for a commissioned sales employee along with all of the deductions.
Input: Prompt the user for the weekly sales.
Process: Perform the calculations. The employee receives 7% of his or her total sales as his or her gross pay. His or her federal tax rate is 18%. He or she contributes 10% to his or her retirement program and 6% to Social Security.
Output: Display the results
Sample Output from Lab 1: Enter Weekly Sales: 28000
Total Sales: 28000.00 Gross pay (7%): 1960.00
Federal tax paid: 352.80 Social security paid: 117.60 Retirement contribution: 196.00 Total deductions: 666.40 Take home pay: 1293.60 Press any key to continue . . .
Flowchart: (continued on next page)
Pseudo Code:
1. Declare variables 2. Accept Input - weeklySales 3. Calculate Gross Sales * .07 4. Calculate Federal Pay * .18 5. Calculate Social Pay * .06 6. Calculate Pay * .1 7. Calculate Total Tax + Social Security + Retirement 8. Calculate Total Take Home Pay - Total Deductions 9. Display the following on separate lines and format variables with $ and decimal. a. Total Sales Amount: value of weekly sales b. Gross Pay (.07): value of gross pay c. Federal Tax paid (.18): value of federal tax d. Social Security paid (.06): value of social security e. Retirement contribution (.1): value of retirement f. Total Deductions: value of total deductions g. Take Home Pay: value of take home pay
Note: Use SetPrecisions(2) to format the output (see page 98 of the text). The statements should look something like the following:
//include the iomanip header file at the top of the file
#include
//use fixed and setprecision(2) to format the number
//use setw(8) to control the width of the field
//use \t to control the spacing between fields
cout< fixed=""><setprecision(2);="">
cout< "gross="" pay="" (0.07):\t="" $"=""><setw(8)=""><grosspay=""><endl;="">
  m
0 notes
mmkumr · 8 years ago
Text
'setprecision()' function and 'fixed' keyword in C++
        Today I learnt about funtion ‘setprecision()’ and keyword 'fixed’. The keyword is used when you want to display the float or double variable in the form of fixed point notation. Ex:- 383.34569    The keyword scientific is used when you want to display the float or double in the form of scientific notation. Ex:- 384.00123e+003
        The fuction is used when you need to set the limit of digits after decimal. Ex:- cout << setprecision(2) << 2033.4423211; Then output will be displayed as 2033.44.    
        The nice thing about 'scientific’, 'fixed’ and 'setprecision()’ is there is no need of using this function and keyword again and again you have to use these keyword and function only once. You can simply use by 'cout << scientific;’ or 'cout << default’ or 'cout << setprecision(value);’.
0 notes